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 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-29 00:16:41 -06:00
parent 501ab8b58b
commit ff782b9641
5 changed files with 53 additions and 3 deletions

View File

@@ -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);
}
}

View File

@@ -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);

View File

@@ -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<std::u16string>(u"rail_path") == pathName) possibleRail->NotifyPlayerRailArrived(pathName, waypointNumber);
}
}

View File

@@ -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.
*

View File

@@ -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<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);
}