diff --git a/dScripts/02_server/Map/General/CMakeLists.txt b/dScripts/02_server/Map/General/CMakeLists.txt index cb07588fe..b17a27cbc 100644 --- a/dScripts/02_server/Map/General/CMakeLists.txt +++ b/dScripts/02_server/Map/General/CMakeLists.txt @@ -8,6 +8,7 @@ set(DSCRIPTS_SOURCES_02_SERVER_MAP_GENERAL "ForceVolumeServer.cpp" "GrowingFlower.cpp" "ImaginationBackpackHealServer.cpp" + "KillCounterUtil.cpp" "MailBoxServer.cpp" "NjRailSwitch.cpp" "PetDigServer.cpp" @@ -17,6 +18,7 @@ set(DSCRIPTS_SOURCES_02_SERVER_MAP_GENERAL "QbSpawner.cpp" "SimpleMoverSwitch.cpp" "SimpleOnceOnlyMoverSpinnerEvent.cpp" + "SpinnersVol.cpp" "StoryBoxInteractServer.cpp" "TokenConsoleServer.cpp" "TouchMissionUpdateServer.cpp" diff --git a/dScripts/02_server/Map/General/KillCounterUtil.cpp b/dScripts/02_server/Map/General/KillCounterUtil.cpp new file mode 100644 index 000000000..8ea7628ad --- /dev/null +++ b/dScripts/02_server/Map/General/KillCounterUtil.cpp @@ -0,0 +1,128 @@ +#include "KillCounterUtil.h" +#include "Entity.h" +#include "EntityManager.h" +#include "dZoneManager.h" +#include "Spawner.h" +#include "Logger.h" + +namespace { + // Where the counter starts from unless the object sets min + constexpr int32_t DefaultCounterMin = 0; + + bool IsFalse(Entity* self, const std::u16string& name) { + return self->HasVar(name) && !self->GetVar(name); + } +} + +// The spawner of each spawnNw1, spawnNw2, ... the object names, up to the first it does not +std::vector KillCounterUtil::SpawnNetworks(Entity* self) { + std::vector spawners; + for (int32_t count = 1; self->HasVar(GeneralUtils::ASCIIToUTF16("spawnNw" + std::to_string(count))); count++) { + const auto name = self->GetVarAsString(GeneralUtils::ASCIIToUTF16("spawnNw" + std::to_string(count))); + const auto found = Game::zoneManager->GetSpawnersByName(name); + spawners.push_back(found.empty() ? nullptr : found.front()); + } + return spawners; +} + +void KillCounterUtil::OnStartup(Entity* self) { + const auto cntMin = self->HasVar(u"min") ? self->GetVarAs(u"min") : DefaultCounterMin; + self->SetVar(u"min", cntMin); + self->SetVar(u"counter", cntMin); + + const auto spawnNetworks = SpawnNetworks(self); + if (!self->HasVar(u"max")) { + int32_t cntMax = 1; + if (!spawnNetworks.empty()) { + int32_t cntTally = 0; + for (auto* const spawner : spawnNetworks) { + if (spawner && spawner->m_Info.maxToSpawn != -1) cntTally += spawner->m_Info.maxToSpawn; + } + if (cntTally == 0) LOG_DEBUG("Kill counter %llu has only infinite spawn networks", self->GetObjectID()); + else cntMax = cntTally; + } else { + LOG_DEBUG("Kill counter %llu has no max and no spawn networks", self->GetObjectID()); + } + self->SetVar(u"max", cntMax); + } else { + self->SetVar(u"max", self->GetVarAs(u"max")); + } + + if (IsFalse(self, u"bManageNotifies")) return; + if (!spawnNetworks.empty()) SpawnerNotifies(self, true); + else LOG_DEBUG("Kill counter %llu manages notifies without spawn networks", self->GetObjectID()); +} + +// Listening to the deaths of what the networks spawn (once per network; stopping only mutes it) +void KillCounterUtil::SpawnerNotifies(Entity* self, const bool subscribe) { + self->SetVar(u"killCounterListening", subscribe); + if (!subscribe || self->GetVar(u"killCounterSubscribed")) return; + self->SetVar(u"killCounterSubscribed", true); + const auto selfID = self->GetObjectID(); + for (auto* const spawner : SpawnNetworks(self)) { + if (!spawner) continue; + spawner->AddSpawnedEntityDieCallback([this, selfID]() { + auto* const counter = Game::entityManager->GetEntity(selfID); + if (counter && counter->GetVar(u"killCounterListening")) ModifyCounter(counter, "increment"); + }); + } +} + +void KillCounterUtil::OnFireEventServerSide(Entity* self, Entity* sender, std::string args, int32_t param1, int32_t param2, int32_t param3) { + const auto name = GeneralUtils::ASCIIToUTF16(args); + if (!self->HasVar(name)) return; + const auto eventType = self->GetVarAsString(name); + if (eventType == "increment" || eventType == "decrement") ModifyCounter(self, eventType); + else if (eventType == "stopNotifies") SpawnerNotifies(self, false); + else if (eventType == "startNotifies") SpawnerNotifies(self, true); + else if (eventType == "reset") ResetCounter(self); +} + +void KillCounterUtil::ResetCounter(Entity* self) { + self->SetVar(u"counter", self->GetVar(u"min")); + if (IsFalse(self, u"bManageNotifies")) { + SpawnerNotifies(self, false); + return; + } + if (!SpawnNetworks(self).empty()) SpawnerNotifies(self, true); + else LOG_DEBUG("Kill counter %llu manages notifies without spawn networks", self->GetObjectID()); +} + +void KillCounterUtil::ModifyCounter(Entity* self, const std::string& modType) { + auto counter = self->GetVar(u"counter"); + const auto cntMax = self->GetVar(u"max"); + if (modType == "increment") { + counter++; + if (counter == cntMax) { + SendEvent(self, true); + ProcessEvent(self, true); + if (IsFalse(self, u"bAutoShutoff")) return; + SpawnerNotifies(self, false); + } else if (counter > cntMax) { + return; + } + self->SetVar(u"counter", counter); + } else if (modType == "decrement") { + counter--; + const auto cntMin = self->GetVar(u"min"); + if (counter == cntMin) { + SendEvent(self, false); + ProcessEvent(self, false); + } else if (counter < cntMin) { + return; + } + self->SetVar(u"counter", counter); + } +} + +// The limit's event (maxHit_event / minHit_event) to every object in its group (maxHit_group / minHit_group) +void KillCounterUtil::SendEvent(Entity* self, const bool upperLimitReached) { + const std::u16string eventVar = upperLimitReached ? u"maxHit_event" : u"minHit_event"; + const std::u16string groupVar = upperLimitReached ? u"maxHit_group" : u"minHit_group"; + if (!self->HasVar(eventVar) || !self->HasVar(groupVar)) return; + const auto eventName = self->GetVarAsString(eventVar); + const auto eventGroup = self->GetVarAsString(groupVar); + for (auto* const obj : Game::entityManager->GetEntitiesInGroup(eventGroup)) { + if (obj) obj->OnFireEventServerSide(self, eventName); + } +} diff --git a/dScripts/02_server/Map/General/KillCounterUtil.h b/dScripts/02_server/Map/General/KillCounterUtil.h new file mode 100644 index 000000000..66ee16c22 --- /dev/null +++ b/dScripts/02_server/Map/General/KillCounterUtil.h @@ -0,0 +1,24 @@ +#pragma once +#include "CppScripts.h" + +class Spawner; + +// L_KILL_COUNTER_UTIL.lua: counts the deaths of what its spawner networks (spawnNw1, spawnNw2, ...) spawn, from min +// (0) to max (the networks' max_to_spawn when not set). At max it fires maxHit_event at maxHit_group and stops +// listening (unless bAutoShutoff is false); back at min it fires minHit_event at minHit_group. Events it is sent by +// name (a config value: increment, decrement, stopNotifies, startNotifies, reset) work the counter. Scripts that extend +// it act on the limits in ProcessEvent. +class KillCounterUtil : public CppScripts::Script { +public: + void OnStartup(Entity* self) override; + void OnFireEventServerSide(Entity* self, Entity* sender, std::string args, int32_t param1, int32_t param2, int32_t param3) override; +protected: + // Reached max (true) or min (false) + virtual void ProcessEvent(Entity* self, bool upperLimitReached) {} + void ModifyCounter(Entity* self, const std::string& modType); +private: + void SpawnerNotifies(Entity* self, bool subscribe); + void ResetCounter(Entity* self); + void SendEvent(Entity* self, bool upperLimitReached); + static std::vector SpawnNetworks(Entity* self); +}; diff --git a/dScripts/02_server/Map/General/SpinnersVol.cpp b/dScripts/02_server/Map/General/SpinnersVol.cpp new file mode 100644 index 000000000..6da4b1e84 --- /dev/null +++ b/dScripts/02_server/Map/General/SpinnersVol.cpp @@ -0,0 +1,38 @@ +#include "SpinnersVol.h" +#include "Entity.h" +#include "EntityManager.h" +#include "dZoneManager.h" +#include "Spawner.h" +#include "EffectsMessages.h" +#include "PhantomPhysicsComponent.h" +#include "dpEntity.h" + +namespace { + // The wait before the rail's network is activated + constexpr float SpawnDelay = 2.0f; +} + +void SpinnersVol::ProcessEvent(Entity* self, const bool upperLimitReached) { + if (!upperLimitReached) return; + // Each player in the volume sees the cinematic + auto* const phantomPhysics = self->GetComponent(); + if (phantomPhysics && phantomPhysics->GetdpEntity()) { + for (const auto id : phantomPhysics->GetdpEntity()->GetCurrentlyCollidingObjects()) { + auto* const player = Game::entityManager->GetEntity(id); + if (!player || !player->IsPlayer()) continue; + GameMessages::PlayCinematic cinematic; + cinematic.target = player->GetObjectID(); + cinematic.pathName = m_TargetCine; + cinematic.Send(player->GetSystemAddress()); + } + } + // (a timer with cancel: a second one replaces the first) + self->CancelTimer(m_TimerName); + self->AddTimer(m_TimerName, SpawnDelay); +} + +void SpinnersVol::OnTimerDone(Entity* self, std::string timerName) { + if (timerName != m_TimerName) return; + const auto spawners = Game::zoneManager->GetSpawnersByName(m_TargetNetwork); + if (!spawners.empty() && spawners.front()) spawners.front()->Activate(); +} diff --git a/dScripts/02_server/Map/General/SpinnersVol.h b/dScripts/02_server/Map/General/SpinnersVol.h new file mode 100644 index 000000000..155f49ca0 --- /dev/null +++ b/dScripts/02_server/Map/General/SpinnersVol.h @@ -0,0 +1,30 @@ +#pragma once +#include "KillCounterUtil.h" + +// L_BSPINNERS_VOL.lua and L_DSPINNERS_VOL.lua: the Fire Temple's Blade Spinners and Dart Spinners room volumes, kill +// counters that, once all the room's enemies are dead, play a cinematic for each player in the volume and 2 s later +// activate the spawner network of the rail quick build out of the room +class SpinnersVol : public KillCounterUtil { +public: + SpinnersVol(const std::string& targetNetwork, const std::u16string& targetCine, const std::string& timerName) + : m_TargetNetwork(targetNetwork), m_TargetCine(targetCine), m_TimerName(timerName) {} + void OnTimerDone(Entity* self, std::string timerName) override; +protected: + void ProcessEvent(Entity* self, bool upperLimitReached) override; +private: + std::string m_TargetNetwork; + std::u16string m_TargetCine; + std::string m_TimerName; +}; + +// L_BSPINNERS_VOL.lua +class BSpinnersVol : public SpinnersVol { +public: + BSpinnersVol() : SpinnersVol("BladeSpinners_IRail", u"PopUpBlades_IRail_QBSpawn", "SpawnIceRail") {} +}; + +// L_DSPINNERS_VOL.lua +class DSpinnersVol : public SpinnersVol { +public: + DSpinnersVol() : SpinnersVol("DartSpinEarthRail", u"3Tier_EarthRailSpawn", "SpawnEarthRail") {} +}; diff --git a/dScripts/CppScripts.cpp b/dScripts/CppScripts.cpp index 6c1818eea..a743f1dce 100644 --- a/dScripts/CppScripts.cpp +++ b/dScripts/CppScripts.cpp @@ -276,6 +276,8 @@ #include "FireTransRocks.h" #include "FireTransRocksManager.h" #include "SimpleMoverSwitch.h" +#include "KillCounterUtil.h" +#include "SpinnersVol.h" #include "SimpleOnceOnlyMoverSpinnerEvent.h" #include "ImaginationShrineServer.h" #include "Lieutenant.h" @@ -691,6 +693,9 @@ namespace { {"scripts\\02_server\\Map\\njhub\\L_FIRE_TRANS_ROCKS_MANAGER.lua", []() {return new FireTransRocksManager();}}, {"scripts\\02_server\\Map\\General\\L_SIMPLE_MOVER_SWITCH.lua", []() {return new SimpleMoverSwitch();}}, {"scripts\\02_server\\Map\\firetemple\\L_SIMPLE_ONCE_ONLY_MOVER_SPINNER_EVENT.lua", []() {return new SimpleOnceOnlyMoverSpinnerEvent();}}, + {"scripts\\02_server\\Map\\firetemple\\L_BSPINNERS_VOL.lua", []() {return new BSpinnersVol();}}, + {"scripts\\02_server\\Map\\firetemple\\L_DSPINNERS_VOL.lua", []() {return new DSpinnersVol();}}, + {"scripts\\02_server\\Map\\General\\L_KILL_COUNTER_UTIL.lua", []() {return new KillCounterUtil();}}, {"scripts\\02_server\\Enemy\\General\\L_ENEMY_NJ_BUFF_STUN_IMMUNITY.lua", []() {return new EnemyNjBuff();}}, {"scripts\\02_server\\Map\\njhub\\L_IMAGINATION_SHRINE_SERVER.lua", []() {return new ImaginationShrineServer();}}, {"scripts\\02_server\\Map\\njhub\\L_LIEUTENANT.lua", []() {return new Lieutenant();}}, diff --git a/tests/dGameTests/CMakeLists.txt b/tests/dGameTests/CMakeLists.txt index 94f6ffd1e..10df2b78b 100644 --- a/tests/dGameTests/CMakeLists.txt +++ b/tests/dGameTests/CMakeLists.txt @@ -14,6 +14,7 @@ set(DGAMETEST_SOURCES "KnockbackTests.cpp" "ShootingGalleryTests.cpp" "MedusaScriptsTests.cpp" + "FireTempleScriptsTests.cpp" "LuaAITests.cpp" "CollisionFilterTests.cpp" "MovementBlockerTests.cpp" diff --git a/tests/dGameTests/FireTempleScriptsTests.cpp b/tests/dGameTests/FireTempleScriptsTests.cpp new file mode 100644 index 000000000..dca9c0d51 --- /dev/null +++ b/tests/dGameTests/FireTempleScriptsTests.cpp @@ -0,0 +1,83 @@ +#include "GameDependencies.h" +#include + +#include "Entity.h" +#include "GameMessages.h" +#include "02_server/Map/General/KillCounterUtil.h" +#include "02_server/Map/General/SpinnersVol.h" + +namespace { + // A kill counter extension that records its limit events + class TestCounter : public KillCounterUtil { + public: + std::vector 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(u"max", 2); + info.settings.Insert(u"up", u"increment"); + info.settings.Insert(u"down", u"decrement"); + Entity entity(288300744895900200, info); + info.settings.values.clear(); + TestCounter counter; + counter.OnStartup(&entity); + EXPECT_EQ(entity.GetVar(u"min"), 0); + EXPECT_EQ(entity.GetVar(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(u"counter"), 2); + counter.OnFireEventServerSide(&entity, &entity, "up", 0, 0, 0); + EXPECT_EQ(entity.GetVar(u"counter"), 2); + // An event the config does not name does nothing + counter.OnFireEventServerSide(&entity, &entity, "increment", 0, 0, 0); + EXPECT_EQ(entity.GetVar(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(u"counter"), 0); + counter.OnFireEventServerSide(&entity, &entity, "down", 0, 0, 0); + EXPECT_EQ(entity.GetVar(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(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(u"max", 1); + info.settings.Insert(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"); +} +