Skip to content

Commit

Permalink
gamestate: Use nyan terrain definition for modpacks without terrain g…
Browse files Browse the repository at this point in the history
…raphics.
  • Loading branch information
heinezen committed Jul 28, 2024
1 parent 07da405 commit da96a11
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 38 deletions.
32 changes: 14 additions & 18 deletions libopenage/gamestate/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ Map::Map(const std::shared_ptr<Terrain> &terrain) :
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()) {
if (tile.terrain != std::nullopt) {
auto path_costs = api::APITerrain::get_path_costs(*tile.terrain);
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);
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;
}
this->grid_lookup.emplace(path_cost.first, grid_idx);
grid_idx += 1;
}
}
}
Expand All @@ -48,17 +46,15 @@ Map::Map(const std::shared_ptr<Terrain> &terrain) :
auto chunk_terrain = this->terrain->get_chunk(chunk_idx);
for (size_t tile_idx = 0; tile_idx < chunk_terrain->get_tiles().size(); ++tile_idx) {
auto tile = chunk_terrain->get_tile(tile_idx);
if (tile.terrain != std::nullopt) {
auto path_costs = api::APITerrain::get_path_costs(*tile.terrain);
auto path_costs = api::APITerrain::get_path_costs(tile.terrain);

for (const auto &path_cost : path_costs) {
auto grid_id = this->grid_lookup.at(path_cost.first);
auto grid = this->pathfinder->get_grid(grid_id);
for (const auto &path_cost : path_costs) {
auto grid_id = this->grid_lookup.at(path_cost.first);
auto grid = this->pathfinder->get_grid(grid_id);

auto sector = grid->get_sector(chunk_idx);
auto cost_field = sector->get_cost_field();
cost_field->set_cost(tile_idx, path_cost.second);
}
auto sector = grid->get_sector(chunk_idx);
auto cost_field = sector->get_cost_field();
cost_field->set_cost(tile_idx, path_cost.second);
}
}
}
Expand Down
56 changes: 39 additions & 17 deletions libopenage/gamestate/terrain_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,30 @@ static const std::vector<std::string> test_terrain_paths = {
"../test/textures/test_terrain2.terrain",
};

