SPGrid.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. int32 GetGridIDByLocation(float x, float y, float z);
  58. // Get cell based on cell coordinates
  59. FaceCell* GetFaceCell(int32 x, int32 z);
  60. // Get cell based on world coordinates
  61. FaceCell* GetFaceCell(float x, float z);
  62. float GetBestY(float x, float y, float z);
  63. Face* GetClosestFace(float x, float y, float z);
  64. Face* FindPath(float x, float y, float z, float targX, float targY, float targZ, bool forceEndCell=false);
  65. void InitValues(float minX, float maxX, float minZ, float maxZ, int32 numCellsX, int32 numCellsZ)
  66. {
  67. m_MinX = minX;
  68. m_MaxX = maxX;
  69. m_MinZ = minZ;
  70. m_MaxZ = maxZ;
  71. m_NumCellsX = numCellsX;
  72. m_NumCellsZ = numCellsZ;
  73. float width = m_MaxX - m_MinX;
  74. float height = m_MaxZ - m_MinZ;
  75. // Allocate all the cells
  76. m_Cells.resize(m_NumCellsZ * m_NumCellsX);
  77. m_NumFaceCellsX = ceil(width / FACECELLSIZEDEFAULT);
  78. m_NumFaceCellsZ = ceil(height / FACECELLSIZEDEFAULT);
  79. m_FaceCells.resize(m_NumFaceCellsX * m_NumFaceCellsZ);
  80. }
  81. private:
  82. // 1-D array for cells as it is better performance wise then 2-D
  83. std::vector<Cell> m_Cells;
  84. std::vector<FaceCell> m_FaceCells;
  85. string m_ZoneFile;
  86. int32 m_CellSize;
  87. float m_MinX;
  88. float m_MinZ;
  89. float m_MaxX;
  90. float m_MaxZ;
  91. int32 m_NumCellsX;
  92. int32 m_NumCellsZ;
  93. int32 m_NumFaceCellsX;
  94. int32 m_NumFaceCellsZ;
  95. };