aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/military
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2023-10-15 16:26:39 +0200
committer GitHub <noreply@github.com>2023-10-15 16:26:39 +0200
commit3249e21104bab6002676abe00450e7bd3f682303 (patch)
treec2a13a44d4c8553f6e87193f5f437052cf1d7067 /src/openvic-simulation/military
parent17847e16e14ec52eb48a6fd0d9dc36ee93e457db (diff)
parentd26f9c2fb5a9666822a0f702d76b764600a390d7 (diff)
Merge pull request #52 from OpenVicProject/tgc-compat
TGC compatibility fixes + other cleanup
Diffstat (limited to 'src/openvic-simulation/military')
-rw-r--r--src/openvic-simulation/military/Unit.cpp224
-rw-r--r--src/openvic-simulation/military/Unit.hpp46
2 files changed, 146 insertions, 124 deletions
diff --git a/src/openvic-simulation/military/Unit.cpp b/src/openvic-simulation/military/Unit.cpp
index 63ece91..8b010e3 100644
--- a/src/openvic-simulation/military/Unit.cpp
+++ b/src/openvic-simulation/military/Unit.cpp
@@ -2,22 +2,27 @@
#include <set>
-#define UNIT_ARGS icon, sprite, active, unit_type, floating_flag, priority, max_strength, \
- default_organisation, maximum_speed, weighted_value, build_time, build_cost, supply_consumption, \
- supply_cost
-#define LAND_ARGS primary_culture, sprite_override, sprite_mount, sprite_mount_attach_node, \
- reconnaissance, attack, defence, discipline, support, maneuver, siege
-#define NAVY_ARGS naval_icon, sail, transport, capital, move_sound, select_sound, colonial_points, build_overseas, min_port_level, \
- limit_per_port, supply_consumption_score, hull, gun_power, fire_range, evasion, torpedo_attack
+#define UNIT_ARGS \
+ icon, sprite, active, unit_type, floating_flag, priority, max_strength, default_organisation, maximum_speed, \
+ weighted_value, move_sound, select_sound, build_time, std::move(build_cost), supply_consumption, std::move(supply_cost)
+
+#define LAND_ARGS \
+ primary_culture, sprite_override, sprite_mount, sprite_mount_attach_node, \
+ reconnaissance, attack, defence, discipline, support, maneuver, siege
+
+#define NAVY_ARGS \
+ naval_icon, sail, transport, capital, colonial_points, build_overseas, min_port_level, \
+ limit_per_port, supply_consumption_score, hull, gun_power, fire_range, evasion, torpedo_attack
using namespace OpenVic;
using namespace OpenVic::NodeTools;
-Unit::Unit(std::string_view identifier, type_t type, UNIT_PARAMS) : HasIdentifier { identifier },
- icon { icon }, type { type }, sprite { sprite }, active { active }, unit_type { unit_type },
- floating_flag { floating_flag }, priority { priority }, max_strength { max_strength },
+Unit::Unit(std::string_view identifier, type_t type, UNIT_PARAMS)
+ : HasIdentifier { identifier }, icon { icon }, type { type }, sprite { sprite }, active { active },
+ unit_type { unit_type }, floating_flag { floating_flag }, priority { priority }, max_strength { max_strength },
default_organisation { default_organisation }, maximum_speed { maximum_speed }, weighted_value { weighted_value },
- build_time { build_time }, build_cost { build_cost }, supply_consumption { supply_consumption }, supply_cost { supply_cost } {}
+ move_sound { move_sound }, select_sound { select_sound }, build_time { build_time }, build_cost { std::move(build_cost) },
+ supply_consumption { supply_consumption }, supply_cost { std::move(supply_cost) } {}
Unit::icon_t Unit::get_icon() const {
return icon;
@@ -67,7 +72,15 @@ fixed_point_t Unit::get_weighted_value() const {
return weighted_value;
}
-std::map<Good const*, fixed_point_t> const& Unit::get_build_cost() const {
+std::string_view Unit::get_move_sound() const {
+ return move_sound;
+}
+
+std::string_view Unit::get_select_sound() const {
+ return select_sound;
+}
+
+Good::good_map_t const& Unit::get_build_cost() const {
return build_cost;
}
@@ -75,14 +88,15 @@ fixed_point_t Unit::get_supply_consumption() const {
return supply_consumption;
}
-std::map<Good const*, fixed_point_t> const& Unit::get_supply_cost() const {
+Good::good_map_t const& Unit::get_supply_cost() const {
return supply_cost;
}
LandUnit::LandUnit(std::string_view identifier, UNIT_PARAMS, LAND_PARAMS)
- : Unit { identifier, type_t::LAND, UNIT_ARGS }, primary_culture { primary_culture }, sprite_override { sprite_override },
- sprite_mount { sprite_mount }, sprite_mount_attach_node { sprite_mount_attach_node }, reconnaissance { reconnaissance },
- attack { attack }, defence { defence }, discipline { discipline }, support { support }, maneuver { maneuver }, siege { siege } {}
+ : Unit { identifier, type_t::LAND, UNIT_ARGS }, primary_culture { primary_culture },
+ sprite_override { sprite_override }, sprite_mount { sprite_mount }, sprite_mount_attach_node { sprite_mount_attach_node },
+ reconnaissance { reconnaissance }, attack { attack }, defence { defence }, discipline { discipline }, support { support },
+ maneuver { maneuver }, siege { siege } {}
bool LandUnit::get_primary_culture() const {
return primary_culture;
@@ -128,11 +142,12 @@ fixed_point_t LandUnit::get_siege() const {
return siege;
}
-NavalUnit::NavalUnit(std::string_view identifier, UNIT_PARAMS, NAVY_PARAMS) : Unit { identifier, type_t::NAVAL, UNIT_ARGS },
- naval_icon { naval_icon }, sail { sail }, transport { transport }, capital { capital }, move_sound { move_sound },
- select_sound { select_sound }, colonial_points { colonial_points }, build_overseas { build_overseas },
- min_port_level { min_port_level }, limit_per_port { limit_per_port }, supply_consumption_score { supply_consumption_score },
- hull { hull }, gun_power { gun_power }, fire_range { fire_range }, evasion { evasion }, torpedo_attack { torpedo_attack } {};
+NavalUnit::NavalUnit(std::string_view identifier, UNIT_PARAMS, NAVY_PARAMS)
+ : Unit { identifier, type_t::NAVAL, UNIT_ARGS }, naval_icon { naval_icon }, sail { sail },
+ transport { transport }, capital { capital }, colonial_points { colonial_points },
+ build_overseas { build_overseas }, min_port_level { min_port_level },limit_per_port { limit_per_port },
+ supply_consumption_score { supply_consumption_score }, hull { hull }, gun_power { gun_power },
+ fire_range { fire_range }, evasion { evasion }, torpedo_attack { torpedo_attack } {};
NavalUnit::icon_t NavalUnit::get_naval_icon() const {
return naval_icon;
@@ -146,14 +161,6 @@ bool NavalUnit::is_transport() const {
return transport;
}
-std::string_view NavalUnit::get_move_sound() const {
- return move_sound;
-}
-
-std::string_view NavalUnit::get_select_sound() const {
- return select_sound;
-}
-
fixed_point_t NavalUnit::get_colonial_points() const {
return colonial_points;
}
@@ -235,32 +242,33 @@ bool UnitManager::add_naval_unit(std::string_view identifier, UNIT_PARAMS, NAVY_
return units.add_item(NavalUnit { identifier, UNIT_ARGS, NAVY_ARGS });
}
-static bool shared_keys_callback(std::string_view key, ast::NodeCPtr) {
- static const std::set<std::string, std::less<void>> reserved_keys = {
- "icon", "type", "sprite", "active", "unit_type", "floating_flag", "priority",
- "max_strength", "default_organisation", "maximum_speed", "weighted_value",
- "build_time", "build_cost", "supply_consumption", "supply_cost"
- };
- if (reserved_keys.contains(key)) return true;
- Logger::error("Invalid key: ", key);
- return false;
-};
-
bool UnitManager::load_unit_file(GoodManager const& good_manager, ast::NodeCPtr root) {
return expect_dictionary([this, &good_manager](std::string_view key, ast::NodeCPtr value) -> bool {
+ Unit::type_t type;
Unit::icon_t icon = 0;
- std::string_view type, unit_type, sprite;
+ std::string_view unit_type, sprite, move_sound, select_sound; // TODO defaults for move_sound and select_sound
bool active = true, floating_flag = false;
uint32_t priority = 0;
Timespan build_time;
fixed_point_t maximum_speed = 0, max_strength = 0, default_organisation = 0, weighted_value = 0, supply_consumption = 0;
- std::map<Good const*, fixed_point_t> build_cost, supply_cost;
+ Good::good_map_t build_cost, supply_cost;
+ using enum Unit::type_t;
+ static const string_map_t<Unit::type_t> type_map = {
+ { "land", LAND }, { "naval", NAVAL }
+ };
+ bool ret = expect_key("type", expect_identifier(expect_mapped_string(type_map, assign_variable_callback(type))))(value);
+
+ if (!ret) {
+ Logger::error("Failed to read type for unit: ", key);
+ return false;
+ }
+
+ key_map_t key_map;
//shared
- bool ret = expect_dictionary_keys_and_default(
- key_value_success_callback,
+ ret &= add_key_map_entries(key_map,
"icon", ONE_EXACTLY, expect_uint(assign_variable_callback(icon)),
- "type", ONE_EXACTLY, expect_identifier(assign_variable_callback(type)),
+ "type", ONE_EXACTLY, success_callback,
"sprite", ONE_EXACTLY, expect_identifier(assign_variable_callback(sprite)),
"active", ZERO_OR_ONE, expect_bool(assign_variable_callback(active)),
"unit_type", ONE_EXACTLY, expect_identifier(assign_variable_callback(unit_type)),
@@ -270,69 +278,77 @@ bool UnitManager::load_unit_file(GoodManager const& good_manager, ast::NodeCPtr
"default_organisation", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(default_organisation)),
"maximum_speed", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(maximum_speed)),
"weighted_value", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(weighted_value)),
+ "move_sound", ZERO_OR_ONE, expect_identifier(assign_variable_callback(move_sound)),
+ "select_sound", ZERO_OR_ONE, expect_identifier(assign_variable_callback(select_sound)),
"build_time", ONE_EXACTLY, expect_days(assign_variable_callback(build_time)),
"build_cost", ONE_EXACTLY, good_manager.expect_good_decimal_map(move_variable_callback(build_cost)),
"supply_consumption", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(supply_consumption)),
"supply_cost", ONE_EXACTLY, good_manager.expect_good_decimal_map(move_variable_callback(supply_cost))
- )(value);
-
- if (type == "land") {
- bool primary_culture = false;
- std::string_view sprite_override, sprite_mount, sprite_mount_attach_node;
- fixed_point_t reconnaissance = 0, attack = 0, defence = 0, discipline = 0, support = 0, maneuver = 0, siege = 0;
-
- ret &= expect_dictionary_keys_and_default(
- shared_keys_callback,
- "primary_culture", ZERO_OR_ONE, expect_bool(assign_variable_callback(primary_culture)),
- "sprite_override", ZERO_OR_ONE, expect_identifier(assign_variable_callback(sprite_override)),
- "sprite_mount", ZERO_OR_ONE, expect_identifier(assign_variable_callback(sprite_mount)),
- "sprite_mount_attach_node", ZERO_OR_ONE, expect_identifier(assign_variable_callback(sprite_mount_attach_node)),
- "reconnaissance", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(reconnaissance)),
- "attack", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(attack)),
- "defence", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(defence)),
- "discipline", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(discipline)),
- "support", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(support)),
- "maneuver", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(maneuver)),
- "siege", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(siege))
- )(value);
-
- ret &= add_land_unit(key, UNIT_ARGS, LAND_ARGS);
-
- return ret;
- } else if (type == "naval") {
- Unit::icon_t naval_icon = 0;
- bool sail = false, transport = false, capital = false, build_overseas = false;
- std::string_view move_sound, select_sound; //TODO defaults for both
- uint32_t min_port_level = 0;
- int32_t limit_per_port = 0;
- fixed_point_t fire_range = 0, evasion = 0, supply_consumption_score = 0, hull = 0, gun_power = 0, colonial_points = 0, torpedo_attack = 0;
-
- ret &= expect_dictionary_keys_and_default(
- shared_keys_callback,
- "naval_icon", ONE_EXACTLY, expect_uint(assign_variable_callback(naval_icon)),
- "sail", ZERO_OR_ONE, expect_bool(assign_variable_callback(sail)),
- "transport", ZERO_OR_ONE, expect_bool(assign_variable_callback(transport)),
- "capital", ZERO_OR_ONE, expect_bool(assign_variable_callback(capital)),
- "move_sound", ZERO_OR_ONE, expect_identifier(assign_variable_callback(move_sound)),
- "select_sound", ZERO_OR_ONE, expect_identifier(assign_variable_callback(select_sound)),
- "colonial_points", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(colonial_points)),
- "can_build_overseas", ZERO_OR_ONE, expect_bool(assign_variable_callback(build_overseas)),
- "min_port_level", ONE_EXACTLY, expect_uint(assign_variable_callback(min_port_level)),
- "limit_per_port", ONE_EXACTLY, expect_int(assign_variable_callback(limit_per_port)),
- "supply_consumption_score", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(supply_consumption_score)),
- "hull", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(hull)),
- "gun_power", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(gun_power)),
- "fire_range", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(fire_range)),
- "evasion", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(evasion)),
- "torpedo_attack", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(torpedo_attack))
- )(value);
-
- ret &= add_naval_unit(key, UNIT_ARGS, NAVY_ARGS);
-
- return ret;
- } else {
- Logger::error("Invalid type for unit ", key, ": ", type);
+ );
+
+ switch (type) {
+ case LAND:
+ {
+ bool primary_culture = false;
+ std::string_view sprite_override, sprite_mount, sprite_mount_attach_node;
+ fixed_point_t reconnaissance = 0, attack = 0, defence = 0, discipline = 0, support = 0, maneuver = 0, siege = 0;
+
+ ret &= add_key_map_entries(key_map,
+ "primary_culture", ZERO_OR_ONE, expect_bool(assign_variable_callback(primary_culture)),
+ "sprite_override", ZERO_OR_ONE, expect_identifier(assign_variable_callback(sprite_override)),
+ "sprite_mount", ZERO_OR_ONE, expect_identifier(assign_variable_callback(sprite_mount)),
+ "sprite_mount_attach_node", ZERO_OR_ONE, expect_identifier(assign_variable_callback(sprite_mount_attach_node)),
+ "reconnaissance", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(reconnaissance)),
+ "attack", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(attack)),
+ "defence", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(defence)),
+ "discipline", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(discipline)),
+ "support", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(support)),
+ "maneuver", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(maneuver)),
+ "siege", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(siege))
+ );
+
+ ret &= expect_dictionary_key_map(key_map)(value);
+
+ ret &= add_land_unit(key, UNIT_ARGS, LAND_ARGS);
+
+ return ret;
+ }
+ break;
+ case NAVAL:
+ {
+ Unit::icon_t naval_icon = 0;
+ bool sail = false, transport = false, capital = false, build_overseas = false;
+ uint32_t min_port_level = 0;
+ int32_t limit_per_port = 0;
+ fixed_point_t fire_range = 0, evasion = 0, supply_consumption_score = 0, hull = 0, gun_power = 0, colonial_points = 0, torpedo_attack = 0;
+
+ ret &= add_key_map_entries(key_map,
+ "naval_icon", ONE_EXACTLY, expect_uint(assign_variable_callback(naval_icon)),
+ "sail", ZERO_OR_ONE, expect_bool(assign_variable_callback(sail)),
+ "transport", ZERO_OR_ONE, expect_bool(assign_variable_callback(transport)),
+ "capital", ZERO_OR_ONE, expect_bool(assign_variable_callback(capital)),
+ "colonial_points", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(colonial_points)),
+ "can_build_overseas", ZERO_OR_ONE, expect_bool(assign_variable_callback(build_overseas)),
+ "min_port_level", ONE_EXACTLY, expect_uint(assign_variable_callback(min_port_level)),
+ "limit_per_port", ONE_EXACTLY, expect_int(assign_variable_callback(limit_per_port)),
+ "supply_consumption_score", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(supply_consumption_score)),
+ "hull", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(hull)),
+ "gun_power", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(gun_power)),
+ "fire_range", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(fire_range)),
+ "evasion", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(evasion)),
+ "torpedo_attack", ZERO_OR_ONE, expect_fixed_point(assign_variable_callback(torpedo_attack))
+ );
+
+ ret &= expect_dictionary_key_map(key_map)(value);
+
+ ret &= add_naval_unit(key, UNIT_ARGS, NAVY_ARGS);
+
+ return ret;
+ }
+ break;
+ default:
+ Logger::error("Unknown unit type for ", key, ": ", static_cast<int>(type));
return false;
}
})(root);
-} \ No newline at end of file
+}
diff --git a/src/openvic-simulation/military/Unit.hpp b/src/openvic-simulation/military/Unit.hpp
index de30763..17408cf 100644
--- a/src/openvic-simulation/military/Unit.hpp
+++ b/src/openvic-simulation/military/Unit.hpp
@@ -9,18 +9,22 @@
#include "openvic-simulation/economy/Good.hpp"
#include "openvic-simulation/types/Date.hpp"
-#define UNIT_PARAMS Unit::icon_t icon, std::string_view sprite, bool active, std::string_view unit_type, \
- bool floating_flag, uint32_t priority, fixed_point_t max_strength, fixed_point_t default_organisation, \
- fixed_point_t maximum_speed, fixed_point_t weighted_value, Timespan build_time, \
- std::map<Good const*, fixed_point_t> build_cost, fixed_point_t supply_consumption, \
- std::map<Good const*, fixed_point_t> supply_cost
-#define LAND_PARAMS bool primary_culture, std::string_view sprite_override, std::string_view sprite_mount, \
- std::string_view sprite_mount_attach_node, fixed_point_t reconnaissance, fixed_point_t attack, \
- fixed_point_t defence, fixed_point_t discipline, fixed_point_t support, fixed_point_t maneuver, fixed_point_t siege
-#define NAVY_PARAMS Unit::icon_t naval_icon, bool sail, bool transport, bool capital, std::string_view move_sound, \
- std::string_view select_sound, fixed_point_t colonial_points, bool build_overseas, uint32_t min_port_level, \
- int32_t limit_per_port, fixed_point_t supply_consumption_score, fixed_point_t hull, fixed_point_t gun_power, \
- fixed_point_t fire_range, fixed_point_t evasion, fixed_point_t torpedo_attack
+#define UNIT_PARAMS \
+ Unit::icon_t icon, std::string_view sprite, bool active, std::string_view unit_type, \
+ bool floating_flag, uint32_t priority, fixed_point_t max_strength, fixed_point_t default_organisation, \
+ fixed_point_t maximum_speed, fixed_point_t weighted_value, std::string_view move_sound, \
+ std::string_view select_sound, Timespan build_time, Good::good_map_t&& build_cost, \
+ fixed_point_t supply_consumption, Good::good_map_t&& supply_cost
+
+#define LAND_PARAMS \
+ bool primary_culture, std::string_view sprite_override, std::string_view sprite_mount, \
+ std::string_view sprite_mount_attach_node, fixed_point_t reconnaissance, fixed_point_t attack, fixed_point_t defence, \
+ fixed_point_t discipline, fixed_point_t support, fixed_point_t maneuver, fixed_point_t siege
+
+#define NAVY_PARAMS \
+ Unit::icon_t naval_icon, bool sail, bool transport, bool capital, fixed_point_t colonial_points, bool build_overseas, \
+ uint32_t min_port_level, int32_t limit_per_port, fixed_point_t supply_consumption_score, fixed_point_t hull, \
+ fixed_point_t gun_power, fixed_point_t fire_range, fixed_point_t evasion, fixed_point_t torpedo_attack
namespace OpenVic {
struct Unit : HasIdentifier {
@@ -44,10 +48,13 @@ namespace OpenVic {
const fixed_point_t maximum_speed;
const fixed_point_t weighted_value;
+ const std::string move_sound;
+ const std::string select_sound;
+
const Timespan build_time;
- const std::map<Good const*, fixed_point_t> build_cost;
+ const Good::good_map_t build_cost;
const fixed_point_t supply_consumption;
- const std::map<Good const*, fixed_point_t> supply_cost;
+ const Good::good_map_t supply_cost;
protected:
Unit(std::string_view identifier, type_t type, UNIT_PARAMS);
@@ -68,10 +75,13 @@ namespace OpenVic {
fixed_point_t get_maximum_speed() const;
fixed_point_t get_weighted_value() const;
+ std::string_view get_move_sound() const;
+ std::string_view get_select_sound() const;
+
Timespan get_build_time() const;
- std::map<Good const*, fixed_point_t> const& get_build_cost() const;
+ Good::good_map_t const& get_build_cost() const;
fixed_point_t get_supply_consumption() const;
- std::map<Good const*, fixed_point_t> const& get_supply_cost() const;
+ Good::good_map_t const& get_supply_cost() const;
};
struct LandUnit : Unit {
@@ -115,8 +125,6 @@ namespace OpenVic {
const bool sail;
const bool transport;
const bool capital;
- const std::string move_sound;
- const std::string select_sound;
const fixed_point_t colonial_points;
const bool build_overseas;
const uint32_t min_port_level;
@@ -138,8 +146,6 @@ namespace OpenVic {
bool can_sail() const;
bool is_transport() const;
bool is_capital() const;
- std::string_view get_move_sound() const;
- std::string_view get_select_sound() const;
fixed_point_t get_colonial_points() const;
bool can_build_overseas() const;
uint32_t get_min_port_level() const;