diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/headless/main.cpp | 11 | ||||
-rw-r--r-- | src/openvic-simulation/GameAdvancementHook.cpp (renamed from src/openvic/GameAdvancementHook.cpp) | 0 | ||||
-rw-r--r-- | src/openvic-simulation/GameAdvancementHook.hpp (renamed from src/openvic/GameAdvancementHook.hpp) | 0 | ||||
-rw-r--r-- | src/openvic-simulation/GameManager.cpp (renamed from src/openvic/GameManager.cpp) | 2 | ||||
-rw-r--r-- | src/openvic-simulation/GameManager.hpp (renamed from src/openvic/GameManager.hpp) | 6 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/Dataloader.cpp (renamed from src/openvic/dataloader/Dataloader.cpp) | 4 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/Dataloader.hpp (renamed from src/openvic/dataloader/Dataloader.hpp) | 0 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/NodeTools.cpp (renamed from src/openvic/dataloader/NodeTools.cpp) | 26 | ||||
-rw-r--r-- | src/openvic-simulation/dataloader/NodeTools.hpp (renamed from src/openvic/dataloader/NodeTools.hpp) | 39 | ||||
-rw-r--r-- | src/openvic-simulation/economy/Good.cpp (renamed from src/openvic/economy/Good.cpp) | 36 | ||||
-rw-r--r-- | src/openvic-simulation/economy/Good.hpp (renamed from src/openvic/economy/Good.hpp) | 19 | ||||
-rw-r--r-- | src/openvic-simulation/map/Building.cpp (renamed from src/openvic/map/Building.cpp) | 12 | ||||
-rw-r--r-- | src/openvic-simulation/map/Building.hpp (renamed from src/openvic/map/Building.hpp) | 7 | ||||
-rw-r--r-- | src/openvic-simulation/map/Map.cpp (renamed from src/openvic/map/Map.cpp) | 24 | ||||
-rw-r--r-- | src/openvic-simulation/map/Map.hpp (renamed from src/openvic/map/Map.hpp) | 8 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.cpp (renamed from src/openvic/map/Province.cpp) | 18 | ||||
-rw-r--r-- | src/openvic-simulation/map/Province.hpp (renamed from src/openvic/map/Province.hpp) | 9 | ||||
-rw-r--r-- | src/openvic-simulation/map/Region.cpp (renamed from src/openvic/map/Region.cpp) | 0 | ||||
-rw-r--r-- | src/openvic-simulation/map/Region.hpp (renamed from src/openvic/map/Region.hpp) | 2 | ||||
-rw-r--r-- | src/openvic-simulation/pop/Culture.cpp (renamed from src/openvic/pop/Culture.cpp) | 144 | ||||
-rw-r--r-- | src/openvic-simulation/pop/Culture.hpp (renamed from src/openvic/pop/Culture.hpp) | 22 | ||||
-rw-r--r-- | src/openvic-simulation/pop/Pop.cpp (renamed from src/openvic/pop/Pop.cpp) | 42 | ||||
-rw-r--r-- | src/openvic-simulation/pop/Pop.hpp (renamed from src/openvic/pop/Pop.hpp) | 9 | ||||
-rw-r--r-- | src/openvic-simulation/pop/Religion.cpp (renamed from src/openvic/pop/Religion.cpp) | 32 | ||||
-rw-r--r-- | src/openvic-simulation/pop/Religion.hpp (renamed from src/openvic/pop/Religion.hpp) | 14 | ||||
-rw-r--r-- | src/openvic-simulation/types/Colour.hpp (renamed from src/openvic/types/Colour.hpp) | 0 | ||||
-rw-r--r-- | src/openvic-simulation/types/Date.cpp (renamed from src/openvic/types/Date.cpp) | 4 | ||||
-rw-r--r-- | src/openvic-simulation/types/Date.hpp (renamed from src/openvic/types/Date.hpp) | 0 | ||||
-rw-r--r-- | src/openvic-simulation/types/IdentifierRegistry.cpp (renamed from src/openvic/types/IdentifierRegistry.cpp) | 0 | ||||
-rw-r--r-- | src/openvic-simulation/types/IdentifierRegistry.hpp (renamed from src/openvic/types/IdentifierRegistry.hpp) | 55 | ||||
-rw-r--r-- | src/openvic-simulation/types/fixed_point/FixedPoint.hpp | 584 | ||||
-rw-r--r-- | src/openvic-simulation/types/fixed_point/FixedPointLUT.hpp (renamed from src/openvic/types/fixed_point/FPLUT.hpp) | 13 | ||||
-rw-r--r-- | src/openvic-simulation/types/fixed_point/FixedPointLUT_sin.hpp | 42 | ||||
-rw-r--r-- | src/openvic-simulation/types/fixed_point/FixedPointMath.hpp | 11 | ||||
-rw-r--r-- | src/openvic-simulation/types/fixed_point/lut_generator/lut_generator.py | 63 | ||||
-rw-r--r-- | src/openvic-simulation/utility/BMP.cpp (renamed from src/openvic/utility/BMP.cpp) | 2 | ||||
-rw-r--r-- | src/openvic-simulation/utility/BMP.hpp (renamed from src/openvic/utility/BMP.hpp) | 2 | ||||
-rw-r--r-- | src/openvic-simulation/utility/Logger.cpp (renamed from src/openvic/utility/Logger.cpp) | 0 | ||||
-rw-r--r-- | src/openvic-simulation/utility/Logger.hpp (renamed from src/openvic/utility/Logger.hpp) | 0 | ||||
-rw-r--r-- | src/openvic-simulation/utility/NumberUtils.hpp (renamed from src/openvic/utility/NumberUtils.hpp) | 0 | ||||
-rw-r--r-- | src/openvic-simulation/utility/StringUtils.hpp (renamed from src/openvic/utility/StringUtils.hpp) | 0 | ||||
-rw-r--r-- | src/openvic/types/fixed_point/FP.hpp | 581 | ||||
-rw-r--r-- | src/openvic/types/fixed_point/FPLUT_sin_512.hpp | 58 | ||||
-rw-r--r-- | src/openvic/types/fixed_point/FPMath.hpp | 11 | ||||
-rw-r--r-- | src/openvic/types/fixed_point/lut_generator/lut_generator.py | 39 |
45 files changed, 898 insertions, 1053 deletions
diff --git a/src/headless/main.cpp b/src/headless/main.cpp index a5bfc03..3185001 100644 --- a/src/headless/main.cpp +++ b/src/headless/main.cpp @@ -1,8 +1,6 @@ -#ifdef OPENVIC_SIM_HEADLESS - -#include <openvic/GameManager.hpp> -#include <openvic/dataloader/Dataloader.hpp> -#include <openvic/utility/Logger.hpp> +#include <openvic-simulation/GameManager.hpp> +#include <openvic-simulation/dataloader/Dataloader.hpp> +#include <openvic-simulation/utility/Logger.hpp> using namespace OpenVic; @@ -70,8 +68,7 @@ int main(int argc, char const* argv[]) { std::cout << "!!! HEADLESS SIMULATION END !!!" << std::endl; - std::cout << "\nLoad returned: " << (ret ? "true" : "false") << std::endl; + std::cout << "\nLoad returned: " << (ret ? "SUCCESS" : "FAILURE") << std::endl; return ret ? 0 : -1; } -#endif diff --git a/src/openvic/GameAdvancementHook.cpp b/src/openvic-simulation/GameAdvancementHook.cpp index ac16158..ac16158 100644 --- a/src/openvic/GameAdvancementHook.cpp +++ b/src/openvic-simulation/GameAdvancementHook.cpp diff --git a/src/openvic/GameAdvancementHook.hpp b/src/openvic-simulation/GameAdvancementHook.hpp index 59e43a4..59e43a4 100644 --- a/src/openvic/GameAdvancementHook.hpp +++ b/src/openvic-simulation/GameAdvancementHook.hpp diff --git a/src/openvic/GameManager.cpp b/src/openvic-simulation/GameManager.cpp index 052b814..ee5050d 100644 --- a/src/openvic/GameManager.cpp +++ b/src/openvic-simulation/GameManager.cpp @@ -1,6 +1,6 @@ #include "GameManager.hpp" -#include "openvic/utility/Logger.hpp" +#include "openvic-simulation/utility/Logger.hpp" using namespace OpenVic; diff --git a/src/openvic/GameManager.hpp b/src/openvic-simulation/GameManager.hpp index af2ec4f..d221a99 100644 --- a/src/openvic/GameManager.hpp +++ b/src/openvic-simulation/GameManager.hpp @@ -1,8 +1,8 @@ #pragma once -#include "openvic/GameAdvancementHook.hpp" -#include "openvic/economy/Good.hpp" -#include "openvic/map/Map.hpp" +#include "openvic-simulation/GameAdvancementHook.hpp" +#include "openvic-simulation/economy/Good.hpp" +#include "openvic-simulation/map/Map.hpp" namespace OpenVic { struct GameManager { diff --git a/src/openvic/dataloader/Dataloader.cpp b/src/openvic-simulation/dataloader/Dataloader.cpp index 55994d3..334d5b8 100644 --- a/src/openvic/dataloader/Dataloader.cpp +++ b/src/openvic-simulation/dataloader/Dataloader.cpp @@ -1,7 +1,7 @@ #include "Dataloader.hpp" -#include "openvic/GameManager.hpp" -#include "openvic/utility/Logger.hpp" +#include "openvic-simulation/GameManager.hpp" +#include "openvic-simulation/utility/Logger.hpp" #include <openvic-dataloader/csv/Parser.hpp> #include <openvic-dataloader/detail/CallbackOStream.hpp> diff --git a/src/openvic/dataloader/Dataloader.hpp b/src/openvic-simulation/dataloader/Dataloader.hpp index f723803..f723803 100644 --- a/src/openvic/dataloader/Dataloader.hpp +++ b/src/openvic-simulation/dataloader/Dataloader.hpp diff --git a/src/openvic/dataloader/NodeTools.cpp b/src/openvic-simulation/dataloader/NodeTools.cpp index 499527f..3b05d04 100644 --- a/src/openvic/dataloader/NodeTools.cpp +++ b/src/openvic-simulation/dataloader/NodeTools.cpp @@ -6,7 +6,7 @@ using namespace OpenVic; using namespace OpenVic::NodeTools; template<typename T> -static node_callback_t _expect_type(std::function<bool(T const&)> callback) { +static node_callback_t _expect_type(callback_t<T const&> callback) { return [callback](ast::NodeCPtr node) -> bool { if (node != nullptr) { T const* cast_node = node->cast_to<T>(); @@ -23,21 +23,21 @@ static node_callback_t _expect_type(std::function<bool(T const&)> callback) { template<typename T = ast::AbstractStringNode> requires(std::derived_from<T, ast::AbstractStringNode>) -static std::function<bool(T const&)> abstract_string_node_callback(std::function<bool(std::string_view)> callback) { +static callback_t<T const&> abstract_string_node_callback(callback_t<std::string_view> callback) { return [callback](T const& node) -> bool { return callback(node._name); }; } -node_callback_t NodeTools::expect_identifier(std::function<bool(std::string_view)> callback) { +node_callback_t NodeTools::expect_identifier(callback_t<std::string_view> callback) { return _expect_type<ast::IdentifierNode>(abstract_string_node_callback<ast::IdentifierNode>(callback)); } -node_callback_t NodeTools::expect_string(std::function<bool(std::string_view)> callback) { +node_callback_t NodeTools::expect_string(callback_t<std::string_view> callback) { return _expect_type<ast::StringNode>(abstract_string_node_callback<ast::StringNode>(callback)); } -node_callback_t NodeTools::expect_identifier_or_string(std::function<bool(std::string_view)> callback) { +node_callback_t NodeTools::expect_identifier_or_string(callback_t<std::string_view> callback) { return [callback](ast::NodeCPtr node) -> bool { if (node != nullptr) { ast::AbstractStringNode const* cast_node = node->cast_to<ast::IdentifierNode>(); @@ -55,7 +55,7 @@ node_callback_t NodeTools::expect_identifier_or_string(std::function<bool(std::s }; } -node_callback_t NodeTools::expect_bool(std::function<bool(bool)> callback) { +node_callback_t NodeTools::expect_bool(callback_t<bool> callback) { return expect_identifier( [callback](std::string_view identifier) -> bool { if (identifier == "yes") { @@ -69,7 +69,7 @@ node_callback_t NodeTools::expect_bool(std::function<bool(bool)> callback) { ); } -node_callback_t NodeTools::expect_int(std::function<bool(int64_t)> callback) { +node_callback_t NodeTools::expect_int(callback_t<int64_t> callback) { return expect_identifier( [callback](std::string_view identifier) -> bool { bool successful = false; @@ -83,7 +83,7 @@ node_callback_t NodeTools::expect_int(std::function<bool(int64_t)> callback) { ); } -node_callback_t NodeTools::expect_uint(std::function<bool(uint64_t)> callback) { +node_callback_t NodeTools::expect_uint(callback_t<uint64_t> callback) { return expect_identifier( [callback](std::string_view identifier) -> bool { bool successful = false; @@ -97,11 +97,11 @@ node_callback_t NodeTools::expect_uint(std::function<bool(uint64_t)> callback) { ); } -node_callback_t NodeTools::expect_fixed_point(std::function<bool(FP)> callback) { +node_callback_t NodeTools::expect_fixed_point(callback_t<fixed_point_t> callback) { return expect_identifier( [callback](std::string_view identifier) -> bool { bool successful = false; - const FP val = FP::parse(identifier.data(), identifier.length(), &successful); + const fixed_point_t val = fixed_point_t::parse(identifier.data(), identifier.length(), &successful); if (successful) { return callback(val); } @@ -111,13 +111,13 @@ node_callback_t NodeTools::expect_fixed_point(std::function<bool(FP)> callback) ); } -node_callback_t NodeTools::expect_colour(std::function<bool(colour_t)> callback) { +node_callback_t NodeTools::expect_colour(callback_t<colour_t> callback) { return [callback](ast::NodeCPtr node) -> bool { colour_t col = NULL_COLOUR; uint32_t components = 0; bool ret = expect_list_of_length(3, expect_fixed_point( - [&col, &components](FP val) -> bool { + [&col, &components](fixed_point_t val) -> bool { components++; col <<= 8; if (val < 0 || val > 255) { @@ -136,7 +136,7 @@ node_callback_t NodeTools::expect_colour(std::function<bool(colour_t)> callback) }; } -node_callback_t NodeTools::expect_date(std::function<bool(Date)> callback) { +node_callback_t NodeTools::expect_date(callback_t<Date> callback) { return expect_identifier( [callback](std::string_view identifier) -> bool { bool successful = false; diff --git a/src/openvic/dataloader/NodeTools.hpp b/src/openvic-simulation/dataloader/NodeTools.hpp index daea8ce..805de94 100644 --- a/src/openvic/dataloader/NodeTools.hpp +++ b/src/openvic-simulation/dataloader/NodeTools.hpp @@ -2,9 +2,9 @@ #include <map> -#include "openvic/types/Colour.hpp" -#include "openvic/types/Date.hpp" -#include "openvic/types/fixed_point/FP.hpp" +#include "openvic-simulation/types/Colour.hpp" +#include "openvic-simulation/types/Date.hpp" +#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" #include <openvic-dataloader/v2script/AbstractSyntaxTree.hpp> @@ -13,20 +13,23 @@ namespace OpenVic { namespace NodeTools { - using node_callback_t = std::function<bool(ast::NodeCPtr)>; + template<typename... Args> + using callback_t = std::function<bool(Args...)>; + + using node_callback_t = callback_t<ast::NodeCPtr>; constexpr bool success_callback(ast::NodeCPtr) { return true; } - using key_value_callback_t = std::function<bool(std::string_view, ast::NodeCPtr)>; - - node_callback_t expect_identifier(std::function<bool(std::string_view)> callback); - node_callback_t expect_string(std::function<bool(std::string_view)> callback); - node_callback_t expect_identifier_or_string(std::function<bool(std::string_view)> callback); - node_callback_t expect_bool(std::function<bool(bool)> callback); - node_callback_t expect_int(std::function<bool(int64_t)> callback); - node_callback_t expect_uint(std::function<bool(uint64_t)> callback); - node_callback_t expect_fixed_point(std::function<bool(FP)> callback); - node_callback_t expect_colour(std::function<bool(colour_t)> callback); - node_callback_t expect_date(std::function<bool(Date)> callback); + using key_value_callback_t = callback_t<std::string_view, ast::NodeCPtr>; + + node_callback_t expect_identifier(callback_t<std::string_view> callback); + node_callback_t expect_string(callback_t<std::string_view> callback); + node_callback_t expect_identifier_or_string(callback_t<std::string_view> callback); + node_callback_t expect_bool(callback_t<bool> callback); + node_callback_t expect_int(callback_t<int64_t> callback); + node_callback_t expect_uint(callback_t<uint64_t> callback); + node_callback_t expect_fixed_point(callback_t<fixed_point_t> callback); + node_callback_t expect_colour(callback_t<colour_t> callback); + node_callback_t expect_date(callback_t<Date> callback); node_callback_t expect_assign(key_value_callback_t callback); using length_callback_t = std::function<size_t(size_t)>; @@ -123,7 +126,7 @@ namespace OpenVic { node_callback_t name_list_callback(std::vector<std::string>& list); template<typename T> - std::function<bool(T)> assign_variable_callback(T& var) { + callback_t<T> assign_variable_callback(T& var) { return [&var](T val) -> bool { var = val; return true; @@ -132,7 +135,7 @@ namespace OpenVic { template<typename T> requires(std::integral<T>) - std::function<bool(uint64_t)> assign_variable_callback_uint(const std::string_view name, T& var) { + callback_t<uint64_t> assign_variable_callback_uint(const std::string_view name, T& var) { return [&var, name](uint64_t val) -> bool { if (val <= std::numeric_limits<T>::max()) { var = val; @@ -145,7 +148,7 @@ namespace OpenVic { template<typename T> requires(std::integral<T>) - std::function<bool(int64_t)> assign_variable_callback_int(const std::string_view name, T& var) { + callback_t<int64_t> assign_variable_callback_int(const std::string_view name, T& var) { return [&var, name](int64_t val) -> bool { if (std::numeric_limits<T>::lowest() <= val && val <= std::numeric_limits<T>::max()) { var = val; diff --git a/src/openvic/economy/Good.cpp b/src/openvic-simulation/economy/Good.cpp index 8ecaae0..e3dbd3e 100644 --- a/src/openvic/economy/Good.cpp +++ b/src/openvic-simulation/economy/Good.cpp @@ -66,22 +66,6 @@ bool GoodManager::add_good_category(const std::string_view identifier) { return good_categories.add_item({ identifier }); } -void GoodManager::lock_good_categories() { - good_categories.lock(); -} - -GoodCategory const* GoodManager::get_good_category_by_identifier(const std::string_view identifier) const { - return good_categories.get_item_by_identifier(identifier); -} - -size_t GoodManager::get_good_category_count() const { - return good_categories.size(); -} - -std::vector<GoodCategory> const& GoodManager::get_good_categories() const { - return good_categories.get_items(); -} - bool GoodManager::add_good(const std::string_view identifier, colour_t colour, GoodCategory const* category, Good::price_t base_price, bool available_from_start, bool tradeable, bool money, bool overseas_penalty) { if (identifier.empty()) { @@ -103,26 +87,6 @@ bool GoodManager::add_good(const std::string_view identifier, colour_t colour, G return goods.add_item({ identifier, colour, *category, base_price, available_from_start, tradeable, money, overseas_penalty }); } -void GoodManager::lock_goods() { - goods.lock(); -} - -Good const* GoodManager::get_good_by_index(size_t index) const { - return goods.get_item_by_index(index); -} - -Good const* GoodManager::get_good_by_identifier(const std::string_view identifier) const { - return goods.get_item_by_identifier(identifier); -} - -size_t GoodManager::get_good_count() const { - return goods.size(); -} - -std::vector<Good> const& GoodManager::get_goods() const { - return goods.get_items(); -} - void GoodManager::reset_to_defaults() { for (Good& good : goods.get_items()) good.reset_to_defaults(); diff --git a/src/openvic/economy/Good.hpp b/src/openvic-simulation/economy/Good.hpp index f04c9b2..d5cd532 100644 --- a/src/openvic/economy/Good.hpp +++ b/src/openvic-simulation/economy/Good.hpp @@ -1,7 +1,7 @@ #pragma once -#include "openvic/types/IdentifierRegistry.hpp" -#include "openvic/dataloader/NodeTools.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" namespace OpenVic { struct GoodManager; @@ -31,8 +31,8 @@ namespace OpenVic { struct Good : HasIdentifierAndColour { friend struct GoodManager; - using price_t = FP; - static constexpr price_t NULL_PRICE = FP::_0(); + using price_t = fixed_point_t; + static constexpr price_t NULL_PRICE = fixed_point_t::_0(); private: GoodCategory const& category; @@ -67,18 +67,11 @@ namespace OpenVic { GoodManager(); bool add_good_category(const std::string_view identifier); - void lock_good_categories(); - GoodCategory const* get_good_category_by_identifier(const std::string_view identifier) const; - size_t get_good_category_count() const; - std::vector<GoodCategory> const& get_good_categories() const; + IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(GoodCategory, good_category, good_categories) bool add_good(const std::string_view identifier, colour_t colour, GoodCategory const* category, Good::price_t base_price, bool available_from_start, bool tradeable, bool money, bool overseas_penalty); - void lock_goods(); - Good const* get_good_by_index(size_t index) const; - Good const* get_good_by_identifier(const std::string_view identifier) const; - size_t get_good_count() const; - std::vector<Good> const& get_goods() const; + IDENTIFIER_REGISTRY_ACCESSORS(Good, good) void reset_to_defaults(); bool load_good_file(ast::NodeCPtr root); diff --git a/src/openvic/map/Building.cpp b/src/openvic-simulation/map/Building.cpp index 72bccc1..6e5cf18 100644 --- a/src/openvic/map/Building.cpp +++ b/src/openvic-simulation/map/Building.cpp @@ -2,8 +2,8 @@ #include <cassert> -#include "openvic/map/Province.hpp" -#include "openvic/utility/Logger.hpp" +#include "openvic-simulation/map/Province.hpp" +#include "openvic-simulation/utility/Logger.hpp" using namespace OpenVic; @@ -110,14 +110,6 @@ bool BuildingManager::add_building_type(const std::string_view identifier, Build return building_types.add_item({ identifier, max_level, build_time }); } -void BuildingManager::lock_building_types() { - building_types.lock(); -} - -BuildingType const* BuildingManager::get_building_type_by_identifier(const std::string_view identifier) const { - return building_types.get_item_by_identifier(identifier); -} - bool BuildingManager::generate_province_buildings(Province& province) const { province.reset_buildings(); if (!building_types.is_locked()) { diff --git a/src/openvic/map/Building.hpp b/src/openvic-simulation/map/Building.hpp index d1c0348..d36dfd4 100644 --- a/src/openvic/map/Building.hpp +++ b/src/openvic-simulation/map/Building.hpp @@ -2,8 +2,8 @@ #include <vector> -#include "openvic/types/Date.hpp" -#include "openvic/types/IdentifierRegistry.hpp" +#include "openvic-simulation/types/Date.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" namespace OpenVic { @@ -79,8 +79,7 @@ namespace OpenVic { BuildingManager(); bool add_building_type(const std::string_view identifier, Building::level_t max_level, Timespan build_time); - void lock_building_types(); - BuildingType const* get_building_type_by_identifier(const std::string_view identifier) const; + IDENTIFIER_REGISTRY_ACCESSORS(BuildingType, building_type) bool generate_province_buildings(Province& province) const; }; } diff --git a/src/openvic/map/Map.cpp b/src/openvic-simulation/map/Map.cpp index 6375691..3f86ccc 100644 --- a/src/openvic/map/Map.cpp +++ b/src/openvic-simulation/map/Map.cpp @@ -3,8 +3,8 @@ #include <cassert> #include <unordered_set> -#include "openvic/economy/Good.hpp" -#include "openvic/utility/Logger.hpp" +#include "openvic-simulation/economy/Good.hpp" +#include "openvic-simulation/utility/Logger.hpp" using namespace OpenVic; @@ -365,26 +365,6 @@ bool Map::add_mapmode(const std::string_view identifier, Mapmode::colour_func_t return mapmodes.add_item({ identifier, mapmodes.size(), colour_func }); } -void Map::lock_mapmodes() { - mapmodes.lock(); -} - -size_t Map::get_mapmode_count() const { - return mapmodes.size(); -} - -std::vector<Mapmode> const& Map::get_mapmodes() const { - return mapmodes.get_items(); -} - -Mapmode const* Map::get_mapmode_by_index(size_t index) const { - return mapmodes.get_item_by_index(index); -} - -Mapmode const* Map::get_mapmode_by_identifier(const std::string_view identifier) const { - return mapmodes.get_item_by_identifier(identifier); -} - bool Map::generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) const { if (target == nullptr) { Logger::error("Mapmode colour target pointer is null!"); diff --git a/src/openvic/map/Map.hpp b/src/openvic-simulation/map/Map.hpp index 4684226..71719e2 100644 --- a/src/openvic/map/Map.hpp +++ b/src/openvic-simulation/map/Map.hpp @@ -4,7 +4,7 @@ #include <openvic-dataloader/csv/LineObject.hpp> -#include "openvic/map/Region.hpp" +#include "openvic-simulation/map/Region.hpp" namespace OpenVic { @@ -98,11 +98,7 @@ namespace OpenVic { std::vector<shape_pixel_t> const& get_province_shape_image() const; bool add_mapmode(const std::string_view identifier, Mapmode::colour_func_t colour_func); - void lock_mapmodes(); - size_t get_mapmode_count() const; - std::vector<Mapmode> const& get_mapmodes() const; - Mapmode const* get_mapmode_by_index(Mapmode::index_t index) const; - Mapmode const* get_mapmode_by_identifier(const std::string_view identifier) const; + IDENTIFIER_REGISTRY_ACCESSORS(Mapmode, mapmode) static constexpr size_t MAPMODE_COLOUR_SIZE = 4; bool generate_mapmode_colours(Mapmode::index_t index, uint8_t* target) const; diff --git a/src/openvic/map/Province.cpp b/src/openvic-simulation/map/Province.cpp index cfdc263..43eddd1 100644 --- a/src/openvic/map/Province.cpp +++ b/src/openvic-simulation/map/Province.cpp @@ -10,7 +10,7 @@ using namespace OpenVic::NodeTools; Province::Province(const std::string_view new_identifier, colour_t new_colour, index_t new_index) : HasIdentifierAndColour { new_identifier, new_colour, false }, index { new_index }, - buildings { "buildings" } { + buildings { "buildings", false } { assert(index != NULL_INDEX); } @@ -34,26 +34,10 @@ bool Province::add_building(Building&& building) { return buildings.add_item(std::move(building)); } -void Province::lock_buildings() { - buildings.lock(false); -} - void Province::reset_buildings() { buildings.reset(); } -Building const* Province::get_building_by_identifier(const std::string_view identifier) const { - return buildings.get_item_by_identifier(identifier); -} - -size_t Province::get_building_count() const { - return buildings.size(); -} - -std::vector<Building> const& Province::get_buildings() const { - return buildings.get_items(); -} - bool Province::expand_building(const std::string_view building_type_identifier) { Building* building = buildings.get_item_by_identifier(building_type_identifier); if (building == nullptr) return false; diff --git a/src/openvic/map/Province.hpp b/src/openvic-simulation/map/Province.hpp index f64bca2..682fc16 100644 --- a/src/openvic/map/Province.hpp +++ b/src/openvic-simulation/map/Province.hpp @@ -1,7 +1,7 @@ #pragma once -#include "openvic/map/Building.hpp" -#include "openvic/pop/Pop.hpp" +#include "openvic-simulation/map/Building.hpp" +#include "openvic-simulation/pop/Pop.hpp" namespace OpenVic { struct Map; @@ -42,11 +42,8 @@ namespace OpenVic { bool is_water() const; life_rating_t get_life_rating() const; bool add_building(Building&& building); - void lock_buildings(); + IDENTIFIER_REGISTRY_ACCESSORS(Building, building) void reset_buildings(); - Building const* get_building_by_identifier(const std::string_view identifier) const; - size_t get_building_count() const; - std::vector<Building> const& get_buildings() const; bool expand_building(const std::string_view building_type_identifier); Good const* get_rgo() const; std::string to_string() const; diff --git a/src/openvic/map/Region.cpp b/src/openvic-simulation/map/Region.cpp index 33092c5..33092c5 100644 --- a/src/openvic/map/Region.cpp +++ b/src/openvic-simulation/map/Region.cpp diff --git a/src/openvic/map/Region.hpp b/src/openvic-simulation/map/Region.hpp index a3640ca..2fccf06 100644 --- a/src/openvic/map/Region.hpp +++ b/src/openvic-simulation/map/Region.hpp @@ -1,6 +1,6 @@ #pragma once -#include "openvic/map/Province.hpp" +#include "openvic-simulation/map/Province.hpp" namespace OpenVic { diff --git a/src/openvic/pop/Culture.cpp b/src/openvic-simulation/pop/Culture.cpp index d86d608..7b595fb 100644 --- a/src/openvic/pop/Culture.cpp +++ b/src/openvic-simulation/pop/Culture.cpp @@ -1,6 +1,6 @@ #include "Culture.hpp" -#include "openvic/dataloader/NodeTools.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -58,22 +58,6 @@ bool CultureManager::add_graphical_culture_type(const std::string_view identifie return graphical_culture_types.add_item({ identifier }); } -void CultureManager::lock_graphical_culture_types() { - graphical_culture_types.lock(); -} - -GraphicalCultureType const* CultureManager::get_graphical_culture_type_by_identifier(const std::string_view identifier) const { - return graphical_culture_types.get_item_by_identifier(identifier); -} - -size_t CultureManager::get_graphical_culture_type_count() const { - return graphical_culture_types.size(); -} - -std::vector<GraphicalCultureType> const& CultureManager::get_graphical_culture_types() const { - return graphical_culture_types.get_items(); -} - bool CultureManager::add_culture_group(const std::string_view identifier, const std::string_view leader, GraphicalCultureType const* graphical_culture_type, bool is_overseas) { if (!graphical_culture_types.is_locked()) { Logger::error("Cannot register culture groups until graphical culture types are locked!"); @@ -94,22 +78,6 @@ bool CultureManager::add_culture_group(const std::string_view identifier, const return culture_groups.add_item({ identifier, leader, *graphical_culture_type, is_overseas }); } -void CultureManager::lock_culture_groups() { - culture_groups.lock(); -} - -CultureGroup const* CultureManager::get_culture_group_by_identifier(const std::string_view identifier) const { - return culture_groups.get_item_by_identifier(identifier); -} - -size_t CultureManager::get_culture_group_count() const { - return culture_groups.size(); -} - -std::vector<CultureGroup> const& CultureManager::get_culture_groups() const { - return culture_groups.get_items(); -} - bool CultureManager::add_culture(const std::string_view identifier, colour_t colour, CultureGroup const* group, std::vector<std::string> const& first_names, std::vector<std::string> const& last_names) { if (!culture_groups.is_locked()) { Logger::error("Cannot register cultures until culture groups are locked!"); @@ -130,22 +98,6 @@ bool CultureManager::add_culture(const std::string_view identifier, colour_t col return cultures.add_item({ identifier, colour, *group, first_names, last_names }); } -void CultureManager::lock_cultures() { - cultures.lock(); -} - -Culture const* CultureManager::get_culture_by_identifier(const std::string_view identifier) const { - return cultures.get_item_by_identifier(identifier); -} - -size_t CultureManager::get_culture_count() const { - return cultures.size(); -} - -std::vector<Culture> const& CultureManager::get_cultures() const { - return cultures.get_items(); -} - bool CultureManager::load_graphical_culture_type_file(ast::NodeCPtr root) { const bool ret = expect_list_reserve_length( graphical_culture_types, @@ -157,6 +109,58 @@ bool CultureManager::load_graphical_culture_type_file(ast::NodeCPtr root) { return ret; } +bool CultureManager::_load_culture_group(size_t& total_expected_cultures, + GraphicalCultureType const* default_unit_graphical_culture_type, + const std::string_view culture_group_key, ast::NodeCPtr culture_group_node) { + + std::string_view leader; + GraphicalCultureType const* unit_graphical_culture_type = default_unit_graphical_culture_type; + bool is_overseas = true; + + bool ret = expect_dictionary_keys_and_length( + [&total_expected_cultures](size_t size) -> size_t { + total_expected_cultures += size; + return size; + }, + ALLOW_OTHER_KEYS, + "leader", ONE_EXACTLY, [&total_expected_cultures, &leader](ast::NodeCPtr node) -> bool { + total_expected_cultures--; + return expect_identifier(assign_variable_callback(leader))(node); + }, + "unit", ZERO_OR_ONE, [this, &total_expected_cultures, &unit_graphical_culture_type](ast::NodeCPtr node) -> bool { + total_expected_cultures--; + return expect_graphical_culture_type(unit_graphical_culture_type)(node); + }, + "union", ZERO_OR_ONE, [&total_expected_cultures](ast::NodeCPtr) -> bool { + total_expected_cultures--; + return true; + }, + "is_overseas", ZERO_OR_ONE, [&total_expected_cultures, &is_overseas](ast::NodeCPtr node) -> bool { + total_expected_cultures--; + return expect_bool(assign_variable_callback(is_overseas))(node); + } + )(culture_group_node); + ret &= add_culture_group(culture_group_key, leader, unit_graphical_culture_type, is_overseas); + return ret; +} + +bool CultureManager::_load_culture(CultureGroup const* culture_group, + const std::string_view culture_key, ast::NodeCPtr culture_node) { + + colour_t colour = NULL_COLOUR; + std::vector<std::string> first_names, last_names; + + bool ret = expect_dictionary_keys( + "color", ONE_EXACTLY, expect_colour(assign_variable_callback(colour)), + "first_names", ONE_EXACTLY, name_list_callback(first_names), + "last_names", ONE_EXACTLY, name_list_callback(last_names), + "radicalism", ZERO_OR_ONE, success_callback, + "primary", ZERO_OR_ONE, success_callback + )(culture_node); + ret &= add_culture(culture_key, colour, culture_group, first_names, last_names); + return ret; +} + bool CultureManager::load_culture_file(ast::NodeCPtr root) { if (!graphical_culture_types.is_locked()) { Logger::error("Cannot load culture groups until graphical culture types are locked!"); @@ -173,31 +177,7 @@ bool CultureManager::load_culture_file(ast::NodeCPtr root) { bool ret = expect_dictionary_reserve_length( culture_groups, [this, default_unit_graphical_culture_type, &total_expected_cultures](std::string_view key, ast::NodeCPtr value) -> bool { - std::string_view leader; - GraphicalCultureType const* unit_graphical_culture_type = default_unit_graphical_culture_type; - bool is_overseas = true; - - bool ret = expect_dictionary_keys_and_length( - [&total_expected_cultures](size_t size) -> size_t { - total_expected_cultures += size; - return size; - }, - ALLOW_OTHER_KEYS, - "leader", ONE_EXACTLY, expect_identifier(assign_variable_callback(leader)), - "unit", ZERO_OR_ONE, - expect_identifier( - [this, &unit_graphical_culture_type](std::string_view identifier) -> bool { - unit_graphical_culture_type = get_graphical_culture_type_by_identifier(identifier); - if (unit_graphical_culture_type != nullptr) return true; - Logger::error("Invalid unit graphical culture type: ", identifier); - return false; - } - ), - "union", ZERO_OR_ONE, success_callback, - "is_overseas", ZERO_OR_ONE, expect_bool(assign_variable_callback(is_overseas)) - )(value); - ret &= add_culture_group(key, leader, unit_graphical_culture_type, is_overseas); - return ret; + return _load_culture_group(total_expected_cultures, default_unit_graphical_culture_type, key, value); } )(root); lock_culture_groups(); @@ -205,25 +185,11 @@ bool CultureManager::load_culture_file(ast::NodeCPtr root) { ret &= expect_dictionary( [this](std::string_view culture_group_key, ast::NodeCPtr culture_group_value) -> bool { - CultureGroup const* culture_group = get_culture_group_by_identifier(culture_group_key); - return expect_dictionary( [this, culture_group](std::string_view key, ast::NodeCPtr value) -> bool { if (key == "leader" || key == "unit" || key == "union" || key == "is_overseas") return true; - - colour_t colour = NULL_COLOUR; - std::vector<std::string> first_names, last_names; - - bool ret = expect_dictionary_keys( - "color", ONE_EXACTLY, expect_colour(assign_variable_callback(colour)), - "first_names", ONE_EXACTLY, name_list_callback(first_names), - "last_names", ONE_EXACTLY, name_list_callback(last_names), - "radicalism", ZERO_OR_ONE, success_callback, - "primary", ZERO_OR_ONE, success_callback - )(value); - ret &= add_culture(key, colour, culture_group, first_names, last_names); - return ret; + return _load_culture(culture_group, key, value); } )(culture_group_value); } diff --git a/src/openvic/pop/Culture.hpp b/src/openvic-simulation/pop/Culture.hpp index cc6c2a6..d27c7c9 100644 --- a/src/openvic/pop/Culture.hpp +++ b/src/openvic-simulation/pop/Culture.hpp @@ -1,7 +1,6 @@ #pragma once -#include "openvic/dataloader/NodeTools.hpp" -#include "openvic/types/IdentifierRegistry.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" namespace OpenVic { @@ -62,26 +61,21 @@ namespace OpenVic { IdentifierRegistry<CultureGroup> culture_groups; IdentifierRegistry<Culture> cultures; + bool _load_culture_group(size_t& total_expected_cultures, GraphicalCultureType const* default_unit_graphical_culture_type, + const std::string_view culture_group_key, ast::NodeCPtr culture_group_node); + bool _load_culture(CultureGroup const* culture_group, const std::string_view culture_key, ast::NodeCPtr node); + public: CultureManager(); bool add_graphical_culture_type(const std::string_view identifier); - void lock_graphical_culture_types(); - GraphicalCultureType const* get_graphical_culture_type_by_identifier(const std::string_view identifier) const; - size_t get_graphical_culture_type_count() const; - std::vector<GraphicalCultureType> const& get_graphical_culture_types() const; + IDENTIFIER_REGISTRY_ACCESSORS(GraphicalCultureType, graphical_culture_type) bool add_culture_group(const std::string_view identifier, const std::string_view leader, GraphicalCultureType const* new_graphical_culture_type, bool is_overseas); - void lock_culture_groups(); - CultureGroup const* get_culture_group_by_identifier(const std::string_view identifier) const; - size_t get_culture_group_count() const; - std::vector<CultureGroup> const& get_culture_groups() const; + IDENTIFIER_REGISTRY_ACCESSORS(CultureGroup, culture_group) bool add_culture(const std::string_view identifier, colour_t colour, CultureGroup const* group, std::vector<std::string> const& first_names, std::vector<std::string> const& last_names); - void lock_cultures(); - Culture const* get_culture_by_identifier(const std::string_view identifier) const; - size_t get_culture_count() const; - std::vector<Culture> const& get_cultures() const; + IDENTIFIER_REGISTRY_ACCESSORS(Culture, culture) bool load_graphical_culture_type_file(ast::NodeCPtr root); bool load_culture_file(ast::NodeCPtr root); diff --git a/src/openvic/pop/Pop.cpp b/src/openvic-simulation/pop/Pop.cpp index fbe8708..7b4dd60 100644 --- a/src/openvic/pop/Pop.cpp +++ b/src/openvic-simulation/pop/Pop.cpp @@ -2,9 +2,9 @@ #include <cassert> -#include "openvic/dataloader/NodeTools.hpp" -#include "openvic/map/Province.hpp" -#include "openvic/utility/Logger.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/map/Province.hpp" +#include "openvic-simulation/utility/Logger.hpp" using namespace OpenVic; using namespace OpenVic::NodeTools; @@ -126,22 +126,6 @@ bool PopManager::add_pop_type(const std::string_view identifier, colour_t colour return pop_types.add_item({ identifier, colour, strata, sprite, max_size, merge_max_size, state_capital_only, demote_migrant, is_artisan, is_slave }); } -void PopManager::lock_pop_types() { - pop_types.lock(); -} - -PopType const* PopManager::get_pop_type_by_identifier(const std::string_view identifier) const { - return pop_types.get_item_by_identifier(identifier); -} - -size_t PopManager::get_pop_type_count() const { - return pop_types.size(); -} - -std::vector<PopType> const& PopManager::get_pop_types() const { - return pop_types.get_items(); -} - bool PopManager::load_pop_type_file(std::filesystem::path const& path, ast::NodeCPtr root) { // TODO - pop type loading @@ -160,24 +144,8 @@ bool PopManager::load_pop_into_province(Province& province, ast::NodeCPtr root) bool ret = expect_assign( [this, &culture, &religion, &size](std::string_view, ast::NodeCPtr pop_node) -> bool { return expect_dictionary_keys( - "culture", ONE_EXACTLY, - expect_identifier( - [&culture, this](std::string_view identifier) -> bool { - culture = culture_manager.get_culture_by_identifier(identifier); - if (culture != nullptr) return true; - Logger::error("Invalid pop culture: ", identifier); - return false; - } - ), - "religion", ONE_EXACTLY, - expect_identifier( - [&religion, this](std::string_view identifier) -> bool { - religion = religion_manager.get_religion_by_identifier(identifier); - if (religion != nullptr) return true; - Logger::error("Invalid pop religion: ", identifier); - return false; - } - ), + "culture", ONE_EXACTLY, culture_manager.expect_culture(culture), + "religion", ONE_EXACTLY, religion_manager.expect_religion(religion), "size", ONE_EXACTLY, expect_uint(assign_variable_callback_uint("pop size", size)), "militancy", ZERO_OR_ONE, success_callback, "rebel_type", ZERO_OR_ONE, success_callback diff --git a/src/openvic/pop/Pop.hpp b/src/openvic-simulation/pop/Pop.hpp index f4694f0..3c635a4 100644 --- a/src/openvic/pop/Pop.hpp +++ b/src/openvic-simulation/pop/Pop.hpp @@ -2,8 +2,8 @@ #include <filesystem> -#include "openvic/pop/Culture.hpp" -#include "openvic/pop/Religion.hpp" +#include "openvic-simulation/pop/Culture.hpp" +#include "openvic-simulation/pop/Religion.hpp" namespace OpenVic { @@ -93,10 +93,7 @@ namespace OpenVic { bool add_pop_type(const std::string_view identifier, colour_t new_colour, PopType::strata_t strata, PopType::sprite_t sprite, Pop::pop_size_t max_size, Pop::pop_size_t merge_max_size, bool state_capital_only, bool demote_migrant, bool is_artisan, bool is_slave); - void lock_pop_types(); - PopType const* get_pop_type_by_identifier(const std::string_view identifier) const; - size_t get_pop_type_count() const; - std::vector<PopType> const& get_pop_types() const; + IDENTIFIER_REGISTRY_ACCESSORS(PopType, pop_type) bool load_pop_type_file(std::filesystem::path const& path, ast::NodeCPtr root); bool load_pop_into_province(Province& province, ast::NodeCPtr root) const; diff --git a/src/openvic/pop/Religion.cpp b/src/openvic-simulation/pop/Religion.cpp index f7c657a..b336ae1 100644 --- a/src/openvic/pop/Religion.cpp +++ b/src/openvic-simulation/pop/Religion.cpp @@ -40,22 +40,6 @@ bool ReligionManager::add_religion_group(const std::string_view identifier) { return religion_groups.add_item({ identifier }); } -void ReligionManager::lock_religion_groups() { - religion_groups.lock(); -} - -ReligionGroup const* ReligionManager::get_religion_group_by_identifier(const std::string_view identifier) const { - return religion_groups.get_item_by_identifier(identifier); -} - -size_t ReligionManager::get_religion_group_count() const { - return religion_groups.size(); -} - -std::vector<ReligionGroup> const& ReligionManager::get_religion_groups() const { - return religion_groups.get_items(); -} - bool ReligionManager::add_religion(const std::string_view identifier, colour_t colour, ReligionGroup const* group, Religion::icon_t icon, bool pagan) { if (!religion_groups.is_locked()) { Logger::error("Cannot register religions until religion groups are locked!"); @@ -80,22 +64,6 @@ bool ReligionManager::add_religion(const std::string_view identifier, colour_t c return religions.add_item({ identifier, colour, *group, icon, pagan }); } -void ReligionManager::lock_religions() { - religions.lock(); -} - -Religion const* ReligionManager::get_religion_by_identifier(const std::string_view identifier) const { - return religions.get_item_by_identifier(identifier); -} - -size_t ReligionManager::get_religion_count() const { - return religions.size(); -} - -std::vector<Religion> const& ReligionManager::get_religions() const { - return religions.get_items(); -} - bool ReligionManager::load_religion_file(ast::NodeCPtr root) { size_t total_expected_religions = 0; diff --git a/src/openvic/pop/Religion.hpp b/src/openvic-simulation/pop/Religion.hpp index f04b035..bd65321 100644 --- a/src/openvic/pop/Religion.hpp +++ b/src/openvic-simulation/pop/Religion.hpp @@ -1,7 +1,7 @@ #pragma once -#include "openvic/types/IdentifierRegistry.hpp" -#include "openvic/dataloader/NodeTools.hpp" +#include "openvic-simulation/types/IdentifierRegistry.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" namespace OpenVic { @@ -46,16 +46,10 @@ namespace OpenVic { ReligionManager(); bool add_religion_group(const std::string_view identifier); - void lock_religion_groups(); - ReligionGroup const* get_religion_group_by_identifier(const std::string_view identifier) const; - size_t get_religion_group_count() const; - std::vector<ReligionGroup> const& get_religion_groups() const; + IDENTIFIER_REGISTRY_ACCESSORS(ReligionGroup, religion_group) bool add_religion(const std::string_view identifier, colour_t colour, ReligionGroup const* group, Religion::icon_t icon, bool pagan); - void lock_religions(); - Religion const* get_religion_by_identifier(const std::string_view identifier) const; - size_t get_religion_count() const; - std::vector<Religion> const& get_religions() const; + IDENTIFIER_REGISTRY_ACCESSORS(Religion, religion) bool load_religion_file(ast::NodeCPtr root); }; diff --git a/src/openvic/types/Colour.hpp b/src/openvic-simulation/types/Colour.hpp index 01f3852..01f3852 100644 --- a/src/openvic/types/Colour.hpp +++ b/src/openvic-simulation/types/Colour.hpp diff --git a/src/openvic/types/Date.cpp b/src/openvic-simulation/types/Date.cpp index 1f13808..3449591 100644 --- a/src/openvic/types/Date.cpp +++ b/src/openvic-simulation/types/Date.cpp @@ -5,8 +5,8 @@ #include <cctype> #include <charconv> -#include "openvic/utility/Logger.hpp" -#include "openvic/utility/StringUtils.hpp" +#include "openvic-simulation/utility/Logger.hpp" +#include "openvic-simulation/utility/StringUtils.hpp" using namespace OpenVic; diff --git a/src/openvic/types/Date.hpp b/src/openvic-simulation/types/Date.hpp index 601f9dc..601f9dc 100644 --- a/src/openvic/types/Date.hpp +++ b/src/openvic-simulation/types/Date.hpp diff --git a/src/openvic/types/IdentifierRegistry.cpp b/src/openvic-simulation/types/IdentifierRegistry.cpp index 6d5221b..6d5221b 100644 --- a/src/openvic/types/IdentifierRegistry.cpp +++ b/src/openvic-simulation/types/IdentifierRegistry.cpp diff --git a/src/openvic/types/IdentifierRegistry.hpp b/src/openvic-simulation/types/IdentifierRegistry.hpp index 502b74b..494ff3e 100644 --- a/src/openvic/types/IdentifierRegistry.hpp +++ b/src/openvic-simulation/types/IdentifierRegistry.hpp @@ -3,8 +3,8 @@ #include <map> #include <vector> -#include "openvic/types/Colour.hpp" -#include "openvic/utility/Logger.hpp" +#include "openvic-simulation/dataloader/NodeTools.hpp" +#include "openvic-simulation/utility/Logger.hpp" namespace OpenVic { /* @@ -77,12 +77,19 @@ namespace OpenVic { using identifier_index_map_t = std::map<std::string, size_t, std::less<void>>; const std::string name; + const bool log_lock; std::vector<T> items; bool locked = false; identifier_index_map_t identifier_index_map; public: - IdentifierRegistry(const std::string_view new_name) : name { new_name } {} + IdentifierRegistry(const std::string_view new_name, bool new_log_lock = true) + : name { new_name }, log_lock { new_log_lock } {} + + std::string const& get_name() const { + return name; + } + bool add_item(T&& item) { if (locked) { Logger::error("Cannot add item to the ", name, " registry - locked!"); @@ -97,28 +104,34 @@ namespace OpenVic { items.push_back(std::move(item)); return true; } - void lock(bool log = true) { + + void lock() { if (locked) { Logger::error("Failed to lock ", name, " registry - already locked!"); } else { locked = true; - if (log) Logger::info("Locked ", name, " registry after registering ", size(), " items"); + if (log_lock) Logger::info("Locked ", name, " registry after registering ", size(), " items"); } } + bool is_locked() const { return locked; } + void reset() { identifier_index_map.clear(); items.clear(); locked = false; } + size_t size() const { return items.size(); } + bool empty() const { return items.empty(); } + void reserve(size_t size) { if (locked) { Logger::error("Failed to reserve space for ", size, " items in ", name, " registry - already locked!"); @@ -126,27 +139,59 @@ namespace OpenVic { items.reserve(size); } } + T* get_item_by_identifier(const std::string_view identifier) { const identifier_index_map_t::const_iterator it = identifier_index_map.find(identifier); if (it != identifier_index_map.end()) return &items[it->second]; return nullptr; } + T const* get_item_by_identifier(const std::string_view identifier) const { const identifier_index_map_t::const_iterator it = identifier_index_map.find(identifier); if (it != identifier_index_map.end()) return &items[it->second]; return nullptr; } + T* get_item_by_index(size_t index) { return index < items.size() ? &items[index] : nullptr; } + T const* get_item_by_index(size_t index) const { return index < items.size() ? &items[index] : nullptr; } + std::vector<T>& get_items() { return items; } + std::vector<T> const& get_items() const { return items; } + + NodeTools::node_callback_t expect_item(T const*& ret) const { + return NodeTools::expect_identifier( + [this, &ret](std::string_view identifier) -> bool { + ret = get_item_by_identifier(identifier); + if (ret != nullptr) return true; + Logger::error("Invalid ", name, ": ", identifier); + return false; + } + ); + } }; + +#define IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(type, singular, plural) \ + void lock_##plural() { plural.lock(); } \ + type const* get_##singular##_by_index(size_t index) const { \ + return plural.get_item_by_index(index); } \ + type const* get_##singular##_by_identifier(const std::string_view identifier) const { \ + return plural.get_item_by_identifier(identifier); } \ + size_t get_##singular##_count() const { \ + return plural.size(); } \ + std::vector<type> const& get_##plural() const { \ + return plural.get_items(); } \ + NodeTools::node_callback_t expect_##singular(type const*& ret) const { \ + return plural.expect_item(ret); } + +#define IDENTIFIER_REGISTRY_ACCESSORS(type, name) IDENTIFIER_REGISTRY_ACCESSORS_CUSTOM_PLURAL(type, name, name##s) } diff --git a/src/openvic-simulation/types/fixed_point/FixedPoint.hpp b/src/openvic-simulation/types/fixed_point/FixedPoint.hpp new file mode 100644 index 0000000..913f237 --- /dev/null +++ b/src/openvic-simulation/types/fixed_point/FixedPoint.hpp @@ -0,0 +1,584 @@ +#pragma once + +#include <cerrno> +#include <cmath> +#include <cstdint> +#include <cstdlib> +#include <limits> +#include <string_view> + +#include "openvic-simulation/utility/Logger.hpp" +#include "openvic-simulation/utility/NumberUtils.hpp" +#include "openvic-simulation/utility/StringUtils.hpp" + +#include "FixedPointLUT.hpp" + +namespace OpenVic { + struct fixed_point_t { + static constexpr size_t SIZE = 8; + + static constexpr int32_t PRECISION = FPLUT::SIN_LUT_PRECISION; + static constexpr int64_t ONE = 1 << PRECISION; + + constexpr fixed_point_t() : value { 0 } {} + constexpr fixed_point_t(int64_t new_value) : value { new_value } {} + constexpr fixed_point_t(int32_t new_value) : value { static_cast<int64_t>(new_value) << PRECISION } {} + + // Trivial destructor + ~fixed_point_t() = default; + + static constexpr fixed_point_t max() { + return std::numeric_limits<int64_t>::max(); + } + + static constexpr fixed_point_t min() { + return std::numeric_limits<int64_t>::min(); + } + + static constexpr fixed_point_t usable_max() { + return static_cast<int64_t>(2147483648LL); + } + + static constexpr fixed_point_t usable_min() { + return -usable_max(); + } + + static constexpr fixed_point_t _0() { + return 0; + } + + static constexpr fixed_point_t _1() { + return 1; + } + + static constexpr fixed_point_t _2() { + return 2; + } + + static constexpr fixed_point_t _3() { + return 3; + } + + static constexpr fixed_point_t _4() { + return 4; + } + + static constexpr fixed_point_t _5() { + return 5; + } + + static constexpr fixed_point_t _6() { + return 6; + } + + static constexpr fixed_point_t _7() { + return 7; + } + + static constexpr fixed_point_t _8() { + return 8; + } + + static constexpr fixed_point_t _9() { + return 9; + } + + static constexpr fixed_point_t _10() { + return 10; + } + + static constexpr fixed_point_t _50() { + return 50; + } + + static constexpr fixed_point_t _100() { + return 100; + } + + static constexpr fixed_point_t _200() { + return 200; + } + + static constexpr fixed_point_t _0_01() { + return _1() / _100(); + } + + static constexpr fixed_point_t _0_02() { + return _0_01() * 2; + } + + static constexpr fixed_point_t _0_03() { + return _0_01() * 3; + } + + static constexpr fixed_point_t _0_04() { + return _0_01() * 4; + } + + static constexpr fixed_point_t _0_05() { + return _0_01() * 5; + } + + static constexpr fixed_point_t _0_10() { + return _1() / 10; + } + + static constexpr fixed_point_t _0_20() { + return _0_10() * 2; + } + + static constexpr fixed_point_t _0_25() { + return _1() / 4; + } + + static constexpr fixed_point_t _0_33() { + return _1() / 3; + } + + static constexpr fixed_point_t _0_50() { + return _1() / 2; + } + + static constexpr fixed_point_t _0_75() { + return _1() - _0_25(); + } + + static constexpr fixed_point_t _0_95() { + return _1() - _0_05(); + } + + static constexpr fixed_point_t _0_99() { + return _1() - _0_01(); + } + + static constexpr fixed_point_t _1_01() { + return _1() + _0_01(); + } + + static constexpr fixed_point_t _1_10() { + return _1() + _0_10(); + } + + static constexpr fixed_point_t _1_50() { + return _1() + _0_50(); + } + + static constexpr fixed_point_t minus_one() { + return -1; + } + + static constexpr fixed_point_t pi() { + return static_cast<int64_t>(205887LL); + } + + static constexpr fixed_point_t pi2() { + return pi() * 2; + } + + static constexpr fixed_point_t pi_quarter() { + return pi() / 4; + } + + static constexpr fixed_point_t pi_half() { + return pi() / 2; + } + + static constexpr fixed_point_t one_div_pi2() { + return 1 / pi2(); + } + + static constexpr fixed_point_t deg2rad() { + return static_cast<int64_t>(1143LL); + } + + static constexpr fixed_point_t rad2deg() { + return static_cast<int64_t>(3754936LL); + } + + static constexpr fixed_point_t e() { + return static_cast<int64_t>(178145LL); + } + + constexpr bool is_negative() const { + return value < 0; + } + + constexpr fixed_point_t abs() const { + return !is_negative() ? value : -value; + } + + // Doesn't account for sign, so -n.abc -> 1 - 0.abc + constexpr fixed_point_t get_frac() const { + return value & (ONE - 1); + } + + constexpr int64_t to_int64_t() const { + return value >> PRECISION; + } + + constexpr void set_raw_value(int64_t value) { + this->value = value; + } + + constexpr int64_t get_raw_value() const { + return value; + } + + constexpr int32_t to_int32_t() const { + return static_cast<int32_t>(to_int64_t()); + } + + constexpr float to_float() const { + return value / static_cast<float>(ONE); + } + + constexpr float to_float_rounded() const { + return static_cast<float>(NumberUtils::round_to_int64((value / static_cast<float>(ONE)) * 100000.0f)) / 100000.0f; + } + + constexpr double to_double() const { + return value / static_cast<double>(ONE); + } + + constexpr float to_double_rounded() const { + return NumberUtils::round_to_int64((value / static_cast<double>(ONE)) * 100000.0) / 100000.0; + } + + std::string to_string() const { + fixed_point_t val = abs(); + std::string str = std::to_string(val.to_int64_t()) + "."; + if (is_negative()) str = "-" + str; + val = val.get_frac(); + do { + val *= 10; + str.push_back('0' + static_cast<char>(val.to_int64_t())); + val = val.get_frac(); + } while (val > 0); + return str; + } + + // Deterministic + static constexpr fixed_point_t parse_raw(int64_t value) { + return value; + } + + // Deterministic + static constexpr fixed_point_t parse(int64_t value) { + return value << PRECISION; + } + + // Deterministic + static constexpr fixed_point_t parse(char const* str, char const* const end, bool* successful = nullptr) { + if (successful != nullptr) *successful = false; + + if (str == nullptr || str >= end) { + return _0(); + } + + bool negative = false; + + if (*str == '-') { + negative = true; + ++str; + if (str == end) return _0(); + } + + char const* dot_pointer = str; + while (*dot_pointer != '.' && ++dot_pointer != end); + + if (dot_pointer == str && dot_pointer + 1 == end) { + // Invalid: ".", "+." or "-." + return _0(); + } + + fixed_point_t result = _0(); + if (successful != nullptr) *successful = true; + + if (dot_pointer != str) { + // Non-empty integer part + bool int_successful = false; + result += parse_integer(str, dot_pointer, &int_successful); + if (!int_successful && successful != nullptr) *successful = false; + } + + if (dot_pointer + 1 < end) { + // Non-empty fractional part + bool frac_successful = false; + result += parse_fraction(dot_pointer + 1, end, &frac_successful); + if (!frac_successful && successful != nullptr) *successful = false; + } + + return negative ? -result : result; + } + + static constexpr fixed_point_t parse(char const* str, size_t length, bool* successful = nullptr) { + return parse(str, str + length, successful); + } + + static fixed_point_t parse(const std::string_view str, bool* successful = nullptr) { + return parse(str.data(), str.length(), successful); + } + + // Not Deterministic + static constexpr fixed_point_t parse_unsafe(float value) { + return static_cast<int64_t>(value * ONE + 0.5f * (value < 0 ? -1 : 1)); + } + + // Not Deterministic + static fixed_point_t parse_unsafe(char const* value) { + char* endpointer; + double double_value = std::strtod(value, &endpointer); + + if (*endpointer != '\0') { + Logger::error("Unsafe fixed point parse failed to parse the end of a string: \"", endpointer, "\""); + } + + int64_t integer_value = static_cast<long>(double_value * ONE + 0.5 * (double_value < 0 ? -1 : 1)); + + return integer_value; + } + + constexpr operator int32_t() const { + return to_int32_t(); + } + + constexpr operator int64_t() const { + return to_int64_t(); + } + + constexpr operator float() const { + return to_float(); + } + + constexpr operator double() const { + return to_double(); + } + + operator std::string() const { + return to_string(); + } + + friend std::ostream& operator<<(std::ostream& stream, fixed_point_t const& obj) { + return stream << obj.to_string(); + } + + constexpr friend fixed_point_t operator-(fixed_point_t const& obj) { + return -obj.value; + } + + constexpr friend fixed_point_t operator+(fixed_point_t const& obj) { + return +obj.value; + } + + constexpr friend fixed_point_t operator+(fixed_point_t const& lhs, fixed_point_t const& rhs) { + return lhs.value + rhs.value; + } + + constexpr friend fixed_point_t operator+(fixed_point_t const& lhs, int32_t const& rhs) { + return lhs.value + (static_cast<int64_t>(rhs) << PRECISION); + } + + constexpr friend fixed_point_t operator+(int32_t const& lhs, fixed_point_t const& rhs) { + return (static_cast<int64_t>(lhs) << PRECISION) + rhs.value; + } + + constexpr fixed_point_t operator+=(fixed_point_t const& obj) { + value += obj.value; + return *this; + } + + constexpr fixed_point_t operator+=(int32_t const& obj) { + value += (static_cast<int64_t>(obj) << PRECISION); + return *this; + } + + constexpr friend fixed_point_t operator-(fixed_point_t const& lhs, fixed_point_t const& rhs) { + return lhs.value - rhs.value; + } + + constexpr friend fixed_point_t operator-(fixed_point_t const& lhs, int32_t const& rhs) { + return lhs.value - (static_cast<int64_t>(rhs) << PRECISION); + } + + constexpr friend fixed_point_t operator-(int32_t const& lhs, fixed_point_t const& rhs) { + return (static_cast<int64_t>(lhs) << PRECISION) - rhs.value; + } + + constexpr fixed_point_t operator-=(fixed_point_t const& obj) { + value -= obj.value; + return *this; + } + + constexpr fixed_point_t operator-=(int32_t const& obj) { + value -= (static_cast<int64_t>(obj) << PRECISION); + return *this; + } + + constexpr friend fixed_point_t operator*(fixed_point_t const& lhs, fixed_point_t const& rhs) { + return lhs.value * rhs.value >> PRECISION; + } + + constexpr friend fixed_point_t operator*(fixed_point_t const& lhs, int32_t const& rhs) { + return lhs.value * rhs; + } + + constexpr friend fixed_point_t operator*(int32_t const& lhs, fixed_point_t const& rhs) { + return lhs * rhs.value; + } + + constexpr fixed_point_t operator*=(fixed_point_t const& obj) { + value *= obj.value >> PRECISION; + return *this; + } + + constexpr fixed_point_t operator*=(int32_t const& obj) { + value *= obj; + return *this; + } + + constexpr friend fixed_point_t operator/(fixed_point_t const& lhs, fixed_point_t const& rhs) { + return (lhs.value << PRECISION) / rhs.value; + } + + constexpr friend fixed_point_t operator/(fixed_point_t const& lhs, int32_t const& rhs) { + return lhs.value / rhs; + } + + constexpr friend fixed_point_t operator/(int32_t const& lhs, fixed_point_t const& rhs) { + return (static_cast<int64_t>(lhs) << (2 * PRECISION)) / rhs.value; + } + + constexpr fixed_point_t operator/=(fixed_point_t const& obj) { + value = (value << PRECISION) / obj.value; + return *this; + } + + constexpr fixed_point_t operator/=(int32_t const& obj) { + value /= obj; + return *this; + } + + constexpr friend fixed_point_t operator%(fixed_point_t const& lhs, fixed_point_t const& rhs) { + return lhs.value % rhs.value; + } + + constexpr friend fixed_point_t operator%(fixed_point_t const& lhs, int32_t const& rhs) { + return lhs.value % (static_cast<int64_t>(rhs) << PRECISION); + } + + constexpr friend fixed_point_t operator%(int32_t const& lhs, fixed_point_t const& rhs) { + return (static_cast<int64_t>(lhs) << PRECISION) % rhs.value; + } + + constexpr fixed_point_t operator%=(fixed_point_t const& obj) { + value %= obj.value; + return *this; + } + + constexpr fixed_point_t operator%=(int32_t const& obj) { + value %= (static_cast<int64_t>(obj) << PRECISION); + return *this; + } + + constexpr friend bool operator<(fixed_point_t const& lhs, fixed_point_t const& rhs) { + return lhs.value < rhs.value; + } + + constexpr friend bool operator<(fixed_point_t const& lhs, int32_t const& rhs) { + return lhs.value < static_cast<int64_t>(rhs) << PRECISION; + } + + constexpr friend bool operator<(int32_t const& lhs, fixed_point_t const& rhs) { + return static_cast<int64_t>(lhs) << PRECISION < rhs.value; + } + + constexpr friend bool operator<=(fixed_point_t const& lhs, fixed_point_t const& rhs) { + return lhs.value <= rhs.value; + } + + constexpr friend bool operator<=(fixed_point_t const& lhs, int32_t const& rhs) { + return lhs.value <= static_cast<int64_t>(rhs) << PRECISION; + } + + constexpr friend bool operator<=(int32_t const& lhs, fixed_point_t const& rhs) { + return static_cast<int64_t>(lhs) << PRECISION <= rhs.value; + } + + constexpr friend bool operator>(fixed_point_t const& lhs, fixed_point_t const& rhs) { + return lhs.value > rhs.value; + } + + constexpr friend bool operator>(fixed_point_t const& lhs, int32_t const& rhs) { + return lhs.value > static_cast<int64_t>(rhs) << PRECISION; + } + + constexpr friend bool operator>(int32_t const& lhs, fixed_point_t const& rhs) { + return static_cast<int64_t>(lhs) << PRECISION > rhs.value; + } + + constexpr friend bool operator>=(fixed_point_t const& lhs, fixed_point_t const& rhs) { + return lhs.value >= rhs.value; + } + + constexpr friend bool operator>=(fixed_point_t const& lhs, int32_t const& rhs) { + return lhs.value >= static_cast<int64_t>(rhs) << PRECISION; + } + + constexpr friend bool operator>=(int32_t const& lhs, fixed_point_t const& rhs) { + return static_cast<int64_t>(lhs) << PRECISION >= rhs.value; + } + + constexpr friend bool operator==(fixed_point_t const& lhs, fixed_point_t const& rhs) { + return lhs.value == rhs.value; + } + + constexpr friend bool operator==(fixed_point_t const& lhs, int32_t const& rhs) { + return lhs.value == static_cast<int64_t>(rhs) << PRECISION; + } + + constexpr friend bool operator==(int32_t const& lhs, fixed_point_t const& rhs) { + return static_cast<int64_t>(lhs) << PRECISION == rhs.value; + } + + constexpr friend bool operator!=(fixed_point_t const& lhs, fixed_point_t const& rhs) { + return lhs.value != rhs.value; + } + + constexpr friend bool operator!=(fixed_point_t const& lhs, int32_t const& rhs) { + return lhs.value != static_cast<int64_t>(rhs) << PRECISION; + } + + constexpr friend bool operator!=(int32_t const& lhs, fixed_point_t const& rhs) { + return static_cast<int64_t>(lhs) << PRECISION != rhs.value; + } + + private: + int64_t value; + + static constexpr fixed_point_t parse_integer(char const* str, char const* const end, bool* successful) { + int64_t parsed_value = StringUtils::string_to_int64(str, end, successful, 10); + return parse(parsed_value); + } + + static constexpr fixed_point_t parse_fraction(char const* str, char const* end, bool* successful) { + char const* const read_end = str + PRECISION; + if (read_end < end) end = read_end; + uint64_t parsed_value = StringUtils::string_to_uint64(str, end, successful, 10); + while (end++ < read_end) { + parsed_value *= 10; + } + uint64_t decimal = NumberUtils::pow(static_cast<uint64_t>(10), PRECISION); + int64_t ret = 0; + for (int i = PRECISION - 1; i >= 0; --i) { + decimal >>= 1; + if (parsed_value > decimal) { + parsed_value -= decimal; + ret |= 1 << i; + } + } + return ret; + } + }; + + static_assert(sizeof(fixed_point_t) == fixed_point_t::SIZE, "fixed_point_t is not 8 bytes"); +} diff --git a/src/openvic/types/fixed_point/FPLUT.hpp b/src/openvic-simulation/types/fixed_point/FixedPointLUT.hpp index f9b21ab..466517b 100644 --- a/src/openvic/types/fixed_point/FPLUT.hpp +++ b/src/openvic-simulation/types/fixed_point/FixedPointLUT.hpp @@ -7,14 +7,11 @@ #include <numbers> #include <utility> -#include "FPLUT_sin_512.hpp" - namespace OpenVic::FPLUT { - constexpr int32_t PRECISION = 16; - constexpr int64_t ONE = 1 << PRECISION; - // The LUT index is between 0 and 2^16, the index table goes until 512, if we shift by 7 our index will be between 0 and 511 - constexpr int32_t SHIFT = 16 - 9; + #include "FixedPointLUT_sin.hpp" + + constexpr int32_t SHIFT = SIN_LUT_PRECISION - SIN_LUT_COUNT_LOG2; constexpr int64_t sin(int64_t value) { int sign = 1; @@ -26,8 +23,8 @@ namespace OpenVic::FPLUT { int index = value >> SHIFT; int64_t a = SIN_LUT[index]; int64_t b = SIN_LUT[index + 1]; - int64_t fraction = (value - (index << SHIFT)) << 9; - int64_t result = a + (((b - a) * fraction) >> PRECISION); + int64_t fraction = (value - (index << SHIFT)) << SIN_LUT_COUNT_LOG2; + int64_t result = a + (((b - a) * fraction) >> SIN_LUT_PRECISION); return result * sign; } } diff --git a/src/openvic-simulation/types/fixed_point/FixedPointLUT_sin.hpp b/src/openvic-simulation/types/fixed_point/FixedPointLUT_sin.hpp new file mode 100644 index 0000000..0c75efe --- /dev/null +++ b/src/openvic-simulation/types/fixed_point/FixedPointLUT_sin.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include <cstdint> + +static constexpr int32_t SIN_LUT_PRECISION = 16; +static constexpr int32_t SIN_LUT_COUNT_LOG2 = 9; + +static constexpr int64_t SIN_LUT[(1 << SIN_LUT_COUNT_LOG2) + 1] = { + 0, 804, 1608, 2412, 3216, 4019, 4821, 5623, 6424, 7224, 8022, 8820, 9616, 10411, 11204, 11996, + 12785, 13573, 14359, 15143, 15924, 16703, 17479, 18253, 19024, 19792, 20557, 21320, 22078, 22834, 23586, 24335, + 25080, 25821, 26558, 27291, 28020, 28745, 29466, 30182, 30893, 31600, 32303, 33000, 33692, 34380, 35062, 35738, + 36410, 37076, 37736, 38391, 39040, 39683, 40320, 40951, 41576, 42194, 42806, 43412, 44011, 44604, 45190, 45769, + 46341, 46906, 47464, 48015, 48559, 49095, 49624, 50146, 50660, 51166, 51665, 52156, 52639, 53114, 53581, 54040, + 54491, 54934, 55368, 55794, 56212, 56621, 57022, 57414, 57798, 58172, 58538, 58896, 59244, 59583, 59914, 60235, + 60547, 60851, 61145, 61429, 61705, 61971, 62228, 62476, 62714, 62943, 63162, 63372, 63572, 63763, 63944, 64115, + 64277, 64429, 64571, 64704, 64827, 64940, 65043, 65137, 65220, 65294, 65358, 65413, 65457, 65492, 65516, 65531, + 65536, 65531, 65516, 65492, 65457, 65413, 65358, 65294, 65220, 65137, 65043, 64940, 64827, 64704, 64571, 64429, + 64277, 64115, 63944, 63763, 63572, 63372, 63162, 62943, 62714, 62476, 62228, 61971, 61705, 61429, 61145, 60851, + 60547, 60235, 59914, 59583, 59244, 58896, 58538, 58172, 57798, 57414, 57022, 56621, 56212, 55794, 55368, 54934, + 54491, 54040, 53581, 53114, 52639, 52156, 51665, 51166, 50660, 50146, 49624, 49095, 48559, 48015, 47464, 46906, + 46341, 45769, 45190, 44604, 44011, 43412, 42806, 42194, 41576, 40951, 40320, 39683, 39040, 38391, 37736, 37076, + 36410, 35738, 35062, 34380, 33692, 33000, 32303, 31600, 30893, 30182, 29466, 28745, 28020, 27291, 26558, 25821, + 25080, 24335, 23586, 22834, 22078, 21320, 20557, 19792, 19024, 18253, 17479, 16703, 15924, 15143, 14359, 13573, + 12785, 11996, 11204, 10411, 9616, 8820, 8022, 7224, 6424, 5623, 4821, 4019, 3216, 2412, 1608, 804, + 0, -804, -1608, -2412, -3216, -4019, -4821, -5623, -6424, -7224, -8022, -8820, -9616, -10411, -11204, -11996, + -12785, -13573, -14359, -15143, -15924, -16703, -17479, -18253, -19024, -19792, -20557, -21320, -22078, -22834, -23586, -24335, + -25080, -25821, -26558, -27291, -28020, -28745, -29466, -30182, -30893, -31600, -32303, -33000, -33692, -34380, -35062, -35738, + -36410, -37076, -37736, -38391, -39040, -39683, -40320, -40951, -41576, -42194, -42806, -43412, -44011, -44604, -45190, -45769, + -46341, -46906, -47464, -48015, -48559, -49095, -49624, -50146, -50660, -51166, -51665, -52156, -52639, -53114, -53581, -54040, + -54491, -54934, -55368, -55794, -56212, -56621, -57022, -57414, -57798, -58172, -58538, -58896, -59244, -59583, -59914, -60235, + -60547, -60851, -61145, -61429, -61705, -61971, -62228, -62476, -62714, -62943, -63162, -63372, -63572, -63763, -63944, -64115, + -64277, -64429, -64571, -64704, -64827, -64940, -65043, -65137, -65220, -65294, -65358, -65413, -65457, -65492, -65516, -65531, + -65536, -65531, -65516, -65492, -65457, -65413, -65358, -65294, -65220, -65137, -65043, -64940, -64827, -64704, -64571, -64429, + -64277, -64115, -63944, -63763, -63572, -63372, -63162, -62943, -62714, -62476, -62228, -61971, -61705, -61429, -61145, -60851, + -60547, -60235, -59914, -59583, -59244, -58896, -58538, -58172, -57798, -57414, -57022, -56621, -56212, -55794, -55368, -54934, + -54491, -54040, -53581, -53114, -52639, -52156, -51665, -51166, -50660, -50146, -49624, -49095, -48559, -48015, -47464, -46906, + -46341, -45769, -45190, -44604, -44011, -43412, -42806, -42194, -41576, -40951, -40320, -39683, -39040, -38391, -37736, -37076, + -36410, -35738, -35062, -34380, -33692, -33000, -32303, -31600, -30893, -30182, -29466, -28745, -28020, -27291, -26558, -25821, + -25080, -24335, -23586, -22834, -22078, -21320, -20557, -19792, -19024, -18253, -17479, -16703, -15924, -15143, -14359, -13573, + -12785, -11996, -11204, -10411, -9616, -8820, -8022, -7224, -6424, -5623, -4821, -4019, -3216, -2412, -1608, -804, + 0 +}; diff --git a/src/openvic-simulation/types/fixed_point/FixedPointMath.hpp b/src/openvic-simulation/types/fixed_point/FixedPointMath.hpp new file mode 100644 index 0000000..c0b5f42 --- /dev/null +++ b/src/openvic-simulation/types/fixed_point/FixedPointMath.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include "FixedPoint.hpp" + +namespace OpenVic::FPMath { + constexpr fixed_point_t sin(fixed_point_t number) { + number %= fixed_point_t::pi2(); + number *= fixed_point_t::one_div_pi2(); + return FPLUT::sin(number.get_raw_value()); + } +} diff --git a/src/openvic-simulation/types/fixed_point/lut_generator/lut_generator.py b/src/openvic-simulation/types/fixed_point/lut_generator/lut_generator.py new file mode 100644 index 0000000..8ae7a32 --- /dev/null +++ b/src/openvic-simulation/types/fixed_point/lut_generator/lut_generator.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python +from math import pi, sin +from argparse import ArgumentParser +from sys import exit + +def generate_sin_lut(precision : int, count_log2 : int): + one = 1 << precision + count = 1 << count_log2 + + SinLut = [] + + for i in range(count): + angle = 2 * pi * i / count + + sin_value = sin(angle) + moved_sin = sin_value * one + rounded_sin = int(moved_sin + 0.5) if moved_sin > 0 else int(moved_sin - 0.5) + SinLut.append(rounded_sin) + + SinLut.append(SinLut[0]) + + output = [ + "#pragma once", + "", + "#include <cstdint>", + "", + f"static constexpr int32_t SIN_LUT_PRECISION = {precision};", + f"static constexpr int32_t SIN_LUT_COUNT_LOG2 = {count_log2};", + "", + "static constexpr int64_t SIN_LUT[(1 << SIN_LUT_COUNT_LOG2) + 1] = {" + ] + + VALS_PER_LINE = 16 + + lines = [SinLut[i : i + VALS_PER_LINE] for i in range(0, len(SinLut), VALS_PER_LINE)] + + for line in lines: + output.append("\t" + ", ".join(str(value) for value in line) + ",") + + output[-1] = output[-1][:-1] # Remove the last comma + output += ["};", ""] + + cpp_code = "\n".join(output) + + with open(f"FixedPointLUT_sin.hpp", "w", newline="\n") as file: + file.write(cpp_code) + +PRECISION = 16 +COUNT = 9 + +if __name__ == "__main__": + + parser = ArgumentParser(prog="Fixed Point LUT Generator", description="Fixed-Point number Look-Up Table generator") + parser.add_argument("-p", "--precision", type=int, default=PRECISION, choices=range(1, 65), help="The number of bits after the point (fractional bits)") + parser.add_argument("-c", "--count", type=int, default=COUNT, choices=range(1, 65), help="The base 2 log of the number of values in the look-up table (must be <= precision)") + args = parser.parse_args() + + if args.precision < args.count: + print("ERROR: invalid count ", args.count, " - can't be greater than precision (", args.precision, ")") + exit(-1) + else: + generate_sin_lut(args.precision, args.count) + exit(0) diff --git a/src/openvic/utility/BMP.cpp b/src/openvic-simulation/utility/BMP.cpp index 0ea0f30..531870b 100644 --- a/src/openvic/utility/BMP.cpp +++ b/src/openvic-simulation/utility/BMP.cpp @@ -3,7 +3,7 @@ #include <cstring> #include <set> -#include "openvic/utility/Logger.hpp" +#include "openvic-simulation/utility/Logger.hpp" using namespace OpenVic; diff --git a/src/openvic/utility/BMP.hpp b/src/openvic-simulation/utility/BMP.hpp index 7ed36a5..f04b41a 100644 --- a/src/openvic/utility/BMP.hpp +++ b/src/openvic-simulation/utility/BMP.hpp @@ -3,7 +3,7 @@ #include <cstdio> #include <vector> -#include "openvic/types/Colour.hpp" +#include "openvic-simulation/types/Colour.hpp" namespace OpenVic { class BMP { diff --git a/src/openvic/utility/Logger.cpp b/src/openvic-simulation/utility/Logger.cpp index bf9a67c..bf9a67c 100644 --- a/src/openvic/utility/Logger.cpp +++ b/src/openvic-simulation/utility/Logger.cpp diff --git a/src/openvic/utility/Logger.hpp b/src/openvic-simulation/utility/Logger.hpp index 417aba7..417aba7 100644 --- a/src/openvic/utility/Logger.hpp +++ b/src/openvic-simulation/utility/Logger.hpp diff --git a/src/openvic/utility/NumberUtils.hpp b/src/openvic-simulation/utility/NumberUtils.hpp index 6211772..6211772 100644 --- a/src/openvic/utility/NumberUtils.hpp +++ b/src/openvic-simulation/utility/NumberUtils.hpp diff --git a/src/openvic/utility/StringUtils.hpp b/src/openvic-simulation/utility/StringUtils.hpp index 5d6001c..5d6001c 100644 --- a/src/openvic/utility/StringUtils.hpp +++ b/src/openvic-simulation/utility/StringUtils.hpp diff --git a/src/openvic/types/fixed_point/FP.hpp b/src/openvic/types/fixed_point/FP.hpp deleted file mode 100644 index 42ddf79..0000000 --- a/src/openvic/types/fixed_point/FP.hpp +++ /dev/null @@ -1,581 +0,0 @@ -#pragma once - -#include <cerrno> -#include <cmath> -#include <cstdint> -#include <cstdlib> -#include <limits> -#include <string_view> - -#include "openvic/utility/Logger.hpp" -#include "openvic/utility/NumberUtils.hpp" -#include "openvic/utility/StringUtils.hpp" - -#include "FPLUT.hpp" - -namespace OpenVic { - struct FP { - static constexpr size_t SIZE = 8; - - constexpr FP() : value { 0 } {} - constexpr FP(int64_t new_value) : value { new_value } {} - constexpr FP(int32_t new_value) : value { static_cast<int64_t>(new_value) << FPLUT::PRECISION } {} - - // Trivial destructor - ~FP() = default; - - static constexpr FP max() { - return std::numeric_limits<int64_t>::max(); - } - - static constexpr FP min() { - return std::numeric_limits<int64_t>::min(); - } - - static constexpr FP usable_max() { - return static_cast<int64_t>(2147483648LL); - } - - static constexpr FP usable_min() { - return -usable_max(); - } - - static constexpr FP _0() { - return 0; - } - - static constexpr FP _1() { - return 1; - } - - static constexpr FP _2() { - return 2; - } - - static constexpr FP _3() { - return 3; - } - - static constexpr FP _4() { - return 4; - } - - static constexpr FP _5() { - return 5; - } - - static constexpr FP _6() { - return 6; - } - - static constexpr FP _7() { - return 7; - } - - static constexpr FP _8() { - return 8; - } - - static constexpr FP _9() { - return 9; - } - - static constexpr FP _10() { - return 10; - } - - static constexpr FP _50() { - return 50; - } - - static constexpr FP _100() { - return 100; - } - - static constexpr FP _200() { - return 200; - } - - static constexpr FP _0_01() { - return _1() / _100(); - } - - static constexpr FP _0_02() { - return _0_01() * 2; - } - - static constexpr FP _0_03() { - return _0_01() * 3; - } - - static constexpr FP _0_04() { - return _0_01() * 4; - } - - static constexpr FP _0_05() { - return _0_01() * 5; - } - - static constexpr FP _0_10() { - return _1() / 10; - } - - static constexpr FP _0_20() { - return _0_10() * 2; - } - - static constexpr FP _0_25() { - return _1() / 4; - } - - static constexpr FP _0_33() { - return _1() / 3; - } - - static constexpr FP _0_50() { - return _1() / 2; - } - - static constexpr FP _0_75() { - return _1() - _0_25(); - } - - static constexpr FP _0_95() { - return _1() - _0_05(); - } - - static constexpr FP _0_99() { - return _1() - _0_01(); - } - - static constexpr FP _1_01() { - return _1() + _0_01(); - } - - static constexpr FP _1_10() { - return _1() + _0_10(); - } - - static constexpr FP _1_50() { - return _1() + _0_50(); - } - - static constexpr FP minus_one() { - return -1; - } - - static constexpr FP pi() { - return static_cast<int64_t>(205887LL); - } - - static constexpr FP pi2() { - return pi() * 2; - } - - static constexpr FP pi_quarter() { - return pi() / 4; - } - - static constexpr FP pi_half() { - return pi() / 2; - } - - static constexpr FP one_div_pi2() { - return 1 / pi2(); - } - - static constexpr FP deg2rad() { - return static_cast<int64_t>(1143LL); - } - - static constexpr FP rad2deg() { - return static_cast<int64_t>(3754936LL); - } - - static constexpr FP e() { - return static_cast<int64_t>(178145LL); - } - - constexpr bool is_negative() const { - return value < 0; - } - - constexpr FP abs() const { - return !is_negative() ? value : -value; - } - - // Doesn't account for sign, so -n.abc -> 1 - 0.abc - constexpr FP get_frac() const { - return value & (FPLUT::ONE - 1); - } - - constexpr int64_t to_int64_t() const { - return value >> FPLUT::PRECISION; - } - - constexpr void set_raw_value(int64_t value) { - this->value = value; - } - - constexpr int64_t get_raw_value() const { - return value; - } - - constexpr int32_t to_int32_t() const { - return static_cast<int32_t>(to_int64_t()); - } - - constexpr float to_float() const { - return value / static_cast<float>(FPLUT::ONE); - } - - constexpr float to_float_rounded() const { - return static_cast<float>(NumberUtils::round_to_int64((value / static_cast<float>(FPLUT::ONE)) * 100000.0f)) / 100000.0f; - } - - constexpr double to_double() const { - return value / static_cast<double>(FPLUT::ONE); - } - - constexpr float to_double_rounded() const { - return NumberUtils::round_to_int64((value / static_cast<double>(FPLUT::ONE)) * 100000.0) / 100000.0; - } - - std::string to_string() const { - FP val = abs(); - std::string str = std::to_string(val.to_int64_t()) + "."; - if (is_negative()) str = "-" + str; - val = val.get_frac(); - do { - val *= 10; - str.push_back('0' + static_cast<char>(val.to_int64_t())); - val = val.get_frac(); - } while (val > 0); - return str; - } - - // Deterministic - static constexpr FP parse_raw(int64_t value) { - return value; - } - - // Deterministic - static constexpr FP parse(int64_t value) { - return value << FPLUT::PRECISION; - } - - // Deterministic - static constexpr FP parse(char const* str, char const* const end, bool* successful = nullptr) { - if (successful != nullptr) *successful = false; - - if (str == nullptr || str >= end) { - return _0(); - } - - bool negative = false; - - if (*str == '-') { - negative = true; - ++str; - if (str == end) return _0(); - } - - char const* dot_pointer = str; - while (*dot_pointer != '.' && ++dot_pointer != end); - - if (dot_pointer == str && dot_pointer + 1 == end) { - // Invalid: ".", "+." or "-." - return _0(); - } - - FP result = _0(); - if (successful != nullptr) *successful = true; - - if (dot_pointer != str) { - // Non-empty integer part - bool int_successful = false; - result += parse_integer(str, dot_pointer, &int_successful); - if (!int_successful && successful != nullptr) *successful = false; - } - - if (dot_pointer + 1 < end) { - // Non-empty fractional part - bool frac_successful = false; - result += parse_fraction(dot_pointer + 1, end, &frac_successful); - if (!frac_successful && successful != nullptr) *successful = false; - } - - return negative ? -result : result; - } - - static constexpr FP parse(char const* str, size_t length, bool* successful = nullptr) { - return parse(str, str + length, successful); - } - - static FP parse(const std::string_view str, bool* successful = nullptr) { - return parse(str.data(), str.length(), successful); - } - - // Not Deterministic - static constexpr FP parse_unsafe(float value) { - return static_cast<int64_t>(value * FPLUT::ONE + 0.5f * (value < 0 ? -1 : 1)); - } - - // Not Deterministic - static FP parse_unsafe(char const* value) { - char* endpointer; - double double_value = std::strtod(value, &endpointer); - - if (*endpointer != '\0') { - Logger::error("Unsafe fixed point parse failed to parse the end of a string: \"", endpointer, "\""); - } - - int64_t integer_value = static_cast<long>(double_value * FPLUT::ONE + 0.5 * (double_value < 0 ? -1 : 1)); - - return integer_value; - } - - constexpr operator int32_t() const { - return to_int32_t(); - } - - constexpr operator int64_t() const { - return to_int64_t(); - } - - constexpr operator float() const { - return to_float(); - } - - constexpr operator double() const { - return to_double(); - } - - operator std::string() const { - return to_string(); - } - - friend std::ostream& operator<<(std::ostream& stream, FP const& obj) { - return stream << obj.to_string(); - } - - constexpr friend FP operator-(FP const& obj) { - return -obj.value; - } - - constexpr friend FP operator+(FP const& obj) { - return +obj.value; - } - - constexpr friend FP operator+(FP const& lhs, FP const& rhs) { - return lhs.value + rhs.value; - } - - constexpr friend FP operator+(FP const& lhs, int32_t const& rhs) { - return lhs.value + (static_cast<int64_t>(rhs) << FPLUT::PRECISION); - } - - constexpr friend FP operator+(int32_t const& lhs, FP const& rhs) { - return (static_cast<int64_t>(lhs) << FPLUT::PRECISION) + rhs.value; - } - - constexpr FP operator+=(FP const& obj) { - value += obj.value; - return *this; - } - - constexpr FP operator+=(int32_t const& obj) { - value += (static_cast<int64_t>(obj) << FPLUT::PRECISION); - return *this; - } - - constexpr friend FP operator-(FP const& lhs, FP const& rhs) { - return lhs.value - rhs.value; - } - - constexpr friend FP operator-(FP const& lhs, int32_t const& rhs) { - return lhs.value - (static_cast<int64_t>(rhs) << FPLUT::PRECISION); - } - - constexpr friend FP operator-(int32_t const& lhs, FP const& rhs) { - return (static_cast<int64_t>(lhs) << FPLUT::PRECISION) - rhs.value; - } - - constexpr FP operator-=(FP const& obj) { - value -= obj.value; - return *this; - } - - constexpr FP operator-=(int32_t const& obj) { - value -= (static_cast<int64_t>(obj) << FPLUT::PRECISION); - return *this; - } - - constexpr friend FP operator*(FP const& lhs, FP const& rhs) { - return lhs.value * rhs.value >> FPLUT::PRECISION; - } - - constexpr friend FP operator*(FP const& lhs, int32_t const& rhs) { - return lhs.value * rhs; - } - - constexpr friend FP operator*(int32_t const& lhs, FP const& rhs) { - return lhs * rhs.value; - } - - constexpr FP operator*=(FP const& obj) { - value *= obj.value >> FPLUT::PRECISION; - return *this; - } - - constexpr FP operator*=(int32_t const& obj) { - value *= obj; - return *this; - } - - constexpr friend FP operator/(FP const& lhs, FP const& rhs) { - return (lhs.value << FPLUT::PRECISION) / rhs.value; - } - - constexpr friend FP operator/(FP const& lhs, int32_t const& rhs) { - return lhs.value / rhs; - } - - constexpr friend FP operator/(int32_t const& lhs, FP const& rhs) { - return (static_cast<int64_t>(lhs) << (2 * FPLUT::PRECISION)) / rhs.value; - } - - constexpr FP operator/=(FP const& obj) { - value = (value << FPLUT::PRECISION) / obj.value; - return *this; - } - - constexpr FP operator/=(int32_t const& obj) { - value /= obj; - return *this; - } - - constexpr friend FP operator%(FP const& lhs, FP const& rhs) { - return lhs.value % rhs.value; - } - - constexpr friend FP operator%(FP const& lhs, int32_t const& rhs) { - return lhs.value % (static_cast<int64_t>(rhs) << FPLUT::PRECISION); - } - - constexpr friend FP operator%(int32_t const& lhs, FP const& rhs) { - return (static_cast<int64_t>(lhs) << FPLUT::PRECISION) % rhs.value; - } - - constexpr FP operator%=(FP const& obj) { - value %= obj.value; - return *this; - } - - constexpr FP operator%=(int32_t const& obj) { - value %= (static_cast<int64_t>(obj) << FPLUT::PRECISION); - return *this; - } - - constexpr friend bool operator<(FP const& lhs, FP const& rhs) { - return lhs.value < rhs.value; - } - - constexpr friend bool operator<(FP const& lhs, int32_t const& rhs) { - return lhs.value < static_cast<int64_t>(rhs) << FPLUT::PRECISION; - } - - constexpr friend bool operator<(int32_t const& lhs, FP const& rhs) { - return static_cast<int64_t>(lhs) << FPLUT::PRECISION < rhs.value; - } - - constexpr friend bool operator<=(FP const& lhs, FP const& rhs) { - return lhs.value <= rhs.value; - } - - constexpr friend bool operator<=(FP const& lhs, int32_t const& rhs) { - return lhs.value <= static_cast<int64_t>(rhs) << FPLUT::PRECISION; - } - - constexpr friend bool operator<=(int32_t const& lhs, FP const& rhs) { - return static_cast<int64_t>(lhs) << FPLUT::PRECISION <= rhs.value; - } - - constexpr friend bool operator>(FP const& lhs, FP const& rhs) { - return lhs.value > rhs.value; - } - - constexpr friend bool operator>(FP const& lhs, int32_t const& rhs) { - return lhs.value > static_cast<int64_t>(rhs) << FPLUT::PRECISION; - } - - constexpr friend bool operator>(int32_t const& lhs, FP const& rhs) { - return static_cast<int64_t>(lhs) << FPLUT::PRECISION > rhs.value; - } - - constexpr friend bool operator>=(FP const& lhs, FP const& rhs) { - return lhs.value >= rhs.value; - } - - constexpr friend bool operator>=(FP const& lhs, int32_t const& rhs) { - return lhs.value >= static_cast<int64_t>(rhs) << FPLUT::PRECISION; - } - - constexpr friend bool operator>=(int32_t const& lhs, FP const& rhs) { - return static_cast<int64_t>(lhs) << FPLUT::PRECISION >= rhs.value; - } - - constexpr friend bool operator==(FP const& lhs, FP const& rhs) { - return lhs.value == rhs.value; - } - - constexpr friend bool operator==(FP const& lhs, int32_t const& rhs) { - return lhs.value == static_cast<int64_t>(rhs) << FPLUT::PRECISION; - } - - constexpr friend bool operator==(int32_t const& lhs, FP const& rhs) { - return static_cast<int64_t>(lhs) << FPLUT::PRECISION == rhs.value; - } - - constexpr friend bool operator!=(FP const& lhs, FP const& rhs) { - return lhs.value != rhs.value; - } - - constexpr friend bool operator!=(FP const& lhs, int32_t const& rhs) { - return lhs.value != static_cast<int64_t>(rhs) << FPLUT::PRECISION; - } - - constexpr friend bool operator!=(int32_t const& lhs, FP const& rhs) { - return static_cast<int64_t>(lhs) << FPLUT::PRECISION != rhs.value; - } - - private: - int64_t value; - - static constexpr FP parse_integer(char const* str, char const* const end, bool* successful) { - int64_t parsed_value = StringUtils::string_to_int64(str, end, successful, 10); - return parse(parsed_value); - } - - static constexpr FP parse_fraction(char const* str, char const* end, bool* successful) { - char const* const read_end = str + FPLUT::PRECISION; - if (read_end < end) end = read_end; - uint64_t parsed_value = StringUtils::string_to_uint64(str, end, successful, 10); - while (end++ < read_end) { - parsed_value *= 10; - } - uint64_t decimal = NumberUtils::pow(static_cast<uint64_t>(10), FPLUT::PRECISION); - int64_t ret = 0; - for (int i = FPLUT::PRECISION - 1; i >= 0; --i) { - decimal >>= 1; - if (parsed_value > decimal) { - parsed_value -= decimal; - ret |= 1 << i; - } - } - return ret; - } - }; - - static_assert(sizeof(FP) == FP::SIZE, "FP is not 8 bytes"); -} diff --git a/src/openvic/types/fixed_point/FPLUT_sin_512.hpp b/src/openvic/types/fixed_point/FPLUT_sin_512.hpp deleted file mode 100644 index 1af0380..0000000 --- a/src/openvic/types/fixed_point/FPLUT_sin_512.hpp +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once - -#include <cstdint> - -static constexpr int32_t SIN_LUT[] = { - 0, 804, 1608, 2412, 3216, 4019, 4821, 5623, 6424, 7224, - 8022, 8820, 9616, 10411, 11204, 11996, 12785, 13573, 14359, 15143, - 15924, 16703, 17479, 18253, 19024, 19792, 20557, 21320, 22078, 22834, - 23586, 24335, 25080, 25821, 26558, 27291, 28020, 28745, 29466, 30182, - 30893, 31600, 32303, 33000, 33692, 34380, 35062, 35738, 36410, 37076, - 37736, 38391, 39040, 39683, 40320, 40951, 41576, 42194, 42806, 43412, - 44011, 44604, 45190, 45769, 46341, 46906, 47464, 48015, 48559, 49095, - 49624, 50146, 50660, 51166, 51665, 52156, 52639, 53114, 53581, 54040, - 54491, 54934, 55368, 55794, 56212, 56621, 57022, 57414, 57798, 58172, - 58538, 58896, 59244, 59583, 59914, 60235, 60547, 60851, 61145, 61429, - 61705, 61971, 62228, 62476, 62714, 62943, 63162, 63372, 63572, 63763, - 63944, 64115, 64277, 64429, 64571, 64704, 64827, 64940, 65043, 65137, - 65220, 65294, 65358, 65413, 65457, 65492, 65516, 65531, 65536, 65531, - 65516, 65492, 65457, 65413, 65358, 65294, 65220, 65137, 65043, 64940, - 64827, 64704, 64571, 64429, 64277, 64115, 63944, 63763, 63572, 63372, - 63162, 62943, 62714, 62476, 62228, 61971, 61705, 61429, 61145, 60851, - 60547, 60235, 59914, 59583, 59244, 58896, 58538, 58172, 57798, 57414, - 57022, 56621, 56212, 55794, 55368, 54934, 54491, 54040, 53581, 53114, - 52639, 52156, 51665, 51166, 50660, 50146, 49624, 49095, 48559, 48015, - 47464, 46906, 46341, 45769, 45190, 44604, 44011, 43412, 42806, 42194, - 41576, 40951, 40320, 39683, 39040, 38391, 37736, 37076, 36410, 35738, - 35062, 34380, 33692, 33000, 32303, 31600, 30893, 30182, 29466, 28745, - 28020, 27291, 26558, 25821, 25080, 24335, 23586, 22834, 22078, 21320, - 20557, 19792, 19024, 18253, 17479, 16703, 15924, 15143, 14359, 13573, - 12785, 11996, 11204, 10411, 9616, 8820, 8022, 7224, 6424, 5623, - 4821, 4019, 3216, 2412, 1608, 804, 0, -804, -1608, -2412, - -3216, -4019, -4821, -5623, -6424, -7224, -8022, -8820, -9616, -10411, - -11204, -11996, -12785, -13573, -14359, -15143, -15924, -16703, -17479, -18253, - -19024, -19792, -20557, -21320, -22078, -22834, -23586, -24335, -25080, -25821, - -26558, -27291, -28020, -28745, -29466, -30182, -30893, -31600, -32303, -33000, - -33692, -34380, -35062, -35738, -36410, -37076, -37736, -38391, -39040, -39683, - -40320, -40951, -41576, -42194, -42806, -43412, -44011, -44604, -45190, -45769, - -46341, -46906, -47464, -48015, -48559, -49095, -49624, -50146, -50660, -51166, - -51665, -52156, -52639, -53114, -53581, -54040, -54491, -54934, -55368, -55794, - -56212, -56621, -57022, -57414, -57798, -58172, -58538, -58896, -59244, -59583, - -59914, -60235, -60547, -60851, -61145, -61429, -61705, -61971, -62228, -62476, - -62714, -62943, -63162, -63372, -63572, -63763, -63944, -64115, -64277, -64429, - -64571, -64704, -64827, -64940, -65043, -65137, -65220, -65294, -65358, -65413, - -65457, -65492, -65516, -65531, -65536, -65531, -65516, -65492, -65457, -65413, - -65358, -65294, -65220, -65137, -65043, -64940, -64827, -64704, -64571, -64429, - -64277, -64115, -63944, -63763, -63572, -63372, -63162, -62943, -62714, -62476, - -62228, -61971, -61705, -61429, -61145, -60851, -60547, -60235, -59914, -59583, - -59244, -58896, -58538, -58172, -57798, -57414, -57022, -56621, -56212, -55794, - -55368, -54934, -54491, -54040, -53581, -53114, -52639, -52156, -51665, -51166, - -50660, -50146, -49624, -49095, -48559, -48015, -47464, -46906, -46341, -45769, - -45190, -44604, -44011, -43412, -42806, -42194, -41576, -40951, -40320, -39683, - -39040, -38391, -37736, -37076, -36410, -35738, -35062, -34380, -33692, -33000, - -32303, -31600, -30893, -30182, -29466, -28745, -28020, -27291, -26558, -25821, - -25080, -24335, -23586, -22834, -22078, -21320, -20557, -19792, -19024, -18253, - -17479, -16703, -15924, -15143, -14359, -13573, -12785, -11996, -11204, -10411, - -9616, -8820, -8022, -7224, -6424, -5623, -4821, -4019, -3216, -2412, - -1608, -804, 0 -}; diff --git a/src/openvic/types/fixed_point/FPMath.hpp b/src/openvic/types/fixed_point/FPMath.hpp deleted file mode 100644 index 965d63c..0000000 --- a/src/openvic/types/fixed_point/FPMath.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#include "FP.hpp" - -namespace OpenVic::FPMath { - constexpr FP sin(FP number) { - number %= FP::pi2(); - number *= FP::one_div_pi2(); - return FPLUT::sin(number.get_raw_value()); - } -} diff --git a/src/openvic/types/fixed_point/lut_generator/lut_generator.py b/src/openvic/types/fixed_point/lut_generator/lut_generator.py deleted file mode 100644 index 5c5ba8b..0000000 --- a/src/openvic/types/fixed_point/lut_generator/lut_generator.py +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env python -import math - -PRECISION = 16 -ONE = 1 << PRECISION -SIN_VALUE_COUNT = 512 - -SinLut = [] - -for i in range(SIN_VALUE_COUNT): - angle = 2 * math.pi * i / SIN_VALUE_COUNT - - sin_value = math.sin(angle) - moved_sin = sin_value * ONE - rounded_sin = int(moved_sin + 0.5) if moved_sin > 0 else int(moved_sin - 0.5) - SinLut.append(rounded_sin) - -SinLut.append(SinLut[0]) - -output = [ -"""#pragma once - -#include <cstdint> - -static constexpr int32_t SIN_LUT[] = {""" -] - -lines = [SinLut[i:i+10] for i in range(0, len(SinLut), 10)] - -for line in lines: - output.append("\t" + ", ".join(str(value) for value in line) + ",") - -output[-1] = output[-1][:-1] # Remove the last comma -output.append("};\n") - -cpp_code = "\n".join(output) - -with open(f"FPLUT_sin_{SIN_VALUE_COUNT}.hpp", "w", newline="\n") as file: - file.write(cpp_code) |