#include "npc_caves.h" #include "sfx_manager.h" #include "boss_ai.h" #include "entity_bomb.h" #include "entity_effect.h" #include "entity_familiar.h" #include "entity_player.h" #include "room_grid.h" #include #include // The maggot kin this AI runs: the Maggot, the Charger (drowned, dank, the Carrion Princess and My Shadow), the Spitty // (and its tainted variant), the conjoined Spitty, the Leech (kamikaze and holy) and the Gurglings and Turdling. constexpr int CHARGER_DROWNED = 1; constexpr int CHARGER_DANK = 2; constexpr int CHARGER_CARRION = 3; constexpr int MY_SHADOW_SUBTYPE = 1; constexpr int SPITTY_TAINTED = 1; constexpr int LEECH_HOLY = 2; constexpr int GURGLING_PLAIN = 0; constexpr int GURGLING_PAIR_SUBTYPE = 1; constexpr int GURGLING_BOMBER_SUBTYPE = 2; constexpr int DIP_CORN_VARIANT = 2; constexpr int STAIN_CLIMBABLE_STATE = NPC_STATE_SPECIAL; constexpr ColorMod TURDLING_SPLAT_COLOR = {{0, 0, 0, 1}, {0, 0, 0, 0}, {0.6f, 0.4f, 0}}; constexpr ColorMod DANK_CHARGER_SPLAT_COLOR = {{0, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0}}; // The animations by facing: sideways, up, down. The Gurgling's run up, down, left and right. using FacingAnims = std::array; constexpr FacingAnims MOVE_ANIMS = {"Move Hori", "Move Up", "Move Down"}; constexpr FacingAnims LEECH_ANIMS = {"Hori", "Up", "Down"}; constexpr FacingAnims DASH_ANIMS = {"DashHori", "DashUp", "DashDown"}; constexpr FacingAnims ATTACK_ANIMS = {"Attack Hori", "Attack Up", "Attack Down"}; constexpr FacingAnims CONJOINED_MOVE_ANIMS = {"Move Hori", "Move Vert", "Move Vert"}; constexpr FacingAnims CONJOINED_ATTACK_ANIMS = {"Attack Hori", "Attack Down", "Attack Down"}; constexpr FacingAnims ALERT_ANIMS = {"Alert Hori", "Alert Up", "Alert Down"}; constexpr FacingAnims RECOVER_ANIMS = {"Recover Hori", "Recover Up", "Recover Down"}; constexpr std::array GURGLING_WALK_ANIMS = {"WalkVert", "WalkVert", "WalkLeft", "WalkRight"}; constexpr std::array GURGLING_CHARGE_ANIMS = {"ChargeUp", "ChargeDown", "ChargeLeft", "ChargeRight"}; enum Facing : int { FACING_SIDE, FACING_UP, FACING_DOWN }; // They wander along the grid's axes (the drowned Charger slowest) and keep their cell marked for the pathfinder. constexpr float MAGGOT_SPEED = 0.75f; constexpr float CHARGER_SPEED = 0.6f; constexpr float DROWNED_SPEED = 0.54f; constexpr float SPITTY_SPEED = 0.72f; constexpr float LEECH_SPEED = 0.85f; constexpr float MAGGOT_FRICTION = 0.85f; constexpr float MAGGOT_FRICTION_Y = 0.9647058f; constexpr int MAGGOT_GRID_PATH = 900; constexpr float STAIN_CHASE_SCALE = 1.4f; // The Stain's mound lifts and tilts the maggots that crawl over it. constexpr float STAIN_SQUASH = 0.6f; constexpr int STAIN_CLIMB_DEPTH = 1400; constexpr float STAIN_TILT = 35; constexpr float STAIN_CLIMB_HEIGHT = 26; // A charger charges every fourth frame at a target within 200 in line with one of its axes in a clear line (a leech // at any range), aiming ahead of it. constexpr int CHARGE_CHECK_FRAMES = 4; constexpr int CONTROLLER_ACTION_CHARGE = 9; constexpr int CONTROLLER_ACTION_BURST = 8; constexpr float CHARGE_LEAD = 16; constexpr float CHARGE_MARGIN = 20; constexpr float CHARGE_AXIS = 20; constexpr float CHARGE_RANGE_SQUARED = 40000; constexpr float CHARGE_INPUT_SCALE = 100; constexpr float MAGGOT_CHARGE_SPEED = 1.3f; constexpr float LEECH_CHARGE_SPEED = 1.55f; constexpr float GURGLING_PAIR_CHARGE_SCALE = 1.5f; constexpr float DROWNED_CHARGE_SCALE = 0.9f; constexpr float DANK_CHARGE_SCALE = 14; constexpr int MAGGOT_CHARGE_SOUND_DELAY = 20; // The Carrion Princess charges along a diagonal: at a target within 20 of one of its cell's diagonal lanes (20 to // either side of its cell's middle) in a clear line to 10 short of it, kept on that lane. constexpr float CARRION_TURN = 45; constexpr float CARRION_LANE_WIDTH = 20; constexpr float CARRION_LANE_SIDE = 20; constexpr float CARRION_END_SHORT = 10; // A charge stops at the grid or a status, when it stalls for five frames (moving less than its speed's root of two), // or when it slows to a crawl while slowed. constexpr float CHARGE_STALL_SQUARED = 2; constexpr int CHARGE_STALL_FRAMES = 5; constexpr float CHARGE_SLOW_SPEED = 3; constexpr float CHARGER_CHARGE_FRICTION = 0.8f; constexpr float CHARGE_FRICTION = 0.9f; constexpr float CHARGE_ALIGN_FRICTION = 0.7f; // The dank Charger lunges after its alert, bouncing off walls and slowing down, leaving creep, until it recovers. constexpr float DANK_FRICTION = 0.8f; constexpr float DANK_STEER_FRICTION = 0.2f; constexpr int DANK_RECOVER_FRAMES = 45; constexpr float DANK_RECOVER_SPEED = 2; constexpr float DANK_SLOWDOWN_START = 10; constexpr float DANK_SLOWDOWN_FRAMES = 70; constexpr float DANK_SLOWDOWN = 0.06f; constexpr float DANK_ALIGN = 0.1f; constexpr float DANK_KEEP = 0.9f; constexpr int DANK_SPLAT_ODDS = 10; constexpr float DANK_SPLAT_SIZE = 0.2f; constexpr float DANK_SPLAT_RANGE = 0.5f; constexpr int DANK_CREEP_SKIP_FRAMES = 3; constexpr int DANK_CHARGER_CREEP_TIMEOUT = 90; constexpr float DANK_CHARGER_CREEP_SCALE = 0.8f; // The Spitty spits now and then (one in 35 checks) along its facing, the conjoined one both ways, the tainted one a // spray. The tainted one also lobs a few shots while walking and bursts into two rings as it dies. constexpr int SPITTY_ATTACK_ODDS = 35; constexpr float SPITTY_CONTROLLED_RANGE = 1000; constexpr int SPITTY_RANGE_FRAMES = 7; constexpr float SPIT_HEIGHT = 14; constexpr float SPIT_FALL_SPEED = -0.1f; constexpr float SPIT_FALL_ACCEL = -0.101f; constexpr float SPITTY_SHOT_SPEED = 8; constexpr int SPRAY_SHOTS = 10; constexpr float SPRAY_HEIGHT = 10; constexpr float SPRAY_SCALE = 1.7f; constexpr float SPRAY_SCALE_STEP = 0.1f; constexpr float SPRAY_ACCEL_STEP = 0.03f; constexpr float SPRAY_FALL_STEP = 0.005f; constexpr float SPRAY_FALL = 0.08f; constexpr float SPRAY_SPEED = 18; constexpr float SPRAY_JITTER_HALF = 0.5f; constexpr float TAINTED_LOB_TRAJECTORY = 10; constexpr int TAINTED_LOB_SHOTS = 3; constexpr float TAINTED_RING_SCALE = 1.25f; constexpr float TAINTED_RING_FALL = -0.07f; constexpr float TAINTED_RING_SPEED = 8; constexpr float TAINTED_RING_SHOTS = 8; constexpr float TAINTED_RING_TURN = 0.3926991f; // The dying drowned Charger bursts into a cross of shots, the kamikaze and holy Leeches explode. constexpr float DROWNED_BURST_SPEED = 8; constexpr float DROWNED_HARD_BURST_SPEED = 10; constexpr float LEECH_BLAST_DAMAGE = 40; constexpr float LEECH_BLAST_RADIUS = 60; // A thrown maggot flies off with its throw until it leaves the room. constexpr float THROWN_FRICTION = 0.9f; constexpr float THROWN_EXIT_MARGIN = 80; // The Gurgling boss pair: its two halves bring partners, it shrugs its own kind's explosions off to 5 damage, and // its runs leave red creep (the Turdling brown, farting now and then) and its third form trolls bombs. constexpr float GURGLING_SELF_BLAST_DAMAGE = 5; constexpr float GURGLING_PARTNER_STEP = 60; constexpr float GURGLING_TRAIL_SPEED_SQUARED = 9; constexpr float GURGLING_TRAIL_INTERVAL = 5; constexpr float GURGLING_SPLAT_SIZE = 0.2f; constexpr float GURGLING_SPLAT_RANGE = 0.39f; constexpr int TURDLING_CREEP_VARIANT = 94; constexpr int TURDLING_FART_ODDS = 3; constexpr float TURDLING_FART_RANGE = 0.3f; constexpr float TURDLING_FART_VOLUME = 0.5f; constexpr float GURGLING_CREEP_SCALE = 1.23f; constexpr float GURGLING_BOMB_INTERVAL = 15; constexpr int GURGLING_BOMB_GRID_PATH = 900; constexpr float GURGLING_RING_SPEED = 7.5f; constexpr float GURGLING_RING_SHOTS = 6; constexpr int TURDLING_DIPS = 4; constexpr float TURDLING_DIP_TURN = 90; constexpr int TURDLING_ENEMY_LIMIT = 8; constexpr float TURDLING_DIP_SPEED = 10; constexpr float TURDLING_DIP_GAP = 5; constexpr float TURDLING_DIP_HEALTH = 0.1f; constexpr float TURDLING_FART_REACH = 120; constexpr float TURDLING_SOUND_VOLUME = 0.8f; constexpr int TURDLING_SOUND_DELAY = 10; constexpr int GURGLING_FLY_ODDS = 3; constexpr float GURGLING_FLY_REACH = 40; constexpr float GURGLING_FLY_SPEED = 2; // A summoning maggot (one Ultra Greed and the like set to summon) drops a dip behind it. constexpr float SUMMON_DIP_REACH = 8; constexpr float SUMMON_DIP_SPEED = 6; // My Shadow keeps its distance from its own kind: a push from each within 15 to 30, harder the faster it runs. constexpr RNG SHADOW_RNG = {0, 2, 7, 25}; constexpr float SHADOW_RANGE = 15; constexpr float SHADOW_PUSH_BASE = 0.4f; constexpr float SHADOW_PUSH_MIN = 0.8f; constexpr float SHADOW_PUSH_MAX = 2; constexpr float SHADOW_PUSH_FLOOR = 0.1f; // What one update of a maggot works with: the Stain it climbs, its cell, pace and facing, and whether it charges. struct MaggotFrame { Entity* stain = nullptr; float speed = CHARGER_SPEED; int cell = -1; int facing = FACING_SIDE; bool isCharger = false; bool isClimbing = false; bool isDank = false; }; // The Gurgling pair: a lead Gurgling brings partners until there are half again as many as the lead ones. static void gurgling_partners_spawn(Entity_NPC& npc) { std::vector pair = entity_query(ENTITY_GURGLING, GURGLING_PAIR_SUBTYPE); int leads = 0; for (Entity* gurgling : pair) if (gurgling->spawnerType == 0) leads++; int wanted = (leads + 1) / 2 + leads; for (int i = (int)pair.size(); i < wanted; i++) { uint32_t seed = Random(); Vector spot = room_free_pickup_position_find({.position = npc.pos, .step = GURGLING_PARTNER_STEP}); g_game->Spawn({.position = spot, .spawner = &npc, .subType = npc.subtype, .type = npc.type, .variant = npc.variant, .seed = seed}) ->Update(); } } // The first frame: it starts wandering. The holy Leech brings its eternal fly, the lead Gurgling its partners, and the // Turdling and dank Charger their blood colors. The update ends there. static void maggot_init(Entity_NPC& npc) { if (npc.type == ENTITY_LEECH) { if (npc.variant == LEECH_HOLY) g_game->Spawn({.position = npc.pos, .spawner = &npc, .type = ENTITY_ETERNALFLY, .seed = Random()})->parent = &npc; npc.entityGridCollisionClass = GRIDCOLL_WALLS; } npc.state = NPC_STATE_MOVE; bool isLeadGurgling = npc.type == ENTITY_GURGLING && npc.isBoss && npc.spawnerType == 0 && npc.subtype == GURGLING_PAIR_SUBTYPE; if (isLeadGurgling) gurgling_partners_spawn(npc); if (npc.type == ENTITY_GURGLING && npc.variant == GURGLING_TURDLING) npc.splatColor = TURDLING_SPLAT_COLOR; else if (npc.type == ENTITY_CHARGER && npc.variant == CHARGER_DANK) npc.splatColor = DANK_CHARGER_SPLAT_COLOR; } // The dying drowned Charger's cross of shots, and the kamikaze and holy Leeches' explosion. A shrunk one only dies. static void maggot_death_burst(Entity_NPC& npc) { if (npc.HasEntityFlags(FLAG_SHRINK)) return; if (npc.type == ENTITY_LEECH && npc.variant != 0) { explosion_damage({.position = npc.pos, .source = &npc, .damage = LEECH_BLAST_DAMAGE, .radius = LEECH_BLAST_RADIUS}); g_game->Spawn({.position = npc.pos, .type = ENTITY_EFFECT, .variant = EFFECT_BOMB_EXPLOSION, .seed = Random()}); } else if (npc.type == ENTITY_CHARGER && npc.variant == CHARGER_DROWNED) { ProjectileParams params; float speed = game_is_hard_mode() ? DROWNED_HARD_BURST_SPEED : DROWNED_BURST_SPEED; npc.FireProjectiles(npc.pos, vector_make(speed, 0), PROJECTILES_CROSS, params); } } // The nearest Stain mound that can be climbed (one in its special state), if any. static Entity* stain_find(Entity_NPC& npc) { Entity* nearest = nullptr; float best = 0; for (Entity* stain : entity_query(ENTITY_STAIN, -1)) { if (static_cast(stain)->state != STAIN_CLIMBABLE_STATE) continue; float distance = vector_length_squared(vector_sub(stain->pos, npc.pos)); if (nearest && distance >= best) continue; nearest = stain; best = distance; } return nearest; } // Over a Stain mound it draws lifted and tilted to the mound's slope (by the mound's ClimbPos height), in front of it. // Off it, it draws plainly. Whether it climbs. static bool stain_climb(Entity_NPC& npc, Entity* stain) { npc.sprite.offset = {}; npc.renderZOffset = 0; npc.sprite.rotation = 0; if (!stain) return false; float dx = stain->pos.x - npc.pos.x; float dy = stain->pos.y / STAIN_SQUASH - npc.pos.y / STAIN_SQUASH; float distance = vector_length(vector_make(dx, dy)); if (distance >= stain->size) return false; npc.renderZOffset = STAIN_CLIMB_DEPTH; float tilt = dx / stain->size * STAIN_TILT * (1 - std::abs(dy / stain->size)); float reach = distance / stain->size; float lift = 1 - reach * reach; const NullFrame* climb = stain->sprite.animState.animData ? stain->sprite.animState.GetNullFrame("ClimbPos") : nullptr; if (climb) { npc.sprite.offset = vector_make(0, lift * climb->pos.y); tilt *= climb->pos.y / STAIN_CLIMB_HEIGHT; } npc.sprite.rotation = tilt; npc.friction *= MAGGOT_FRICTION; return true; } // The pace it wanders at. static float maggot_speed_get(Entity_NPC& npc) { if (npc.type == ENTITY_MAGGOT) return MAGGOT_SPEED; if (npc.type == ENTITY_LEECH) return LEECH_SPEED; if (npc.type == ENTITY_SPITTY || npc.type == ENTITY_CONJOINED_SPITTY) return SPITTY_SPEED; if (npc.type == ENTITY_CHARGER && npc.variant == CHARGER_DROWNED) return DROWNED_SPEED; return CHARGER_SPEED; } // The facing its animations use: the Gurgling's four ways by its motion, the others' three by their charge (while // charging) or motion, flipped and tilted to face left (but while spitting). static int maggot_facing_get(Entity_NPC& npc, bool isDank) { if (npc.type == ENTITY_GURGLING) { Vector v = npc.velocity; if (std::abs(v.x) > std::abs(v.y)) return v.x >= 0 ? 3 : 2; return v.y >= 0 ? 1 : 0; } bool isCharging = npc.state == NPC_STATE_ATTACK || (isDank && npc.state == NPC_STATE_SPECIAL); Vector v = isCharging ? npc.v1 : npc.velocity; if (std::abs(v.x) <= std::abs(v.y)) return v.y >= 0 ? FACING_DOWN : FACING_UP; if (npc.state == NPC_STATE_ATTACK2) return FACING_SIDE; npc.sprite.isFlipX = v.x < 0; if (npc.sprite.isFlipX) npc.sprite.rotation *= -1; return FACING_SIDE; } // A thrown maggot flies on with its throw, and is gone once it leaves the room sideways. static void maggot_thrown_update(Entity_NPC& npc) { if (vector_is_zero(npc.v1)) npc.v1 = npc.targetPosition; npc.friction *= THROWN_FRICTION; npc.AddVelocity_divided(npc.v1, false); Room& room = *g_game->room; bool isRightGone = npc.velocity.x > 0 && npc.pos.x > room.bottomRightPos.x + THROWN_EXIT_MARGIN; bool isLeftGone = npc.velocity.x < 0 && room.topLeftPos.x - THROWN_EXIT_MARGIN > npc.pos.x; if (isRightGone || isLeftGone) npc.Remove(); } // The Turdling's dips: four corny ones flung out around it (fewer while a Delirium form has some), or a fart with too // many enemies around. static void turdling_dips_throw(Entity_NPC& npc) { int count = npc.GetAliveEnemyCount() <= TURDLING_ENEMY_LIMIT ? TURDLING_DIPS : 0; if (npc.HasEntityFlags(FLAG_DELIRIUM_FORM)) count = std::min(count, TURDLING_DIPS - npc.CountRelatedEntities(ENTITY_DIP, DIP_CORN_VARIANT, nullptr)); if (count < 1) return game_butter_bean_fart({.position = npc.pos, .source = &npc, .radius = TURDLING_FART_REACH}); for (int i = 0; i < count; i++) { Vector direction = vector_from_angle((float)i * TURDLING_DIP_TURN); float sine = direction.y; float cosine = direction.x; uint32_t seed = Random(); Vector velocity = vector_make(cosine * TURDLING_DIP_SPEED, sine * TURDLING_DIP_SPEED); float reach = npc.size + TURDLING_DIP_GAP; Vector spot = vector_make(reach * cosine + npc.pos.x, sine * reach + npc.pos.y); Entity* dip = g_game->Spawn({.position = spot, .velocity = velocity, .spawner = &npc, .type = ENTITY_DIP, .variant = DIP_CORN_VARIANT, .seed = seed}); dip->ClearEntityFlags(FLAG_APPEAR); dip->AddVelocity_divided(velocity, false); dip->health = TURDLING_DIP_HEALTH; dip->Update(); } npc.PlaySound(SOUND_PLOP, TURDLING_SOUND_VOLUME, TURDLING_SOUND_DELAY, false, 1); } // Resting: it slows down, and the Gurglings act on their Shoot event (a ring of shots, the Turdling's dips, the boss's // attack flies). It wanders again once its animation ends. static void maggot_rest_update(Entity_NPC& npc) { npc.friction *= MAGGOT_FRICTION; AnimationState& anim = npc.sprite.animState; if (npc.type == ENTITY_GURGLING && anim.animData && anim.IsEventTriggered("Shoot")) { if (npc.variant == GURGLING_PLAIN) { npc.PlaySound(SOUND_BLOODSHOOT, 1, 2, false, 1); ProjectileParams params; npc.FireProjectiles(npc.pos, vector_make(GURGLING_RING_SPEED, GURGLING_RING_SHOTS), PROJECTILES_RING, params); } else if (npc.variant == GURGLING_TURDLING) turdling_dips_throw(npc); else if (npc.subtype == 0 && Random() % GURGLING_FLY_ODDS != 0) { uint32_t spawned = 0; do { Vector direction = random_direction(); uint32_t seed = Random(); Entity* fly = g_game->Spawn({.position = vector_add(vector_scale(direction, GURGLING_FLY_REACH), npc.pos), .velocity = vector_scale(direction, GURGLING_FLY_SPEED), .spawner = &npc, .type = ENTITY_ATTACKFLY, .seed = seed}); fly->ClearEntityFlags(FLAG_APPEAR); spawned++; } while (spawned < Random() % GURGLING_FLY_ODDS); } } if (npc_anim_is_done(npc)) npc.state = NPC_STATE_MOVE; } // Starts a charge at its target (or its controller's push): in line with one of its axes, close and in a clear line // from its cell, it screams and dashes along that axis. The dank one shows its alert first. // The nearer of the two diagonal lanes through a cell, 20 to either side of its middle, across a charge. static Vector carrion_lane_get(Vector position, Vector cell, Vector across) { Vector left = vector_make(cell.x - CARRION_LANE_SIDE, cell.y); Vector right = vector_make(cell.x + CARRION_LANE_SIDE, cell.y); float leftOffset = vector_dot(vector_sub(position, left), across); float rightOffset = vector_dot(vector_sub(position, right), across); return std::abs(rightOffset) <= std::abs(leftOffset) ? right : left; } // The Carrion Princess's charge along the diagonal its target lies on, from its lane in a clear line to the target. static void carrion_charge_try(Entity_NPC& npc, Vector cell, Vector spot, Vector delta) { Vector turned = vector_rotated(delta, CARRION_TURN); bool isOnDiagonal = std::abs(turned.x) < CARRION_LANE_WIDTH || std::abs(turned.y) < CARRION_LANE_WIDTH; if (!isOnDiagonal || npc.HasEntityFlags(FLAG_DISTRACTED)) return; Vector direction = vector_rotated(vector_cardinal(turned), -CARRION_TURN); Vector across = vector_rotated(direction, RIGHT_ANGLE_DEGREES); Vector lane = carrion_lane_get(npc.pos, cell, across); Vector start = vector_sub(npc.pos, vector_scale(across, vector_dot(vector_sub(npc.pos, lane), across))); Vector end = vector_sub(vector_sub(spot, vector_scale(across, vector_dot(vector_sub(spot, lane), across))), vector_scale(direction, CARRION_END_SHORT)); if (npc.controllerID == -1 && !room_check_line(start, end, LINE_MODE_CELL, 0)) return; npc.PlaySound(SOUND_MAGGOTCHARGE, 1, MAGGOT_CHARGE_SOUND_DELAY, false, 1); npc.v1 = vector_scale(direction, MAGGOT_CHARGE_SPEED); npc.state = NPC_STATE_ATTACK; npc.stateFrame = 0; npc.v2 = npc.pos; npc.pathfinder.gridPathTarget = npc.pos; npc.pathfinder.moveCountdown = 0; } // The Carrion Princess's charge: facing up or down its diagonal, kept on its lane. static void carrion_charge_steer(Entity_NPC& npc, Vector cell) { npc.sprite.Play(npc.v1.y >= 0 ? "Attack Down" : "Attack Up"); npc.sprite.isFlipX = npc.v1.x < 0; Vector across = vector_normalized(vector_rotated(npc.v1, RIGHT_ANGLE_DEGREES)); float offset = vector_dot(vector_sub(npc.pos, carrion_lane_get(npc.pos, cell, across)), across); float drift = vector_dot(npc.velocity, across); float keep = 1 - timescale_factor(CHARGE_ALIGN_FRICTION, npc.speedMultiplier); npc.velocity = vector_sub(npc.velocity, vector_scale(across, (offset + drift) * keep)); } static void maggot_charge_try(Entity_NPC& npc, MaggotFrame& frame) { Room& room = *g_game->room; Vector cell = room.GetGridPosition(frame.cell); Vector aim = npc.CalcTargetPosition(0); Vector lead = npc.GetPlayerTarget()->velocity; float share = random_unit(); Vector spot = room.GetClampedPosition(vector_add(vector_scale(vector_scale(lead, share), CHARGE_LEAD), aim), CHARGE_MARGIN); if (frame.stain) spot = frame.stain->pos; bool isControlled = npc.controllerID != -1; Vector delta = vector_sub(spot, cell); if (isControlled) delta = vector_scale(npc.GetControllerInput(0), CHARGE_INPUT_SCALE); if (npc.type == ENTITY_CHARGER && npc.variant == CHARGER_CARRION) return carrion_charge_try(npc, cell, spot, delta); if (npc.HasEntityFlags(FLAG_DISTRACTED) || frame.isClimbing) return; if (!isControlled) { if (std::abs(delta.x) >= CHARGE_AXIS && std::abs(delta.y) >= CHARGE_AXIS) return; if (vector_length_squared(delta) >= CHARGE_RANGE_SQUARED && npc.type != ENTITY_LEECH) return; bool isClear = npc.type == ENTITY_LEECH ? explosion_line_clear(cell, spot, false) : room_check_line(cell, spot, LINE_MODE_GROUND, 0); if (!isClear) return; } if (npc.type == ENTITY_LEECH) npc.PlaySound(SOUND_LEECH, 1, MAGGOT_CHARGE_SOUND_DELAY, false, 1); else if (npc.type == ENTITY_GURGLING) { npc.PlaySound(SOUND_CHILD_ANGRY_ROAR, 1, MAGGOT_CHARGE_SOUND_DELAY, false, 1); npc.PlaySound(SOUND_GURGLING_ATTACK, 1, 2, false, 1); } else npc.PlaySound(SOUND_MAGGOTCHARGE, 1, MAGGOT_CHARGE_SOUND_DELAY, false, 1); npc.v1 = vector_scale(vector_cardinal(delta), npc.type == ENTITY_LEECH ? LEECH_CHARGE_SPEED : MAGGOT_CHARGE_SPEED); if (npc.type == ENTITY_GURGLING) { if (npc.isBoss && npc.subtype == GURGLING_PAIR_SUBTYPE) npc.v1 = vector_scale(npc.v1, GURGLING_PAIR_CHARGE_SCALE); } else if (npc.type == ENTITY_CHARGER && npc.variant == CHARGER_DROWNED) npc.v1 = vector_scale(npc.v1, DROWNED_CHARGE_SCALE); else if (frame.isDank) npc.v1 = vector_scale(npc.v1, DANK_CHARGE_SCALE); npc.state = NPC_STATE_ATTACK; npc.pathfinder.gridPathTarget = npc.pos; npc.pathfinder.moveCountdown = 0; npc.stateFrame = 0; npc.v2 = npc.pos; if (!frame.isDank) return; frame.facing = FACING_SIDE; if (std::abs(npc.v1.x) <= std::abs(npc.v1.y)) frame.facing = npc.v1.y >= 0 ? FACING_DOWN : FACING_UP; else { npc.sprite.isFlipX = npc.v1.x < 0; if (npc.sprite.isFlipX) npc.sprite.rotation *= -1; } npc.sprite.Play(ALERT_ANIMS[frame.facing]); } // A Spitty starts spitting now and then (or when its controller aims), along its facing. static void spitty_attack_try(Entity_NPC& npc, const MaggotFrame& frame) { if (npc.type != ENTITY_SPITTY && npc.type != ENTITY_CONJOINED_SPITTY) return; bool isReady = npc.controllerID != -1 ? npc.IsTargetInAttackRange(SPITTY_CONTROLLED_RANGE, SPITTY_RANGE_FRAMES, false) : Random() % SPITTY_ATTACK_ODDS == 0; if (!isReady || npc.HasEntityFlags(FLAG_DISTRACTED)) return; npc.UpdateControllerMovement(true); npc.state = NPC_STATE_ATTACK2; npc.stateFrame = 0; const FacingAnims& anims = npc.type == ENTITY_SPITTY ? ATTACK_ANIMS : CONJOINED_ATTACK_ANIMS; npc.sprite.Play(anims[frame.facing], PLAY_FORCED); npc.i1 = frame.facing; } // Wandering: it walks along the grid's axes (toward a Stain mound to climb), the tainted Spitty lobbing shots, and // starts a charge or a spit when it can. static void maggot_wander_update(Entity_NPC& npc, MaggotFrame& frame) { if (npc.type == ENTITY_LEECH) npc.sprite.Play(LEECH_ANIMS[frame.facing]); else if (npc.type == ENTITY_GURGLING) npc.sprite.Play(GURGLING_WALK_ANIMS[frame.facing]); else if (npc.type == ENTITY_CONJOINED_SPITTY) npc.sprite.Play(CONJOINED_MOVE_ANIMS[frame.facing]); else npc.sprite.Play(MOVE_ANIMS[frame.facing]); if (!frame.stain) npc.pathfinder.MoveRandomlyAxisAligned(frame.speed, STATUS_OBEYED); else { npc.pathfinder.FindGridPath(frame.stain->pos, frame.speed * STAIN_CHASE_SCALE, 0, DIRECT_PATH_FAR); npc.velocity = vector_scale(vector_cardinal(npc.velocity), vector_length(npc.velocity)); } npc.friction *= MAGGOT_FRICTION; npc.velocity.y *= timescale_factor(MAGGOT_FRICTION_Y, npc.speedMultiplier); if (npc.type == ENTITY_SPITTY && npc.variant == SPITTY_TAINTED && npc.sprite.IsEventTriggered("Shoot")) { npc.PlaySound(SOUND_BLOODSHOOT, 1, 2, false, 1); ProjectileParams params; params = ProjectileParams(); int count = (int)(Random() & 1) + TAINTED_LOB_SHOTS; npc.FireBossProjectiles(count, {}, TAINTED_LOB_TRAJECTORY, params); } bool isReady = frame.isCharger && (npc.controllerID != -1 ? npc.IsControllerActionPressed(CONTROLLER_ACTION_CHARGE) : npc.IsFrame(CHARGE_CHECK_FRAMES, (int)npc.index)); if (isReady) return maggot_charge_try(npc, frame); spitty_attack_try(npc, frame); } // The dank Charger's lunge: it bounces off walls, steers into its charge while slowing down and keeps to its cell's // line, bleeding and leaving creep, then recovers once slow after 45 frames. static void dank_charge_update(Entity_NPC& npc, MaggotFrame& frame) { npc.friction *= DANK_FRICTION; if (npc_anim_is_done(npc)) npc.sprite.Play(ATTACK_ANIMS[frame.facing]); if (!npc.sprite.animState.IsLoopingAnimation()) return; if (npc.isCollidesWithGrid) npc.v1 = vector_scale(npc.v1, -1); if (npc.stateFrame > DANK_RECOVER_FRAMES && vector_length(npc.v1) < DANK_RECOVER_SPEED) { npc.pathfinder.ResetMovementTarget(); npc.sprite.Play(RECOVER_ANIMS[frame.facing]); npc.state = NPC_STATE_SPECIAL; } else { npc.friction = 1; float keep = 1 - timescale_factor(DANK_STEER_FRICTION, npc.speedMultiplier); npc.velocity.x += keep * (npc.v1.x - npc.velocity.x); npc.velocity.y += keep * (npc.v1.y - npc.velocity.y); float slowdown = std::clamp(((float)npc.stateFrame - DANK_SLOWDOWN_START) / DANK_SLOWDOWN_FRAMES, 0.0f, 1.0f); npc.v1 = vector_scale(npc.v1, 1 - slowdown * DANK_SLOWDOWN); Vector cell = g_game->room->GetGridPosition(frame.cell); if (npc.controllerID != -1) cell = npc.v2; if (std::abs(npc.velocity.y) > std::abs(npc.velocity.x)) npc.pos.x = cell.x * DANK_ALIGN + npc.pos.x * DANK_KEEP; else npc.pos.y = cell.y * DANK_ALIGN + npc.pos.y * DANK_KEEP; npc.sprite.Play(ATTACK_ANIMS[frame.facing]); } if (Random() % DANK_SPLAT_ODDS == 0) npc.MakeSplat(random_unit() * DANK_SPLAT_RANGE + DANK_SPLAT_SIZE) ->SetColor(npc.splatColor, -1, COLOR_PRIORITY_DEFAULT, false, true); if (npc.IsFrame(DANK_CREEP_SKIP_FRAMES, 0) || npc.HasEntityFlags(FLAG_SHRINK)) return; auto* creep = static_cast(g_game->Spawn( {.position = npc.pos, .spawner = &npc, .type = ENTITY_EFFECT, .variant = EFFECT_CREEP_BLACK, .seed = Random()})); creep->parent = &npc; creep->SetTimeout(DANK_CHARGER_CREEP_TIMEOUT); creep->sprite.scale = vector_make(DANK_CHARGER_CREEP_SCALE, DANK_CHARGER_CREEP_SCALE); creep->Update(); } // A charge: it dashes along its axis, kept on its cell's line, until it stalls, hits the grid or a status stops it // (the Gurgling then stops short). static void maggot_charge_update(Entity_NPC& npc, MaggotFrame& frame) { if (frame.isDank) return dank_charge_update(npc, frame); PathFinder& path = npc.pathfinder; float speedSquared = npc.speedMultiplier * npc.speedMultiplier; if (vector_length_squared(vector_sub(path.gridPathTarget, npc.pos)) / speedSquared < CHARGE_STALL_SQUARED) path.moveCountdown += npc_ticks_get(npc); else path.moveCountdown = 0; path.gridPathTarget = npc.pos; bool isSlowedStill = npc.HasEntityFlags(FLAG_SLOW) && vector_length(npc.velocity) < CHARGE_SLOW_SPEED; if (npc.isCollidesWithGrid || npc.HasEntityFlags(FLAG_DISTRACTED) || isSlowedStill || path.moveCountdown > CHARGE_STALL_FRAMES || frame.isClimbing) { path.ResetMovementTarget(); if (npc.type != ENTITY_GURGLING) { npc.state = NPC_STATE_MOVE; return; } npc.sprite.Play("Stop"); npc.state = NPC_STATE_IDLE; return; } npc.friction *= npc.type == ENTITY_CHARGER ? CHARGER_CHARGE_FRICTION : CHARGE_FRICTION; npc.AddVelocity_divided(npc.v1, false); Vector cell = g_game->room->GetGridPosition(frame.cell); if (npc.controllerID != -1) cell = npc.v2; if (npc.type == ENTITY_CHARGER && npc.variant == CHARGER_CARRION) return carrion_charge_steer(npc, cell); if (npc.type != ENTITY_GURGLING) { float keep = 1 - timescale_factor(CHARGE_ALIGN_FRICTION, npc.speedMultiplier); if (std::abs(npc.v1.y) <= std::abs(npc.v1.x)) npc.velocity.y = ((cell.y - npc.pos.y) - npc.velocity.y) * keep + npc.velocity.y; else npc.velocity.x = ((cell.x - npc.pos.x) - npc.velocity.x) * keep + npc.velocity.x; } if (npc.type == ENTITY_LEECH) npc.sprite.Play(DASH_ANIMS[frame.facing]); else if (npc.type == ENTITY_GURGLING) npc.sprite.Play(GURGLING_CHARGE_ANIMS[frame.facing]); else npc.sprite.Play(ATTACK_ANIMS[frame.facing]); } // The way a spitting maggot faces (left, up, right or down): its controller's aim (facing it), else the facing it // started spitting with. static int spitty_direction_get(Entity_NPC& npc, int& facing) { if (npc.controllerID == -1) { if (npc.i1 == FACING_SIDE) return npc.sprite.isFlipX ? DIRECTION_LEFT : DIRECTION_RIGHT; return npc.i1 == FACING_UP ? DIRECTION_UP : DIRECTION_DOWN; } int direction = vector_to_direction(npc.controllerMovement); npc.sprite.isFlipX = false; facing = direction == DIRECTION_UP ? FACING_UP : direction == DIRECTION_DOWN ? FACING_DOWN : FACING_SIDE; if (facing == FACING_SIDE) npc.sprite.isFlipX = direction == DIRECTION_LEFT; const FacingAnims& anims = npc.type == ENTITY_SPITTY ? ATTACK_ANIMS : CONJOINED_ATTACK_ANIMS; if (!npc.sprite.IsPlaying(anims[facing])) npc.sprite.SetAnimation(anims[facing]); return direction; } // The tainted Spitty's spray: ten shots fanning wider, smaller and slower along its facing. static void tainted_spray(Entity_NPC& npc, int direction) { ProjectileParams params; params.heightModifier = SPRAY_HEIGHT; Vector axis = direction_unit(direction); for (int i = 0; i < SPRAY_SHOTS; i++) { params.bulletFlags = PROJECTILE_ACCELERATE; params.scale = SPRAY_SCALE - (float)i * SPRAY_SCALE_STEP; params.acceleration = 1 - (float)i * SPRAY_ACCEL_STEP; params.fallingAccelModifier = (float)(SPRAY_SHOTS - i) * SPRAY_FALL_STEP - SPRAY_FALL; float angle = (random_unit() - SPRAY_JITTER_HALF) * (float)i * DEGREES_TO_RADIANS; float cosine = std::cos(angle); float sine = std::sin(angle); Vector shot = vector_make((cosine * axis.x - sine * axis.y) * SPRAY_SPEED, (sine * axis.x + cosine * axis.y) * SPRAY_SPEED); npc.FireProjectiles(npc.pos, shot, PROJECTILES_SINGLE, params); } } // Spitting: on its Shoot event it spits along its facing (the conjoined one both ways, the tainted one a spray). static void spitty_spit_update(Entity_NPC& npc, MaggotFrame& frame) { npc.friction *= MAGGOT_FRICTION; npc.UpdateControllerMovement(false); if (!npc.sprite.IsEventTriggered("Shoot")) return; npc.PlaySound(SOUND_BLOODSHOOT, 1, 2, false, 1); npc.PlaySound(SOUND_LITTLE_SPIT, 1, 2, false, 1); ProjectileParams params; params.heightModifier = SPIT_HEIGHT; params.fallingSpeedModifier = SPIT_FALL_SPEED; params.fallingAccelModifier = SPIT_FALL_ACCEL; int direction = spitty_direction_get(npc, frame.facing); if (npc.type == ENTITY_SPITTY && npc.variant == SPITTY_TAINTED) return tainted_spray(npc, direction); Vector shot = vector_scale(direction_unit(direction), SPITTY_SHOT_SPEED); npc.FireProjectiles(npc.pos, shot, PROJECTILES_SINGLE, params); if (npc.type == ENTITY_CONJOINED_SPITTY) npc.FireProjectiles(npc.pos, vector_scale(shot, -1), PROJECTILES_SINGLE, params); } // Summoning: on its Shoot event it farts a dip out behind it, holding it as its child. static void maggot_summon_update(Entity_NPC& npc) { npc.friction *= DANK_FRICTION; if (!npc.sprite.IsEventTriggered("Shoot")) return; npc.PlaySound(SOUND_FART, 1, 2, false, 1); int direction = npc.i1 == FACING_SIDE ? (npc.sprite.isFlipX ? DIRECTION_LEFT : DIRECTION_RIGHT) : (npc.i1 != FACING_UP ? DIRECTION_DOWN : DIRECTION_UP); Vector behind = vector_scale(direction_unit(direction), -1); uint32_t seed = npc.dropRNG.Next(); Entity* dip = g_game->Spawn({.position = vector_add(vector_scale(behind, SUMMON_DIP_REACH), npc.pos), .velocity = vector_scale(behind, SUMMON_DIP_SPEED), .spawner = &npc, .type = ENTITY_DIP, .seed = seed}); dip->ClearEntityFlags(FLAG_APPEAR); dip->Update(); npc.child = dip; } // A running Gurgling's trail every 5 frames of its own time: blood, and creep (the Turdling's brown with farts) while // it runs. The third boss Gurgling trolls a bomb every 15 frames instead. static void gurgling_trail(Entity_NPC& npc) { if (vector_length_squared(npc.velocity) <= GURGLING_TRAIL_SPEED_SQUARED) return; if (npc.GetIntervalTicks(GURGLING_TRAIL_INTERVAL, 0) != 0) npc.MakeSplat(random_unit() * GURGLING_SPLAT_RANGE + GURGLING_SPLAT_SIZE) ->SetColor(npc.splatColor, -1, COLOR_PRIORITY_DEFAULT, false, true); if (npc.variant != GURGLING_PLAIN && npc.state != NPC_STATE_ATTACK) return; if ((!npc.isBoss || npc.subtype == 0) && npc.GetIntervalTicks(GURGLING_TRAIL_INTERVAL, 0) != 0) { int variant = EFFECT_CREEP_RED; if (npc.variant == GURGLING_TURDLING) { variant = TURDLING_CREEP_VARIANT; if (Random() % TURDLING_FART_ODDS == 0) { float pitch = random_unit() * TURDLING_FART_RANGE + 1; float volume = random_unit() * TURDLING_FART_RANGE + TURDLING_FART_VOLUME; npc.PlaySound(SOUND_FART, volume, 2, false, pitch); } } auto* creep = static_cast(g_game->Spawn( {.position = npc.pos, .spawner = &npc, .type = ENTITY_EFFECT, .variant = variant, .seed = Random()})); creep->sprite.scale = vector_make(GURGLING_CREEP_SCALE, GURGLING_CREEP_SCALE); creep->rotation = 0; creep->Update(); if (npc.isChampion) creep->SetColor(npc.color, -1, COLOR_PRIORITY_DEFAULT, false, true); else if (npc.variant != GURGLING_TURDLING) { ColorMod color = npc.color; color._tint[3] = 1; creep->SetColor(color, -1, COLOR_PRIORITY_DEFAULT, false, true); } return; } if (!npc.isBoss || npc.subtype != GURGLING_BOMBER_SUBTYPE || npc.GetIntervalTicks(GURGLING_BOMB_INTERVAL, 0) == 0) return; g_game->Spawn({.position = npc.pos, .spawner = &npc, .type = ENTITY_BOMB, .variant = BOMB_TROLL, .seed = Random()}) ->entityCollisionClass = ENTCOLL_PLAYERONLY; Room& room = *g_game->room; room.SetGridPath(room.GetGridIndex(npc.pos), GURGLING_BOMB_GRID_PATH); } // The tainted Spitty's death: two rings of orbiting shots, one each way. static void tainted_spitty_burst(Entity_NPC& npc) { ProjectileParams params; params.targetPosition = npc.pos; params.scale = TAINTED_RING_SCALE; params.fallingAccelModifier = TAINTED_RING_FALL; params.circleAngle = 0; params.bulletFlags = PROJECTILE_ORBIT_CW; Vector ring = vector_make(TAINTED_RING_SPEED, TAINTED_RING_SHOTS); npc.FireProjectiles(npc.pos, ring, PROJECTILES_RING, params); params.circleAngle = TAINTED_RING_TURN; params.bulletFlags = PROJECTILE_ORBIT_CCW; npc.FireProjectiles(npc.pos, ring, PROJECTILES_RING, params); } // My Shadow keeps its distance from the others of its kind while wandering. static void shadow_spread(Entity_NPC& npc) { RNG rng = SHADOW_RNG; rng._seed = npc.initSeed; float range = rng.RandomFloat() * SHADOW_RANGE + SHADOW_RANGE; float push = std::min(std::max(vector_length(npc.velocity) + SHADOW_PUSH_BASE, SHADOW_PUSH_MIN), SHADOW_PUSH_MAX); Vector total{}; bool isVersus = g_game->deathmatchManager.state >= DEATHMATCH_INTRO; for (Entity* other : entity_query(npc.type, npc.variant)) { bool isTarget = (isVersus && other->type == ENTITY_PLAYER) || Entity::is_enemy_type(other->type); if (other->subtype != npc.subtype || !isTarget || other == &npc) continue; Vector away = vector_sub(npc.pos, other->pos); float distance = vector_length(away); if (distance <= 0 || distance >= range || push <= SHADOW_PUSH_FLOOR) continue; total.x += push * (away.x / distance); total.y += push * (away.y / distance); } npc.velocity.x += npc.speedMultiplier * total.x; npc.velocity.y += npc.speedMultiplier * total.y; } // The forward vector other code reads: the Gurgling's down, a charging Carrion Princess's diagonal, the others' // facing. static void maggot_forward_set(Entity_NPC& npc, int facing) { if (npc.type == ENTITY_GURGLING) { npc.forwardVector = vector_make(0, 1); return; } if (npc.type == ENTITY_CHARGER && npc.variant == CHARGER_CARRION && npc.state == NPC_STATE_ATTACK) { npc.forwardVector = vector_normalized(npc.v1); return; } int direction = facing == FACING_UP ? DIRECTION_UP : facing == FACING_DOWN ? DIRECTION_DOWN : (npc.sprite.isFlipX ? DIRECTION_LEFT : DIRECTION_RIGHT); npc.forwardVector = direction_unit(direction); } // 0052db50 The maggot kin's AI: they wander along the grid's axes, and the Chargers, Leeches and Gurglings charge // along an axis at a target in line with them while the Spitties spit along their facing. The dank Charger lunges, // the drowned one and the Leeches burst as they die, and the Gurglings leave trails and summon. void npc_maggot_update(Entity_NPC& npc) { bool isShadow = npc.type == ENTITY_CHARGER && npc.variant == 0 && npc.subtype == MY_SHADOW_SUBTYPE; if (npc.type == ENTITY_LEECH && npc.variant != 0 && npc.IsControllerActionPressed(CONTROLLER_ACTION_BURST)) npc.Kill(EntityRef()); if (npc.isDead) maggot_death_burst(npc); if (npc.type == ENTITY_GURGLING && npc.isBoss && npc.subtype == GURGLING_BOMBER_SUBTYPE) for (DamageEvent& event : npc.damageEvents) if ((event.damageFlags & DAMAGE_EXPLOSION) && event.source.type == npc.type) event.damage = GURGLING_SELF_BLAST_DAMAGE; MaggotFrame frame{.stain = stain_find(npc)}; if (npc.state == NPC_STATE_INIT) return maggot_init(npc); frame.isClimbing = stain_climb(npc, frame.stain); frame.isCharger = npc.type == ENTITY_CHARGER || npc.type == ENTITY_LEECH || npc.type == ENTITY_GURGLING; bool isLeftAlone = npc.GetPlayerTarget()->type == ENTITY_PLAYER && npc.HasEntityFlags(FLAG_FRIENDLY) && npc.controllerID == -1 && g_game->deathmatchManager.state < DEATHMATCH_INTRO; if (isLeftAlone) frame.isCharger = false; frame.speed = maggot_speed_get(npc); Room& room = *g_game->room; frame.cell = room.GetGridIndex(npc.pos); room_path_raise_at(npc.pos, MAGGOT_GRID_PATH); frame.isDank = npc.type == ENTITY_CHARGER && npc.variant == CHARGER_DANK; if (npc.state == NPC_STATE_SUICIDE) return maggot_thrown_update(npc); frame.facing = maggot_facing_get(npc, frame.isDank); switch (npc.state) { case NPC_STATE_IDLE: maggot_rest_update(npc); break; case NPC_STATE_MOVE: maggot_wander_update(npc, frame); break; case NPC_STATE_ATTACK: maggot_charge_update(npc, frame); break; case NPC_STATE_ATTACK2: spitty_spit_update(npc, frame); if (npc.sprite.IsFinished()) npc.state = NPC_STATE_MOVE; break; case NPC_STATE_SPECIAL: npc.friction *= DANK_FRICTION; if (npc.sprite.IsFinished()) npc.state = NPC_STATE_MOVE; break; case NPC_STATE_SUMMON: maggot_summon_update(npc); if (npc.sprite.IsFinished()) npc.state = NPC_STATE_MOVE; break; } npc.stateFrame += npc_ticks_get(npc); maggot_forward_set(npc, frame.facing); if (npc.type == ENTITY_GURGLING) gurgling_trail(npc); bool isTaintedSpitty = npc.type == ENTITY_SPITTY && npc.variant == SPITTY_TAINTED; if (npc.isDead && isTaintedSpitty && !npc.HasEntityFlags(FLAG_SHRINK)) tainted_spitty_burst(npc); if (isShadow && npc.state == NPC_STATE_MOVE) shadow_spread(npc); }