-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Map.c
75 lines (61 loc) · 2.22 KB
/
Map.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Copyright DarkNeutrino 2021
#include "../Extern/libmapvxl/libmapvxl.h"
#include "Structs.h"
#include "Util/Compress.h"
#include "Util/DataStream.h"
#include "Util/Queue.h"
#include "Util/Types.h"
#include <stdio.h>
uint8 LoadMap(Server* server, const char* path)
{
LOG_STATUS("Loading map");
while (server->map.compressedMap) {
server->map.compressedMap = Pop(server->map.compressedMap);
}
FILE* file = fopen(path, "rb");
if (!file) {
perror("MAP NOT FOUND");
exit(EXIT_FAILURE);
}
fseek(file, 0L, SEEK_END);
server->map.mapSize = ftell(file);
fseek(file, 0, SEEK_SET);
size_t maxMapSize = MAP_MAX_X * MAP_MAX_Y * (MAP_MAX_Z / 2) * 8;
if (server->map.mapSize > maxMapSize) {
fclose(file);
LOG_ERROR("Map file %s.vxl is larger then maximum VXL size of X: %d, Y: %d, Z: %d. Please set the correct map "
"size in libmapvxl",
server->mapName,
MAP_MAX_X,
MAP_MAX_Y,
MAP_MAX_Z);
server->running = 0;
return 0;
}
uint8* buffer = (uint8*) calloc(server->map.mapSize, sizeof(uint8));
// The biggest possible VXL size given the XYZ size
uint8* mapOut = (uint8*) calloc(MAP_MAX_X * MAP_MAX_Y * (MAP_MAX_Z / 2), sizeof(uint8));
if (fread(buffer, server->map.mapSize, 1, file) < server->map.mapSize) {
LOG_STATUS("Finished loading map");
}
fclose(file);
mapvxlLoadVXL(&server->map.map, buffer);
free(buffer);
LOG_STATUS("Compressing map data");
// Write map to mapOut
server->map.mapSize = mapvxlWriteMap(&server->map.map, mapOut);
// Resize the map to the exact VXL memory size for given XYZ coordinate size
mapOut = (uint8*) realloc(mapOut, server->map.mapSize);
server->map.compressedMap = CompressData(mapOut, server->map.mapSize, DEFAULT_COMPRESSOR_CHUNK_SIZE);
free(mapOut);
Queue* node = server->map.compressedMap;
server->map.compressedSize = 0;
while (node) {
server->map.compressedSize += node->length;
node = node->next;
}
while (server->map.compressedMap) {
server->map.compressedMap = Pop(server->map.compressedMap);
}
return 1;
}