aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/diplomacy/DiplomaticAction.hpp
diff options
context:
space:
mode:
author George L. Albany <Megacake1234@gmail.com>2024-01-03 02:05:10 +0100
committer GitHub <noreply@github.com>2024-01-03 02:05:10 +0100
commit143a422bc1c27e6d1ebf7e3ead32fff32877797e (patch)
treea5af99c508adf4207ffe2386d5fa73b83b01ba00 /src/openvic-simulation/diplomacy/DiplomaticAction.hpp
parentd893c0ad8c6a0c347dcec72762be49f20886a90a (diff)
parent5fa456c21ad950a6f269b83f5e88ce8c90ae9a14 (diff)
Merge pull request #101 from OpenVicProject/spooky-scary-skeletons-send-diplo-down-your-spine
Diffstat (limited to 'src/openvic-simulation/diplomacy/DiplomaticAction.hpp')
-rw-r--r--src/openvic-simulation/diplomacy/DiplomaticAction.hpp134
1 files changed, 134 insertions, 0 deletions
diff --git a/src/openvic-simulation/diplomacy/DiplomaticAction.hpp b/src/openvic-simulation/diplomacy/DiplomaticAction.hpp
new file mode 100644
index 0000000..7254510
--- /dev/null
+++ b/src/openvic-simulation/diplomacy/DiplomaticAction.hpp
@@ -0,0 +1,134 @@
+#pragma once
+
+#include <any>
+#include <concepts>
+#include <cstdint>
+#include <string_view>
+#include <type_traits>
+#include <variant>
+
+#include "openvic-simulation/country/Country.hpp"
+#include "openvic-simulation/types/FunctionRef.hpp"
+#include "openvic-simulation/types/IdentifierRegistry.hpp"
+
+namespace OpenVic {
+ struct DiplomaticActionType {
+ friend struct DiplomaticActionManager;
+ friend struct CancelableDiplomaticActionType;
+
+ struct Argument {
+ Country& sender;
+ Country& reciever;
+ std::any context_data;
+ };
+
+ using allowed_to_commit_func = FunctionRef<bool(const Argument&)>;
+ using get_acceptance_func = FunctionRef<std::int32_t(const Argument&)>;
+ using commit_action_func = FunctionRef<void(Argument&)>;
+
+ static bool allowed_to_commit_default(const Argument& argument) {
+ return true;
+ }
+
+ static std::int32_t get_acceptance_default(const Argument& argument) {
+ return 1;
+ }
+
+ struct Initializer {
+ commit_action_func commit;
+ allowed_to_commit_func allowed = allowed_to_commit_default;
+ get_acceptance_func get_acceptance = get_acceptance_default;
+ };
+
+ const commit_action_func commit_action_caller;
+ const allowed_to_commit_func allowed_to_commit = allowed_to_commit_default;
+ const get_acceptance_func get_acceptance = get_acceptance_default;
+
+ void commit_action(Argument& arg) const {
+ commit_action_caller(arg);
+ }
+
+ private:
+ DiplomaticActionType(Initializer&& initializer);
+ };
+
+ struct CancelableDiplomaticActionType : DiplomaticActionType {
+ friend struct DiplomaticActionManager;
+
+ using allowed_to_cancel_func = FunctionRef<bool(const Argument&)>;
+
+
+ static bool allowed_to_cancel_default(const Argument& argument) {
+ return true;
+ }
+
+ struct Initializer {
+ commit_action_func commit;
+ allowed_to_commit_func allowed = allowed_to_commit_default;
+ get_acceptance_func get_acceptance = get_acceptance_default;
+ allowed_to_cancel_func allowed_cancel = allowed_to_cancel_default;
+
+ operator DiplomaticActionType::Initializer() {
+ return {commit, allowed, get_acceptance};
+ }
+ };
+
+ const allowed_to_cancel_func allowed_to_cancel = allowed_to_cancel_default;
+
+ private:
+ CancelableDiplomaticActionType(Initializer&& initializer);
+ };
+
+ struct DiplomaticActionTickCache;
+
+ struct DiplomaticActionTypeStorage : std::variant<DiplomaticActionType, CancelableDiplomaticActionType>, HasIdentifier {
+ using base_type = std::variant<DiplomaticActionType, CancelableDiplomaticActionType>;
+
+ template<typename T>
+ constexpr DiplomaticActionTypeStorage(std::string_view identifier, T&& t) : HasIdentifier(identifier), base_type(t) {}
+
+ template<class Visitor>
+ constexpr decltype(auto) visit(Visitor&& vis){
+ return std::visit(std::forward<Visitor>(vis), *this);
+ }
+
+ template<class Visitor>
+ constexpr decltype(auto) visit(Visitor&& vis) const {
+ return std::visit(std::forward<Visitor>(vis), *this);
+ }
+
+ constexpr bool is_cancelable() const {
+ return visit([](auto&& arg) -> bool {
+ using T = std::decay_t<decltype(arg)>;
+ if constexpr(std::same_as<T, CancelableDiplomaticActionType>) {
+ return true;
+ } else {
+ return false;
+ }
+ });
+ }
+ };
+
+ struct DiplomaticActionTickCache {
+ DiplomaticActionType::Argument argument;
+ const DiplomaticActionTypeStorage* type;
+ bool allowed_to_commit;
+ std::int32_t acceptance = -1;
+ };
+
+ struct DiplomaticActionManager {
+ private:
+ IdentifierRegistry<DiplomaticActionTypeStorage> IDENTIFIER_REGISTRY(diplomatic_action_type);
+
+ public:
+ DiplomaticActionManager();
+
+ bool add_diplomatic_action(std::string_view identifier, DiplomaticActionType::Initializer&& initializer);
+ bool add_cancelable_diplomatic_action(std::string_view identifier, CancelableDiplomaticActionType::Initializer&& initializer);
+
+ DiplomaticActionTickCache
+ create_diplomatic_action_tick(std::string_view identifier, Country& sender, Country& reciever, std::any context_data);
+
+ bool setup_diplomatic_actions();
+ };
+}