#include "npc_alt.h" #include "boss_ai.h" #include "entity_bomb.h" #include "entity_config.h" #include "entity_effect_combat.h" #include "grid_entity.h" #include "room_descriptor.h" #include "room_grid.h" #include "seeds.h" #include #include // The Mole (variant 0) and Tainted Mole (variant 1) burrow toward their target, leaving a trail of dirt piles (the // Tainted Mole's crushing what stands on them), dig out near it, shoot (three shots, or up to four big ones), and jump // back under to a nearby cell, leaving a poop where they went down. In darkness they use their glowing animations. constexpr int TAINTED_MOLE = 1; constexpr float DARKNESS_SEEN = 0.8f; constexpr float MOLE_CHARGE_BAR_FULL = 150; // Its states beyond the shared ones: jumping back under, burrowing (the shared move), digging out and shooting. constexpr int MOLE_JUMP = NPC_STATE_IDLE; constexpr int MOLE_DIG_OUT = NPC_STATE_JUMP; constexpr int MOLE_SHOOT = NPC_STATE_ATTACK; // Jumping under: it picks the farthest of ten spots within 120 it can reach on a free cell (its target 1 time in 4, // else one 90 away), or its controller's way, and leaves a poop of 125 or 250 health where it went down. constexpr int JUMP_TRIES = 10; constexpr int JUMP_TARGET_MASK = 3; constexpr float JUMP_STEP = 90; constexpr float JUMP_CONTROLLED_REACH = 120; constexpr float JUMP_CONTROLLED_SHRINK = 5; constexpr int JUMP_CONTROLLED_FREE_TRIES = 3; constexpr float JUMP_FREE_RADIUS = 64; constexpr float JUMP_REACH = 120; constexpr int JUMP_BLOCKED_PATH = 500; constexpr int POOP_HEALTH_STEP = 125; constexpr int DIRT_PARTICLES = 4; constexpr float DIRT_PARTICLE_SPEED = 5; constexpr int GRID_MARKED_PATH = 900; constexpr float JUMP_PULL_GROWTH = 0.04f; constexpr float MOLE_JUMP_FRICTION = 0.4f; // Burrowing: it heads 0.8 fast at its target when in line (aiming a little ahead), else paths there, and digs out 1 // time in 10 within 90 (or within 200 after 90 frames). A controlled one digs out on its button or after 150 frames. constexpr float BURROW_SPEED = 0.8f; constexpr int BURROW_PATH_MARKER = 499; constexpr int BURROW_SNAPSHOT_FRAMES = 30; constexpr float BURROW_LEAD = 0.4f; constexpr float BURROW_WOBBLE_MIN = 128; constexpr float BURROW_WOBBLE_SHARE = 0.5f; constexpr float BURROW_FRICTION = 0.8f; constexpr uint32_t DIG_OUT_ODDS = 10; constexpr float DIG_OUT_NEAR_SQUARED = 8100; constexpr float DIG_OUT_FAR_SQUARED = 40000; constexpr int DIG_OUT_LATE_FRAMES = 90; constexpr int DIG_OUT_CONTROLLED_FRAMES = 150; constexpr int CONTROLLER_ATTACK_ACTION = 9; constexpr float CONTROLLED_ATTACK_RANGE = 1000; constexpr int CONTROLLED_ATTACK_EVERY = 7; constexpr float DIRT_PILE_SPACING_SQUARED = 144; constexpr float TAINTED_CRUSH_DAMAGE = 10; constexpr float TAINTED_CRUSH_RADIUS = 20; constexpr float ROCK_EXPLOSION_DEPTH = 10; // Digging out: a crush and shockwave for the Tainted Mole, a poof of dust, then 1 time in 10 a frame it shoots. constexpr float MOLE_SHOCKWAVE_MAX_RADIUS = 50; constexpr int MOLE_SHOCKWAVE_FRAMES = 5; constexpr int DIG_OUT_SHAKE = 4; constexpr ColorMod DIG_OUT_POOF_COLOR = tint_color_make(0.9f, 0.9f, 0.9f, 0.6f); constexpr uint32_t MOLE_SHOOT_ODDS = 10; // Its shots: three 8 fast (spread a little), or the Tainted Mole's big ones up to four times. constexpr float MOLE_SHOT_SPEED = 8; constexpr float MOLE_SHOT_SPREAD = 0.4f; constexpr float TAINTED_SHOT_SCALE = 1.4f; constexpr int TAINTED_VOLLEYS = 3; constexpr int SHOT_BLOOD = 2; constexpr float SHOT_BLOOD_RISE = -20; constexpr float SHOT_PITCH = 1.2f; // Whether the room is too dark to see a plain Mole: Curse of the Darkness, a pitch black room, or deep darkness. static bool mole_is_dark() { Game& game = *g_game; uint32_t curses = (Seeds::GetForcedCurses() | game.debugCurses | game.curses) & ~Seeds::GetPreventedCurses(); float darkness = (curses & CURSE_OF_DARKNESS) ? 1 : game.darknessStrength * game.darknessModifier; return (game.room->descriptor->flags & ROOM_DESCRIPTOR_FLAG_PITCH_BLACK) || darkness > DARKNESS_SEEN; } // The cell a position falls in, or -1 off the grid. static int cell_get(Room& room, const Vector& position, int& column, int& row) { column = (int)((position.x - GRID_ORIGIN_X) / GRID_CELL_SIZE + 0.5f); row = (int)((position.y - GRID_ORIGIN_Y) / GRID_CELL_SIZE + 0.5f); bool isInside = column >= 0 && column < room.gridWidth && row >= 0 && row < room.gridHeight; return isInside ? row * room.gridWidth + column : -1; } // A cell's center. static Vector cell_center(Room& room, int cell) { return vector_make((float)(cell % room.gridWidth) * GRID_CELL_SIZE + GRID_ORIGIN_X, (float)(cell / room.gridWidth) * GRID_CELL_SIZE + GRID_ORIGIN_Y); } // Marks a cell as taken for the walkers' paths. static void cell_mark(Room& room, int cell) { if (cell >= 0 && cell < ROOM_GRID_CELLS) room.gridPaths[cell] = GRID_MARKED_PATH; } // The poop it leaves, placed as Room::SpawnGridEntityDesc places a saved one unless the cell is taken. The poop rarely // drops anything. static void dug_poop_spawn(Room& room, int cell) { if (cell < 0 || cell >= ROOM_GRID_CELLS) KAGE_LogMessage(LOG_ASSERT, (uint8_t*)"SpawnGridEntity: Grid index invalid\n"); if (room.gridPaths[cell] > GRID_MARKED_PATH) return; GridEntity* poop = CreateGridEntity(GRID_POOP, cell); if (!poop) return; poop->state = (int)((Random() & 1) + 1) * POOP_HEALTH_STEP; poop->spawnSeed = Random(); poop->variableSeed = Random(); delete room.gridEntities[cell]; room.gridEntities[cell] = poop; poop->collisionClass = COLLISION_NONE; poop->subclass_init(); poop->spawnCount++; poop->isInitialized = true; poop->spawnFrame = g_game->frameCount; poop->varData = 1; poop->PostInit(); } // A spot to jump to on this try: its controller's way, its target 1 time in 4, or a random one 90 away. static Vector jump_spot_get(Entity_NPC& npc, int tries) { if (npc.controllerID != -1) { Vector input = npc.GetControllerInput(0); return vector_add(npc.pos, vector_scale(input, JUMP_CONTROLLED_REACH - (float)tries * JUMP_CONTROLLED_SHRINK)); } if ((Random() & JUMP_TARGET_MASK) == 0) return npc.CalcTargetPosition(0); float angle = random_unit() * RANDOM_HALF_TURN * 2; return vector_add(npc.pos, vector_make(std::cos(angle) * JUMP_STEP, std::sin(angle) * JUMP_STEP)); } // On Shoot of its jump it picks where to come up, leaves its poop behind, and drops underground (forward if lower). static void mole_dig_in(Entity_NPC& npc) { Room& room = *g_game->room; npc.shadowSize = npc.config->shadowSize; npc.targetPosition = {}; float farthest = 0; int start = room.GetGridIndex(npc.pos); int best = start; for (int tries = 0; tries < JUMP_TRIES; tries++) { Vector spot = jump_spot_get(npc, tries); if (npc.controllerID == -1 || tries > JUMP_CONTROLLED_FREE_TRIES) spot = room_free_tile_position_find(spot, JUMP_FREE_RADIUS); float distance = vector_length(vector_sub(npc.pos, spot)); int column = 0; int row = 0; int cell = cell_get(room, spot, column, row); bool isOpen = cell < 0 || cell >= ROOM_GRID_CELLS || room.gridPaths[cell] < JUMP_BLOCKED_PATH; if (!isOpen || !npc.pathfinder.HasPathToPos(spot, false) || distance >= JUMP_REACH || farthest >= distance) continue; farthest = distance; best = cell; if (npc.controllerID != -1) break; } if (start != best && room.CanSpawnObstacleAtPosition(start, false)) { npc_sound_play(SOUND_ROCK_CRUMBLE, 1, 2, 1); dug_poop_spawn(room, start); game_particles_spawn({.position = npc.pos, .speed = DIRT_PARTICLE_SPEED, .count = DIRT_PARTICLES, .variant = EFFECT_ROCK_PARTICLE}); } npc.targetPosition = cell_center(room, best); npc.v1.x = 0; if (npc.pos.y <= npc.targetPosition.y) { npc.entityGridCollisionClass = GRIDCOLL_NONE; return; } int frame = npc.sprite.animState.animData ? (int)std::floor(npc.sprite.animState.animFrame) : -1; npc.sprite.Play("JumpBack"); npc.sprite.SetFrame((float)frame); npc.forwardVector = vector_make(0, -1); npc.entityGridCollisionClass = GRIDCOLL_NONE; } // Jumping back under: picking its spot on Shoot, gone from sight on Shoot2, then burrowing. static void mole_jump(Entity_NPC& npc) { ANM2& sprite = npc.sprite; if (!sprite.animState.animData || !sprite.WasEventTriggered("Shoot")) cell_mark(*g_game->room, g_game->room->GetGridIndex(npc.pos)); if (sprite.animState.animData && sprite.IsEventTriggered("Shoot")) mole_dig_in(npc); if (sprite.animState.animData) { if (sprite.IsEventTriggered("Shoot2")) { npc.shadowSize = 0; npc.entityCollisionClass = ENTCOLL_NONE; npc.entityGridCollisionClass = GRIDCOLL_GROUND; } if (sprite.IsPlaying()) return; } npc.state = NPC_STATE_MOVE; npc.targetPosition = {}; } // Whether it digs out now: 1 time in 10 near its target, or by its controller. static bool mole_is_digging_out(Entity_NPC& npc) { float distanceSquared = vector_length_squared(vector_sub(npc.CalcTargetPosition(0), npc.pos)); if (npc.controllerID == -1) return Random() % DIG_OUT_ODDS == 0 && (distanceSquared < DIG_OUT_NEAR_SQUARED || (npc.stateFrame > DIG_OUT_LATE_FRAMES && distanceSquared < DIG_OUT_FAR_SQUARED)); return npc.IsControllerActionPressed(CONTROLLER_ATTACK_ACTION) || npc.IsTargetInAttackRange(CONTROLLED_ATTACK_RANGE, 0, CONTROLLED_ATTACK_EVERY) || npc.stateFrame > DIG_OUT_CONTROLLED_FRAMES; } // Its dirt trail: a pile wherever none lies within 12, the Tainted Mole's with a rock burst crushing what stands there. static void dirt_pile_leave(Entity_NPC& npc) { for (Entity* pile : entity_query(ENTITY_EFFECT, EFFECT_DIRT_PILE)) if (vector_length_squared(vector_sub(npc.pos, pile->pos)) < DIRT_PILE_SPACING_SQUARED) return; g_game->Spawn({.position = npc.pos, .type = ENTITY_EFFECT, .variant = EFFECT_DIRT_PILE, .seed = Random()}); if (npc.variant != TAINTED_MOLE) return; Entity* burst = g_game->Spawn({.position = npc.pos, .type = ENTITY_EFFECT, .variant = EFFECT_ROCK_EXPLOSION, .seed = Random()}); burst->renderDepthOffset = ROCK_EXPLOSION_DEPTH; explosion_damage({.position = npc.pos, .source = npc.parent.get(), .damageFlags = DAMAGE_CRUSH, .damage = TAINTED_CRUSH_DAMAGE, .radius = TAINTED_CRUSH_RADIUS, .isLineCheck = false}); npc_sound_play(SOUND_ROCK_CRUMBLE, 1, 2, 1); } // Burrowing to its target (straight at it, a little ahead, when in line), digging out on a free cell near it. static void mole_burrow(Entity_NPC& npc, bool isDark) { Room& room = *g_game->room; Vector target = npc.CalcTargetPosition(0); Entity* player = npc.GetPlayerTarget(); if (npc.IsFrame(BURROW_SNAPSHOT_FRAMES, (int)npc.index)) npc.v2 = player->velocity; if (npc.controllerID == -1 && room_check_line(npc.pos, target, LINE_MODE_GROUND, 0)) { RNG rng = {npc.initSeed + npc.index, 5, 9, 7}; float distance = vector_length(vector_sub(npc.pos, target)); target = vector_add(target, vector_scale(npc.v2, distance * rng.RandomFloat() * BURROW_LEAD)); // The original clamps this wobble's radius to nothing, so only its draws are left. float wobble = std::min(std::max(BURROW_WOBBLE_MIN, vector_length(vector_sub(npc.pos, target)) * BURROW_WOBBLE_SHARE), 0.0f); float age = rng.RandomFloat() * (float)(g_game->frameCount - npc.spawnFrame); float angle = (rng.RandomFloat() * DEGREES_PER_TURN + age + age) * DEGREES_TO_RADIANS; target = vector_add(target, vector_make(std::cos(angle) * wobble, std::sin(angle) * wobble)); npc.friction *= BURROW_FRICTION; npc_push(npc, vector_scale(vector_normalized(vector_sub(target, npc.pos)), BURROW_SPEED)); } else npc.pathfinder.FindGridPath(target, BURROW_SPEED, BURROW_PATH_MARKER, DIRECT_PATH_FAR); if (mole_is_digging_out(npc)) { int cell = room.GetGridIndex(npc.pos); bool isFree = cell >= 0 && room_grid_collision_at(npc.pos) == 0 && (npc.controllerID != -1 || cell >= ROOM_GRID_CELLS || room.gridPaths[cell] < JUMP_BLOCKED_PATH); if (isFree) { npc.pos = cell_center(room, cell); npc.state = MOLE_DIG_OUT; npc.targetPosition = npc.pos; npc.stateFrame = 0; npc.sprite.Play(isDark ? "DigOut2" : "DigOut"); npc.UpdateDirtColor(true); return; } if (npc.stateFrame > DIG_OUT_CONTROLLED_FRAMES && npc.controllerID != -1) reconstruction_missing("NpcAI::ENTITY_MOLE 004fdf80: a controlled mole kept under too long"); } npc.stateFrame += npc_ticks_get(npc); dirt_pile_leave(npc); } // Digging out: on Shoot it is solid again (the Tainted Mole with a shockwave and crush), then 1 time in 10 (or on its // controller's aim) it shoots. A controlled one not aiming jumps back under. static void mole_dig_out(Entity_NPC& npc, bool isDark) { if (npc.controllerID != -1) npc.SpawnControlTarget(0, -1); if (npc.sprite.animState.animData) { if (npc.sprite.IsEventTriggered("Shoot")) { npc.entityCollisionClass = ENTCOLL_ALL; if (npc.variant == TAINTED_MOLE) { auto* wave = static_cast(g_game->Spawn({.position = npc.pos, .spawner = &npc, .type = ENTITY_EFFECT, .variant = EFFECT_SHOCKWAVE, .seed = Random()})); wave->parent = &npc; wave->SetRadii(npc.size, MOLE_SHOCKWAVE_MAX_RADIUS); wave->SetTimeout(MOLE_SHOCKWAVE_FRAMES); npc.PlaySound(SOUND_HELLBOSS_GROUNDPOUND, 1, 2, false, 1); game_shake_screen(DIG_OUT_SHAKE); npc.MakeGroundPoof(npc.pos, DIG_OUT_POOF_COLOR, 1); } } if (npc.sprite.IsPlaying()) return; } bool isShooting = npc.controllerID == -1 ? Random() % MOLE_SHOOT_ODDS == 0 : !vector_is_zero(vector_sub(npc.GetAimTargetPosition(), npc.pos)); if (isShooting) { npc.state = MOLE_SHOOT; npc.i2 = 0; } else if (npc.controllerID != -1) { npc.state = MOLE_JUMP; npc.sprite.Play(isDark ? "Jump2" : "Jump"); } } // Its shots on Shoot at its target (or its controller's aim), the Tainted Mole firing up to three more volleys. static void mole_shoot(Entity_NPC& npc, bool isDark) { if (npc.controllerID != -1) npc.SpawnControlTarget(0, -1); npc.sprite.Play("Shoot"); if (npc.sprite.IsEventTriggered("Shoot")) { Vector at = npc.GetPlayerTarget()->pos; if (npc.controllerID != -1) at = npc.GetAimTargetPosition(); ProjectileParams params; if (npc.variant == TAINTED_MOLE) params.scale = TAINTED_SHOT_SCALE; else params.spread = MOLE_SHOT_SPREAD; Vector aim = vector_scale(vector_normalized(vector_sub(at, npc.pos)), MOLE_SHOT_SPEED); npc.FireProjectiles(npc.pos, aim, PROJECTILES_THREE, params); blood_explosion_spawn(SHOT_BLOOD, npc.pos, vector_make(npc.sprite.scale.x * 0, npc.sprite.scale.y * SHOT_BLOOD_RISE), COLOR_DEFAULT, {}); bool isTainted = npc.variant == TAINTED_MOLE; npc.PlaySound(isTainted ? SOUND_MONSTER_GRUNT_2 : SOUND_WHEEZY_COUGH, 1, 2, false, isTainted ? 1 : SHOT_PITCH); npc.sprite.isFlipX = at.x < npc.pos.x; } if (npc.variant == TAINTED_MOLE && npc.sprite.animState.animData && npc.sprite.IsEventTriggered("Shoot2") && npc.i2 < TAINTED_VOLLEYS) { npc.sprite.Play("Shoot", PLAY_FORCED); npc.i2++; } if (npc.sprite.IsPlaying()) return; npc.state = MOLE_JUMP; npc.sprite.Play(isDark ? "Jump2" : "Jump"); } // 004fdf80 (NpcAI::ENTITY_MOLE) The Mole and Tainted Mole: immune to its kind's own hits, it burrows, digs out, shoots // and jumps back under, drawn toward its spot while it moves. void npc_mole_update(Entity_NPC& npc) { bool isDark = mole_is_dark(); if (npc.type == ENTITY_MOLE && npc.pendingDamage > 0) for (DamageEvent& event : npc.damageEvents) if (!event.source.IsCharmed() && (event.source.type == npc.type || event.source.spawnerType == npc.type)) event.damage = 0; npc.UpdateChargeBar(MOLE_CHARGE_BAR_FULL, 0, (float)npc.stateFrame, true); npc.forwardVector = vector_make(0, 1); switch (npc.state) { case NPC_STATE_INIT: npc.state = NPC_STATE_MOVE; npc.entityCollisionClass = ENTCOLL_NONE; npc.shadowSize = 0; npc.v2 = {}; break; case MOLE_JUMP: mole_jump(npc); break; case NPC_STATE_MOVE: mole_burrow(npc, isDark); break; case MOLE_DIG_OUT: mole_dig_out(npc, isDark); break; case MOLE_SHOOT: mole_shoot(npc, isDark); break; } npc.UpdateDirtColor(false); if (vector_is_zero(npc.targetPosition)) return; if (npc.state == MOLE_JUMP) { npc.v1.x = std::clamp(npc.v1.x + JUMP_PULL_GROWTH, 0.0f, 1.0f); npc.friction *= MOLE_JUMP_FRICTION; npc.velocity = vector_scale(vector_sub(npc.targetPosition, npc.pos), npc.v1.x / npc.friction); return; } npc.friction *= BURROW_FRICTION; npc.velocity = vector_sub(npc.targetPosition, npc.pos); int column = 0; int row = 0; int cell = cell_get(*g_game->room, npc.pos, column, row); if (cell >= 0) cell_mark(*g_game->room, cell); }