28 #include <unordered_map>
29 #include <unordered_set>
31 #include <glm/glm.hpp>
32 #include <glm/vec3.hpp>
33 #include <glm/gtc/constants.hpp>
34 #include <glm/trigonometric.hpp>
35 #include <glm/geometric.hpp>
37 #include "eng3d/pathfind.hpp"
38 #include "eng3d/log.hpp"
39 #include "eng3d/utils.hpp"
50 return std::hash<decltype(
id.
id)>{}(
id.id);
58 const auto prev_size = enemy.
size;
63 enemy.
size -= glm::min(enemy.
size, damage);
64 return prev_size - enemy.
size;
70 this->target_province_id = province.
get_id();
71 this->has_target =
true;
79 auto& province = world.provinces[this->
province_id()];
80 return province.get_pos();
86 return world.unit_manager.unit_province[
cached_id];
91 auto& start_province = world.provinces[this->
province_id()];
92 auto& end_province = _province;
94 const glm::vec2 world_size{ world.width, world.height };
95 const auto distance = start_province.
euclidean_distance(end_province, world_size, 100.f) * world.terrain_types[start_province.terrain_type_id].penalty * world.terrain_types[end_province.terrain_type_id].penalty;
101 this->days_left_until_move--;
102 if(this->days_left_until_move <= 0) {
106 if(!this->path.empty()) {
108 this->path.pop_back();
123 units[index].cached_id = index;
130 if(
static_cast<size_t>(unit_current_province) >=
province_units.size())
131 province_units.resize(
static_cast<size_t>(unit_current_province) + 1);
148 assert(std::find(p.begin(), p.end(), unit_id) == p.end());
152 assert(
units[unit_id].can_move());
162 assert(std::find(p1.begin(), p1.end(), unit_id) == p1.end());
165 assert(std::find(p2.begin(), p2.end(), unit_id) == p2.end());
174 for(
const auto unit_id : unit_ids) {
175 const auto& unit = this->
units[unit_id];
176 assert(unit.province_id() ==
id);
190 auto start_id = world.unit_manager.get_unit_current_province(this->
get_id());
191 if(start_id == target.
get_id())
193 this->path = Eng3D::Pathfind::get_path<ProvinceId>(start_id, target,
196 const auto& province = world.provinces[
province_id];
202 const auto& province1 = world.provinces[province1_id];
203 const auto& province2 = world.provinces[province2_id];
204 const glm::vec2 world_size{ world.width, world.height };
205 constexpr
auto radius = 100.f;
208 float cos_angle = glm::dot(sphere_coord1, sphere_coord2) / (radius * radius);
209 float angle = glm::acos(cos_angle);
210 float distance = angle / (2 * glm::pi<float>());
214 assert(this->path.size() >= 2);
215 assert(this->path.back() == start_id);
216 this->path.pop_back();
218 this->
set_target(world.provinces[this->path.back()]);
219 this->path.pop_back();
224 return (this->
size * (type.attack + type.defense)) / 1000.f;
void broadcast(const Eng3D::Networking::Packet &packet)
This will broadcast the given packet to all clients currently on the server.
A single province, which is used to simulate economy in a "bulk-tiles" way instead of doing economica...
std::vector< ProvinceId > neighbour_ids
float euclidean_distance(const Province &other_province, glm::vec2 world_size, float radius) const
Roughly a batallion, consisting of approximately 500 soldiers each.
float days_to_move_to(const Province &province) const
void set_owner(const Nation &nation)
void set_target(const Province &province)
bool can_move() const
Checks if the unit can move (if it can set_province)
float get_strength() const
ProvinceId province_id() const
bool has_target_province() const
float attack(Unit &enemy)
glm::vec2 get_pos() const
void set_path(const Province &target)
bool update_movement(UnitManager &unit_manager)
void add_unit(Unit unit, ProvinceId unit_current_province)
Eng3D::Freelist< Unit > units
std::vector< std::vector< UnitId > > province_units
std::vector< ProvinceId > unit_province
void init(World &world)
Fill in the relationship vectors for each nation.
void move_unit(UnitId unit, ProvinceId target_province)
void remove_unit(UnitId unit)
static World & get_instance()
void debug(const std::string_view category, const std::string_view msg)
std::string string_format(const std::string_view format, Args &&... args)
String formatter.
void fast_erase(C &c, T value) noexcept
glm::vec3 get_sphere_coord(glm::vec2 size, glm::vec2 pos, float radius)
static Eng3D::Networking::Packet form_packet(const Unit &unit)
static Eng3D::Networking::Packet form_packet(const Unit &unit, const Province &province)
static Eng3D::Networking::Packet form_packet(const Unit &unit)
void remove(size_t index)
constexpr Id get_id() const
Id cached_id
Id used to speed up Id lookups on any context.
std::size_t operator()(const ProvinceId &id) const noexcept