refactor(Core/Combat): Port TrinityCore heap-based threat system (#24715)

Co-authored-by: blinkysc <blinkysc@users.noreply.github.com>
Co-authored-by: Treeston <treeston.mmoc@gmail.com>
Co-authored-by: killerwife <killerwife@gmail.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
blinkysc
2026-03-18 13:36:59 -05:00
committed by GitHub
parent c80a0f1fad
commit 984baa92dd
101 changed files with 7045 additions and 2659 deletions
+141
View File
@@ -0,0 +1,141 @@
/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TestCreature.h"
#include "ThreatManager.h"
#include "CombatManager.h"
// Heap-allocated to avoid static init calling CreatureMovementData()
// which requires sWorld to be set up (calls getIntConfig).
CreatureTemplate* TestCreature::_fakeCreatureTemplate = nullptr;
bool TestCreature::_fakeTemplateInitialized = false;
TestCreature::TestCreature() : Creature()
{
}
TestCreature::~TestCreature()
{
CleanupCombatState();
}
void TestCreature::CleanupCombatState()
{
m_threatManager.ClearAllThreat();
m_combatManager.EndAllCombat();
// Must remove from world before destruction or ~Object will ABORT
SetInWorld(false);
}
void TestCreature::ForceInitValues(ObjectGuid::LowType guidLow, uint32 entry)
{
Object::_Create(guidLow, entry, HighGuid::Unit);
m_objectType |= TYPEMASK_UNIT;
m_objectTypeId = TYPEID_UNIT;
m_originalEntry = entry;
// Initialize the fake creature template once with safe defaults.
// Heap-allocated because CreatureMovementData() constructor needs sWorld.
if (!_fakeTemplateInitialized)
{
_fakeCreatureTemplate = new CreatureTemplate();
_fakeCreatureTemplate->Entry = 0;
_fakeCreatureTemplate->faction = 14; // hostile monster default
_fakeCreatureTemplate->unit_class = 1; // CLASS_WARRIOR
_fakeCreatureTemplate->speed_walk = 1.0f;
_fakeCreatureTemplate->speed_run = 1.14286f;
_fakeCreatureTemplate->speed_swim = 1.0f;
_fakeCreatureTemplate->speed_flight = 1.0f;
_fakeCreatureTemplate->scale = 1.0f;
_fakeCreatureTemplate->DamageModifier = 1.0f;
_fakeCreatureTemplate->BaseAttackTime = 2000;
_fakeCreatureTemplate->RangeAttackTime = 2000;
_fakeCreatureTemplate->BaseVariance = 1.0f;
_fakeCreatureTemplate->RangeVariance = 1.0f;
_fakeCreatureTemplate->ModHealth = 1.0f;
_fakeCreatureTemplate->ModMana = 1.0f;
_fakeCreatureTemplate->ModArmor = 1.0f;
_fakeCreatureTemplate->ModExperience = 1.0f;
_fakeCreatureTemplate->HoverHeight = 1.0f;
_fakeCreatureTemplate->detection_range = 20.0f;
_fakeCreatureTemplate->flags_extra = 0;
_fakeCreatureTemplate->unit_flags = 0;
_fakeCreatureTemplate->unit_flags2 = 0;
_fakeCreatureTemplate->dynamicflags = 0;
_fakeCreatureTemplate->type = 0;
_fakeCreatureTemplate->type_flags = 0;
// Movement is default-constructed by new CreatureTemplate():
// Ground=Run, Swim=true -> CanWalk() and CanSwim() return true
_fakeTemplateInitialized = true;
}
m_creatureInfo = _fakeCreatureTemplate;
}
void TestCreature::SetTestMap(Map* map)
{
_testMap = map;
// Also set the base class map pointer so GetMap() works
// through the Unit* base pointer (polymorphic calls)
WorldObject::SetMap(map);
}
void TestCreature::SetAlive(bool alive)
{
m_deathState = alive ? DeathState::Alive : DeathState::Dead;
}
void TestCreature::SetInWorld(bool inWorld)
{
if (inWorld && !Object::IsInWorld())
Object::AddToWorld();
else if (!inWorld && Object::IsInWorld())
Object::RemoveFromWorld();
}
void TestCreature::SetPhase(uint32 phase)
{
SetPhaseMask(phase, false);
}
void TestCreature::SetFaction(uint32 faction)
{
// Set faction directly, bypassing Unit::SetFaction which calls
// UpdateMoveInLineOfSightState() -> sObjectMgr->GetCreatureTemplate()
SetUInt32Value(UNIT_FIELD_FACTIONTEMPLATE, faction);
}
void TestCreature::SetupForCombatTest(Map* map, ObjectGuid::LowType guidLow, uint32 entry)
{
ForceInitValues(guidLow, entry);
// SetTestMap calls WorldObject::SetMap which asserts !IsInWorld(),
// so we must set map BEFORE SetInWorld
SetTestMap(map);
SetInWorld(true);
SetAlive(true);
SetPhase(1);
SetHostileFaction();
SetIsCombatDisallowed(false);
ClearUnitState(UNIT_STATE_EVADE);
ClearUnitState(UNIT_STATE_IN_FLIGHT);
InitializeThreatManager();
}
void TestCreature::InitializeThreatManager()
{
m_threatManager.Initialize();
}
+87
View File
@@ -0,0 +1,87 @@
/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TEST_CREATURE_H
#define TEST_CREATURE_H
#include "Creature.h"
#include "CreatureData.h"
#include "ObjectGuid.h"
class TestMap;
/**
* TestCreature - A test harness for Creature that bypasses database dependencies.
*
* Usage:
* TestCreature* creature = new TestCreature();
* creature->ForceInitValues(1, 12345); // guidLow, entry
* creature->SetTestMap(testMap);
* creature->SetAlive(true);
* creature->SetupForCombatTest(); // Sets up all necessary state for combat/threat tests
*/
class TestCreature : public Creature
{
public:
TestCreature();
~TestCreature() override;
// Override methods that require database/world access
void UpdateObjectVisibility(bool /*forced*/ = true, bool /*fromUpdate*/ = false) override { }
void AddToWorld() override { }
void RemoveFromWorld() override { }
// Force initialization without database
void ForceInitValues(ObjectGuid::LowType guidLow, uint32 entry);
// Test control methods
void SetTestMap(Map* map);
// Set alive state (affects m_deathState)
void SetAlive(bool alive);
// Set in-world state (affects m_inWorld)
void SetInWorld(bool inWorld);
// Set phase mask for phase checks
void SetPhase(uint32 phase);
// Set faction for friendliness checks
// Use hostile factions (14 = hostile monster) for combat tests
void SetHostileFaction() { SetFaction(14); }
void SetFriendlyFaction() { SetFaction(35); }
void SetFaction(uint32 faction);
// Complete setup for combat/threat testing
// Sets creature to be alive, in-world, hostile, and initializes managers
void SetupForCombatTest(Map* map, ObjectGuid::LowType guidLow, uint32 entry);
// Initialize ThreatManager for testing
void InitializeThreatManager();
// Access managers directly for testing
ThreatManager& TestGetThreatMgr() { return m_threatManager; }
CombatManager& TestGetCombatMgr() { return m_combatManager; }
// Clear all combat state for cleanup
void CleanupCombatState();
private:
Map* _testMap = nullptr;
static CreatureTemplate* _fakeCreatureTemplate;
static bool _fakeTemplateInitialized;
};
#endif // TEST_CREATURE_H
+62
View File
@@ -0,0 +1,62 @@
/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TestMap.h"
#include "DBCStores.h"
#include "ScriptMgr.h"
#include "ScriptDefines/AllMapScript.h"
#include "ScriptDefines/GlobalScript.h"
#include "ScriptDefines/MiscScript.h"
#include "ScriptDefines/UnitScript.h"
#include "ScriptDefines/WorldObjectScript.h"
TestMap::TestMap()
: Map(0, 0, REGULAR_DIFFICULTY, nullptr)
{
_fakeMapEntry = {};
_fakeMapEntry.map_type = MAP_COMMON;
_fakeMapEntry.MapID = 0;
const_cast<MapEntry const*&>(i_mapEntry) = &_fakeMapEntry;
}
TestMap::~TestMap()
{
}
/*static*/ void TestMap::EnsureDBC()
{
static bool initialized = false;
if (initialized)
return;
initialized = true;
// Insert a fake MapEntry so Map constructor doesn't crash
if (!sMapStore.LookupEntry(0))
{
auto* entry = new MapEntry{};
entry->MapID = 0;
entry->map_type = MAP_COMMON;
entry->entrance_map = -1;
sMapStore.SetEntry(0, entry);
}
// Initialize all script registries so CALL_ENABLED_HOOKS doesn't
// crash on uninitialized vectors during Object/Unit/Map operations
ScriptRegistry<AllMapScript>::InitEnabledHooksIfNeeded(ALLMAPHOOK_END);
ScriptRegistry<GlobalScript>::InitEnabledHooksIfNeeded(GLOBALHOOK_END);
ScriptRegistry<MiscScript>::InitEnabledHooksIfNeeded(MISCHOOK_END);
ScriptRegistry<UnitScript>::InitEnabledHooksIfNeeded(UNITHOOK_END);
ScriptRegistry<WorldObjectScript>::InitEnabledHooksIfNeeded(WORLDOBJECTHOOK_END);
}
+51
View File
@@ -0,0 +1,51 @@
/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TEST_MAP_H
#define TEST_MAP_H
#include "Map.h"
#include "DBCStructure.h"
/**
* TestMap - A minimal test harness for Map.
*
* Since Map has a complex constructor requiring MapEntry from DBC,
* this creates a fake MapEntry to control IsRaid()/IsDungeon() behavior.
*
* Usage:
* TestMap* map = new TestMap();
* map->SetIsRaid(true); // Make this a raid map
*/
class TestMap : public Map
{
public:
TestMap();
~TestMap() override;
// Must be called before constructing TestMap to insert fake DBC entry
// and initialize script registries
static void EnsureDBC();
// Control map type for testing
void SetMapType(uint32 type) { _fakeMapEntry.map_type = type; }
void SetIsRaid(bool val) { _fakeMapEntry.map_type = val ? MAP_RAID : MAP_COMMON; }
void SetIsDungeon(bool val) { _fakeMapEntry.map_type = val ? MAP_INSTANCE : MAP_COMMON; }
private:
MapEntry _fakeMapEntry;
};
#endif // TEST_MAP_H
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff