Skip to content

Commit

Permalink
gamestate: Create grids for all existing path types.
Browse files Browse the repository at this point in the history
  • Loading branch information
heinezen committed Sep 8, 2024
1 parent 347d586 commit eb62b8c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 41 deletions.
2 changes: 1 addition & 1 deletion libopenage/gamestate/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void Game::generate_terrain(const std::shared_ptr<TerrainFactory> &terrain_facto

auto terrain = terrain_factory->add_terrain({20, 20}, {chunk0, chunk1, chunk2, chunk3});

auto map = std::make_shared<Map>(terrain);
auto map = std::make_shared<Map>(this->state, terrain);
this->state->set_map(map);
}

Expand Down
35 changes: 15 additions & 20 deletions libopenage/gamestate/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <nyan/nyan.h>

#include "gamestate/api/terrain.h"
#include "gamestate/game_state.h"
#include "gamestate/terrain.h"
#include "gamestate/terrain_chunk.h"
#include "pathfinding/cost_field.h"
Expand All @@ -14,31 +15,25 @@


namespace openage::gamestate {
Map::Map(const std::shared_ptr<Terrain> &terrain) :
Map::Map(const std::shared_ptr<GameState> &state,
const std::shared_ptr<Terrain> &terrain) :
terrain{terrain},
pathfinder{std::make_shared<path::Pathfinder>()},
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<nyan::fqon_t> 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<path::Grid>(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<path::Grid>(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
Expand Down
5 changes: 4 additions & 1 deletion libopenage/gamestate/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Pathfinder;
} // namespace path

namespace gamestate {
class GameState;
class Terrain;

class Map {
Expand All @@ -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> &terrain);
Map(const std::shared_ptr<GameState> &state,
const std::shared_ptr<Terrain> &terrain);

~Map() = default;

Expand Down
9 changes: 2 additions & 7 deletions libopenage/gamestate/terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<TerrainChunk> &chunk) {
Expand Down
13 changes: 3 additions & 10 deletions libopenage/gamestate/terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions libopenage/pathfinding/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit eb62b8c

Please sign in to comment.