Skip to content

Commit

Permalink
MMAP: Refactor mmap manager dependancies and improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
killerwife committed Sep 2, 2024
1 parent f205793 commit fd1b862
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 62 deletions.
2 changes: 2 additions & 0 deletions src/game/Chat/Level3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
#include "Globals/CombatCondition.h"
#include "World/WorldStateExpression.h"

#include "MotionGenerators/MoveMap.h"

#ifdef BUILD_AHBOT
#include "AuctionHouseBot/AuctionHouseBot.h"

Expand Down
11 changes: 11 additions & 0 deletions src/game/Entities/Creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2999,6 +2999,17 @@ void Creature::SetCanDualWield(bool state)
UpdateDamagePhysical(OFF_ATTACK);
}

Unit::MmapForcingStatus Creature::IsIgnoringMMAP() const
{
if (m_ignoreMMAP)
return MmapForcingStatus::IGNORED;

if (GetCreatureInfo()->ExtraFlags & CREATURE_EXTRA_FLAG_MMAP_FORCE_ENABLE)
return MmapForcingStatus::FORCED;

return Unit::IsIgnoringMMAP();
}

bool Creature::CanRestockPickpocketLoot() const
{
return GetMap()->GetCurrentClockTime() >= m_pickpocketRestockTime;
Expand Down
2 changes: 1 addition & 1 deletion src/game/Entities/Creature.h
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ class Creature : public Unit
bool IsInvisible() const { return m_isInvisible; }

void SetIgnoreMMAP(bool ignore) { m_ignoreMMAP = ignore; }
bool IsIgnoringMMAP() const { return m_ignoreMMAP; }
virtual MmapForcingStatus IsIgnoringMMAP() const override;

void OnEventHappened(uint16 eventId, bool activate, bool resume) override { return AI()->OnEventHappened(eventId, activate, resume); }

Expand Down
8 changes: 8 additions & 0 deletions src/game/Entities/Unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12857,6 +12857,14 @@ void Unit::SelectAttackingTargets(std::vector<Unit*>& selectedTargets, Attacking
}
}

Unit::MmapForcingStatus Unit::IsIgnoringMMAP() const
{
if (IsPlayer() || IsPlayerControlled())
return MmapForcingStatus::FORCED;

return MmapForcingStatus::DEFAULT;
}

