Browse Source

Integrate SPGrid loading process into Map class, this is a temporary solution until we embed gridids to Map class

Image 4 years ago
parent
commit
ce1632941d

+ 18 - 0
EQ2/source/WorldServer/Zone/SPGrid.h

@@ -90,6 +90,24 @@ public:
 	Face* GetClosestFace(float x, float y, float z);
 	Face* FindPath(float x, float y, float z, float targX, float targY, float targZ, bool forceEndCell=false);
 
+	void InitValues(float minX, float maxX, float minZ, float maxZ, int32 numCellsX, int32 numCellsZ)
+	{
+		m_MinX = minX;
+		m_MaxX = maxX;
+		m_MinZ = minZ;
+		m_MaxZ = maxZ;
+		m_NumCellsX = numCellsX;
+		m_NumCellsZ = numCellsZ;
+
+		float width = m_MaxX - m_MinX;
+		float height = m_MaxZ - m_MinZ;
+		// Allocate all the cells
+		m_Cells.resize(m_NumCellsZ * m_NumCellsX);
+
+		m_NumFaceCellsX = ceil(width / FACECELLSIZEDEFAULT);
+		m_NumFaceCellsZ = ceil(height / FACECELLSIZEDEFAULT);
+		m_FaceCells.resize(m_NumFaceCellsX * m_NumFaceCellsZ);
+	}
 private:
 	// 1-D array for cells as it is better performance wise then 2-D
 	std::vector<Cell> m_Cells;

+ 29 - 4
EQ2/source/WorldServer/Zone/map.cpp

@@ -31,14 +31,18 @@ ThreadReturnType LoadMapAsync(void* mapToLoad)
 
 	if (map->Load(filename))
 		map->SetMapLoaded(true);
+
+	map->SetMapLoading(false);
 	THREAD_RETURN(NULL);
 }
 
