feat(scripts): SG_TARGET time score write, PlayAim and ChangeSpeed timer

Ports the parts of SG_TARGET.lua shipped data never reaches: the
SetActivityUserData typeIndex 5 write, the PlayAim waypoint action and the
ChangeSpeed timer branch.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-27 19:09:53 -06:00
parent 22e115414f
commit ce4f557c81
3 changed files with 111 additions and 15 deletions

View File

@@ -1,10 +1,12 @@
#include "SgTarget.h"
#include "ScriptedActivityComponent.h"
#include "DestroyableComponent.h"
#include "Entity.h"
#include "EntityManager.h"
#include "GeneralUtils.h"
#include "MovementAIComponent.h"
#include "RenderComponent.h"
#include "SGCannon.h"
#include "Zone.h"
#include "dZoneManager.h"
@@ -15,7 +17,20 @@ void SgTarget::OnStartup(Entity* self) {
}
int32_t SgTarget::GetActivityPoints(Entity* self) {
return self->HasVar(u"SpawnData") ? self->GetVar<SGEnemy>(u"SpawnData").score : 0;
if (!self->HasVar(u"SpawnData")) return 0;
const int32_t points = self->GetVar<SGEnemy>(u"SpawnData").score;
// SetActivityUserData{userID = zone control's GetActivityUser, typeIndex 5, value = points} on the target: only an
// activity takes it, and the zone control without one has no user
const auto* const zoneControl = Game::entityManager->GetZoneControlEntity();
const auto* const zoneActivity = zoneControl != nullptr ? zoneControl->GetComponent<ScriptedActivityComponent>() : nullptr;
const LWOOBJID userID = zoneActivity != nullptr ? zoneActivity->GetActivityUser() : LWOOBJID_EMPTY;
if (auto* const activity = self->GetComponent<ScriptedActivityComponent>()) activity->SetActivityValue(userID, 5, static_cast<float>(points));
return points;
}
std::string SgTarget::GetPlayAimAnimation(const PathWaypoint& waypoint) {
if (waypoint.actions.empty() || waypoint.actions.front().first != "PlayAim") return "";
return waypoint.actions.front().second;
}
bool SgTarget::DiedSilently(Entity* self) {
@@ -31,8 +46,12 @@ void SgTarget::OnWaypointReached(Entity* self, const uint32_t waypointIndex) {
return;
}
ChangeSpeed(self);
// The script also plays the animation a waypoint's PlayAim action names; no path in the game has one. Then it
// continues its waypoints, which the movement does by itself.
// A first action of PlayAim plays the animation it names, when the target has it (no path in the game has one)
if (path != nullptr && waypointIndex < path->pathWaypoints.size()) {
const auto animation = GetPlayAimAnimation(path->pathWaypoints[waypointIndex]);
if (!animation.empty() && RenderComponent::GetAnimationTime(self, animation) > 0.0f) RenderComponent::PlayAnimation(self, animation);
}
// Then it continues its waypoints, which the movement does by itself
}
// The cannon spawns the next one (as after a hit, without a score); then the target despawns
@@ -53,23 +72,30 @@ void SgTarget::NotifyParentToRespawn(Entity* self) {
self->AddTimer("Despawn", spawnData.isMovingPlatform ? 2.0f : 1.0f);
}
// ChangeSpeed: a speed of 1 to 4 every 5 s (the script's startup never starts this timer); Despawn: dies silently
void SgTarget::OnTimerDone(Entity* self, std::string timerName) {
if (timerName != "Despawn") return;
self->CancelAllTimers();
self->SetVar<bool>(u"SilentDeath", true);
self->Smash(self->GetObjectID(), eKillType::SILENT);
if (timerName == "ChangeSpeed") {
SetPathingSpeed(self, static_cast<float>(GeneralUtils::GenerateRandomNumber<int32_t>(1, 4)));
self->AddTimer("ChangeSpeed", 5.0f);
} else if (timerName == "Despawn") {
self->CancelAllTimers();
self->SetVar<bool>(u"SilentDeath", true);
self->Smash(self->GetObjectID(), eKillType::SILENT);
}
}
// With its spawn data's chance, a new speed between its least and most (whole numbers, as Lua's math.random). The
// script sets it with SetPathingSpeed, which only an object following waypoints takes (a moving platform has no
// handler for it).
// With its spawn data's chance, a new speed between its least and most (whole numbers, as Lua's math.random)
void SgTarget::ChangeSpeed(Entity* self) {
if (!self->HasVar(u"SpawnData")) return;
const auto& spawnData = self->GetVar<SGEnemy>(u"SpawnData");
if (!spawnData.changeSpeedAtWaypoint || GeneralUtils::GenerateRandomNumber<float>(0.0f, 1.0f) > spawnData.speedChangeChance) return;
const auto newSpeed = static_cast<float>(GeneralUtils::GenerateRandomNumber<int32_t>(static_cast<int32_t>(spawnData.minSpeed), static_cast<int32_t>(spawnData.maxSpeed)));
SetPathingSpeed(self, static_cast<float>(GeneralUtils::GenerateRandomNumber<int32_t>(static_cast<int32_t>(spawnData.minSpeed), static_cast<int32_t>(spawnData.maxSpeed))));
}
// SetPathingSpeed: only an object following waypoints takes it (a moving platform has no handler for it)
void SgTarget::SetPathingSpeed(Entity* self, const float speed) {
if (auto* const movementAI = self->GetComponent<MovementAIComponent>()) {
movementAI->SetMaxSpeed(newSpeed);
movementAI->SetCurrentSpeed(newSpeed);
movementAI->SetMaxSpeed(speed);
movementAI->SetCurrentSpeed(speed);
}
}

