Files
DarkflameServer/tests/dGameTests/dComponentsTests/SpawnerComponentTests.cpp
Aaron Kimbrell 9a1e74426a feat(spawner): spawn at a node by index, and the node index of a spawned object
For the Lua's SpawnerSpawnNewAtIndex and the spawnNode of SpawnedObjectLoaded (the Fire Temple boss manager moves its
portal between the nodes of its spawner).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 00:01:20 -06:00

81 lines
3.1 KiB
C++

#include "GameDependencies.h"
#include "CDClientManager.h"
#include "CDClientDatabase.h"
#include "CDComponentsRegistryTable.h"
#include "Entity.h"
#include "Spawner.h"
#include "SpawnerComponent.h"
#include "dZoneManager.h"
#include <gtest/gtest.h>
// A spawner object's SpawnerComponent owns its spawner; the zone manager only indexes it.
class SpawnerComponentTests : public GameDependenciesTest {
protected:
void SetUp() override {
SetUpDependencies();
// The entity's own LOT has no components, so it doesn't look anything up in a database
CDClientManager::GetEntriesMutable<CDComponentsRegistryTable>().insert_or_assign(static_cast<uint64_t>(info.lot), 0);
}
void TearDown() override { TearDownDependencies(); }
static SpawnerInfo InactiveSpawner(const LWOOBJID id) {
SpawnerInfo spawnerInfo{};
spawnerInfo.spawnerID = id;
spawnerInfo.activeOnLoad = false;
return spawnerInfo;
}
};
TEST_F(SpawnerComponentTests, TheZoneManagerFindsTheComponentsSpawner) {
constexpr LWOOBJID id = 1234;
auto entity = std::make_unique<Entity>(id, info);
auto* const spawnerComponent = entity->AddComponent<SpawnerComponent>(-1);
auto spawner = std::make_unique<Spawner>(InactiveSpawner(id));
auto* const spawnerPtr = spawner.get();
spawnerComponent->SetSpawner(std::move(spawner));
Game::zoneManager->AddSpawner(id, spawnerPtr);
EXPECT_EQ(spawnerComponent->GetSpawner(), spawnerPtr);
EXPECT_EQ(Game::zoneManager->GetSpawner(id), spawnerPtr);
}
TEST_F(SpawnerComponentTests, TheSpawnerGoesWithItsObject) {
constexpr LWOOBJID id = 1235;
auto entity = std::make_unique<Entity>(id, info);
auto* const spawnerComponent = entity->AddComponent<SpawnerComponent>(-1);
auto spawner = std::make_unique<Spawner>(InactiveSpawner(id));
Game::zoneManager->AddSpawner(id, spawner.get());
spawnerComponent->SetSpawner(std::move(spawner));
entity.reset();
EXPECT_EQ(Game::zoneManager->GetSpawner(id), nullptr);
}
// SpawnerSpawnNewAtIndex: spawns at that node even when the spawner is inactive or full, and the object's node index
// is known (a SpawnedObjectLoaded's spawnNode)
TEST_F(SpawnerComponentTests, SpawnAtIndexSpawnsAtThatNode) {
CDClientDatabase::Connect(":memory:");
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS ComponentsRegistry (id INTEGER, component_type INTEGER, component_id INTEGER);");
CDClientDatabase::ExecuteDML("CREATE TABLE IF NOT EXISTS Objects (id INTEGER, name TEXT, type TEXT, interactionDistance REAL);");
auto spawnerInfo = InactiveSpawner(4321);
spawnerInfo.templateID = info.lot;
spawnerInfo.amountMaintained = 0;
SpawnerNode first;
first.position = NiPoint3(1.0f, 0.0f, 0.0f);
SpawnerNode second;
second.position = NiPoint3(2.0f, 0.0f, 0.0f);
spawnerInfo.nodes = { &first, &second };
Spawner spawner(spawnerInfo);
EXPECT_EQ(spawner.SpawnAtIndex(2), nullptr);
auto* const spawned = spawner.SpawnAtIndex(1);
ASSERT_NE(spawned, nullptr);
// (no physics: the position it was made at)
EXPECT_EQ(spawned->GetDefaultPosition(), second.position);
EXPECT_EQ(spawner.GetNodeIndex(spawned->GetObjectID()), 1);
EXPECT_EQ(spawner.GetNodeIndex(1), -1);
}