Core/Phasing: Implemented db spawns in personal phases

Port From (https://github.com/TrinityCore/TrinityCore/commit/8fabe5a3aacf7797f03d074ab8434f445be64955)
This commit is contained in:
hondacrx
2022-01-27 14:01:49 -05:00
parent 6c0e21b4de
commit 7531735669
18 changed files with 555 additions and 123 deletions
+99 -38
View File
@@ -22,16 +22,71 @@ using System.Collections.Generic;
namespace Game.Maps
{
class ObjectGridLoader : Notifier
class ObjectGridLoaderBase : Notifier
{
public ObjectGridLoader(Grid grid, Map map, Cell cell)
internal Cell i_cell;
internal Grid i_grid;
internal Map i_map;
internal uint i_gameObjects;
internal uint i_creatures;
internal uint i_corpses;
internal uint i_areaTriggers;
public ObjectGridLoaderBase(Grid grid, Map map, Cell cell)
{
i_cell = new Cell(cell);
i_grid = grid;
i_map = map;
}
public uint GetLoadedCreatures() { return i_creatures; }
public uint GetLoadedGameObjects() { return i_gameObjects; }
public uint GetLoadedCorpses() { return i_corpses; }
public uint GetLoadedAreaTriggers() { return i_areaTriggers; }
internal void LoadHelper<T>(SortedSet<ulong> guid_set, CellCoord cell, ref uint count, Map map, uint phaseId = 0, ObjectGuid? phaseOwner = null) where T : WorldObject, new()
{
foreach (var guid in guid_set)
{
// Don't spawn at all if there's a respawn timer
if (!map.ShouldBeSpawnedOnGridLoad<T>(guid))
continue;
T obj = new();
if (!obj.LoadFromDB(guid, map, false, phaseOwner.HasValue /*allowDuplicate*/))
{
obj.Dispose();
continue;
}
if (phaseOwner.HasValue)
{
PhasingHandler.InitDbPersonalOwnership(obj.GetPhaseShift(), phaseOwner.Value);
map.GetMultiPersonalPhaseTracker().RegisterTrackedObject(phaseId, phaseOwner.Value, obj);
}
AddObjectHelper(cell, ref count, map, obj);
}
}
void AddObjectHelper<T>(CellCoord cellCoord, ref uint count, Map map, T obj) where T : WorldObject
{
var cell = new Cell(cellCoord);
map.AddToGrid(obj, cell);
obj.AddToWorld();
if (obj.IsCreature())
if (obj.IsActiveObject())
map.AddToActive(obj);
++count;
}
}
class ObjectGridLoader : ObjectGridLoaderBase
{
public ObjectGridLoader(Grid grid, Map map, Cell cell) : base(grid, map, cell) { }
public void LoadN()
{
i_creatures = 0;
@@ -53,7 +108,7 @@ namespace Game.Maps
i_grid.VisitGrid(x, y, visitor);
}
}
Log.outDebug(LogFilter.Maps, "{0} GameObjects, {1} Creatures, and {2} Corpses/Bones loaded for grid {3} on map {4}", i_gameObjects, i_creatures, i_corpses, i_grid.GetGridId(), i_map.GetId());
Log.outDebug(LogFilter.Maps, $"{i_gameObjects} GameObjects, {i_creatures} Creatures, {i_areaTriggers} AreaTrriggers and {i_corpses} Corpses/Bones loaded for grid {i_grid.GetGridId()} on map {i_map.GetId()}");
}
public override void Visit(IList<GameObject> objs)
@@ -85,51 +140,57 @@ namespace Game.Maps
LoadHelper<AreaTrigger>(areaTriggers, cellCoord, ref i_areaTriggers, i_map);
}
}
void LoadHelper<T>(SortedSet<ulong> guid_set, CellCoord cell, ref uint count, Map map) where T : WorldObject, new()
class PersonalPhaseGridLoader : ObjectGridLoaderBase
{
uint _phaseId;
ObjectGuid _phaseOwner;
public PersonalPhaseGridLoader(Grid grid, Map map, Cell cell, ObjectGuid phaseOwner) : base(grid, map, cell)
{
foreach (var guid in guid_set)
_phaseId = 0;
_phaseOwner = phaseOwner;
}
public override void Visit(IList<GameObject> objs)
{
CellCoord cellCoord = i_cell.GetCellCoord();
CellObjectGuids cell_guids = Global.ObjectMgr.GetCellPersonalObjectGuids(i_map.GetId(), i_map.GetDifficultyID(), _phaseId, cellCoord.GetId());
if (cell_guids != null)
LoadHelper<GameObject>(cell_guids.gameobjects, cellCoord, ref i_gameObjects, i_map, _phaseId, _phaseOwner);
}
public override void Visit(IList<Creature> objs)
{
CellCoord cellCoord = i_cell.GetCellCoord();
CellObjectGuids cell_guids = Global.ObjectMgr.GetCellPersonalObjectGuids(i_map.GetId(), i_map.GetDifficultyID(), _phaseId, cellCoord.GetId());
if (cell_guids != null)
LoadHelper<Creature>(cell_guids.creatures, cellCoord, ref i_creatures, i_map, _phaseId, _phaseOwner);
}
public void Load(uint phaseId)
{
_phaseId = phaseId;
i_cell.data.cell_y = 0;
for (uint x = 0; x < MapConst.MaxCells; ++x)
{
// Don't spawn at all if there's a respawn timer
if (!map.ShouldBeSpawnedOnGridLoad<T>(guid))
continue;
T obj = new();
if (!obj.LoadFromDB(guid, map, false, false))
i_cell.data.cell_x = x;
for (uint y = 0; y < MapConst.MaxCells; ++y)
{
obj.Dispose();
continue;
}
i_cell.data.cell_y = y;
AddObjectHelper(cell, ref count, map, obj);
//Load creatures and game objects
var visitor = new Visitor(this, GridMapTypeMask.AllGrid);
i_grid.VisitGrid(x, y, visitor);
}
}
}
void AddObjectHelper<T>(CellCoord cellCoord, ref uint count, Map map, T obj) where T : WorldObject
{
var cell = new Cell(cellCoord);
map.AddToGrid(obj, cell);
obj.AddToWorld();
if (obj.IsCreature())
if (obj.IsActiveObject())
map.AddToActive(obj);
++count;
}
public Cell i_cell;
public Grid i_grid;
public Map i_map;
uint i_gameObjects;
uint i_creatures;
public uint i_corpses;
uint i_areaTriggers;
}
class ObjectWorldLoader : Notifier
{
public ObjectWorldLoader(ObjectGridLoader gloader)
public ObjectWorldLoader(ObjectGridLoaderBase gloader)
{
i_cell = gloader.i_cell;
i_map = gloader.i_map;