VOX
A little voxel engine
Loading...
Searching...
No Matches
ServerWorld.hpp
Go to the documentation of this file.
1#pragma once
2
3
4#include "Server.hpp"
5#include "Packets.hpp"
6#include "World.hpp"
7#include "server_define.hpp"
8#include "logger.hpp"
9#include <unordered_set>
10#include <set>
11#include "tasks.hpp"
12#include "Executor.hpp"
13#include "TaskGraph.hpp"
14
15class ServerWorld : public World
16{
17public:
18 ServerWorld(Server & server);
20
21 ServerWorld(ServerWorld & other) = delete;
22 ServerWorld(ServerWorld && other) = delete;
23 ServerWorld & operator=(ServerWorld & other) = delete;
24 ServerWorld & operator=(ServerWorld && other) = delete;
25
27 {
28 std::vector<std::shared_ptr<Chunk>> chunks_to_load;
29 std::vector<glm::ivec3> chunks_to_unload;
30 };
31
32 // std::shared_ptr<Chunk> getAndLoadChunk(const glm::ivec3 & chunk_position);
33
34
35 void update();
36
37 /*********************************\
38 * BLOCKS
39 \*********************************/
41 {
42 enum class Type
43 {
44 PLACE,
45 DESTROY,
46 UPDATE,
47 RANDOM
48 };
50 glm::ivec3 position;
52
53 };
54 void addBlockUpdate(const BlockUpdateData & data);
55 void updateBlocks();
56 // void loadChunk(const glm::ivec3 & chunk_position);
57
58 void placeBlock(const glm::vec3 & position, BlockInfo::Type block);
59 void setBlock(const glm::vec3 & position, BlockInfo::Type block);
60
61 // ChunkLoadUnloadData getChunksToUnload(
62 // const glm::vec3 & old_player_position,
63 // const glm::vec3 & new_player_position
64 // );
65
66
67 /*********************************\
68 * TICKET MANAGER
69 \*********************************/
70 void setPlayerTicketLevel(const int & level);
71
72 int getLoadDistance() const;
73 struct Ticket
74 {
75 bool operator==(const Ticket & other) const = default;
76 enum class Type
77 {
78 PLAYER,
79 OTHER
81 int level;
82 glm::ivec3 position;
83 struct hash
84 {
85 std::size_t operator()(const ServerWorld::Ticket & ticket) const
86 {
87 return std::hash<int>()(ticket.level) ^ std::hash<glm::ivec3>()(ticket.position);
88 }
89 };
90
91 std::size_t hash() const
92 {
93 return std::hash<int>()(level) ^ std::hash<glm::ivec3>()(position);
94 }
95 };
96 typedef std::unordered_multimap<uint64_t, Ticket> TicketMultiMap;
97
103 const std::unordered_set<glm::ivec3> & getBlockUpdateChunks() const;
104
110 const std::unordered_set<glm::ivec3> & getEntityUpdateChunks() const;
111
117 const TicketMultiMap & getTickets() const;
118
129 uint64_t addTicket(const Ticket & ticket);
130
136 void removeTicket(const uint64_t & ticket_id);
137
149 uint64_t changeTicket(const uint64_t & old_ticket_id, const Ticket & new_ticket);
150
151 /***********************************\
152 * NETWORK
153 \***********************************/
154 void handlePacket(std::shared_ptr<IPacket> packet);
155
156
157private:
158 // typedef std::unordered_set<glm::ivec3> ChunkGenList;
159 /*********************************\
160 * NETWORK
161 \*********************************/
162 Server & m_server;
163 task::Executor & m_executor;
164 std::unordered_map<uint64_t, uint64_t> m_player_to_connection_id;
165 std::unordered_map<uint64_t, uint64_t> m_connection_to_player_id;
166 TracyLockableN(std::mutex, m_players_info_mutex, "PlayerInfoMutex");
167
168 void handleBlockActionPacket(std::shared_ptr<BlockActionPacket> packet);
169 void handleConnectionPacket(std::shared_ptr<ConnectionPacket> packet);
170 void handlePlayerMovePacket(std::shared_ptr<PlayerMovePacket> packet);
171 void handleDisconnectPacket(std::shared_ptr<DisconnectPacket> packet);
172 void handleLoadDistancePacket(std::shared_ptr<LoadDistancePacket> packet);
173 void handlePingPacket(std::shared_ptr<PingPacket> packet);
174 void sendChunkLoadUnloadData(const ChunkLoadUnloadData & data, uint64_t player_id);
175
176 /*********************************\
177 * TICKET MANAGER
178 \*********************************/
179 // std::vector<Ticket> m_active_tickets;
180 // std::vector<Ticket> m_tickets_to_add;
181 // std::vector<Ticket> m_tickets_to_remove;
182
183 constexpr static int TICKET_LEVEL_ENTITY_UPDATE = 31;
184 constexpr static int TICKET_LEVEL_BLOCK_UPDATE = 32;
185 constexpr static int TICKET_LEVEL_BORDER = 33;
186 constexpr static int TICKET_LEVEL_INACTIVE = 34;
187
188 constexpr static int SPAWN_TICKET_LEVEL = 34;
189 constexpr static int DEFAULT_PLAYER_TICKET_LEVEL = 31;
190 int m_player_ticket_level = DEFAULT_PLAYER_TICKET_LEVEL;
191
192
193
194 TicketMultiMap m_active_tickets;
195 TicketMultiMap m_tickets_to_add;
196 std::unordered_multiset<uint64_t> m_tickets_to_remove;
197
198 std::unordered_set<glm::ivec3> m_block_update_chunks;
199 std::unordered_set<glm::ivec3> m_entity_update_chunks;
200 std::unordered_set<glm::ivec3> m_border_chunks;
201 std::unordered_set<glm::ivec2> m_regions_to_unload;
202
203 mutable TracyLockableN(std::mutex, m_tickets_mutex, "TicketManager");
204
205 /*************************\
206 * METHODS
207 \*************************/
208
218 void updateTickets();
219
228 void floodFill(const TicketMultiMap & tickets, WorldGenerator::ChunkGenList & chunk_gen_list);
229
231 void clearChunksLoadLevel();
232
233
246 bool applyTicketToChunk(const Ticket & ticket, WorldGenerator::ChunkGenList & chunk_gen_list);
247 /*********************************\
248 * BLOCKS
249 \*********************************/
250 std::queue<BlockUpdateData> m_block_updates;
251 mutable TracyLockableN(std::mutex, m_block_updates_mutex, "BlockUpdateQueue");
252
253
254 void doChunkGens(WorldGenerator::ChunkGenList & chunks_to_gen);
255
256 void unloadEmptyRegions();
257
267 uint64_t asyncGenChunk(const glm::ivec3 & chunk_position, Chunk::genLevel gen_level, Chunk::genLevel current_level);
268
269 //LIGHTS
270 std::queue<glm::ivec3> m_block_light_update;
271 TracyLockableN (std::mutex, m_block_light_update_mutex, "Block light update");
272
273 void updateLights();
274
275
276 ChunkLoadUnloadData updateChunkObservations(uint64_t player_id, const int & old_load_distance);
277 void removeAllPlayerObservations(std::shared_ptr<Player> player);
278 void removePlayerObservation(uint64_t player_id, std::shared_ptr<Chunk> chunk);
279
280 /*********************************\
281 * MISCELLANEOUS
282 \*********************************/
283 std::unordered_map<uint64_t, glm::dvec3> m_last_tick_player_positions;
284 std::unordered_map<uint64_t, glm::dvec3> m_current_tick_player_positions;
285
286 struct chunkGenData
287 {
288 std::shared_ptr<task::TaskGraph> graph;
289 std::future<void> future;
290 TracyLockableN(std::mutex, m_chunk_gen_data_mutex, "ChunkFuturesIds");
291 };
292 // std::vector<std::future<void>> m_chunk_futures;
293 chunkGenData m_chunk_gen_data;
294
295
296
297 // ChunkMap getChunkZone(glm::ivec3 zoneStart, glm::ivec3 zoneSize);
298
299
300
301
302 void savePlayerPositions();
303 void updatePlayerPositions();
304 void waitForChunkFutures();
305
306 struct tickedUpdates
307 {
308 int player_ticket_level = DEFAULT_PLAYER_TICKET_LEVEL;
309 bool player_ticket_level_changed = false;
310 } m_ticked_updates;
311
312
317 void updateTickedValues();
318};
319
320
321 namespace std
322{
323 template<>
324 struct hash<ServerWorld::Ticket>
325 {
326 std::size_t operator()(const ServerWorld::Ticket & ticket) const
327 {
328 return std::hash<int>()(ticket.level) ^ std::hash<glm::ivec3>()(ticket.position);
329 }
330 };
331}
Type
Definition: Block.hpp:33
genLevel
Definition: Chunk.hpp:30
Definition: ServerWorld.hpp:16
void removeTicket(const uint64_t &ticket_id)
Remove a ticket from the ticket manager.
Definition: ServerWorldTickets.cpp:27
int getLoadDistance() const
Definition: ServerWorld.cpp:124
void setBlock(const glm::vec3 &position, BlockInfo::Type block)
Definition: ServerWorldBlocks.cpp:83
const std::unordered_set< glm::ivec3 > & getEntityUpdateChunks() const
Get the list of Chunks that need to get an entity update tick.
Definition: ServerWorldTickets.cpp:8
ServerWorld & operator=(ServerWorld &&other)=delete
ServerWorld(ServerWorld &other)=delete
std::unordered_multimap< uint64_t, Ticket > TicketMultiMap
Definition: ServerWorld.hpp:96
void update()
Definition: ServerWorld.cpp:30
void handlePacket(std::shared_ptr< IPacket > packet)
Definition: ServerWorldNetwork.cpp:3
uint64_t addTicket(const Ticket &ticket)
Add a ticket to the ticket manager.
Definition: ServerWorldTickets.cpp:19
ServerWorld & operator=(ServerWorld &other)=delete
void addBlockUpdate(const BlockUpdateData &data)
Definition: ServerWorldBlocks.cpp:39
void placeBlock(const glm::vec3 &position, BlockInfo::Type block)
Definition: ServerWorldBlocks.cpp:58
const TicketMultiMap & getTickets() const
Get the list of Tickets ( May contains duplicates )
Definition: ServerWorldTickets.cpp:13
void updateBlocks()
Definition: ServerWorldBlocks.cpp:3
~ServerWorld()
Definition: ServerWorld.cpp:26
void setPlayerTicketLevel(const int &level)
Definition: ServerWorldTickets.cpp:41
ServerWorld(ServerWorld &&other)=delete
const std::unordered_set< glm::ivec3 > & getBlockUpdateChunks() const
Get the list of Chunks that need to get a block update tick.
Definition: ServerWorldTickets.cpp:3
uint64_t changeTicket(const uint64_t &old_ticket_id, const Ticket &new_ticket)
Shortcut to remove a ticket and add a new one.
Definition: ServerWorldTickets.cpp:33
The server side interface with the network module.
Definition: Server.hpp:33
Definition: World.hpp:24
Definition: Executor.hpp:21
Definition: ObjLoader.hpp:65
Definition: ServerWorld.hpp:41
glm::ivec3 position
Definition: ServerWorld.hpp:50
Type
Definition: ServerWorld.hpp:43
Type type
Definition: ServerWorld.hpp:49
BlockInfo::Type block
Definition: ServerWorld.hpp:51
Definition: ServerWorld.hpp:27
std::vector< std::shared_ptr< Chunk > > chunks_to_load
Definition: ServerWorld.hpp:28
std::vector< glm::ivec3 > chunks_to_unload
Definition: ServerWorld.hpp:29
Definition: ServerWorld.hpp:84
std::size_t operator()(const ServerWorld::Ticket &ticket) const
Definition: ServerWorld.hpp:85
Definition: ServerWorld.hpp:74
enum ServerWorld::Ticket::Type type
std::size_t hash() const
Definition: ServerWorld.hpp:91
bool operator==(const Ticket &other) const =default
glm::ivec3 position
Definition: ServerWorld.hpp:82
Type
Definition: ServerWorld.hpp:77
int level
Definition: ServerWorld.hpp:81
std::size_t operator()(const ServerWorld::Ticket &ticket) const
Definition: ServerWorld.hpp:326