From e91e2670abfd4976883c7a5d654d1f5469b4ac7a Mon Sep 17 00:00:00 2001 From: zaaarf Date: Fri, 22 Sep 2023 16:14:22 +0200 Subject: feat: removed enum, passed goods manager --- src/openvic-simulation/units/Unit.cpp | 70 +++++++++++++++++------------------ 1 file changed, 35 insertions(+), 35 deletions(-) (limited to 'src/openvic-simulation/units/Unit.cpp') diff --git a/src/openvic-simulation/units/Unit.cpp b/src/openvic-simulation/units/Unit.cpp index 1b02efe..55ff08d 100644 --- a/src/openvic-simulation/units/Unit.cpp +++ b/src/openvic-simulation/units/Unit.cpp @@ -1,18 +1,15 @@ #include "Unit.hpp" -#include "GameManager.hpp" -#include "dataloader/NodeTools.hpp" +#include -#define EXTRA_ARGS icon, sprite, active, type, floating_flag, priority, max_strength, \ +#define UNIT_ARGS icon, sprite, active, type, floating_flag, priority, max_strength, \ default_organisation, maximum_speed, weighted_value, build_time, build_cost, supply_consumption, \ supply_cost, supply_consumption_score -#define FOR_SUPER(cat) identifier, cat, EXTRA_ARGS - using namespace OpenVic; using namespace OpenVic::NodeTools; -Unit::Unit(std::string_view identifier, UnitCategory category, UNIT_PARAMS) : HasIdentifier { identifier }, - category { category }, icon { icon }, sprite { sprite }, active { active }, type { type }, +Unit::Unit(std::string_view identifier, std::string_view category, UNIT_PARAMS) : HasIdentifier { identifier }, + icon { icon }, category { category }, sprite { sprite }, active { active }, type { 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 }, @@ -30,7 +27,7 @@ bool Unit::is_active() const { return active; } -UnitType Unit::get_type() const { +std::string_view Unit::get_type() const { return type; } @@ -78,7 +75,7 @@ uint32_t Unit::get_supply_consumption_score() const { return supply_consumption_score; } -LandUnit::LandUnit(std::string_view identifier, UNIT_PARAMS, LAND_PARAMS) : Unit { FOR_SUPER(UnitCategory::LAND) }, +LandUnit::LandUnit(std::string_view identifier, UNIT_PARAMS, LAND_PARAMS) : Unit { identifier, "land", UNIT_ARGS }, reconnaisance { reconnaisance }, attack { attack }, defence { defence }, discipline { discipline }, support { support }, maneuver { maneuver }, siege { siege } {} @@ -110,7 +107,7 @@ uint32_t LandUnit::get_siege() const { return siege; } -NavalUnit::NavalUnit(std::string_view identifier, UNIT_PARAMS, NAVY_PARAMS) : Unit { FOR_SUPER(UnitCategory::NAVAL) }, +NavalUnit::NavalUnit(std::string_view identifier, UNIT_PARAMS, NAVY_PARAMS) : Unit { identifier, "naval", UNIT_ARGS }, naval_icon { naval_icon }, sail { sail }, transport { transport }, 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 }, hull { hull }, gun_power { gun_power }, fire_range { fire_range }, evasion { evasion }, @@ -172,9 +169,9 @@ uint32_t NavalUnit::get_torpedo_attack() const { return torpedo_attack; } -UnitManager::UnitManager() : units { "units " } {}; +UnitManager::UnitManager(GoodManager& game_manager) : good_manager { good_manager }, units { "units " } {}; -bool UnitManager::_check_superclass_parameters(const std::string_view identifier, UnitCategory cat, UNIT_PARAMS) { +bool UnitManager::_check_shared_parameters(const std::string_view identifier, UNIT_PARAMS) { if (identifier.empty()) { Logger::error("Invalid religion identifier - empty!"); return false; @@ -182,41 +179,44 @@ bool UnitManager::_check_superclass_parameters(const std::string_view identifier //TODO check that icon and sprite exist + if (!allowed_unit_types.contains(type)) { + Logger::error("Invalid unit type \"", type, "\" specified!"); + return false; + } + return true; } -bool UnitManager::add_land_unit(const std::string_view identifier, UnitCategory cat, UNIT_PARAMS, LAND_PARAMS) { - if (!_check_superclass_parameters(FOR_SUPER(cat))) { +bool UnitManager::add_land_unit(const std::string_view identifier, UNIT_PARAMS, LAND_PARAMS) { + if (!_check_shared_parameters(identifier, UNIT_ARGS)) { return false; } - return units.add_item( - LandUnit { identifier, EXTRA_ARGS, reconnaisance, attack, defence, discipline, support, maneuver, siege } - ); + return units.add_item(LandUnit { + identifier, UNIT_ARGS, reconnaisance, attack, defence, discipline, support, maneuver, siege + }); } -bool UnitManager::add_naval_unit(const std::string_view identifier, UnitCategory cat, UNIT_PARAMS, NAVY_PARAMS) { - if (!_check_superclass_parameters(FOR_SUPER(cat))) { +bool UnitManager::add_naval_unit(const std::string_view identifier, UNIT_PARAMS, NAVY_PARAMS) { + if (!_check_shared_parameters(identifier, UNIT_ARGS)) { return false; } //TODO: check that icon and sounds exist - return units.add_item( - NavalUnit { identifier, EXTRA_ARGS, naval_icon, sail, transport, move_sound, select_sound, colonial_points, build_overseas, min_port_level, limit_per_port, hull, gun_power, fire_range, evasion, torpedo_attack } - ); + return units.add_item(NavalUnit { + identifier, UNIT_ARGS, naval_icon, sail, transport, move_sound, select_sound, colonial_points, + build_overseas, min_port_level, limit_per_port, hull, gun_power, fire_range, evasion, torpedo_attack + }); } bool UnitManager::load_unit_file(ast::NodeCPtr root) { return NodeTools::expect_dictionary([this](std::string_view key, ast::NodeCPtr value) -> bool { - //TODO: enum parsing, or use set of strings? probably the latter - //TODO: need to get GameManager to access GoodsManager and call the expect_goods_map - Unit::icon_t icon; - UnitCategory category; + std::string_view category; Unit::sprite_t sprite; bool active, floating_flag; - UnitType type; + std::string_view type; uint32_t priority, max_strength, default_organisation, maximum_speed, build_time, supply_consumption_score; fixed_point_t weighted_value, supply_consumption; std::map build_cost, supply_cost; @@ -224,10 +224,10 @@ bool UnitManager::load_unit_file(ast::NodeCPtr root) { //shared bool ret = expect_dictionary_keys( "icon", ONE_EXACTLY, expect_uint(assign_variable_callback(icon)), - //"type", ONE_EXACTLY, expect_string(assign_variable_callback(category)), + "type", ONE_EXACTLY, expect_string(assign_variable_callback(category)), "sprite", ONE_EXACTLY, expect_string(assign_variable_callback(sprite)), "active", ONE_EXACTLY, expect_bool(assign_variable_callback(active)), - //"unit_type", ONE_EXACTLY, expect_string(assign_variable_callback(type)), + "unit_type", ONE_EXACTLY, expect_string(assign_variable_callback(type)), "floating_flag", ONE_EXACTLY, expect_bool(assign_variable_callback(floating_flag)), "priority", ONE_EXACTLY, expect_uint(assign_variable_callback(priority)), "max_strength", ONE_EXACTLY, expect_uint(assign_variable_callback(max_strength)), @@ -235,13 +235,13 @@ bool UnitManager::load_unit_file(ast::NodeCPtr root) { "maximum_speed", ONE_EXACTLY, expect_uint(assign_variable_callback(maximum_speed)), "weighted_value", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(weighted_value)), "build_time", ONE_EXACTLY, expect_uint(assign_variable_callback(build_time)), - //"build_cost", ONE_EXACTLY, goods_manager.expect_goods_map(assign_variable_callback(build_cost)), + "build_cost", ONE_EXACTLY, good_manager.expect_goods_map(assign_variable_callback(build_cost)), "supply_consumption", ONE_EXACTLY, expect_fixed_point(assign_variable_callback(supply_consumption)), - //"supply_cost", ONE_EXACTLY, goods_manager.expect_goods_map(assign_variable_callback(supply_cost)), + "supply_cost", ONE_EXACTLY, good_manager.expect_goods_map(assign_variable_callback(supply_cost)), "supply_consumption_score", ONE_EXACTLY, expect_uint(assign_variable_callback(supply_consumption_score)) )(value); - if (category == UnitCategory::LAND) { + if (category == "land") { uint32_t reconnaisance, attack, defence, discipline, support, maneuver, siege; ret &= expect_dictionary_keys( @@ -255,12 +255,12 @@ bool UnitManager::load_unit_file(ast::NodeCPtr root) { )(value); ret &= add_land_unit( - key, category, EXTRA_ARGS, + key, UNIT_ARGS, reconnaisance, attack, defence, discipline, support, maneuver, siege ); return ret; - } else if (category == UnitCategory::NAVAL) { + } else if (category == "naval") { Unit::icon_t naval_icon; bool sail = false, transport = false, build_overseas; Unit::sound_t move_sound, select_sound; @@ -286,7 +286,7 @@ bool UnitManager::load_unit_file(ast::NodeCPtr root) { )(value); ret &= add_naval_unit( - key, category, EXTRA_ARGS, + key, UNIT_ARGS, naval_icon, sail, transport, move_sound, select_sound, colonial_points, build_overseas, min_port_level, limit_per_port, hull, gun_power, fire_range, evasion, torpedo_attack ); -- cgit v1.2.3-56-ga3b1