Skip to content

Commit

Permalink
fix(Core/Grids): Grid improvements (azerothcore#20955)
Browse files Browse the repository at this point in the history
  • Loading branch information
Takenbacon authored Feb 14, 2025
1 parent 10183ef commit ee69a56
Show file tree
Hide file tree
Showing 67 changed files with 1,952 additions and 2,023 deletions.
1 change: 1 addition & 0 deletions src/common/Collision/Maps/MapDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "DetourNavMesh.h"

#define MAX_NUMBER_OF_GRIDS 64
#define MAX_NUMBER_OF_CELLS 8
#define SIZE_OF_GRIDS 533.3333f

#define MMAP_MAGIC 0x4d4d4150 // 'MMAP'
Expand Down
5 changes: 0 additions & 5 deletions src/server/game/Achievements/AchievementMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,11 +751,6 @@ void AchievementMgr::SendAchievementEarned(AchievementEntry const* achievement)
// if player is in world he can tell his friends about new achievement
else if (GetPlayer()->IsInWorld())
{
CellCoord p = Acore::ComputeCellCoord(GetPlayer()->GetPositionX(), GetPlayer()->GetPositionY());

Cell cell(p);
cell.SetNoCreate();

Acore::BroadcastTextBuilder _builder(GetPlayer(), CHAT_MSG_ACHIEVEMENT, BROADCAST_TEXT_ACHIEVEMENT_EARNED, GetPlayer()->getGender(), GetPlayer(), achievement->ID);
Acore::LocalizedPacketDo<Acore::BroadcastTextBuilder> _localizer(_builder);
Acore::PlayerDistWorker<Acore::LocalizedPacketDo<Acore::BroadcastTextBuilder>> _worker(GetPlayer(), sWorld->getFloatConfig(CONFIG_LISTEN_RANGE_SAY), _localizer);
Expand Down
1 change: 1 addition & 0 deletions src/server/game/Entities/Creature/CreatureGroups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "MoveSplineInit.h"
#include "ObjectMgr.h"
#include "QueryResult.h"
#include "Timer.h"
#include "WaypointMgr.h"

FormationMgr::~FormationMgr()
Expand Down
1 change: 1 addition & 0 deletions src/server/game/Entities/Item/ItemEnchantmentMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Log.h"
#include "ObjectMgr.h"
#include "QueryResult.h"
#include "Timer.h"
#include "Util.h"
#include <cmath>
#include <functional>
Expand Down
11 changes: 1 addition & 10 deletions src/server/game/Entities/Object/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3098,18 +3098,9 @@ void WorldObject::BuildUpdate(UpdateDataMapType& data_map, UpdatePlayerSet& play

void WorldObject::GetCreaturesWithEntryInRange(std::list<Creature*>& creatureList, float radius, uint32 entry)
{
CellCoord pair(Acore::ComputeCellCoord(this->GetPositionX(), this->GetPositionY()));
Cell cell(pair);
cell.SetNoCreate();

Acore::AllCreaturesOfEntryInRange check(this, entry, radius);
Acore::CreatureListSearcher<Acore::AllCreaturesOfEntryInRange> searcher(this, creatureList, check);

TypeContainerVisitor<Acore::CreatureListSearcher<Acore::AllCreaturesOfEntryInRange>, WorldTypeMapContainer> world_visitor(searcher);
cell.Visit(pair, world_visitor, *(this->GetMap()), *this, radius);

TypeContainerVisitor<Acore::CreatureListSearcher<Acore::AllCreaturesOfEntryInRange>, GridTypeMapContainer> grid_visitor(searcher);
cell.Visit(pair, grid_visitor, *(this->GetMap()), *this, radius);
Cell::VisitAllObjects(this, searcher, radius);
}

void WorldObject::AddToObjectUpdate()
Expand Down
1 change: 1 addition & 0 deletions src/server/game/Entities/Transport/Transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define TRANSPORTS_H

#include "GameObject.h"
#include "Timer.h"
#include "TransportMgr.h"
#include "VehicleDefines.h"
#include "ZoneScript.h"
Expand Down
2 changes: 2 additions & 0 deletions src/server/game/Entities/Unit/Unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20535,6 +20535,8 @@ void Unit::ExecuteDelayedUnitRelocationEvent()
}
}

GetMap()->LoadGridsInRange(*player, MAX_VISIBILITY_DISTANCE);

Acore::PlayerRelocationNotifier relocateNoLarge(*player, false); // visit only objects which are not large; default distance
Cell::VisitAllObjects(viewPoint, relocateNoLarge, player->GetSightRange() + VISIBILITY_INC_FOR_GOBJECTS);
relocateNoLarge.SendToSelf();
Expand Down
18 changes: 9 additions & 9 deletions src/server/game/Globals/ObjectMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2341,8 +2341,8 @@ void ObjectMgr::AddCreatureToGrid(ObjectGuid::LowType guid, CreatureData const*
{
if (mask & 1)
{
CellCoord cellCoord = Acore::ComputeCellCoord(data->posX, data->posY);
CellObjectGuids& cell_guids = _mapObjectGuidsStore[MAKE_PAIR32(data->mapid, i)][cellCoord.GetId()];
GridCoord gridCoord = Acore::ComputeGridCoord(data->posX, data->posY);
CellObjectGuids& cell_guids = _mapObjectGuidsStore[MAKE_PAIR32(data->mapid, i)][gridCoord.GetId()];
cell_guids.creatures.insert(guid);
}
}
Expand All @@ -2355,8 +2355,8 @@ void ObjectMgr::RemoveCreatureFromGrid(ObjectGuid::LowType guid, CreatureData co
{
if (mask & 1)
{
CellCoord cellCoord = Acore::ComputeCellCoord(data->posX, data->posY);
CellObjectGuids& cell_guids = _mapObjectGuidsStore[MAKE_PAIR32(data->mapid, i)][cellCoord.GetId()];
GridCoord gridCoord = Acore::ComputeGridCoord(data->posX, data->posY);
CellObjectGuids& cell_guids = _mapObjectGuidsStore[MAKE_PAIR32(data->mapid, i)][gridCoord.GetId()];
cell_guids.creatures.erase(guid);
}
}
Expand Down Expand Up @@ -2454,7 +2454,7 @@ uint32 ObjectMgr::AddCreData(uint32 entry, uint32 mapId, float x, float y, float
AddCreatureToGrid(spawnId, &data);

// Spawn if necessary (loaded grids only)
if (!map->Instanceable() && !map->IsRemovalGrid(x, y))
if (!map->Instanceable() && !map->IsGridCreated(x, y))
{
Creature* creature = new Creature();
if (!creature->LoadCreatureFromDB(spawnId, map, true, true))
Expand Down Expand Up @@ -2644,8 +2644,8 @@ void ObjectMgr::AddGameobjectToGrid(ObjectGuid::LowType guid, GameObjectData con
{
if (mask & 1)
{
CellCoord cellCoord = Acore::ComputeCellCoord(data->posX, data->posY);
CellObjectGuids& cell_guids = _mapObjectGuidsStore[MAKE_PAIR32(data->mapid, i)][cellCoord.GetId()];
GridCoord gridCoord = Acore::ComputeGridCoord(data->posX, data->posY);
CellObjectGuids& cell_guids = _mapObjectGuidsStore[MAKE_PAIR32(data->mapid, i)][gridCoord.GetId()];
cell_guids.gameobjects.insert(guid);
}
}
Expand All @@ -2658,8 +2658,8 @@ void ObjectMgr::RemoveGameobjectFromGrid(ObjectGuid::LowType guid, GameObjectDat
{
if (mask & 1)
{
CellCoord cellCoord = Acore::ComputeCellCoord(data->posX, data->posY);
CellObjectGuids& cell_guids = _mapObjectGuidsStore[MAKE_PAIR32(data->mapid, i)][cellCoord.GetId()];
GridCoord gridCoord = Acore::ComputeGridCoord(data->posX, data->posY);
CellObjectGuids& cell_guids = _mapObjectGuidsStore[MAKE_PAIR32(data->mapid, i)][gridCoord.GetId()];
cell_guids.gameobjects.erase(guid);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/game/Globals/ObjectMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -1150,12 +1150,12 @@ class ObjectMgr
return nullptr;
}

CellObjectGuids const& GetCellObjectGuids(uint16 mapid, uint8 spawnMode, uint32 cell_id)
CellObjectGuids const& GetGridObjectGuids(uint16 mapid, uint8 spawnMode, uint32 gridId)
{
MapObjectGuids::const_iterator itr1 = _mapObjectGuidsStore.find(MAKE_PAIR32(mapid, spawnMode));
if (itr1 != _mapObjectGuidsStore.end())
{
CellObjectGuidsMap::const_iterator itr2 = itr1->second.find(cell_id);
CellObjectGuidsMap::const_iterator itr2 = itr1->second.find(gridId);
if (itr2 != itr1->second.end())
return itr2->second;
}
Expand Down
24 changes: 10 additions & 14 deletions src/server/game/Grids/Cells/Cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ struct Cell
[[nodiscard]] uint32 CellY() const { return data.Part.cell_y; }
[[nodiscard]] uint32 GridX() const { return data.Part.grid_x; }
[[nodiscard]] uint32 GridY() const { return data.Part.grid_y; }
[[nodiscard]] bool NoCreate() const { return data.Part.nocreate; }
void SetNoCreate() { data.Part.nocreate = 1; }

[[nodiscard]] CellCoord GetCellCoord() const
{
Expand All @@ -92,12 +90,10 @@ struct Cell
{
struct
{
unsigned grid_x : 6;
unsigned grid_y : 6;
unsigned cell_x : 6;
unsigned cell_y : 6;
unsigned nocreate : 1;
unsigned reserved : 7;
unsigned grid_x : 8;
unsigned grid_y : 8;
unsigned cell_x : 8;
unsigned cell_y : 8;
} Part;
uint32 All;
} data;
Expand All @@ -107,13 +103,13 @@ struct Cell

static CellArea CalculateCellArea(float x, float y, float radius);

template<class T> static void VisitGridObjects(WorldObject const* obj, T& visitor, float radius, bool dont_load = true);
template<class T> static void VisitWorldObjects(WorldObject const* obj, T& visitor, float radius, bool dont_load = true);
template<class T> static void VisitAllObjects(WorldObject const* obj, T& visitor, float radius, bool dont_load = true);
template<class T> static void VisitGridObjects(WorldObject const* obj, T& visitor, float radius);
template<class T> static void VisitWorldObjects(WorldObject const* obj, T& visitor, float radius);
template<class T> static void VisitAllObjects(WorldObject const* obj, T& visitor, float radius);

template<class T> static void VisitGridObjects(float x, float y, Map* map, T& visitor, float radius, bool dont_load = true);
template<class T> static void VisitWorldObjects(float x, float y, Map* map, T& visitor, float radius, bool dont_load = true);
template<class T> static void VisitAllObjects(float x, float y, Map* map, T& visitor, float radius, bool dont_load = true);
template<class T> static void VisitGridObjects(float x, float y, Map* map, T& visitor, float radius);
template<class T> static void VisitWorldObjects(float x, float y, Map* map, T& visitor, float radius);
template<class T> static void VisitAllObjects(float x, float y, Map* map, T& visitor, float radius);

private:
template<class T, class CONTAINER> void VisitCircle(TypeContainerVisitor<T, CONTAINER>&, Map&, CellCoord const&, CellCoord const&) const;
Expand Down
59 changes: 10 additions & 49 deletions src/server/game/Grids/Cells/CellImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ inline Cell::Cell(CellCoord const& p)
data.Part.grid_y = p.y_coord / MAX_NUMBER_OF_CELLS;
data.Part.cell_x = p.x_coord % MAX_NUMBER_OF_CELLS;
data.Part.cell_y = p.y_coord % MAX_NUMBER_OF_CELLS;
data.Part.nocreate = 0;
data.Part.reserved = 0;
}

inline Cell::Cell(float x, float y)
Expand All @@ -39,8 +37,6 @@ inline Cell::Cell(float x, float y)
data.Part.grid_y = p.y_coord / MAX_NUMBER_OF_CELLS;
data.Part.cell_x = p.x_coord % MAX_NUMBER_OF_CELLS;
data.Part.cell_y = p.y_coord % MAX_NUMBER_OF_CELLS;
data.Part.nocreate = 0;
data.Part.reserved = 0;
}

inline CellArea Cell::CalculateCellArea(float x, float y, float radius)
Expand All @@ -66,7 +62,8 @@ inline void Cell::Visit(CellCoord const& standing_cell, TypeContainerVisitor<T,
}

template<class T, class CONTAINER>
inline void Cell::Visit(CellCoord const& standing_cell, TypeContainerVisitor<T, CONTAINER>& visitor, Map& map, float x_off, float y_off, float radius) const{
inline void Cell::Visit(CellCoord const& standing_cell, TypeContainerVisitor<T, CONTAINER>& visitor, Map& map, float x_off, float y_off, float radius) const
{
if (!standing_cell.IsCoordValid())
return;

Expand Down Expand Up @@ -101,23 +98,14 @@ inline void Cell::Visit(CellCoord const& standing_cell, TypeContainerVisitor<T,
return;
}

//ALWAYS visit standing cell first!!! Since we deal with small radiuses
//it is very essential to call visitor for standing cell firstly...
map.Visit(*this, visitor);

// loop the cell range
for (uint32 x = area.low_bound.x_coord; x <= area.high_bound.x_coord; ++x)
{
for (uint32 y = area.low_bound.y_coord; y <= area.high_bound.y_coord; ++y)
{
CellCoord cellCoord(x, y);
//lets skip standing cell since we already visited it
if (cellCoord != standing_cell)
{
Cell r_zone(cellCoord);
r_zone.data.Part.nocreate = this->data.Part.nocreate;
map.Visit(r_zone, visitor);
}
Cell r_zone(cellCoord);
map.Visit(r_zone, visitor);
}
}
}
Expand All @@ -138,7 +126,6 @@ inline void Cell::VisitCircle(TypeContainerVisitor<T, CONTAINER>& visitor, Map&
{
CellCoord cellCoord(x, y);
Cell r_zone(cellCoord);
r_zone.data.Part.nocreate = this->data.Part.nocreate;
map.Visit(r_zone, visitor);
}
}
Expand All @@ -162,55 +149,41 @@ inline void Cell::VisitCircle(TypeContainerVisitor<T, CONTAINER>& visitor, Map&
//e.g. filling 2 trapezoids after filling central cell strip...
CellCoord cellCoord_left(x_start - step, y);
Cell r_zone_left(cellCoord_left);
r_zone_left.data.Part.nocreate = this->data.Part.nocreate;
map.Visit(r_zone_left, visitor);

//right trapezoid cell visit
CellCoord cellCoord_right(x_end + step, y);
Cell r_zone_right(cellCoord_right);
r_zone_right.data.Part.nocreate = this->data.Part.nocreate;
map.Visit(r_zone_right, visitor);
}
}
}

template<class T>
inline void Cell::VisitGridObjects(WorldObject const* center_obj, T& visitor, float radius, bool dont_load /*= true*/)
inline void Cell::VisitGridObjects(WorldObject const* center_obj, T& visitor, float radius)
{
CellCoord p(Acore::ComputeCellCoord(center_obj->GetPositionX(), center_obj->GetPositionY()));
Cell cell(p);
if (dont_load)
{
cell.SetNoCreate();
}

TypeContainerVisitor<T, GridTypeMapContainer> gnotifier(visitor);
cell.Visit(p, gnotifier, *center_obj->GetMap(), *center_obj, radius);
}

template<class T>
inline void Cell::VisitWorldObjects(WorldObject const* center_obj, T& visitor, float radius, bool dont_load /*= true*/)
inline void Cell::VisitWorldObjects(WorldObject const* center_obj, T& visitor, float radius)
{
CellCoord p(Acore::ComputeCellCoord(center_obj->GetPositionX(), center_obj->GetPositionY()));
Cell cell(p);
if (dont_load)
{
cell.SetNoCreate();
}

TypeContainerVisitor<T, WorldTypeMapContainer> wnotifier(visitor);
cell.Visit(p, wnotifier, *center_obj->GetMap(), *center_obj, radius);
}

template<class T>
inline void Cell::VisitAllObjects(WorldObject const* center_obj, T& visitor, float radius, bool dont_load /*= true*/)
inline void Cell::VisitAllObjects(WorldObject const* center_obj, T& visitor, float radius)
{
CellCoord p(Acore::ComputeCellCoord(center_obj->GetPositionX(), center_obj->GetPositionY()));
Cell cell(p);
if (dont_load)
{
cell.SetNoCreate();
}

TypeContainerVisitor<T, WorldTypeMapContainer> wnotifier(visitor);
cell.Visit(p, wnotifier, *center_obj->GetMap(), *center_obj, radius);
Expand All @@ -219,42 +192,30 @@ inline void Cell::VisitAllObjects(WorldObject const* center_obj, T& visitor, flo
}

template<class T>
inline void Cell::VisitGridObjects(float x, float y, Map* map, T& visitor, float radius, bool dont_load /*= true*/)
inline void Cell::VisitGridObjects(float x, float y, Map* map, T& visitor, float radius)
{
CellCoord p(Acore::ComputeCellCoord(x, y));
Cell cell(p);
if (dont_load)
{
cell.SetNoCreate();
}

TypeContainerVisitor<T, GridTypeMapContainer> gnotifier(visitor);
cell.Visit(p, gnotifier, *map, x, y, radius);
}

template<class T>
inline void Cell::VisitWorldObjects(float x, float y, Map* map, T& visitor, float radius, bool dont_load /*= true*/)
inline void Cell::VisitWorldObjects(float x, float y, Map* map, T& visitor, float radius)
{
CellCoord p(Acore::ComputeCellCoord(x, y));
Cell cell(p);
if (dont_load)
{
cell.SetNoCreate();
}

TypeContainerVisitor<T, WorldTypeMapContainer> wnotifier(visitor);
cell.Visit(p, wnotifier, *map, x, y, radius);
}

template<class T>
inline void Cell::VisitAllObjects(float x, float y, Map* map, T& visitor, float radius, bool dont_load /*= true*/)
inline void Cell::VisitAllObjects(float x, float y, Map* map, T& visitor, float radius)
{
CellCoord p(Acore::ComputeCellCoord(x, y));
Cell cell(p);
if (dont_load)
{
cell.SetNoCreate();
}

TypeContainerVisitor<T, WorldTypeMapContainer> wnotifier(visitor);
cell.Visit(p, wnotifier, *map, x, y, radius);
Expand Down
Loading

0 comments on commit ee69a56

Please sign in to comment.