aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation
diff options
context:
space:
mode:
Diffstat (limited to 'src/openvic-simulation')
-rw-r--r--src/openvic-simulation/GameManager.cpp4
-rw-r--r--src/openvic-simulation/GameManager.hpp16
-rw-r--r--src/openvic-simulation/dataloader/Dataloader.cpp19
-rw-r--r--src/openvic-simulation/dataloader/Dataloader.hpp2
-rw-r--r--src/openvic-simulation/units/Unit.cpp70
-rw-r--r--src/openvic-simulation/units/Unit.hpp62
6 files changed, 97 insertions, 76 deletions
diff --git a/src/openvic-simulation/GameManager.cpp b/src/openvic-simulation/GameManager.cpp
index 4c8b55f..5067a65 100644
--- a/src/openvic-simulation/GameManager.cpp
+++ b/src/openvic-simulation/GameManager.cpp
@@ -1,11 +1,13 @@
#include "GameManager.hpp"
#include "openvic-simulation/utility/Logger.hpp"
+#include "units/Unit.hpp"
using namespace OpenVic;
GameManager::GameManager(state_updated_func_t state_updated_callback)
- : clock { [this]() { tick(); }, [this]() { update_state(); } },
+ : unit_manager { good_manager },
+ clock { [this]() { tick(); }, [this]() { update_state(); } },
state_updated { state_updated_callback } {}
void GameManager::set_needs_update() {
diff --git a/src/openvic-simulation/GameManager.hpp b/src/openvic-simulation/GameManager.hpp
index 70d8cf0..fa61eaf 100644
--- a/src/openvic-simulation/GameManager.hpp
+++ b/src/openvic-simulation/GameManager.hpp
@@ -1,10 +1,14 @@
#pragma once
-#include "GameAdvancementHook.hpp"
-#include "economy/Good.hpp"
-#include "map/Map.hpp"
-#include "politics/Ideology.hpp"
-#include "politics/Issue.hpp"
+#include "openvic-simulation/GameAdvancementHook.hpp"
+#include "openvic-simulation/economy/Good.hpp"
+#include "openvic-simulation/map/Map.hpp"
+#include "openvic-simulation/politics/Ideology.hpp"
+#include "openvic-simulation/politics/Issue.hpp"
+#include "openvic-simulation/GameAdvancementHook.hpp"
+#include "openvic-simulation/economy/Good.hpp"
+#include "openvic-simulation/map/Map.hpp"
+#include "openvic-simulation/units/Unit.hpp"
namespace OpenVic {
struct GameManager {
@@ -16,7 +20,7 @@ namespace OpenVic {
PopManager pop_manager;
IdeologyManager ideology_manager;
IssueManager issue_manager;
-
+ UnitManager unit_manager;
GameAdvancementHook clock;
private:
diff --git a/src/openvic-simulation/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp
index d0d9506..55dbf8a 100644
--- a/src/openvic-simulation/dataloader/Dataloader.cpp
+++ b/src/openvic-simulation/dataloader/Dataloader.cpp
@@ -1,4 +1,5 @@
#include "Dataloader.hpp"
+#include <filesystem>
#include <openvic-dataloader/csv/Parser.hpp>
#include <openvic-dataloader/detail/CallbackOStream.hpp>
@@ -6,6 +7,7 @@
#include "openvic-simulation/GameManager.hpp"
#include "openvic-simulation/utility/Logger.hpp"
+#include "units/Unit.hpp"
using namespace OpenVic;
using namespace OpenVic::NodeTools;
@@ -150,13 +152,19 @@ bool Dataloader::_load_pop_types(PopManager& pop_manager, fs::path const& pop_ty
return pop_manager.load_pop_type_file(file.stem().string(), _parse_defines(file).get_file_node());
}
);
- if (!ret) {
- Logger::error("Failed to load pop types!");
- }
pop_manager.lock_pop_types();
return ret;
}
+bool Dataloader::_load_units(UnitManager& unit_manager, fs::path const& units_directory) const {
+ const bool ret = apply_to_files_in_dir(units_directory, ".txt",
+ [&unit_manager](fs::path const& file) -> bool {
+ return unit_manager.load_unit_file(_parse_defines(file).get_file_node());
+ }
+ );
+ unit_manager.lock_units();
+ return ret;}
+
bool Dataloader::_load_map_dir(GameManager& game_manager, fs::path const& map_directory) const {
Map& map = game_manager.map;
@@ -260,6 +268,7 @@ bool Dataloader::load_defines(GameManager& game_manager) const {
static const fs::path ideology_file = "common/ideologies.txt";
static const fs::path issues_file = "common/issues.txt";
static const fs::path map_directory = "map";
+ static const fs::path units_directory = "units";
bool ret = true;
@@ -291,6 +300,10 @@ bool Dataloader::load_defines(GameManager& game_manager) const {
Logger::error("Failed to load issues!");
ret = false;
}
+ if (!_load_units(game_manager.unit_manager, units_directory)) {
+ Logger::error("Failed to load units!");
+ ret = false;
+ }
if (!_load_map_dir(game_manager, map_directory)) {
Logger::error("Failed to load map!");
ret = false;
diff --git a/src/openvic-simulation/dataloader/Dataloader.hpp b/src/openvic-simulation/dataloader/Dataloader.hpp
index 6741361..9e15102 100644
--- a/src/openvic-simulation/dataloader/Dataloader.hpp
+++ b/src/openvic-simulation/dataloader/Dataloader.hpp
@@ -5,6 +5,7 @@
#include <vector>
#include "openvic-simulation/dataloader/NodeTools.hpp"
+#include "units/Unit.hpp"
namespace OpenVic {
namespace fs = std::filesystem;
@@ -21,6 +22,7 @@ namespace OpenVic {
path_vector_t roots;
bool _load_pop_types(PopManager& pop_manager, fs::path const& pop_type_directory) const;
+ bool _load_units(UnitManager& unit_manager, fs::path const& units_directory) const;
bool _load_map_dir(GameManager& game_manager, fs::path const& map_directory) const;
public:
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 <string_view>
-#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<const Good*, fixed_point_t> 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
);
diff --git a/src/openvic-simulation/units/Unit.hpp b/src/openvic-simulation/units/Unit.hpp
index b105afc..9653526 100644
--- a/src/openvic-simulation/units/Unit.hpp
+++ b/src/openvic-simulation/units/Unit.hpp
@@ -1,16 +1,20 @@
#pragma once
#include <cstdint>
+#include <set>
#include <string>
#include <string_view>
+#include <unordered_set>
#include "openvic-simulation/economy/Good.hpp"
#include "openvic-simulation/types/IdentifierRegistry.hpp"
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#include "openvic-simulation/dataloader/NodeTools.hpp"
+#include "openvic-simulation/economy/Good.hpp"
-#define UNIT_PARAMS Unit::icon_t icon, Unit::sprite_t sprite, bool active, UnitType type, bool floating_flag, uint32_t priority,\
- uint32_t max_strength, uint32_t default_organisation,uint32_t maximum_speed, fixed_point_t weighted_value, \
- uint32_t build_time, std::map<const Good*, fixed_point_t> build_cost, fixed_point_t supply_consumption, \
+#define UNIT_PARAMS Unit::icon_t icon, Unit::sprite_t sprite, bool active, std::string_view type, \
+ bool floating_flag, uint32_t priority, uint32_t max_strength, uint32_t default_organisation, \
+ uint32_t maximum_speed, fixed_point_t weighted_value, uint32_t build_time, \
+ std::map<const Good*, fixed_point_t> build_cost, fixed_point_t supply_consumption, \
std::map<const Good*, fixed_point_t> supply_cost, uint32_t supply_consumption_score
#define LAND_PARAMS uint32_t reconnaisance, uint32_t attack, uint32_t defence, uint32_t discipline, uint32_t support, \
uint32_t maneuver, uint32_t siege
@@ -19,32 +23,17 @@
uint32_t hull, uint32_t gun_power, fixed_point_t fire_range, uint32_t evasion, uint32_t torpedo_attack
namespace OpenVic {
- enum UnitCategory {
- NAVAL,
- LAND
- };
-
- enum UnitType {
- INFANTRY, //guard, infantry, irregular
- CAVALRY, //cavalry, cuirassier, dragoon, hussar, plane
- SUPPORT, //artillery
- SPECIAL, //engineer, tank
- TRANSPORT, //clipper transport, steam transport
- LIGHT_SHIP, //commerce raider, cruiser, frigate
- BIG_SHIP //battleship, dreadnought, ironclad, manowar, monitor
- };
-
struct Unit : HasIdentifier {
using icon_t = uint32_t;
using sprite_t = std::string_view;
using sound_t = std::string_view;
private:
+ const std::string_view category;
const icon_t icon;
- const UnitCategory category; //type
const sprite_t sprite;
const bool active;
- const UnitType type; //unit_type
+ const std::string_view type;
const bool floating_flag;
const uint32_t priority;
@@ -60,16 +49,16 @@ namespace OpenVic {
const uint32_t supply_consumption_score;
protected:
- Unit(std::string_view identifier, UnitCategory category, UNIT_PARAMS);
+ Unit(std::string_view identifier, std::string_view category, UNIT_PARAMS);
public:
Unit(Unit&&) = default;
icon_t get_icon() const;
- UnitCategory get_category() const;
+ std::string_view get_category() const;
sprite_t get_sprite() const;
bool is_active() const;
- UnitType get_type() const;
+ std::string_view get_type() const;
bool has_floating_flag() const;
uint32_t get_priority() const;
@@ -120,10 +109,10 @@ namespace OpenVic {
const bool transport;
const sound_t move_sound;
const sound_t select_sound;
- const uint32_t colonial_points; //some
- const bool build_overseas; //can_build_overseas
+ const uint32_t colonial_points;
+ const bool build_overseas;
const uint32_t min_port_level;
- const int32_t limit_per_port; //-1 is unlimited
+ const int32_t limit_per_port;
const uint32_t hull;
const uint32_t gun_power;
@@ -155,15 +144,26 @@ namespace OpenVic {
struct UnitManager {
private:
+ GoodManager& good_manager;
IdentifierRegistry<Unit> units;
- bool _check_superclass_parameters(const std::string_view identifier, UnitCategory cat, UNIT_PARAMS);
+ bool _check_shared_parameters(const std::string_view identifier, UNIT_PARAMS);
public:
- UnitManager();
-
- bool add_land_unit(const std::string_view identifier, UnitCategory cat, UNIT_PARAMS, LAND_PARAMS);
- bool add_naval_unit(const std::string_view identifier, UnitCategory cat, UNIT_PARAMS, NAVY_PARAMS);
+ const std::unordered_set<std::string_view> allowed_unit_types { //TODO is this useful?
+ "infantry", //guard, infantry, irregular
+ "cavalry", //cavalry, cuirassier, dragoon, hussar, plane
+ "support", //artillery
+ "special", //engineer, tank
+ "transport", //clipper transport, steam transport
+ "light_ship", //commerce raider, cruiser, frigate
+ "big_ship" //battleship, dreadnought, ironclad, manowar, monitor
+ };
+
+ UnitManager(GoodManager& good_manager);
+
+ bool add_land_unit(const std::string_view identifier, UNIT_PARAMS, LAND_PARAMS);
+ bool add_naval_unit(const std::string_view identifier, UNIT_PARAMS, NAVY_PARAMS);
IDENTIFIER_REGISTRY_ACCESSORS(Unit, unit)
bool load_unit_file(ast::NodeCPtr root);