Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Address crash in Cannon Cove Shooting Gallery #1211

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
278 changes: 155 additions & 123 deletions dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,166 +160,198 @@ void SGCannon::OnMessageBoxResponse(Entity* self, Entity* sender, int32_t button
}
}

void SGCannon::OnActivityTimerDone(Entity* self, const std::string& name) {
if (name == SuperChargeTimer && !self->GetVar<bool>(SuperChargePausedVariable)) {
if (self->GetVar<bool>(WaveStatusVariable) || self->GetVar<uint32_t>(CurrentSuperChargedTimeVariable) < 1) {
self->SetNetworkVar<uint32_t>(ChargeCountingVariable, 99);
self->SetNetworkVar<uint32_t>(SuperChargeBarVariable, 0);
ToggleSuperCharge(self, false);
}
} else if (name == SpawnWaveTimer) {
if (self->GetVar<bool>(GameStartedVariable)) {
self->SetVar<bool>(WaveStatusVariable, true);
const auto wave = (int32_t)self->GetVar<uint32_t>(ThisWaveVariable);
void SGCannon::SuperChargeTimerFunc(Entity* self) {
if (self->GetVar<bool>(WaveStatusVariable) || self->GetVar<uint32_t>(CurrentSuperChargedTimeVariable) < 1) {
self->SetNetworkVar<uint32_t>(ChargeCountingVariable, 99);
self->SetNetworkVar<uint32_t>(SuperChargeBarVariable, 0);
ToggleSuperCharge(self, false);
}
}

if (wave != 0 && self->GetVar<bool>(SuperChargePausedVariable)) {
StartChargedCannon(self, self->GetVar<uint32_t>(CurrentSuperChargedTimeVariable));
self->SetVar<uint32_t>(CurrentSuperChargedTimeVariable, 0);
}
void SGCannon::SpawnWaveTimerFunc(Entity* self) {
if (self->GetVar<bool>(GameStartedVariable)) {
self->SetVar<bool>(WaveStatusVariable, true);
const auto wave = (int32_t)self->GetVar<uint32_t>(ThisWaveVariable);

TimerToggle(self, true);
if (wave != 0 && self->GetVar<bool>(SuperChargePausedVariable)) {
StartChargedCannon(self, self->GetVar<uint32_t>(CurrentSuperChargedTimeVariable));
self->SetVar<uint32_t>(CurrentSuperChargedTimeVariable, 0);
}

for (const auto& enemyToSpawn : m_Waves.at(self->GetVar<uint32_t>(ThisWaveVariable))) {
SpawnObject(self, enemyToSpawn, true);
}
TimerToggle(self, true);

Game::logger->Log("SGCannon", "Current wave spawn: %i/%i", wave, m_Waves.size());
for (const auto& enemyToSpawn : m_Waves.at(self->GetVar<uint32_t>(ThisWaveVariable))) {
SpawnObject(self, enemyToSpawn, true);
}

// All waves completed
const auto timeLimit = (float_t)self->GetVar<uint32_t>(TimeLimitVariable);
if (wave >= m_Waves.size()) {
ActivityTimerStart(self, GameOverTimer, timeLimit, timeLimit);
} else {
ActivityTimerStart(self, EndWaveTimer, timeLimit, timeLimit);
}
Game::logger->Log("SGCannon", "Current wave spawn: %i/%i", wave, m_Waves.size());

const auto* player = Game::entityManager->GetEntity(self->GetVar<LWOOBJID>(PlayerIDVariable));
if (player != nullptr) {
GameMessages::SendPlayFXEffect(player->GetObjectID(), -1, u"SG-start", "");
// All waves completed
const auto timeLimit = (float_t)self->GetVar<uint32_t>(TimeLimitVariable);
if (wave >= m_Waves.size()) {
ActivityTimerStart(self, GameOverTimer, timeLimit, timeLimit);
} else {
ActivityTimerStart(self, EndWaveTimer, timeLimit, timeLimit);
}

GameMessages::SendStartActivityTime(self->GetObjectID(), timeLimit, player->GetSystemAddress());
Game::logger->Log("SGCannon", "Sending ActivityPause false");
const auto* player = Game::entityManager->GetEntity(self->GetVar<LWOOBJID>(PlayerIDVariable));
if (player != nullptr) {
GameMessages::SendPlayFXEffect(player->GetObjectID(), -1, u"SG-start", "");

GameMessages::SendActivityPause(self->GetObjectID(), false, player->GetSystemAddress());
}
}
} else if (name == EndWaveTimer) {
self->SetVar<bool>(WaveStatusVariable, false);
TimerToggle(self);
RecordPlayerScore(self);
GameMessages::SendStartActivityTime(self->GetObjectID(), timeLimit, player->GetSystemAddress());
Game::logger->Log("SGCannon", "Sending ActivityPause false");

if (self->GetVar<uint32_t>(ThisWaveVariable) >= 2) {
GameMessages::SendActivityPause(self->GetObjectID(), true);
ActivityTimerStart(self, GameOverTimer, 0.1, 0.1);
return;
GameMessages::SendActivityPause(self->GetObjectID(), false, player->GetSystemAddress());
}
}
}

self->SetVar<uint32_t>(ThisWaveVariable, self->GetVar<uint32_t>(ThisWaveVariable) + 1);
PlaySceneAnimation(self, u"wave" + GeneralUtils::to_u16string(self->GetVar<uint32_t>(ThisWaveVariable)), true, true, 1.7f);
self->SetNetworkVar<uint32_t>(WaveNumVariable, self->GetVar<uint32_t>(ThisWaveVariable) + 1);
self->SetNetworkVar<uint32_t>(WaveStrVariable, self->GetVar<uint32_t>(TimeLimitVariable));
void SGCannon::EndWaveTimerFunc(Entity* self) {
self->SetVar<bool>(WaveStatusVariable, false);
TimerToggle(self);
RecordPlayerScore(self);

Game::logger->Log("SGCannon", "Current wave: %i/%i", self->GetVar<uint32_t>(ThisWaveVariable), m_Waves.size());
if (self->GetVar<uint32_t>(ThisWaveVariable) >= 2) {
GameMessages::SendActivityPause(self->GetObjectID(), true);
ActivityTimerStart(self, GameOverTimer, 0.1, 0.1);
return;
}

if (self->GetVar<uint32_t>(ThisWaveVariable) >= m_Waves.size()) {
ActivityTimerStart(self, GameOverTimer, 0.1, 0.1);
} else {
ActivityTimerStart(self, SpawnWaveTimer, constants.inBetweenWavePause, constants.inBetweenWavePause);
}
self->SetVar<uint32_t>(ThisWaveVariable, self->GetVar<uint32_t>(ThisWaveVariable) + 1);
PlaySceneAnimation(self, u"wave" + GeneralUtils::to_u16string(self->GetVar<uint32_t>(ThisWaveVariable)), true, true, 1.7f);
self->SetNetworkVar<uint32_t>(WaveNumVariable, self->GetVar<uint32_t>(ThisWaveVariable) + 1);
self->SetNetworkVar<uint32_t>(WaveStrVariable, self->GetVar<uint32_t>(TimeLimitVariable));

Game::logger->Log("SGCannon", "Current wave: %i/%i", self->GetVar<uint32_t>(ThisWaveVariable), m_Waves.size());

if (self->GetVar<uint32_t>(ThisWaveVariable) >= m_Waves.size()) {
ActivityTimerStart(self, GameOverTimer, 0.1, 0.1);
} else {
ActivityTimerStart(self, SpawnWaveTimer, constants.inBetweenWavePause, constants.inBetweenWavePause);
}

Game::logger->Log("SGCannon", "Sending ActivityPause true");

GameMessages::SendActivityPause(self->GetObjectID(), true);
if (self->GetVar<bool>(SuperChargeActiveVariable) && !self->GetVar<bool>(SuperChargePausedVariable)) {
PauseChargeCannon(self);
}
}

void SGCannon::GameOverTimerFunc(Entity* self) {
auto* player = Game::entityManager->GetEntity(self->GetVar<LWOOBJID>(PlayerIDVariable));
if (player != nullptr) {
Game::logger->Log("SGCannon", "Sending ActivityPause true");

GameMessages::SendActivityPause(self->GetObjectID(), true);
if (self->GetVar<bool>(SuperChargeActiveVariable) && !self->GetVar<bool>(SuperChargePausedVariable)) {
PauseChargeCannon(self);
}
} else if (name == GameOverTimer) {
auto* player = Game::entityManager->GetEntity(self->GetVar<LWOOBJID>(PlayerIDVariable));
if (player != nullptr) {
Game::logger->Log("SGCannon", "Sending ActivityPause true");
GameMessages::SendActivityPause(self->GetObjectID(), true, player->GetSystemAddress());

GameMessages::SendActivityPause(self->GetObjectID(), true, player->GetSystemAddress());
/*const auto leftoverCannonballs = Game::entityManager->GetEntitiesInGroup("cannonball");
if (leftoverCannonballs.empty()) {
RecordPlayerScore(self);

/*const auto leftoverCannonballs = Game::entityManager->GetEntitiesInGroup("cannonball");
if (leftoverCannonballs.empty()) {
RecordPlayerScore(self);
} else {
ActivityTimerStart(self, EndGameBufferTimer, 1, leftoverCannonballs.size());
}*/

} else {
ActivityTimerStart(self, EndGameBufferTimer, 1, leftoverCannonballs.size());
}*/
ActivityTimerStart(self, EndGameBufferTimer, 1, 1);

ActivityTimerStart(self, EndGameBufferTimer, 1, 1);
TimerToggle(self);
}
}

TimerToggle(self);
void SGCannon::DoSpawnTimerFunc(Entity* self, const std::string& name) {
if (self->GetVar<bool>(GameStartedVariable)) {
Game::logger->LogDebug("SGCannon", "time name %s %s", name.c_str(), name.substr(7).c_str());
const auto spawnNumber = (uint32_t)std::stoi(name.substr(7));
const auto& activeSpawns = self->GetVar<std::vector<SGEnemy>>(ActiveSpawnsVariable);
Game::logger->LogDebug("SGCannon", "size %i, %i", activeSpawns.size(), spawnNumber);
if (activeSpawns.size() <= spawnNumber) {
Game::logger->Log("SGCannon", "Trying to spawn %i when spawns size is only %i", spawnNumber, activeSpawns.size());
return;
}
} else if (name.rfind(DoSpawnTimer, 0) == 0) {
if (self->GetVar<bool>(GameStartedVariable)) {
const auto spawnNumber = (uint32_t)std::stoi(name.substr(7));
const auto& activeSpawns = self->GetVar<std::vector<SGEnemy>>(ActiveSpawnsVariable);
if (activeSpawns.size() < spawnNumber) {
Game::logger->Log("SGCannon", "Trying to spawn %i when spawns size is only %i", spawnNumber, activeSpawns.size());
return;
}
const auto& toSpawn = activeSpawns.at(spawnNumber);
const auto pathIndex = GeneralUtils::GenerateRandomNumber<float_t>(0, toSpawn.spawnPaths.size() - 1);
const auto* path = Game::zoneManager->GetZone()->GetPath(toSpawn.spawnPaths.at(pathIndex));
if (!path) {
Game::logger->Log("SGCannon", "Path %s at index %i is null", toSpawn.spawnPaths.at(pathIndex).c_str(), pathIndex);
return;
}

auto info = EntityInfo{};
info.lot = toSpawn.lot;
info.spawnerID = self->GetObjectID();
info.pos = path->pathWaypoints.at(0).position;
const auto& toSpawn = activeSpawns.at(spawnNumber);
Game::logger->LogDebug("SGCannon", "toSpawn %i", toSpawn.spawnPaths.size());
const auto pathIndex = GeneralUtils::GenerateRandomNumber<float_t>(0, toSpawn.spawnPaths.size() - 1);
Game::logger->LogDebug("SGCannon", "index %f", pathIndex);
Game::logger->LogDebug("SGCannon", "%s", toSpawn.spawnPaths.at(pathIndex).c_str());
const auto* path = Game::zoneManager->GetZone()->GetPath(toSpawn.spawnPaths.at(pathIndex));
if (!path) {
Game::logger->Log("SGCannon", "Path %s at index %i is null", toSpawn.spawnPaths.at(pathIndex).c_str(), pathIndex);
return;
}

info.settings = {
new LDFData<SGEnemy>(u"SpawnData", toSpawn),
new LDFData<std::string>(u"custom_script_server", "scripts/ai/ACT/SG_TARGET.lua"),
new LDFData<std::string>(u"custom_script_client", "scripts/client/ai/SG_TARGET_CLIENT.lua"),
new LDFData<std::string>(u"attached_path", path->pathName),
new LDFData<uint32_t>(u"attached_path_start", 0),
new LDFData<std::u16string>(u"groupID", u"SGEnemy")
};
Game::logger->LogDebug("SGCannon", "%s", path->pathName.c_str());

Game::logger->Log("SGCannon", "Spawning enemy %i on path %s", toSpawn.lot, path->pathName.c_str());
auto info = EntityInfo{};
info.lot = toSpawn.lot;
info.spawnerID = self->GetObjectID();
info.pos = path->pathWaypoints.at(0).position;

auto* enemy = Game::entityManager->CreateEntity(info, nullptr, self);
Game::entityManager->ConstructEntity(enemy);
info.settings = {
new LDFData<SGEnemy>(u"SpawnData", toSpawn),
new LDFData<std::string>(u"custom_script_server", "scripts/ai/ACT/SG_TARGET.lua"),
new LDFData<std::string>(u"custom_script_client", "scripts/client/ai/SG_TARGET_CLIENT.lua"),
new LDFData<std::string>(u"attached_path", path->pathName),
new LDFData<uint32_t>(u"attached_path_start", 0),
new LDFData<std::u16string>(u"groupID", u"SGEnemy")
};

auto* movementAI = new MovementAIComponent(enemy, {});
Game::logger->Log("SGCannon", "Spawning enemy %i on path %s", toSpawn.lot, path->pathName.c_str());

enemy->AddComponent(eReplicaComponentType::MOVEMENT_AI, movementAI);
auto* enemy = Game::entityManager->CreateEntity(info, nullptr, self);
Game::entityManager->ConstructEntity(enemy);

movementAI->SetMaxSpeed(toSpawn.initialSpeed);
movementAI->SetCurrentSpeed(toSpawn.initialSpeed);
movementAI->SetHaltDistance(0.0f);
auto* movementAI = new MovementAIComponent(enemy, {});

std::vector<NiPoint3> pathWaypoints;
enemy->AddComponent(eReplicaComponentType::MOVEMENT_AI, movementAI);

for (const auto& waypoint : path->pathWaypoints) {
pathWaypoints.push_back(waypoint.position);
}
movementAI->SetMaxSpeed(toSpawn.initialSpeed);
movementAI->SetCurrentSpeed(toSpawn.initialSpeed);
movementAI->SetHaltDistance(0.0f);

if (GeneralUtils::GenerateRandomNumber<float_t>(0, 1) < 0.5f) {
std::reverse(pathWaypoints.begin(), pathWaypoints.end());
}
std::vector<NiPoint3> pathWaypoints;

movementAI->SetPath(pathWaypoints);
for (const auto& waypoint : path->pathWaypoints) {
pathWaypoints.push_back(waypoint.position);
}

enemy->AddDieCallback([this, self, enemy, name]() {
RegisterHit(self, enemy, name);
});
if (GeneralUtils::GenerateRandomNumber<float_t>(0, 1) < 0.5f) {
std::reverse(pathWaypoints.begin(), pathWaypoints.end());
}

// Save the enemy and tell it to start pathing
if (enemy != nullptr) {
const_cast<std::vector<LWOOBJID>&>(self->GetVar<std::vector<LWOOBJID>>(SpawnedObjects)).push_back(enemy->GetObjectID());
GameMessages::SendPlatformResync(enemy, UNASSIGNED_SYSTEM_ADDRESS);
}
movementAI->SetPath(pathWaypoints);

enemy->AddDieCallback([this, self, enemy, name]() {
RegisterHit(self, enemy, name);
});

// Save the enemy and tell it to start pathing
if (enemy != nullptr) {
const_cast<std::vector<LWOOBJID>&>(self->GetVar<std::vector<LWOOBJID>>(SpawnedObjects)).push_back(enemy->GetObjectID());
GameMessages::SendPlatformResync(enemy, UNASSIGNED_SYSTEM_ADDRESS);
}
}
}

void SGCannon::EndGameBufferTimerFunc(Entity* self) {
RecordPlayerScore(self);
StopGame(self, false);
}

void SGCannon::OnActivityTimerDone(Entity* self, const std::string& name) {
if (name == SuperChargeTimer && !self->GetVar<bool>(SuperChargePausedVariable)) {
SuperChargeTimerFunc(self);
} else if (name == SpawnWaveTimer) {
SpawnWaveTimerFunc(self);
} else if (name == EndWaveTimer) {
EndWaveTimerFunc(self);
} else if (name == GameOverTimer) {
GameOverTimerFunc(self);
} else if (name.rfind(DoSpawnTimer, 0) == 0) {
DoSpawnTimerFunc(self, name);
} else if (name == EndGameBufferTimer) {
RecordPlayerScore(self);
StopGame(self, false);
EndGameBufferTimerFunc(self);
}
}

Expand Down
6 changes: 6 additions & 0 deletions dScripts/ai/MINIGAME/SG_GF/SERVER/SGCannon.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class SGCannon : public ActivityManager {
void OnActivityTimerDone(Entity* self, const std::string& name) override;
void OnActivityTimerUpdate(Entity* self, const std::string& name, float_t timeRemaining, float_t elapsedTime) override;
void OnRequestActivityExit(Entity* self, LWOOBJID player, bool canceled) override;
void SuperChargeTimerFunc(Entity* self);
void SpawnWaveTimerFunc(Entity* self);
void EndWaveTimerFunc(Entity* self);
void GameOverTimerFunc(Entity* self);
void DoSpawnTimerFunc(Entity* self, const std::string& name);
void EndGameBufferTimerFunc(Entity* self);
private:
static std::vector<std::vector<SGEnemy>> GetWaves();
static SGConstants GetConstants();
Expand Down
Loading