feat(scripts): GF shooting gallery target script (SG_TARGET)

The cannon gives its targets scripts/ai/ACT/SG_TARGET.lua as their
custom_script_server, but their own script row is the empty script, which
won. A custom server script now replaces the empty script too, and the
target script is ported:

- explode factor 2.5 at startup;
- its score is its spawn data's (the cannon scores a hit with it);
- at the last waypoint of its path it tells the cannon (FinishedPath, the
  cannon spawns the next one, as after a hit) and, when its spawn data
  says so, dies silently 1 s later (2 s on a moving platform), which is
  no hit;
- at the other waypoints it may change speed (spawn data chance, whole
  numbers between its least and most speed; only an object following
  waypoints takes it).

The cannon's own end-of-path handling is gone; it now answers the
target's FinishedPath. The waypoint PlayAim action is not ported: no
path in the game has one. The spawn data has no time score.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-11-27 17:04:15 -06:00
parent b63f65ee5d
commit 5a863659ee
8 changed files with 155 additions and 41 deletions

View File

@@ -40,7 +40,7 @@ void ScriptComponent::LoadDataFromTemplate() {
m_ClientScriptName = row.client_script_name;
}
// custom_script_server replaces a missing server script; a client script (the row's or custom_script_client, or
// custom_script_server replaces a missing (or empty) server script; a client script (the row's or custom_script_client, or
// any object's without a server script) makes it a client script component
void ScriptComponent::LoadConfigData() {
if (!m_FromObject) return;
@@ -50,7 +50,9 @@ void ScriptComponent::LoadConfigData() {
const auto customScriptServer = m_Parent->GetVarAsString(u"custom_script_server");
if (!m_Parent->GetVarAsString(u"custom_script_client").empty()) client = true;
if (!customScriptServer.empty() && m_ScriptName.empty()) m_ScriptName = customScriptServer;
// The client's script component takes a custom script over its row's; here it replaces the row's only where that is
// the empty script (the GF shooting gallery targets: the cannon gives them SG_TARGET.lua)
if (!customScriptServer.empty() && (m_ScriptName.empty() || m_ScriptName == "scripts\\empty.lua")) m_ScriptName = customScriptServer;
m_Client = client && m_ScriptName.empty();
SetScript(m_ScriptName);

View File

@@ -129,6 +129,7 @@
// SG Scripts
#include "SGCannon.h"
#include "SgTarget.h"
#include "ZoneSGServer.h"
// FV Scripts
@@ -529,6 +530,8 @@ namespace {
//SG
{"scripts\\ai\\MINIGAME\\SG_GF\\SERVER\\SG_CANNON.lua", []() {return new SGCannon();}},
// The cannon gives its targets this one, with forward slashes
{"scripts/ai/ACT/SG_TARGET.lua", []() {return new SgTarget();}},
{"scripts\\ai\\MINIGAME\\SG_GF\\L_ZONE_SG_SERVER.lua", []() {return new ZoneSGServer();}},
//PR

View File

@@ -1,3 +1,4 @@
set(DSCRIPTS_SOURCES_AI_MINIGAME_SG_GF_SERVER
"SGCannon.cpp"
"SgTarget.cpp"
PARENT_SCOPE)

View File

@@ -1,4 +1,5 @@
#include "SGCannon.h"
#include "SgTarget.h"
#include "EntityManager.h"
#include "GameMessages.h"
#include "EffectsMessages.h"
@@ -287,7 +288,7 @@ void SGCannon::DoSpawnTimerFunc(Entity* self, const std::string& name) {
info.pos = path->pathWaypoints[0].position;
info.settings.Insert<SGEnemy>(u"SpawnData", toSpawn);
info.settings.Insert<std::string>(u"custom_script_server", "scripts/ai/ACT/SG_TARGET.lua"); // never loaded (the targets have a script row): TargetFinishedPath does its part
info.settings.Insert<std::string>(u"custom_script_server", "scripts/ai/ACT/SG_TARGET.lua");
info.settings.Insert<std::string>(u"custom_script_client", "scripts/client/ai/SG_TARGET_CLIENT.lua");
info.settings.Insert<std::string>(u"attached_path", path->pathName);
info.settings.Insert<uint32_t>(u"attached_path_start", 0);
@@ -295,6 +296,10 @@ void SGCannon::DoSpawnTimerFunc(Entity* self, const std::string& name) {
info.settings.Insert<uint32_t>(u"wave", self->GetVar<uint32_t>(ThisWaveVariable));
auto* enemy = Game::entityManager->CreateEntity(info, nullptr, self);
// sg_cannon.lua (onChildLoaded) stores itself as the target's parent, which the target tells at the end of its
// path; the spawn timer is the one that spawns the next
enemy->SetVar<LWOOBJID>(u"My_Parent_ID", self->GetObjectID());
enemy->SetVar<std::string>(u"SpawnTimerName", name);
// sg_cannon.lua (onChildLoaded): a moving platform target gets SetPathingSpeed, which nothing on a moving platform
// reads, and SetCurrentPath{ pathName, startPoint = 0 }: its mover runs the path at the waypoints' speeds, as the
@@ -323,26 +328,11 @@ void SGCannon::DoSpawnTimerFunc(Entity* self, const std::string& name) {
}
// A hit target scores (live's cannon ball tells the cannon of its kills). One that got to the end of its path
// tells the cannon and dies silently a moment later (SG_TARGET.lua), which is no hit.
const auto silentDeath = std::make_shared<bool>(false);
enemy->AddDieCallback([this, self, enemy, name, silentDeath]() {
if (!*silentDeath) RegisterHit(self, enemy, name);
// tells the cannon and dies silently a moment later (its script, SG_TARGET.lua), which is no hit.
enemy->AddDieCallback([this, self, enemy, name]() {
if (!SgTarget::DiedSilently(enemy)) RegisterHit(self, enemy, name);
});
// SG_TARGET.lua (onArrived): the Arrived message says whether the waypoint is the path's last one, for a moving
// platform as for an object following waypoints (the script reads isLastPoint, which the client's PushToLua
// sets from the path and the waypoint)
const auto lastWaypoint = static_cast<uint32_t>(path->pathWaypoints.size() - 1);
if (movingPlatform) {
movingPlatform->AddPlatformAtLastWaypointCallback([this, self, enemy, name, silentDeath]() {
TargetFinishedPath(self, enemy, name, silentDeath);
});
} else if (movementAI) {
movementAI->AddWaypointReachedCallback([this, self, enemy, name, silentDeath, lastWaypoint](const uint32_t waypoint) {
if (waypoint == lastWaypoint) TargetFinishedPath(self, enemy, name, silentDeath);
});
}
// Save the enemy
if (enemy != nullptr) {
const_cast<std::vector<LWOOBJID>&>(self->GetVar<std::vector<LWOOBJID>>(SpawnedObjects)).push_back(enemy->GetObjectID());
@@ -712,24 +702,9 @@ void SGCannon::CheckSpawn(Entity* self, Entity* target, const std::string& timer
}
}
// SG_TARGET.lua: at the last waypoint of its path a target notifies the cannon (FinishedPath: sg_cannon.lua spawns the
// next one as after a hit, without a score) and, when it is to despawn there, dies silently 1 s later, a moving
// platform after 2 s (so the clients' platforms get to the end of the path first).
void SGCannon::TargetFinishedPath(Entity* self, Entity* target, const std::string& timerName, const std::shared_ptr<bool>& silentDeath) {
// Once: a platform sent on would tell again
if (target->GetVar<bool>(u"FinishedPath")) return;
target->SetVar<bool>(u"FinishedPath", true);
CheckSpawn(self, target, timerName);
const auto& spawnInfo = target->GetVar<SGEnemy>(u"SpawnData");
if (!spawnInfo.despawnOnLastWaypoint) return;
target->AddCallbackTimer(spawnInfo.isMovingPlatform ? 2.0f : 1.0f, [target, silentDeath]() {
// Hit in the meantime
if (target->GetIsDead()) return;
*silentDeath = true;
target->Smash(target->GetObjectID(), eKillType::SILENT);
});
// sg_cannon.lua: a target at the end of its path (SG_TARGET.lua) has the next one spawned, as after a hit
void SGCannon::OnNotifyObject(Entity* self, Entity* sender, const std::string& name, int32_t param1, int32_t param2) {
if (name == "FinishedPath" && sender != nullptr) CheckSpawn(self, sender, sender->GetVar<std::string>(u"SpawnTimerName"));
}
void SGCannon::RegisterHit(Entity* self, Entity* target, const std::string& timerName) {
@@ -742,7 +717,7 @@ void SGCannon::RegisterHit(Entity* self, Entity* target, const std::string& time
CheckSpawn(self, target, timerName);
int32_t score = spawnInfo.score;
int32_t score = SgTarget::GetActivityPoints(target);
if (score > 0) {
score += score * GetCurrentBonus(self);

View File

@@ -59,6 +59,7 @@ class SGCannon : public ActivityManager {
public:
void OnStartup(Entity* self) override;
void OnPlayerLoaded(Entity* self, Entity* player) override;
void OnNotifyObject(Entity* self, Entity* sender, const std::string& name, int32_t param1 = 0, int32_t param2 = 0) override;
void OnFireEventServerSide(Entity* self, Entity* sender, std::string args, int32_t param1, int32_t param2, int32_t param3) override;
void OnActivityStateChangeRequest(Entity* self, LWOOBJID senderID, int32_t value1, int32_t value2, const std::u16string& stringValue) override;
void OnMessageBoxResponse(Entity* self, Entity* sender, int32_t button, const std::u16string& identifier, const std::u16string& userData) override;
@@ -93,7 +94,6 @@ private:
// sg_cannon.lua (checkSpawn): a target that respawns gets its next spawn, while its wave runs
void CheckSpawn(Entity* self, Entity* target, const std::string& timerName);
// SG_TARGET.lua (onArrived at the last waypoint): the cannon spawns the next one, and the target is gone a moment later
void TargetFinishedPath(Entity* self, Entity* target, const std::string& timerName, const std::shared_ptr<bool>& silentDeath);
void UpdateStreak(Entity* self);
float_t GetCurrentBonus(Entity* self);

View File

@@ -0,0 +1,75 @@
#include "SgTarget.h"
#include "DestroyableComponent.h"
#include "Entity.h"
#include "EntityManager.h"
#include "GeneralUtils.h"
#include "MovementAIComponent.h"
#include "SGCannon.h"
#include "Zone.h"
#include "dZoneManager.h"
#include "eKillType.h"
void SgTarget::OnStartup(Entity* self) {
if (auto* const destroyable = self->GetComponent<DestroyableComponent>()) destroyable->SetExplodeFactor(2.5f);
}
int32_t SgTarget::GetActivityPoints(Entity* self) {
return self->HasVar(u"SpawnData") ? self->GetVar<SGEnemy>(u"SpawnData").score : 0;
}
bool SgTarget::DiedSilently(Entity* self) {
return self->GetVar<bool>(u"SilentDeath");
}
// The Arrived message's isLastPoint: whether the waypoint is the last of the path it arrived on (the one it was put
// on: its attached_path)
void SgTarget::OnWaypointReached(Entity* self, const uint32_t waypointIndex) {
const auto* const path = Game::zoneManager->GetZone()->GetPath(self->GetVarAsString(u"attached_path"));
if (path != nullptr && !path->pathWaypoints.empty() && waypointIndex == path->pathWaypoints.size() - 1) {
NotifyParentToRespawn(self);
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.
}
// The cannon spawns the next one (as after a hit, without a score); then the target despawns
void SgTarget::NotifyParentToRespawn(Entity* self) {
auto* const parent = Game::entityManager->GetEntity(self->GetVar<LWOOBJID>(u"My_Parent_ID"));
if (parent != nullptr) parent->GetScript()->OnNotifyObject(parent, self, "FinishedPath");
if (!self->HasVar(u"SpawnData")) {
self->CancelAllTimers();
self->SetVar<bool>(u"SilentDeath", true);
self->Smash(self->GetObjectID(), eKillType::SILENT);
return;
}
// 2 s for a moving platform, so the clients' platforms get to the end of the path first
const auto& spawnData = self->GetVar<SGEnemy>(u"SpawnData");
if (!spawnData.despawnOnLastWaypoint) return;
self->CancelTimer("Despawn");
self->AddTimer("Despawn", spawnData.isMovingPlatform ? 2.0f : 1.0f);
}
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);
}
// 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).
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)));
if (auto* const movementAI = self->GetComponent<MovementAIComponent>()) {
movementAI->SetMaxSpeed(newSpeed);
movementAI->SetCurrentSpeed(newSpeed);
}
}

View File

@@ -0,0 +1,22 @@
#pragma once
#include "CppScripts.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.
*/
class SgTarget : public CppScripts::Script {
public:
void OnStartup(Entity* self) override;
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)
static int32_t GetActivityPoints(Entity* self);
// 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);
};

View File

@@ -16,6 +16,8 @@
#include "MovementMessages.h"
#include "MovingPlatformComponent.h"
#include "02_server/Map/General/SimpleOnceOnlyMoverSpinnerEvent.h"
#include "ai/MINIGAME/SG_GF/SERVER/SGCannon.h"
#include "ai/MINIGAME/SG_GF/SERVER/SgTarget.h"
#include "ScriptComponent.h"
#include "SimplePhysicsComponent.h"
#include "Zone.h"
@@ -1167,3 +1169,37 @@ TEST_F(MovingPlatformComponentTest, WaypointsInOnePlaceArriveAtOnce) {
EXPECT_EQ(aloneMessages.arrived, (std::vector<uint32_t>{ 0, 0, 0 }));
EXPECT_TRUE(aloneMessages.departed.empty());
}
// SG_TARGET.lua: a target scores its spawn data's score; at the last waypoint of its path it despawns a moment later
// (1 s, 2 s on a moving platform) when its spawn data says so, silently (no hit); at the others it does not
TEST_F(MovingPlatformComponentTest, ShootingGalleryTargetAtTheEndOfItsPath) {
Path path;
path.pathType = PathType::MovingPlatform;
path.pathName = "TestTargetPath";
path.pathBehavior = PathBehavior::Once;
PathWaypoint first;
first.speed = 10.0f;
PathWaypoint second = first;
second.position = NiPoint3(10.0f, 0.0f, 0.0f);
path.pathWaypoints = { first, second };
const_cast<Zone*>(Game::zoneManager->GetZone())->AddPath(path);
SGEnemy spawnData{};
spawnData.score = 1500;
spawnData.isMovingPlatform = true;
spawnData.despawnOnLastWaypoint = true;
info.settings.Insert<std::string>(u"attached_path", "TestTargetPath");
Entity entity(288300744895900134, info);
info.settings.values.clear();
entity.SetVar<SGEnemy>(u"SpawnData", spawnData);
SgTarget script;
EXPECT_EQ(SgTarget::GetActivityPoints(&entity), 1500);
script.OnWaypointReached(&entity, 0);
entity.Update(0.0f);
EXPECT_FALSE(entity.HasTimer("Despawn"));
script.OnWaypointReached(&entity, 1);
entity.Update(0.0f);
EXPECT_TRUE(entity.HasTimer("Despawn"));
EXPECT_FALSE(SgTarget::DiedSilently(&entity));
}