mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-11-30 01:41:40 +00:00
feat(scripts): shared shooting gallery types and arithmetic
A common ShootingGallery folder for the gallery scripts: the zone's constants, the cannon's spawn table and spawn data as the original script lays them out, and its arithmetic (streak bonus and marks, reward model percent, wave scores, random path, spawn times, the summary string) apart from its messages, so other galleries can reuse it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,12 @@ foreach(file ${DSCRIPTS_SOURCES_AI_MINIGAME_SG_GF})
|
||||
set(DSCRIPTS_SOURCES_AI_MINIGAME ${DSCRIPTS_SOURCES_AI_MINIGAME} "SG_GF/${file}")
|
||||
endforeach()
|
||||
|
||||
add_subdirectory(ShootingGallery)
|
||||
|
||||
foreach(file ${DSCRIPTS_SOURCES_AI_MINIGAME_SHOOTING_GALLERY})
|
||||
set(DSCRIPTS_SOURCES_AI_MINIGAME ${DSCRIPTS_SOURCES_AI_MINIGAME} "ShootingGallery/${file}")
|
||||
endforeach()
|
||||
|
||||
add_subdirectory(Objects)
|
||||
|
||||
foreach(file ${DSCRIPTS_SOURCES_AI_MINIGAME_OBJECTS})
|
||||
@@ -13,5 +19,5 @@ foreach(file ${DSCRIPTS_SOURCES_AI_MINIGAME_OBJECTS})
|
||||
endforeach()
|
||||
|
||||
add_library(dScriptsAiMinigame OBJECT ${DSCRIPTS_SOURCES_AI_MINIGAME})
|
||||
target_include_directories(dScriptsAiMinigame PUBLIC "." "Objects" "SG_GF" "SG_GF/SERVER")
|
||||
target_include_directories(dScriptsAiMinigame PUBLIC "." "Objects" "SG_GF" "SG_GF/SERVER" "ShootingGallery")
|
||||
target_precompile_headers(dScriptsAiMinigame REUSE_FROM dScriptsBase)
|
||||
|
||||
3
dScripts/ai/MINIGAME/ShootingGallery/CMakeLists.txt
Normal file
3
dScripts/ai/MINIGAME/ShootingGallery/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
set(DSCRIPTS_SOURCES_AI_MINIGAME_SHOOTING_GALLERY
|
||||
"ShootingGalleryLogic.cpp"
|
||||
PARENT_SCOPE)
|
||||
102
dScripts/ai/MINIGAME/ShootingGallery/ShootingGalleryLogic.cpp
Normal file
102
dScripts/ai/MINIGAME/ShootingGallery/ShootingGalleryLogic.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#include "ShootingGalleryLogic.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <sstream>
|
||||
|
||||
namespace ShootingGallery::Logic {
|
||||
int32_t GetCurrentBonus(int32_t curStreak) {
|
||||
// Cap the streak
|
||||
if (curStreak > 12) curStreak = 12;
|
||||
// intdiv: math.floor(a / b)
|
||||
return static_cast<int32_t>(std::floor(curStreak / 3.0));
|
||||
}
|
||||
|
||||
StreakMark GetStreakMark(const int32_t curStreak) {
|
||||
if (curStreak < 0 || curStreak >= 13) return StreakMark::NONE;
|
||||
// math.mod(curStreak, 3)
|
||||
const auto marks = curStreak % 3;
|
||||
if (marks == 1) return StreakMark::MARK1;
|
||||
if (marks == 2) return StreakMark::MARK2;
|
||||
if (marks == 0 && curStreak > 0) return StreakMark::MARK3;
|
||||
return StreakMark::UNMARK_ALL;
|
||||
}
|
||||
|
||||
int32_t ApplyStreakBonus(const int32_t points, const int32_t curStreak) {
|
||||
if (points < 0) return points;
|
||||
return points + (points * GetCurrentBonus(curStreak));
|
||||
}
|
||||
|
||||
float GetRewardPercent(const int32_t totalScore, const int32_t scoreReward) {
|
||||
const double rewardAmount = scoreReward / 100.0 * 3.0;
|
||||
const double rewardS = totalScore / rewardAmount;
|
||||
// round(rewardS * 3, 0): math.floor(x + 0.5)
|
||||
double rewardF = std::floor(rewardS * 3.0 + 0.5);
|
||||
if (rewardF > 100.0) rewardF = 100.0;
|
||||
return static_cast<float>(rewardF);
|
||||
}
|
||||
|
||||
void RecordPlayerWaveScore(std::vector<int32_t>& playerScore, const int32_t totalScore, const uint32_t waveNum) {
|
||||
if (waveNum == 0) return;
|
||||
if (playerScore.size() < waveNum) playerScore.resize(waveNum, 0);
|
||||
int32_t score = totalScore;
|
||||
// to get the wave score we must subtract prior waves from it
|
||||
for (uint32_t wave = 1; wave < waveNum; wave++) score -= playerScore[wave - 1];
|
||||
playerScore[waveNum - 1] = score;
|
||||
}
|
||||
|
||||
std::optional<std::string> GetRandomPath(const std::vector<std::string>& paths, const std::optional<std::string>& lastPathIn,
|
||||
const std::optional<std::string>& lastRandomPathIn, const std::function<int32_t(int32_t, int32_t)>& random) {
|
||||
const std::string lastRandomPath = lastRandomPathIn.value_or("");
|
||||
const std::string lastPath = lastPathIn.value_or("");
|
||||
|
||||
if (paths.size() > 1) {
|
||||
int32_t newPathTableSize = 0;
|
||||
for (const auto& path : paths) {
|
||||
// if we find the lastPath or lastRandomPath then remove it from the temp table
|
||||
if (path != lastPath || path != lastRandomPath) newPathTableSize++;
|
||||
}
|
||||
// a table is always true in Lua: the pick is from the full list, up to the filtered count
|
||||
return paths.at(random(1, newPathTableSize) - 1);
|
||||
}
|
||||
|
||||
if (paths.empty() || paths.front() == lastRandomPath) return std::nullopt;
|
||||
return paths.front();
|
||||
}
|
||||
|
||||
std::optional<float> GetSpawnTime(const Spawn& spawn, const bool bSpawnNow, const float random01) {
|
||||
if (bSpawnNow) {
|
||||
if (spawn.initSpawnTimeMin > 0 && spawn.initSpawnTimeMax > 0) {
|
||||
float ranSpawnTime = (random01 * (spawn.initSpawnTimeMax - spawn.initSpawnTimeMin)) + spawn.initSpawnTimeMin;
|
||||
if (ranSpawnTime < 1) ranSpawnTime = 1;
|
||||
return ranSpawnTime;
|
||||
}
|
||||
return 1.0f;
|
||||
}
|
||||
if (spawn.bRespawn) {
|
||||
float ranSpawnTime = (random01 * (spawn.maxTime - spawn.minTime)) + spawn.minTime;
|
||||
if (ranSpawnTime < 1) ranSpawnTime = 1;
|
||||
return ranSpawnTime;
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<uint32_t> GetTimerNumber(const std::string& name, const std::string& prefix) {
|
||||
if (!name.starts_with(prefix)) return std::nullopt;
|
||||
const auto rest = name.substr(prefix.size());
|
||||
if (rest.empty() || rest.find_first_not_of("0123456789") != std::string::npos) return std::nullopt;
|
||||
return static_cast<uint32_t>(std::stoul(rest));
|
||||
}
|
||||
|
||||
float GetAccuracy(const int32_t fired, const int32_t misses) {
|
||||
if (fired > 0) return static_cast<float>(fired - misses) / static_cast<float>(fired);
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
std::string GetRewardsString(const int32_t totalScore, const std::vector<int32_t>& playerScore, const int32_t shots, const int32_t kills, const int32_t streak) {
|
||||
std::ostringstream stream;
|
||||
stream << totalScore;
|
||||
for (size_t wave = 0; wave < 3; wave++) stream << "_" << (wave < playerScore.size() ? playerScore[wave] : 0);
|
||||
stream << "_" << shots << "_" << kills << "_" << streak;
|
||||
return stream.str();
|
||||
}
|
||||
};
|
||||
50
dScripts/ai/MINIGAME/ShootingGallery/ShootingGalleryLogic.h
Normal file
50
dScripts/ai/MINIGAME/ShootingGallery/ShootingGalleryLogic.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include "ShootingGalleryTypes.h"
|
||||
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* The arithmetic of sg_cannon.lua, kept apart from its messages so it can be tested: streaks, scores, reward models,
|
||||
* paths and spawn times. Each function names the Lua it comes from.
|
||||
*/
|
||||
namespace ShootingGallery::Logic {
|
||||
// getCurrentBonus: the streak multiplier (streak capped at 12, divided by 3)
|
||||
int32_t GetCurrentBonus(int32_t curStreak);
|
||||
|
||||
// updateStreak: which mark the client shows for a streak (only streaks 0 to 12 show one)
|
||||
enum class StreakMark { NONE, MARK1, MARK2, MARK3, UNMARK_ALL };
|
||||
StreakMark GetStreakMark(int32_t curStreak);
|
||||
|
||||
// registerHit: only points of 0 or more get the streak bonus
|
||||
int32_t ApplyStreakBonus(int32_t points, int32_t curStreak);
|
||||
|
||||
// registerHit: the reward model's percent (rewardF) for a total score, given the matrix's Score_Reward.
|
||||
// Above 100 it is 100; it can be 0 or less.
|
||||
float GetRewardPercent(int32_t totalScore, int32_t scoreReward);
|
||||
|
||||
// RecordPlayerWaveScore: the wave's score is the total less the waves before it (playerScore[0] is wave 1)
|
||||
void RecordPlayerWaveScore(std::vector<int32_t>& playerScore, int32_t totalScore, uint32_t waveNum);
|
||||
|
||||
// GetRandomPath, with Lua's quirks: a path is left out only when it is both the last path and the last random one,
|
||||
// the pick indexes the unfiltered list, and a spawn with one path that was the last random path gets none.
|
||||
// random(1, n) is math.random's whole number from 1 to n.
|
||||
std::optional<std::string> GetRandomPath(const std::vector<std::string>& paths, const std::optional<std::string>& lastPath,
|
||||
const std::optional<std::string>& lastRandomPath, const std::function<int32_t(int32_t, int32_t)>& random);
|
||||
|
||||
// SpawnObject: the spawn timer's time (at least 1 s), or none when it neither spawns now nor respawns. random01 is
|
||||
// math.random(): [0, 1).
|
||||
std::optional<float> GetSpawnTime(const Spawn& spawn, bool bSpawnNow, float random01);
|
||||
|
||||
// The number after a timer's prefix (string.sub(name, len + 1)), when the name starts with it
|
||||
std::optional<uint32_t> GetTimerNumber(const std::string& name, const std::string& prefix);
|
||||
|
||||
// stopGame: the hit share (fired - misses) / fired, 0 with no shots
|
||||
float GetAccuracy(int32_t fired, int32_t misses);
|
||||
|
||||
// showSummaryDialog: UI_Rewards, "<score>_<wave 1>_<wave 2>_<wave 3>_<shots>_<kills>_<streak>"
|
||||
std::string GetRewardsString(int32_t totalScore, const std::vector<int32_t>& playerScore, int32_t shots, int32_t kills, int32_t streak);
|
||||
};
|
||||
97
dScripts/ai/MINIGAME/ShootingGallery/ShootingGalleryTypes.h
Normal file
97
dScripts/ai/MINIGAME/ShootingGallery/ShootingGalleryTypes.h
Normal file
@@ -0,0 +1,97 @@
|
||||
#pragma once
|
||||
|
||||
#include "dCommonVars.h"
|
||||
#include "NiPoint3.h"
|
||||
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* The data a shooting gallery runs on, as the original scripts lay it out: the zone's CONSTANTS table and the
|
||||
* cannon's spawn table (o_ShootingGallery.lua's AddSpawn / AddSpawnsForWave). One gallery's data is a thin layer over
|
||||
* the shared cannon and zone scripts.
|
||||
*/
|
||||
namespace ShootingGallery {
|
||||
// o_ShootingGallery.lua: AddSpawn's row. Lua mutates bRespawn (no path left) and sdLastPath (checkSpawn) on the
|
||||
// table itself, and the table lives as long as the cannon, so a cannon keeps its own copy.
|
||||
struct Spawn {
|
||||
std::vector<std::string> path{};
|
||||
LOT id = LOT_NULL;
|
||||
float initSpawnTimeMin = 0.0f;
|
||||
float initSpawnTimeMax = 0.0f;
|
||||
bool bRespawn = false;
|
||||
float minTime = 0.0f;
|
||||
float maxTime = 0.0f;
|
||||
float speed = 0.0f;
|
||||
int32_t score = 0;
|
||||
bool bChangeSpeed = false;
|
||||
float speedChangeChance = 0.0f;
|
||||
float minSpeed = 0.0f;
|
||||
float maxSpeed = 0.0f;
|
||||
bool bMovingPlatform = false;
|
||||
// AddSpawn's "Despawn On Last Waypoint" (Lua calls it despawnTime)
|
||||
bool despawnTime = false;
|
||||
float timeScore = 0.0f;
|
||||
std::optional<std::string> sdLastPath{};
|
||||
};
|
||||
|
||||
// The spawns of each wave, wave 1 first (AddSpawnsForWave)
|
||||
using SpawnTable = std::vector<std::vector<Spawn>>;
|
||||
|
||||
// sg_cannon.lua SpawnObject: what a spawn timer spawns, stored on the target as its SpawnData
|
||||
struct SpawnData {
|
||||
LOT sdTemplate = LOT_NULL;
|
||||
bool sdRespawn = false;
|
||||
float sdSpeed = 0.0f;
|
||||
int32_t sdScore = 0;
|
||||
// false in Lua when GetRandomPath found none
|
||||
std::optional<std::string> sdPath{};
|
||||
// The spawn's index in its wave (0 based here, Lua's is 1 based)
|
||||
uint32_t sdnum = 0;
|
||||
bool sdChangeSpeed = false;
|
||||
float sdSpeedChance = 0.0f;
|
||||
float sdMinSpeed = 0.0f;
|
||||
float sdMaxSpeed = 0.0f;
|
||||
bool sdMovingPlat = false;
|
||||
bool sdDespawnTime = false;
|
||||
float sdTimeScore = 0.0f;
|
||||
bool bSpawned = false;
|
||||
};
|
||||
|
||||
// The zone script's CONSTANTS (the cannon reads them from the zone control)
|
||||
struct Constants {
|
||||
LOT CANNON_TEMPLATEID = LOT_NULL;
|
||||
uint32_t IMPACT_SKILLID = 0;
|
||||
LOT PROJECTILE_TEMPLATEID = LOT_NULL;
|
||||
NiPoint3 CANNON_PLAYER_OFFSET{};
|
||||
uint32_t Reward_Model_Matrix = 0;
|
||||
float CANNON_VELOCITY = 0.0f;
|
||||
float CANNON_MIN_DISTANCE = 0.0f;
|
||||
float CANNON_REFIRE_RATE = 0.0f;
|
||||
// The muzzle offset (nil when the cannon script does not build it)
|
||||
std::optional<NiPoint3> CANNON_BARREL_OFFSET{};
|
||||
// The super charged cannon ball's LOT: what shot_done names for a super charged shot
|
||||
LOT CANNON_SUPER_CHARGE = LOT_NULL;
|
||||
LOT CANNON_PROJECTILE = LOT_NULL;
|
||||
uint32_t CANNON_SUPERCHARGE_SKILL = 0;
|
||||
uint32_t CANNON_SKILL = 0;
|
||||
float CANNON_TIMEOUT = -1.0f;
|
||||
float CANNON_FOV = 0.0f;
|
||||
bool CANNON_USE_LEADERBOARDS = true;
|
||||
int32_t STREAK_MOD = 0;
|
||||
float ChargedTime = 0.0f;
|
||||
int32_t ChargedPoints = 0;
|
||||
std::string Reward_Model_GrpName{};
|
||||
uint32_t ActivityID = 0;
|
||||
// Score_Reward_1..5 and Score_LootMatrix_1..5
|
||||
std::vector<int32_t> Score_Reward{};
|
||||
std::vector<uint32_t> Score_LootMatrix{};
|
||||
// The waves' time limits (AddWave); NUM_WAVES is their count, the zone's timelimit the first one's
|
||||
std::vector<float> waveTimeLimits{};
|
||||
float FIRST_WAVE_START_TIME = 0.0f;
|
||||
float IN_BETWEEN_WAVE_PAUSE = 0.0f;
|
||||
|
||||
uint32_t NUM_WAVES() const { return static_cast<uint32_t>(waveTimeLimits.size()); }
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user