From ff782b9641bac911ab3dab1dfc3b4ca25e262c8f Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Sun, 29 Nov 2026 00:16:41 -0600 Subject: [PATCH] feat(scripts): scripts can listen for the riders of a rail activator A rail activator whose rail_path is the arrival's path tells the scripts subscribed to it ("PlayerRailArrivedNotification", the Lua's notification request) that a rider reached a waypoint. Co-Authored-By: Claude Opus 5.5 --- dGame/Entity.cpp | 14 +++++++++-- dGame/Entity.h | 2 ++ dGame/dGameMessages/MovementMessages.cpp | 5 +++- dScripts/CppScripts.h | 8 ++++++ tests/dGameTests/FireTempleScriptsTests.cpp | 27 +++++++++++++++++++++ 5 files changed, 53 insertions(+), 3 deletions(-) diff --git a/dGame/Entity.cpp b/dGame/Entity.cpp index b75962168..e327ad58d 100644 --- a/dGame/Entity.cpp +++ b/dGame/Entity.cpp @@ -589,7 +589,7 @@ 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" || notificationName == "CelebrationCompleted") { + } else if (notificationName == "FireEvent" || notificationName == "CelebrationCompleted" || notificationName == "PlayerRailArrivedNotification") { m_Subscriptions[scriptObjId][notificationName] = scriptToAdd; } } @@ -602,7 +602,7 @@ 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" || notificationName == "CelebrationCompleted") { + } else if (notificationName == "FireEvent" || notificationName == "CelebrationCompleted" || notificationName == "PlayerRailArrivedNotification") { m_Subscriptions[scriptObjId].erase(notificationName); } } @@ -1939,3 +1939,13 @@ void Entity::NotifyCelebrationCompleted() { if (subscriber) it->second->NotifyCelebrationCompleted(subscriber, this); } } + +void Entity::NotifyPlayerRailArrived(const std::u16string& pathName, const int32_t waypointNumber) { + const auto subscriptions = m_Subscriptions; + for (const auto& [subscriberID, scripts] : subscriptions) { + const auto it = scripts.find("PlayerRailArrivedNotification"); + if (it == scripts.end()) continue; + auto* const subscriber = Game::entityManager->GetEntity(subscriberID); + if (subscriber) it->second->NotifyPlayerRailArrived(subscriber, this, pathName, waypointNumber); + } +} diff --git a/dGame/Entity.h b/dGame/Entity.h index 0f8c92747..bcab1bf41 100644 --- a/dGame/Entity.h +++ b/dGame/Entity.h @@ -202,6 +202,8 @@ public: void Unsubscribe(LWOOBJID scriptObjId, const std::string& notificationName); // Tells the scripts subscribed to "CelebrationCompleted" that this player's celebration ended void NotifyCelebrationCompleted(); + // Tells the scripts subscribed to this rail activator ("PlayerRailArrivedNotification") of a rider's arrival + void NotifyPlayerRailArrived(const std::u16string& pathName, int32_t waypointNumber); void SetProximityRadius(float proxRadius, std::string name, uint32_t collisionGroup = 0); void SetProximityRadius(dpEntity* entity, std::string name); diff --git a/dGame/dGameMessages/MovementMessages.cpp b/dGame/dGameMessages/MovementMessages.cpp index dafe57a3c..d2bb49b06 100644 --- a/dGame/dGameMessages/MovementMessages.cpp +++ b/dGame/dGameMessages/MovementMessages.cpp @@ -317,7 +317,10 @@ namespace GameMessages { void PlayerRailArrivedNotification::Handle(Entity& entity, const SystemAddress& sysAddr) { const auto possibleRails = Game::entityManager->GetEntitiesByComponent(eReplicaComponentType::RAIL_ACTIVATOR); for (auto* possibleRail : possibleRails) { - if (possibleRail) possibleRail->GetScript()->OnPlayerRailArrived(possibleRail, &entity, pathName, waypointNumber); + if (!possibleRail) continue; + possibleRail->GetScript()->OnPlayerRailArrived(possibleRail, &entity, pathName, waypointNumber); + // The activator of this rail tells the scripts that asked to hear of its riders + if (possibleRail->GetVar(u"rail_path") == pathName) possibleRail->NotifyPlayerRailArrived(pathName, waypointNumber); } } diff --git a/dScripts/CppScripts.h b/dScripts/CppScripts.h index 2811cb1a6..34259b2db 100644 --- a/dScripts/CppScripts.h +++ b/dScripts/CppScripts.h @@ -207,6 +207,14 @@ namespace CppScripts { */ virtual void NotifyCelebrationCompleted(Entity* self, Entity* player) {}; + /** + * Invoked when a rider of a rail activator self subscribed to ("PlayerRailArrivedNotification") reaches a + * waypoint of its rail path. + * + * Equivalent to 'function notifyPlayerRailArrivedNotification(self, activator, msg)' + */ + virtual void NotifyPlayerRailArrived(Entity* self, Entity* activator, const std::u16string& pathName, int32_t waypointNumber) {}; + /** * Invoked when a player has responsed to a mission. * diff --git a/tests/dGameTests/FireTempleScriptsTests.cpp b/tests/dGameTests/FireTempleScriptsTests.cpp index a8a39e007..6e2046e7e 100644 --- a/tests/dGameTests/FireTempleScriptsTests.cpp +++ b/tests/dGameTests/FireTempleScriptsTests.cpp @@ -233,3 +233,30 @@ TEST_F(FireTempleScriptsTest, CelebrationCompletedReachesSubscribers) { message.Handle(*player, UNASSIGNED_SYSTEM_ADDRESS); EXPECT_EQ(listener.heard.size(), 1u); } + +namespace { + class RailListener : public CppScripts::Script { + public: + std::vector 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); +}