diff --git a/dZoneManager/Spawner.cpp b/dZoneManager/Spawner.cpp index 5399d21b4..98d916159 100644 --- a/dZoneManager/Spawner.cpp +++ b/dZoneManager/Spawner.cpp @@ -57,9 +57,14 @@ Spawner::Spawner(const SpawnerInfo& info) { for (Spawner* ssSpawner : spawnSmashSpawnersN) { m_SpawnSmashFoundGroup = true; m_SpawnOnSmashID = ssSpawner ? ssSpawner->m_Info.spawnerID : LWOOBJID_EMPTY; + // A spawner that only appears on a smash (no_auto_spawn) spawns when an object of the network it names dies. + // Every such entry in the game's levels names a zone spawner network and has no_auto_spawn but one. + if (!m_Info.noAutoSpawn) { + LOG_DEBUG("Spawner %s (LOT %i) names network %s to spawn on smash but has no no_auto_spawn, not linked", m_Info.name.c_str(), m_EntityInfo.lot, m_Info.spawnOnSmashGroupName.c_str()); + continue; + } ssSpawner->AddSpawnedEntityDieCallback([=, this]() { - // Intentionally left as a non debug log since i have no idea how much stuff this would affect - LOG("WOULD HAVE SPAWNED %i", m_EntityInfo.lot); + Spawn(); }); } } diff --git a/tests/dGameTests/dComponentsTests/CMakeLists.txt b/tests/dGameTests/dComponentsTests/CMakeLists.txt index b120a102c..1274a408d 100644 --- a/tests/dGameTests/dComponentsTests/CMakeLists.txt +++ b/tests/dGameTests/dComponentsTests/CMakeLists.txt @@ -40,6 +40,7 @@ set(DCOMPONENTS_TESTS "UseItemResultTests.cpp" "LootMetricsTests.cpp" "SpawnerComponentTests.cpp" + "SpawnOnSmashTests.cpp" "SkillComponentTests.cpp" ) diff --git a/tests/dGameTests/dComponentsTests/SpawnOnSmashTests.cpp b/tests/dGameTests/dComponentsTests/SpawnOnSmashTests.cpp new file mode 100644 index 000000000..0a277ddf9 --- /dev/null +++ b/tests/dGameTests/dComponentsTests/SpawnOnSmashTests.cpp @@ -0,0 +1,115 @@ +#include "GameDependencies.h" + +#include "CDClientDatabase.h" +#include "CDClientManager.h" +#include "CDComponentsRegistryTable.h" +#include "Entity.h" +#include "EntityManager.h" +#include "Spawner.h" +#include "dZoneManager.h" + +#include + +// A spawner with spawnsGroupOnSmash and spawnNetNameForSpawnGroupOnSmash spawns when an object of the zone spawner +// network it names (a crate) dies. +class SpawnOnSmashTests : public GameDependenciesTest { +protected: + static constexpr LOT CrateLot = 16830; + static constexpr LOT QuickBuildLot = 16834; + static constexpr const char* CrateNetwork = "Room02Smash01"; + + void SetUp() override { + SetUpDependencies(); + // Creating an entity looks its LOT up in the CDClient + CDClientDatabase::Connect(":memory:"); + for (const auto* const table : { + "ComponentsRegistry (id INTEGER, component_type INTEGER, component_id INTEGER)", + "Objects (id INTEGER, name TEXT, type TEXT, interactionDistance REAL)", + }) { + CDClientDatabase::ExecuteDML(std::string("CREATE TABLE IF NOT EXISTS ") + table + ";"); + } + // None of these LOTs has components, so nothing is looked up in a database + auto& registry = CDClientManager::GetEntriesMutable(); + for (const LOT lot : { 176, CrateLot, QuickBuildLot }) registry.insert_or_assign(static_cast(lot), 0); + } + + void TearDown() override { TearDownDependencies(); } + + SpawnerInfo WithOneNode(const LOT lot) { + SpawnerInfo spawnerInfo{}; + spawnerInfo.templateID = lot; + spawnerInfo.nodes.push_back(m_Nodes.emplace_back(std::make_unique()).get()); + return spawnerInfo; + } + + // The zone spawner network, made first as the zone file is loaded before the levels + Spawner* MakeCrateNetwork() { + auto spawnerInfo = WithOneNode(CrateLot); + spawnerInfo.name = CrateNetwork; + spawnerInfo.isNetwork = true; + return Game::zoneManager->GetSpawner(Game::zoneManager->MakeSpawner(spawnerInfo)); + } + + Spawner* MakeQuickBuild(const bool noAutoSpawn) { + auto spawnerInfo = WithOneNode(QuickBuildLot); + spawnerInfo.spawnsOnSmash = true; + spawnerInfo.spawnOnSmashGroupName = CrateNetwork; + spawnerInfo.noAutoSpawn = noAutoSpawn; + spawnerInfo.respawnTime = 20.0f; + return Game::zoneManager->GetSpawner(Game::zoneManager->MakeSpawner(spawnerInfo)); + } + + static void SmashCrate(Spawner& crates) { + auto* const crate = crates.Spawn(); + ASSERT_NE(crate, nullptr); + crates.NotifyOfEntityDeath(crate->GetObjectID()); + } + + std::vector> m_Nodes; +}; + +TEST_F(SpawnOnSmashTests, TheQuickBuildWaitsForTheSmash) { + MakeCrateNetwork(); + auto* const quickBuild = MakeQuickBuild(true); + ASSERT_NE(quickBuild, nullptr); + EXPECT_TRUE(quickBuild->GetIsSpawnSmashGroup()); + + // No timed spawn before the crate is smashed + quickBuild->Update(30.0f); + quickBuild->Update(30.0f); + EXPECT_TRUE(quickBuild->GetSpawnedObjectIDs().empty()); +} + +TEST_F(SpawnOnSmashTests, SmashingTheCrateSpawnsTheQuickBuild) { + auto* const crates = MakeCrateNetwork(); + auto* const quickBuild = MakeQuickBuild(true); + ASSERT_NE(crates, nullptr); + ASSERT_NE(quickBuild, nullptr); + + SmashCrate(*crates); + + const auto spawned = quickBuild->GetSpawnedObjectIDs(); + ASSERT_EQ(spawned.size(), 1); + auto* const entity = Game::entityManager->GetEntity(spawned[0]); + ASSERT_NE(entity, nullptr); + EXPECT_EQ(entity->GetLOT(), QuickBuildLot); +} + +TEST_F(SpawnOnSmashTests, AnotherSmashKeepsOneQuickBuild) { + auto* const crates = MakeCrateNetwork(); + auto* const quickBuild = MakeQuickBuild(true); + + SmashCrate(*crates); + SmashCrate(*crates); + + EXPECT_EQ(quickBuild->GetSpawnedObjectIDs().size(), 1); +} + +TEST_F(SpawnOnSmashTests, WithoutNoAutoSpawnTheSmashSpawnsNothing) { + auto* const crates = MakeCrateNetwork(); + auto* const quickBuild = MakeQuickBuild(false); + + SmashCrate(*crates); + + EXPECT_TRUE(quickBuild->GetSpawnedObjectIDs().empty()); +}