316 lines
14 KiB
C++
316 lines
14 KiB
C++
#include "npc_basement.h"
|
|
#include "boss_ai.h"
|
|
#include "entity_effect.h"
|
|
#include "entity_player.h"
|
|
#include "room_grid.h"
|
|
#include <algorithm>
|
|
|
|
// The clotties this AI runs: the Clotty with its Clot, I.Blob and Grilled variants, the Mega Clotty and the Cloggy.
|
|
constexpr int ENTITY_GLOBIN_SPAWN = 24;
|
|
constexpr int ENTITY_MEGA_CLOTTY = 282;
|
|
constexpr int ENTITY_CLOGGY = 872;
|
|
constexpr int CLOT = 1;
|
|
constexpr int I_BLOB = 2;
|
|
constexpr int GRILLED_CLOTTY = 3;
|
|
|
|
constexpr int SOUND_ANIMAL_SQUISH = 166;
|
|
constexpr int BLOOD_EXPLOSION_SPURT = 2;
|
|
|
|
// They hop around (a Mega Clotty wading through more friction) and attack once a hop ends while the target is in
|
|
// range, waiting 30 frames between (45 for the Mega Clotty, 15 for the Grilled Clotty, who rests after three).
|
|
constexpr float CLOTTY_FRICTION = 0.9f;
|
|
constexpr float FLEE_FRICTION = 0.8f;
|
|
constexpr float MEGA_CLOTTY_FRICTION = 0.85f;
|
|
constexpr float CLOTTY_WAIT = 30;
|
|
constexpr float MEGA_CLOTTY_WAIT = 45;
|
|
constexpr float GRILLED_WAIT = 15;
|
|
constexpr int GRILLED_ATTACKS = 3;
|
|
constexpr uint32_t CLOTTY_LINGER_ODDS = 30;
|
|
constexpr float CLOTTY_RANGE = 1000;
|
|
constexpr int CLOTTY_RANGE_FRAMES = 7;
|
|
constexpr float HOP_THRESHOLD = 0.5f;
|
|
constexpr float CLOTTY_DEADZONE = 0.1f;
|
|
constexpr uint64_t FLAG_STUNNED = FLAG_FREEZE | FLAG_CHARM | FLAG_CONFUSION | FLAG_MIDAS_FREEZE;
|
|
|
|
// They bleed every 5 frames. Clotties shoot a plus (Clots a black cross, I.Blobs eight ways) at 7 (10 in hard mode),
|
|
// the Grilled Clotty a ring of four at 7 (8), the Cloggy a curving ring of four puke shots.
|
|
constexpr float BLEED_TICKS = 5;
|
|
constexpr float SPLAT_MIN = 0.5f;
|
|
constexpr float SPLAT_RANGE = 0.7f;
|
|
constexpr float SPURT_RISE = -20;
|
|
constexpr float CLOTTY_SHOT_SPEED = 7;
|
|
constexpr float CLOTTY_SHOT_SPEED_HARD = 10;
|
|
constexpr float GRILLED_SHOT_SPEED_HARD = 8;
|
|
constexpr float RING_SHOTS = 4;
|
|
constexpr float GRILLED_FALL = 0.02f;
|
|
constexpr float GRILLED_SPIN = 0.05f;
|
|
constexpr float CLOGGY_SHOT_SPEED = 8;
|
|
constexpr float CLOGGY_CURVE = 0.012f;
|
|
constexpr float CLOGGY_FALL = -0.02f;
|
|
constexpr float CLOGGY_ANGLE = 0.9424779f;
|
|
|
|
// The Mega Clotty shoots a ring of six at 10 three times in four, else spawns a Clotty or Globin (while fewer than
|
|
// five enemies are left) or spreads four pools of creep within 40, lasting 210 frames. It splits in two Clotties.
|
|
constexpr float MEGA_RING_SPEED = 10;
|
|
constexpr float MEGA_RING_SHOTS = 6;
|
|
constexpr float HALF_TURN_DEGREES = 180;
|
|
constexpr float FULL_TURN_DEGREES_CLOTTY = 360;
|
|
constexpr float MEGA_SOUND_VOLUME = 0.8f;
|
|
constexpr int MEGA_SPAWN_LIMIT = 5;
|
|
constexpr float MEGA_SPAWN_STEP = 40;
|
|
constexpr int MEGA_CREEPS = 4;
|
|
constexpr float MEGA_CREEP_SPREAD = 40;
|
|
constexpr float MEGA_CREEP_SCALE_MIN = 1;
|
|
constexpr float MEGA_CREEP_SCALE_RANGE = 0.5f;
|
|
constexpr int MEGA_CREEP_FRAMES = 210;
|
|
constexpr int MEGA_SPLIT = 2;
|
|
constexpr float MEGA_SPLIT_SPREAD = 20;
|
|
|
|
// A Clotty in a room with little water grills in fire, a Grilled Clotty in water cools down.
|
|
constexpr float GRILL_WATER_MAX = 0.2f;
|
|
constexpr uint64_t DAMAGE_FIRE_CLOTTY = 1ull << 1;
|
|
|
|
constexpr ColorMod CLOGGY_SPLAT_COLOR = {{0, 0, 0, 1}, {0, 0, 0, 0}, {0.25490198f, 0.16078432f, 0.13725491f}};
|
|
constexpr ColorMod CLOT_SPLAT_COLOR = {{0.02f, 0.05f, 0.05f, 1}, {0, 0, 0, 0}, {0, 0, 0}};
|
|
constexpr ColorMod GRILLED_SPLAT_COLOR = {{0, 0, 0, 1}, {0, 0, 0, 0}, {0.14117648f, 0.12156863f, 0.10980392f}};
|
|
constexpr ColorMod CLOT_SHOT_COLOR = {{1, 1, 1, 1}, {0.5f, 0.5f, 0.5f, 1}, {0, 0, 0}};
|
|
|
|
static bool is_grilled(Entity_NPC& npc)
|
|
{
|
|
return npc.type == ENTITY_CLOTTY && npc.variant == GRILLED_CLOTTY;
|
|
}
|
|
|
|
// Keeps a main animation playing, restarting it once it ends or another shows.
|
|
static void anim_loop(ANM2& sprite, std::string_view name)
|
|
{
|
|
const AnimationData* data = sprite.animState.animData;
|
|
bool isShown = data && data->name == name && (!data->isLooping || sprite.animState.isPlaying);
|
|
if (isShown || !sprite.SetAnimation(name, SWITCH_KEEP_FRAME)) return;
|
|
|
|
sprite.animState.Rewind();
|
|
sprite.animState.isPlaying = true;
|
|
}
|
|
|
|
// The first frame's blood: the Cloggy's brown (and which way its shots curve first), the Clot's black, the Grilled
|
|
// Clotty's burnt.
|
|
static void clotty_init(Entity_NPC& npc)
|
|
{
|
|
if (npc.type == ENTITY_CLOGGY)
|
|
{
|
|
npc.splatColor = CLOGGY_SPLAT_COLOR;
|
|
npc.i1 = (int)(npc.dropRNG.Next() & 1);
|
|
}
|
|
else if (npc.variant == CLOT) npc.splatColor = CLOT_SPLAT_COLOR;
|
|
else if (npc.variant == GRILLED_CLOTTY) npc.splatColor = GRILLED_SPLAT_COLOR;
|
|
}
|
|
|
|
// A dead Mega Clotty splits into two Clotties (fewer for a regenerating champion).
|
|
static void mega_clotty_split(Entity_NPC& npc)
|
|
{
|
|
for (int i = std::max(0, MEGA_SPLIT - npc.championRegenCount); i > 0; i--)
|
|
{
|
|
uint32_t seed = Random();
|
|
float angle = random_unit() * RANDOM_HALF_TURN;
|
|
angle += angle;
|
|
Vector at = vector_make(std::cos(angle) * MEGA_SPLIT_SPREAD + npc.pos.x,
|
|
std::sin(angle) * MEGA_SPLIT_SPREAD + npc.pos.y);
|
|
g_game->Spawn({.position = at, .spawner = &npc, .type = ENTITY_CLOTTY, .seed = seed});
|
|
}
|
|
}
|
|
|
|
// Hopping: the hop (idle when its player holds still), wandering until a hop lands in range and an attack starts,
|
|
// fleeing while feared or shrunk.
|
|
static void clotty_hop_update(Entity_NPC& npc)
|
|
{
|
|
bool isControlled = npc.controllerID != -1;
|
|
npc.v1.x = npc.type == ENTITY_MEGA_CLOTTY ? MEGA_CLOTTY_WAIT : is_grilled(npc) ? GRILLED_WAIT : CLOTTY_WAIT;
|
|
if (!isControlled || vector_length(npc.velocity) >= HOP_THRESHOLD ||
|
|
!vector_is_zero(npc.GetControllerInput(CLOTTY_DEADZONE)))
|
|
npc.AnimWalkFrame("Hop", "Hop", HOP_THRESHOLD);
|
|
else anim_loop(npc.sprite, "Idle");
|
|
|
|
if (npc.HasEntityFlags(FLAG_FEAR | FLAG_SHRINK) && !npc.HasEntityFlags(FLAG_STUNNED) && !isControlled)
|
|
{
|
|
npc.pathfinder.EvadeTarget(npc.GetPlayerTarget()->pos, STATUS_OBEYED);
|
|
if (npc.pathfinder.evadeMovementCountdown > 0) npc.friction *= FLEE_FRICTION;
|
|
}
|
|
else if (npc.pathfinder.MoveRandomly(STATUS_OBEYED) && !npc.HasEntityFlags(FLAG_CONFUSION) &&
|
|
(!isControlled || npc.IsTargetInAttackRange(CLOTTY_RANGE, CLOTTY_RANGE_FRAMES, false) ||
|
|
npc.IsControllerActionPressed(CONTROLLER_ACTION_SHOOT)))
|
|
{
|
|
if (is_grilled(npc) && npc.i2 >= GRILLED_ATTACKS) npc.i2 = 0;
|
|
else
|
|
{
|
|
npc.state = NPC_STATE_ATTACK;
|
|
npc.i2++;
|
|
npc.controllerMovement = isControlled ? npc.GetControllerMovementInput(CLOTTY_DEADZONE) : vector_make(0, 0);
|
|
anim_loop(npc.sprite, "Attack");
|
|
}
|
|
}
|
|
if (npc.type == ENTITY_MEGA_CLOTTY) npc.friction *= MEGA_CLOTTY_FRICTION;
|
|
}
|
|
|
|
// The Mega Clotty's way to attack: its ring (three times in four, or its player aiming sideways), else a spawn (one
|
|
// time in three, or aiming up for a Globin) or creep.
|
|
static bool mega_clotty_ring_get(Entity_NPC& npc, Direction aim)
|
|
{
|
|
if (npc.controllerID == -1) return (Random() & 3) != 0;
|
|
return aim == DIRECTION_LEFT || aim == DIRECTION_RIGHT;
|
|
}
|
|
|
|
// The Mega Clotty's "Shoot": a ring of six, a Clotty or Globin from a free spot near it, or four pools of creep.
|
|
static void mega_clotty_shoot(Entity_NPC& npc)
|
|
{
|
|
npc.PlaySound(SOUND_FORESTBOSS_STOMPS, MEGA_SOUND_VOLUME, 2, false, 1);
|
|
bool isControlled = npc.controllerID != -1;
|
|
Vector movement = npc.controllerMovement;
|
|
Direction aim = vector_is_zero(movement) ? DIRECTION_NONE : vector_to_direction(movement);
|
|
if (mega_clotty_ring_get(npc, aim))
|
|
{
|
|
ProjectileParams params;
|
|
params.circleAngle = random_unit() * FULL_TURN_DEGREES_CLOTTY - HALF_TURN_DEGREES;
|
|
npc.FireProjectiles(npc.pos, vector_make(MEGA_RING_SPEED, MEGA_RING_SHOTS), PROJECTILES_RING, params);
|
|
return;
|
|
}
|
|
|
|
bool isSpawning = isControlled ? aim != DIRECTION_NONE : Random() % 3 == 0;
|
|
if (isSpawning && npc.GetAliveEnemyCount() < MEGA_SPAWN_LIMIT)
|
|
{
|
|
bool isGlobin = isControlled ? aim == DIRECTION_UP : Random() % 3 == 0;
|
|
uint32_t seed = Random();
|
|
Vector spot = room_free_pickup_position_find({.position = npc.pos, .step = MEGA_SPAWN_STEP});
|
|
Entity* spawned = g_game->Spawn({.position = spot, .spawner = &npc,
|
|
.type = isGlobin ? ENTITY_GLOBIN_SPAWN : ENTITY_CLOTTY, .seed = seed});
|
|
if (!isGlobin) return;
|
|
|
|
auto* globin = static_cast<Entity_NPC*>(spawned);
|
|
globin->sprite.Play("ReGen");
|
|
globin->state = NPC_STATE_IDLE;
|
|
globin->Update();
|
|
return;
|
|
}
|
|
|
|
for (int i = 0; i < MEGA_CREEPS; i++)
|
|
{
|
|
uint32_t seed = Random();
|
|
float angle = random_unit() * RANDOM_HALF_TURN;
|
|
angle += angle;
|
|
float distance = random_unit() * MEGA_CREEP_SPREAD;
|
|
Vector at = vector_make(std::cos(angle) * distance + npc.pos.x, std::sin(angle) * distance + npc.pos.y);
|
|
auto* creep = static_cast<Entity_Effect*>(g_game->Spawn(
|
|
{.position = at, .spawner = &npc, .type = ENTITY_EFFECT, .variant = EFFECT_CREEP_RED, .seed = seed}));
|
|
float scale = random_unit() * MEGA_CREEP_SCALE_RANGE + MEGA_CREEP_SCALE_MIN;
|
|
creep->timeout = MEGA_CREEP_FRAMES;
|
|
creep->sprite.scale = vector_make(scale, scale);
|
|
creep->lifespan = MEGA_CREEP_FRAMES;
|
|
}
|
|
}
|
|
|
|
// The spurt of blood and squish a Clotty or Cloggy shoots with.
|
|
static void clotty_spurt(Entity_NPC& npc)
|
|
{
|
|
Vector rise = vector_make(0, SPURT_RISE);
|
|
blood_explosion_spawn(BLOOD_EXPLOSION_SPURT, npc.pos, rise, npc.splatColor, vector_make(0, 0));
|
|
npc_sound_play(SOUND_ANIMAL_SQUISH, 1, 2, 1);
|
|
}
|
|
|
|
// The Cloggy's "Shoot": a ring of four puke shots curving one way, then the other.
|
|
static void cloggy_shoot(Entity_NPC& npc)
|
|
{
|
|
clotty_spurt(npc);
|
|
ProjectileParams params;
|
|
params.variant = PROJECTILE_PUKE;
|
|
params.curvingStrength = CLOGGY_CURVE;
|
|
params.fallingAccelModifier = CLOGGY_FALL;
|
|
params.bulletFlags = npc.i1 == 0 ? PROJECTILE_CURVE_RIGHT : PROJECTILE_CURVE_LEFT;
|
|
params.circleAngle = npc.i1 == 0 ? -CLOGGY_ANGLE : CLOGGY_ANGLE;
|
|
npc.FireProjectiles(npc.pos, vector_make(CLOGGY_SHOT_SPEED, RING_SHOTS), PROJECTILES_RING, params);
|
|
npc.i1 = 1 - npc.i1;
|
|
}
|
|
|
|
// A Clotty's "Shoot": the Grilled Clotty's slowly turning ring, else a plus, a Clot's black cross or an I.Blob's eight.
|
|
// A variant past the I.Blob shoots a plus where the original reads a stale register.
|
|
static void clotty_shoot(Entity_NPC& npc)
|
|
{
|
|
clotty_spurt(npc);
|
|
ProjectileParams params;
|
|
if (npc.variant == GRILLED_CLOTTY)
|
|
{
|
|
params.fallingAccelModifier = GRILLED_FALL;
|
|
params.circleAngle = random_unit() * PI_F * GRILLED_SPIN;
|
|
float speed = game_is_hard_mode() ? GRILLED_SHOT_SPEED_HARD : CLOTTY_SHOT_SPEED;
|
|
npc.FireProjectiles(npc.pos, vector_make(speed, RING_SHOTS), PROJECTILES_RING, params);
|
|
return;
|
|
}
|
|
|
|
ProjectilesMode mode = npc.variant == CLOT ? PROJECTILES_CROSS : npc.variant == I_BLOB ? PROJECTILES_EIGHT
|
|
: PROJECTILES_PLUS;
|
|
if (npc.variant == CLOT) params.color = CLOT_SHOT_COLOR;
|
|
float speed = game_is_hard_mode() ? CLOTTY_SHOT_SPEED_HARD : CLOTTY_SHOT_SPEED;
|
|
npc.FireProjectiles(npc.pos, vector_make(speed, 0), mode, params);
|
|
}
|
|
|
|
// Attacking: once the attack ends (or one time in 30 after the wait) it hops again, else it plays the attack and
|
|
// shoots on its "Shoot". The wait runs down by the speed it moves at.
|
|
static void clotty_attack_update(Entity_NPC& npc)
|
|
{
|
|
AnimationState& animation = npc.sprite.animState;
|
|
if (!animation.animData || !animation.isPlaying || (Random() % CLOTTY_LINGER_ODDS != 0 && npc.v1.x <= 0))
|
|
npc.state = NPC_STATE_MOVE;
|
|
else
|
|
{
|
|
anim_loop(npc.sprite, "Attack");
|
|
bool isShooting = animation.animData && animation.IsEventTriggered("Shoot");
|
|
if (isShooting && npc.type == ENTITY_MEGA_CLOTTY) mega_clotty_shoot(npc);
|
|
else if (isShooting && npc.type == ENTITY_CLOGGY) cloggy_shoot(npc);
|
|
else if (isShooting) clotty_shoot(npc);
|
|
}
|
|
npc.v1.x -= npc.speedMultiplier;
|
|
}
|
|
|
|
// A Clotty grills when fire hits it in a dry room (the hit does no damage), and a Grilled Clotty cools in water.
|
|
static void clotty_grill_update(Entity_NPC& npc)
|
|
{
|
|
if (g_game->room->waterAmount > GRILL_WATER_MAX)
|
|
{
|
|
if (npc.variant == GRILLED_CLOTTY) npc.Morph(ENTITY_CLOTTY, 0, 0, -1);
|
|
return;
|
|
}
|
|
if (npc.variant != 0 && npc.variant != GRILLED_CLOTTY) return;
|
|
|
|
for (DamageEvent& event : npc.damageEvents)
|
|
{
|
|
if (!(event.damageFlags & DAMAGE_FIRE_CLOTTY)) continue;
|
|
|
|
npc.pendingDamage -= event.damage;
|
|
event.damage = 0;
|
|
if (npc.variant != 0) continue;
|
|
|
|
npc.Morph(ENTITY_CLOTTY, GRILLED_CLOTTY, 0, -1);
|
|
npc.splatColor = GRILLED_SPLAT_COLOR;
|
|
}
|
|
}
|
|
|
|
// 0050d370 The clotties' AI: they bleed, hop about at random and stop to shoot, the Mega Clotty spawning and splitting,
|
|
// the Cloggy curving its shots, a Clotty grilling in fire.
|
|
void npc_clotty_update(Entity_NPC& npc)
|
|
{
|
|
if (npc.state == NPC_STATE_INIT) clotty_init(npc);
|
|
if (npc.GetIntervalTicks(BLEED_TICKS, (float)(npc.index % (uint32_t)BLEED_TICKS)))
|
|
npc.MakeSplat(random_unit() * SPLAT_RANGE + SPLAT_MIN)
|
|
->SetColor(npc.splatColor, -1, COLOR_PRIORITY_DEFAULT, false, true);
|
|
if (npc.isDead && npc.type == ENTITY_MEGA_CLOTTY) mega_clotty_split(npc);
|
|
|
|
npc.friction *= CLOTTY_FRICTION;
|
|
if (npc.state == NPC_STATE_IDLE || npc.state == NPC_STATE_MOVE) clotty_hop_update(npc);
|
|
else if (npc.state == NPC_STATE_ATTACK) clotty_attack_update(npc);
|
|
else
|
|
{
|
|
npc.state = (Random() & 1) == 0 ? NPC_STATE_ATTACK : NPC_STATE_MOVE;
|
|
npc.v1.x = is_grilled(npc) ? GRILLED_WAIT : CLOTTY_WAIT;
|
|
anim_loop(npc.sprite, "Hop");
|
|
}
|
|
|
|
if (npc.type == ENTITY_CLOTTY) clotty_grill_update(npc);
|
|
}
|