Symphony Of Empires
province.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 // province.hpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #pragma once
26 
27 #include <cstdint>
28 #include <vector>
29 #include <unordered_set>
30 #include <string>
31 #include <array>
32 #include <memory>
33 #include <glm/vec3.hpp>
34 #include <glm/vec2.hpp>
35 #include "eng3d/rectangle.hpp"
36 
37 #include "objects.hpp"
38 #include "indpobj.hpp"
39 #include "product.hpp"
40 #include "building.hpp"
41 #include "diplomacy.hpp"
42 
43 class World;
44 class Nation;
45 
48 class Province : public RefnameEntity<ProvinceId> {
49  Province& operator=(const Province&) = delete;
50 public:
51  float total_pops() const {
52  auto total = 0.f;
53  for(const auto& pop : pops)
54  total += pop.size;
55  return total;
56  }
57 
58  float average_militancy() const {
59  auto total = 0.f;
60  for(const auto& pop : pops)
61  total += pop.militancy;
62  return total / pops.size();
63  }
64 
65  float get_attractiveness(const Pop& pop) const;
66  void add_building(const BuildingType& building_type);
68  bool is_neighbour(const Province& province) const;
69 
70  inline glm::vec2 get_pos() const {
71  return glm::vec2(box_area.left + ((box_area.right - box_area.left) / 2.f), box_area.top + ((box_area.bottom - box_area.top) / 2.f));
72  }
73 
74  float euclidean_distance(const Province& other_province, glm::vec2 world_size, float radius) const;
75 
76  inline const std::vector<Building>& get_buildings() const {
77  return buildings;
78  }
79 
80  inline std::vector<Building>& get_buildings() {
81  return buildings;
82  }
83 
84  bool is_populated() const {
85  for(const auto& pop : pops)
86  if(pop.size > 0.f)
87  return true;
88  return false;
89  }
90 
91  void unpopulate() {
92  for(auto& pop : pops)
93  pop.size = 0.f;
94  }
95 
97  std::uint32_t color = 0; // Color of the province, used for mapping the province's shape from the map_div.png file
98  bool is_coastal = false;
99  float base_attractive = 0.f; // Attractiveness of province
100  // Rectangle coordinates (x,y - x,y) for "area" scanning a province when needed
101  // (for example, when changing owners)
102  Eng3D::Rect box_area = Eng3D::Rect(0.f, 0.f, 0.f, 0.f);
103  NationId owner_id; // The owner of this province
106  std::vector<uint32_t> rgo_size; // How much of each rgo that can be extracted
107  std::array<Pop, 7> pops;
108 
109  std::vector<Product> products;
110  std::vector<Building> buildings;
111  struct Battle {
112  Battle() = default;
113  float attacker_casualties = 0.f;
114  float defender_casualties = 0.f;
115  std::vector<UnitId> unit_ids; // Units inside the battle
116  std::vector<NationId> attacker_nations_ids;
117  std::vector<NationId> defender_nations_ids;
118  bool active = false;
119 
120  std::vector<UnitId> get_attacker_unit_ids() const;
121  std::vector<UnitId> get_defender_unit_ids() const;
123  std::vector<NationId> nuclei; // Nations who have a nuclei in this province
124  std::vector<ProvinceId> neighbour_ids; // Neighbouring provinces
126  std::vector<float> languages;
128  std::vector<float> religions;
129 };
130 template<>
132  template<bool is_const>
134  template<bool is_serialize>
136  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.attacker_casualties);
137  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.defender_casualties);
138  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.unit_ids);
139  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.attacker_nations_ids);
140  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.defender_nations_ids);
141  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.active);
142  }
143 };
144 template<>
146  template<bool is_const>
148  template<bool is_serialize>
150  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.cached_id);
151  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.name);
152  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.ref_name);
153  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.color);
154  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.box_area);
155  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.owner_id);
156  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.nuclei);
157  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.rgo_size);
158  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.neighbour_ids);
159  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.products);
160  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.pops);
161  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.buildings);
162  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.controller_id);
163  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.base_attractive);
164  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.languages);
165  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.religions);
166  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.terrain_type_id);
167  Eng3D::Deser::deser_dynamic<is_serialize>(ar, obj.battle);
168  }
169 };
170 
172  ProvinceManager& operator=(const ProvinceManager&) = default;
173  std::vector<ProvinceId> recently_changed_owner;
174  std::vector<ProvinceId> recently_changed_control;
175  bool changed;
176 public:
177  ProvinceManager() = default;
178 
179  inline void mark_province_owner_changed(ProvinceId province_id) {
180  recently_changed_owner.push_back(province_id);
181  }
182 
183  inline void mark_province_control_changed(ProvinceId province_id) {
184  recently_changed_control.push_back(province_id);
185  }
186 
187  inline void clear() {
188  recently_changed_owner.clear();
189  recently_changed_control.clear();
190  }
191 
192  inline const std::vector<ProvinceId>& get_changed_owner_provinces() const {
193  return recently_changed_owner;
194  }
195 
196  inline const std::vector<ProvinceId>& get_changed_control_provinces() const {
197  return recently_changed_control;
198  }
199 
200  inline bool is_provinces_changed() const {
201  return !recently_changed_owner.empty() || !recently_changed_control.empty();
202  }
203 };
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
std::vector< float > religions
Percentage of each religion prescence on the pops, from 0 to 1.
Definition: province.hpp:128
glm::vec2 get_pos() const
Definition: province.hpp:70
std::vector< Building > & get_buildings()
Definition: province.hpp:80
float get_attractiveness(const Pop &pop) const
Definition: province.cpp:42
float base_attractive
Definition: province.hpp:99
float average_militancy() const
Definition: province.hpp:58
std::array< Pop, 7 > pops
Definition: province.hpp:107
void cancel_construction_project()
Definition: province.cpp:54
Eng3D::Rect box_area
Definition: province.hpp:102
std::vector< Product > products
Definition: province.hpp:109
std::vector< ProvinceId > neighbour_ids
Definition: province.hpp:124
bool is_neighbour(const Province &province) const
Definition: province.cpp:68
void add_building(const BuildingType &building_type)
Definition: province.cpp:48
std::uint32_t color
Definition: province.hpp:97
Eng3D::StringRef name
Definition: province.hpp:96
const std::vector< Building > & get_buildings() const
Definition: province.hpp:76
NationId controller_id
Definition: province.hpp:104
struct Province::Battle battle
std::vector< uint32_t > rgo_size
Definition: province.hpp:106
std::vector< NationId > nuclei
Definition: province.hpp:123
std::vector< float > languages
Percentage of each languages from 0 to 1.
Definition: province.hpp:126
bool is_populated() const
Definition: province.hpp:84
TerrainTypeId terrain_type_id
Definition: province.hpp:105
bool is_coastal
Definition: province.hpp:98
NationId owner_id
Definition: province.hpp:103
float euclidean_distance(const Province &other_province, glm::vec2 world_size, float radius) const
Definition: province.cpp:59
float total_pops() const
Definition: province.hpp:51
std::vector< Building > buildings
Definition: province.hpp:110
void unpopulate()
Definition: province.hpp:91
const std::vector< ProvinceId > & get_changed_control_provinces() const
Definition: province.hpp:196
ProvinceManager()=default
const std::vector< ProvinceId > & get_changed_owner_provinces() const
Definition: province.hpp:192
bool is_provinces_changed() const
Definition: province.hpp:200
void mark_province_control_changed(ProvinceId province_id)
Definition: province.hpp:183
void mark_province_owner_changed(ProvinceId province_id)
Definition: province.hpp:179
Definition: world.hpp:114
struct Rectangle Rect
Definition: rectangle.hpp:176
Type for military outposts.
Definition: building.hpp:38
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: province.hpp:149
typename Eng3D::Deser::CondConstType< is_const, Province >::type type
Definition: province.hpp:147
typename Eng3D::Deser::CondConstType< is_const, Province::Battle >::type type
Definition: province.hpp:133
static void deser_dynamic(Eng3D::Deser::Archive &ar, type< is_serialize > &obj)
Definition: province.hpp:135
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
std::vector< UnitId > get_attacker_unit_ids() const
Definition: province.cpp:72
float defender_casualties
Definition: province.hpp:114
float attacker_casualties
Definition: province.hpp:113
std::vector< NationId > defender_nations_ids
Definition: province.hpp:117
std::vector< UnitId > unit_ids
Definition: province.hpp:115
std::vector< UnitId > get_defender_unit_ids() const
Definition: province.cpp:84
std::vector< NationId > attacker_nations_ids
Definition: province.hpp:116
Battle()=default
An entity which can be referenced via a ref_name and also via id.
Definition: entity.hpp:160