-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
MapStart.c
36 lines (34 loc) · 1.51 KB
/
MapStart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <Server/Server.h>
#include <Util/Compress.h>
#include <Util/Log.h>
void send_map_start(server_t* server, player_t* player)
{
if (server->protocol.num_players == 0) {
return;
}
LOG_INFO("Sending map info to %s (#%hhu)", player->name, player->id);
ENetPacket* packet = enet_packet_create(NULL, 5, ENET_PACKET_FLAG_RELIABLE);
stream_t stream = {packet->data, packet->dataLength, 0};
stream_write_u8(&stream, PACKET_TYPE_MAP_START);
stream_write_u32(&stream, server->s_map.compressed_size);
if (enet_peer_send(player->peer, 0, packet) == 0) {
player->state = STATE_LOADING_CHUNKS;
// The biggest possible VXL size given the XYZ size
uint8_t* map = (uint8_t*) calloc(
server->s_map.map.size_x * server->s_map.map.size_y * (server->s_map.map.size_z / 2), sizeof(uint8_t));
// Write map to out
server->s_map.map_size = mapvxl_write(&server->s_map.map, map);
// Resize the map to the exact VXL memory size for given XYZ coordinate size
uint8_t* old_map;
old_map = (uint8_t*) realloc(map, server->s_map.map_size);
if (!old_map) {
free(map);
return;
}
map = old_map;
server->s_map.compressed_map = compress_queue(map, server->s_map.map_size, DEFAULT_COMPRESS_CHUNK_SIZE);
player->queues = server->s_map.compressed_map;
free(map);
LOG_INFO("Sending map chunks to %s (#%hhu)", player->name, player->id);
}
}