SPGrid.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 30
  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. // Adds a face to the cells it needs to be in and sets its GridID
  50. void AddFace(Face* face, int32 grid);
  51. // Checks the faces below the given spawn to determine the GridID
  52. int32 GetGridID(Spawn* spawn);
  53. int32 GetGridIDByLocation(float x, float y, float z);
  54. // Get cell based on cell coordinates
  55. FaceCell* GetFaceCell(int32 x, int32 z);
  56. // Get cell based on world coordinates
  57. FaceCell* GetFaceCell(float x, float z);
  58. float GetBestY(float x, float y, float z);
  59. Face* GetClosestFace(float x, float y, float z);
  60. Face* FindPath(float x, float y, float z, float targX, float targY, float targZ, bool forceEndCell=false);
  61. void InitValues(float minX, float maxX, float minZ, float maxZ, int32 numCellsX, int32 numCellsZ)
  62. {
  63. m_MinX = minX;
  64. m_MaxX = maxX;
  65. m_MinZ = minZ;
  66. m_MaxZ = maxZ;
  67. m_NumCellsX = numCellsX;
  68. m_NumCellsZ = numCellsZ;
  69. float width = m_MaxX - m_MinX;
  70. float height = m_MaxZ - m_MinZ;
  71. m_NumFaceCellsX = ceil(width / CELLSIZEDEFAULT);
  72. m_NumFaceCellsZ = ceil(height / CELLSIZEDEFAULT);
  73. m_FaceCells.resize(m_NumFaceCellsX * m_NumFaceCellsZ);
  74. }
  75. private:
  76. std::vector<FaceCell> m_FaceCells;
  77. string m_ZoneFile;
  78. float m_MinX;
  79. float m_MinZ;
  80. float m_MaxX;
  81. float m_MaxZ;
  82. int32 m_NumCellsX;
  83. int32 m_NumCellsZ;
  84. int32 m_NumFaceCellsX;
  85. int32 m_NumFaceCellsZ;
  86. };