Symphony Of Empires
province.cpp
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.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include "province.hpp"
26 #include "world.hpp"
27 #include "product.hpp"
28 #include "unit.hpp"
29 #include "building.hpp"
30 #include "indpobj.hpp"
31 #include <glm/vec3.hpp>
32 #include <glm/gtc/constants.hpp>
33 #include <glm/trigonometric.hpp>
34 #include <glm/geometric.hpp>
35 
36 #include "eng3d/utils.hpp"
37 
38 //
39 // Province
40 //
41 // Calculates the total number of POPs in this province (total population)
42 float Province::get_attractiveness(const Pop& pop) const {
43  float attractive = this->base_attractive;
44  const auto& owner = g_world.nations[this->owner_id];
45  return attractive;
46 }
47 
48 void Province::add_building(const BuildingType& building_type) {
49  // Now build the building
50  this->buildings[building_type].level += 1.f;
51  this->buildings[building_type].req_goods = building_type.req_goods;
52 }
53 
55  for(auto& building : buildings)
56  building.stop_working_on_unit();
57 }
58 
59 float Province::euclidean_distance(const Province& other_province, glm::vec2 world_size, float radius) const {
60  const auto sphere_coord1 = Eng3D::get_sphere_coord(world_size, this->get_pos(), radius);
61  const auto sphere_coord2 = Eng3D::get_sphere_coord(world_size, other_province.get_pos(), radius);
62  const auto cos_angle = glm::dot(sphere_coord1, sphere_coord2) / (radius * radius);
63  const auto angle = glm::acos(glm::clamp(cos_angle, -1.f, 1.f));
64  const auto distance = angle * radius;
65  return distance;
66 }
67 
68 bool Province::is_neighbour(const Province& province) const {
69  return std::find(this->neighbour_ids.begin(), this->neighbour_ids.end(), province) != this->neighbour_ids.end();
70 }
71 
72 std::vector<UnitId> Province::Battle::get_attacker_unit_ids() const {
73  const auto& units = g_world.unit_manager.units;
74  std::vector<UnitId> v;
75  for(const auto nation_id : this->attacker_nations_ids)
76  for(const auto unit_id : this->unit_ids)
77  if(units[unit_id].owner_id == nation_id)
78  v.push_back(unit_id);
79  std::sort(v.begin(), v.end());
80  v.erase(std::unique(v.begin(), v.end()), v.end());
81  return v;
82 }
83 
84 std::vector<UnitId> Province::Battle::get_defender_unit_ids() const {
85  const auto& units = g_world.unit_manager.units;
86  std::vector<UnitId> v;
87  for(const auto nation_id : this->defender_nations_ids)
88  for(const auto unit_id : this->unit_ids)
89  if(units[unit_id].owner_id == nation_id)
90  v.push_back(unit_id);
91  std::sort(v.begin(), v.end());
92  v.erase(std::unique(v.begin(), v.end()), v.end());
93  return v;
94 }
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
glm::vec2 get_pos() const
Definition: province.hpp:70
float get_attractiveness(const Pop &pop) const
Definition: province.cpp:42
float base_attractive
Definition: province.hpp:99
void cancel_construction_project()
Definition: province.cpp:54
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
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
std::vector< Building > buildings
Definition: province.hpp:110
Eng3D::Freelist< Unit > units
Definition: unit.hpp:173
UnitManager unit_manager
Definition: world.hpp:145
glm::vec3 get_sphere_coord(glm::vec2 size, glm::vec2 pos, float radius)
Definition: utils.hpp:149
Type for military outposts.
Definition: building.hpp:38
std::vector< std::pair< CommodityId, float > > req_goods
Definition: building.hpp:65
std::vector< UnitId > get_attacker_unit_ids() const
Definition: province.cpp:72
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
World g_world
Definition: world.cpp:59