spawn_validator.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #pragma once
  2. #include "../../game/map/terrain.h"
  3. #include <QVector3D>
  4. #include <cstdint>
  5. #include <vector>
  6. namespace Render::Ground {
  7. struct SpawnValidationConfig {
  8. int grid_width = 0;
  9. int grid_height = 0;
  10. float tile_size = 1.0F;
  11. float edge_padding = 0.08F;
  12. float max_slope = 0.65F;
  13. int river_margin = 1;
  14. bool allow_flat = true;
  15. bool allow_hill = false;
  16. bool allow_mountain = false;
  17. bool allow_river = false;
  18. bool check_buildings = true;
  19. bool check_roads = true;
  20. bool check_bridges = true;
  21. bool check_slope = true;
  22. bool check_river_margin = true;
  23. };
  24. struct SpawnTerrainCache {
  25. std::vector<QVector3D> normals;
  26. std::vector<float> heights;
  27. std::vector<Game::Map::TerrainType> terrain_types;
  28. int width = 0;
  29. int height = 0;
  30. float tile_size = 1.0F;
  31. void build_from_height_map(const std::vector<float> &height_data,
  32. const std::vector<Game::Map::TerrainType> &types,
  33. int w, int h, float ts);
  34. [[nodiscard]] auto sample_height_at(float gx, float gz) const -> float;
  35. [[nodiscard]] auto get_slope_at(int grid_x, int grid_z) const -> float;
  36. [[nodiscard]] auto
  37. get_terrain_type_at(int grid_x, int grid_z) const -> Game::Map::TerrainType;
  38. };
  39. class SpawnValidator {
  40. public:
  41. SpawnValidator(const SpawnTerrainCache &cache,
  42. const SpawnValidationConfig &config);
  43. [[nodiscard]] auto can_spawn_at_grid(float gx, float gz) const -> bool;
  44. [[nodiscard]] auto can_spawn_at_world(float world_x,
  45. float world_z) const -> bool;
  46. void grid_to_world(float gx, float gz, float &out_world_x,
  47. float &out_world_z) const;
  48. void world_to_grid(float world_x, float world_z, float &out_gx,
  49. float &out_gz) const;
  50. private:
  51. const SpawnTerrainCache &m_cache;
  52. SpawnValidationConfig m_config;
  53. float m_edge_margin_x = 0.0F;
  54. float m_edge_margin_z = 0.0F;
  55. float m_half_width = 0.0F;
  56. float m_half_height = 0.0F;
  57. [[nodiscard]] auto check_edge_padding(float gx, float gz) const -> bool;
  58. [[nodiscard]] auto check_terrain_type(int grid_x, int grid_z) const -> bool;
  59. [[nodiscard]] auto check_river_margin(int grid_x, int grid_z) const -> bool;
  60. [[nodiscard]] auto check_slope(int grid_x, int grid_z) const -> bool;
  61. [[nodiscard]] auto check_building_collision(float world_x,
  62. float world_z) const -> bool;
  63. [[nodiscard]] auto check_road_collision(float world_x,
  64. float world_z) const -> bool;
  65. [[nodiscard]] auto check_bridge_collision(float world_x,
  66. float world_z) const -> bool;
  67. };
  68. [[nodiscard]] auto make_plant_spawn_config() -> SpawnValidationConfig;
  69. [[nodiscard]] auto make_stone_spawn_config() -> SpawnValidationConfig;
  70. [[nodiscard]] auto make_tree_spawn_config() -> SpawnValidationConfig;
  71. [[nodiscard]] auto make_firecamp_spawn_config() -> SpawnValidationConfig;
  72. [[nodiscard]] auto make_grass_spawn_config() -> SpawnValidationConfig;
  73. } // namespace Render::Ground