#include "boss_ai.h" #include "npc_depths.h" // Its burrow: how near a target keeps it under, and how long it stays out. constexpr float COD_WORM_HIDE_REACH_SQUARED = 22500; constexpr uint32_t COD_WORM_OUT_SPREAD = 40; constexpr int COD_WORM_OUT_MIN = 80; constexpr float COD_WORM_FRICTION = 0.7f; // Its spit, and the anger hits on its shell stoke. constexpr float COD_WORM_CONTROL_REACH = 180; constexpr float COD_WORM_ARC = 10; constexpr uint32_t COD_WORM_SHOTS_SPREAD = 3; constexpr int COD_WORM_SHOTS_MIN = 5; constexpr int COD_WORM_ANGER_MAX = 30; // A controlled worm's charge: it builds under cover with no player near, and drains out of it. constexpr float COD_WORM_CHARGE_FRAMES = 150; constexpr float COD_WORM_CHARGE_DRAIN = 1.5f; constexpr float COD_WORM_CHARGE_REACH = 150; constexpr float COD_WORM_CHARGE_ATTACK = 0.8f; constexpr float COD_WORM_CHARGE_COST = 0.2f; constexpr float COD_WORM_CONTROL_RANGE = 1000; constexpr float COD_WORM_CONTROL_DEADZONE = 0.1f; // Under its shell it pulses, and comes out (for a while) once its target is away or out of sight. Confused, it always // comes out. static void cod_worm_hide(Entity_NPC& npc) { npc.sprite.Play("Pulse"); if (npc.stateFrame > 0) npc.stateFrame -= npc_ticks_get(npc); bool isStaying; if (npc.controllerID == -1) { Entity* target = npc.GetPlayerTarget(); bool isWatched = vector_length_squared(vector_sub(target->pos, npc.pos)) <= COD_WORM_HIDE_REACH_SQUARED && room_check_line(npc.pos, npc.GetPlayerTarget()->pos, LINE_MODE_PROJECTILE, LINE_CHECK_THRESHOLD); isStaying = isWatched || npc.stateFrame > 0; } else isStaying = npc.v1.x < 1 && !npc.IsControllerActionPressed(CONTROLLER_ACTION_SHOOT); if (isStaying && !npc.HasEntityFlags(FLAG_CONFUSION)) return; npc.state = NPC_STATE_JUMP; npc.stateFrame = (int)(Random() % COD_WORM_OUT_SPREAD) + COD_WORM_OUT_MIN; npc.sprite.Play("DigOut"); } // Out of its shell it pulses, open to hits, then digs back in (a controlled one when it attacks again). static void cod_worm_out(Entity_NPC& npc) { if (npc_anim_is_done(npc)) npc.sprite.Play("PulseOut"); npc.stateFrame -= npc_ticks_get(npc); bool isDigging; if (npc.controllerID == -1) isDigging = npc.stateFrame < 1; else { const AnimationData* data = npc.sprite.animState.animData; isDigging = data && data->isLooping && npc.v1.x < COD_WORM_CHARGE_ATTACK && (npc.IsControllerActionPressed(CONTROLLER_ACTION_SHOOT) || npc.IsTargetInAttackRange(COD_WORM_CONTROL_RANGE)); } if (!isDigging) return; npc.state = NPC_STATE_STOMP; npc.sprite.Play("DigIn"); npc.v1.x = std::max(npc.v1.x - COD_WORM_CHARGE_COST, 0.0f); } // Its spit: a burst of five to eight shots lobbed at its target (or where its controller points). static void cod_worm_spit(Entity_NPC& npc) { npc.sprite.Play("Attack"); npc.UpdateControllerMovement(false); if (npc.sprite.animState.animData) { if (npc.sprite.animState.IsEventTriggered("Shoot")) { npc.PlaySound(SOUND_BLOODSHOOT, 1, 2, false, 1); Vector aim = npc.GetPlayerTarget()->pos; if (npc.controllerID != -1) aim = vector_add(vector_scale(npc.controllerMovement, COD_WORM_CONTROL_REACH), npc.pos); float arc = random_unit() * COD_WORM_ARC; int count = (int)(Random() & COD_WORM_SHOTS_SPREAD) + COD_WORM_SHOTS_MIN; npc.FireBossProjectiles(count, aim, arc, ProjectileParams()); } if (!npc_anim_is_done(npc)) return; } npc.state = NPC_STATE_IDLE; npc.sprite.Play("Pulse"); } // Hits on its shell do nothing but anger it into spitting, longer for harder hits. static void cod_worm_shell_hit(Entity_NPC& npc, bool isShielded) { float blocked = 0; if (isShielded) for (DamageEvent& event : npc.damageEvents) { if (event.damageFlags & DAMAGE_POISON_BURN) continue; blocked += event.damage; event.damage = 0; } bool isAngered = (npc.state == NPC_STATE_IDLE || npc.state == NPC_STATE_ATTACK) && blocked > 0 && !npc.HasEntityFlags(FLAG_FEAR | FLAG_SHRINK); if (npc.controllerID != -1 || !isAngered) return; npc.state = NPC_STATE_ATTACK; npc.stateFrame = std::min(npc.stateFrame + (int)blocked * 2, COD_WORM_ANGER_MAX); } // A controlled worm spits when its target is in range, and charges under cover away from the players. static void cod_worm_control_update(Entity_NPC& npc, bool isShielded) { npc.UpdateChargeBar(1, 0, npc.v1.x, true); if (npc.state == NPC_STATE_IDLE && npc.IsTargetInAttackRange(COD_WORM_CONTROL_RANGE)) { npc.controllerMovement = npc.GetControllerMovementInput(COD_WORM_CONTROL_DEADZONE); npc.state = NPC_STATE_ATTACK; } float rate = npc.speedMultiplier / COD_WORM_CHARGE_FRAMES; if (!isShielded) npc.v1.x = std::max(npc.v1.x - npc.speedMultiplier * COD_WORM_CHARGE_DRAIN / COD_WORM_CHARGE_FRAMES, 0.0f); else if (npc.IsPlayerInRadius(npc.pos, COD_WORM_CHARGE_REACH)) npc.v1.x = std::max(npc.v1.x - rate, 0.0f); else npc.v1.x = std::min(npc.v1.x + rate, 1.0f); } // 00531350 NpcAI::ENTITY_COD_WORM: the Cod Worm hides in its invincible shell while watched, pops out when left // alone, and spits bursts of shots when hit. void npc_cod_worm_update(Entity_NPC& npc) { if (npc.state == NPC_STATE_INIT) { npc.state = NPC_STATE_IDLE; npc.targetPosition = npc.pos; npc.isInvincible = true; return; } npc.isInvincible = false; npc.velocity = vector_sub(npc.targetPosition, npc.pos); npc.friction *= COD_WORM_FRICTION; if (npc.state == NPC_STATE_IDLE) cod_worm_hide(npc); else if (npc.state == NPC_STATE_ATTACK) cod_worm_spit(npc); else if (npc.state == NPC_STATE_JUMP) cod_worm_out(npc); else if (npc.state == NPC_STATE_STOMP && npc_anim_is_done(npc)) npc.state = NPC_STATE_IDLE; const AnimationData* data = npc.sprite.animState.animData; bool isShielded = npc.state == NPC_STATE_IDLE || npc.state == NPC_STATE_ATTACK || npc.state == NPC_STATE_STOMP || (npc.state == NPC_STATE_JUMP && (!data || !data->isLooping)); if (npc.pendingDamage > 0) cod_worm_shell_hit(npc, isShielded); if (npc.controllerID != -1) cod_worm_control_update(npc, isShielded); if (npc.state == NPC_STATE_JUMP) npc.ClearEntityFlags(FLAG_NO_TARGET); else npc.AddEntityFlags(FLAG_NO_TARGET); if (npc.sprite.animState.animData && npc.sprite.animState.IsEventTriggered("Dig")) npc_sound_play(SOUND_BIRD_FLAP, 1, 2, champion_pitch_get(npc)); }