Files
DarkflameServer/tests/dGameTests/FireTempleScriptsTests.cpp
Aaron Kimbrell 6136922ff4 feat(scripts): L_BOSS_MANAGER_SERVER
The Fire Temple boss fight: intro celebration and camera, the centre portal and the tornado, four elemental phases
whose portal moves between its spawner's nodes and is hit by riders of the element's rail, the middle celebration,
Garmadon stunned and a wave of spinner enemies each fifth of his health, then the end bouncer. The client's
CelebrationCompleted carries no celebration id, so the manager branches on the celebration it started.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 00:19:49 -06:00

292 lines
14 KiB
C++

#include "GameDependencies.h"
#include <gtest/gtest.h>
#include "Entity.h"
#include "GameMessages.h"
#include "02_server/Map/General/KillCounterUtil.h"
#include "02_server/Map/General/SpinnersVol.h"
#include "02_server/Map/General/DoorIndicatorServer.h"
#include "02_server/Map/General/ElementalChestsServer.h"
#include "CDClientDatabase.h"
#include "02_server/Map/General/ZoneFireTempleServer.h"
#include "EffectsMessages.h"
#include "02_server/Map/General/EventCounterUtil.h"
#include "02_server/Map/General/BossGarmadonTornado.h"
#include "02_server/Map/General/WavesManagerServer.h"
#include "02_server/Map/General/BossManagerServer.h"
#include "EntityManager.h"
#include "BitStream.h"
namespace {
// A kill counter extension that records its limit events
class TestCounter : public KillCounterUtil {
public:
std::vector<bool> events;
protected:
void ProcessEvent(Entity* self, bool upperLimitReached) override { events.push_back(upperLimitReached); }
};
}
class FireTempleScriptsTest : public GameDependenciesTest {
protected:
void SetUp() override { SetUpDependencies(); }
void TearDown() override { TearDownDependencies(); }
};
// L_KILL_COUNTER_UTIL: events named by the object's config count up to max and back down to min; past either end
// nothing more happens
TEST_F(FireTempleScriptsTest, KillCounterCountsToItsLimits) {
info.settings.Insert<int32_t>(u"max", 2);
info.settings.Insert<std::u16string>(u"up", u"increment");
info.settings.Insert<std::u16string>(u"down", u"decrement");
Entity entity(288300744895900200, info);
info.settings.values.clear();
TestCounter counter;
counter.OnStartup(&entity);
EXPECT_EQ(entity.GetVar<int32_t>(u"min"), 0);
EXPECT_EQ(entity.GetVar<int32_t>(u"max"), 2);
counter.OnFireEventServerSide(&entity, &entity, "up", 0, 0, 0);
EXPECT_TRUE(counter.events.empty());
counter.OnFireEventServerSide(&entity, &entity, "up", 0, 0, 0);
ASSERT_EQ(counter.events.size(), 1u);
EXPECT_TRUE(counter.events[0]);
EXPECT_EQ(entity.GetVar<int32_t>(u"counter"), 2);
counter.OnFireEventServerSide(&entity, &entity, "up", 0, 0, 0);
EXPECT_EQ(entity.GetVar<int32_t>(u"counter"), 2);
// An event the config does not name does nothing
counter.OnFireEventServerSide(&entity, &entity, "increment", 0, 0, 0);
EXPECT_EQ(entity.GetVar<int32_t>(u"counter"), 2);
counter.OnFireEventServerSide(&entity, &entity, "down", 0, 0, 0);
counter.OnFireEventServerSide(&entity, &entity, "down", 0, 0, 0);
ASSERT_EQ(counter.events.size(), 2u);
EXPECT_FALSE(counter.events[1]);
EXPECT_EQ(entity.GetVar<int32_t>(u"counter"), 0);
counter.OnFireEventServerSide(&entity, &entity, "down", 0, 0, 0);
EXPECT_EQ(entity.GetVar<int32_t>(u"counter"), 0);
}
// Without max or spawn networks the counter's max is 1
TEST_F(FireTempleScriptsTest, KillCounterWithoutNetworksCountsToOne) {
Entity entity(288300744895900201, info);
TestCounter counter;
counter.OnStartup(&entity);
EXPECT_EQ(entity.GetVar<int32_t>(u"max"), 1);
}
// The spinner volumes at max: a timer, then the rail's network; at min nothing
TEST_F(FireTempleScriptsTest, SpinnersVolStartsItsRailTimerAtMax) {
info.settings.Insert<int32_t>(u"max", 1);
info.settings.Insert<std::u16string>(u"up", u"increment");
Entity entity(288300744895900202, info);
info.settings.values.clear();
BSpinnersVol blades;
blades.OnStartup(&entity);
EXPECT_FALSE(entity.HasTimer("SpawnIceRail"));
blades.OnFireEventServerSide(&entity, &entity, "up", 0, 0, 0);
// (a new timer starts on the object's next update)
entity.Update(0.0f);
EXPECT_TRUE(entity.HasTimer("SpawnIceRail"));
// No spawner of that name: nothing to activate
blades.OnTimerDone(&entity, "SpawnIceRail");
}
// L_DOOR_INDICATOR_SERVER: either named event lights the flame; any other does nothing
TEST_F(FireTempleScriptsTest, DoorIndicatorLightsOnEitherEvent) {
info.settings.Insert<std::u16string>(u"name_activated_event", u"DoorOpen");
info.settings.Insert<std::u16string>(u"name_deactivated_event", u"DoorClose");
Entity entity(288300744895900203, info);
info.settings.values.clear();
DoorIndicatorServer indicator;
indicator.OnFireEventServerSide(&entity, &entity, "Other", 0, 0, 0);
EXPECT_FALSE(entity.GetNetworkVar<bool>(u"FlameOn"));
indicator.OnFireEventServerSide(&entity, &entity, "DoorClose", 0, 0, 0);
EXPECT_TRUE(entity.GetNetworkVar<bool>(u"FlameOn"));
}
// L_ELEMENTAL_CHESTS_SERVER: the flag set on use is the chest's precondition's target
TEST_F(FireTempleScriptsTest, ElementalChestFlagIsItsPreconditionsTarget) {
CDClientDatabase::Connect(":memory:");
CDClientDatabase::ExecuteDML("CREATE TABLE Preconditions (id INTEGER, type INTEGER, targetLOT TEXT, targetCount INTEGER, validContexts INTEGER);");
CDClientDatabase::ExecuteDML("INSERT INTO Preconditions VALUES (321, 23, '2063', 1, 0);");
EXPECT_EQ(ElementalChestsServer::PreconditionFlag("321"), 2063u);
EXPECT_EQ(ElementalChestsServer::PreconditionFlag("322"), std::nullopt);
EXPECT_EQ(ElementalChestsServer::PreconditionFlag(""), std::nullopt);
}
// L_ZONE_FIRE_TEMPLE_SERVER: the close button asks once to confirm; no keeps the player here, and the box can be asked
// for again
TEST_F(FireTempleScriptsTest, FireTempleExitAsksToConfirm) {
Entity zone(288300744895900204, info);
Entity player(288300744895900205, info);
ZoneFireTempleServer script;
auto* const mock = static_cast<dServerMock*>(Game::server);
mock->ClearSentPackets();
script.OnMessageBoxResponse(&zone, &player, 1, u"ActivityButton", u"");
EXPECT_EQ(mock->GetSentPackets().size(), 1u);
// Asked again before answering: nothing more
script.OnMessageBoxResponse(&zone, &player, 1, u"ActivityButton", u"");
EXPECT_EQ(mock->GetSentPackets().size(), 1u);
script.OnMessageBoxResponse(&zone, &player, 0, u"Exit", u"");
script.OnMessageBoxResponse(&zone, &player, 1, u"ActivityButton", u"");
EXPECT_EQ(mock->GetSentPackets().size(), 2u);
}
// L_EVENT_COUNTER_UTIL: max 1 by default; reaching it fires the max event at its group, once
TEST_F(FireTempleScriptsTest, EventCounterFiresAtMax) {
info.settings.Insert<std::u16string>(u"hit", u"increment");
info.settings.Insert<std::u16string>(u"maxHit_event", u"Open");
info.settings.Insert<std::u16string>(u"maxHit_group", u"TestDoors");
Entity entity(288300744895900206, info);
info.settings.values.clear();
EventCounterUtil counter;
counter.OnStartup(&entity);
EXPECT_EQ(entity.GetVar<int32_t>(u"max"), 1);
counter.OnFireEventServerSide(&entity, &entity, "hit", 0, 0, 0);
EXPECT_EQ(entity.GetVar<int32_t>(u"counter"), 1);
counter.OnFireEventServerSide(&entity, &entity, "hit", 0, 0, 0);
EXPECT_EQ(entity.GetVar<int32_t>(u"counter"), 1);
}
// L_BOSS_GARMADON_TORNADO: the bomb wait is a whole number of seconds from half the element's delay to all of it, and
// the boss manager's NewElement picks the element by its place in the order
TEST_F(FireTempleScriptsTest, TornadoBombsSoonerForLaterElements) {
for (int32_t i = 0; i < 50; i++) {
const auto earth = BossGarmadonTornado::BombDelay("earth");
EXPECT_GE(earth, 20);
EXPECT_LE(earth, 40);
const auto fire = BossGarmadonTornado::BombDelay("fire");
EXPECT_GE(fire, 14);
EXPECT_LE(fire, 28);
}
Entity entity(288300744895900207, info);
BossGarmadonTornado tornado;
tornado.OnNotifyObject(&entity, &entity, "NewElement", 3);
EXPECT_EQ(entity.GetVar<std::string>(u"CurrentElement"), "lightning");
tornado.OnNotifyObject(&entity, &entity, "NewElement", 0);
EXPECT_EQ(entity.GetVar<std::string>(u"CurrentElement"), "lightning");
}
// L_WAVES_MANAGER_SERVER: it hears its spinners being sent SpawnedEnemiesDead (a FireEvent subscription), stops
// listening to each, and when the last is done waits a moment and shows the door
TEST_F(FireTempleScriptsTest, WavesManagerHearsItsSpinnersFinish) {
// Creating an entity looks its LOT up in the CDClient
CDClientDatabase::Connect(":memory:");
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS ComponentsRegistry (id INTEGER, component_type INTEGER, component_id INTEGER);");
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS Objects (id INTEGER, name TEXT, type TEXT, interactionDistance REAL);");
auto* const manager = Game::entityManager->CreateEntity(info, nullptr, nullptr, false, 288300744895900210);
auto* const first = Game::entityManager->CreateEntity(info, nullptr, nullptr, false, 288300744895900211);
auto* const second = Game::entityManager->CreateEntity(info, nullptr, nullptr, false, 288300744895900212);
WavesManagerServer waves;
manager->SetVar<int32_t>(u"SpinnersActive", 2);
first->Subscribe(manager->GetObjectID(), &waves, "FireEvent");
second->Subscribe(manager->GetObjectID(), &waves, "FireEvent");
first->OnFireEventServerSide(first, "SomethingElse");
EXPECT_EQ(manager->GetVar<int32_t>(u"SpinnersActive"), 2);
first->OnFireEventServerSide(first, "SpawnedEnemiesDead");
EXPECT_EQ(manager->GetVar<int32_t>(u"SpinnersActive"), 1);
// No longer listening to the first
first->OnFireEventServerSide(first, "SpawnedEnemiesDead");
EXPECT_EQ(manager->GetVar<int32_t>(u"SpinnersActive"), 1);
manager->Update(0.0f);
EXPECT_FALSE(manager->HasTimer("ShowDoor"));
second->OnFireEventServerSide(second, "SpawnedEnemiesDead");
EXPECT_EQ(manager->GetVar<int32_t>(u"SpinnersActive"), 0);
manager->Update(0.0f);
EXPECT_TRUE(manager->HasTimer("ShowDoor"));
}
// The waves as the Lua has them: three, the last with eight kinds
TEST_F(FireTempleScriptsTest, WavesManagerWaves) {
ASSERT_EQ(WavesManagerServer::Waves().size(), 3u);
EXPECT_EQ(WavesManagerServer::Waves()[2].size(), 8u);
EXPECT_EQ(WavesManagerServer::Waves()[0][0].largeNum, 3);
}
namespace {
class CelebrationListener : public CppScripts::Script {
public:
std::vector<LWOOBJID> heard;
void NotifyCelebrationCompleted(Entity* self, Entity* player) override { heard.push_back(player->GetObjectID()); }
};
}
// The client's CelebrationCompleted (no payload) reaches the scripts that subscribed to the player, until they cancel
TEST_F(FireTempleScriptsTest, CelebrationCompletedReachesSubscribers) {
CDClientDatabase::Connect(":memory:");
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS ComponentsRegistry (id INTEGER, component_type INTEGER, component_id INTEGER);");
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS Objects (id INTEGER, name TEXT, type TEXT, interactionDistance REAL);");
auto* const manager = Game::entityManager->CreateEntity(info, nullptr, nullptr, false, 288300744895900220);
auto* const player = Game::entityManager->CreateEntity(info, nullptr, nullptr, false, 288300744895900221);
CelebrationListener listener;
player->Subscribe(manager->GetObjectID(), &listener, "CelebrationCompleted");
GameMessages::CelebrationCompleted message;
RakNet::BitStream bitStream;
message.Serialize(bitStream);
EXPECT_EQ(bitStream.GetNumberOfBitsUsed(), 0u);
message.Handle(*player, UNASSIGNED_SYSTEM_ADDRESS);
ASSERT_EQ(listener.heard.size(), 1u);
EXPECT_EQ(listener.heard[0], player->GetObjectID());
player->Unsubscribe(manager->GetObjectID(), "CelebrationCompleted");
message.Handle(*player, UNASSIGNED_SYSTEM_ADDRESS);
EXPECT_EQ(listener.heard.size(), 1u);
}
namespace {
class RailListener : public CppScripts::Script {
public:
std::vector<int32_t> waypoints;
void NotifyPlayerRailArrived(Entity* self, Entity* activator, const std::u16string& pathName, int32_t waypointNumber) override {
waypoints.push_back(waypointNumber);
}
};
}
// A rail activator's subscribers hear its riders reach the waypoints of its rail
TEST_F(FireTempleScriptsTest, RailArrivalReachesTheActivatorsSubscribers) {
CDClientDatabase::Connect(":memory:");
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS ComponentsRegistry (id INTEGER, component_type INTEGER, component_id INTEGER);");
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS Objects (id INTEGER, name TEXT, type TEXT, interactionDistance REAL);");
auto* const manager = Game::entityManager->CreateEntity(info, nullptr, nullptr, false, 288300744895900230);
auto* const activator = Game::entityManager->CreateEntity(info, nullptr, nullptr, false, 288300744895900231);
RailListener listener;
activator->Subscribe(manager->GetObjectID(), &listener, "PlayerRailArrivedNotification");
activator->NotifyPlayerRailArrived(u"BossRail", 5);
ASSERT_EQ(listener.waypoints.size(), 1u);
EXPECT_EQ(listener.waypoints[0], 5);
activator->Unsubscribe(manager->GetObjectID(), "PlayerRailArrivedNotification");
activator->NotifyPlayerRailArrived(u"BossRail", 6);
EXPECT_EQ(listener.waypoints.size(), 1u);
}
// L_BOSS_MANAGER_SERVER: a wave starts each time Garmadon loses another fifth of his health
TEST_F(FireTempleScriptsTest, BossWavesAtEachFifthOfGarmadonsHealth) {
EXPECT_FALSE(BossManagerServer::ReachedWave(81.0f, 100.0f, 1));
EXPECT_TRUE(BossManagerServer::ReachedWave(80.0f, 100.0f, 1));
EXPECT_FALSE(BossManagerServer::ReachedWave(61.0f, 100.0f, 2));
EXPECT_TRUE(BossManagerServer::ReachedWave(20.0f, 100.0f, 4));
}
// The intro celebration's end (the last player's) starts the start camera timers; the manager knows it was the intro
TEST_F(FireTempleScriptsTest, BossManagerBranchesOnTheCelebrationItStarted) {
CDClientDatabase::Connect(":memory:");
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS ComponentsRegistry (id INTEGER, component_type INTEGER, component_id INTEGER);");
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS Objects (id INTEGER, name TEXT, type TEXT, interactionDistance REAL);");
auto* const manager = Game::entityManager->CreateEntity(info, nullptr, nullptr, false, 288300744895900240);
auto* const player = Game::entityManager->CreateEntity(info, nullptr, nullptr, false, 288300744895900241);
BossManagerServer boss;
manager->SetVar<int32_t>(u"CurrentCelebration", 26);
manager->SetVar<int32_t>(u"PlayersInCutscene", 1);
player->Subscribe(manager->GetObjectID(), &boss, "CelebrationCompleted");
player->NotifyCelebrationCompleted();
manager->Update(0.0f);
EXPECT_TRUE(manager->HasTimer("KillStartingPortal"));
EXPECT_TRUE(manager->HasTimer("UnstunTornado"));
// No longer subscribed
player->NotifyCelebrationCompleted();
EXPECT_EQ(manager->GetVar<int32_t>(u"PlayersInCutscene"), 0);
}