From eb62b8ce432c3209cabdb4b52c8e2d24bd184c5b Mon Sep 17 00:00:00 2001 From: heinezen Date: Sun, 8 Sep 2024 22:32:05 +0200 Subject: [PATCH] gamestate: Create grids for all existing path types. --- libopenage/gamestate/game.cpp | 2 +- libopenage/gamestate/map.cpp | 35 ++++++++++++++------------------ libopenage/gamestate/map.h | 5 ++++- libopenage/gamestate/terrain.cpp | 9 ++------ libopenage/gamestate/terrain.h | 13 +++--------- libopenage/pathfinding/grid.h | 4 ++-- 6 files changed, 27 insertions(+), 41 deletions(-) diff --git a/libopenage/gamestate/game.cpp b/libopenage/gamestate/game.cpp index db50f8cc72..e63499d769 100644 --- a/libopenage/gamestate/game.cpp +++ b/libopenage/gamestate/game.cpp @@ -143,7 +143,7 @@ void Game::generate_terrain(const std::shared_ptr &terrain_facto auto terrain = terrain_factory->add_terrain({20, 20}, {chunk0, chunk1, chunk2, chunk3}); - auto map = std::make_shared(terrain); + auto map = std::make_shared(this->state, terrain); this->state->set_map(map); } diff --git a/libopenage/gamestate/map.cpp b/libopenage/gamestate/map.cpp index 3d9af584cb..1de5b0fe27 100644 --- a/libopenage/gamestate/map.cpp +++ b/libopenage/gamestate/map.cpp @@ -5,6 +5,7 @@ #include #include "gamestate/api/terrain.h" +#include "gamestate/game_state.h" #include "gamestate/terrain.h" #include "gamestate/terrain_chunk.h" #include "pathfinding/cost_field.h" @@ -14,31 +15,25 @@ namespace openage::gamestate { -Map::Map(const std::shared_ptr &terrain) : +Map::Map(const std::shared_ptr &state, + const std::shared_ptr &terrain) : terrain{terrain}, pathfinder{std::make_shared()}, grid_lookup{} { - // Get grid types - // TODO: This should probably not be dependent on the existing tiles, but - // on all defined nyan PathType objects + // Create a grid for each path type + // TODO: This is non-deterministic because of the unordered set. Is this a problem? + auto nyan_db = state->get_db_view(); + std::unordered_set path_types = nyan_db->get_obj_children_all("engine.util.path_type.PathType"); size_t grid_idx = 0; auto chunk_size = this->terrain->get_chunk(0)->get_size(); - auto side_length = std::max(chunk_size[0], chunk_size[0]); - util::Vector2s grid_size{this->terrain->get_row_size(), this->terrain->get_column_size()}; - for (const auto &chunk : this->terrain->get_chunks()) { - for (const auto &tile : chunk->get_tiles()) { - auto path_costs = api::APITerrain::get_path_costs(tile.terrain); - - for (const auto &path_cost : path_costs) { - if (not this->grid_lookup.contains(path_cost.first)) { - auto grid = std::make_shared(grid_idx, grid_size, side_length); - this->pathfinder->add_grid(grid); - - this->grid_lookup.emplace(path_cost.first, grid_idx); - grid_idx += 1; - } - } - } + auto side_length = std::max(chunk_size[0], chunk_size[1]); + auto grid_size = this->terrain->get_chunks_size(); + for (const auto &path_type : path_types) { + auto grid = std::make_shared(grid_idx, grid_size, side_length); + this->pathfinder->add_grid(grid); + + this->grid_lookup.emplace(path_type, grid_idx); + grid_idx += 1; } // Set path costs diff --git a/libopenage/gamestate/map.h b/libopenage/gamestate/map.h index 4033373023..2817f74457 100644 --- a/libopenage/gamestate/map.h +++ b/libopenage/gamestate/map.h @@ -17,6 +17,7 @@ class Pathfinder; } // namespace path namespace gamestate { +class GameState; class Terrain; class Map { @@ -26,9 +27,11 @@ class Map { * * Initializes the pathfinder with the terrain path costs. * + * @param state Game state. * @param terrain Terrain object. */ - Map(const std::shared_ptr &terrain); + Map(const std::shared_ptr &state, + const std::shared_ptr &terrain); ~Map() = default; diff --git a/libopenage/gamestate/terrain.cpp b/libopenage/gamestate/terrain.cpp index e7d078f2c5..97c5ef5b51 100644 --- a/libopenage/gamestate/terrain.cpp +++ b/libopenage/gamestate/terrain.cpp @@ -78,14 +78,9 @@ const util::Vector2s &Terrain::get_size() const { return this->size; } -size_t Terrain::get_row_size() const { +const util::Vector2s Terrain::get_chunks_size() const { auto chunk_size = this->chunks[0]->get_size(); - return this->size[0] / chunk_size[0]; -} - -size_t Terrain::get_column_size() const { - auto chunk_size = this->chunks[0]->get_size(); - return this->size[1] / chunk_size[1]; + return {this->size[0] / chunk_size[0], this->size[1] / chunk_size[1]}; } void Terrain::add_chunk(const std::shared_ptr &chunk) { diff --git a/libopenage/gamestate/terrain.h b/libopenage/gamestate/terrain.h index 1a5db1bf95..d77eaf939c 100644 --- a/libopenage/gamestate/terrain.h +++ b/libopenage/gamestate/terrain.h @@ -52,18 +52,11 @@ class Terrain { const util::Vector2s &get_size() const; /** - * Get the size of a row in the terrain. + * Get the size of the terrain (in chunks). * - * @return Row size (width). + * @return Terrain chunk size (width x height). */ - size_t get_row_size() const; - - /** - * Get the size of a column in the terrain. - * - * @return Column size (height). - */ - size_t get_column_size() const; + const util::Vector2s get_chunks_size() const; /** * Add a chunk to the terrain. diff --git a/libopenage/pathfinding/grid.h b/libopenage/pathfinding/grid.h index 8f34a7643c..8f24743433 100644 --- a/libopenage/pathfinding/grid.h +++ b/libopenage/pathfinding/grid.h @@ -23,7 +23,7 @@ class Grid { * Create a new empty grid of width x height sectors with a specified size. * * @param id ID of the grid. - * @param size Size of the grid (width x height). + * @param size Size of the grid in sectors (width x height). * @param sector_size Side length of each sector. */ Grid(grid_id_t id, @@ -34,7 +34,7 @@ class Grid { * Create a grid of width x height sectors from a list of existing sectors. * * @param id ID of the grid. - * @param size Size of the grid (width x height). + * @param size Size of the grid in sectors (width x height). * @param sectors Existing sectors. */ Grid(grid_id_t id,