void Unit::SetLevitate(bool enable)
{
if (!IsClientControlled())
Expand Down
9 changes: 9 additions & 0 deletions src/game/Entities/Unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -1818,6 +1818,15 @@ class Unit : public WorldObject
bool IsJumping() const { return m_movementInfo.HasMovementFlag(MOVEFLAG_JUMPING); }
bool IsFalling() const { return m_movementInfo.HasMovementFlag(MOVEFLAG_FALLING); }

enum class MmapForcingStatus
{
FORCED,
DEFAULT,
IGNORED
};

virtual MmapForcingStatus IsIgnoringMMAP() const;

bool IsDebuggingMovement() const { return m_debuggingMovement; }
void SetDebuggingMovement(bool state) { m_debuggingMovement = state; }

Expand Down
2 changes: 1 addition & 1 deletion src/game/Maps/GridMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ GridMap* TerrainInfo::LoadMapAndVMap(const uint32 x, const uint32 y, bool mapOnl
if (!MMAP::MMapFactory::createOrGetMMapManager()->IsMMapIsLoaded(m_mapId, x, y))
{
// load navmesh
MMAP::MMapFactory::createOrGetMMapManager()->loadMap(m_mapId, x, y);
MMAP::MMapFactory::createOrGetMMapManager()->loadMap(sWorld.GetDataPath(), m_mapId, x, y);
}

if (m_GridMaps[x][y])
Expand Down
79 changes: 28 additions & 51 deletions src/game/MotionGenerators/MoveMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
*/

#include "Log/Log.h"
#include "World/World.h"
#include "Entities/Creature.h"
#include "Entities/Unit.h"
#include "MotionGenerators/MoveMap.h"
#include "MoveMapSharedDefines.h"

namespace MMAP
{
constexpr char MAP_FILE_NAME_FORMAT[] = "mmaps/%03i.mmap";
constexpr char TILE_FILE_NAME_FORMAT[] = "mmaps/%03i%02i%02i.mmtile";
constexpr char GO_FILE_NAME_FORMAT[] = "mmaps/go%04i.mmtile";

// ######################## MMapFactory ########################
// our global singleton copy
MMapManager* g_MMapManager = nullptr;
Expand Down Expand Up @@ -60,25 +63,21 @@ namespace MMAP

bool MMapFactory::IsPathfindingEnabled(uint32 mapId, const Unit* unit = nullptr)
{
if (!sWorld.getConfig(CONFIG_BOOL_MMAP_ENABLED))
if (!createOrGetMMapManager()->IsEnabled())
return false;

if (unit)
{
// always use mmaps for players
if (unit->GetTypeId() == TYPEID_PLAYER)
return true;

if (IsPathfindingForceDisabled(unit))
return false;

if (IsPathfindingForceEnabled(unit))
return true;

// always use mmaps for pets of players (can still be disabled by extra-flag for pet creature)
if (unit->GetTypeId() == TYPEID_UNIT && ((Creature*)unit)->IsPet() && unit->GetOwner() &&
unit->GetOwner()->GetTypeId() == TYPEID_PLAYER)
return true;
Unit::MmapForcingStatus status = unit->IsIgnoringMMAP();
switch (status)
{
case Unit::MmapForcingStatus::DEFAULT:
break;
case Unit::MmapForcingStatus::FORCED:
return true;
case Unit::MmapForcingStatus::IGNORED:
return false;
}
}

return g_mmapDisabledIds->find(mapId) == g_mmapDisabledIds->end();
Expand All @@ -93,45 +92,23 @@ namespace MMAP
g_MMapManager = nullptr;
}

bool MMapFactory::IsPathfindingForceEnabled(const Unit* unit)
{
if (const Creature* pCreature = dynamic_cast<const Creature*>(unit))
{
if (const CreatureInfo* pInfo = pCreature->GetCreatureInfo())
{
if (pInfo->ExtraFlags & CREATURE_EXTRA_FLAG_MMAP_FORCE_ENABLE)
return true;
}
}

return false;
}

bool MMapFactory::IsPathfindingForceDisabled(const Unit* unit)
{
if (const Creature* pCreature = dynamic_cast<const Creature*>(unit))
return pCreature->IsIgnoringMMAP();

return false;
}

// ######################## MMapManager ########################
MMapManager::~MMapManager()
{
// by now we should not have maps loaded
// if we had, tiles in MMapData->mmapLoadedTiles, their actual data is lost!
}

bool MMapManager::loadMapData(uint32 mapId)
bool MMapManager::loadMapData(std::string const& basePath, uint32 mapId)
{
// we already have this map loaded?
if (loadedMMaps.find(mapId) != loadedMMaps.end())
return true;

// load and init dtNavMesh - read parameters from file
uint32 pathLen = sWorld.GetDataPath().length() + strlen("mmaps/%03i.mmap") + 1;
uint32 pathLen = basePath.length() + strlen(MAP_FILE_NAME_FORMAT) + 1;
char* fileName = new char[pathLen];
snprintf(fileName, pathLen, (sWorld.GetDataPath() + "mmaps/%03i.mmap").c_str(), mapId);
snprintf(fileName, pathLen, (basePath + MAP_FILE_NAME_FORMAT).c_str(), mapId);

FILE* file = fopen(fileName, "rb");
if (!file)
Expand Down Expand Up @@ -188,10 +165,10 @@ namespace MMAP
return false;
}

bool MMapManager::loadMap(uint32 mapId, int32 x, int32 y)
bool MMapManager::loadMap(std::string const& basePath, uint32 mapId, int32 x, int32 y)
{
// make sure the mmap is loaded and ready to load tiles
if (!loadMapData(mapId))
if (!loadMapData(basePath, mapId))
return false;

// get this mmap data
Expand All @@ -207,9 +184,9 @@ namespace MMAP
}

// load this tile :: mmaps/MMMXXYY.mmtile
uint32 pathLen = sWorld.GetDataPath().length() + strlen("mmaps/%03i%02i%02i.mmtile") + 1;
uint32 pathLen = basePath.length() + strlen(TILE_FILE_NAME_FORMAT) + 1;
char* fileName = new char[pathLen];
snprintf(fileName, pathLen, (sWorld.GetDataPath() + "mmaps/%03i%02i%02i.mmtile").c_str(), mapId, x, y);
snprintf(fileName, pathLen, (basePath + TILE_FILE_NAME_FORMAT).c_str(), mapId, x, y);

FILE* file = fopen(fileName, "rb");
if (!file)
Expand Down Expand Up @@ -270,22 +247,22 @@ namespace MMAP
return true;
}

void MMapManager::loadAllGameObjectModels(std::vector<uint32> const& displayIds)
void MMapManager::loadAllGameObjectModels(std::string const& basePath, std::vector<uint32> const& displayIds)
{
for (uint32 displayId : displayIds)
loadGameObject(displayId);
loadGameObject(basePath, displayId);
}

bool MMapManager::loadGameObject(uint32 displayId)
bool MMapManager::loadGameObject(std::string const& basePath, uint32 displayId)
{
// we already have this map loaded?
if (m_loadedModels.find(displayId) != m_loadedModels.end())
return true;

// load and init dtNavMesh - read parameters from file
uint32 pathLen = sWorld.GetDataPath().length() + strlen("mmaps/go%04i.mmtile") + 1;
uint32 pathLen = basePath.length() + strlen(GO_FILE_NAME_FORMAT) + 1;
char* fileName = new char[pathLen];
snprintf(fileName, pathLen, (sWorld.GetDataPath() + "mmaps/go%04i.mmtile").c_str(), displayId);
snprintf(fileName, pathLen, (basePath + GO_FILE_NAME_FORMAT).c_str(), displayId);

FILE* file = fopen(fileName, "rb");
if (!file)
Expand Down
17 changes: 10 additions & 7 deletions src/game/MotionGenerators/MoveMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ namespace MMAP
class MMapManager
{
public:
MMapManager() : loadedTiles(0) {}
MMapManager() : loadedTiles(0), m_enabled(true) {}
~MMapManager();

bool loadMap(uint32 mapId, int32 x, int32 y);
void loadAllGameObjectModels(std::vector<uint32> const& displayIds);
bool loadGameObject(uint32 displayId);
bool loadMap(std::string const& basePath, uint32 mapId, int32 x, int32 y);
void loadAllGameObjectModels(std::string const& basePath, std::vector<uint32> const& displayIds);
bool loadGameObject(std::string const& basePath, uint32 displayId);
bool unloadMap(uint32 mapId, int32 x, int32 y);
bool unloadMap(uint32 mapId);
bool unloadMapInstance(uint32 mapId, uint32 instanceId);
Expand All @@ -110,15 +110,20 @@ namespace MMAP

uint32 getLoadedTilesCount() const { return loadedTiles; }
uint32 getLoadedMapsCount() const { return loadedMMaps.size(); }

void SetEnabled(bool state) { m_enabled = state; }
bool IsEnabled() const { return m_enabled; }
private:
bool loadMapData(uint32 mapId);
bool loadMapData(std::string const& basePath, uint32 mapId);
uint32 packTileID(int32 x, int32 y) const;

std::unordered_map<uint32, std::unique_ptr<MMapData>> loadedMMaps;
uint32 loadedTiles;

std::unordered_map<uint32, std::unique_ptr<MMapGOData>> m_loadedModels;
std::mutex m_modelsMutex;

bool m_enabled;
};

// static class
Expand All @@ -131,8 +136,6 @@ namespace MMAP
static void clear();
static void preventPathfindingOnMaps(const char* ignoreMapIds);
static bool IsPathfindingEnabled(uint32 mapId, const Unit* unit);
static bool IsPathfindingForceEnabled(const Unit* unit);
static bool IsPathfindingForceDisabled(const Unit* unit);
};
}

Expand Down
6 changes: 4 additions & 2 deletions src/game/World/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,9 @@ void World::LoadConfigSettings(bool reload)
setConfig(CONFIG_BOOL_MMAP_ENABLED, "mmap.enabled", true);
std::string ignoreMapIds = sConfig.GetStringDefault("mmap.ignoreMapIds");
MMAP::MMapFactory::preventPathfindingOnMaps(ignoreMapIds.c_str());
sLog.outString("WORLD: MMap pathfinding %sabled", getConfig(CONFIG_BOOL_MMAP_ENABLED) ? "en" : "dis");
bool enabledPathfinding = getConfig(CONFIG_BOOL_MMAP_ENABLED);
sLog.outString("WORLD: MMap pathfinding %sabled", enabledPathfinding ? "en" : "dis");
MMAP::MMapFactory::createOrGetMMapManager()->SetEnabled(enabledPathfinding);

setConfig(CONFIG_BOOL_PATH_FIND_OPTIMIZE, "PathFinder.OptimizePath", true);
setConfig(CONFIG_BOOL_PATH_FIND_NORMALIZE_Z, "PathFinder.NormalizeZ", false);
Expand Down Expand Up @@ -986,7 +988,7 @@ void World::SetInitialWorldSettings()

sLog.outString("Loading Game Object Templates..."); // must be after LoadPageTexts
std::vector<uint32> transportDisplayIds = sObjectMgr.LoadGameobjectInfo();
MMAP::MMapFactory::createOrGetMMapManager()->loadAllGameObjectModels(transportDisplayIds);
MMAP::MMapFactory::createOrGetMMapManager()->loadAllGameObjectModels(GetDataPath(), transportDisplayIds);

sLog.outString("Loading GameObject models...");
LoadGameObjectModelList();
Expand Down

0 comments on commit fd1b862

Please sign in to comment.