Files
DarkflameServer/dScripts/ai/MINIGAME/SG_GF/SERVER/SgTarget.cpp
Aaron Kimbrell ce4f557c81 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>
2026-11-27 19:09:53 -06:00

102 lines
4.7 KiB
C++

#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"
#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) {
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) {
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);
// 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
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);
}
// 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 == "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)
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;
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(speed);
movementAI->SetCurrentSpeed(speed);
}
}