Files
isaac-port/src/grid_entity_pressure_plate.cpp
T

486 lines
20 KiB
C++

#include "grid_entity_pressure_plate.h"
#include "entity_pickup.h"
#include "entity_player.h"
#include "font.h"
#include "game.h"
#include "grid_entity_door.h"
#include "grid_entity_poop.h"
#include "manager.h"
#include "npc_ai.h"
#include "room.h"
#include "room_grid.h"
#include <algorithm>
#include <cstdio>
#include <span>
// Who presses a plate, and how close: the entities a query finds around it, within reach of its center.
constexpr float PLATE_QUERY_RADIUS = 60;
constexpr float PLATE_PRESS_RADIUS = 30;
// An unpressed plate claims its cell's path, and a plain one holds the room's clear back.
constexpr int PLATE_PATH = 300;
constexpr int ROOM_CLEAR_HOLD = 10;
// A reward plate whose ambush was left unbeaten spawns it again this many frames into the next visit.
constexpr int AMBUSH_RESPAWN_FRAME = 4;
// Greed's timer under the button: seconds and minutes, how far below the button it shows, and its grey blink.
constexpr int TIMER_FRAMES_PER_SECOND = 30;
constexpr int SECONDS_PER_MINUTE = 60;
constexpr int TIMER_TEXT_SIZE = 32;
constexpr int TIMER_BLINK_FRAMES = 4;
constexpr float TIMER_PLATE_DROP = 21;
constexpr float TIMER_TEXT_DROP = 13;
constexpr KColor TIMER_WHITE = kcolor_rgba(1, 1, 1, 1);
constexpr KColor TIMER_GREY = kcolor_rgba(0.3f, 0.3f, 0.3f, 1);
// The wave Greed's button shows the last boss's pentagram from, one later in Greedier.
constexpr uint32_t GREED_FINAL_BOSS_WAVE = 11;
// What a reward plate's roll out of 100 gives below each bound, the most of a pickup it drops at once, and the
// chests it picks from.
constexpr int REWARD_ROLLS = 100;
constexpr int ROLL_TRAPDOOR = 5;
constexpr int ROLL_GOLDEN_POOP = 10;
constexpr int ROLL_POOP = 11;
constexpr int ROLL_ITEM = 12;
constexpr int ROLL_COINS = 30;
constexpr int ROLL_HEARTS = 50;
constexpr int ROLL_BOMBS = 70;
constexpr int ROLL_CHEST = 80;
constexpr int ROLL_SLOT = 85;
constexpr int ROLL_DONATION = 90;
constexpr uint32_t REWARD_COINS_MAX = 3;
constexpr uint32_t REWARD_HEARTS_MAX = 2;
constexpr uint32_t REWARD_BOMBS_MAX = 2;
constexpr int REWARD_CHESTS[] = {PICKUP_CHEST, PICKUP_LOCKEDCHEST, PICKUP_BOMBCHEST, PICKUP_REDCHEST};
// The slots and bomb a reward plate drops (Lua's SlotVariant and BombVariant).
constexpr int SLOT_MACHINE = 1;
constexpr int SLOT_FORTUNE_TELLING = 3;
constexpr int SLOT_DONATION = 8;
constexpr int BOMB_TROLL = 3;
// An ambush brings two to four enemies, near the doors no player stands within reach of, scattered a little.
constexpr uint32_t AMBUSH_EXTRA_MAX = 3;
constexpr int AMBUSH_MIN = 2;
constexpr float AMBUSH_DOOR_REACH = 80;
constexpr float AMBUSH_SCATTER = 20;
constexpr uint32_t AMBUSH_START_MASK = 3;
// The doors of the secret rooms, which an ambush shuts with their plain open animation.
constexpr int SECRET_DOOR_ROOM_TYPES[] = {ROOM_SECRET, ROOM_SUPERSECRET, ROOM_ULTRASECRET};
// The void's backdrops take the ambush of the floor they copy, by their order: Basement, Caves, Depths and Womb three
// each, then Blue Womb, Sheol, Cathedral, Dark Room and Chest.
constexpr int VOID_BACKDROPS_PER_CHAPTER = 3;
constexpr int VOID_CHAPTERS = 4;
constexpr int VOID_BACKDROP_BLUE_WOMB = 13;
constexpr int VOID_BACKDROP_LAST = 17;
// An enemy an ambush brings.
struct AmbushEnemy
{
int type = 0;
int variant = 0;
};
// The ambushes of each floor: the chapters, the Blue Womb, Sheol and the Cathedral, the Dark Room and the Chest, and
// Greed mode's shop floor and last floor.
constexpr AmbushEnemy AMBUSH_BASEMENT[] = {{18}, {222}, {256}, {12}, {23}, {85}};
constexpr AmbushEnemy AMBUSH_CAVES[] = {{23}, {25}, {22}, {205}, {26}, {234}};
constexpr AmbushEnemy AMBUSH_DEPTHS[] = {{38}, {24}, {227}, {41}, {209}, {237}};
constexpr AmbushEnemy AMBUSH_WOMB[] = {{57}, {223}, {56}, {213}, {216}};
constexpr AmbushEnemy AMBUSH_BLUE_WOMB[] = {{23, 1}, {257, 1}};
constexpr AmbushEnemy AMBUSH_SHEOL[] = {{404}, {50}, {405}};
constexpr AmbushEnemy AMBUSH_CATHEDRAL[] = {{20}, {79}, {67}};
constexpr AmbushEnemy AMBUSH_DARK_ROOM[] = {{404}, {252}};
constexpr AmbushEnemy AMBUSH_CHEST[] = {{20}, {51, 1}};
constexpr AmbushEnemy AMBUSH_GREED_SHOP[] = {{20}, {51, 1}, {404}, {252}};
constexpr AmbushEnemy AMBUSH_GREED_LAST[] = {{102}};
constexpr std::span<const AmbushEnemy> AMBUSH_VOID[] = {AMBUSH_BASEMENT, AMBUSH_CAVES, AMBUSH_DEPTHS,
AMBUSH_WOMB, AMBUSH_BLUE_WOMB, AMBUSH_SHEOL,
AMBUSH_CATHEDRAL, AMBUSH_DARK_ROOM, AMBUSH_CHEST};
static Entity* entity_spawn(int type, int variant, Vector position, uint32_t seed)
{
return g_game->Spawn({.position = position, .type = type, .variant = variant, .seed = seed});
}
// The entities near a position, as EntityList::QueryRadius finds them in the enemy partition and among the players:
// those whose size reaches into the radius and that queries may see.
static EntityList_EL radius_query(Vector center, float radius)
{
EntityList_EL found;
g_game->room->entityList.partitions[PARTITION_ENEMIES].query_box(found, center, vector_make(radius, radius));
for (Entity_Player* player : g_game->playerManager.playerList) found.push_back(player);
std::erase_if(found, [&](Entity* entity) {
float reach = radius + entity->size;
return entity->HasEntityFlags(FLAG_NO_QUERY) || vector_length_squared(vector_sub(entity->pos, center)) >= reach * reach;
});
return found;
}
// Whether an entity presses plates: a coop ghost always, a living player but a hologram, a living Dople or Grudge.
static bool is_presser(Entity& entity)
{
if (entity.type != ENTITY_PLAYER) return !entity.isDead && (entity.type == ENTITY_DOPLE || entity.type == ENTITY_GRUDGE);
Entity_Player& player = static_cast<Entity_Player&>(entity);
return player.isCoopGhost || (!player.isDead && !player.IsHologram());
}
// Whether one who presses plates stands on a plate's center.
static bool is_plate_stood_on(Vector center)
{
return std::ranges::any_of(radius_query(center, PLATE_QUERY_RADIUS), [&](Entity* entity) {
return is_presser(*entity) &&
vector_length_squared(vector_sub(center, entity->pos)) < PLATE_PRESS_RADIUS * PLATE_PRESS_RADIUS;
});
}
// 00985f00 Loads the plate with its variant's spritesheet, Greed's timer plate, and starts it off or on.
void GridEntity_PressurePlate::subclass_init()
{
sprite.Load("gfx/grid/grid_pressureplate.anm2", GRAPHICS_DEFER);
nextGreedAnimation = "";
switch (variant)
{
case PLATE_ROOM_CLEAR:
sprite.ReplaceSpritesheet(0, "gfx/grid/grid_pressureplate_silver.png");
if (state == PLATE_UNPRESSED) g_game->room->roomClearDelay = ROOM_CLEAR_HOLD;
break;
case PLATE_GREED:
timerPlate.Load("gfx/grid/TimerPlate.anm2", GRAPHICS_LOAD);
timerPlate.Play("Idle");
break;
case PLATE_RAIL: sprite.ReplaceSpritesheet(0, "gfx/grid/grid_button_rail.png"); break;
case PLATE_KILL: sprite.ReplaceSpritesheet(0, "gfx/grid/grid_button_kill.png"); break;
default:
if (variant >= PLATE_OUTPUT_FIRST && variant <= PLATE_OUTPUT_LAST)
sprite.ReplaceSpritesheet(0, "gfx/grid/grid_button_output.png");
}
sprite.LoadGraphics();
if (variant == PLATE_GREED)
{
update_greed_button_anim(GREED_BUTTON_NORMAL);
play_pending_animation();
}
else if (state == PLATE_UNPRESSED) sprite.Play("Off");
else if (state == PLATE_PRESSED || state == PLATE_AMBUSH) sprite.Play("On");
greedModeRNG = {spawnSeed, 4, 3, 27};
}
// 00983330 Animates the plate, runs Greed's button or the press, and claims the cell's path while unpressed.
void GridEntity_PressurePlate::Update()
{
sprite.Update();
if (variant == PLATE_GREED) update_greed_button();
else update_press();
if (state == PLATE_UNPRESSED && (uint32_t)gridIndex < ROOM_GRID_CELLS && g_game->room->gridPaths[gridIndex] < PLATE_PATH)
path_set(PLATE_PATH);
}
// Greed mode's wave timer under the button in minutes and seconds, grey once it ran out and blinking grey at zero.
static void greed_timer_render(Vector position)
{
Game& game = *g_game;
Font& font = g_manager->font4_terminus;
int timer = game.room->greedWaveTimer;
int seconds = (timer - 1) / TIMER_FRAMES_PER_SECOND;
int minutes = seconds / SECONDS_PER_MINUTE;
char text[TIMER_TEXT_SIZE];
std::snprintf(text, sizeof text, "%02d:%02d", minutes, seconds % SECONDS_PER_MINUTE);
bool isZero = minutes < 1 && seconds % SECONDS_PER_MINUTE < 1;
bool isGrey = timer <= 0 || (isZero && game.frameCount % TIMER_BLINK_FRAMES <= 1);
float x = position.x - (float)(font.GetStringWidth(text) / 2) + game.screenShakeOffset.x;
float y = position.y + TIMER_TEXT_DROP + game.screenShakeOffset.y;
FontSettings settings;
font.DrawString(text, vector_make(x, y), vector_make(1, 1), isGrey ? TIMER_GREY : TIMER_WHITE, settings);
}
// 00985c10 Draws the plate in the floor's color, Greed's timer plate and its timer.
void GridEntity_PressurePlate::Render(const Vector& offset)
{
Vector position = vector_add(get_render_position(), offset);
GridEntity::Render(offset);
timerPlate.Render({.position = vector_make(position.x, position.y + TIMER_PLATE_DROP)});
if (variant == PLATE_GREED) greed_timer_render(position);
}
// 00986430 Water stands raised around the plate.
WaterClipInfo GridEntity_PressurePlate::GetWaterClipInfo()
{
return {WATER_CLIP_RAISED, 0};
}
// 009833c0 A player (or a Dople or Grudge) on an unpressed plate switches it with a click, and once switched it
// turns on and rewards. A plain plate holds the room's clear while up. A reward plate whose ambush was left unbeaten
// brings it back on the next visit, and counts as pressed once the room is clear.
void GridEntity_PressurePlate::update_press()
{
Game& game = *g_game;
Room& room = *game.room;
if (state == PLATE_UNPRESSED && is_plate_stood_on(Position()))
{
if (!sprite.IsPlaying("Switched")) grid_sound_play(SOUND_BUTTON_PRESS);
sprite.Play("Switched");
}
if (!sprite.IsPlaying() && state == PLATE_UNPRESSED)
{
state = PLATE_PRESSED;
sprite.Play("On");
Reward();
}
if (state == PLATE_UNPRESSED && variant == PLATE_ROOM_CLEAR) room.roomClearDelay = ROOM_CLEAR_HOLD;
if (state != PLATE_AMBUSH || variant != PLATE_REWARD || !room.isInitialized) return;
int frames = game.frameCount - room.enterFrame;
if (frames == AMBUSH_RESPAWN_FRAME) Reward();
else if (frames > AMBUSH_RESPAWN_FRAME && room.IsClear()) state = PLATE_PRESSED;
}
// Greed's button for the coming wave: the last boss wave's look, a boss wave's, or the plain one.
static std::string_view greed_look_pick(std::string_view finalBoss, std::string_view boss, std::string_view plain)
{
Game& game = *g_game;
uint32_t wave = (uint32_t)game.greedModeWave + 1;
uint32_t greedier = game.difficulty == DIFFICULTY_GREEDIER;
if (!game.IsGreedMode()) return plain;
if (wave >= greedier + GREED_FINAL_BOSS_WAVE) return finalBoss;
return wave >= greedier + GREED_BOSS_WAVE ? boss : plain;
}
// 009837b0 Picks the animation Greed's button plays next for its state: off (red starting at once), switched, or on.
void GridEntity_PressurePlate::update_greed_button_anim(GreedButtonColor color)
{
bool isRed = color == GREED_BUTTON_RED;
switch (state)
{
case PLATE_UNPRESSED:
if (!isRed)
{
nextGreedAnimation = greed_look_pick("OffPentagram", "OffSkull", "Off");
return;
}
sprite.Play("OffRedStart");
nextGreedAnimation = "OffRed";
return;
case PLATE_GREED_PRESSED:
case PLATE_GREED_HELD:
nextGreedAnimation = isRed ? "SwitchedRed" : greed_look_pick("SwitchedPentagram", "SwitchedSkull", "Switched");
return;
case PLATE_PRESSED: nextGreedAnimation = greed_look_pick("OnPentagram", "OnSkull", "On"); return;
default: return;
}
}
// 009838c0 Plays Greed's button's next animation unless it shows already or the red start still plays.
void GridEntity_PressurePlate::play_pending_animation()
{
if (sprite.GetAnimation() == nextGreedAnimation || sprite.IsPlaying("OffRedStart")) return;
sprite.Play(nextGreedAnimation);
}
// 009839f0 Runs Greed mode's wave button.
void GridEntity_PressurePlate::update_greed_button()
{
reconstruction_missing("GridEntity_PressurePlate::update_greed_button 009839f0");
}
// Where a slot fits: the spot, when a cell beside it on both axes is free, else a free spot near the room's center.
static Vector slot_position_get(Vector position)
{
Room& room = *g_game->room;
auto isFree = [&](float x, float y) {
return room.GetGridCollision(room.GetGridIndex(vector_make(position.x + x, position.y + y))) == COLLISION_NONE;
};
if ((isFree(GRID_CELL_SIZE, 0) || isFree(-GRID_CELL_SIZE, 0)) && (isFree(0, GRID_CELL_SIZE) || isFree(0, -GRID_CELL_SIZE)))
return position;
return room_free_pickup_position_find({.position = Room::GetCenterPos(), .isAvoidActiveEntities = true});
}
// A reward plate's reward, by a roll out of 100: a trapdoor (golden poop where none may open), golden poop, poop, an
// item, coins, hearts or bombs, a chest, a slot machine or fortune teller, a donation machine, or a troll bomb.
static void plate_reward_spawn(GridEntity& plate, RNG& rng)
{
Room& room = *g_game->room;
grid_sound_play(SOUND_THUMBSUP);
Vector position = room_free_pickup_position_find({.position = plate.Position(), .isAvoidActiveEntities = true});
int roll = (int)(rng.Next() % REWARD_ROLLS);
auto pickupsSpawn = [&](int variant, uint32_t most) {
for (int i = (int)rng.RandomInt(most) + 1; i > 0; i--)
entity_spawn(ENTITY_PICKUP, variant, room_free_pickup_position_find({.position = plate.Position(), .isAvoidActiveEntities = true}), rng.Next());
};
if (roll < ROLL_TRAPDOOR && g_game->CanSpawnTrapDoor())
{
uint32_t seed = rng.Next();
room.SpawnGridEntity(room.GetGridIndex(position), GRID_TRAPDOOR, 0, seed, 0);
}
else if (roll < ROLL_POOP)
{
uint32_t seed = rng.Next();
room.SpawnGridEntity(room.GetGridIndex(position), GRID_POOP, roll < ROLL_GOLDEN_POOP ? POOP_GOLDEN : POOP_NORMAL, seed, 0);
}
else if (roll < ROLL_ITEM) entity_spawn(ENTITY_PICKUP, PICKUP_COLLECTIBLE, position, rng.Next());
else if (roll < ROLL_COINS) pickupsSpawn(PICKUP_COIN, REWARD_COINS_MAX);
else if (roll < ROLL_HEARTS) pickupsSpawn(PICKUP_HEART, REWARD_HEARTS_MAX);
else if (roll < ROLL_BOMBS) pickupsSpawn(PICKUP_BOMB, REWARD_BOMBS_MAX);
else if (roll < ROLL_CHEST)
{
uint32_t seed = rng.Next();
entity_spawn(ENTITY_PICKUP, REWARD_CHESTS[rng.RandomInt(std::size(REWARD_CHESTS))], position, seed);
}
else if (roll < ROLL_SLOT)
{
position = slot_position_get(position);
uint32_t seed = rng.Next();
entity_spawn(ENTITY_SLOT, rng.RandomInt(2) != 0 ? SLOT_FORTUNE_TELLING : SLOT_MACHINE, position, seed);
}
else if (roll < ROLL_DONATION)
{
position = slot_position_get(position);
entity_spawn(ENTITY_SLOT, SLOT_DONATION, position, rng.Next());
}
else entity_spawn(ENTITY_BOMB, BOMB_TROLL, position, rng.Next());
}
// The enemies an ambush draws from: by Greed's floor, by the floor (the Sheol and Dark Room sides of the last
// chapters apart), and in the Void by the backdrop of the floor it copies.
static std::span<const AmbushEnemy> ambush_pool_get()
{
Game& game = *g_game;
bool isOriginal = game.stageType == STAGETYPE_ORIGINAL;
if (game.IsGreedMode())
{
switch (game.stage)
{
case 2: return AMBUSH_CAVES;
case 3: return AMBUSH_DEPTHS;
case 4: return AMBUSH_WOMB;
case 5: return AMBUSH_SHEOL;
case 6: return AMBUSH_GREED_SHOP;
case 7: return AMBUSH_GREED_LAST;
default: return AMBUSH_BASEMENT;
}
}
switch (game.stage)
{
case 3:
case 4: return AMBUSH_CAVES;
case 5:
case 6: return AMBUSH_DEPTHS;
case 7:
case 8: return AMBUSH_WOMB;
case 9: return AMBUSH_BLUE_WOMB;
case 10: return isOriginal ? AMBUSH_SHEOL : AMBUSH_CATHEDRAL;
case 11: return isOriginal ? AMBUSH_DARK_ROOM : AMBUSH_CHEST;
case 12:
{
int backdrop = game.room->backdropType;
if (backdrop < 1 || backdrop > VOID_BACKDROP_LAST) return AMBUSH_DARK_ROOM;
if (backdrop < VOID_BACKDROP_BLUE_WOMB) return AMBUSH_VOID[(backdrop - 1) / VOID_BACKDROPS_PER_CHAPTER];
return AMBUSH_VOID[backdrop - VOID_BACKDROP_BLUE_WOMB + VOID_CHAPTERS];
}
default: return AMBUSH_BASEMENT;
}
}
// The doors no player stands near, where an ambush comes in, or the room's center when every door has a player.
static std::vector<Vector> ambush_positions_get()
{
Room& room = *g_game->room;
std::vector<Vector> positions;
for (GridEntity_Door* door : room.doors)
{
if (!door) continue;
Vector position = room.GetGridPosition(door->gridIndex);
Entity_Player* player = game_nearest_player(position);
if (vector_length_squared(vector_sub(position, player->pos)) > AMBUSH_DOOR_REACH * AMBUSH_DOOR_REACH)
positions.push_back(position);
}
if (positions.empty()) positions.push_back(Room::GetCenterPos());
return positions;
}
// A reward plate's ambush: two to four of the floor's enemies come in at the doors no player is near. New enemies
// shut the doors (secret ones with their plain animation) and unclear the room, which then gives no clear reward.
static void plate_ambush_spawn(GridEntity& plate, RNG& rng)
{
Game& game = *g_game;
Room& room = *game.room;
if (plate.state != PLATE_AMBUSH) grid_sound_play(SOUND_THUMBS_DOWN);
int enemiesBefore = room.GetAliveEnemiesCount();
int count = (int)rng.RandomInt(AMBUSH_EXTRA_MAX) + AMBUSH_MIN;
std::span<const AmbushEnemy> pool = ambush_pool_get();
AmbushEnemy enemy = pool.size() > 1 ? pool[rng.RandomInt((uint32_t)pool.size())] : pool[0];
std::vector<Vector> positions = ambush_positions_get();
rng.Next();
RNG seeds = rng;
int first = (int)(rng._seed & AMBUSH_START_MASK);
for (int i = first; i < first + count; i++)
{
uint32_t seed = seeds.Next();
Vector position = room_free_pickup_position_find({.position = positions[(size_t)i % positions.size()]});
float angle = random_unit() * RANDOM_HALF_TURN;
angle += angle;
float distance = random_unit() * AMBUSH_SCATTER;
position = vector_add(position, vector_make(std::cos(angle) * distance, std::sin(angle) * distance));
entity_spawn(enemy.type, enemy.variant, position, seed);
}
if (enemiesBefore < room.GetAliveEnemiesCount())
{
for (GridEntity_Door* door : room.doors)
{
if (!door || door->state == DOOR_STATE_CLOSED) continue;
door->state = DOOR_STATE_CLOSED;
door->UpdateAnimation();
for (int type : SECRET_DOOR_ROOM_TYPES)
if (door->currentRoomType == type || door->targetRoomType == type) door->openAnimation = "Open";
}
if (room.IsClear()) game.GetRoomByIdx(game.currentRoomIdx)->flags |= ROOM_DESCRIPTOR_FLAG_NO_REWARD;
room.descriptor->flags &= ~ROOM_DESCRIPTOR_FLAG_CLEAR;
}
if (!room.IsClear()) plate.state = PLATE_AMBUSH;
}
// 00984550 What a pressed plate does: a reward plate rewards or ambushes by its seed, the rail button raises the
// mineshaft's rail, the kill button kills the room's enemies and the output buttons fire their group.
// The kill button's timer and the output groups stop, as Game::ShakeScreen and Room::TriggerOutput are not here yet.
void GridEntity_PressurePlate::Reward()
{
if (variant == PLATE_REWARD)
{
RNG rng = {spawnSeed, 5, 9, 7};
rng.Next();
if ((rng._seed & 1) == 0) plate_reward_spawn(*this, rng);
else plate_ambush_spawn(*this, rng);
}
else if (variant == PLATE_RAIL) g_game->raise_mineshaft_rail();
else if (variant == PLATE_KILL && g_game->room->GetAliveEnemiesCount() > 0)
reconstruction_missing("GridEntity_PressurePlate::Reward 00984550: the kill button (Game::ShakeScreen, "
"pressure_plate_kill_enemies_timer_callback 00983f30)");
else if (variant >= PLATE_OUTPUT_FIRST && variant <= PLATE_OUTPUT_LAST)
g_game->room->TriggerOutput(variant - PLATE_OUTPUT_FIRST);
}