#include "boss_ai.h" #include "entity_projectile.h" #include "room_grid.h" #include "sfx_manager.h" #include // The Duke of Flies (variant 0) and the Husk (variant 1), and their subtypes. constexpr int HUSK = 1; constexpr int DUKE_MOTERS = 1; constexpr int DUKE_SUCKERS = 2; constexpr int HUSK_BOOM_FLY = 1; constexpr int HUSK_RED_BOOM_FLY = 2; constexpr int RED_BOOM_FLY = 1; // The Duke's attacks, by state: the fly burst (blowing its flies away), the fly cough and the big fly. constexpr int DUKE_BURST = NPC_STATE_ATTACK; constexpr int DUKE_COUGH = NPC_STATE_SUMMON; constexpr int DUKE_BIG_FLY = NPC_STATE_SUMMON2; constexpr float CEILING_MARGIN = 40; constexpr float DUKE_SPEED = 0.4f; constexpr float HUSK_SPEED = 0.8f; constexpr float RED_HUSK_SPEED = 1.1f; constexpr float BOOM_HUSK_SPEED = 0.5f; constexpr float DRIFT_DOWN_THRESHOLD = 0.1f; constexpr float DUKE_FRICTION = 0.8f; constexpr float SUCKER_DUKE_FRICTION = 0.9f; constexpr float DUKE_FLEE_RANGE = 180; constexpr float DUKE_FLEE_STRENGTH = 0.02f; constexpr float DUKE_FLEE_MAX = 1; constexpr uint32_t COUGH_ODDS = 40; constexpr uint32_t BIG_FLY_ODDS = 70; constexpr uint32_t DUKE_BURST_ODDS = 45; constexpr uint32_t LONE_BURST_ODDS = 70; constexpr uint32_t RED_HUSK_BURST_ODDS = 20; constexpr int COUGH_LIMIT = 9; constexpr int BIG_FLY_LIMIT = 10; constexpr int BURST_FLIES = 5; constexpr int BURST_OTHERS = 4; constexpr int ATTACK_FRAMES = 39; constexpr int BURST_FRAME = 17; constexpr float DUKE_BURST_PUSH = 18; constexpr float TINY_FLY_PUSH = 3; constexpr float NEAR_SQUARED = 0.01f; constexpr float HUSK_RING_HEIGHT = 40; constexpr float HUSK_RING_SPEED = 8; constexpr float MOTER_RING_SPEED = 7; constexpr int DUKE_RING_SHOTS = 8; constexpr int COUGH_FRAME = 20; constexpr float WING_TURN = 90; constexpr float WING_HEIGHT = -40; constexpr ColorMod WING_COLOR = {{0, 0, 0, 0.5f}, {0, 0, 0, 0}, {0, 0, 0}}; constexpr float MOUTH_DROP = 20; constexpr float FLY_GAP = 10; constexpr int COUGHED_FLIES = 3; constexpr int DELIRIUM_FLY_LIMIT = 6; constexpr int DELIRIUM_BOOM_FLY_LIMIT = 3; constexpr float MIDPOINT = 0.5f; constexpr float SPIDER_TARGET_REACH = 70; constexpr float SPIDER_HEIGHT = -30; constexpr int TINY_FLY_LIMIT = 25; constexpr uint32_t TINY_FLIES = 5; constexpr float TINY_FLY_SCATTER = 40; constexpr int BIG_FLY_FRAME = 18; constexpr int BIG_FLY_LIMIT_ALIVE = 5; constexpr float DUKE_BIG_FLY_SCALE = 1.25f; constexpr float BIG_FLY_HEALTH = 1.8f; constexpr int HUSK_SHOTS = 10; constexpr float DEATH_TINY_FLY_SCATTER = 50; constexpr uint32_t DEATH_TINY_FLIES = 10; constexpr uint32_t DEATH_TINY_FLIES_MIN = 5; constexpr int DEATH_FLIES = 6; constexpr uint32_t HUSK_DEATH_FLIES = 3; constexpr int HUSK_DEATH_FLIES_MIN = 4; constexpr float DEATH_FLY_SCATTER = 0.8f; // A spot scattered about a center, the angle rolled first. static Vector scatter_spot(const Vector& center, float reach) { Vector direction = random_direction(); float distance = random_unit() * reach; return vector_add(vector_scale(direction, distance), center); } // Pushes an entity straight away from the Duke by a speed, unless it sits on it. static void push_away(Entity_NPC& npc, Entity& entity, float speed) { Vector away = vector_sub(entity.pos, npc.pos); float distanceSquared = vector_length_squared(away); if (distanceSquared <= NEAR_SQUARED) return; float distance = std::sqrt(distanceSquared); Vector push = vector_scale(away, speed / distance); if (entity.friction != 0) entity.velocity = vector_add(entity.velocity, vector_scale(push, entity.speedMultiplier / entity.friction)); } // A minion that starts at once and dies on touch. static void minion_ready(Entity& minion) { minion.ClearEntityFlags(FLAG_APPEAR); minion.isKillOnTouch = true; } // It floats diagonally, bouncing off the walls below the ceiling margin, and keeps away from its target while feared // or shrunk. Confused (and not charmed), it wanders. static void duke_float(Entity_NPC& npc) { Room& room = *g_game->room; npc.entityGridCollisionClass = GRIDCOLL_WALLS; npc.pos = room.GetClampedPositionImpl(npc.pos, CEILING_MARGIN, 0, 0, 0); if (npc.pos.x == room.bottomRightPos.x) npc.velocity.x = -1; if (npc.pos.x == room.topLeftPos.x) npc.velocity.x = 1; if (npc.pos.y == room.bottomRightPos.y) npc.velocity.y = -1; if (npc.pos.y == room.topLeftPos.y + CEILING_MARGIN) npc.velocity.y = 1; float speed = DUKE_SPEED; if (npc.variant == HUSK) speed = npc.subtype == HUSK_RED_BOOM_FLY ? RED_HUSK_SPEED : npc.subtype == HUSK_BOOM_FLY ? BOOM_HUSK_SPEED : HUSK_SPEED; bool isSuckerDuke = npc.variant == 0 && npc.subtype == DUKE_SUCKERS; if (npc.HasEntityFlags(FLAG_CONFUSION) && !npc.HasEntityFlags(FLAG_CHARM)) { npc.friction *= DUKE_FRICTION; if (isSuckerDuke) npc.friction *= SUCKER_DUKE_FRICTION; npc.pathfinder.MoveRandomly(STATUS_OBEYED); return; } Vector drift = vector_make(npc.velocity.x > 0 ? 1 : -1, npc.velocity.y < DRIFT_DOWN_THRESHOLD ? -1 : 1); npc.friction *= DUKE_FRICTION; if (npc.friction != 0) npc.velocity = vector_add(npc.velocity, vector_scale(drift, speed * npc.speedMultiplier / npc.friction)); if (isSuckerDuke) npc.friction *= SUCKER_DUKE_FRICTION; if (!npc.HasEntityFlags(FLAG_FEAR | FLAG_SHRINK)) return; Vector away = vector_sub(npc.pos, npc.GetPlayerTarget()->pos); float distance = vector_length(away); float strength = std::clamp((DUKE_FLEE_RANGE - distance) * DUKE_FLEE_STRENGTH, 0.0f, DUKE_FLEE_MAX); if (distance > 0) away = vector_scale(away, strength / distance); if (npc.friction != 0) npc.velocity = vector_add(npc.velocity, vector_scale(away, npc.speedMultiplier / npc.friction)); } // While floating it may cough up flies, spit a big fly, or burst its flies away, by how many flies it keeps // (groupSize) and how crowded the room is. The red Husk never coughs. A calm Delirium form only floats. static void duke_choose(Entity_NPC& npc, int groupSize, int enemies) { bool isRedHusk = npc.variant == HUSK && npc.subtype == HUSK_RED_BOOM_FLY; bool isMoterDuke = npc.variant == 0 && npc.subtype == DUKE_MOTERS; std::string_view attack; if (!(npc.HasEntityFlags(FLAG_DELIRIUM_FORM) && room_is_clear())) { if (Random() % COUGH_ODDS == 0 && groupSize + enemies < COUGH_LIMIT && !isRedHusk) { npc.state = DUKE_COUGH; attack = "Attack01"; } else if (Random() % BIG_FLY_ODDS == 0 && enemies - groupSize < BIG_FLY_LIMIT && !isRedHusk) { npc.state = DUKE_BIG_FLY; attack = "Attack02"; } else { bool isBurst = Random() % DUKE_BURST_ODDS == 0 && groupSize >= BURST_FLIES && enemies - groupSize <= BURST_OTHERS && !isMoterDuke; if (!isBurst) { bool isLoneRoll = Random() % LONE_BURST_ODDS == 0; if (isLoneRoll) isBurst = npc.variant == HUSK || isMoterDuke; else isBurst = isRedHusk && Random() % RED_HUSK_BURST_ODDS == 0; } if (isBurst) { npc.state = DUKE_BURST; attack = "Attack03"; } } } if (attack.empty()) npc.sprite.Play("Walk"); else if (npc.sprite.SetAnimation(attack, SWITCH_KEEP_FRAME)) { npc.sprite.animState.Rewind(); npc.sprite.animState.isPlaying = true; } npc.stateFrame = 0; } // The burst: its flies leave its group and are blown away with the tiny flies about, and the Husk and the Moter // Duke shoot a ring of eight. static void duke_burst(Entity_NPC& npc, const std::vector& group) { npc.PlaySound(SOUND_MONSTER_GRUNT_4, 1, 2, false, 1); for (Entity_NPC* fly : group) { fly->parent = nullptr; fly->groupIdx = -1; push_away(npc, *fly, DUKE_BURST_PUSH); } for (Entity* tinyFly : entity_query(ENTITY_EFFECT, EFFECT_TINY_FLY)) push_away(npc, *tinyFly, TINY_FLY_PUSH); ProjectileParams params; if (npc.variant == HUSK) npc.FireProjectiles(vector_make(npc.pos.x, npc.pos.y - HUSK_RING_HEIGHT), vector_make(HUSK_RING_SPEED, DUKE_RING_SHOTS), PROJECTILES_RING, params); else if (npc.subtype == DUKE_MOTERS) npc.FireProjectiles(npc.pos, vector_make(MOTER_RING_SPEED, DUKE_RING_SHOTS), PROJECTILES_RING, params); } // An attack fly coughed up into its group, at a spot below its mouth. static void fly_cough(Entity_NPC& npc, const Vector& spot, std::vector& coughed) { coughed.push_back(g_game->Spawn( {.position = spot, .spawner = &npc, .type = ENTITY_ATTACKFLY, .seed = Random()})); } // The cough: two wing gusts, then three attack flies that join its group (the Husk throws a spider for the third), // a boom fly for the boom Husk instead, and a few tiny flies while the room has few of them. static void duke_cough(Entity_NPC& npc) { npc.PlaySound(SOUND_MONSTER_GRUNT_2, 1, 2, false, 1); for (int i = 0; i < 2; i++) { auto* gust = static_cast(g_game->Spawn( {.position = npc.pos, .spawner = &npc, .type = ENTITY_EFFECT, .variant = EFFECT_BOMB_EXPLOSION, .seed = Random()})); gust->rotation = i == 0 ? WING_TURN : -WING_TURN; gust->AddEntityFlags(FLAG_NO_BLOOD_SPLASH); gust->SetColor(WING_COLOR, -1, COLOR_PRIORITY_DEFAULT, false, true); gust->posOffset = vector_make(0, WING_HEIGHT); gust->Update(); } Vector mouth = vector_make(npc.pos.x, npc.pos.y + MOUTH_DROP); if (npc.variant == HUSK && npc.subtype == HUSK_BOOM_FLY) { bool isRoom = !npc.HasEntityFlags(FLAG_DELIRIUM_FORM) || npc.CountRelatedEntities(ENTITY_BOOMFLY, 0, nullptr) < DELIRIUM_BOOM_FLY_LIMIT; if (isRoom) g_game->Spawn({.position = mouth, .spawner = &npc, .type = ENTITY_BOOMFLY, .seed = Random()}); } else { int count = COUGHED_FLIES; if (npc.HasEntityFlags(FLAG_DELIRIUM_FORM)) count = DELIRIUM_FLY_LIMIT - npc.CountRelatedEntities(ENTITY_ATTACKFLY, 0, nullptr); std::vector coughed; if (count > 0) fly_cough(npc, mouth, coughed); mouth.x += FLY_GAP; if (count > 1) fly_cough(npc, mouth, coughed); mouth.x -= 2 * FLY_GAP; if (npc.variant == 0 && count > 2) fly_cough(npc, mouth, coughed); else if (npc.variant == HUSK && count > 2) { Vector between = vector_add(vector_scale(npc.pos, MIDPOINT), vector_scale(npc.GetPlayerTarget()->pos, MIDPOINT)); Vector landing = room_free_pickup_position_find({.position = between, .step = SPIDER_TARGET_REACH}); Entity_NPC::ThrowSpider(npc.pos, &npc, landing, false, SPIDER_HEIGHT); } for (Entity* fly : coughed) { static_cast(fly)->groupIdx = (int)npc.index; fly->parent = &npc; minion_ready(*fly); } } if (entity_query(ENTITY_EFFECT, EFFECT_TINY_FLY).size() >= TINY_FLY_LIMIT) return; Vector center = vector_make(npc.pos.x, npc.pos.y + MOUTH_DROP); for (uint32_t count = Random() % TINY_FLIES + 1; count > 0; count--) { uint32_t seed = Random(); Vector heading = random_direction(); Vector spot = scatter_spot(center, TINY_FLY_SCATTER); Entity* tinyFly = g_game->Spawn({.position = spot, .velocity = heading, .spawner = &npc, .type = ENTITY_EFFECT, .variant = EFFECT_TINY_FLY, .seed = seed}); tinyFly->parent = &npc; tinyFly->ClearEntityFlags(FLAG_APPEAR); tinyFly->Update(); } } // The big fly: the Duke spits one big attack fly (a Moter or Sucker by subtype) while fewer than five of its kind // are its own, the Husk a spray of ten shots at its target. static void duke_big_fly(Entity_NPC& npc) { npc.PlaySound(SOUND_MONSTER_GRUNT_1, 1, 2, false, 1); if (npc.variant == HUSK) { ProjectileParams params; npc.FireBossProjectiles(HUSK_SHOTS, npc.GetPlayerTarget()->pos, 0, params); return; } if (npc.variant != 0) return; int kind = npc.subtype == DUKE_MOTERS ? ENTITY_MOTER : npc.subtype == DUKE_SUCKERS ? ENTITY_SUCKER : ENTITY_ATTACKFLY; if (npc.CountRelatedEntities(kind, 0, nullptr) >= BIG_FLY_LIMIT_ALIVE) return; auto* fly = static_cast(g_game->Spawn({.position = vector_make(npc.pos.x, npc.pos.y + MOUTH_DROP), .spawner = &npc, .type = kind, .seed = Random()})); minion_ready(*fly); if (npc.subtype != 0) return; fly->scale *= DUKE_BIG_FLY_SCALE; float size = fly->scale * fly->shrinkStatus1; fly->sprite.scale = vector_make(size, size); fly->maxHitPoints *= BIG_FLY_HEALTH; fly->health *= BIG_FLY_HEALTH; } // A fly of the death burst, about it, a plain or attack fly as its own seed's generator rolls. static void death_fly_spawn(Entity_NPC& npc, RNG& rng, int kind, int variant) { Vector direction = random_direction(); float distance = random_unit() * DEATH_FLY_SCATTER * npc.size; Vector spot = vector_add(vector_scale(direction, distance), npc.pos); uint32_t seed = Random(); if (kind == ENTITY_NULL) kind = rng.Next() & 1 ? ENTITY_FLY : ENTITY_ATTACKFLY; g_game->Spawn({.position = spot, .spawner = &npc, .type = kind, .variant = variant, .seed = seed}) ->ClearEntityFlags(FLAG_APPEAR); } // Its death: five to fourteen tiny flies, then six flies, or for the Husk four to six and one or two pooters (boom // flies for the boom Husks, red for the red one). static void duke_death(Entity_NPC& npc) { uint32_t tinyFlies = 0; do { uint32_t seed = Random(); Vector spot = scatter_spot(npc.pos, DEATH_TINY_FLY_SCATTER); g_game->Spawn({.position = spot, .spawner = &npc, .type = ENTITY_EFFECT, .variant = EFFECT_TINY_FLY, .seed = seed}); tinyFlies++; } while (tinyFlies < Random() % DEATH_TINY_FLIES + DEATH_TINY_FLIES_MIN); RNG rng = {npc.initSeed, 5, 9, 7}; if (npc.variant != HUSK) { for (int i = 0; i < DEATH_FLIES; i++) death_fly_spawn(npc, rng, ENTITY_NULL, 0); return; } for (int count = (int)(rng.Next() % HUSK_DEATH_FLIES) + HUSK_DEATH_FLIES_MIN; count > 0; count--) death_fly_spawn(npc, rng, ENTITY_NULL, 0); int kind = npc.subtype == HUSK_BOOM_FLY || npc.subtype == HUSK_RED_BOOM_FLY ? ENTITY_BOOMFLY : ENTITY_POOTER; int variant = npc.subtype == HUSK_RED_BOOM_FLY ? RED_BOOM_FLY : 0; for (int count = (int)(rng.Next() & 1) + 1; count > 0; count--) death_fly_spawn(npc, rng, kind, variant); } // 0046e290 NpcAI::ENTITY_DUKE: the Duke of Flies and the Husk float about the room, coughing up flies that circle // them, spitting big flies and blowing their flies away. They burst into flies when they die. void boss_duke_update(Entity_NPC& npc) { duke_float(npc); std::vector group = Entity_NPC::QueryNPCsGroup((int)npc.index); int groupSize = (int)group.size(); int enemies = g_game->room->GetAliveEnemiesCount(); switch (npc.state) { case NPC_STATE_INIT: npc.state = NPC_STATE_MOVE; break; case NPC_STATE_MOVE: duke_choose(npc, groupSize, enemies); break; case DUKE_BURST: if (npc.stateFrame == BURST_FRAME) duke_burst(npc, group); break; case DUKE_COUGH: if (npc.stateFrame == COUGH_FRAME) duke_cough(npc); break; case DUKE_BIG_FLY: if (npc.stateFrame == BIG_FLY_FRAME) duke_big_fly(npc); break; } if (++npc.stateFrame > ATTACK_FRAMES) npc.state = NPC_STATE_MOVE; if (npc.isDead) duke_death(npc); }