Files
DarkflameServer/dScripts/ai/MINIGAME/SG_GF/SERVER/SgTarget.cpp
Aaron Kimbrell 5a863659ee 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>
2026-11-27 18:49:46 -06:00

76 lines
3.1 KiB
C++

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