From 09931b6c9d5942f94827b9837581f2baeac28112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Zieli=C5=84ski?= Date: Fri, 23 Sep 2022 18:57:16 +0200 Subject: [PATCH] Moved roads and rivers to config files. This also allows adding new ones via mods. --- config/rivers.json | 30 ++++++++++++++ config/roads.json | 23 +++++++++++ lib/GameConstants.h | 4 +- lib/Terrain.cpp | 96 +++++++++++++++++++++++++++++---------------- 4 files changed, 117 insertions(+), 36 deletions(-) create mode 100644 config/rivers.json create mode 100644 config/roads.json diff --git a/config/rivers.json b/config/rivers.json new file mode 100644 index 000000000..1ff5e32c2 --- /dev/null +++ b/config/rivers.json @@ -0,0 +1,30 @@ +{ + "waterRiver": + { + "originalRiverId": 1, + "code": "rw", //must be 2 characters + "animation": "clrrvr", + "delta": "clrdelt" + }, + "iceRiver": + { + "originalRiverId": 2, + "code": "ri", + "animation": "icyrvr", + "delta": "icedelt" + }, + "mudRiver": + { + "originalRiverId": 3, + "code": "rm", + "animation": "mudrvr", + "delta": "muddelt" + }, + "lavaRiver": + { + "originalRiverId": 4, + "code": "rl", + "animation": "lavrvr", + "delta": "lavdelt" + } +} \ No newline at end of file diff --git a/config/roads.json b/config/roads.json new file mode 100644 index 000000000..48832d2e0 --- /dev/null +++ b/config/roads.json @@ -0,0 +1,23 @@ +{ + "dirtRoad": + { + "originalRoadId": 1, + "code": "pd", //must be 2 characters + "animation": "dirtrd", + "moveCost": 75 + }, + "gravelRoad": + { + "originalRoadId": 2, + "code": "pg", + "animation": "gravrd", + "moveCost": 65 + }, + "cobblestoneRoad": + { + "originalRoadId": 3, + "code": "pc", + "animation": "cobbrd", + "moveCost": 50 + } +} \ No newline at end of file diff --git a/lib/GameConstants.h b/lib/GameConstants.h index 41b10bd36..f864cdd53 100644 --- a/lib/GameConstants.h +++ b/lib/GameConstants.h @@ -859,7 +859,7 @@ namespace Road DIRT_ROAD = 1, GRAVEL_ROAD = 2, COBBLESTONE_ROAD = 3, - ORIGINAL_ROAD_COUNT = COBBLESTONE_ROAD + ORIGINAL_ROAD_COUNT //+1 }; } @@ -872,7 +872,7 @@ namespace River ICY_RIVER = 2, MUD_RIVER = 3, LAVA_RIVER = 4, - ORIGINAL_RIVER_COUNT = LAVA_RIVER + ORIGINAL_RIVER_COUNT //+1 }; } diff --git a/lib/Terrain.cpp b/lib/Terrain.cpp index 4127329bd..951797591 100644 --- a/lib/Terrain.cpp +++ b/lib/Terrain.cpp @@ -196,30 +196,38 @@ TerrainTypeHandler::TerrainTypeHandler() void TerrainTypeHandler::initRivers() { - //TODO: load from file? + auto allConfigs = VLC->modh->getActiveMods(); + allConfigs.insert(allConfigs.begin(), "core"); - const std::vector RIVER_DELTA_TEMPLATE_NAME - { - {""}, - {"clrdelt"}, - {"icedelt"}, - {"muddelt"}, - {"lavdelt"} - }; + riverTypes.resize(River::ORIGINAL_RIVER_COUNT, nullptr); //make space for original rivers + riverTypes[River::NO_RIVER] = new RiverType(); //default - const std::vector> RIVER_CONSTANTS = + for (auto & mod : allConfigs) { - {"", ""}, - {"clrrvr", "rw"}, - {"icyrvr", "ri"}, - {"mudrvr", "rm"}, - {"lavrvr", "rl"} - }; + if (!CResourceHandler::get(mod)->existsResource(ResourceID("config/rivers.json"))) + continue; - for (size_t i = 0; i < std::size(RIVER_CONSTANTS); i++) - { - riverTypes.emplace_back(new RiverType(RIVER_CONSTANTS[i].first, RIVER_CONSTANTS[i].second, i)); - riverTypes[i]->deltaName = RIVER_DELTA_TEMPLATE_NAME[i]; + JsonNode rivs(mod, ResourceID("config/rivers.json")); + for (auto & river : rivs.Struct()) + { + auto * info = new RiverType(); + + info->fileName = river.second["animation"].String(); + info->code = river.second["code"].String(); + info->deltaName = river.second["delta"].String(); + //info->movementCost = river.second["moveCost"].Integer(); + + if (!river.second["originalRiverId"].isNull()) + { + info->id = static_cast(river.second["originalRiverId"].Float()); + riverTypes[info->id] = info; + } + else + { + info->id = riverTypes.size(); + riverTypes.push_back(info); + } + } } recreateRiverMaps(); @@ -227,24 +235,38 @@ void TerrainTypeHandler::initRivers() void TerrainTypeHandler::initRoads() { - //TODO: read from config + auto allConfigs = VLC->modh->getActiveMods(); + allConfigs.insert(allConfigs.begin(), "core"); - const std::vector> ROAD_CONSTANTS = - { - {"", ""}, - {"dirtrd", "pd"}, - {"gravrd", "pg"}, - {"cobbrd", "pc"} - }; + roadTypes.resize(Road::ORIGINAL_ROAD_COUNT, nullptr); //make space for original rivers + roadTypes[Road::NO_ROAD] = new RoadType(); //default - for (size_t i = 0; i < std::size(ROAD_CONSTANTS); i++) + for (auto & mod : allConfigs) { - roadTypes.emplace_back(new RoadType(ROAD_CONSTANTS[i].first, ROAD_CONSTANTS[i].second, i)); + if (!CResourceHandler::get(mod)->existsResource(ResourceID("config/roads.json"))) + continue; + + JsonNode rds(mod, ResourceID("config/roads.json")); + for (auto & road : rds.Struct()) + { + auto * info = new RoadType(); + + info->fileName = road.second["animation"].String(); + info->code = road.second["code"].String(); + info->movementCost = static_cast(road.second["moveCost"].Float()); + + if (!road.second["originalRoadId"].isNull()) + { + info->id = static_cast(road.second["originalRoadId"].Float()); + roadTypes[info->id] = info; + } + else + { + info->id = roadTypes.size(); + roadTypes.push_back(info); + } + } } - - roadTypes[1]->movementCost = 75; - roadTypes[2]->movementCost = 65; - roadTypes[3]->movementCost = 50; recreateRoadMaps(); } @@ -263,6 +285,9 @@ void TerrainTypeHandler::recreateRiverMaps() { for (const RiverType * riverInfo : riverTypes) { + if (riverInfo->id == River::NO_RIVER) + continue; + riverInfoByName[riverInfo->fileName] = riverInfo; riverInfoByCode[riverInfo->code] = riverInfo; riverInfoById[riverInfo->id] = riverInfo; @@ -273,6 +298,9 @@ void TerrainTypeHandler::recreateRoadMaps() { for (const RoadType * roadInfo : roadTypes) { + if (roadInfo->id == Road::NO_ROAD) + continue; + roadInfoByName[roadInfo->fileName] = roadInfo; roadInfoByCode[roadInfo->code] = roadInfo; roadInfoById[roadInfo->id] = roadInfo;