[7593] Map::EnsureGridLoaded related cleanups and fixes:

* Make generic EnsureGridLoaded function and move specialized code to LoadGrid and EnsureGridLoadedAtEnter
* Rename args to gx/gy for low-level grid numbering values.
* Remove redundent LoadMpa call after always case loading maps in EnsureGridCreated
This commit is contained in:
VladimirMangos 2009-03-31 23:09:11 +04:00
parent 66b9cf7829
commit 5dc0a1ed97
4 changed files with 58 additions and 64 deletions

View file

@ -48,11 +48,11 @@ Map::~Map()
UnloadAll(true); UnloadAll(true);
} }
bool Map::ExistMap(uint32 mapid,int x,int y) bool Map::ExistMap(uint32 mapid,int gx,int gy)
{ {
int len = sWorld.GetDataPath().length()+strlen("maps/%03u%02u%02u.map")+1; int len = sWorld.GetDataPath().length()+strlen("maps/%03u%02u%02u.map")+1;
char* tmp = new char[len]; char* tmp = new char[len];
snprintf(tmp, len, (char *)(sWorld.GetDataPath()+"maps/%03u%02u%02u.map").c_str(),mapid,x,y); snprintf(tmp, len, (char *)(sWorld.GetDataPath()+"maps/%03u%02u%02u.map").c_str(),mapid,gx,gy);
FILE *pf=fopen(tmp,"rb"); FILE *pf=fopen(tmp,"rb");
@ -79,17 +79,17 @@ bool Map::ExistMap(uint32 mapid,int x,int y)
return true; return true;
} }
bool Map::ExistVMap(uint32 mapid,int x,int y) bool Map::ExistVMap(uint32 mapid,int gx,int gy)
{ {
if(VMAP::IVMapManager* vmgr = VMAP::VMapFactory::createOrGetVMapManager()) if(VMAP::IVMapManager* vmgr = VMAP::VMapFactory::createOrGetVMapManager())
{ {
if(vmgr->isMapLoadingEnabled()) if(vmgr->isMapLoadingEnabled())
{ {
// x and y are swapped !! => fixed now // x and y are swapped !! => fixed now
bool exists = vmgr->existsMap((sWorld.GetDataPath()+ "vmaps").c_str(), mapid, x,y); bool exists = vmgr->existsMap((sWorld.GetDataPath()+ "vmaps").c_str(), mapid, gx,gy);
if(!exists) if(!exists)
{ {
std::string name = vmgr->getDirFileName(mapid,x,y); std::string name = vmgr->getDirFileName(mapid,gx,gy);
sLog.outError("VMap file '%s' is missing or point to wrong version vmap file, redo vmaps with latest vmap_assembler.exe program", (sWorld.GetDataPath()+"vmaps/"+name).c_str()); sLog.outError("VMap file '%s' is missing or point to wrong version vmap file, redo vmaps with latest vmap_assembler.exe program", (sWorld.GetDataPath()+"vmaps/"+name).c_str());
return false; return false;
} }
@ -99,75 +99,70 @@ bool Map::ExistVMap(uint32 mapid,int x,int y)
return true; return true;
} }
void Map::LoadVMap(int x,int y) void Map::LoadVMap(int gx,int gy)
{ {
// x and y are swapped !! // x and y are swapped !!
int vmapLoadResult = VMAP::VMapFactory::createOrGetVMapManager()->loadMap((sWorld.GetDataPath()+ "vmaps").c_str(), GetId(), x,y); int vmapLoadResult = VMAP::VMapFactory::createOrGetVMapManager()->loadMap((sWorld.GetDataPath()+ "vmaps").c_str(), GetId(), gx,gy);
switch(vmapLoadResult) switch(vmapLoadResult)
{ {
case VMAP::VMAP_LOAD_RESULT_OK: case VMAP::VMAP_LOAD_RESULT_OK:
sLog.outDetail("VMAP loaded name:%s, id:%d, x:%d, y:%d (vmap rep.: x:%d, y:%d)", GetMapName(), GetId(), x,y, x,y); sLog.outDetail("VMAP loaded name:%s, id:%d, x:%d, y:%d (vmap rep.: x:%d, y:%d)", GetMapName(), GetId(), gx,gy,gx,gy);
break; break;
case VMAP::VMAP_LOAD_RESULT_ERROR: case VMAP::VMAP_LOAD_RESULT_ERROR:
sLog.outDetail("Could not load VMAP name:%s, id:%d, x:%d, y:%d (vmap rep.: x:%d, y:%d)", GetMapName(), GetId(), x,y, x,y); sLog.outDetail("Could not load VMAP name:%s, id:%d, x:%d, y:%d (vmap rep.: x:%d, y:%d)", GetMapName(), GetId(), gx,gy,gx,gy);
break; break;
case VMAP::VMAP_LOAD_RESULT_IGNORED: case VMAP::VMAP_LOAD_RESULT_IGNORED:
DEBUG_LOG("Ignored VMAP name:%s, id:%d, x:%d, y:%d (vmap rep.: x:%d, y:%d)", GetMapName(), GetId(), x,y, x,y); DEBUG_LOG("Ignored VMAP name:%s, id:%d, x:%d, y:%d (vmap rep.: x:%d, y:%d)", GetMapName(), GetId(), gx,gy,gx,gy);
break; break;
} }
} }
void Map::LoadMap(uint32 mapid, uint32 instanceid, int x,int y) void Map::LoadMap(int gx,int gy)
{ {
if( instanceid != 0 ) if( i_InstanceId != 0 )
{ {
if(GridMaps[x][y]) if(GridMaps[gx][gy])
return; return;
Map* baseMap = const_cast<Map*>(MapManager::Instance().GetBaseMap(mapid)); Map* baseMap = const_cast<Map*>(MapManager::Instance().GetBaseMap(i_id));
// load gridmap for base map // load grid map for base map
if (!baseMap->GridMaps[x][y]) if (!baseMap->GridMaps[gx][gy])
baseMap->EnsureGridCreated(GridPair(63-x,63-y)); baseMap->EnsureGridCreated(GridPair(63-gx,63-gy));
//+++ if (!baseMap->GridMaps[x][y]) don't check for GridMaps[gx][gy], we need the management for vmaps ((MapInstanced*)(baseMap))->AddGridMapReference(GridPair(gx,gy));
// return; GridMaps[gx][gy] = baseMap->GridMaps[gx][gy];
((MapInstanced*)(baseMap))->AddGridMapReference(GridPair(x,y));
GridMaps[x][y] = baseMap->GridMaps[x][y];
return; return;
} }
//map already load, delete it before reloading (Is it necessary? Do we really need the ability the reload maps during runtime?) //map already load, delete it before reloading (Is it necessary? Do we really need the ability the reload maps during runtime?)
if(GridMaps[x][y]) if(GridMaps[gx][gy])
{ {
sLog.outDetail("Unloading already loaded map %u before reloading.",mapid); sLog.outDetail("Unloading already loaded map %u before reloading.",i_id);
delete (GridMaps[x][y]); delete (GridMaps[gx][gy]);
GridMaps[x][y]=NULL; GridMaps[gx][gy]=NULL;
} }
// map file name // map file name
char *tmp=NULL; char *tmp=NULL;
// Pihhan: dataPath length + "maps/" + 3+2+2+ ".map" length may be > 32 !
int len = sWorld.GetDataPath().length()+strlen("maps/%03u%02u%02u.map")+1; int len = sWorld.GetDataPath().length()+strlen("maps/%03u%02u%02u.map")+1;
tmp = new char[len]; tmp = new char[len];
snprintf(tmp, len, (char *)(sWorld.GetDataPath()+"maps/%03u%02u%02u.map").c_str(),mapid,x,y); snprintf(tmp, len, (char *)(sWorld.GetDataPath()+"maps/%03u%02u%02u.map").c_str(),i_id,gx,gy);
sLog.outDetail("Loading map %s",tmp); sLog.outDetail("Loading map %s",tmp);
// loading data // loading data
GridMaps[x][y] = new GridMap(); GridMaps[gx][gy] = new GridMap();
if (!GridMaps[x][y]->loadData(tmp)) if (!GridMaps[gx][gy]->loadData(tmp))
{ {
sLog.outError("Error load map file: \n %s\n", tmp); sLog.outError("Error load map file: \n %s\n", tmp);
} }
delete [] tmp; delete [] tmp;
return;
} }
void Map::LoadMapAndVMap(uint32 mapid, uint32 instanceid, int x,int y) void Map::LoadMapAndVMap(int gx,int gy)
{ {
LoadMap(mapid,instanceid,x,y); LoadMap(gx,gy);
if(instanceid == 0) if(i_InstanceId == 0)
LoadVMap(x, y); // Only load the data for the base map LoadVMap(gx, gy); // Only load the data for the base map
} }
void Map::InitStateMachine() void Map::InitStateMachine()
@ -334,19 +329,17 @@ Map::EnsureGridCreated(const GridPair &p)
int gy=63-p.y_coord; int gy=63-p.y_coord;
if(!GridMaps[gx][gy]) if(!GridMaps[gx][gy])
Map::LoadMapAndVMap(i_id,i_InstanceId,gx,gy); LoadMapAndVMap(gx,gy);
} }
} }
} }
void void
Map::EnsureGridLoaded(const Cell &cell, Player *player) Map::EnsureGridLoadedAtEnter(const Cell &cell, Player *player)
{ {
EnsureGridCreated(GridPair(cell.GridX(), cell.GridY()));
NGridType *grid = getNGrid(cell.GridX(), cell.GridY()); NGridType *grid = getNGrid(cell.GridX(), cell.GridY());
assert(grid != NULL); if(EnsureGridLoaded(cell))
if (!isGridObjectDataLoaded(cell.GridX(), cell.GridY()))
{ {
if (player) if (player)
{ {
@ -358,13 +351,6 @@ Map::EnsureGridLoaded(const Cell &cell, Player *player)
DEBUG_LOG("Active object nearby triggers of loading grid [%u,%u] on map %u", cell.GridX(), cell.GridY(), i_id); DEBUG_LOG("Active object nearby triggers of loading grid [%u,%u] on map %u", cell.GridX(), cell.GridY(), i_id);
} }
ObjectGridLoader loader(*grid, this, cell);
loader.LoadN();
setGridObjectDataLoaded(true, cell.GridX(), cell.GridY());
// Add resurrectable corpses to world object list in grid
ObjectAccessor::Instance().AddCorpsesToGrid(GridPair(cell.GridX(),cell.GridY()),(*grid)(cell.CellX(), cell.CellY()), this);
ResetGridExpiry(*getNGrid(cell.GridX(), cell.GridY()), 0.1f); ResetGridExpiry(*getNGrid(cell.GridX(), cell.GridY()), 0.1f);
grid->SetGridState(GRID_STATE_ACTIVE); grid->SetGridState(GRID_STATE_ACTIVE);
} }
@ -373,8 +359,7 @@ Map::EnsureGridLoaded(const Cell &cell, Player *player)
AddToGrid(player,grid,cell); AddToGrid(player,grid,cell);
} }
void bool Map::EnsureGridLoaded(const Cell &cell)
Map::LoadGrid(const Cell& cell, bool no_unload)
{ {
EnsureGridCreated(GridPair(cell.GridX(), cell.GridY())); EnsureGridCreated(GridPair(cell.GridX(), cell.GridY()));
NGridType *grid = getNGrid(cell.GridX(), cell.GridY()); NGridType *grid = getNGrid(cell.GridX(), cell.GridY());
@ -389,10 +374,18 @@ Map::LoadGrid(const Cell& cell, bool no_unload)
ObjectAccessor::Instance().AddCorpsesToGrid(GridPair(cell.GridX(),cell.GridY()),(*grid)(cell.CellX(), cell.CellY()), this); ObjectAccessor::Instance().AddCorpsesToGrid(GridPair(cell.GridX(),cell.GridY()),(*grid)(cell.CellX(), cell.CellY()), this);
setGridObjectDataLoaded(true,cell.GridX(), cell.GridY()); setGridObjectDataLoaded(true,cell.GridX(), cell.GridY());
if(no_unload) return true;
getNGrid(cell.GridX(), cell.GridY())->setUnloadExplicitLock(true);
} }
LoadVMap(63-cell.GridX(),63-cell.GridY());
return false;
}
void Map::LoadGrid(const Cell& cell, bool no_unload)
{
EnsureGridLoaded(cell);
if(no_unload)
getNGrid(cell.GridX(), cell.GridY())->setUnloadExplicitLock(true);
} }
bool Map::Add(Player *player) bool Map::Add(Player *player)
@ -404,7 +397,7 @@ bool Map::Add(Player *player)
// update player state for other player and visa-versa // update player state for other player and visa-versa
CellPair p = MaNGOS::ComputeCellPair(player->GetPositionX(), player->GetPositionY()); CellPair p = MaNGOS::ComputeCellPair(player->GetPositionX(), player->GetPositionY());
Cell cell(p); Cell cell(p);
EnsureGridLoaded(cell, player); EnsureGridLoadedAtEnter(cell, player);
player->AddToWorld(); player->AddToWorld();
SendInitSelf(player); SendInitSelf(player);
@ -433,7 +426,7 @@ Map::Add(T *obj)
Cell cell(p); Cell cell(p);
if(obj->isActiveObject()) if(obj->isActiveObject())
EnsureGridLoaded(cell); EnsureGridLoadedAtEnter(cell);
else else
EnsureGridCreated(GridPair(cell.GridX(), cell.GridY())); EnsureGridCreated(GridPair(cell.GridX(), cell.GridY()));
@ -788,7 +781,7 @@ Map::PlayerRelocation(Player *player, float x, float y, float z, float orientati
if( !old_cell.DiffGrid(new_cell) ) if( !old_cell.DiffGrid(new_cell) )
AddToGrid(player, oldGrid,new_cell); AddToGrid(player, oldGrid,new_cell);
else else
EnsureGridLoaded(new_cell, player); EnsureGridLoadedAtEnter(new_cell, player);
} }
// if move then update what player see and who seen // if move then update what player see and who seen
@ -912,7 +905,7 @@ bool Map::CreatureCellRelocation(Creature *c, Cell new_cell)
// in diff. grids but active creature // in diff. grids but active creature
if(c->isActiveObject()) if(c->isActiveObject())
{ {
EnsureGridLoaded(new_cell); EnsureGridLoadedAtEnter(new_cell);
#ifdef MANGOS_DEBUG #ifdef MANGOS_DEBUG
if((sLog.getLogFilter() & LOG_FILTER_CREATURE_MOVES)==0) if((sLog.getLogFilter() & LOG_FILTER_CREATURE_MOVES)==0)

View file

@ -299,9 +299,8 @@ class MANGOS_DLL_SPEC Map : public GridRefManager<NGridType>, public MaNGOS::Obj
time_t GetGridExpiry(void) const { return i_gridExpiry; } time_t GetGridExpiry(void) const { return i_gridExpiry; }
uint32 GetId(void) const { return i_id; } uint32 GetId(void) const { return i_id; }
static bool ExistMap(uint32 mapid, int x, int y); static bool ExistMap(uint32 mapid, int gx, int gy);
static bool ExistVMap(uint32 mapid, int x, int y); static bool ExistVMap(uint32 mapid, int gx, int gy);
void LoadMapAndVMap(uint32 mapid, uint32 instanceid, int x, int y);
static void InitStateMachine(); static void InitStateMachine();
static void DeleteStateMachine(); static void DeleteStateMachine();
@ -404,8 +403,9 @@ class MANGOS_DLL_SPEC Map : public GridRefManager<NGridType>, public MaNGOS::Obj
void RemoveFromActive(Creature* obj); void RemoveFromActive(Creature* obj);
private: private:
void LoadVMap(int pX, int pY); void LoadMapAndVMap(int gx, int gy);
void LoadMap(uint32 mapid, uint32 instanceid, int x,int y); void LoadVMap(int gx, int gy);
void LoadMap(int gx,int gy);
GridMap *GetGrid(float x, float y); GridMap *GetGrid(float x, float y);
void SetTimer(uint32 t) { i_gridExpiry = t < MIN_GRID_DELAY ? MIN_GRID_DELAY : t; } void SetTimer(uint32 t) { i_gridExpiry = t < MIN_GRID_DELAY ? MIN_GRID_DELAY : t; }
@ -425,8 +425,9 @@ class MANGOS_DLL_SPEC Map : public GridRefManager<NGridType>, public MaNGOS::Obj
CreatureMoveList i_creaturesToMove; CreatureMoveList i_creaturesToMove;
bool loaded(const GridPair &) const; bool loaded(const GridPair &) const;
void EnsureGridLoaded(const Cell&, Player* player = NULL);
void EnsureGridCreated(const GridPair &); void EnsureGridCreated(const GridPair &);
bool EnsureGridLoaded(Cell const&);
void EnsureGridLoadedAtEnter(Cell const&, Player* player = NULL);
void buildNGridLinkage(NGridType* pNGridType) { pNGridType->link(this); } void buildNGridLinkage(NGridType* pNGridType) { pNGridType->link(this); }

View file

@ -49,7 +49,7 @@ class MANGOS_DLL_DECL MapInstanced : public Map
SetUnloadReferenceLock(GridPair(63-p.x_coord, 63-p.y_coord), true); SetUnloadReferenceLock(GridPair(63-p.x_coord, 63-p.y_coord), true);
} }
void RemoveGridMapReference(const GridPair &p) void RemoveGridMapReference(GridPair const& p)
{ {
--GridMapReference[p.x_coord][p.y_coord]; --GridMapReference[p.x_coord][p.y_coord];
if (!GridMapReference[p.x_coord][p.y_coord]) if (!GridMapReference[p.x_coord][p.y_coord])

View file

@ -1,4 +1,4 @@
#ifndef __REVISION_NR_H__ #ifndef __REVISION_NR_H__
#define __REVISION_NR_H__ #define __REVISION_NR_H__
#define REVISION_NR "7592" #define REVISION_NR "7593"
#endif // __REVISION_NR_H__ #endif // __REVISION_NR_H__