343 lines
13 KiB
C++
343 lines
13 KiB
C++
#include "boss_ai.h"
|
|
#include "entity_laser.h"
|
|
#include "npc_caves.h"
|
|
#include "room_grid.h"
|
|
#include "sfx_manager.h"
|
|
#include <cmath>
|
|
|
|
// The Gate's subtypes: the red one that fires patterns of shots, and the black one that throws big spiders.
|
|
constexpr int GATE_RED = 1;
|
|
constexpr int GATE_BLACK = 2;
|
|
|
|
// The Gate's states: idle, the laser, the ring of shots, and the Leaper summon.
|
|
constexpr int GATE_LASER = NPC_STATE_ATTACK;
|
|
constexpr int GATE_RING = NPC_STATE_ATTACK2;
|
|
constexpr int GATE_SUMMON = NPC_STATE_SUMMON;
|
|
|
|
// The attacks' phases (i1): charging, then shooting.
|
|
enum GateCharge
|
|
{
|
|
GATE_CHARGING,
|
|
GATE_SHOOTING
|
|
};
|
|
|
|
// The red one's patterns (i2): triangles, curving shots, waving shots and homing shots.
|
|
enum GatePattern
|
|
{
|
|
GATE_PATTERN_TRIANGLE,
|
|
GATE_PATTERN_CURVE,
|
|
GATE_PATTERN_WAVE,
|
|
GATE_PATTERN_HOMING
|
|
};
|
|
|
|
constexpr float GATE_LIFT = 40;
|
|
constexpr float GATE_FRICTION = 0.8f;
|
|
constexpr float GATE_HURT_HEALTH = 0.3f;
|
|
constexpr int GATE_PERIOD = 20;
|
|
constexpr int GATE_HURT_PERIOD = 10;
|
|
constexpr int GATE_SUMMON_ENEMY_LIMIT = 5;
|
|
constexpr int GATE_DELIRIUM_LIMIT = 3;
|
|
constexpr int GATE_LASER_CROWD = 2;
|
|
constexpr int GATE_LASER_ODDS = 12;
|
|
constexpr float GATE_FULL_TURN = 360;
|
|
constexpr int GATE_RED_REST = 30;
|
|
|
|
constexpr int GATE_SOUND_DELAY = 60;
|
|
constexpr float GATE_LASER_ANGLE = 90;
|
|
constexpr int GATE_LASER_TIMEOUT = 10;
|
|
constexpr float GATE_LASER_HEIGHT = -30;
|
|
constexpr float GATE_LASER_DEPTH = 100;
|
|
constexpr uint32_t GATE_LASER_HOMING_FREEZE = 1;
|
|
|
|
constexpr float GATE_FIRE_HEIGHT = 18.5f;
|
|
constexpr float GATE_FIRE_ACCEL = -0.06f;
|
|
constexpr float GATE_FIRE_FALL = -2;
|
|
constexpr float GATE_FIRE_SPEEDUP = 1.05f;
|
|
constexpr float GATE_FIRE_SCALE = 1.8f;
|
|
constexpr float GATE_RING_SPEED = 10;
|
|
constexpr float GATE_RING_SHOTS = 9;
|
|
constexpr float GATE_RING_WIDE_SHOTS = 11;
|
|
constexpr float GATE_RING_PAIR_SHOTS = 6;
|
|
constexpr float GATE_RING_INNER_SPEED = 7;
|
|
|
|
constexpr float GATE_PATTERN_SCALE = 1.75f;
|
|
constexpr int GATE_PATTERN_TICKS = 3;
|
|
constexpr int GATE_HOMING_TICKS = 10;
|
|
constexpr float GATE_PATTERN_SHOTS = 8;
|
|
constexpr float GATE_CURVE_ACCEL = -0.05f;
|
|
constexpr float GATE_WAVE_RATE = 0.15f;
|
|
constexpr float GATE_WAVE_SWING = 0.2f;
|
|
constexpr float GATE_WAVE_SPEED = 5;
|
|
constexpr float GATE_WAVE_SHOTS = 7;
|
|
constexpr float GATE_HOMING_SPEED = 5;
|
|
constexpr float GATE_HOMING_SHOTS = 5;
|
|
constexpr float GATE_HOMING_STRENGTH = 0.3f;
|
|
constexpr float GATE_HOMING_TURN = 1.5f;
|
|
|
|
constexpr float GATE_SPIDER_DROP = 0.5f;
|
|
constexpr float GATE_SPIDER_HEIGHT = -100;
|
|
constexpr float GATE_LEAPER_HEIGHT = -60;
|
|
constexpr float GATE_LEAPER_REACH = 150;
|
|
constexpr int GATE_LEAPER_FRAME = 5;
|
|
constexpr int GATE_LEAPER_DEPTH = 100000;
|
|
constexpr float GATE_LEAPER_DELIRIUM_HEALTH = 10;
|
|
|
|
// The chance of a summon on its beat, halving with each enemy past one, and none from six.
|
|
static float gate_summon_chance(int enemies)
|
|
{
|
|
switch (enemies)
|
|
{
|
|
case 2:
|
|
return 0.5f;
|
|
case 3:
|
|
return 0.25f;
|
|
case 4:
|
|
return 0.125f;
|
|
case 5:
|
|
return 0.0625f;
|
|
case 6:
|
|
return 0;
|
|
default:
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
// Idle: on its beat (faster once hurt) a summon while few enemies are about, a laser more likely the more enemies
|
|
// there are (never for the black one), or a ring one time in two. The red one instead fires a new pattern once its
|
|
// rest runs out.
|
|
static void gate_idle(Entity_NPC& npc)
|
|
{
|
|
npc.sprite.Play("Idle");
|
|
bool isDelirium = npc.HasEntityFlags(FLAG_DELIRIUM_FORM);
|
|
if (isDelirium && room_is_clear()) return;
|
|
|
|
int period = npc.health / npc.maxHitPoints < GATE_HURT_HEALTH ? GATE_HURT_PERIOD : GATE_PERIOD;
|
|
int enemies = g_game->room->GetAliveEnemiesCount();
|
|
float chance = gate_summon_chance(enemies);
|
|
bool isSummon = enemies < GATE_SUMMON_ENEMY_LIMIT;
|
|
if (isDelirium && npc.subtype == GATE_BLACK)
|
|
isSummon = npc.CountRelatedEntities(ENTITY_STRIDER, 0, nullptr) +
|
|
npc.CountRelatedEntities(ENTITY_BIG_SPIDER, 0, nullptr) +
|
|
npc.CountRelatedEntities(ENTITY_SPIDER, 0, nullptr) <
|
|
GATE_DELIRIUM_LIMIT;
|
|
else if (isDelirium) isSummon = npc.CountRelatedEntities(ENTITY_LEAPER, 0, nullptr) < GATE_DELIRIUM_LIMIT;
|
|
|
|
if (isSummon && npc.IsFrame(period, 0) && random_unit() < chance)
|
|
{
|
|
npc.state = GATE_SUMMON;
|
|
npc.i1 = GATE_CHARGING;
|
|
}
|
|
else if (npc.subtype != GATE_BLACK && enemies > GATE_LASER_CROWD && npc.IsFrame(period, 0) &&
|
|
random_below(GATE_LASER_ODDS / enemies) == 0 && !npc.HasEntityFlags(FLAG_DISTRACTED))
|
|
{
|
|
npc.state = GATE_LASER;
|
|
npc.i1 = GATE_CHARGING;
|
|
}
|
|
else if (npc.IsFrame(period, 0) && (Random() & 1) == 0)
|
|
{
|
|
npc.state = GATE_RING;
|
|
npc.i1 = GATE_CHARGING;
|
|
}
|
|
if (npc.subtype != GATE_RED) return;
|
|
|
|
npc.state = GATE_RING;
|
|
npc.i1 = GATE_CHARGING;
|
|
npc.i2 = (int)(Random() & 3);
|
|
npc.v1.x = random_unit() * GATE_FULL_TURN - GATE_FULL_TURN / 2;
|
|
npc.v1.y = (Random() & 1) ? 1.0f : -1.0f;
|
|
if (npc.stateFrame-- > 0) npc.state = NPC_STATE_IDLE;
|
|
}
|
|
|
|
// The charge before an attack, its sound looping, then the shooting.
|
|
static void gate_charge(Entity_NPC& npc, SoundId sound)
|
|
{
|
|
npc.sprite.Play("Charging");
|
|
npc.PlaySound(sound, 1, GATE_SOUND_DELAY, false, 1);
|
|
if (npc_anim_is_done(npc)) npc.i1 = GATE_SHOOTING;
|
|
}
|
|
|
|
// The laser: a freezing beam straight down from its mouth.
|
|
static void gate_laser(Entity_NPC& npc)
|
|
{
|
|
if (npc.i1 == GATE_CHARGING) return gate_charge(npc, SOUND_MOUTH_FULL);
|
|
if (npc.i1 != GATE_SHOOTING) return;
|
|
|
|
npc.sprite.Play("Shooting");
|
|
if (npc.sprite.IsEventTriggered("Shoot"))
|
|
{
|
|
npc.PlaySound(SOUND_GHOST_ROAR, 1, 2, false, 1);
|
|
Entity_Laser* laser = Entity_Laser::ShootAngle({.position = npc.pos,
|
|
.posOffset = vector_make(0, GATE_LASER_HEIGHT),
|
|
.source = &npc,
|
|
.angleDegrees = GATE_LASER_ANGLE,
|
|
.timeout = GATE_LASER_TIMEOUT,
|
|
.variant = LASER_THICK_RED});
|
|
laser->renderDepthOffset = GATE_LASER_DEPTH;
|
|
laser->homingType = GATE_LASER_HOMING_FREEZE;
|
|
laser->Update();
|
|
}
|
|
if (npc_anim_is_done(npc)) npc.state = NPC_STATE_IDLE;
|
|
}
|
|
|
|
// The fire ring: big shots rising and orbiting it one way, the other, or both in two rings, or a wider ring.
|
|
static void gate_fire_ring(Entity_NPC& npc)
|
|
{
|
|
npc.PlaySound(SOUND_FIRE_RUSH, 1, 2, false, 1);
|
|
ProjectileParams params;
|
|
params.targetPosition = npc.pos;
|
|
params.bulletFlags |= PROJECTILE_HIT_ENEMIES | PROJECTILE_NO_WALL_COLLIDE;
|
|
params.heightModifier = GATE_FIRE_HEIGHT;
|
|
params.fallingAccelModifier = GATE_FIRE_ACCEL;
|
|
params.fallingSpeedModifier = GATE_FIRE_FALL;
|
|
params.isGridCollision = false;
|
|
params.velocityMulti = GATE_FIRE_SPEEDUP;
|
|
params.scale = GATE_FIRE_SCALE;
|
|
|
|
Vector ring = vector_make(GATE_RING_SPEED, GATE_RING_SHOTS);
|
|
switch (Random() & 3)
|
|
{
|
|
case 0:
|
|
params.bulletFlags |= PROJECTILE_ORBIT_CW;
|
|
break;
|
|
case 1:
|
|
params.bulletFlags |= PROJECTILE_ORBIT_CCW;
|
|
break;
|
|
case 2:
|
|
params.bulletFlags |= PROJECTILE_ORBIT_CW;
|
|
npc.FireProjectiles(npc.pos, vector_make(GATE_RING_SPEED, GATE_RING_PAIR_SHOTS), PROJECTILES_RING, params);
|
|
params.bulletFlags |= PROJECTILE_ORBIT_CCW;
|
|
ring = vector_make(GATE_RING_INNER_SPEED, GATE_RING_PAIR_SHOTS);
|
|
break;
|
|
case 3:
|
|
ring.y = GATE_RING_WIDE_SHOTS;
|
|
break;
|
|
}
|
|
npc.FireProjectiles(npc.pos, ring, PROJECTILES_RING, params);
|
|
}
|
|
|
|
// The red one's pattern, a ring every few ticks after Shoot: triangles, shots curving its spin's way, waving shots, or
|
|
// slow homing shots turning with time.
|
|
static void gate_pattern(Entity_NPC& npc)
|
|
{
|
|
if (!npc.sprite.WasEventTriggered("Shoot")) return;
|
|
ProjectileParams params;
|
|
params.targetPosition = npc.pos;
|
|
params.heightModifier = GATE_FIRE_HEIGHT;
|
|
params.fallingAccelModifier = GATE_FIRE_ACCEL;
|
|
params.fallingSpeedModifier = GATE_FIRE_FALL;
|
|
params.scale = GATE_PATTERN_SCALE;
|
|
|
|
Vector ring = vector_make(GATE_RING_SPEED, GATE_PATTERN_SHOTS);
|
|
float age = (float)(g_game->frameCount - npc.spawnFrame);
|
|
switch (npc.i2)
|
|
{
|
|
case GATE_PATTERN_TRIANGLE:
|
|
if (!npc.IsFrame(GATE_PATTERN_TICKS, npc.index)) return;
|
|
params.circleAngle = npc.v1.x;
|
|
params.bulletFlags = PROJECTILE_TRIANGLE;
|
|
break;
|
|
case GATE_PATTERN_CURVE:
|
|
if (!npc.IsFrame(GATE_PATTERN_TICKS, npc.index)) return;
|
|
params.circleAngle = npc.v1.x;
|
|
params.fallingAccelModifier = GATE_CURVE_ACCEL;
|
|
params.bulletFlags |= (npc.v1.y >= 0 ? PROJECTILE_CURVE_RIGHT : PROJECTILE_CURVE_LEFT) |
|
|
PROJECTILE_NO_WALL_COLLIDE;
|
|
break;
|
|
case GATE_PATTERN_WAVE:
|
|
if (!npc.IsFrame(GATE_PATTERN_TICKS, npc.index)) return;
|
|
params.bulletFlags |= PROJECTILE_CURVE_LEFT | PROJECTILE_SINE_VELOCITY;
|
|
params.circleAngle = std::sin(age * GATE_WAVE_RATE) * GATE_WAVE_SWING + npc.v1.x;
|
|
ring = vector_make(GATE_WAVE_SPEED, GATE_WAVE_SHOTS);
|
|
break;
|
|
case GATE_PATTERN_HOMING:
|
|
if (!npc.IsFrame(GATE_HOMING_TICKS, npc.index)) return;
|
|
ring = vector_make(GATE_HOMING_SPEED, GATE_HOMING_SHOTS);
|
|
params.bulletFlags |= PROJECTILE_SMART;
|
|
params.homingStrength = GATE_HOMING_STRENGTH;
|
|
params.circleAngle = age * GATE_HOMING_TURN + npc.v1.x;
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
npc.FireProjectiles(npc.pos, ring, PROJECTILES_RING, params);
|
|
}
|
|
|
|
// The ring: charged, then a fire ring on Shoot, or the red one's pattern. The red one rests a while after.
|
|
static void gate_ring(Entity_NPC& npc)
|
|
{
|
|
if (npc.i1 == GATE_CHARGING) return gate_charge(npc, SOUND_LOW_INHALE);
|
|
if (npc.i1 != GATE_SHOOTING) return;
|
|
|
|
npc.sprite.Play("Shooting");
|
|
if (npc.subtype == GATE_RED) gate_pattern(npc);
|
|
else if (npc.sprite.IsEventTriggered("Shoot")) gate_fire_ring(npc);
|
|
if (!npc_anim_is_done(npc)) return;
|
|
npc.state = NPC_STATE_IDLE;
|
|
npc.stateFrame = GATE_RED_REST;
|
|
}
|
|
|
|
// The summon: a Leaper leaping out over it to land nearby, or a big spider thrown at its target by the black one.
|
|
static void gate_summon(Entity_NPC& npc)
|
|
{
|
|
npc.sprite.Play("Spawn");
|
|
npc.PlaySound(SOUND_MONSTER_GRUNT_4, 1, GATE_SOUND_DELAY, false, 1);
|
|
if (npc.sprite.IsEventTriggered("Shoot"))
|
|
{
|
|
npc.PlaySound(SOUND_MONSTER_GRUNT_5, 1, 2, false, 1);
|
|
if (npc.subtype == GATE_BLACK)
|
|
{
|
|
Entity* target = npc.GetPlayerTarget();
|
|
Vector mouth = vector_make(npc.pos.x, npc.pos.y + GATE_SPIDER_DROP);
|
|
Entity_NPC::ThrowSpider(mouth, &npc, target->pos, true, GATE_SPIDER_HEIGHT);
|
|
}
|
|
else
|
|
{
|
|
Vector mouth = vector_make(npc.pos.x, npc.pos.y + GATE_LEAPER_HEIGHT);
|
|
auto* leaper = static_cast<Entity_NPC*>(
|
|
g_game->Spawn({.position = mouth, .spawner = &npc, .type = ENTITY_LEAPER, .seed = Random()}));
|
|
leaper->targetPosition = room_free_pickup_position_find({.position = npc.pos, .step = GATE_LEAPER_REACH});
|
|
leaper->state = NPC_STATE_JUMP;
|
|
leaper->stateFrame = GATE_LEAPER_FRAME;
|
|
npc_anim_restart(leaper->sprite, "BigJumpUp");
|
|
if (leaper->sprite.animState.animData) leaper->sprite.animState.SetFrameFloat(GATE_LEAPER_FRAME);
|
|
leaper->renderZOffset = GATE_LEAPER_DEPTH;
|
|
if (npc.HasEntityFlags(FLAG_DELIRIUM_FORM))
|
|
{
|
|
leaper->health = GATE_LEAPER_DELIRIUM_HEALTH;
|
|
leaper->maxHitPoints = GATE_LEAPER_DELIRIUM_HEALTH;
|
|
}
|
|
}
|
|
}
|
|
if (!npc_anim_is_done(npc)) return;
|
|
npc.state = NPC_STATE_IDLE;
|
|
npc.i1 = 0;
|
|
}
|
|
|
|
// 0045fc00 NpcAI::ENTITY_GATE: the Gate hangs fixed on the wall, summons Leapers, fires a freezing laser down and
|
|
// rings of rising fire. The red one fires patterns of shots, and the black one throws big spiders.
|
|
void boss_gate_update(Entity_NPC& npc)
|
|
{
|
|
if (npc.state == NPC_STATE_INIT)
|
|
{
|
|
npc.state = NPC_STATE_IDLE;
|
|
npc.stateFrame = 0;
|
|
if (npc.deliriumBossType == 0) npc.pos.y -= GATE_LIFT;
|
|
npc.targetPosition = npc.pos;
|
|
return;
|
|
}
|
|
|
|
npc.friction *= GATE_FRICTION;
|
|
npc.velocity = vector_sub(npc.targetPosition, npc.pos);
|
|
switch (npc.state)
|
|
{
|
|
case NPC_STATE_IDLE:
|
|
return gate_idle(npc);
|
|
case GATE_LASER:
|
|
return gate_laser(npc);
|
|
case GATE_RING:
|
|
return gate_ring(npc);
|
|
case GATE_SUMMON:
|
|
return gate_summon(npc);
|
|
}
|
|
}
|