SPGrid.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "SPGrid.h"
  17. #include "../../common/Log.h"
  18. #include <cmath>
  19. SPGrid::SPGrid(string file, int32 cellSize) {
  20. m_ZoneFile = file;
  21. m_CellSize = cellSize;
  22. m_MinX = 0;
  23. m_MinZ = 0;
  24. m_MaxX = 0;
  25. m_MaxZ = 0;
  26. m_NumCellsX = 0;
  27. m_NumCellsZ = 0;
  28. m_NumFaceCellsX = 0;
  29. m_NumFaceCellsZ = 0;
  30. }
  31. SPGrid::~SPGrid() {
  32. vector<Cell>::iterator CellItr;
  33. map<int32, vector<Face*> >::iterator MapItr;
  34. vector<Face*>::iterator FaceItr;
  35. // Loop through the vector of cells
  36. /*for (CellItr = m_Cells.begin(); CellItr != m_Cells.end(); CellItr++) {
  37. // Loop through the map of vertices on this cell
  38. for (MapItr = (*CellItr).FaceList.begin(); MapItr != (*CellItr).FaceList.end(); MapItr++) {
  39. // Loop through the vector of faces in the map and delete the pointers
  40. for (FaceItr = (*MapItr).second.begin(); FaceItr != (*MapItr).second.end(); FaceItr++) {
  41. safe_delete((*FaceItr));
  42. }
  43. }
  44. }*/
  45. }
  46. bool SPGrid::Init() {
  47. // Make sure we have a zone file
  48. if (m_ZoneFile.empty()) {
  49. LogWrite(ZONE__ERROR, 0, "SPGrid", "SPGrid::Init() m_ZoneFile is empty.");
  50. return false;
  51. }
  52. // Make sure we have a cell size
  53. if (m_CellSize == 0)
  54. m_CellSize = CELLSIZEDEFAULT;
  55. // Open the map file for this zone
  56. string filePath = "Maps/" + m_ZoneFile + ".EQ2Map";
  57. FILE* file = fopen(filePath.c_str(), "rb");
  58. if (file == nullptr) {
  59. LogWrite(ZONE__WARNING, 0, "SPGrid", "SPGrid::Init() unable to open the map file for %s. (zoneserver will continue to run fine without it)", m_ZoneFile.c_str());
  60. return false;
  61. }
  62. // Read the string for the zone file name this was created for
  63. int8 strSize;
  64. char name[256];
  65. fread(&strSize, sizeof(int8), 1, file);
  66. LogWrite(ZONE__DEBUG, 0, "SPGrid", "strSize = %u", strSize);
  67. size_t len = fread(&name, sizeof(char), strSize, file);
  68. name[len] = '\0';
  69. LogWrite(ZONE__DEBUG, 0, "SPGrid", "name = %s", name);
  70. // Make sure file contents are for the correct zone
  71. if (string(name) != m_ZoneFile) {
  72. fclose(file);
  73. LogWrite(ZONE__ERROR, 0, "SPGrid", "SPGrid::Init() map contents (%s) do not match its name (%s).", &name, m_ZoneFile.c_str());
  74. return false;
  75. }
  76. // Read the min bounds
  77. fread(&m_MinX, sizeof(float), 1, file);
  78. fread(&m_MinZ, sizeof(float), 1, file);
  79. LogWrite(ZONE__DEBUG, 0, "SPGrid", "minx = %f, minz = %f", m_MinX, m_MinZ);
  80. // Read the max bounds
  81. fread(&m_MaxX, sizeof(float), 1, file);
  82. fread(&m_MaxZ, sizeof(float), 1, file);
  83. LogWrite(ZONE__DEBUG, 0, "SPGrid", "maxx = %f, maxz = %f", m_MaxX, m_MaxZ);
  84. // Calculate how many cells we need
  85. // in both the X and Z direction
  86. float width = m_MaxX - m_MinX;
  87. float height = m_MaxZ - m_MinZ;
  88. m_NumCellsX = ceil(width / m_CellSize);
  89. m_NumCellsZ = ceil(height / m_CellSize);
  90. LogWrite(ZONE__DEBUG, 0, "SPGrid", "CellSize = %u, x cells = %u, z cells = %u", m_CellSize, m_NumCellsX, m_NumCellsZ);
  91. // Allocate all the cells
  92. m_Cells.resize(m_NumCellsZ * m_NumCellsX);
  93. m_NumFaceCellsX = ceil(width / FACECELLSIZEDEFAULT);
  94. m_NumFaceCellsZ = ceil(height / FACECELLSIZEDEFAULT);
  95. m_FaceCells.resize(m_NumFaceCellsX * m_NumFaceCellsZ);
  96. // Read the number of grids
  97. int32 NumGrids;
  98. fread(&NumGrids, sizeof(int32), 1, file);
  99. LogWrite(ZONE__DEBUG, 0, "SPGrid", "NumGrids = %u", NumGrids);
  100. // Loop through the grids loading the face list
  101. for (int32 i = 0; i < NumGrids; i++) {
  102. // Read the grid id
  103. int32 GridID;
  104. fread(&GridID, sizeof(int32), 1, file);
  105. LogWrite(ZONE__DEBUG, 0, "SPGrid", "GridID = %u", GridID);
  106. // Read the number of vertices
  107. int32 NumFaces;
  108. fread(&NumFaces, sizeof(int32), 1, file);
  109. LogWrite(ZONE__DEBUG, 0, "SPGrid", "NumFaces = %u", NumFaces);
  110. // Loop through the vertices list reading
  111. // 3 at a time to creat a triangle (face)
  112. for (int32 y = 0; y < NumFaces; ) {
  113. // Each vertex need an x,y,z coordinate and
  114. // we will be reading 3 to create the face
  115. float x1, x2, x3;
  116. float y1, y2, y3;
  117. float z1, z2, z3;
  118. // Read the first vertex
  119. fread(&x1, sizeof(float), 1, file);
  120. fread(&y1, sizeof(float), 1, file);
  121. fread(&z1, sizeof(float), 1, file);
  122. y++;
  123. // Read the second vertex
  124. fread(&x2, sizeof(float), 1, file);
  125. fread(&y2, sizeof(float), 1, file);
  126. fread(&z2, sizeof(float), 1, file);
  127. y++;
  128. // Read the third (final) vertex
  129. fread(&x3, sizeof(float), 1, file);
  130. fread(&y3, sizeof(float), 1, file);
  131. fread(&z3, sizeof(float), 1, file);
  132. y++;
  133. // Create the face and add it to the grid
  134. Face* face = new Face;
  135. face->Vertex1[0] = x1;
  136. face->Vertex1[1] = y1;
  137. face->Vertex1[2] = z1;
  138. face->Vertex2[0] = x2;
  139. face->Vertex2[1] = y2;
  140. face->Vertex2[2] = z2;
  141. face->Vertex3[0] = x3;
  142. face->Vertex3[1] = y3;
  143. face->Vertex3[2] = z3;
  144. AddFace(face, GridID);
  145. }
  146. }
  147. fclose(file);
  148. /*map<int32, vector<Face*> >::iterator itr;
  149. vector<Face*>::iterator itr2;
  150. for (int32 i = 0; i < m_Cells.size(); i++) {
  151. Cell& cell = m_Cells[i];
  152. for (itr = cell.FaceList.begin(); itr != cell.FaceList.end(); itr++) {
  153. float min_x = 0.0f;
  154. float min_y = 0.0f;
  155. float min_z = 0.0f;
  156. float max_x = 0.0f;
  157. float max_y = 0.0f;
  158. float max_z = 0.0f;
  159. for (itr2 = (*itr).second.begin(); itr2 != (*itr).second.end(); itr2++) {
  160. Face* face = (*itr2);
  161. if (min_x == 0.0f || face->Vertex1[0] < min_x)
  162. min_x = face->Vertex1[0];
  163. if (face->Vertex2[0] < min_x)
  164. min_x = face->Vertex2[0];
  165. if (face->Vertex3[0] < min_x)
  166. min_x = face->Vertex3[0];
  167. if (min_y == 0.0f || face->Vertex1[1] < min_y)
  168. min_y = face->Vertex1[1];
  169. if (face->Vertex2[1] < min_y)
  170. min_y = face->Vertex2[1];
  171. if (face->Vertex3[1] < min_y)
  172. min_y = face->Vertex3[1];
  173. if (min_z == 0.0f || face->Vertex1[2] < min_z)
  174. min_z = face->Vertex1[2];
  175. if (face->Vertex2[2] < min_z)
  176. min_z = face->Vertex2[2];
  177. if (face->Vertex3[2] < min_z)
  178. min_z = face->Vertex3[2];
  179. // Max bounds
  180. if (max_x == 0.0f || face->Vertex1[0] > max_x)
  181. max_x = face->Vertex1[0];
  182. if (face->Vertex2[0] > max_x)
  183. max_x = face->Vertex2[0];
  184. if (face->Vertex3[0] > max_x)
  185. max_x = face->Vertex3[0];
  186. if (max_y == 0.0f || face->Vertex1[1] > max_y)
  187. max_y = face->Vertex1[1];
  188. if (face->Vertex2[1] > max_y)
  189. max_y = face->Vertex2[1];
  190. if (face->Vertex3[1] > max_y)
  191. max_y = face->Vertex3[1];
  192. if (max_z == 0.0f || face->Vertex1[2] > max_z)
  193. max_z = face->Vertex1[2];
  194. if (face->Vertex2[2] > max_z)
  195. max_z = face->Vertex2[2];
  196. if (face->Vertex3[2] > max_z)
  197. max_z = face->Vertex3[2];
  198. }
  199. GridBounds* bounds = new GridBounds;
  200. bounds->MinBounds[0] = min_x;
  201. bounds->MinBounds[1] = min_y;
  202. bounds->MinBounds[2] = min_z;
  203. bounds->MaxBounds[0] = max_x;
  204. bounds->MaxBounds[1] = max_y;
  205. bounds->MaxBounds[2] = max_z;
  206. cell.GridBounds[(*itr).first] = bounds;
  207. }
  208. }*/
  209. return true;
  210. }
  211. Cell* SPGrid::GetCell(int32 x, int32 z) {
  212. if (x >= m_NumCellsX)
  213. x = m_NumCellsX - 1;
  214. if (z >= m_NumCellsZ)
  215. z = m_NumCellsZ - 1;
  216. return &m_Cells[z * m_NumCellsX + x];
  217. }
  218. Cell* SPGrid::GetCell(float x, float z) {
  219. // As cell grid coordinates are all positive we need to
  220. // modify the coordinates by subtracting the min bounds
  221. float newX = x - m_MinX;
  222. float newZ = z - m_MinZ;
  223. // Get the cell coordinates by doing int division
  224. // with the modified coordinates and the cell size
  225. int32 CellX = (int32)(newX / m_CellSize);
  226. int32 CellZ = (int32)(newZ / m_CellSize);
  227. return GetCell(CellX, CellZ);
  228. }
  229. FaceCell* SPGrid::GetFaceCell(int32 x, int32 z) {
  230. if (x >= m_NumFaceCellsX)
  231. x = m_NumFaceCellsX - 1;
  232. if (z >= m_NumFaceCellsZ)
  233. z = m_NumFaceCellsZ - 1;
  234. return &m_FaceCells[z * m_NumFaceCellsX + x];
  235. }
  236. FaceCell* SPGrid::GetFaceCell(float x, float z) {
  237. // As cell grid coordinates are all positive we need to
  238. // modify the coordinates by subtracting the min bounds
  239. float newX = x - m_MinX;
  240. float newZ = z - m_MinZ;
  241. // Get the cell coordinates by doing int division
  242. // with the modified coordinates and the cell size
  243. int32 CellX = (int32)(newX / FACECELLSIZEDEFAULT);
  244. int32 CellZ = (int32)(newZ / FACECELLSIZEDEFAULT);
  245. return GetFaceCell(CellX, CellZ);
  246. }
  247. void SPGrid::AddFace(Face* face, int32 grid) {
  248. // As each face has three vertices we will need to check the cell
  249. // for all of them and add the face to each cell that it is within
  250. // Get the cell at the first vertex position (X and Z, Y is vertical in EQ2)
  251. // as this is the first check we will add it to this cell and compare it
  252. // to the other two cells we get for the other two verticies
  253. FaceCell* cell = GetFaceCell(face->Vertex1[0], face->Vertex1[2]);
  254. cell->FaceList[grid].push_back(face);
  255. // Get the cells for the other two verticies and compare
  256. FaceCell* cell2 = GetFaceCell(face->Vertex2[0], face->Vertex2[2]);
  257. FaceCell* cell3 = GetFaceCell(face->Vertex3[0], face->Vertex3[2]);
  258. // If cell 2 is not the same cell as the original cell then add the face to cell2
  259. if (cell2 != cell)
  260. cell2->FaceList[grid].push_back(face);
  261. // If cell 3 is not the same as the original cell AND not the same as cell 2 then add the face to cell 3
  262. if (cell3 != cell && cell3 != cell2)
  263. cell3->FaceList[grid].push_back(face);
  264. }
  265. float rayIntersectsTriangle(float *p, float *d, float *v0, float *v1, float *v2);
  266. int32 SPGrid::GetGridID(Spawn * spawn) {
  267. FaceCell* cell = GetFaceCell(spawn->GetX(), spawn->GetZ());
  268. /*if (cell->GridBounds.size() == 1)
  269. return cell->FaceList.begin()->first;*/
  270. // Create the starting point for the trace
  271. float point[3];
  272. point[0] = spawn->GetX();
  273. point[1] = spawn->GetY() + 3.0f; // Small bump to make sure we are above ground when we do the trace
  274. point[2] = spawn->GetZ();
  275. // Create the direction for the trace, as we want what
  276. // is below it will just be -1 in the y direction
  277. float direction[3];
  278. direction[0] = 0.0f;
  279. direction[1] = -1.0f;
  280. direction[2] = 0.0f;
  281. float MinDistance = 0.0f;
  282. int32 Grid = 0;
  283. /*map<int32, GridBounds*>::iterator itr;
  284. for (itr = cell->GridBounds.begin(); itr != cell->GridBounds.end(); itr++) {
  285. GridBounds* bounds = (*itr).second;
  286. if (point[0] >= bounds->MinBounds[0] && point[1] >= bounds->MinBounds[1] && point[2] >= bounds->MinBounds[2]
  287. && point[0] <= bounds->MaxBounds[0] && point[1] <= bounds->MaxBounds[1] && point[2] <= bounds->MaxBounds[2]) {
  288. vector<Face*>::iterator itr2;
  289. for (itr2 = cell->FaceList[(*itr).first].begin(); itr2 != cell->FaceList[(*itr).first].end(); itr2++) {
  290. Face* face = *itr2;
  291. float distance;
  292. if ((distance = rayIntersectsTriangle(point, direction, face->Vertex1, face->Vertex2, face->Vertex3)) != 0) {
  293. if (MinDistance == 0.0f || distance < MinDistance) {
  294. MinDistance = distance;
  295. Grid = (*itr).first;
  296. }
  297. }
  298. }
  299. }
  300. }*/
  301. map<int32, vector<Face*> >::iterator mapitr;
  302. for (mapitr = cell->FaceList.begin(); mapitr != cell->FaceList.end(); mapitr++) {
  303. vector<Face*>::iterator itr;
  304. for (itr = (*mapitr).second.begin(); itr != (*mapitr).second.end(); itr++) {
  305. Face* face = *itr;
  306. float distance;
  307. if ((distance = rayIntersectsTriangle(point, direction, face->Vertex1, face->Vertex2, face->Vertex3)) != 0) {
  308. if (MinDistance == 0.0f || distance < MinDistance) {
  309. MinDistance = distance;
  310. Grid = (*mapitr).first;
  311. }
  312. }
  313. }
  314. }
  315. return Grid;
  316. }
  317. void SPGrid::AddSpawn(Spawn * spawn) {
  318. Cell* cell = GetCell(spawn->GetX(), spawn->GetZ());
  319. AddSpawn(spawn, cell);
  320. }
  321. void SPGrid::AddSpawn(Spawn * spawn, Cell * cell) {
  322. cell->SpawnList.push_back(spawn);
  323. spawn->Cell_Info.CurrentCell = cell;
  324. spawn->Cell_Info.CellListIndex = cell->SpawnList.size() - 1;
  325. }
  326. void SPGrid::RemoveSpawnFromCell(Spawn * spawn) {
  327. if (spawn->Cell_Info.CurrentCell) {
  328. vector<Spawn*>& spawns = spawn->Cell_Info.CurrentCell->SpawnList;
  329. // Only do the vector swap if the vector has more than 1 spawn in it
  330. if (spawns.size() > 1) {
  331. // Swap the last spawn in this list to our position and update its stored index to match its new index
  332. spawns[spawn->Cell_Info.CellListIndex] = spawns.back();
  333. spawns[spawn->Cell_Info.CellListIndex]->Cell_Info.CellListIndex = spawn->Cell_Info.CellListIndex;
  334. }
  335. // Remove the last spawn from the list which should now be the spawn passed as a parameter
  336. spawns.pop_back();
  337. // Reset the spawns CellInfo to default values now that it is no longer in a cell
  338. spawn->Cell_Info.CellListIndex = -1;
  339. spawn->Cell_Info.CurrentCell = nullptr;
  340. }
  341. }
  342. /**********************************************************************
  343. Math functions/macros to test a ray intersection in 3D space
  344. **********************************************************************/
  345. /* a = b - c */
  346. #define vector(a,b,c) \
  347. (a)[0] = (b)[0] - (c)[0]; \
  348. (a)[1] = (b)[1] - (c)[1]; \
  349. (a)[2] = (b)[2] - (c)[2];
  350. #define crossProduct(a,b,c) \
  351. (a)[0] = (b)[1] * (c)[2] - (c)[1] * (b)[2]; \
  352. (a)[1] = (b)[2] * (c)[0] - (c)[2] * (b)[0]; \
  353. (a)[2] = (b)[0] * (c)[1] - (c)[0] * (b)[1];
  354. #define innerProduct(v,q) \
  355. ((v)[0] * (q)[0] + \
  356. (v)[1] * (q)[1] + \
  357. (v)[2] * (q)[2])
  358. // all parameters should be vectors (float[3])
  359. float rayIntersectsTriangle(float *p, float *d, float *v0, float *v1, float *v2) {
  360. float e1[3], e2[3], h[3], s[3], q[3];
  361. float a, f, u, v;
  362. vector(e1, v1, v0);
  363. vector(e2, v2, v0);
  364. crossProduct(h, d, e2);
  365. a = innerProduct(e1, h);
  366. if (a > -0.00001 && a < 0.00001)
  367. return 0;
  368. f = 1 / a;
  369. vector(s, p, v0);
  370. u = f * (innerProduct(s, h));
  371. if (u < 0.0 || u > 1.0)
  372. return 0;
  373. crossProduct(q, s, e1);
  374. v = f * innerProduct(d, q);
  375. if (v < 0.0 || u + v > 1.0)
  376. return 0;
  377. // at this stage we can compute t to find out where
  378. // the intersection point is on the line
  379. float t = f * innerProduct(e2, q);
  380. if (t > 0.00001) // ray intersection
  381. return t;
  382. else // this means that there is a line intersection
  383. // but not a ray intersection
  384. return 0;
  385. }