Symphony Of Empires
diplomacy.hpp
Go to the documentation of this file.
1 // Symphony of Empires
2 // Copyright (C) 2021, Symphony of Empires contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // diplomacy.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <cstdint>
28 #include <cstddef>
29 #include <vector>
30 #include "eng3d/string.hpp"
31 
32 #include "objects.hpp"
33 #include "indpobj.hpp"
34 
35 class Nation;
36 class Province;
37 
38 namespace Diplomacy {
39  // Determines if the other nation is a friendly potential ally
40  inline bool is_friend(Nation& us, Nation& them);
41 
42  // Determines if the other nation is an enemy and potential rival
43  inline bool is_foe(Nation& us, Nation& them);
44 }
45 
46 enum class TreatyClauseType {
47  PAYMENT,
48  HUMILIATE,
52  CEASEFIRE,
53  PUPPET,
54  //TECHNOLOGY,
55 };
56 template<>
58 
59 namespace TreatyClause {
60  class BaseClause {
61  public:
62  BaseClause() = default;
63  BaseClause(const Nation& _sender, const Nation& _receiver);
64  virtual ~BaseClause() = default;
65 
67  NationId sender_id; // Who created this clause
68  NationId receiver_id; // Who should accept/reject this clause
69  size_t days_duration = 0; // Days this clause lasts
70  bool done = false; // Used for 1 time clauses
71 
72  // Function to determine the "political" cost of this clause, and how much willing the AI
73  // is to accept this clause, this is only used by the AI
74  virtual unsigned cost() {
75  return 0;
76  }
77 
78  // Function to enforce the policy per day (or higher time spans)
79  virtual void enforce() {}
80 
81  // Determines whenever the clause is in effect or not, when it is not in effect
82  // then it's removed permanently
83  virtual bool in_effect() const {
84  return false;
85  }
86  };
87 
88  // Makes loser to pay war reparations to the winner
89  class Payment : public BaseClause {
90  public:
92  : BaseClause()
93  {
95  }
96  unsigned cost();
97  void enforce();
98  bool in_effect() const;
99 
100  float amount = 0.f;
101  };
102 
103  // Reduces prestige of loser and increments prestige from winner
104  class Humiliate : public BaseClause {
105  public:
107  : BaseClause()
108  {
110  }
111  unsigned cost();
112  void enforce();
113  bool in_effect() const;
114 
115  float amount = 0.f;
116  };
117 
118  // Liberates a nation from another
119  class LiberateNation : public BaseClause {
120  public:
122  : BaseClause()
123  {
125  }
126  unsigned cost();
127  void enforce();
128  bool in_effect() const;
129 
131  std::vector<ProvinceId> province_ids;
132  };
133 
134  // Imposes a policy to be put in action on a nation
135  class ImposePolicies : public BaseClause {
136  public:
138  : BaseClause()
139  {
141  }
142  unsigned cost();
143  void enforce();
144  bool in_effect() const;
145 
147  };
148 
149  // Annexes territory from the loser
150  class AnnexProvince : public BaseClause {
151  public:
153  : BaseClause()
154  {
156  }
157  unsigned cost();
158  void enforce();
159  bool in_effect() const;
160 
161  std::vector<ProvinceId> province_ids;
162  };
163 
164  // Makes the receiver the puppet of the sender
165  class Puppet : public BaseClause {
166  public:
168  : BaseClause()
169  {
171  }
172  unsigned cost();
173  void enforce();
174  bool in_effect() const;
175  };
176 }
177 template<>
179  template<bool is_const>
181 
182  template<bool is_serialize>
183  static inline void deser_dynamic(Eng3D::Deser::Archive& ar, type<is_serialize>*& obj) {
184  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj->sender_id);
185  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj->receiver_id);
186  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj->days_duration);
187  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj->done);
188  if constexpr(is_serialize) {
189  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj->type);
190  switch(obj->type) {
192  const auto& dyn_clause = (TreatyClause::AnnexProvince&)*obj;
193  Eng3D::Deser::deser_dynamic<is_serialize>(ar, dyn_clause.province_ids);
194  } break;
196  const auto& dyn_clause = (TreatyClause::LiberateNation&)*obj;
197  Eng3D::Deser::deser_dynamic<is_serialize>(ar, dyn_clause.province_ids);
198  Eng3D::Deser::deser_dynamic<is_serialize>(ar, dyn_clause.liberated_id);
199  } break;
201  const auto& dyn_clause = (TreatyClause::ImposePolicies&)*obj;
202  Eng3D::Deser::deser_dynamic<is_serialize>(ar, dyn_clause.imposed);
203  } break;
205  const auto& dyn_clause = (TreatyClause::Payment&)*obj;
206  Eng3D::Deser::deser_dynamic<is_serialize>(ar, dyn_clause.amount);
207  } break;
209  const auto& dyn_clause = (TreatyClause::Humiliate&)*obj;
210  Eng3D::Deser::deser_dynamic<is_serialize>(ar, dyn_clause.amount);
211  } break;
213  const auto& dyn_clause = (TreatyClause::Humiliate&)*obj;
214  } break;
215  default:
216  break;
217  }
218  } else {
220  Eng3D::Deser::deser_dynamic<is_serialize>(ar, type);
221  switch(type) {
223  obj = new TreatyClause::AnnexProvince();
224  auto dyn_clause = (TreatyClause::AnnexProvince&)*obj;
225  Eng3D::Deser::deser_dynamic<is_serialize>(ar, dyn_clause.province_ids);
226  } break;
228  obj = new TreatyClause::LiberateNation();
229  auto dyn_clause = (TreatyClause::LiberateNation&)*obj;
230  Eng3D::Deser::deser_dynamic<is_serialize>(ar, dyn_clause.province_ids);
231  Eng3D::Deser::deser_dynamic<is_serialize>(ar, dyn_clause.liberated_id);
232  } break;
234  obj = new TreatyClause::ImposePolicies();
235  auto dyn_clause = (TreatyClause::ImposePolicies&)*obj;
236  Eng3D::Deser::deser_dynamic<is_serialize>(ar, dyn_clause.imposed);
237  } break;
239  obj = new TreatyClause::Payment();
240  auto dyn_clause = (TreatyClause::Payment&)*obj;
241  Eng3D::Deser::deser_dynamic<is_serialize>(ar, dyn_clause.amount);
242  } break;
244  obj = new TreatyClause::Humiliate();
245  auto dyn_clause = (TreatyClause::Humiliate&)*obj;
246  Eng3D::Deser::deser_dynamic<is_serialize>(ar, dyn_clause.amount);
247  } break;
249  obj = new TreatyClause::Puppet();
250  } break;
251  default:
252  break;
253  }
254  }
255  }
256 };
257 
258 enum class TreatyApproval {
259  UNDECIDED,
260  ACCEPTED,
261  DENIED,
262  ABSENT,
263 };
264 template<>
266 
267 struct Treaty : Entity<TreatyId> {
268  bool does_participate(const Nation& nation) const;
269  bool in_effect() const;
270 
272  NationId sender_id; // The one who sent the treaty
273  NationId receiver_id; // The one who is going to receive this treaty
274  // Clauses of this treaty;
275  std::vector<TreatyClause::BaseClause*> clauses;
276  // List of who are involved in the treaty
277  std::vector<std::pair<NationId, TreatyApproval>> approval_status;
278 };
279 template<>
281  template<bool is_const>
283 
284  template<bool is_serialize>
286  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.cached_id);
287  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.name);
288  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.receiver_id);
289  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.sender_id);
290  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.approval_status);
291  }
292 };
A single province, which is used to simulate economy in a "bulk-tiles" way instead of doing economica...
Definition: province.hpp:48
std::vector< ProvinceId > province_ids
Definition: diplomacy.hpp:161
virtual void enforce()
Definition: diplomacy.hpp:79
virtual bool in_effect() const
Definition: diplomacy.hpp:83
virtual ~BaseClause()=default
enum TreatyClauseType type
Definition: diplomacy.hpp:66
virtual unsigned cost()
Definition: diplomacy.hpp:74
bool in_effect() const
Definition: diplomacy.cpp:83
std::vector< ProvinceId > province_ids
Definition: diplomacy.hpp:131
bool in_effect() const
Definition: diplomacy.cpp:64
bool in_effect() const
Definition: diplomacy.cpp:158
TreatyApproval
Definition: diplomacy.hpp:258
TreatyClauseType
Definition: diplomacy.hpp:46
bool is_foe(Nation &us, Nation &them)
Definition: diplomacy.cpp:44
bool is_friend(Nation &us, Nation &them)
Definition: diplomacy.cpp:38
Base class that serves as archiver, stores (in memory) the data required for serialization/deserializ...
Definition: serializer.hpp:64
typename Eng3D::Deser::CondConstType< is_const, Treaty >::type type
Definition: diplomacy.hpp:282
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj)
Definition: diplomacy.hpp:285
typename Eng3D::Deser::CondConstType< is_const, TreatyClause::BaseClause >::type type
Definition: diplomacy.hpp:180
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > *&obj)
Definition: diplomacy.hpp:183
A serializer (base class) which can be used to serialize objects and create per-object optimized clas...
Definition: serializer.hpp:111
typename CondConstType< is_const, T >::type type
Definition: serializer.hpp:113
A serializer optimized to memcpy directly the element into the byte stream use only when the object c...
Definition: serializer.hpp:162
A reference to a string on the global string pool.
Definition: string.hpp:39
An entity which can only be referenced by an (presumably) unique Id this is the base class for the ot...
Definition: entity.hpp:94
bool in_effect() const
Definition: diplomacy.cpp:171
NationId receiver_id
Definition: diplomacy.hpp:273
std::vector< std::pair< NationId, TreatyApproval > > approval_status
Definition: diplomacy.hpp:277
NationId sender_id
Definition: diplomacy.hpp:272
Eng3D::StringRef name
Definition: diplomacy.hpp:271
bool does_participate(const Nation &nation) const
Definition: diplomacy.cpp:163
std::vector< TreatyClause::BaseClause * > clauses
Definition: diplomacy.hpp:275