-Map::Map(string file) {
+Map::Map(string file, SPGrid* grid) {
 	CheckMapMutex.SetName(file + "MapMutex");
 	SetMapLoaded(false);
 	m_ZoneFile = file;
 	imp = nullptr;
+	mGrid = grid;
+	m_CellSize = CELLSIZEDEFAULT;
 }
 
 Map::~Map() {
@@ -240,7 +244,7 @@ inline bool file_exists(const std::string& name) {
 	return f.good();
 }
 
-Map *Map::LoadMapFile(std::string file) {
+Map *Map::LoadMapFile(std::string file, SPGrid* grid) {
 
 	std::string filename = "Maps/";
 	filename += file;
@@ -248,8 +252,8 @@ Map *Map::LoadMapFile(std::string file) {
 
 	LogWrite(MAP__INFO, 7, "Map", "Attempting to load Map File [{%s}]", filename.c_str());
 
-	auto m = new Map(file);
-
+	auto m = new Map(file, grid);
+	m->SetMapLoading(true);
 #ifdef WIN32
 	_beginthread(LoadMapAsync, 0, (void*)m);
 #else
@@ -334,6 +338,9 @@ bool Map::LoadV2(FILE* f) {
 	m_NumCellsX = ceil(width / m_CellSize);
 	m_NumCellsZ = ceil(height / m_CellSize);
 
+	if (mGrid != nullptr)
+		mGrid->InitValues(m_MinX, m_MaxX, m_MinZ, m_MaxZ, m_NumCellsX, m_NumCellsZ);
+
 	// Read the number of grids
 	int32 NumGrids;
 	fread(&NumGrids, sizeof(int32), 1, f);
@@ -393,6 +400,24 @@ bool Map::LoadV2(FILE* f) {
 
 			verts.push_back(c);
 			indices.push_back((uint32)sz + 2);
+
+			if (mGrid != nullptr)
+			{
+				Face* face = new Face;
+				face->Vertex1[0] = x1;
+				face->Vertex1[1] = y1;
+				face->Vertex1[2] = z1;
+
+				face->Vertex2[0] = x2;
+				face->Vertex2[1] = y2;
+				face->Vertex2[2] = z2;
+
+				face->Vertex3[0] = x3;
+				face->Vertex3[1] = y3;
+				face->Vertex3[2] = z3;
+
+				mGrid->AddFace(face, GridID);
+			}
 		}
 	}
 	face_count = face_count / 3;

+ 18 - 3
EQ2/source/WorldServer/Zone/map.h

@@ -26,13 +26,14 @@
 #include "../../common/Mutex.h"
 #include "position.h"
 #include <stdio.h>
+#include "SPGrid.h"
 
 #define BEST_Z_INVALID -99999
 
 class Map
 {
 public:
-	Map(string filename);
+	Map(string filename, SPGrid* grid=nullptr);
 	~Map();
 
 	float FindBestZ(glm::vec3 &start, glm::vec3 *result);
@@ -48,11 +49,10 @@ public:
 	bool Load(const std::string& filename);
 #endif
 
-	static Map *LoadMapFile(std::string file);
+	static Map *LoadMapFile(std::string file, SPGrid* grid=nullptr);
 
 	std::string GetFileName() { return m_ZoneFile; }
 	void SetMapLoaded(bool val) {
-		bool isMapLoaded = false;
 		CheckMapMutex.writelock();
 		mapLoaded = val;
 		CheckMapMutex.releasewritelock();
@@ -64,6 +64,19 @@ public:
 		CheckMapMutex.releasereadlock();
 		return isMapLoaded;
 	}
+
+	void SetMapLoading(bool val) {
+		CheckMapMutex.writelock();
+		mapLoading = val;
+		CheckMapMutex.releasewritelock();
+	}
+	bool IsMapLoading() {
+		bool isMapLoading = false;
+		CheckMapMutex.readlock();
+		isMapLoading = mapLoading;
+		CheckMapMutex.releasereadlock();
+		return isMapLoading;
+	}
 private:
 	void RotateVertex(glm::vec3 &v, float rx, float ry, float rz);
 	void ScaleVertex(glm::vec3 &v, float sx, float sy, float sz);
@@ -92,7 +105,9 @@ private:
 	struct impl;
 	impl *imp;
 	bool mapLoaded;
+	bool mapLoading;
 	Mutex CheckMapMutex;
+	SPGrid* mGrid;
 };
 
 #endif

+ 18 - 17
EQ2/source/WorldServer/zoneserver.cpp

@@ -271,21 +271,14 @@ void ZoneServer::Init()
 	UpdateWindowTitle(0);
 
 	string zoneName(GetZoneFile());
+	if (Grid == nullptr) {
+		Grid = new SPGrid(string(GetZoneFile()), 0);
+	}
 	if (zonemap == nullptr) {
-		zonemap = Map::LoadMapFile(zoneName);
+		zonemap = Map::LoadMapFile(zoneName, Grid);
 	}
 	pathing = IPathfinder::Load(zoneName);
 	movementMgr = new MobMovementManager();
-	if (Grid == nullptr) {
-		Grid = new SPGrid(string(GetZoneFile()), 0);
-		if (Grid->Init())
-			LogWrite(ZONE__DEBUG, 0, "SPGrid", "ZoneServer::Init() successfully initialized the grid");
-		else {
-			LogWrite(ZONE__DEBUG, 0, "SPGrid", "ZoneServer::Init() failed to initialize the grid... poor tron...");
-			delete Grid;
-			Grid = nullptr;
-		}
-	}
 //	else
 	//	LogWrite(ZONE__ERROR, 0, "SPGrid", "ZoneServer::Init() Grid is not null in init, wtf!");
 
@@ -1240,15 +1233,15 @@ void ZoneServer::RemoveDamagedSpawn(Spawn* spawn){
 		damaged_spawns.Remove(spawn->GetID());
 }
 
-bool ZoneServer::Process() 
+bool ZoneServer::Process()
 {
 	MMasterZoneLock->lock(); //Changing this back to a recursive lock to fix a possible /reload spells crash with multiple zones running - Foof
 #ifndef NO_CATCH
 	try
 	{
 #endif
-		if(LoadingData){
-			while(zoneID == 0){ //this is loaded by world
+		if (LoadingData) {
+			while (zoneID == 0) { //this is loaded by world
 				Sleep(10);
 			}
 
@@ -1277,7 +1270,7 @@ bool ZoneServer::Process()
 				LogWrite(GROUNDSPAWN__INFO, 0, "GSpawn", "-Load Groundspawn data complete!");
 
 				LogWrite(PET__INFO, 0, "Pet", "-Loading Pet data...");
-				database.GetPetNames(this);	
+				database.GetPetNames(this);
 				LogWrite(PET__INFO, 0, "Pet", "-Load Pet data complete!");
 
 				LogWrite(LOOT__INFO, 0, "Loot", "-Loading Spawn loot data...");
@@ -1306,6 +1299,14 @@ bool ZoneServer::Process()
 			spawn_group_chances.clear();
 			MSpawnGroupChances.releasewritelock(__FUNCTION__, __LINE__);
 
+			while (zonemap != nullptr && zonemap->IsMapLoading())
+			{
+				Sleep(10);
+			}
+
+			if (zonemap != nullptr && Grid != nullptr && !zonemap->IsMapLoaded())
+				delete Grid;
+
 			DeleteTransporters();
 			ReloadTransporters();
 
@@ -1315,7 +1316,7 @@ bool ZoneServer::Process()
 			if (!revive_points)
 				revive_points = new vector<RevivePoint*>;
 			else {
-				while (!revive_points->empty()){
+				while (!revive_points->empty()) {
 					safe_delete(revive_points->back());
 					revive_points->pop_back();
 				}
@@ -1331,7 +1332,7 @@ bool ZoneServer::Process()
 			spawn_check_add.Trigger();
 
 			const char* zone_script = world.GetZoneScript(this->GetZoneID());
-			if(lua_interface && zone_script) {
+			if (lua_interface && zone_script) {
 				RemoveLocationProximities();
 				lua_interface->RunZoneScript(zone_script, "init_zone_script", this);
 			}