Symphony Of Empires
nation.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 // nation.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <cstdint>
28 #include <queue>
29 #include <deque>
30 #include <set>
31 #include <unordered_set>
32 #include <unordered_map>
33 #include <string>
34 #include <vector>
35 #include <functional>
36 #include "eng3d/string.hpp"
37 
38 #include "objects.hpp"
39 #include "event.hpp"
40 #include "indpobj.hpp"
41 
42 class Province;
43 struct Technology;
44 class Pop;
45 namespace TreatyClause {
46  class BaseClause;
47 }
48 struct Technology;
49 class Nation : public RefnameEntity<NationId> {
50  void do_diplomacy() {
52  //diplomatic_timer = glm::max((60 * 48) - glm::min(10.f * 48.f, prestige / 100.f), 4.f);
53  diplomatic_timer = 48;
54  }
55 
56  bool can_do_diplomacy() const {
57  return diplomatic_timer == 0;
58  }
59  Nation& operator=(const Nation&) = default;
60 public:
62  struct Relation {
63  bool is_allied() const {
64  return alliance > 0.f && !has_war;
65  }
66 
67  bool is_customs_union() const {
68  return alliance > 0.5f;
69  }
70 
71  bool has_landpass() const {
72  return relation > 0.5f || alliance > 0.f || has_war;
73  }
74 
75  float relation = 0.f;
76  bool has_war = false;
77  float alliance = 0.f; // From 0 to 1; 0 = diplomatic alliance, tariiff excemption
78  // 1 = political alliance, after this they can form a single country
79  };
80 
82  struct ClientHint {
83  uint32_t color;
86  };
87 
88  void declare_war(Nation& nation, std::vector<TreatyClause::BaseClause*> clauses = std::vector<TreatyClause::BaseClause*>());
89  bool is_ally(const Nation& nation) const;
90  bool is_enemy(const Nation& nation) const;
91  void auto_relocate_capital();
92  void set_policy(const Policies& policies);
93  bool is_accepted_language(const Language& language) const;
94  bool is_accepted_religion(const Religion& relgion) const;
95  float get_tax(const Pop& pop) const;
96  void give_province(Province& province);
97  void control_province(Province& province);
98  const Nation::ClientHint& get_client_hint() const;
99  float get_research_points() const;
100  bool can_research(const Technology& tech) const;
101  void change_research_focus(const Technology& tech);
102  void get_allies(std::function<void(const Nation&)> fn) const;
103 
107  bool exists() const {
108  return !(controlled_provinces.empty());
109  }
110 
111  const Ideology::Subideology& get_subideology() const;
112 
113  // Policies
114  bool can_directly_control_factories() const;
115 
116  void make_puppet(const Nation& master) {
117  this->is_puppeted = true;
118  this->puppet_master_id = master.get_id();
119  }
120 
121  bool is_puppeted_by(const Nation& master) const {
122  return this->is_puppeted && this->puppet_master_id == master.get_id();
123  }
124 
126  float prestige = 0.1f; // Amount of prestige
127  // Total budget of the nation (money in ark), this is not equal to GDP, the GDP is the total sum of the price
128  // of all products in the nation, which are volatile unless they are sold
129  float budget = 10000.f;
130  // Default and can be disabled by the player
131  bool ai_controlled = true;
132  bool ai_do_cmd_troops = true;
133 
134  bool is_puppeted = false;
135  NationId puppet_master_id; // Pupeeter of this nation (if any)
136  ProvinceId capital_id; // The capital of this nation (can be invalid id)
137  IdeologyId ideology_id; // Current ideology of the nation
138  SubideologyId subideology_id; // Current subideology
139  TechnologyId focus_tech_id; // Current tech being researched
140  Policies current_policy; // Current policy of this nation
141  std::vector<float> commodity_production; // Commodity production for each commodity
142  uint16_t diplomatic_timer; // Time until a diplomacy can be done
143  // Accepted languages in this nation, the accepted languages may have some bonuses on provinces *totally*
144  // owned by this nation
145  std::vector<float> language_acceptance;
146  std::vector<float> religion_discrim;
147  // List of provinces which are owned by this nation (including partial ownership)
148  std::vector<ProvinceId> owned_provinces;
149  std::vector<ProvinceId> controlled_provinces;
150  std::deque<Event> inbox; // Inbox of the nation; events that require our attention / should be processed
151  std::vector<float> research; // Progress on technologies (1:1)
152  std::vector<Nation::ClientHint> client_hints; // Hints for the client on how to draw a nation on the client
153  std::unordered_map<std::string, float> flags; // Flags that can be manipulated by events
154  std::string client_username; // Used by clients to store usernames from nations - not saved
155 };
156 template<>
157 struct Eng3D::Deser::Serializer<Nation::Relation> {
158  template<bool is_const>
160  template<bool is_serialize>
162  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.alliance);
163  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.has_war);
164  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.relation);
165  }
166 };
167 template<>
168 struct Eng3D::Deser::Serializer<Nation::ClientHint> {
169  template<bool is_const>
171  template<bool is_serialize>
173  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.color);
174  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.name);
175  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.ideology_id);
176  }
177 };
178 template<>
180  template<bool is_const>
182  template<bool is_serialize>
184  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.cached_id);
185  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.name);
186  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.ref_name);
187  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.is_puppeted);
188  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.puppet_master_id);
189  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.prestige);
190  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.budget);
191  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.capital_id);
192  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.commodity_production);
193  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.language_acceptance);
194  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.religion_discrim);
195  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.owned_provinces);
196  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.controlled_provinces);
197  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.current_policy);
198  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.diplomatic_timer);
199  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.inbox);
200  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.client_hints);
201  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.ideology_id);
202  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.subideology_id);
203  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.research);
204  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.focus_tech_id);
205  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.flags);
206  }
207 };
TechnologyId focus_tech_id
Definition: nation.hpp:139
bool ai_controlled
Definition: nation.hpp:131
Policies current_policy
Definition: nation.hpp:140
NationId puppet_master_id
Definition: nation.hpp:135
std::vector< ProvinceId > controlled_provinces
Definition: nation.hpp:149
float get_tax(const Pop &pop) const
Gets the total tax applied to a POP depending on their "wealth" (not exactly like that,...
Definition: nation.cpp:147
float budget
Definition: nation.hpp:129
std::vector< Nation::ClientHint > client_hints
Definition: nation.hpp:152
bool is_ally(const Nation &nation) const
Definition: nation.cpp:108
ProvinceId capital_id
Definition: nation.hpp:136
Eng3D::StringRef name
Definition: nation.hpp:125
void set_policy(const Policies &policies)
Enacts a policy on a nation.
Definition: nation.cpp:130
void give_province(Province &province)
Gives this nation a specified province (for example on a treaty)
Definition: nation.cpp:156
bool is_accepted_language(const Language &language) const
Checks if a LANGUAGE is part of one of our accepted languages.
Definition: nation.cpp:136
SubideologyId subideology_id
Definition: nation.hpp:138
IdeologyId ideology_id
Definition: nation.hpp:137
uint16_t diplomatic_timer
Definition: nation.hpp:142
bool is_puppeted
Definition: nation.hpp:134
bool can_directly_control_factories() const
Definition: nation.cpp:244
std::vector< float > language_acceptance
Definition: nation.hpp:145
std::vector< float > research
Definition: nation.hpp:151
void make_puppet(const Nation &master)
Definition: nation.hpp:116
std::vector< float > commodity_production
Definition: nation.hpp:141
const Ideology::Subideology & get_subideology() const
Definition: nation.cpp:239
void control_province(Province &province)
Definition: nation.cpp:170
float prestige
Definition: nation.hpp:126
std::unordered_map< std::string, float > flags
Definition: nation.hpp:153
std::deque< Event > inbox
Definition: nation.hpp:150
bool ai_do_cmd_troops
Definition: nation.hpp:132
float get_research_points() const
Definition: nation.cpp:206
bool exists() const
Whetever the nation exists at all - we cannot add nations in-game so we just check if the nation "exi...
Definition: nation.hpp:107
std::string client_username
Definition: nation.hpp:154
bool is_accepted_religion(const Religion &relgion) const
Checks if a RELIGION is part of one of our accepted relgion.
Definition: nation.cpp:141
bool is_puppeted_by(const Nation &master) const
Definition: nation.hpp:121
void get_allies(std::function< void(const Nation &)> fn) const
Definition: nation.cpp:232
bool can_research(const Technology &tech) const
Definition: nation.cpp:218
std::vector< ProvinceId > owned_provinces
Definition: nation.hpp:148
void change_research_focus(const Technology &tech)
Definition: nation.cpp:226
bool is_enemy(const Nation &nation) const
Definition: nation.cpp:114
void declare_war(Nation &nation, std::vector< TreatyClause::BaseClause * > clauses=std::vector< TreatyClause::BaseClause * >())
Declare war.
Definition: nation.cpp:43
std::vector< float > religion_discrim
Definition: nation.hpp:146
void auto_relocate_capital()
Automatically relocates the capital of a nation to another province Use this when a treaty makes a na...
Definition: nation.cpp:120
const Nation::ClientHint & get_client_hint() const
Definition: nation.cpp:202
Definition: indpobj.hpp:264
A single province, which is used to simulate economy in a "bulk-tiles" way instead of doing economica...
Definition: province.hpp:48
Base class that serves as archiver, stores (in memory) the data required for serialization/deserializ...
Definition: serializer.hpp:64
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj)
Definition: nation.hpp:183
typename Eng3D::Deser::CondConstType< is_const, Nation >::type type
Definition: nation.hpp:181
typename Eng3D::Deser::CondConstType< is_const, Nation::ClientHint >::type type
Definition: nation.hpp:170
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj)
Definition: nation.hpp:172
typename Eng3D::Deser::CondConstType< is_const, Nation::Relation >::type type
Definition: nation.hpp:159
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj)
Definition: nation.hpp:161
A serializer (base class) which can be used to serialize objects and create per-object optimized clas...
Definition: serializer.hpp:111
A reference to a string on the global string pool.
Definition: string.hpp:39
constexpr Id get_id() const
Definition: entity.hpp:152
Hints for the client on how to display the nation.
Definition: nation.hpp:82
IdeologyId ideology_id
Definition: nation.hpp:85
Eng3D::StringRef name
Definition: nation.hpp:84
uint32_t color
Definition: nation.hpp:83
Diplomatic relations between two nations.
Definition: nation.hpp:62
bool is_allied() const
Definition: nation.hpp:63
bool has_landpass() const
Definition: nation.hpp:71
float relation
Definition: nation.hpp:75
bool is_customs_union() const
Definition: nation.hpp:67
float alliance
Definition: nation.hpp:77
An entity which can be referenced via a ref_name and also via id.
Definition: entity.hpp:160