View File

@@ -1,10 +1,12 @@
#pragma once
#include "CppScripts.h"
#include "ZoneFile.h"
/**
* A GF shooting gallery target (SG_TARGET.lua, set by the cannon as the target's custom_script_server): its explode
* factor, its score, and what it does at the waypoints of its path. At the last one it tells the cannon
* (FinishedPath, the cannon spawns the next one) and dies silently a moment later; at the others it may change speed.
* (FinishedPath, the cannon spawns the next one) and dies silently a moment later; at the others it may change speed
* and plays the animation a PlayAim action names.
*/
class SgTarget : public CppScripts::Script {
public:
@@ -12,11 +14,15 @@ public:
void OnWaypointReached(Entity* self, uint32_t waypointIndex) override;
void OnTimerDone(Entity* self, std::string timerName) override;
// The points a hit gives (the script's GetActivityPoints: its spawn data's score; the spawn data has no time score)
// The points a hit gives (the script's GetActivityPoints: its spawn data's score; the spawn data has no time score).
// Also writes them as user data 5 of the zone control's activity user on the target's own activity, if it has one.
static int32_t GetActivityPoints(Entity* self);
// The animation a waypoint's first action names when that action is PlayAim, else empty
static std::string GetPlayAimAnimation(const PathWaypoint& waypoint);
// Whether it died silently at the end of its path (that is no hit)
static bool DiedSilently(Entity* self);
private:
void NotifyParentToRespawn(Entity* self);
void ChangeSpeed(Entity* self);
void SetPathingSpeed(Entity* self, float speed);
};

View File

@@ -7,6 +7,7 @@
#include <glm/gtc/quaternion.hpp>
#include "ScriptedActivityComponent.h"
#include "BitStream.h"
#include "CDClientDatabase.h"
#include "Entity.h"
@@ -1203,3 +1204,66 @@ TEST_F(MovingPlatformComponentTest, ShootingGalleryTargetAtTheEndOfItsPath) {
EXPECT_TRUE(entity.HasTimer("Despawn"));
EXPECT_FALSE(SgTarget::DiedSilently(&entity));
}
// SG_TARGET.lua's PlayAim: only a waypoint whose first action is PlayAim names an animation
TEST_F(MovingPlatformComponentTest, ShootingGalleryTargetPlayAimAction) {
PathWaypoint waypoint;
EXPECT_EQ(SgTarget::GetPlayAimAnimation(waypoint), "");
waypoint.actions = { { "PlayAim", "attack" } };
EXPECT_EQ(SgTarget::GetPlayAimAnimation(waypoint), "attack");
waypoint.actions = { { "delay", "1" }, { "PlayAim", "attack" } };
EXPECT_EQ(SgTarget::GetPlayAimAnimation(waypoint), "");
// A target without the animation (no render component) arrives there as at any other waypoint
Path path;
path.pathType = PathType::MovingPlatform;
path.pathName = "TestAimPath";
path.pathBehavior = PathBehavior::Once;
PathWaypoint first;
first.speed = 10.0f;
first.actions = { { "PlayAim", "attack" } };
PathWaypoint second = first;
second.position = NiPoint3(10.0f, 0.0f, 0.0f);
path.pathWaypoints = { first, second };
const_cast<Zone*>(Game::zoneManager->GetZone())->AddPath(path);
info.settings.Insert<std::string>(u"attached_path", "TestAimPath");
Entity entity(288300744895900140, info);
info.settings.values.clear();
entity.SetVar<SGEnemy>(u"SpawnData", SGEnemy{});
SgTarget script;
script.OnWaypointReached(&entity, 0);
entity.Update(0.0f);
EXPECT_FALSE(entity.HasTimer("Despawn"));
}
// SG_TARGET.lua's ChangeSpeed timer: it starts again every time it runs (5 s); Despawn cancels it
TEST_F(MovingPlatformComponentTest, ShootingGalleryTargetChangeSpeedTimer) {
Entity entity(288300744895900141, info);
SgTarget script;
script.OnTimerDone(&entity, "ChangeSpeed");
entity.Update(0.0f);
EXPECT_TRUE(entity.HasTimer("ChangeSpeed"));
EXPECT_FALSE(SgTarget::DiedSilently(&entity));
script.OnTimerDone(&entity, "Despawn");
entity.Update(0.0f);
EXPECT_FALSE(entity.HasTimer("ChangeSpeed"));
EXPECT_TRUE(SgTarget::DiedSilently(&entity));
}
// SG_TARGET.lua's GetActivityPoints writes the points as user data 5 on the target's own activity, for the zone
// control's activity user (none here: no zone control)
TEST_F(MovingPlatformComponentTest, ShootingGalleryTargetWritesItsTimeScore) {
Entity entity(288300744895900142, info);
SGEnemy spawnData{};
spawnData.score = 500;
entity.SetVar<SGEnemy>(u"SpawnData", spawnData);
auto* const activity = entity.AddComponent<ScriptedActivityComponent>(-1);
EXPECT_EQ(SgTarget::GetActivityPoints(&entity), 500);
EXPECT_FLOAT_EQ(activity->GetActivityValue(LWOOBJID_EMPTY, 5), 500.0f);
EXPECT_FLOAT_EQ(activity->GetActivityValue(LWOOBJID_EMPTY, 0), 0.0f);
Entity noData(288300744895900143, info);
auto* const otherActivity = noData.AddComponent<ScriptedActivityComponent>(-1);
EXPECT_EQ(SgTarget::GetActivityPoints(&noData), 0);
EXPECT_FALSE(otherActivity->PlayerHasActivityData(LWOOBJID_EMPTY));
}