WorldmapTerrain.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <functional>
  17. #include <vector>
  18. #include <OgreVector3.h>
  19. #include "common/TypeDefine.h"
  20. #include "formats/worldmap/WorldmapFileSerializer.h"
  21. namespace Ogre{
  22. class SceneManager;
  23. class SceneNode;
  24. }
  25. namespace VGears {
  26. /**
  27. * A triangle of the terrain of a world map.
  28. */
  29. struct TerrainTriangle{
  30. /// Corners of the triangle, in world units.
  31. Ogre::Vector3 vertices[3];
  32. /// Type of the terrain.
  33. uint8 walkability_type;
  34. /// Function of the world map script that runs when the triangle is entered, if 3 or more.
  35. uint8 function_id;
  36. /// Column of its mesh in the 9x7 block grid of the world.
  37. int mesh_x;
  38. /// Row of its mesh in the 9x7 block grid of the world.
  39. int mesh_y;
  40. /// Encounter region of the triangle, 0-15.
  41. uint8 region;
  42. };
  43. /**
  44. * The terrain of the loaded world map: its surfaces in the scene, and the search of the ground
  45. * under a position.
  46. */
  47. class WorldmapTerrain{
  48. public:
  49. /**
  50. * Tells if a triangle can be walked, at the height of the searched position.
  51. */
  52. typedef std::function<bool(const TerrainTriangle& triangle, Ogre::Real height)> WalkableCheck;
  53. /**
  54. * Constructor.
  55. */
  56. WorldmapTerrain();
  57. /**
  58. * Destructor. Removes the terrain from the scene.
  59. */
  60. ~WorldmapTerrain();
  61. /**
  62. * Loads the terrain of a map and shows it. {@see Clear} must be called before loading another one.
  63. *
  64. * @param[in] scene_manager Scene where the terrain is added.
  65. * @param[in] map_id 0 is WM0, 1 WM2 and 2 WM3.
  66. */
  67. void Load(Ogre::SceneManager* scene_manager, const int map_id);
  68. /**
  69. * Removes the surfaces and the triangles of the loaded map.
  70. */
  71. void Clear();
  72. /**
  73. * Shows or hides the terrain.
  74. *
  75. * @param[in] visible True to show it.
  76. */
  77. void SetVisible(const bool visible);
  78. /**
  79. * Searches the ground under a position: of the triangles over or under it, the one with the closest
  80. * height.
  81. *
  82. * @param[in] position Position to search at.
  83. * @param[out] surface The position on the ground. Only set if there is a triangle.
  84. * @param[in] grass_only True to only search grass.
  85. * @param[in] check Tells if the triangle can be walked. If empty, any triangle can.
  86. * @param[out] walkability_type Type of the terrain found. Can be nullptr.
  87. * @param[out] triangle_index Index of the triangle found. Can be nullptr.
  88. * @return True if the closest triangle can be walked.
  89. */
  90. bool FindSurface(
  91. const Ogre::Vector3& position, Ogre::Vector3& surface, bool grass_only,
  92. const WalkableCheck& check, uint8* walkability_type, size_t* triangle_index
  93. ) const;
  94. /**
  95. * Gets the triangles of the loaded map.
  96. */
  97. const std::vector<TerrainTriangle>& GetTriangles() const;
  98. /**
  99. * Gets the center of the loaded map, at height 0.
  100. */
  101. Ogre::Vector3 GetCenter() const;
  102. private:
  103. /**
  104. * Groups the triangles by the cell of a grid they touch, so the search of the triangle under a
  105. * position only checks a few of them.
  106. */
  107. void BuildIndex();
  108. std::vector<TerrainTriangle> triangles_;
  109. /// Indices of the triangles (ascending) that touch each cell of the grid, row by row.
  110. std::vector<std::vector<uint32>> terrain_grid_;
  111. float terrain_grid_min_x_ = 0.0f;
  112. float terrain_grid_min_z_ = 0.0f;
  113. float terrain_grid_cell_ = 1.0f;
  114. int terrain_grid_columns_ = 0;
  115. int terrain_grid_rows_ = 0;
  116. Ogre::SceneManager* scene_manager_ = nullptr;
  117. Ogre::SceneNode* world_map_node_ = nullptr;
  118. /// Size of the map in blocks.
  119. size_t grid_width_ = 9;
  120. size_t grid_height_ = 7;
  121. /// Position of the first block of the map in the 9x7 block grid of the world.
  122. size_t origin_x_ = 0;
  123. size_t origin_y_ = 0;
  124. };
  125. }