aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/history/DiplomaticHistory.cpp
blob: 883a212c0fcce1538b5f3f602966396528968b1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "DiplomaticHistory.hpp"

#include "openvic-simulation/GameManager.hpp"
#include "openvic-simulation/dataloader/NodeTools.hpp"
#include "openvic-simulation/history/Period.hpp"

using namespace OpenVic;
using namespace OpenVic::NodeTools;

WarHistory::added_wargoal_t::added_wargoal_t(
   Date new_added,
   Country const* new_actor,
   Country const* new_receiver,
   WargoalType const* new_wargoal,
   std::optional<Country const*> new_third_party,
   std::optional<Province const*> new_target
) : added { new_added }, actor { new_actor }, receiver { new_receiver }, wargoal { new_wargoal }, third_party { new_third_party }, target { new_target } {}

WarHistory::war_participant_t::war_participant_t(
   Country const* new_country,
   const Period new_period
) : country { new_country }, period { new_period } {}

WarHistory::WarHistory(
   std::string_view new_war_name,
   std::vector<war_participant_t>&& new_attackers,
   std::vector<war_participant_t>&& new_defenders,
   std::vector<added_wargoal_t>&& new_wargoals
) : war_name { new_war_name }, attackers { std::move(new_attackers) }, defenders { std::move(new_defenders) }, wargoals { std::move(new_wargoals) } {}

AllianceHistory::AllianceHistory(
   Country const* new_first,
   Country const* new_second,
   const Period new_period
) : first { new_first }, second { new_second }, period {new_period} {}

ReparationsHistory::ReparationsHistory(
   Country const* new_receiver,
   Country const* new_sender,
   const Period new_period
) : receiver { new_receiver }, sender { new_sender }, period {new_period} {}

SubjectHistory::SubjectHistory(
   Country const* new_overlord,
   Country const* new_subject,
   const type_t new_type,
   const Period new_period
) : overlord { new_overlord }, subject { new_subject }, type { new_type }, period {new_period} {}

void DiplomaticHistoryManager::lock_diplomatic_history() {
   Logger::info("Locked diplomacy history registry after registering ", alliances.size() + subjects.size() + wars.size(), " items");
   locked = true;
}

bool DiplomaticHistoryManager::is_locked() const {
   return locked;
}

std::vector<AllianceHistory const*> DiplomaticHistoryManager::get_alliances(Date date) const {
   std::vector<AllianceHistory const*> ret {};
   for (const auto& alliance : alliances) {
      if (alliance.period.is_date_in_period(date)) {
         ret.push_back(&alliance);
      }
   }
   return ret;
}

std::vector<ReparationsHistory const*> DiplomaticHistoryManager::get_reparations(Date date) const {
   std::vector<ReparationsHistory const*> ret {};
   for (const auto& reparation : reparations) {
      if (reparation.period.is_date_in_period(date)) {
         ret.push_back(&reparation);
      }
   }
   return ret;
}

std::vector<SubjectHistory const*> DiplomaticHistoryManager::get_subjects(Date date) const {
   std::vector<SubjectHistory const*> ret {};
   for (const auto& subject : subjects) {
      if (subject.period.is_date_in_period(date)) {
         ret.push_back(&subject);
      }
   }
   return ret;
}

std::vector<WarHistory const*> DiplomaticHistoryManager::get_wars(Date date) const {
   std::vector<WarHistory const*> ret;
   for (const auto& war : wars) {
      Date start {};
      for (const auto& wargoal : war.wargoals) {
         if (wargoal.added < start) start = wargoal.added;
      }
      if (start >= date) ret.push_back(&war);
   }
   return ret;
}

