SPGrid.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #pragma once
  17. #include <vector>
  18. #include <map>
  19. #include "../../common/types.h"
  20. #include <cmath>
  21. class Spawn;
  22. #define CELLSIZEDEFAULT 150
  23. #define FACECELLSIZEDEFAULT 15
  24. struct Face {
  25. float Vertex1[3];
  26. float Vertex2[3];
  27. float Vertex3[3];
  28. int32 grid_id;
  29. };
  30. /*struct GridBounds {
  31. int32 Grid;
  32. float MinBounds[3];
  33. float MaxBounds[3];
  34. };*/
  35. struct Cell {
  36. std::vector<Spawn*> SpawnList;
  37. //std::map<int32, vector<Face*> > FaceList;
  38. //std::map<int32, GridBounds*> GridBounds;
  39. };
  40. struct FaceCell {
  41. std::map<int32, vector<Face*> > FaceList;
  42. };
  43. class SPGrid {
  44. public:
  45. SPGrid(string file, int32 cellSize);
  46. ~SPGrid();
  47. // Sets up the spacial partioning grid
  48. bool Init();
  49. // Get cell based on cell coordinates
  50. Cell* GetCell(int32 x, int32 z);
  51. // Get cell based on world coordinates
  52. Cell* GetCell(float x, float z);
  53. // Adds a face to the cells it needs to be in and sets its GridID
  54. void AddFace(Face* face, int32 grid);
  55. // Checks the faces below the given spawn to determine the GridID
  56. int32 GetGridID(Spawn* spawn);
  57. // Calculate a cell from spawn's position and add the spawn to it
  58. void AddSpawn(Spawn* spawn);
  59. // Adds the spawn to a specific spell
  60. void AddSpawn(Spawn* spawn, Cell* cell);
  61. // Removes the spawn from the cell it is currently in
  62. void RemoveSpawnFromCell(Spawn* spawn);
  63. // Get cell based on cell coordinates
  64. FaceCell* GetFaceCell(int32 x, int32 z);
  65. // Get cell based on world coordinates
  66. FaceCell* GetFaceCell(float x, float z);
  67. float GetBestY(float x, float y, float z);
  68. Face* GetClosestFace(float x, float y, float z);
  69. Face* FindPath(float x, float y, float z, float targX, float targY, float targZ, bool forceEndCell=false);
  70. void InitValues(float minX, float maxX, float minZ, float maxZ, int32 numCellsX, int32 numCellsZ)
  71. {
  72. m_MinX = minX;
  73. m_MaxX = maxX;
  74. m_MinZ = minZ;
  75. m_MaxZ = maxZ;
  76. m_NumCellsX = numCellsX;
  77. m_NumCellsZ = numCellsZ;
  78. float width = m_MaxX - m_MinX;
  79. float height = m_MaxZ - m_MinZ;
  80. // Allocate all the cells
  81. m_Cells.resize(m_NumCellsZ * m_NumCellsX);
  82. m_NumFaceCellsX = ceil(width / FACECELLSIZEDEFAULT);
  83. m_NumFaceCellsZ = ceil(height / FACECELLSIZEDEFAULT);
  84. m_FaceCells.resize(m_NumFaceCellsX * m_NumFaceCellsZ);
  85. }
  86. private:
  87. // 1-D array for cells as it is better performance wise then 2-D
  88. std::vector<Cell> m_Cells;
  89. std::vector<FaceCell> m_FaceCells;
  90. string m_ZoneFile;
  91. int32 m_CellSize;
  92. float m_MinX;
  93. float m_MinZ;
  94. float m_MaxX;
  95. float m_MaxZ;
  96. int32 m_NumCellsX;
  97. int32 m_NumCellsZ;
  98. int32 m_NumFaceCellsX;
  99. int32 m_NumFaceCellsZ;
  100. };