From 8add7c666455a8a4edd9e02ca5a33f78a25e8f2c Mon Sep 17 00:00:00 2001 From: Hop311 Date: Fri, 15 Sep 2023 21:55:00 +0100 Subject: First go at Modifiers --- src/openvic-simulation/Modifier.cpp | 115 ++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/openvic-simulation/Modifier.cpp (limited to 'src/openvic-simulation/Modifier.cpp') diff --git a/src/openvic-simulation/Modifier.cpp b/src/openvic-simulation/Modifier.cpp new file mode 100644 index 0000000..ed32ee2 --- /dev/null +++ b/src/openvic-simulation/Modifier.cpp @@ -0,0 +1,115 @@ +#include "Modifier.hpp" + +using namespace OpenVic; + +ModifierEffect::ModifierEffect(const std::string_view new_identifier, bool new_positive_good) + : HasIdentifier { new_identifier }, positive_good { new_positive_good } {} + +bool ModifierEffect::get_positive_good() const { + return positive_good; +} + +ModifierValue::ModifierValue() = default; +ModifierValue::ModifierValue(ModifierValue const&) = default; +ModifierValue::ModifierValue(ModifierValue&&) = default; + +void ModifierValue::trim() { + std::erase_if(values, [](effect_map_t::value_type const& value) -> bool { + return value.second == fixed_point_t::_0(); + }); +} + +size_t ModifierValue::get_effect_count() const { + return values.size(); +} + +fixed_point_t ModifierValue::get_effect(ModifierEffect const* effect, bool* successful) { + const effect_map_t::const_iterator it = values.find(effect); + if (it != values.end()) { + if (successful != nullptr) *successful = true; + return it->second; + } + if (successful != nullptr) *successful = false; + return fixed_point_t::_0(); +} + +bool ModifierValue::has_effect(ModifierEffect const* effect) const { + return values.find(effect) != values.end(); +} + +ModifierValue& ModifierValue::operator+=(ModifierValue const& right) { + for (effect_map_t::value_type const& value : right.values) { + values[value.first] += value.second; + } + return *this; +} + +ModifierValue ModifierValue::operator+(ModifierValue const& right) const { + ModifierValue ret = *this; + return ret += right; +} + +ModifierValue ModifierValue::operator-() const { + ModifierValue ret = *this; + for (effect_map_t::value_type& value : ret.values) { + value.second = -value.second; + } + return ret; +} + +ModifierValue& ModifierValue::operator-=(ModifierValue const& right) { + for (effect_map_t::value_type const& value : right.values) { + values[value.first] -= value.second; + } + return *this; +} + +ModifierValue ModifierValue::operator-(ModifierValue const& right) const { + ModifierValue ret = *this; + return ret -= right; +} + +std::ostream& OpenVic::operator<<(std::ostream& stream, ModifierValue const& value) { + for (ModifierValue::effect_map_t::value_type const& effect : value.values) { + stream << effect.first << ": " << effect.second << "\n"; + } + return stream; +} + +Modifier::Modifier(const std::string_view new_identifier, ModifierValue&& new_values, icon_t new_icon) + : HasIdentifier { new_identifier }, ModifierValue { std::move(new_values) }, icon { new_icon } {} + +Modifier::icon_t Modifier::get_icon() const { + return icon; +} + +Modifier const& ModifierInstance::get_modifier() const { + return modifier; +} + +Date const& ModifierInstance::get_expiry_date() const { + return expiry_date; +} + +ModifierManager::ModifierManager() + : modifier_effects { "modifier effects"}, modifiers { "modifiers" } {} + +bool ModifierManager::add_modifier_effect(const std::string_view identifier, bool positive_good) { + if (identifier.empty()) { + Logger::error("Invalid modifier effect identifier - empty!"); + return false; + } + return modifier_effects.add_item({ identifier, positive_good }); +} + +bool ModifierManager::add_modifier(const std::string_view identifier, ModifierValue&& values, Modifier::icon_t icon) { + if (identifier.empty()) { + Logger::error("Invalid modifier effect identifier - empty!"); + return false; + } + if (icon <= 0) { + Logger::error("Invalid modifier icon for ", identifier, ": ", icon); + return false; + } + return modifiers.add_item({ identifier, std::move(values), icon }); +} -- cgit v1.2.3-56-ga3b1 From 8cf93b6523dc380ef0c7215e15e327a78bf4578e Mon Sep 17 00:00:00 2001 From: Hop311 Date: Sat, 16 Sep 2023 00:08:05 +0100 Subject: 2D-vectors (positions.txt needs fixed point vecs) --- src/openvic-simulation/Modifier.cpp | 1 + src/openvic-simulation/Modifier.hpp | 1 + src/openvic-simulation/types/Vector.cpp | 86 +++++++++++++++++++++++++++++++ src/openvic-simulation/types/Vector.hpp | 35 +++++++++++++ src/openvic-simulation/utility/Logger.hpp | 4 +- 5 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 src/openvic-simulation/types/Vector.cpp create mode 100644 src/openvic-simulation/types/Vector.hpp (limited to 'src/openvic-simulation/Modifier.cpp') diff --git a/src/openvic-simulation/Modifier.cpp b/src/openvic-simulation/Modifier.cpp index ed32ee2..d2e21c6 100644 --- a/src/openvic-simulation/Modifier.cpp +++ b/src/openvic-simulation/Modifier.cpp @@ -10,6 +10,7 @@ bool ModifierEffect::get_positive_good() const { } ModifierValue::ModifierValue() = default; +ModifierValue::ModifierValue(effect_map_t&& new_values) : values { std::move(new_values) } {} ModifierValue::ModifierValue(ModifierValue const&) = default; ModifierValue::ModifierValue(ModifierValue&&) = default; diff --git a/src/openvic-simulation/Modifier.hpp b/src/openvic-simulation/Modifier.hpp index e8c4c8b..eb63788 100644 --- a/src/openvic-simulation/Modifier.hpp +++ b/src/openvic-simulation/Modifier.hpp @@ -31,6 +31,7 @@ namespace OpenVic { public: ModifierValue(); + ModifierValue(effect_map_t&& new_values); ModifierValue(ModifierValue const&); ModifierValue(ModifierValue&&); diff --git a/src/openvic-simulation/types/Vector.cpp b/src/openvic-simulation/types/Vector.cpp new file mode 100644 index 0000000..a5b8566 --- /dev/null +++ b/src/openvic-simulation/types/Vector.cpp @@ -0,0 +1,86 @@ +#include "Vector.hpp" + +#include +#include + +using namespace OpenVic; + +template +constexpr vec2_t::vec2_t() = default; + +template +constexpr vec2_t::vec2_t(T new_val) : x { new_val }, y { new_val } {} + +template +constexpr vec2_t::vec2_t(T new_x, T new_y) : x { new_x }, y { new_y } {} + + +template +constexpr vec2_t vec2_t::abs() const { + return { }; +} + +template +constexpr T vec2_t::length_squared() const { + return x * x + y * y; +} + +template +constexpr T* vec2_t::data() { + return reinterpret_cast(this); +} + +template +constexpr T const* vec2_t::data() const { + return reinterpret_cast(this); +} + +template +constexpr T& vec2_t::operator[](size_t index) { + return data()[index & 1]; +} + +template +constexpr T const& vec2_t::operator[](size_t index) const { + return data()[index & 1]; +} + +template +constexpr vec2_t operator+(vec2_t const& left, vec2_t const& right) { + return { left.x + right.x, left.y + right.y }; +} + +template +constexpr vec2_t& vec2_t::operator+=(vec2_t const& right) { + x += right.x; + y += right.y; + return *this; +} + +template +constexpr vec2_t operator-(vec2_t const& arg) { + return { -arg.x, -arg.y }; +} + +template +constexpr vec2_t operator-(vec2_t const& left, vec2_t const& right) { + return { left.x - right.x, left.y - right.y }; +} + +template +constexpr vec2_t& vec2_t::operator-=(vec2_t const& right) { + x -= right.x; + y -= right.y; + return *this; +} + +template +constexpr std::ostream& operator<<(std::ostream& stream, vec2_t const& value) { + return stream << "(" << value.x << ", " << value.y << ")"; +} + +template struct vec2_t; +template struct vec2_t; + +static_assert(sizeof(ivec2_t) == 2 * sizeof(int64_t), "ivec2_t size does not equal the sum of its parts' sizes"); +static_assert(sizeof(fvec2_t) == 2 * sizeof(fixed_point_t), "fvec2_t size does not equal the sum of its parts' sizes"); diff --git a/src/openvic-simulation/types/Vector.hpp b/src/openvic-simulation/types/Vector.hpp new file mode 100644 index 0000000..fdf7d70 --- /dev/null +++ b/src/openvic-simulation/types/Vector.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include "openvic-simulation/types/fixed_point/FixedPoint.hpp" + +namespace OpenVic { + + template + struct vec2_t { + T x, y; + + constexpr vec2_t(); + constexpr vec2_t(T new_val); + constexpr vec2_t(T new_x, T new_y); + + constexpr vec2_t abs() const; + constexpr T length_squared() const; + + constexpr T* data(); + constexpr T const* data() const; + + constexpr T& operator[](size_t index); + constexpr T const& operator[](size_t index) const; + + constexpr friend vec2_t operator+(vec2_t const& left, vec2_t const& right); + constexpr vec2_t& operator+=(vec2_t const& right); + + constexpr friend vec2_t operator-(vec2_t const& arg); + constexpr friend vec2_t operator-(vec2_t const& left, vec2_t const& right); + constexpr vec2_t& operator-=(vec2_t const& right); + constexpr friend std::ostream& operator<<(std::ostream& stream, vec2_t const& value); + }; + + using ivec2_t = vec2_t; + using fvec2_t = vec2_t; +} diff --git a/src/openvic-simulation/utility/Logger.hpp b/src/openvic-simulation/utility/Logger.hpp index f9ebd5d..c7b8c51 100644 --- a/src/openvic-simulation/utility/Logger.hpp +++ b/src/openvic-simulation/utility/Logger.hpp @@ -70,13 +70,13 @@ namespace OpenVic { static void set_##name##_func(log_func_t log_func) { \ name##_func = log_func; \ } \ - template \ + template \ struct name { \ name(Ts&&... ts, source_location const& location = source_location::current()) { \ log{ name##_func, name##_queue, std::forward(ts)..., location }; \ } \ }; \ - template \ + template \ name(Ts&&...) -> name; LOG_FUNC(info) -- cgit v1.2.3-56-ga3b1