bool DiplomaticHistoryManager::load_diplomacy_history_file(CountryManager const& country_manager, ast::NodeCPtr root) {
   return expect_dictionary_keys(
      "alliance", ZERO_OR_MORE, [this, &country_manager](ast::NodeCPtr node) -> bool {
         Country const* first = nullptr;
         Country const* second = nullptr;
         Date start {};
         std::optional<Date> end {};

         bool ret = expect_dictionary_keys(
            "first", ONE_EXACTLY, expect_identifier_or_string(country_manager.expect_country_str(assign_variable_callback_pointer(first))),
            "second", ONE_EXACTLY, expect_identifier_or_string(country_manager.expect_country_str(assign_variable_callback_pointer(second))),
            "start_date", ONE_EXACTLY, expect_identifier_or_string(expect_date_str(assign_variable_callback(start))),
            "end_date", ZERO_OR_ONE, expect_identifier_or_string(expect_date_str(assign_variable_callback(end)))
         )(node);

         alliances.push_back({ first, second, { start, end } });
         return ret;
      },
      "vassal", ZERO_OR_MORE, [this, &country_manager](ast::NodeCPtr node) -> bool {
         Country const* overlord = nullptr;
         Country const* subject = nullptr;
         Date start {};
         std::optional<Date> end {};

         bool ret = expect_dictionary_keys(
            "first", ONE_EXACTLY, expect_identifier_or_string(country_manager.expect_country_str(assign_variable_callback_pointer(overlord))),
            "second", ONE_EXACTLY, expect_identifier_or_string(country_manager.expect_country_str(assign_variable_callback_pointer(subject))),
            "start_date", ONE_EXACTLY, expect_identifier_or_string(expect_date_str(assign_variable_callback(start))),
            "end_date", ZERO_OR_ONE, expect_identifier_or_string(expect_date_str(assign_variable_callback(end)))
         )(node);

         subjects.push_back({ overlord, subject, SubjectHistory::type_t::VASSAL, { start, end } });
         return ret;
      },
      "union", ZERO_OR_MORE, [this, &country_manager](ast::NodeCPtr node) -> bool {
         Country const* overlord = nullptr;
         Country const* subject = nullptr;
         Date start {};
         std::optional<Date> end {};

         bool ret = expect_dictionary_keys(
            "first", ONE_EXACTLY, country_manager.expect_country_identifier(assign_variable_callback_pointer(overlord)),
            "second", ONE_EXACTLY, country_manager.expect_country_identifier(assign_variable_callback_pointer(subject)),
            "start_date", ONE_EXACTLY, expect_date(assign_variable_callback(start)),
            "end_date", ZERO_OR_ONE, expect_date(assign_variable_callback(end))
         )(node);

         subjects.push_back({ overlord, subject, SubjectHistory::type_t::UNION, { start, end } });
         return ret;
      },
      "substate", ZERO_OR_MORE, [this, &country_manager](ast::NodeCPtr node) -> bool {
         Country const* overlord = nullptr;
         Country const* subject = nullptr;
         Date start {};
         std::optional<Date> end {};

         bool ret = expect_dictionary_keys(
            "first", ONE_EXACTLY, country_manager.expect_country_identifier(assign_variable_callback_pointer(overlord)),
            "second", ONE_EXACTLY, country_manager.expect_country_identifier(assign_variable_callback_pointer(subject)),
            "start_date", ONE_EXACTLY, expect_date(assign_variable_callback(start)),
            "end_date", ZERO_OR_ONE, expect_date(assign_variable_callback(end))
         )(node);

         subjects.push_back({ overlord, subject, SubjectHistory::type_t::SUBSTATE, { start, end } });
         return ret;
      },
      "reparations", ZERO_OR_MORE, [this, &country_manager](ast::NodeCPtr node) -> bool {
         Country const* receiver = nullptr;
         Country const* sender = nullptr;
         Date start {};
         std::optional<Date> end {};

         bool ret = expect_dictionary_keys(
            "first", ONE_EXACTLY, country_manager.expect_country_identifier(assign_variable_callback_pointer(receiver)),
            "second", ONE_EXACTLY, country_manager.expect_country_identifier(assign_variable_callback_pointer(sender)),
            "start_date", ONE_EXACTLY, expect_date(assign_variable_callback(start)),
            "end_date", ZERO_OR_ONE, expect_date(assign_variable_callback(end))
         )(node);

         reparations.push_back({ receiver, sender, { start, end } });
         return ret;
      }
   )(root);
}

