spawn_validator.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "spawn_validator.h"
  2. #include "../../game/map/terrain_service.h"
  3. #include "../../game/systems/building_collision_registry.h"
  4. #include <algorithm>
  5. #include <cmath>
  6. namespace Render::Ground {
  7. void SpawnTerrainCache::build_from_height_map(
  8. const std::vector<float> &height_data,
  9. const std::vector<Game::Map::TerrainType> &types, int w, int h, float ts) {
  10. width = w;
  11. height = h;
  12. tile_size = ts;
  13. heights = height_data;
  14. terrain_types = types;
  15. normals.resize(static_cast<size_t>(width * height),
  16. QVector3D(0.0F, 1.0F, 0.0F));
  17. if (width < 2 || height < 2 || heights.empty()) {
  18. return;
  19. }
  20. for (int z = 0; z < height; ++z) {
  21. for (int x = 0; x < width; ++x) {
  22. int const idx = z * width + x;
  23. float const gx0 = std::clamp(static_cast<float>(x) - 1.0F, 0.0F,
  24. static_cast<float>(width - 1));
  25. float const gx1 = std::clamp(static_cast<float>(x) + 1.0F, 0.0F,
  26. static_cast<float>(width - 1));
  27. float const gz0 = std::clamp(static_cast<float>(z) - 1.0F, 0.0F,
  28. static_cast<float>(height - 1));
  29. float const gz1 = std::clamp(static_cast<float>(z) + 1.0F, 0.0F,
  30. static_cast<float>(height - 1));
  31. float const h_l = sample_height_at(gx0, static_cast<float>(z));
  32. float const h_r = sample_height_at(gx1, static_cast<float>(z));
  33. float const h_d = sample_height_at(static_cast<float>(x), gz0);
  34. float const h_u = sample_height_at(static_cast<float>(x), gz1);
  35. QVector3D const dx(2.0F * tile_size, h_r - h_l, 0.0F);
  36. QVector3D const dz(0.0F, h_u - h_d, 2.0F * tile_size);
  37. QVector3D n = QVector3D::crossProduct(dz, dx);
  38. if (n.lengthSquared() > 0.0F) {
  39. n.normalize();
  40. } else {
  41. n = QVector3D(0, 1, 0);
  42. }
  43. normals[static_cast<size_t>(idx)] = n;
  44. }
  45. }
  46. }
  47. auto SpawnTerrainCache::sample_height_at(float gx, float gz) const -> float {
  48. if (heights.empty() || width < 1 || height < 1) {
  49. return 0.0F;
  50. }
  51. gx = std::clamp(gx, 0.0F, static_cast<float>(width - 1));
  52. gz = std::clamp(gz, 0.0F, static_cast<float>(height - 1));
  53. int const x0 = static_cast<int>(std::floor(gx));
  54. int const z0 = static_cast<int>(std::floor(gz));
  55. int const x1 = std::min(x0 + 1, width - 1);
  56. int const z1 = std::min(z0 + 1, height - 1);
  57. float const tx = gx - static_cast<float>(x0);
  58. float const tz = gz - static_cast<float>(z0);
  59. float const h00 = heights[static_cast<size_t>(z0 * width + x0)];
  60. float const h10 = heights[static_cast<size_t>(z0 * width + x1)];
  61. float const h01 = heights[static_cast<size_t>(z1 * width + x0)];
  62. float const h11 = heights[static_cast<size_t>(z1 * width + x1)];
  63. float const h0 = h00 * (1.0F - tx) + h10 * tx;
  64. float const h1 = h01 * (1.0F - tx) + h11 * tx;
  65. return h0 * (1.0F - tz) + h1 * tz;
  66. }
  67. auto SpawnTerrainCache::get_slope_at(int grid_x, int grid_z) const -> float {
  68. if (normals.empty() || grid_x < 0 || grid_x >= width || grid_z < 0 ||
  69. grid_z >= height) {
  70. return 0.0F;
  71. }
  72. int const idx = grid_z * width + grid_x;
  73. QVector3D const normal = normals[static_cast<size_t>(idx)];
  74. return 1.0F - std::clamp(normal.y(), 0.0F, 1.0F);
  75. }
  76. auto SpawnTerrainCache::get_terrain_type_at(int grid_x, int grid_z) const
  77. -> Game::Map::TerrainType {
  78. if (terrain_types.empty() || grid_x < 0 || grid_x >= width || grid_z < 0 ||
  79. grid_z >= height) {
  80. return Game::Map::TerrainType::Flat;
  81. }
  82. int const idx = grid_z * width + grid_x;
  83. return terrain_types[static_cast<size_t>(idx)];
  84. }
  85. SpawnValidator::SpawnValidator(const SpawnTerrainCache &cache,
  86. const SpawnValidationConfig &config)
  87. : m_cache(cache), m_config(config) {
  88. float const edge_padding = std::clamp(m_config.edge_padding, 0.0F, 0.5F);
  89. m_edge_margin_x = static_cast<float>(m_config.grid_width) * edge_padding;
  90. m_edge_margin_z = static_cast<float>(m_config.grid_height) * edge_padding;
  91. m_half_width = static_cast<float>(m_config.grid_width) * 0.5F - 0.5F;
  92. m_half_height = static_cast<float>(m_config.grid_height) * 0.5F - 0.5F;
  93. }
  94. auto SpawnValidator::can_spawn_at_grid(float gx, float gz) const -> bool {
  95. if (!check_edge_padding(gx, gz)) {
  96. return false;
  97. }
  98. float const sgx =
  99. std::clamp(gx, 0.0F, static_cast<float>(m_config.grid_width - 1));
  100. float const sgz =
  101. std::clamp(gz, 0.0F, static_cast<float>(m_config.grid_height - 1));
  102. int const grid_x = std::clamp(static_cast<int>(std::floor(sgx + 0.5F)), 0,
  103. m_config.grid_width - 1);
  104. int const grid_z = std::clamp(static_cast<int>(std::floor(sgz + 0.5F)), 0,
  105. m_config.grid_height - 1);
  106. if (!check_terrain_type(grid_x, grid_z)) {
  107. return false;
  108. }
  109. if (m_config.check_river_margin && !check_river_margin(grid_x, grid_z)) {
  110. return false;
  111. }
  112. if (m_config.check_slope && !check_slope(grid_x, grid_z)) {
  113. return false;
  114. }
  115. float world_x = 0.0F;
  116. float world_z = 0.0F;
  117. grid_to_world(gx, gz, world_x, world_z);
  118. if (m_config.check_buildings && !check_building_collision(world_x, world_z)) {
  119. return false;
  120. }
  121. if (m_config.check_roads && !check_road_collision(world_x, world_z)) {
  122. return false;
  123. }
  124. if (m_config.check_bridges && !check_bridge_collision(world_x, world_z)) {
  125. return false;
  126. }
  127. return true;
  128. }
  129. auto SpawnValidator::can_spawn_at_world(float world_x,
  130. float world_z) const -> bool {
  131. float gx = 0.0F;
  132. float gz = 0.0F;
  133. world_to_grid(world_x, world_z, gx, gz);
  134. return can_spawn_at_grid(gx, gz);
  135. }
  136. void SpawnValidator::grid_to_world(float gx, float gz, float &out_world_x,
  137. float &out_world_z) const {
  138. out_world_x = (gx - m_half_width) * m_config.tile_size;
  139. out_world_z = (gz - m_half_height) * m_config.tile_size;
  140. }
  141. void SpawnValidator::world_to_grid(float world_x, float world_z, float &out_gx,
  142. float &out_gz) const {
  143. out_gx = world_x / m_config.tile_size + m_half_width;
  144. out_gz = world_z / m_config.tile_size + m_half_height;
  145. }
  146. auto SpawnValidator::check_edge_padding(float gx, float gz) const -> bool {
  147. if (gx < m_edge_margin_x ||
  148. gx > static_cast<float>(m_config.grid_width - 1) - m_edge_margin_x) {
  149. return false;
  150. }
  151. if (gz < m_edge_margin_z ||
  152. gz > static_cast<float>(m_config.grid_height - 1) - m_edge_margin_z) {
  153. return false;
  154. }
  155. return true;
  156. }
  157. auto SpawnValidator::check_terrain_type(int grid_x, int grid_z) const -> bool {
  158. Game::Map::TerrainType const terrain_type =
  159. m_cache.get_terrain_type_at(grid_x, grid_z);
  160. switch (terrain_type) {
  161. case Game::Map::TerrainType::Flat:
  162. return m_config.allow_flat;
  163. case Game::Map::TerrainType::Hill:
  164. return m_config.allow_hill;
  165. case Game::Map::TerrainType::Mountain:
  166. return m_config.allow_mountain;
  167. case Game::Map::TerrainType::River:
  168. return m_config.allow_river;
  169. case Game::Map::TerrainType::Forest:
  170. return m_config.allow_flat;
  171. }
  172. return m_config.allow_flat;
  173. }
  174. auto SpawnValidator::check_river_margin(int grid_x, int grid_z) const -> bool {
  175. int const margin = m_config.river_margin;
  176. for (int dz = -margin; dz <= margin; ++dz) {
  177. for (int dx = -margin; dx <= margin; ++dx) {
  178. if (dx == 0 && dz == 0) {
  179. continue;
  180. }
  181. int const nx = grid_x + dx;
  182. int const nz = grid_z + dz;
  183. if (nx >= 0 && nx < m_config.grid_width && nz >= 0 &&
  184. nz < m_config.grid_height) {
  185. if (m_cache.get_terrain_type_at(nx, nz) ==
  186. Game::Map::TerrainType::River) {
  187. return false;
  188. }
  189. }
  190. }
  191. }
  192. return true;
  193. }
  194. auto SpawnValidator::check_slope(int grid_x, int grid_z) const -> bool {
  195. float const slope = m_cache.get_slope_at(grid_x, grid_z);
  196. return slope <= m_config.max_slope;
  197. }
  198. auto SpawnValidator::check_building_collision(float world_x,
  199. float world_z) const -> bool {
  200. auto &building_registry =
  201. Game::Systems::BuildingCollisionRegistry::instance();
  202. return !building_registry.is_point_in_building(world_x, world_z);
  203. }
  204. auto SpawnValidator::check_road_collision(float world_x,
  205. float world_z) const -> bool {
  206. auto &terrain_service = Game::Map::TerrainService::instance();
  207. return !terrain_service.is_point_on_road(world_x, world_z);
  208. }
  209. auto SpawnValidator::check_bridge_collision(float world_x,
  210. float world_z) const -> bool {
  211. auto &terrain_service = Game::Map::TerrainService::instance();
  212. return !terrain_service.is_on_bridge(world_x, world_z);
  213. }
  214. auto make_plant_spawn_config() -> SpawnValidationConfig {
  215. SpawnValidationConfig config;
  216. config.edge_padding = 0.08F;
  217. config.max_slope = 0.65F;
  218. config.river_margin = 1;
  219. config.allow_flat = true;
  220. config.allow_hill = false;
  221. config.allow_mountain = false;
  222. config.allow_river = false;
  223. config.check_buildings = true;
  224. config.check_roads = true;
  225. config.check_slope = true;
  226. config.check_river_margin = true;
  227. return config;
  228. }
  229. auto make_stone_spawn_config() -> SpawnValidationConfig {
  230. SpawnValidationConfig config;
  231. config.edge_padding = 0.08F;
  232. config.max_slope = 0.15F;
  233. config.river_margin = 1;
  234. config.allow_flat = true;
  235. config.allow_hill = false;
  236. config.allow_mountain = false;
  237. config.allow_river = false;
  238. config.check_buildings = true;
  239. config.check_roads = false;
  240. config.check_slope = true;
  241. config.check_river_margin = true;
  242. return config;
  243. }
  244. auto make_tree_spawn_config() -> SpawnValidationConfig {
  245. SpawnValidationConfig config;
  246. config.edge_padding = 0.08F;
  247. config.max_slope = 0.75F;
  248. config.river_margin = 1;
  249. config.allow_flat = true;
  250. config.allow_hill = false;
  251. config.allow_mountain = false;
  252. config.allow_river = false;
  253. config.check_buildings = true;
  254. config.check_roads = true;
  255. config.check_slope = true;
  256. config.check_river_margin = true;
  257. return config;
  258. }
  259. auto make_firecamp_spawn_config() -> SpawnValidationConfig {
  260. SpawnValidationConfig config;
  261. config.edge_padding = 0.08F;
  262. config.max_slope = 0.30F;
  263. config.river_margin = 0;
  264. config.allow_flat = true;
  265. config.allow_hill = true;
  266. config.allow_mountain = false;
  267. config.allow_river = false;
  268. config.check_buildings = true;
  269. config.check_roads = true;
  270. config.check_slope = true;
  271. config.check_river_margin = false;
  272. return config;
  273. }
  274. auto make_grass_spawn_config() -> SpawnValidationConfig {
  275. SpawnValidationConfig config;
  276. config.edge_padding = 0.08F;
  277. config.max_slope = 0.92F;
  278. config.river_margin = 1;
  279. config.allow_flat = true;
  280. config.allow_hill = false;
  281. config.allow_mountain = false;
  282. config.allow_river = false;
  283. config.check_buildings = true;
  284. config.check_roads = true;
  285. config.check_slope = true;
  286. config.check_river_margin = true;
  287. return config;
  288. }
  289. } // namespace Render::Ground