diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index b12a46a77..f710146de 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -589,6 +589,8 @@ void Entity::Subscribe(LWOOBJID scriptObjId, CppScripts::Script* scriptToAdd, co } else if (notificationName == "PlayerResurrectionFinished") { LOG("Subscribing to PlayerResurrectionFinished"); m_Subscriptions[scriptObjId][notificationName] = scriptToAdd; + } else if (notificationName == "FireEvent") { + m_Subscriptions[scriptObjId][notificationName] = scriptToAdd; } } @@ -600,6 +602,8 @@ void Entity::Unsubscribe(LWOOBJID scriptObjId, const std::string& notificationNa } else if (notificationName == "PlayerResurrectionFinished") { LOG("Unsubscribing from PlayerResurrectionFinished"); m_Subscriptions[scriptObjId].erase(notificationName); + } else if (notificationName == "FireEvent") { + m_Subscriptions[scriptObjId].erase(notificationName); } } @@ -999,6 +1003,14 @@ void Entity::OnCollisionLeavePhantom(const LWOOBJID otherEntity) { void Entity::OnFireEventServerSide(Entity* sender, std::string args, int32_t param1, int32_t param2, int32_t param3) { GetScript()->OnFireEventServerSide(this, sender, args, param1, param2, param3); + // The scripts that asked to hear of this object's FireEvents (a Lua notification request); a copy, as one may cancel + const auto subscriptions = m_Subscriptions; + for (const auto& [subscriberID, scripts] : subscriptions) { + const auto it = scripts.find("FireEvent"); + if (it == scripts.end()) continue; + auto* const subscriber = Game::entityManager->GetEntity(subscriberID); + if (subscriber) it->second->NotifyFireEvent(subscriber, this, args); + } } void Entity::OnActivityStateChangeRequest(LWOOBJID senderID, int32_t value1, int32_t value2, const std::u16string& stringValue) { diff --git a/dScripts/02_server/Map/General/CMakeLists.txt b/dScripts/02_server/Map/General/CMakeLists.txt index cd14dbcfe..293ce65d6 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 "EnemyClearThreat.cpp" "EventCounterUtil.cpp" "ExplodingAsset.cpp" + "FireTempleCinematics.cpp" "FrictionVolumeServer.cpp" "ForceVolumeServer.cpp" "GrowingFlower.cpp" @@ -28,6 +29,7 @@ set(DSCRIPTS_SOURCES_02_SERVER_MAP_GENERAL "TouchMissionUpdateServer.cpp" "VisToggleNotifierServer.cpp" "NTNaomiDirtServer.cpp" + "WavesManagerServer.cpp" "WishingWellServer.cpp" "ZoneFireTempleServer.cpp") diff --git a/dScripts/02_server/Map/General/FireTempleCinematics.cpp b/dScripts/02_server/Map/General/FireTempleCinematics.cpp new file mode 100644 index 000000000..7a5707113 --- /dev/null +++ b/dScripts/02_server/Map/General/FireTempleCinematics.cpp @@ -0,0 +1,35 @@ +#include "FireTempleCinematics.h" +#include "Entity.h" +#include "EntityManager.h" +#include "EffectsMessages.h" +#include "PhantomPhysicsComponent.h" +#include "dpEntity.h" +#include "dZoneManager.h" +#include "Zone.h" + +std::optional FireTempleCinematics::GetCinematicInfo(const std::string& pathName) { + const auto* const zone = Game::zoneManager->GetZone(); + const auto* const path = zone ? zone->GetPath(pathName) : nullptr; + if (!path) return std::nullopt; + float time = 0.0f; + for (size_t i = 0; i + 1 < path->pathWaypoints.size(); i++) time += path->pathWaypoints[i].camera.time; + return time; +} + +std::vector FireTempleCinematics::ObjectsInVolume(Entity* volume) { + std::vector objects; + auto* const phantom = volume ? volume->GetComponent() : nullptr; + if (!phantom || !phantom->GetdpEntity()) return objects; + for (const auto id : phantom->GetdpEntity()->GetCurrentlyCollidingObjects()) { + if (auto* const object = Game::entityManager->GetEntity(id)) objects.push_back(object); + } + return objects; +} + +void FireTempleCinematics::PlayCinematic(Entity* player, const std::u16string& pathName) { + if (!player || !player->IsPlayer()) return; + GameMessages::PlayCinematic cinematic; + cinematic.target = player->GetObjectID(); + cinematic.pathName = pathName; + cinematic.Send(player->GetSystemAddress()); +} diff --git a/dScripts/02_server/Map/General/FireTempleCinematics.h b/dScripts/02_server/Map/General/FireTempleCinematics.h new file mode 100644 index 000000000..1e186c510 --- /dev/null +++ b/dScripts/02_server/Map/General/FireTempleCinematics.h @@ -0,0 +1,16 @@ +#pragma once +#include +#include + +class Entity; + +// Shared by the Fire Temple room managers (L_WAVES_MANAGER_SERVER, L_BOSS_MANAGER_SERVER) +namespace FireTempleCinematics { + // LEVEL:GetCinematicInfo(path): the camera path's time (its waypoints' times but the last one's), nothing without + // the path + std::optional GetCinematicInfo(const std::string& pathName); + // Every object in the phantom volumes of the group, the Lua's GetObjectsInPhysicsBounds + std::vector ObjectsInVolume(Entity* volume); + // PlayCinematic{ pathName, rerouteID = player } to a player + void PlayCinematic(Entity* player, const std::u16string& pathName); +} diff --git a/dScripts/02_server/Map/General/WavesManagerServer.cpp b/dScripts/02_server/Map/General/WavesManagerServer.cpp new file mode 100644 index 000000000..d90adb462 --- /dev/null +++ b/dScripts/02_server/Map/General/WavesManagerServer.cpp @@ -0,0 +1,122 @@ +#include "WavesManagerServer.h" +#include "Entity.h" +#include "EntityManager.h" +#include "BuffComponent.h" +#include "dZoneManager.h" +#include "Spawner.h" +#include "FireTempleCinematics.h" + +namespace { + constexpr float CamDelay = 1.0f; + const std::string EnemySpawnerPre = "Waves_"; + const std::string EnemySpawnerPost = "_Spawner"; + const std::u16string DoorCam = u"WavesDoorCam"; + const std::u16string SpawningCam = u"WavesSpawnerSpinners"; + const std::string RoomVolume = "WavesRoomVolume"; + const std::string DoorIndicators = "WavesDoorIndicator"; + const std::string Door = "WavesDoor"; + const std::string ElevatorSpinner = "WavesElevatorSpinner"; + const std::string SpawnerSpinners = "WavesSpinner"; + + // The first object of the group, sent the event + void FireFirst(Entity* self, const std::string& group, const std::string& event) { + const auto objects = Game::entityManager->GetEntitiesInGroup(group); + for (auto* const object : objects) { + if (!object) continue; + object->OnFireEventServerSide(self, event); + break; + } + } +} + +const std::vector>& WavesManagerServer::Waves() { + static const std::vector> waves = { + { { "Blacksmith", 3, 2 }, { "Hand", 2, 1 }, { "Marksman", 2, 1 } }, + { { "Hand", 2, 2 }, { "Scientist", 4, 2 }, { "Wolf", 2, 1 }, { "Beetle", 2, 1 } }, + { { "Blacksmith", 2, 1 }, { "Scientist", 2, 1 }, { "Marksman", 2, 1 }, { "Overseer", 2, 1 }, { "Wolf", 2, 1 }, + { "Beetle", 1, 1 }, { "Hand", 3, 1 }, { "Bonezai", 1, 1 } }, + }; + return waves; +} + +void WavesManagerServer::OnCollisionPhantom(Entity* self, Entity* target) { + if (self->GetVar(u"bStarted")) return; + self->SetVar(u"bStarted", true); + auto* const zoneControl = Game::entityManager->GetZoneControlEntity(); + if (zoneControl && zoneControl->GetVar(u"initialPlayerCount") > 2) self->SetVar(u"LargeTeam", true); + ActivateWaveSpinners(self, 1); +} + +void WavesManagerServer::NotifyFireEvent(Entity* self, Entity* other, const std::string& args) { + if (args != "SpawnedEnemiesDead") return; + other->Unsubscribe(self->GetObjectID(), "FireEvent"); + if (!self->HasVar(u"SpinnersActive")) return; + auto activeSpinners = self->GetVar(u"SpinnersActive") - 1; + if (activeSpinners < 0) { + LOG("Warning!! too many spinners deactivated"); + return; + } + self->SetVar(u"SpinnersActive", activeSpinners); + if (activeSpinners > 0) return; + self->CancelTimer("ShowDoor"); + self->AddTimer("ShowDoor", CamDelay); +} + +void WavesManagerServer::ActivateWaveSpinners(Entity* self, const int32_t waveNum) { + if (waveNum > static_cast(Waves().size())) return; + PlayCamera(self, SpawningCam); + self->SetVar(u"CurrentWave", waveNum); + for (const auto& enemy : Waves()[waveNum - 1]) { + const auto spawners = Game::zoneManager->GetSpawnersByName(EnemySpawnerPre + enemy.name + EnemySpawnerPost); + if (spawners.empty() || !spawners.front()) continue; + auto* const spawner = spawners.front(); + const auto spawnNum = self->GetVar(u"LargeTeam") ? enemy.largeNum : enemy.smallNum; + if (spawnNum <= 0) continue; + spawner->m_Info.maxToSpawn = spawnNum; + spawner->SetNumToMaintain(spawnNum); + auto spinnersUp = self->GetVar(u"SpinnersActive"); + for (auto* const spinner : Game::entityManager->GetEntitiesInGroup(SpawnerSpinners + enemy.name)) { + if (!spinner) continue; + spinner->OnFireEventServerSide(self, "SpawnEnemies"); + spinner->Subscribe(self->GetObjectID(), this, "FireEvent"); + spinnersUp++; + } + self->SetVar(u"SpinnersActive", spinnersUp); + } +} + +void WavesManagerServer::PlayCamera(Entity* self, const std::u16string& cine) { + const auto roomTrigger = Game::entityManager->GetEntitiesInGroup(RoomVolume); + for (auto* const trigger : roomTrigger) { + if (!trigger) continue; + const auto players = FireTempleCinematics::ObjectsInVolume(trigger); + if (players.empty()) return; + for (auto* const player : players) { + if (auto* const buff = player->GetComponent()) buff->RemoveBuff(60); + FireTempleCinematics::PlayCinematic(player, cine); + } + } +} + +void WavesManagerServer::OnTimerDone(Entity* self, std::string timerName) { + if (timerName == "ShowDoor") { + PlayCamera(self, DoorCam); + self->CancelTimer("TurnOnIndicator"); + self->AddTimer("TurnOnIndicator", 1.0f); + const auto cineTime = FireTempleCinematics::GetCinematicInfo(GeneralUtils::UTF16ToWTF8(DoorCam)).value_or(3.0f); + self->CancelTimer("SpawnNextWave"); + self->AddTimer("SpawnNextWave", cineTime); + } else if (timerName == "TurnOnIndicator") { + const auto currentWave = self->GetVar(u"CurrentWave"); + const auto indicators = Game::entityManager->GetEntitiesInGroup(DoorIndicators + std::to_string(currentWave)); + if (indicators.empty()) return; + FireFirst(self, DoorIndicators + std::to_string(currentWave), "IndicatorOn"); + if (currentWave == static_cast(Waves().size())) { + if (Game::entityManager->GetEntitiesInGroup(Door).empty()) return; + FireFirst(self, Door, "IndicatorOn"); + FireFirst(self, ElevatorSpinner, "TurnOn"); + } + } else if (timerName == "SpawnNextWave") { + ActivateWaveSpinners(self, self->GetVar(u"CurrentWave") + 1); + } +} diff --git a/dScripts/02_server/Map/General/WavesManagerServer.h b/dScripts/02_server/Map/General/WavesManagerServer.h new file mode 100644 index 000000000..86d0e28ba --- /dev/null +++ b/dScripts/02_server/Map/General/WavesManagerServer.h @@ -0,0 +1,25 @@ +#pragma once +#include "CppScripts.h" + +// L_WAVES_MANAGER_SERVER.lua: the Fire Temple waves room. The first touch starts wave 1: each enemy kind's spawner +// network (Waves__Spawner) gets its count for the team size, the spinners of that kind (group WavesSpinner) +// are sent SpawnEnemies, and the manager listens for each to be sent SpawnedEnemiesDead (the spinners send it to their +// own group once their enemies are dead). When the last is, the players in the room see the door camera, the wave's +// door indicator lights, and the next wave starts after the camera; after the last wave the door and the elevator +// spinner are turned on. +class WavesManagerServer : public CppScripts::Script { +public: + struct Enemy { + std::string name; + int32_t largeNum; + int32_t smallNum; + }; + static const std::vector>& Waves(); + + void OnCollisionPhantom(Entity* self, Entity* target) override; + void NotifyFireEvent(Entity* self, Entity* other, const std::string& args) override; + void OnTimerDone(Entity* self, std::string timerName) override; +private: + void ActivateWaveSpinners(Entity* self, int32_t waveNum); + static void PlayCamera(Entity* self, const std::u16string& cine); +}; diff --git a/dScripts/CppScripts.cpp b/dScripts/CppScripts.cpp index 565b9a5ed..a5b8f0eaf 100644 --- a/dScripts/CppScripts.cpp +++ b/dScripts/CppScripts.cpp @@ -283,6 +283,7 @@ #include "ZoneFireTempleServer.h" #include "EventCounterUtil.h" #include "BossGarmadonTornado.h" +#include "WavesManagerServer.h" #include "SimpleOnceOnlyMoverSpinnerEvent.h" #include "ImaginationShrineServer.h" #include "Lieutenant.h" @@ -703,6 +704,7 @@ namespace { {"scripts\\02_server\\Map\\firetemple\\L_DOOR_INDICATOR_SERVER.lua", []() {return new DoorIndicatorServer();}}, {"scripts\\02_server\\Map\\firetemple\\L_ELEMENTAL_CHESTS_SERVER.lua", []() {return new ElementalChestsServer();}}, {"scripts\\02_server\\Map\\firetemple\\L_ZONE_FIRE_TEMPLE_SERVER.lua", []() {return new ZoneFireTempleServer();}}, + {"scripts\\02_server\\Map\\firetemple\\L_WAVES_MANAGER_SERVER.lua", []() {return new WavesManagerServer();}}, // (the CDServer row writes this path with forward slashes) {"scripts/02_server/Map/firetemple/L_BOSS_GARMADON_TORNADO.lua", []() {return new BossGarmadonTornado();}}, {"scripts\\02_server\\Map\\General\\L_KILL_COUNTER_UTIL.lua", []() {return new KillCounterUtil();}}, diff --git a/dScripts/CppScripts.h b/dScripts/CppScripts.h index 1421d2d0d..5cdbee2eb 100644 --- a/dScripts/CppScripts.h +++ b/dScripts/CppScripts.h @@ -192,6 +192,13 @@ namespace CppScripts { virtual void NotifyPlayerResurrectionFinished(Entity& self, GameMessages::PlayerResurrectionFinished& msg) {}; + /** + * Invoked when an object self subscribed to ("FireEvent") was sent a FireEvent. + * + * Equivalent to 'function notifyFireEvent(self, other, msg)' + */ + virtual void NotifyFireEvent(Entity* self, Entity* other, const std::string& args) {}; + /** * Invoked when a player has responsed to a mission. * diff --git a/tests/dGameTests/FireTempleScriptsTests.cpp b/tests/dGameTests/FireTempleScriptsTests.cpp index 1b4f392ee..28d5bb9b8 100644 --- a/tests/dGameTests/FireTempleScriptsTests.cpp +++ b/tests/dGameTests/FireTempleScriptsTests.cpp @@ -12,6 +12,8 @@ #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 "EntityManager.h" namespace { // A kill counter extension that records its limit events @@ -163,3 +165,41 @@ TEST_F(FireTempleScriptsTest, TornadoBombsSoonerForLaterElements) { tornado.OnNotifyObject(&entity, &entity, "NewElement", 0); EXPECT_EQ(entity.GetVar(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(u"SpinnersActive", 2); + first->Subscribe(manager->GetObjectID(), &waves, "FireEvent"); + second->Subscribe(manager->GetObjectID(), &waves, "FireEvent"); + + first->OnFireEventServerSide(first, "SomethingElse"); + EXPECT_EQ(manager->GetVar(u"SpinnersActive"), 2); + first->OnFireEventServerSide(first, "SpawnedEnemiesDead"); + EXPECT_EQ(manager->GetVar(u"SpinnersActive"), 1); + // No longer listening to the first + first->OnFireEventServerSide(first, "SpawnedEnemiesDead"); + EXPECT_EQ(manager->GetVar(u"SpinnersActive"), 1); + manager->Update(0.0f); + EXPECT_FALSE(manager->HasTimer("ShowDoor")); + + second->OnFireEventServerSide(second, "SpawnedEnemiesDead"); + EXPECT_EQ(manager->GetVar(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); +}