Argentum Online - Servidor
map.h
1 #ifndef __MAP_H
2 #define __MAP_H
3 
4 #include <cstdint>
5 #include <queue>
6 #include <unordered_map>
7 #include <unordered_set>
8 #include <vector>
9 
10 #include "../../include/nlohmann/json.hpp"
11 #include "../../include/types.h"
12 #include "actions/action.h"
13 #include "entities/entity.h"
14 #include "entities/entity_factory.h"
15 #include "items/item.h"
16 #include "map_log_factory.h"
17 #include "map_transitions.h"
18 #include "monster_spawner.h"
19 #include "position.h"
20 
21 #define MAP_SIZE 50
22 
23 class Player;
24 
25 typedef struct entity_action {
26  EntityId entity;
27  Action* action;
29 
30 template <typename T>
31 using ObjectMatrix =
32  std::unordered_map<position_t, T, PositionHasher, PositionComparator>;
33 
34 // clave: id de entidad, valor: position_t
35 typedef std::unordered_map<EntityId, position_t> PositionMap;
36 
37 // Clave: position_t, valor: puntero a una entidad
38 typedef ObjectMatrix<Entity*> EntityMatrix;
39 
40 // Clave: position_t. Contiene los bloques colisionables del mapa
41 typedef std::unordered_set<position_t, PositionHasher, PositionComparator>
42  PositionSet;
43 
44 // Clave: position_t, valor: puntero a un item
45 typedef ObjectMatrix<Item*> LootMatrix;
46 
47 class Map {
48  private:
49  friend class Action;
50  friend class MonsterSpawner;
51  int width;
52  int height;
53  bool _is_safe;
54  std::string map_name;
55 
56  position_t healer_pos;
57  PositionMap position_map;
58  bool _dirty_entities;
59  EntityMatrix entity_matrix;
60  bool _dirty_loot;
61  LootMatrix loot_matrix;
62 
63  PositionSet collision_map;
64 
65  MapTransitions transitions;
66  EntityFactory entity_factory;
67  MonsterSpawner monster_spawner;
68 
69  std::queue<entity_action_t> actions;
70  std::queue<map_log_t> update_logs;
71 
72  // Para mandarsela a los clientes.
73  nlohmann::json visual_map_info;
74 
83  template <typename T>
84  position_t get_nearest_free_position(ObjectMatrix<T> object_matrix,
86 
96  template <typename T>
97  bool collides(ObjectMatrix<T> object_matrix, position_t position);
98 
106  void add_entity(Entity* entity, position_t position);
107 
113  void rm_entity(EntityId entity_id);
114 
115  public:
116  Map(MapId map_id, const std::string& map_name, bool is_safe,
117  const nlohmann::json& map_description, const nlohmann::json& map_mobs,
118  const nlohmann::json& map_transitions);
119  ~Map();
120 
121  Map(const Map& other) = delete;
122 
123  const std::string& get_name();
124 
132  void move(EntityId entity_id, position_t steps);
133 
139  std::queue<map_change_t>& get_transitions();
140 
147  void teleport(EntityId player_id, position_t dest);
148 
156  nlohmann::json add_player(nlohmann::json player_info);
157 
165  nlohmann::json rm_player(EntityId entity_id);
166 
167  position_t get_position(EntityId entity_id);
168 
169  position_t get_healer_pos() const;
170 
171  bool is_safe() const;
172 
183  unsigned int distance,
184  entity_type_t entity_type);
185 
192  bool entity_exists(EntityId entity_id);
193 
200  void update(uint64_t delta_t);
201 
210  void push_log(const map_log_t& log);
211 
221  void push_action(EntityId entity_id, Action* action);
222 
229  void drop_loot(EntityId entity_id, Item* item);
230 
237  void drop_loot(EntityId entity_id, const std::vector<Item*>& items);
238 
246  Item* pickup_loot(EntityId entity_id);
247 
254  static nlohmann::json get_position_data(const PositionMap& position_map);
260  const PositionMap get_position_map() const;
261 
269  bool dirty_entities() const;
275  nlohmann::json get_entity_data();
276 
284  bool dirty_loot() const;
291  nlohmann::json get_loot_data();
292 
298  std::queue<map_log_t>& get_update_logs();
299 
305  nlohmann::json get_map_data();
306 };
307 
308 #endif
Item
Definition: item.h:45
Map::update
void update(uint64_t delta_t)
Actualiza todas las entidades que contiene segun el delta_t transcurrido.
Definition: map.cpp:191
Map::get_loot_data
nlohmann::json get_loot_data()
Obtiene la información de los items del mapa.
Definition: map.cpp:246
Map::get_nearest_entity_pos
position_t get_nearest_entity_pos(position_t entity_pos, unsigned int distance, entity_type_t entity_type)
Obtener la posición de la entidad de un tipo más cercana.
Definition: map.cpp:261
Map::get_position_map
const PositionMap get_position_map() const
Obtiene una copia del mapa de posiciones de las entidades.
Definition: map.cpp:226
map_log
Definition: map_log_factory.h:17
Action
Definition: action.h:9
Map::get_position_data
static nlohmann::json get_position_data(const PositionMap &position_map)
Realizar una copia en forma de json de un mapa de posiciones.
Definition: map.cpp:217
Map::move
void move(EntityId entity_id, position_t steps)
Mueve la entidad asociada al entity_id hacia la posicion relativa steps.
Definition: map.cpp:109
Map::dirty_loot
bool dirty_loot() const
Verificar si hubo un cambio en los items caidos en el mapa desde la última vez que se utilizó este mé...
Definition: map.cpp:243
MonsterSpawner
Clase que mantiene los "spawns" de monstruos en un mapa.
Definition: monster_spawner.h:20
Map::get_transitions
std::queue< map_change_t > & get_transitions()
Obtiene la cola de transiciones del mapa.
Definition: map.cpp:133
Map::get_map_data
nlohmann::json get_map_data()
Obtiene la información gráfica del mapa en forma de json.
Definition: map.cpp:257
Map::pickup_loot
Item * pickup_loot(EntityId entity_id)
Intenta obtener de la posición donde se encuentra la entidad el item que haya allí.
Definition: map.cpp:304
Map::teleport
void teleport(EntityId player_id, position_t dest)
Teletransporta a un jugador a través de una transición.
Definition: map.cpp:137
Map::push_log
void push_log(const map_log_t &log)
Agrega un log al mapa.
Definition: map.cpp:205
EntityFactory
Definition: entity_factory.h:10
Entity
Definition: entity.h:15
Map::rm_player
nlohmann::json rm_player(EntityId entity_id)
Eliminar a un jugador identificado por su id del mapa.
Definition: map.cpp:156
Map::get_entity_data
nlohmann::json get_entity_data()
Obtiene la información de las entidades del mapa.
Definition: map.cpp:233
Map
Definition: map.h:47
MapTransitions
Clase que se encarga de manejar las transiciones en cada mapa.
Definition: map_transitions.h:27
Map::add_player
nlohmann::json add_player(nlohmann::json player_info)
Agrega un jugador al mapa a partir de su información.
Definition: map.cpp:146
Map::get_update_logs
std::queue< map_log_t > & get_update_logs()
Obtiene la cola de "logs" del mapa.
Definition: map.cpp:213
Player
Definition: player.h:14
position
Definition: position.h:8
Map::dirty_entities
bool dirty_entities() const
Verificar si hubo un cambio en las entidades del mapa desde la última vez que se utilizó este método.
Definition: map.cpp:230
entity_action
Definition: map.h:25
Map::entity_exists
bool entity_exists(EntityId entity_id)
Verificar si una entidad existe o no.
Definition: map.cpp:175
Map::push_action
void push_action(EntityId entity_id, Action *action)
Ejecuta sobre la entidad asociada al id la accion.
Definition: map.cpp:209
Map::drop_loot
void drop_loot(EntityId entity_id, Item *item)
Deja el item pasado por parámetro en la posición de la entidad.
Definition: map.cpp:293