bool DiplomaticHistoryManager::load_war_history_file(GameManager const& game_manager, ast::NodeCPtr root) {
   std::string name = "";
   std::vector<WarHistory::war_participant_t> attackers {};
   std::vector<WarHistory::war_participant_t> defenders {};
   std::vector<WarHistory::added_wargoal_t> wargoals {};
   Date current_date {};

   bool ret = expect_dictionary_keys_and_default(
      [&game_manager, &attackers, &defenders, &wargoals, &current_date, &name](std::string_view key, ast::NodeCPtr node) -> bool {
         bool ret = expect_date_str(assign_variable_callback(current_date))(key);
         ret &= expect_dictionary_keys(
            "add_attacker", ZERO_OR_MORE, game_manager.get_country_manager().expect_country_identifier([&attackers, &current_date, &name](Country const& country) -> bool {
               for (const auto& attacker : attackers) {
                  if (attacker.get_country() == &country) {
                     Logger::error("In history of war ", name, " at date ", current_date.to_string(), ": Attempted to add attacking country ", attacker.get_country()->get_identifier(), " which is already present!");
                     return false;
                  }
               }

               attackers.push_back({ &country, { current_date, {} } });
               return true;
            }),
            "add_defender", ZERO_OR_MORE, game_manager.get_country_manager().expect_country_identifier([&defenders, &current_date, &name](Country const& country) -> bool {
               for (const auto& defender : defenders) {
                  if (defender.get_country() == &country) {
                     Logger::error("In history of war ", name, " at date ", current_date.to_string(), ": Attempted to add defending country ", defender.get_country()->get_identifier(), " which is already present!");
                     return false;
                  }
               }

               defenders.push_back({ &country, { current_date, {} } });
               return true;
            }),
            "rem_attacker", ZERO_OR_MORE, game_manager.get_country_manager().expect_country_identifier([&attackers, &current_date, &name](Country const& country) -> bool {
               WarHistory::war_participant_t* participant_to_remove = nullptr;

               for (auto& attacker : attackers) {
                  if (attacker.country == &country) {
                     participant_to_remove = &attacker;
                     break;
                  }
               }

               if (participant_to_remove == nullptr) {
                  Logger::warning("In history of war ", name, " at date ", current_date.to_string(), ": Attempted to remove attacking country ", country.get_identifier(), " which was not present!");
                  return true;
               }

               return participant_to_remove->period.try_set_end(current_date);
            }),
            "rem_defender", ZERO_OR_MORE, game_manager.get_country_manager().expect_country_identifier([&defenders, &current_date, &name](Country const& country) -> bool {
               WarHistory::war_participant_t* participant_to_remove = nullptr;

               for (auto& defender : defenders) {
                  if (defender.country == &country) {
                     participant_to_remove = &defender;
                     break;
                  }
               }

               if (participant_to_remove == nullptr) {
                  Logger::warning("In history of war ", name, " at date ", current_date.to_string(), ": Attempted to remove attacking country ", country.get_identifier(), " which was not present!");
                  return true;
               }

               return participant_to_remove->period.try_set_end(current_date);
            }),
            "war_goal", ZERO_OR_MORE, [&game_manager, &wargoals, &current_date](ast::NodeCPtr value) -> bool {
               Country const* actor = nullptr;
               Country const* receiver = nullptr;
               WargoalType const* type = nullptr;
               std::optional<Country const*> third_party {};
               std::optional<Province const*> target {};

               bool ret = expect_dictionary_keys(
                  "actor", ONE_EXACTLY, game_manager.get_country_manager().expect_country_identifier(assign_variable_callback_pointer(actor)),
                  "receiver", ONE_EXACTLY, game_manager.get_country_manager().expect_country_identifier(assign_variable_callback_pointer(receiver)),
                  "casus_belli", ONE_EXACTLY, game_manager.get_military_manager().get_wargoal_type_manager().expect_wargoal_type_identifier(assign_variable_callback_pointer(type)),
                  "country", ZERO_OR_ONE, game_manager.get_country_manager().expect_country_identifier(assign_variable_callback_pointer(*third_party)),
                  "state_province_id", ZERO_OR_ONE, game_manager.get_map().expect_province_identifier(assign_variable_callback_pointer(*target))
               )(value);
               wargoals.push_back({ current_date, actor, receiver, type, third_party, target });
               return ret;
            }
         )(node);
         return ret;
      },
      "name", ZERO_OR_ONE, expect_string(assign_variable_callback_string(name))
   )(root);

   wars.push_back({ name, std::move(attackers), std::move(defenders), std::move(wargoals) });
   return ret;
}