#include "npc_basement.h" #include "boss_ai.h" #include "game.h" #include "room.h" #include #include // The hives this AI runs: the Hive (with its drowned, holy and tainted variants), the Nest and the Willo hive. constexpr int ENTITY_NEST = 205; constexpr int ENTITY_WILLO_HIVE = 817; constexpr int HIVE_DROWNED = 1; constexpr int HIVE_HOLY = 2; constexpr int HIVE_TAINTED = 3; // What they let loose: flies (a Pooter or attack fly now and then), drowned chargers, holy babies, rings of flies, // spiders, big spiders and Trites, Willos and leeches. constexpr int ENTITY_EMBRYO = 77; // How they fidget: evading their target with a buzz now and then, and resting 20 to 69 frames once 4 to 8 enemies // are about. Within 300 they attack, then rest 75 (the Nest 130 to 149, the tainted Hive 210), else 3. constexpr int SOUND_HIVE_BUZZ = 143; constexpr int SOUND_HIVE_SHOOT = 158; constexpr int BUZZ_DELAY_MIN = 45; constexpr int BUZZ_DELAY_RANGE = 100; constexpr int REST_CROWD_MIN = 4; constexpr int REST_CROWD_RANGE = 5; constexpr int REST_MIN = 20; constexpr int REST_RANGE = 50; constexpr float ATTACK_RANGE = 300; constexpr float ATTACK_REST = 75; constexpr int NEST_REST_MIN = 55; constexpr int NEST_REST_RANGE = 20; constexpr float TAINTED_REST = 210; constexpr float CALM_REST = 3; constexpr float HIVE_FRICTION = 0.8f; constexpr float DROWNED_FRICTION = 0.95f; constexpr float WALK_THRESHOLD = 0.1f; // The holy Hive's baby flies out sideways at 8 in a burst of blood. constexpr float BABY_SPEED = 8; constexpr float BABY_BLOOD_RISE = -20; // As it dies it lets loose up to 4 (fewer the more enemies are about), scattered 2 to 6 around it. constexpr float DEATH_SPAWNS = 4; constexpr float DEATH_SPAWN_CROWD = 0.2f; constexpr int DEATH_FLY_ODDS = 11; constexpr int DEATH_FLY_POOTER = 7; constexpr int DEATH_FLY_ATTACK = 8; constexpr float DEATH_SCATTER_MIN = 2; constexpr float DEATH_SCATTER = 6; constexpr float HIVE_OVERKILL = -20; static Entity* spawn_free(Entity_NPC& npc, int type, int variant, const Vector& position, const Vector& velocity, int subType) { uint32_t seed = Random(); Entity* spawned = g_game->Spawn({.position = position, .velocity = velocity, .spawner = &npc, .subType = subType, .type = type, .variant = variant, .seed = seed}); spawned->ClearEntityFlags(FLAG_APPEAR); return spawned; } // The first frame: walking. The Willo hive's head rises, the drowned one swims. static void hive_init(Entity_NPC& npc) { if (npc.type == ENTITY_WILLO_HIVE) { npc.sprite.PlayOverlay("HeadAppear"); if (npc.variant == 1) { npc.posOffset.y = -6; npc.entityGridCollisionClass = GRIDCOLL_NOPITS; } } npc.state = NPC_STATE_MOVE; npc.v1.x = 0; } // Walking: it evades a hostile target (buzzing while it does) or wanders, its head bobbing along. static void hive_walk_update(Entity_NPC& npc) { if (!npc.IsHostileTo(npc.GetPlayerTarget())) npc.pathfinder.MoveRandomly(STATUS_IGNORED); else { npc.pathfinder.EvadeTarget(npc.CalcTargetPosition(0), STATUS_OBEYED); if (npc.pathfinder.evadeMovementCountdown > 0) npc.PlaySound(SOUND_HIVE_BUZZ, 1, (int)(Random() % BUZZ_DELAY_RANGE) + BUZZ_DELAY_MIN, false, 1); } if (npc.type == ENTITY_WILLO_HIVE && npc.variant == 1) { npc.sprite.Play("WalkVert"); if (std::abs(npc.velocity.y) < std::abs(npc.velocity.x)) npc.sprite.isFlipX = npc.velocity.x < 0; } else npc.AnimWalkFrame("WalkHori", "WalkVert", WALK_THRESHOLD); if (!npc.sprite.animState.animData || !npc.sprite.animState.isPlaying) { if (npc.sprite.SetOverlayAnimation("HeadWalk", SWITCH_KEEP_FRAME) && npc.sprite.overlayAnimState.animData) npc.sprite.overlayAnimState.SetFrameFloat(0); } else npc.sprite.PlayOverlay("HeadWalk"); npc.friction *= HIVE_FRICTION; if (npc.type == ENTITY_HIVE && npc.variant == HIVE_DROWNED) npc.friction *= DROWNED_FRICTION; } // The Shoot event of its attack lets one out: a fly (a spider from a Nest) that dies on touch, a drowned charger at // half health, a holy baby, a ring of flies, or a Willo hive's Willo or leech. static void hive_release(Entity_NPC& npc) { npc.PlaySound(SOUND_HIVE_SHOOT + (npc.type == ENTITY_NEST), 1, 2, false, 1); if (npc.type == ENTITY_WILLO_HIVE) { spawn_free(npc, npc.variant == 1 ? ENTITY_WILLO : ENTITY_SMALL_LEECH, 0, npc.pos, {}, 0); return; } if (npc.type == ENTITY_HIVE && npc.variant == HIVE_DROWNED) { Entity* charger = spawn_free(npc, ENTITY_CHARGER, 1, npc.pos, {}, 0); charger->maxHitPoints *= 0.5f; charger->health = charger->maxHitPoints; return; } if (npc.type == ENTITY_HIVE && npc.variant == HIVE_HOLY) { Vector velocity = vector_make(npc.sprite.isFlipX ? -BABY_SPEED : BABY_SPEED, 0); spawn_free(npc, ENTITY_BABY, 1, vector_add(npc.pos, velocity), velocity, 1); blood_explosion_spawn(2, npc.pos, vector_make(0, npc.scale * BABY_BLOOD_RISE), COLOR_DEFAULT, velocity); return; } if (npc.type == ENTITY_HIVE && npc.variant == HIVE_TAINTED) reconstruction_missing("NpcAI::ENTITY_HIVE 0050c370: the tainted Hive's ring of flies"); Entity* fly = spawn_free(npc, npc.type == ENTITY_HIVE ? ENTITY_ATTACKFLY : ENTITY_SPIDER, 0, npc.pos, {}, 0); fly->isKillOnTouch = true; } // Off rest while evading, it attacks a target within 300 (resting a short while otherwise), facing it. static void hive_attack_start(Entity_NPC& npc) { if (npc.v1.x > 0) { npc.v1.x -= npc.speedMultiplier; return; } float facing = npc.GetPlayerTarget()->pos.x - npc.pos.x; float distance = vector_length(vector_sub(npc.GetPlayerTarget()->pos, npc.pos)); if (distance >= ATTACK_RANGE || npc.HasEntityFlags(FLAG_DISTRACTED)) { npc.v1.x = CALM_REST; return; } npc.v1.x = ATTACK_REST; if (npc.type == ENTITY_NEST) npc.v1.x += (float)((int)(Random() % NEST_REST_RANGE) + NEST_REST_MIN); if (npc.type == ENTITY_HIVE && npc.variant == HIVE_TAINTED) npc.v1.x = TAINTED_REST; npc.controllerMovement = {}; if (npc.sprite.SetOverlayAnimation("HeadAttack", SWITCH_KEEP_FRAME)) { npc.sprite.overlayAnimState.Rewind(); npc.sprite.overlayAnimState.isPlaying = true; } npc.state = NPC_STATE_ATTACK; npc.sprite.isFlipX = facing < 0; } // As it dies outright it lets loose its brood: flies (now and then a Pooter or attack fly) or the drowned and holy // kinds' own, a Nest's big spider or Trite, a Willo hive's Willo. Fewer the more enemies are about. static void hive_death(Entity_NPC& npc) { int count = (int)std::ceil(DEATH_SPAWNS / ((float)npc.GetAliveEnemyCount() * DEATH_SPAWN_CROWD + 1)); if (npc.type == ENTITY_HIVE && (npc.variant == HIVE_DROWNED || npc.variant == HIVE_HOLY)) count = std::min((int)(Random() & 1) + 1, count); else if ((npc.type == ENTITY_HIVE && npc.variant == HIVE_TAINTED) || npc.type == ENTITY_NEST) count = 1; else if (npc.type == ENTITY_WILLO_HIVE) count = std::max(count - 1, 1); count = std::max(count - (int)npc.championRegenCount, 0); for (int i = 0; i < count; i++) { if (npc.type == ENTITY_HIVE && npc.variant == HIVE_TAINTED) reconstruction_missing("NpcAI::ENTITY_HIVE 0050c370: the tainted Hive's ring of flies"); if (npc.type == ENTITY_WILLO_HIVE) { if (npc.variant != 1) reconstruction_missing("NpcAI::ENTITY_HIVE 0050c370: a Willo hive's leech (ThrowLeech)"); spawn_free(npc, ENTITY_WILLO, 0, npc.pos, {}, 0); continue; } if (npc.type != ENTITY_HIVE) { int variant = (int)(Random() & 1); spawn_free(npc, variant ? ENTITY_HOPPER : ENTITY_BIG_SPIDER, variant, npc.pos, {}, 0); continue; } uint32_t roll = Random() % DEATH_FLY_ODDS; int type = roll >= DEATH_FLY_ATTACK ? ENTITY_ATTACKFLY : roll == DEATH_FLY_POOTER ? ENTITY_POOTER : ENTITY_FLY; int variant = 0; if (npc.variant == HIVE_DROWNED) { type = ENTITY_CHARGER; variant = 1; } else if (npc.variant == HIVE_HOLY) type = ENTITY_EMBRYO; float angle = random_unit() * PI_F; angle += angle; float reach = std::max(random_unit() * DEATH_SCATTER, DEATH_SCATTER_MIN); Vector at = vector_make(std::cos(angle) * reach + npc.pos.x, std::sin(angle) * reach + npc.pos.y); Entity* spawned = spawn_free(npc, type, variant, at, {}, 0); if (npc.variant != HIVE_DROWNED) continue; spawned->maxHitPoints *= 0.5f; spawned->health = spawned->maxHitPoints; } } // 0050c370 The hives' AI: they shuffle about evading their target, buzzing, and now and then (more often with fewer // enemies about) stop to let a fly or spider loose at it. As they die they let their brood go. void npc_hive_update(Entity_NPC& npc) { if (npc.state == NPC_STATE_INIT) return hive_init(npc); if (npc.controllerID != -1) reconstruction_missing("NpcAI::ENTITY_HIVE 0050c370: a hive under a player's control"); if (npc.state == NPC_STATE_MOVE) hive_walk_update(npc); else { if (npc.type == ENTITY_HIVE && (npc.variant == HIVE_HOLY || npc.variant == HIVE_TAINTED)) { npc.friction *= HIVE_FRICTION; npc.AnimWalkFrame("WalkHori", "WalkVert", WALK_THRESHOLD); } if (npc.sprite.overlayAnimState.IsEventTriggered("Shoot")) hive_release(npc); if (!npc.sprite.overlayAnimState.animData || !npc.sprite.overlayAnimState.isPlaying) npc.state = NPC_STATE_MOVE; } int crowd = npc.GetAliveEnemyCount(); if (npc.type == ENTITY_HIVE && npc.variant == HIVE_TAINTED) reconstruction_missing("NpcAI::ENTITY_HIVE 0050c370: the tainted Hive's own flies left out of the crowd"); if (npc.v1.x - npc.speedMultiplier <= 0 && (int)(Random() % REST_CROWD_RANGE) + REST_CROWD_MIN <= crowd) npc.v1.x += (float)((int)(Random() % REST_RANGE) + REST_MIN); if (npc.pathfinder.evadeMovementCountdown > 0) hive_attack_start(npc); if (npc.isDead && npc.health > HIVE_OVERKILL) hive_death(npc); }