#include "boss_ai.h" #include "npc_caves.h" #include "entity_player.h" #include "player_manager.h" #include "room_grid.h" #include #include // Rag Man's parts: the boss and his rolling head. Rag Mega's: the boss, its purple balls and its rebirth pillars. constexpr int RAG_MAN_HEAD = 1; constexpr int RAG_MEGA_BALL = 1; constexpr int RAG_MEGA_PILLAR = 2; // Rag Man's subtypes: the plain one, the one that summons and revives only, and the one whose heads turn to spiders. constexpr int RAG_MAN_SUMMONER = 1; constexpr int RAG_MAN_SPIDERS = 2; // Rag Man's states: walking, headless, shooting, throwing his head and reviving his Raglings. constexpr int RAG_MAN_HEADLESS = NPC_STATE_IDLE; constexpr int RAG_MAN_SHOOT = NPC_STATE_ATTACK; constexpr int RAG_MAN_THROW = NPC_STATE_SUMMON; constexpr int RAG_MAN_REBIRTH = NPC_STATE_SUMMON2; constexpr int RAGLING_DEAD = NPC_STATE_SPECIAL; constexpr int RAGLING_REGEN = NPC_STATE_APPEAR_CUSTOM; constexpr ColorMod RAG_SPLAT_COLOR = {{0, 0, 0, 1}, {0, 0, 0, 0}, {0.35f, 0.11f, 0.36f}}; constexpr float RAG_MAN_WALL_MARGIN = 80; constexpr float RAG_MAN_PATH_SPEED = 1.05f; constexpr int RAG_MAN_PATH_MARKER = 900; constexpr float RAG_MAN_IDLE_SPEED = 1; constexpr int RAG_MAN_REBIRTH_READY = 120; constexpr float RAG_MAN_REBIRTH_REACH = 70; constexpr float RAG_MAN_GUARD_OFFSET = 60; constexpr float RAG_MAN_CLAMP_MARGIN = 60; constexpr float RAG_MAN_FAR = 9999999; constexpr uint32_t RAG_MAN_SPIDER_REBIRTH_ODDS = 100; constexpr int RAG_MAN_SHOOT_CHECK_FRAMES = 15; constexpr uint32_t RAG_MAN_SHOOT_PERCENT = 20; constexpr uint32_t RAG_MAN_PERCENT = 100; constexpr float RAG_MAN_FRICTION = 0.8f; constexpr float RAG_MAN_HEADLESS_FRICTION = 0.75f; constexpr float RAG_MAN_WANDER_DRAG = 1.1f; constexpr int RAG_MAN_COOLDOWN_MIN = 40; constexpr uint32_t RAG_MAN_COOLDOWN_RANGE = 20; constexpr int RAG_MAN_SHOOT_COOLDOWN = 90; constexpr float RAG_MAN_TRAIL_SPEED = 5; constexpr float RAG_MAN_TRAIL_FALL_ACCEL = 0.3f; constexpr float RAG_MAN_TRAIL_FALL_SPEED = -5; constexpr float RAG_MAN_SHOT_SPEED = 7; constexpr float RAG_MAN_SHOT_OFFSET = 16; constexpr float RAG_MAN_HEAD_OFFSET = 57; constexpr float RAG_MAN_HEAD_SPEED = 10; constexpr float RAG_MAN_DELIRIUM_HEAD_HEALTH = 8; constexpr float RAG_MAN_HEAL = 0.05f; constexpr float RAG_MAN_REVIVE_REACH = 100; constexpr float RAG_MAN_REVIVE_SHOT_CURVE = 10; constexpr int RAG_MAN_REVIVE_SHOTS = 3; constexpr float RAGLING_HEAL = 0.25f; constexpr int HEAD_ROLL_MIN = 60; constexpr float RAG_HEAD_ROLL_RANGE = 45; constexpr float HEAD_SLOW_START = 75; constexpr float HEAD_SLOW_END = 15; constexpr float HEAD_FAST_SPEED = 10; constexpr float HEAD_SLOWDOWN = 8; constexpr float HEAD_STEER = 0.15f; constexpr float HEAD_SPIN_SPEED = 10; constexpr int HEAD_DAMAGE_COUNTDOWN = 30; constexpr float HEAD_SUMMONER_HEALTH = 0.66f; constexpr float HEAD_RAGLING_MIN_HEALTH = 0.5f; constexpr float MEGA_WALL_MARGIN = 40; constexpr float MEGA_DRIFT = 0.4f; constexpr float MEGA_SPIDER_FRICTION = 0.9f; constexpr float MEGA_FLEE_REACH = 180; constexpr float MEGA_FLEE_SCALE = 0.02f; constexpr int MEGA_BALLS = 3; constexpr float MEGA_BALL_ORBIT = 50; constexpr int MEGA_RAGLING_LIMIT = 3; constexpr uint32_t MEGA_SUMMON_ODDS_PER_RAGLING = 100; constexpr uint32_t MEGA_REBIRTH_ODDS = 70; constexpr uint32_t MEGA_COVER_ODDS = 45; constexpr int MEGA_COVER_MIN = 60; constexpr uint32_t MEGA_COVER_RANGE = 30; constexpr int MEGA_COVER_GUARD_FRAMES = 20; constexpr int MEGA_ATTACK_FRAME = 20; constexpr float MEGA_PUFF_ANGLE = 90; constexpr float MEGA_PUFF_HEIGHT = -40; constexpr ColorMod MEGA_PUFF_COLOR = {{0, 0, 0, 0.5f}, {0, 0, 0, 0}, {0, 0, 0}}; constexpr float MEGA_RAGLING_DROP = 10; constexpr float MEGA_DELIRIUM_RAGLING_HEALTH = 15; constexpr float MEGA_PILLAR_SPEED = 10; constexpr float MEGA_PILLAR_REACH = 40; constexpr ColorMod BALL_COLOR = {{1.3f, 0.8f, 1.3f, 1}, {0, 0, 0, 0}, {0.2f, 0, 0.2f}}; constexpr ColorMod MEGA_BALL_SPLAT_COLOR = {{0, 0, 0, 1.1f}, {0, 0, 0, 0}, {0.5f, 0, 0.5f}}; constexpr ColorMod BALL_BARE_SPLAT_COLOR = {{0, 0, 0, 0.8f}, {0, 0, 0, 0}, {0, 0, 0}}; constexpr float BALL_HEIGHT = -40; constexpr float BALL_FRICTION = 0.8f; constexpr float BALL_DRAG = 1.1764705f; constexpr float BALL_CHASE = 0.5f; constexpr float MEGA_BALL_MASS = 8; constexpr float BALL_GLOW_FRAMES = 30; constexpr float BALL_SPACING = 100; constexpr float BALL_SPACING_PUSH = 0.003f; constexpr float BALL_LAUNCH_SPEED = 10; constexpr int BALL_ORBIT_BLEND_FRAMES = 25; constexpr float BALL_ORBIT_BLEND = 30; constexpr float BALL_TRAIL_BACK = -0.5f; constexpr float BALL_TRAIL_SCATTER = 0.4f; constexpr int BALL_TRAIL = 2; // What Rag Man sees each frame: his Raglings (and spiders), his heads still rolling, and where to walk or revive. struct RagManView { std::vector raglings; Vector destination{}; Vector reviveSpot = vector_make(RAG_MAN_FAR, RAG_MAN_FAR); int active = 0; int children = 0; int heads = 0; int maxActive = 0; int maxChildren = 0; bool isDestinationFound = false; bool isThrowLeft = false; }; // Raises a Ragling playing dead, or heals a standing one by a quarter of its health. static void ragling_revive(Entity& ragling) { auto& npc = static_cast(ragling); if (npc.state == RAGLING_DEAD) { npc.state = RAGLING_REGEN; return; } npc.health = std::min(npc.health + npc.maxHitPoints * RAGLING_HEAL, npc.maxHitPoints); } // Rag Man's limits for his health: Raglings and heads out at once (all four for the summoner). static void rag_man_limits(Entity_NPC& npc, RagManView& view) { float health = npc.health; float maxHealth = npc.maxHitPoints; view.maxChildren = health < maxHealth; if (health < maxHealth * 0.75f) view.maxChildren = 2; if (health < maxHealth * 0.55f) view.maxChildren = 3; if (health < maxHealth * 0.35f) view.maxChildren = 4; view.maxActive = health < maxHealth; if (health < maxHealth * 0.6f) view.maxActive = 2; if (npc.subtype != RAG_MAN_SUMMONER) return; view.maxChildren = 4; view.maxActive = 4; } // Counts Rag Man's heads, Raglings and spiders, and picks where to walk: the nearest Ragling playing dead once he // may revive, else beside the nearest standing one, on its far side from the target. static RagManView rag_man_look(Entity_NPC& npc, const Vector& targetPos) { RagManView view; rag_man_limits(npc, view); view.raglings = entity_query(ENTITY_RAGLING, RAGLING_RAG_MAN); for (Entity* head : entity_query(ENTITY_RAG_MAN, -1)) if (head->variant == RAG_MAN_HEAD && head->parent.get() == &npc) view.heads++; int dead = 0; for (Entity* ragling : view.raglings) { if (ragling->parent.get() == &npc) view.children++; if (static_cast(ragling)->state == RAGLING_DEAD) dead++; } if (npc.subtype == RAG_MAN_SPIDERS) for (Entity* spider : entity_query(ENTITY_SPIDER, -1)) if (spider->parent.get() == &npc) view.children++; view.active = view.children - dead; float closest = RAG_MAN_FAR; bool isReviveFound = false; for (Entity* ragling : view.raglings) { float distance = vector_length(vector_sub(ragling->pos, npc.pos)); if (static_cast(ragling)->state == RAGLING_DEAD) { if ((isReviveFound && distance >= closest) || npc.i1 < RAG_MAN_REBIRTH_READY || view.active >= view.maxActive) continue; view.destination = ragling->pos; view.reviveSpot = ragling->pos; view.isDestinationFound = true; isReviveFound = true; closest = distance; } else if (distance < closest && !isReviveFound) { Vector away = vector_normalized(vector_sub(ragling->pos, targetPos)); Vector guard = vector_add(ragling->pos, vector_scale(away, RAG_MAN_GUARD_OFFSET)); view.destination = g_game->room->GetClampedPositionImpl(guard, RAG_MAN_CLAMP_MARGIN, RAG_MAN_CLAMP_MARGIN, RAG_MAN_CLAMP_MARGIN, RAG_MAN_CLAMP_MARGIN); view.isDestinationFound = true; closest = distance; } } return view; } // Walking: toward his chosen spot, then a head throw while he may have more out, a rebirth beside a fallen Ragling, // now and then a volley at a target lined up sideways. static void rag_man_walk(Entity_NPC& npc, const RagManView& view, bool isTargetSideways, bool isTargetLeft) { Vector goal = npc.CalcTargetPosition(0); if (view.isDestinationFound && !npc.HasEntityFlags(FLAG_DISTRACTED | FLAG_CHARM)) goal = view.destination; npc.pathfinder.movementTarget = {}; npc.pathfinder.moveDirection = {}; npc.pathfinder.FindGridPath(goal, RAG_MAN_PATH_SPEED, RAG_MAN_PATH_MARKER, DIRECT_PATH_FAR); npc.sprite.isFlipX = false; if (vector_length(npc.velocity) <= RAG_MAN_IDLE_SPEED) npc_anim_loop(npc.sprite, "Idle"); else if (std::abs(npc.velocity.x) <= std::abs(npc.velocity.y)) npc_anim_loop(npc.sprite, "WalkVert"); else npc_anim_loop(npc.sprite, npc.velocity.x >= 0 ? "WalkRight" : "WalkLeft"); if (npc.HasEntityFlags(FLAG_DELIRIUM_FORM) && room_is_clear()) return; if (npc.i1 >= RAG_MAN_REBIRTH_READY || npc.subtype == RAG_MAN_SUMMONER) { if (view.children < view.maxChildren && view.active < view.maxActive) { npc.state = RAG_MAN_THROW; npc_sound_play(SOUND_RAGMAN_3, 1, 2, 1); return; } if (npc.i1 >= RAG_MAN_REBIRTH_READY && vector_length(vector_sub(npc.pos, view.reviveSpot)) < RAG_MAN_REBIRTH_REACH) { npc.state = RAG_MAN_REBIRTH; return; } } if (npc.subtype == RAG_MAN_SPIDERS && Random() % RAG_MAN_SPIDER_REBIRTH_ODDS == 0) { npc.state = RAG_MAN_REBIRTH; return; } if (!npc.IsFrame(RAG_MAN_SHOOT_CHECK_FRAMES, (int)npc.index)) return; if (Random() % RAG_MAN_PERCENT >= RAG_MAN_SHOOT_PERCENT || !isTargetSideways) return; if (npc.projectileCooldown >= 1 || npc.subtype == RAG_MAN_SUMMONER) return; npc.state = RAG_MAN_SHOOT; npc.sprite.isFlipX = isTargetLeft; } // The throw: his head flies off toward the side he faces on Spawn, rolling on as a head of his own. static void rag_man_throw(Entity_NPC& npc, bool isThrowLeft) { const AnimationData* data = npc.sprite.animState.animData; bool isThrowing = data && npc.sprite.animState.isPlaying && (data->name == "HeadThrowLeft" || data->name == "HeadThrowRight"); if (isThrowing) isThrowLeft = data->name == "HeadThrowLeft"; else if (!data || (data->name != "HeadThrowLeft" && data->name != "HeadThrowRight")) npc_anim_loop(npc.sprite, isThrowLeft ? "HeadThrowLeft" : "HeadThrowRight"); else isThrowLeft = false; if (npc.sprite.animState.animData && npc.sprite.IsEventTriggered("Spawn")) { float side = isThrowLeft ? -1.0f : 1.0f; auto* head = static_cast(g_game->Spawn({.position = vector_make(npc.pos.x + side * RAG_MAN_HEAD_OFFSET, npc.pos.y), .velocity = vector_make(side * RAG_MAN_HEAD_SPEED, 0), .spawner = &npc, .subType = npc.subtype, .type = ENTITY_RAG_MAN, .variant = RAG_MAN_HEAD, .seed = Random()})); head->parent.Set(&npc); head->ClearEntityFlags(FLAG_APPEAR); if (head->bosscoloridx != npc.bosscoloridx) { head->bosscoloridx = npc.bosscoloridx; head->load_graphics(true); head->sprite.SetFrame(head->sprite.GetDefaultAnimationName(), 0); } head->ResetColor(); if (npc.HasEntityFlags(FLAG_DELIRIUM_FORM)) { head->health = RAG_MAN_DELIRIUM_HEAD_HEALTH; head->maxHitPoints = RAG_MAN_DELIRIUM_HEAD_HEALTH; } } npc.TakeDamage(-1, 0, EntityRef(), HEAD_DAMAGE_COUNTDOWN); if (!npc_anim_is_done(npc)) return; npc.state = RAG_MAN_HEADLESS; npc.projectileCooldown = (int)(Random() % RAG_MAN_COOLDOWN_RANGE) + RAG_MAN_COOLDOWN_MIN; } // Headless: wandering (fleeing when feared) and dribbling a falling shot now and then, until his head pops back once // none of his heads still rolls. static void rag_man_headless(Entity_NPC& npc, int heads) { npc.friction *= RAG_MAN_HEADLESS_FRICTION; const AnimationData* data = npc.sprite.animState.animData; bool isPopping = data && data->name == "HeadPop"; if (isPopping && !npc.sprite.animState.isPlaying) { npc.state = NPC_STATE_MOVE; npc.stateFrame = 0; npc.projectileCooldown = 0; return; } if (isPopping) return; if (!npc.HasEntityFlags(FLAG_FEAR | FLAG_SHRINK)) { npc.pathfinder.MoveRandomly(STATUS_OBEYED); npc.velocity = vector_scale(npc.velocity, timescale_factor(RAG_MAN_WANDER_DRAG, npc.speedMultiplier)); } else { npc.pathfinder.EvadeTarget(npc.GetPlayerTarget()->pos, STATUS_OBEYED); if (npc.pathfinder.evadeMovementCountdown > 0) npc.friction *= RAG_MAN_FRICTION; } if (std::abs(npc.velocity.x) <= std::abs(npc.velocity.y)) npc_anim_loop(npc.sprite, "WalkNoHeadVert"); else npc_anim_loop(npc.sprite, npc.velocity.x >= 0 ? "WalkNoHeadRight" : "WalkNoHeadLeft"); if (npc.projectileCooldown < 1 && !npc.HasEntityFlags(FLAG_DISTRACTED) && npc.subtype != RAG_MAN_SUMMONER) { npc.projectileCooldown = (int)(Random() % RAG_MAN_COOLDOWN_RANGE) + RAG_MAN_COOLDOWN_MIN; ProjectileParams params; params.fallingAccelModifier = RAG_MAN_TRAIL_FALL_ACCEL; params.fallingSpeedModifier = RAG_MAN_TRAIL_FALL_SPEED; params.bulletFlags = PROJECTILE_SMART; npc.FireProjectiles(npc.pos, vector_scale(vector_normalized(npc.velocity), RAG_MAN_TRAIL_SPEED), PROJECTILES_SINGLE, params); npc.PlaySound(SOUND_BLOODSHOOT, 1, 2, false, 1); } if (heads == 0) npc_anim_loop(npc.sprite, "HeadPop"); } // The volley's pick: one, three or five homing shots, more as he weakens. static void rag_man_shoot_pick(Entity_NPC& npc) { int pick = (int)(Random() % 3); if (npc.maxHitPoints * 2 / 3 < npc.health) pick--; if (npc.health < npc.maxHitPoints / 3) pick++; if (pick <= 0) { npc.sprite.Play("Shooting1"); npc_sound_play(SOUND_RAGMAN_1, 1, 2, 1); } else if (pick == 1) { npc.sprite.Play("Shooting3"); npc_sound_play(SOUND_RAGMAN_4, 1, 2, 1); } else { npc.sprite.Play("Shooting5"); npc_sound_play(SOUND_RAGMAN_2, 1, 2, 1); } } // The volley: a homing shot at the target from his facing side on each Shoot event, then a long pause. static void rag_man_shoot(Entity_NPC& npc, const Vector& targetPos) { const AnimationData* data = npc.sprite.animState.animData; bool isShooting = data && (data->name == "Shooting1" || data->name == "Shooting3" || data->name == "Shooting5"); if (!isShooting) rag_man_shoot_pick(npc); bool isFlipped = npc.sprite.isFlipX; if (npc.sprite.animState.animData) { if (npc.sprite.IsEventTriggered("Shoot")) { npc_sound_play(SOUND_BLOODSHOOT, 1, 2, 1); ProjectileParams params; params.bulletFlags = PROJECTILE_SMART; params.homingStrength = 1; Vector aim = vector_normalized(vector_sub(targetPos, npc.pos)); Vector from = vector_make(npc.pos.x + (isFlipped ? -RAG_MAN_SHOT_OFFSET : RAG_MAN_SHOT_OFFSET), npc.pos.y); npc.FireProjectiles(from, vector_scale(aim, RAG_MAN_SHOT_SPEED), PROJECTILES_SINGLE, params); } if (npc.sprite.animState.animData && npc.sprite.animState.isPlaying) return; } npc.projectileCooldown = RAG_MAN_SHOOT_COOLDOWN; npc.state = NPC_STATE_MOVE; } // The rebirth: on Revive his Raglings near him rise or heal (the spider one fires a burst instead) and he heals a // little, past his full health if he must. static void rag_man_rebirth(Entity_NPC& npc, const std::vector& raglings) { npc_anim_loop(npc.sprite, "Rebirth"); if (npc.sprite.animState.animData) { if (npc.sprite.IsEventTriggered("Revive")) { npc_sound_play(SOUND_SUMMONSOUND, 1, 2, 1); npc_sound_play(SOUND_RAGMAN_2, 1, 2, 1); if (npc.subtype == RAG_MAN_SPIDERS) { Vector aim = game_random_player(*g_game, npc.pos, RAG_MAN_FAR)->pos; npc.FireBossProjectiles(RAG_MAN_REVIVE_SHOTS, aim, RAG_MAN_REVIVE_SHOT_CURVE, ProjectileParams()); } else for (Entity* ragling : raglings) if (vector_length(vector_sub(ragling->pos, npc.pos)) < RAG_MAN_REVIVE_REACH) ragling_revive(*ragling); npc.health += npc.maxHitPoints * RAG_MAN_HEAL; } if (npc.sprite.animState.animData && npc.sprite.animState.isPlaying) return; } npc.i1 = 0; npc.state = NPC_STATE_MOVE; } // Rag Man (405.0): walking with his Raglings, throwing his head to raise more, shooting and reviving the fallen. // His Raglings die with him. static void rag_man_update(Entity_NPC& npc) { Vector targetPos = npc.GetPlayerTarget()->pos; Room& room = *g_game->room; // The original compares the target's own coordinates, not its offset from Rag Man. bool isTargetSideways = std::abs(targetPos.y) < std::abs(targetPos.x); bool isTargetLeft = targetPos.x - npc.pos.x < 0; bool isThrowLeft = room.topLeftPos.x + RAG_MAN_WALL_MARGIN <= npc.pos.x && isTargetLeft; if (room.bottomRightPos.x - RAG_MAN_WALL_MARGIN < npc.pos.x) isThrowLeft = true; if (npc.state == NPC_STATE_INIT) { npc.state = NPC_STATE_MOVE; npc.i1 = 0; npc.splatColor = RAG_SPLAT_COLOR; return; } RagManView view = rag_man_look(npc, targetPos); npc.i1 = view.active < view.maxActive ? npc.i1 + 1 : 0; if (npc.projectileCooldown > 0) npc.projectileCooldown--; bool isSlowed = true; switch (npc.state) { case NPC_STATE_MOVE: rag_man_walk(npc, view, isTargetSideways, isTargetLeft); break; case RAG_MAN_THROW: rag_man_throw(npc, isThrowLeft); break; case RAG_MAN_SHOOT: rag_man_shoot(npc, targetPos); break; case RAG_MAN_REBIRTH: rag_man_rebirth(npc, view.raglings); break; case RAG_MAN_HEADLESS: rag_man_headless(npc, view.heads); isSlowed = false; break; default: isSlowed = false; } if (isSlowed) npc.friction *= RAG_MAN_FRICTION; if (!npc.isDead || npc.IsFriendly()) return; for (Entity* ragling : entity_query(ENTITY_RAGLING, RAGLING_RAG_MAN)) if (!ragling->IsFriendly()) ragling->Kill(EntityRef(&npc)); } // His head drops what it rolled into: a Ragling (two thirds as tough for the summoner, hurt as he is hurt) or, for // the spider one, a spider or two. A head dying while he lives leaves a Ragling playing dead. static void rag_man_head_drop(Entity_NPC& npc) { uint32_t seed = Random(); if (npc.subtype == RAG_MAN_SPIDERS) { for (uint32_t i = (seed & 1) + 1; i > 0; i--) { Entity* spider = g_game->Spawn({.position = npc.pos, .spawner = &npc, .type = ENTITY_SPIDER, .seed = Random()}); spider->parent.Set(npc.parent.get()); } return; } auto* ragling = static_cast(g_game->Spawn({.position = npc.pos, .velocity = npc.velocity, .spawner = &npc, .type = ENTITY_RAGLING, .variant = RAGLING_RAG_MAN, .seed = seed})); if (!ragling) return; ragling->parent.Set(npc.parent.get()); ragling->SetBossColorIdx(npc.bosscoloridx, 0); if (!npc.isDead) { float health = ragling->maxHitPoints; if (npc.subtype == RAG_MAN_SUMMONER) { health *= HEAD_SUMMONER_HEALTH; ragling->maxHitPoints = health; ragling->health = health; } float hurt = std::clamp(npc.health / npc.maxHitPoints, 0.0f, 1.0f); ragling->health = (health - health * HEAD_RAGLING_MIN_HEALTH) * hurt + health * HEAD_RAGLING_MIN_HEALTH; } else if (!npc.parent.get()) ragling->Remove(); else { g_game->Spawn({.position = npc.pos, .spawner = &npc, .type = ENTITY_EFFECT, .variant = EFFECT_POOF01, .seed = Random()}); ragling->ClearEntityFlags(FLAG_APPEAR); ragling->Update(); ragling->isDead = true; } } // Rag Man's head (405.1): it rolls at the target, fast at first and slowing, passing its hurts to him, then drops // what it carries. A charmed head keeps rolling. static void rag_man_head_update(Entity_NPC& npc) { if (npc.state == NPC_STATE_INIT) { npc.state = NPC_STATE_MOVE; npc.stateFrame = 0; npc.i1 = HEAD_ROLL_MIN + (int)(random_unit() * RAG_HEAD_ROLL_RANGE); npc.splatColor = RAG_SPLAT_COLOR; return; } int slowStart = (int)((float)npc.i1 - HEAD_SLOW_START); int slowEnd = (int)((float)npc.i1 - HEAD_SLOW_END); Entity* parent = npc.parent.get(); if (parent && npc.spawnerType != ENTITY_RAG_MEGA) for (DamageEvent& event : npc.damageEvents) parent->TakeDamage(event.damage, event.damageFlags, event.source, HEAD_DAMAGE_COUNTDOWN); npc.stateFrame++; if (npc.HasEntityFlags(FLAG_CHARM)) { npc.stateFrame = 0; npc.state = NPC_STATE_MOVE; } if (npc.state == NPC_STATE_MOVE) { if (slowStart == slowEnd) KAGE_LogMessage(LOG_ASSERT, (uint8_t*)"Start and End parameters cannot be the same!"); float slowdown = std::clamp((float)(npc.stateFrame - slowStart) / (float)(slowEnd - slowStart), 0.0f, 1.0f); float speed = HEAD_FAST_SPEED - slowdown * HEAD_SLOWDOWN; Vector toTarget = vector_normalized(vector_sub(npc.CalcTargetPosition(0), npc.pos)); Vector heading = vector_normalized(npc.velocity); Vector steer = vector_add(vector_add(vector_scale(toTarget, speed * HEAD_STEER), npc.velocity), vector_scale(heading, speed * HEAD_STEER)); steer = vector_resized(steer, speed); float keep = timescale_factor(RAG_MAN_FRICTION, npc.speedMultiplier); npc.velocity = vector_add(vector_scale(vector_sub(steer, npc.velocity), 1 - keep), npc.velocity); constexpr std::string_view ROLLS[] = {"RollLeft", "RollUp", "RollRight"}; int direction = npc.get_velocity_direction(); npc.sprite.Play(direction >= 0 && direction < 3 ? ROLLS[direction] : "RollDown"); if (npc.stateFrame > slowEnd) npc.state = NPC_STATE_IDLE; } bool isRolling = (npc.state != NPC_STATE_IDLE || npc.stateFrame <= npc.i1) && !npc.isDead; if (isRolling) { npc.sprite.playbackSpeed = std::clamp(vector_length(npc.velocity) / HEAD_SPIN_SPEED, 0.0f, 1.0f) * 2; npc.friction *= RAG_MAN_FRICTION; return; } rag_man_head_drop(npc); npc.Remove(); } // 00493200 Rag Man (405): the boss and his rolling head. void boss_rag_man_update(Entity_NPC& npc) { if (npc.variant == 0) return rag_man_update(npc); if (npc.variant == RAG_MAN_HEAD) rag_man_head_update(npc); } // Rag Mega's purple balls fly free (spawning a Ragling each fall) as it weakens, or circle it while it is strong. static void mega_ball_orbit(Entity_NPC& npc) { Entity_NPC* mega = npc.ParentNPC(); if (!mega) return; int count = std::max(1, mega->i1); float health = mega->health / mega->maxHitPoints; int orbiting = mega->i1; bool isLaunch = health < 0.25f; bool isCounted = (health >= 0.25f && health < 0.5f && npc.i1 > 0) || (health >= 0.5f && health < 0.75f && npc.i1 == 2); if (isCounted) { isLaunch = true; mega->i1--; } if (isLaunch) { npc.state = NPC_STATE_MOVE; npc.PlaySound(SOUND_RAGMEGA_BALL_ATTACK, 1, 2, false, 1); npc_push(npc, vector_resized(vector_sub(npc.pos, mega->pos), BALL_LAUNCH_SPEED)); } else { if (npc.i2 != orbiting) { npc.stateFrame = BALL_ORBIT_BLEND_FRAMES; npc.i2 = orbiting; } float frames = (float)(g_game->frameCount - npc.spawnFrame); float angle = (((float)npc.i1 / (float)count) * 360 + frames + frames) * DEGREES_TO_RADIANS; float blend = 1 - (float)npc.stateFrame / BALL_ORBIT_BLEND; Vector spot = vector_make(std::cos(angle) * MEGA_BALL_ORBIT + mega->pos.x, std::sin(angle) * MEGA_BALL_ORBIT + mega->pos.y); npc.velocity = vector_scale(vector_sub(spot, npc.pos), blend); } } // A free ball chases the target, keeping 100 from its fellows, its glow fading as it loses its guard. static void mega_ball_chase(Entity_NPC& npc) { npc.friction *= BALL_FRICTION; Vector toTarget = vector_sub(npc.CalcTargetPosition(0), npc.pos); npc.velocity = vector_scale(npc.velocity, timescale_factor(BALL_DRAG, npc.speedMultiplier)); npc_push(npc, vector_scale(vector_normalized(toTarget), BALL_CHASE)); npc.mass = MEGA_BALL_MASS; if (npc.v2.y > 0) { npc.v2.y -= npc.speedMultiplier; float glow = std::clamp(npc.v2.y / BALL_GLOW_FRAMES, 0.0f, 1.0f); npc.sprite.GetLayer(0)->color = color_lerp(COLOR_DEFAULT, BALL_COLOR, glow); if (glow < 0.5f) { npc.splatColor = BALL_BARE_SPLAT_COLOR; npc.isInvincible = false; } } for (Entity* other : entity_query(npc.type, npc.variant)) { Vector away = vector_sub(npc.pos, other->pos); float distance = vector_length(away); if (other == &npc || distance >= BALL_SPACING) continue; if (distance > 0) away = vector_scale(away, (BALL_SPACING - distance) / distance); npc_push(npc, vector_scale(away, BALL_SPACING_PUSH)); } } // A ball's trail: a smoke particle in its own color drifting back from it, scattered by its speed. static void mega_ball_trail(Entity_NPC& npc) { Entity* smoke = g_game->Spawn({.position = npc.pos, .spawner = &npc, .subType = BALL_TRAIL, .type = ENTITY_EFFECT, .variant = EFFECT_DARK_BALL_SMOKE_PARTICLE, .seed = Random()}); smoke->SetColor(npc.sprite.GetLayer(0)->color, -1, COLOR_PRIORITY_DEFAULT, false, true); smoke->velocity = vector_scale(npc.velocity, BALL_TRAIL_BACK); float angle = random_unit() * RANDOM_HALF_TURN; angle += angle; float scatter = random_unit() * vector_length(npc.velocity) * BALL_TRAIL_SCATTER; npc_push(*smoke, vector_make(std::cos(angle) * scatter, std::sin(angle) * scatter)); smoke->posOffset = vector_make(0, BALL_HEIGHT); } // Rag Mega's purple ball (409.1): circling it, then chasing the target, dying with it. static void mega_ball_update(Entity_NPC& npc) { if (npc.state == NPC_STATE_INIT) { npc.sprite.GetLayer(0)->color = BALL_COLOR; npc.splatColor = MEGA_BALL_SPLAT_COLOR; npc.posOffset.y = BALL_HEIGHT; npc.state = NPC_STATE_IDLE; npc.i2 = MEGA_BALLS; return; } if (npc.state == NPC_STATE_MOVE) mega_ball_chase(npc); else if (npc.state == NPC_STATE_IDLE) { mega_ball_orbit(npc); if (npc.stateFrame > 0) npc.stateFrame--; } if (!npc.parent.get()) npc.Kill(EntityRef()); mega_ball_trail(npc); npc_anim_loop(npc.sprite, "Idle"); } // Rag Mega's rebirth pillar (409.2): sliding off, it raises or heals the Raglings it passes, gone at a wall or once // its animation ends. static void mega_pillar_update(Entity_NPC& npc) { npc.state = NPC_STATE_IDLE; for (Entity* ragling : entity_query(ENTITY_RAGLING, RAGLING_RAG_MAN)) if (vector_length(vector_sub(ragling->pos, npc.pos)) < MEGA_PILLAR_REACH) ragling_revive(*ragling); if (npc.isCollidesWithGrid) npc.Remove(); npc_anim_loop(npc.sprite, "Rebirth Pillar"); if (npc_anim_is_done(npc)) npc.Remove(); } // Rag Mega drifts diagonally, bouncing off the walls (the top kept 40 clear), fleeing a target near it when feared. static void mega_drift(Entity_NPC& npc) { Room& room = *g_game->room; Vector clamped = room.GetClampedPositionImpl(npc.pos, MEGA_WALL_MARGIN, 0, 0, 0); npc.pos.y = clamped.y; if (clamped.x == room.bottomRightPos.x) npc.velocity.x = -1; if (clamped.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 + MEGA_WALL_MARGIN) npc.velocity.y = 1; npc.friction *= RAG_MAN_FRICTION; if (npc.HasEntityFlags(FLAG_CONFUSION) && !npc.HasEntityFlags(FLAG_CHARM)) { if (npc.subtype == RAG_MAN_SPIDERS) npc.friction *= MEGA_SPIDER_FRICTION; npc.pathfinder.MoveRandomly(STATUS_OBEYED); return; } Vector drift = vector_make(npc.velocity.x > 0 ? 1 : -1, npc.velocity.y < 0.1f ? -1 : 1); npc_push(npc, vector_scale(drift, MEGA_DRIFT)); if (npc.subtype == RAG_MAN_SPIDERS) npc.friction *= MEGA_SPIDER_FRICTION; if (!npc.HasEntityFlags(FLAG_FEAR | FLAG_SHRINK)) return; Vector away = vector_sub(npc.pos, npc.GetPlayerTarget()->pos); float distance = vector_length(away); float flee = std::clamp((MEGA_FLEE_REACH - distance) * MEGA_FLEE_SCALE, 0.0f, 1.0f); if (distance > 0) away = vector_scale(away, flee / distance); npc_push(npc, away); } // Rag Mega's start: three purple balls around it. static void mega_init(Entity_NPC& npc) { for (int i = 0; i < MEGA_BALLS; i++) { uint32_t seed = Random(); float angle = ((float)i / MEGA_BALLS) * 360 * DEGREES_TO_RADIANS; Vector spot = vector_make(std::cos(angle) * MEGA_BALL_ORBIT + npc.pos.x, std::sin(angle) * MEGA_BALL_ORBIT + npc.pos.y); auto* ball = static_cast(g_game->Spawn( {.position = spot, .spawner = &npc, .type = ENTITY_RAG_MEGA, .variant = RAG_MEGA_BALL, .seed = seed})); ball->parent.Set(&npc); ball->ClearEntityFlags(FLAG_APPEAR); ball->i1 = i; ball->isInvincible = true; ball->entityGridCollisionClass = GRIDCOLL_NONE; ball->entityCollisionClass = ENTCOLL_PLAYEROBJECTS; ball->Update(); } npc.i1 = MEGA_BALLS; npc.state = NPC_STATE_MOVE; npc.splatColor = RAG_SPLAT_COLOR; } // Walking: a Ragling summon the fewer it has (none past three), a rebirth when one lies fallen, or its cover. static void mega_walk(Entity_NPC& npc) { std::vector raglings = entity_query(ENTITY_RAGLING, RAGLING_RAG_MAN); bool isRaglingDown = false; for (Entity* ragling : raglings) if (static_cast(ragling)->state == RAGLING_DEAD) { isRaglingDown = true; break; } npc.stateFrame = 0; if (npc.HasEntityFlags(FLAG_DELIRIUM_FORM) && room_is_clear()) return npc_anim_loop(npc.sprite, "Walk"); uint32_t summonOdds = (uint32_t)raglings.size() * MEGA_SUMMON_ODDS_PER_RAGLING; if ((summonOdds == 0 || Random() % summonOdds == 0) && raglings.size() < MEGA_RAGLING_LIMIT) { npc.state = NPC_STATE_SUMMON; npc_anim_restart(npc.sprite, "Attack01"); npc.PlaySound(SOUND_RAGMEGA_INHALE, 1, 2, false, 1); } else if (Random() % MEGA_REBIRTH_ODDS == 0 && isRaglingDown) { npc.state = NPC_STATE_SUMMON2; npc_anim_restart(npc.sprite, "Attack02"); } else if (Random() % MEGA_COVER_ODDS == 0) { npc.state = NPC_STATE_ATTACK; npc_anim_restart(npc.sprite, "Cover"); npc.PlaySound(SOUND_RAGMEGA_INVINCIBLE_ON, 1, 2, false, 1); } else npc_anim_loop(npc.sprite, "Walk"); } // The cover: invincible under its rags for 60 to 89 frames, then uncovering. False while it holds. static bool mega_cover(Entity_NPC& npc) { const AnimationData* data = npc.sprite.animState.animData; if (data && !npc.sprite.animState.isPlaying && data->name == "Cover") { npc.sprite.Play("Covered"); npc.isInvincible = true; npc.stateFrame = (int)(Random() % MEGA_COVER_RANGE) + MEGA_COVER_MIN; } if (npc.stateFrame > 0) { if (--npc.stateFrame != 0) return false; npc.sprite.Play("Uncover"); npc.PlaySound(SOUND_RAGMEGA_INVINCIBLE_OFF, 1, 2, false, 1); return false; } if (npc.sprite.GetFrame() > MEGA_COVER_GUARD_FRAMES) npc.isInvincible = false; return true; } // The summon: two dark puffs either side and a Ragling dropped below it. static void mega_summon(Entity_NPC& npc) { if (npc.stateFrame == MEGA_ATTACK_FRAME) { npc.PlaySound(SOUND_RAGMEGA_EXHALE, 1, 2, false, 1); for (int i = 0; i < 2; i++) { auto* puff = static_cast( g_game->Spawn({.position = npc.pos, .spawner = &npc, .type = ENTITY_EFFECT, .variant = EFFECT_BOMB_EXPLOSION, .seed = Random()})); puff->rotation = i == 0 ? MEGA_PUFF_ANGLE : -MEGA_PUFF_ANGLE; puff->SetColor(MEGA_PUFF_COLOR, -1, COLOR_PRIORITY_DEFAULT, false, true); puff->posOffset = vector_make(0, MEGA_PUFF_HEIGHT); puff->Update(); } Entity* ragling = g_game->Spawn({.position = vector_make(npc.pos.x, npc.pos.y + MEGA_RAGLING_DROP), .velocity = vector_make(0, MEGA_RAGLING_DROP), .spawner = &npc, .type = ENTITY_RAGLING, .variant = RAGLING_RAG_MAN, .seed = Random()}); ragling->parent.Set(&npc); ragling->ClearEntityFlags(FLAG_APPEAR); if (npc.HasEntityFlags(FLAG_DELIRIUM_FORM)) { ragling->health = MEGA_DELIRIUM_RAGLING_HEALTH; ragling->maxHitPoints = MEGA_DELIRIUM_RAGLING_HEALTH; } } npc.stateFrame++; } // The rebirth: three pillars sliding out at even angles from a random start. static void mega_rebirth(Entity_NPC& npc) { if (npc.stateFrame == MEGA_ATTACK_FRAME) { float start = random_unit() * 360; for (int i = 0; i < MEGA_BALLS; i++) { uint32_t seed = Random(); float angle = (((float)i / MEGA_BALLS) * 360 + start) * DEGREES_TO_RADIANS; auto* pillar = static_cast(g_game->Spawn( {.position = npc.pos, .velocity = vector_make(std::cos(angle) * MEGA_PILLAR_SPEED, std::sin(angle) * MEGA_PILLAR_SPEED), .spawner = &npc, .type = ENTITY_RAG_MEGA, .variant = RAG_MEGA_PILLAR, .seed = seed})); pillar->parent.Set(&npc); pillar->ClearEntityFlags(FLAG_APPEAR); pillar->entityCollisionClass = ENTCOLL_NONE; pillar->entityGridCollisionClass = GRIDCOLL_WALLS; pillar->shadowSize = 0; pillar->Update(); } npc.PlaySound(SOUND_RAGMEGA_BEAM, 1, 2, false, 1); } npc.stateFrame++; npc_anim_loop(npc.sprite, "Rebirth"); } // 00495610 Rag Mega (409): the boss bouncing about the room with its purple balls, summoning and reviving Raglings and // covering itself, and its balls and pillars. void boss_rag_mega_update(Entity_NPC& npc) { if (npc.variant == RAG_MEGA_BALL) return mega_ball_update(npc); if (npc.variant == RAG_MEGA_PILLAR) return mega_pillar_update(npc); if (npc.variant != 0) return; mega_drift(npc); switch (npc.state) { case NPC_STATE_INIT: return mega_init(npc); case NPC_STATE_MOVE: return mega_walk(npc); case NPC_STATE_ATTACK: if (!mega_cover(npc)) return; break; case NPC_STATE_SUMMON: mega_summon(npc); break; case NPC_STATE_SUMMON2: mega_rebirth(npc); break; default: return; } if (npc_anim_is_done(npc)) npc.state = NPC_STATE_MOVE; }