static const std::vector<nyan::fqon_t> aoe1_test_terrain = {};
static const std::vector<nyan::fqon_t> de1_test_terrain = {};
static const std::vector<nyan::fqon_t> aoe1_test_terrain = {
"aoe1_base.data.terrain.forest.forest.Forest",
"aoe1_base.data.terrain.grass.grass.Grass",
"aoe1_base.data.terrain.dirt.dirt.Dirt",
"aoe1_base.data.terrain.water.water.Water",
};
static const std::vector<nyan::fqon_t> de1_test_terrain = {
"aoe1_base.data.terrain.desert.desert.Desert",
"aoe1_base.data.terrain.grass.grass.Grass",
"aoe1_base.data.terrain.dirt.dirt.Dirt",
"aoe1_base.data.terrain.water.water.Water",
};
static const std::vector<nyan::fqon_t> aoe2_test_terrain = {
"aoe2_base.data.terrain.foundation.foundation.Foundation",
"aoe2_base.data.terrain.grass.grass.Grass",
"aoe2_base.data.terrain.dirt.dirt.Dirt",
"aoe2_base.data.terrain.water.water.Water",
};
static const std::vector<nyan::fqon_t> de2_test_terrain = {};
static const std::vector<nyan::fqon_t> de2_test_terrain = {
"de2_base.data.terrain.foundation.foundation.Foundation",
"de2_base.data.terrain.grass.grass.Grass",
"de2_base.data.terrain.dirt.dirt.Dirt",
"de2_base.data.terrain.water.water.Water",
};
static const std::vector<nyan::fqon_t> hd_test_terrain = {
"hd_base.data.terrain.foundation.foundation.Foundation",
"hd_base.data.terrain.grass.grass.Grass",
Expand All @@ -47,10 +62,16 @@ static const std::vector<nyan::fqon_t> swgb_test_terrain = {
"swgb_base.data.terrain.desert0.desert0.Desert0",
"swgb_base.data.terrain.water1.water1.Water1",
};
static const std::vector<nyan::fqon_t> trial_test_terrain = {};
static const std::vector<nyan::fqon_t> trial_test_terrain = {
"trial_base.data.terrain.foundation.foundation.Foundation",
"trial_base.data.terrain.grass.grass.Grass",
"trial_base.data.terrain.dirt.dirt.Dirt",
"trial_base.data.terrain.water.water.Water",
};

// TODO: Remove hardcoded test texture references
static std::vector<nyan::fqon_t> test_terrains; // declare static so we only have to do this once
static bool has_graphics = false;

void build_test_terrains(const std::shared_ptr<GameState> &gstate) {
auto modpack_ids = gstate->get_mod_manager()->get_load_order();
Expand All @@ -59,36 +80,43 @@ void build_test_terrains(const std::shared_ptr<GameState> &gstate) {
test_terrains.insert(test_terrains.end(),
aoe1_test_terrain.begin(),
aoe1_test_terrain.end());
has_graphics = false;
}
else if (modpack_id == "de1_base") {
test_terrains.insert(test_terrains.end(),
de1_test_terrain.begin(),
de1_test_terrain.end());
has_graphics = false;
}
else if (modpack_id == "aoe2_base") {
test_terrains.insert(test_terrains.end(),
aoe2_test_terrain.begin(),
aoe2_test_terrain.end());
has_graphics = true;
}
else if (modpack_id == "de2_base") {
test_terrains.insert(test_terrains.end(),
de2_test_terrain.begin(),
de2_test_terrain.end());
has_graphics = false;
}
else if (modpack_id == "hd_base") {
test_terrains.insert(test_terrains.end(),
hd_test_terrain.begin(),
hd_test_terrain.end());
has_graphics = true;
}
else if (modpack_id == "swgb_base") {
test_terrains.insert(test_terrains.end(),
swgb_test_terrain.begin(),
swgb_test_terrain.end());
has_graphics = true;
}
else if (modpack_id == "trial_base") {
test_terrains.insert(test_terrains.end(),
trial_test_terrain.begin(),
trial_test_terrain.end());
has_graphics = true;
}
}
}
Expand Down Expand Up @@ -185,7 +213,7 @@ std::shared_ptr<TerrainChunk> TerrainFactory::add_chunk(const std::shared_ptr<Ga

// TODO: Remove test texture references
// ==========
std::optional<nyan::Object> terrain_obj;
nyan::Object terrain_obj;
if (test_terrains.empty()) {
build_test_terrains(gstate);
}
Expand All @@ -204,20 +232,14 @@ std::shared_ptr<TerrainChunk> TerrainFactory::add_chunk(const std::shared_ptr<Ga
for (size_t i = 0; i < size[0] * size[1]; ++i) {
size_t terrain_index = layout_chunks.at(test_chunk_index).at(i);
terrain_obj = gstate->get_db_view()->get_object(test_terrains.at(terrain_index));
terrain_info_path = api::APITerrain::get_terrain_path(terrain_obj.value());
tiles.push_back({terrain_obj, terrain_info_path, terrain_elevation_t::zero()});
}

test_chunk_index += 1;
}
else {
// use a test texture
if (test_chunk_index >= test_terrain_paths.size()) {
test_chunk_index = 0;
}
terrain_info_path = test_terrain_paths.at(test_chunk_index);
if (has_graphics) {
terrain_info_path = api::APITerrain::get_terrain_path(terrain_obj);
}
else {
terrain_info_path = test_terrain_paths.at(test_chunk_index % test_terrain_paths.size());
}

for (size_t i = 0; i < size[0] * size[1]; ++i) {
tiles.push_back({terrain_obj, terrain_info_path, terrain_elevation_t::zero()});
}

Expand Down
4 changes: 1 addition & 3 deletions libopenage/gamestate/terrain_tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ using terrain_elevation_t = util::FixedPoint<uint64_t, 16>;
struct TerrainTile {
/**
* Terrain definition used by this tile.
*
* TODO: Make this non-optional once all modpacks support terrain graphics.
*/
std::optional<nyan::Object> terrain;
nyan::Object terrain;

/**
* Path to the terrain asset used by this tile.
Expand Down

0 comments on commit da96a11

Please sign in to comment.