diff --git a/tests/dGameTests/CMakeLists.txt b/tests/dGameTests/CMakeLists.txt index 46cd990b2..3ab123ac1 100644 --- a/tests/dGameTests/CMakeLists.txt +++ b/tests/dGameTests/CMakeLists.txt @@ -12,6 +12,7 @@ set(DGAMETEST_SOURCES "StaleSaveGuardTests.cpp" "ContrabandTests.cpp" "KnockbackTests.cpp" + "ShootingGalleryTests.cpp" "CollisionFilterTests.cpp" "MovementBlockerTests.cpp" "ItemSetPassiveAbilityTests.cpp" diff --git a/tests/dGameTests/ShootingGalleryTests.cpp b/tests/dGameTests/ShootingGalleryTests.cpp new file mode 100644 index 000000000..79953f39b --- /dev/null +++ b/tests/dGameTests/ShootingGalleryTests.cpp @@ -0,0 +1,270 @@ +// The shooting gallery scripts (SG_CANNON.lua and the shared code): streaks, scores, reward models, paths, spawn +// times, the wave timers, and the activity timers they run on. + +#include "GameDependencies.h" + +#include "Entity.h" +#include "GameMessages.h" +#include "ActivityManager.h" +#include "ai/MINIGAME/SG_GF/GfShootingGalleryData.h" +#include "ai/MINIGAME/SG_GF/SERVER/SGCannon.h" +#include "ai/MINIGAME/ShootingGallery/ShootingGalleryLogic.h" + +#include + +using namespace ShootingGallery; + +namespace { + // Records the activity timers it is told of + class TimerRecorder : public ActivityManager { + public: + void OnActivityTimerUpdate(Entity* self, const std::string& name, float_t timeRemaining, float_t elapsedTime) override { + updates.push_back(name); + } + void OnActivityTimerDone(Entity* self, const std::string& name) override { done.push_back(name); } + std::vector updates; + std::vector done; + }; + + // A spawn table entry with the fields the spawn time reads + Spawn MakeSpawn(const float initMin, const float initMax, const bool respawn, const float minTime, const float maxTime) { + Spawn spawn; + spawn.initSpawnTimeMin = initMin; + spawn.initSpawnTimeMax = initMax; + spawn.bRespawn = respawn; + spawn.minTime = minTime; + spawn.maxTime = maxTime; + return spawn; + } +}; + +class ShootingGalleryTest : public GameDependenciesTest { +protected: + void SetUp() override { SetUpDependencies(); } + void TearDown() override { TearDownDependencies(); } +}; + +// getCurrentBonus: a third of the streak, the streak capped at 12 +TEST(ShootingGalleryLogicTest, StreakBonus) { + EXPECT_EQ(Logic::GetCurrentBonus(0), 0); + EXPECT_EQ(Logic::GetCurrentBonus(2), 0); + EXPECT_EQ(Logic::GetCurrentBonus(3), 1); + EXPECT_EQ(Logic::GetCurrentBonus(8), 2); + EXPECT_EQ(Logic::GetCurrentBonus(12), 4); + EXPECT_EQ(Logic::GetCurrentBonus(40), 4); + + // registerHit: the bonus only for points of 0 or more + EXPECT_EQ(Logic::ApplyStreakBonus(1500, 0), 1500); + EXPECT_EQ(Logic::ApplyStreakBonus(1500, 3), 3000); + EXPECT_EQ(Logic::ApplyStreakBonus(500, 12), 2500); + EXPECT_EQ(Logic::ApplyStreakBonus(-1000, 9), -1000); +} + +// updateStreak: marks 1 and 2, mark 3 (with the multiplier) on a whole three, none past 12 +TEST(ShootingGalleryLogicTest, StreakMarks) { + EXPECT_EQ(Logic::GetStreakMark(0), Logic::StreakMark::UNMARK_ALL); + EXPECT_EQ(Logic::GetStreakMark(1), Logic::StreakMark::MARK1); + EXPECT_EQ(Logic::GetStreakMark(2), Logic::StreakMark::MARK2); + EXPECT_EQ(Logic::GetStreakMark(3), Logic::StreakMark::MARK3); + EXPECT_EQ(Logic::GetStreakMark(7), Logic::StreakMark::MARK1); + EXPECT_EQ(Logic::GetStreakMark(12), Logic::StreakMark::MARK3); + EXPECT_EQ(Logic::GetStreakMark(13), Logic::StreakMark::NONE); +} + +// registerHit: the reward model's percent, capped at 100 +TEST(ShootingGalleryLogicTest, RewardModelPercent) { + EXPECT_FLOAT_EQ(Logic::GetRewardPercent(25000, 50000), 50.0f); + EXPECT_FLOAT_EQ(Logic::GetRewardPercent(50000, 50000), 100.0f); + EXPECT_FLOAT_EQ(Logic::GetRewardPercent(90000, 50000), 100.0f); + EXPECT_FLOAT_EQ(Logic::GetRewardPercent(50000, 100000), 50.0f); + // round(x, 0) is floor(x + 0.5) + EXPECT_FLOAT_EQ(Logic::GetRewardPercent(1250, 100000), 1.0f); + EXPECT_FLOAT_EQ(Logic::GetRewardPercent(-1000, 50000), -2.0f); +} + +// RecordPlayerWaveScore: each wave keeps the score made in it +TEST(ShootingGalleryLogicTest, WaveScores) { + std::vector playerScore(3, 0); + Logic::RecordPlayerWaveScore(playerScore, 0, 0); + EXPECT_EQ(playerScore, (std::vector{ 0, 0, 0 })); + Logic::RecordPlayerWaveScore(playerScore, 4000, 1); + Logic::RecordPlayerWaveScore(playerScore, 10000, 2); + Logic::RecordPlayerWaveScore(playerScore, 9000, 3); + EXPECT_EQ(playerScore, (std::vector{ 4000, 6000, -1000 })); + + // showSummaryDialog + EXPECT_EQ(Logic::GetRewardsString(9000, playerScore, 20, 12, 5), "9000_4000_6000_-1000_20_12_5"); + EXPECT_EQ(Logic::GetRewardsString(0, {}, 0, 0, 0), "0_0_0_0_0_0_0"); + + // stopGame's hit share + EXPECT_FLOAT_EQ(Logic::GetAccuracy(0, 0), 0.0f); + EXPECT_FLOAT_EQ(Logic::GetAccuracy(20, 5), 0.75f); +} + +// GetRandomPath, with the original's quirks +TEST(ShootingGalleryLogicTest, RandomPath) { + const std::vector two = { "A", "B" }; + int32_t lastHigh = 0; + const auto highest = [&lastHigh](const int32_t low, const int32_t high) { lastHigh = high; return high; }; + + // No path is left out unless it is both the last path and the last random one + EXPECT_EQ(Logic::GetRandomPath(two, "A", "B", highest), "B"); + EXPECT_EQ(lastHigh, 2); + // Both: one less to pick from, and the pick is from the unfiltered list (so the last one, B, is left out) + EXPECT_EQ(Logic::GetRandomPath(two, "B", "B", highest), "A"); + EXPECT_EQ(lastHigh, 1); + + // One path: none when it was the last random path + const std::vector one = { "A" }; + EXPECT_EQ(Logic::GetRandomPath(one, std::nullopt, std::nullopt, highest), "A"); + EXPECT_EQ(Logic::GetRandomPath(one, std::nullopt, "A", highest), std::nullopt); + EXPECT_EQ(Logic::GetRandomPath(one, "A", "B", highest), "A"); +} + +// SpawnObject's timer: at least 1 s; the initial times only when both are above 0 +TEST(ShootingGalleryLogicTest, SpawnTimes) { + const auto ship = MakeSpawn(0.0f, 2.0f, true, 0.0f, 2.0f); + EXPECT_EQ(Logic::GetSpawnTime(ship, true, 0.9f), 1.0f); + EXPECT_EQ(Logic::GetSpawnTime(ship, false, 0.25f), 1.0f); + EXPECT_EQ(Logic::GetSpawnTime(ship, false, 0.75f), 1.5f); + + const auto duck = MakeSpawn(5.0f, 10.0f, true, 5.0f, 10.0f); + EXPECT_EQ(Logic::GetSpawnTime(duck, true, 0.5f), 7.5f); + EXPECT_EQ(Logic::GetSpawnTime(duck, false, 0.0f), 5.0f); + + const auto once = MakeSpawn(0.0f, 0.0f, false, 0.0f, 0.0f); + EXPECT_EQ(Logic::GetSpawnTime(once, true, 0.5f), 1.0f); + EXPECT_EQ(Logic::GetSpawnTime(once, false, 0.5f), std::nullopt); + + EXPECT_EQ(Logic::GetTimerNumber("EndWave2", "EndWave"), 2u); + EXPECT_EQ(Logic::GetTimerNumber("DoSpawn12", "DoSpawn"), 12u); + EXPECT_EQ(Logic::GetTimerNumber("DoSpawn", "DoSpawn"), std::nullopt); + EXPECT_EQ(Logic::GetTimerNumber("SpawnWave1", "EndWave"), std::nullopt); +} + +// GF's data: three waves of 30 s, the super charge cannon ball, the original's table (waves of 5, 8 and 11 spawns) +TEST(ShootingGalleryLogicTest, GfData) { + const auto& constants = GfShootingGalleryData::GetConstants(); + EXPECT_EQ(constants.NUM_WAVES(), 3u); + EXPECT_EQ(constants.waveTimeLimits.front(), 30.0f); + EXPECT_EQ(constants.CANNON_SUPER_CHARGE, 6297); + EXPECT_EQ(constants.Score_Reward.size(), 5u); + EXPECT_EQ(constants.Score_LootMatrix.size(), 5u); + + const auto table = GfShootingGalleryData::GetTargetSettings(); + ASSERT_EQ(table.size(), 3u); + EXPECT_EQ(table[0].size(), 5u); + EXPECT_EQ(table[1].size(), 8u); + EXPECT_EQ(table[2].size(), 11u); + // Wave 1's first sub runs at 10, wave 2's fourth ship scores 1500, wave 3's last friendly takes FShip 3 / 4 + EXPECT_EQ(table[0][2].speed, 10.0f); + EXPECT_EQ(table[1][3].score, 1500); + EXPECT_EQ(table[2][10].path, (std::vector{ "Wave_3_FShip_3", "Wave_3_FShip_4" })); +} + +// Activity timers keep a name with '_' in it, and a timer started again under its name starts over +TEST_F(ShootingGalleryTest, ActivityTimerNames) { + Entity entity(288300744895900200, info); + TimerRecorder script; + + script.ActivityTimerStart(&entity, "Super_Charge_Timer", 1.0f, 2.0f); + script.OnTimerDone(&entity, "ActivityTimer_Super_Charge_Timer"); + EXPECT_EQ(script.updates, (std::vector{ "Super_Charge_Timer" })); + EXPECT_FLOAT_EQ(script.ActivityTimerGetRemainingTime(&entity, "Super_Charge_Timer"), 1.0f); + + // Started over: its time runs from 0 again, one timer + script.ActivityTimerStart(&entity, "Super_Charge_Timer", 1.0f, 2.0f); + EXPECT_FLOAT_EQ(script.ActivityTimerGetRemainingTime(&entity, "Super_Charge_Timer"), 2.0f); + script.OnTimerDone(&entity, "ActivityTimer_Super_Charge_Timer"); + script.OnTimerDone(&entity, "ActivityTimer_Super_Charge_Timer"); + EXPECT_EQ(script.done, (std::vector{ "Super_Charge_Timer" })); + script.OnTimerDone(&entity, "ActivityTimer_Super_Charge_Timer"); + EXPECT_EQ(script.done.size(), 1u); +} + +// The waves: a wave ends (EndWave), the next starts after the pause (SpawnWave); the last wave's +// SpawnWave runs to GameOver with no EndWave +TEST_F(ShootingGalleryTest, WaveTimers) { + Entity cannon(288300744895900201, info); + SGCannon script; + auto& state = script.GetState(&cannon); + state.spawns = script.TargetSettings(); + state.GameStarted = true; + state.ThisWave = 1; + state.TIME_LIMIT = 30.0f; + + script.OnActivityTimerDone(&cannon, "EndWave1"); + EXPECT_EQ(state.ThisWave, 2u); + EXPECT_FALSE(state.WaveStatus); + EXPECT_FLOAT_EQ(script.ActivityTimerGetRemainingTime(&cannon, "SpawnWave2"), 7.0f); + + // A wave that starts spawns each of its targets on a timer, and ends after its time + script.OnActivityTimerDone(&cannon, "SpawnWave2"); + EXPECT_TRUE(state.WaveStatus); + EXPECT_TRUE(state.Started); + EXPECT_EQ(state.SpawnNum, 8u); + EXPECT_FLOAT_EQ(script.ActivityTimerGetRemainingTime(&cannon, "EndWave2"), 30.0f); + // Ships start at 1 s (initial times of 0), the duck at 5 to 10 s + EXPECT_FLOAT_EQ(script.ActivityTimerGetRemainingTime(&cannon, "DoSpawn1"), 1.0f); + const auto duckTime = script.ActivityTimerGetRemainingTime(&cannon, "DoSpawn7"); + EXPECT_GE(duckTime, 5.0f); + EXPECT_LE(duckTime, 10.0f); + + state.ThisWave = 3; + script.OnActivityTimerDone(&cannon, "SpawnWave3"); + EXPECT_FLOAT_EQ(script.ActivityTimerGetRemainingTime(&cannon, "GameOver"), 30.0f); + EXPECT_FLOAT_EQ(script.ActivityTimerGetRemainingTime(&cannon, "EndWave3"), 0.0f); +} + +// onActivityNotify: a shot with no kills is a miss and ends the streak; a super charged one is not +TEST_F(ShootingGalleryTest, MissesAndTheSuperCharge) { + Entity cannon(288300744895900202, info); + SGCannon script; + auto& state = script.GetState(&cannon); + state.m_curStreak = 5; + + GameMessages::ActivityNotify superShot; + superShot.notification.Insert(u"shot_done", 6297); + script.OnActivityNotify(&cannon, superShot); + EXPECT_EQ(state.m_curStreak, 5); + EXPECT_EQ(state.m_misses, 0); + + GameMessages::ActivityNotify shot; + shot.notification.Insert(u"shot_done", 1822); + script.OnActivityNotify(&cannon, shot); + EXPECT_EQ(state.m_curStreak, 0); + EXPECT_EQ(state.m_misses, 1); + + // A friendly hit before the shot's end also ends the streak + state.m_curStreak = 4; + state.friendly_hit = true; + state.CannonBallKills = { 1 }; + script.OnActivityNotify(&cannon, shot); + EXPECT_EQ(state.m_curStreak, 0); + EXPECT_FALSE(state.friendly_hit); + EXPECT_TRUE(state.CannonBallKills.empty()); +} + +// The super charge: paused outside a wave with its time kept, resumed with that time (0 is a time) +TEST_F(ShootingGalleryTest, SuperChargeTimerPausesBetweenWaves) { + Entity cannon(288300744895900203, info); + SGCannon script; + auto& state = script.GetState(&cannon); + state.spawns = script.TargetSettings(); + state.GameStarted = true; + state.ThisWave = 2; + state.TIME_LIMIT = 30.0f; + state.Super_Charge_Paused = true; + state.Current_Super_Charged_Time = 4.0f; + + script.OnActivityTimerDone(&cannon, "SpawnWave2"); + EXPECT_TRUE(state.Super_Charge_Active); + EXPECT_FALSE(state.Super_Charge_Paused); + EXPECT_EQ(state.Current_Super_Charged_Time, 0.0f); + EXPECT_FLOAT_EQ(script.ActivityTimerGetRemainingTime(&cannon, "Super_Charge_Timer"), 4.0f); + + script.OnActivityTimerDone(&cannon, "EndWave2"); + EXPECT_TRUE(state.Super_Charge_Paused); + EXPECT_EQ(state.Current_Super_Charged_Time, 4.0f); + EXPECT_FLOAT_EQ(script.ActivityTimerGetRemainingTime(&cannon, "Super_Charge_Timer"), 0.0f); +}