-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.c
208 lines (185 loc) · 4.85 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include "map.h"
#include "logging.h"
#include <stdio.h>
#define DEFAULT_BIOME 1
Chunk * Chunk_new_empty(){
logmsg(LOG_DEBUG, "Making new empty chunk.");
Chunk *chunk = malloc(sizeof(Chunk));
Block airblock;
airblock.id = 0;
airblock.metadata = 0;
int i;
for (i=0; i < 16*16*256; i++){
chunk->blocks[i] = airblock;
}
for(i=0; i < 16*16; i++){
chunk->biome_data[i] = DEFAULT_BIOME;
}
logmsg(LOG_DEBUG, "Finishing making chunk.");
return chunk;
}
void Chunk_set_block(Chunk *chunk, int x, int y, int z, Block block){
size_t blockpos = y * 256 + z * 16 + x;
chunk->blocks[blockpos] = block;
}
Block Chunk_get_block(Chunk *chunk, int x, int y, int z){
size_t blockpos = y * 256 + z * 16 + x;
return chunk->blocks[blockpos];
}
void Chunk_set_below(Chunk *chunk, Block b, int y){
int x;
int z;
int level;
for (x=0; x < 16; x++){
for (z=0; z<16; z++){
for (level=0; level<y; level++){
Chunk_set_block(chunk, x, level, z, b);
}
}
}
}
// Checks to see if a chunk is within the map's area.
char Map_validate_chunk_coords(Map *map, ssize_t x, ssize_t z){
if (x > map->xchunks-1 || z > map->zchunks-1 || x < 0 || z < 0){
return 0;
}
return 1;
}
Map * Map_new_empty(ssize_t x, ssize_t z){
logmsg(LOG_DEBUG, "Generating new empty map.");
Map *map = malloc(sizeof(Map));
map->chunks = malloc(sizeof(void *) * x * z);
map->xchunks = x;
map->zchunks = z;
int i;
for (i=0; i < x * z; i++){
map->chunks[i] = NULL;
}
return map;
}
Map * Map_new_air(ssize_t x, ssize_t z){
Map *map = Map_new_empty(x, z);
int i;
int j;
for (i=0; i < x; i++){
for (j=0; j < z; j++){
Chunk *c = Chunk_new_empty();
Map_set_chunk(map, c, i, j);
}
}
return map;
}
Chunk * Map_get_chunk(Map *map, ssize_t x, ssize_t z){
if (!Map_validate_chunk_coords(map, x, z)){
logmsg(LOG_DEBUG, "Tried to get invalid chunk!");
return NULL;
}
return map->chunks[(x * map->xchunks) + z];
}
void Map_set_chunk(Map *map, Chunk *chunk, ssize_t x, ssize_t z){
if (!Map_validate_chunk_coords(map, x, z)){
logmsg(LOG_WARN, "Tried to set an invalid chunk position!");
return;
}
map->chunks[(x * map->xchunks) + z] = chunk;
}
void Map_set_block(Map *map, Block block, int x, int y, int z){
ssize_t chunkx = x / 16;
ssize_t chunkz = z / 16;
Chunk *chunk = Map_get_chunk(map, chunkx, chunkz);
if (chunk == NULL){
logmsg(LOG_WARN, "Tried to set block that is outside of map!");
return;
}
Chunk_set_block(chunk, x%16, y, z%16, block);
}
Block Map_get_block(Map *map, int x, int y, int z){
ssize_t chunkx = x / 16;
ssize_t chunkz = z / 16;
Chunk *chunk = Map_get_chunk(map, chunkx, chunkz);
if (chunk == NULL){
logmsg(LOG_WARN, "Tried to get block that is outside of map!");
return (Block) {.id = 0, .metadata = 0};
}
return Chunk_get_block(chunk, x%16, y, z%16);
}
void Map_set_below(Map *map, Block b, int level){
int x = map->xchunks;
int z = map->zchunks;
int i;
int j;
for (i=0; i < x; i++){
for (j=0; j < z; j++){
Chunk_set_below(Map_get_chunk(map, i, j), b, level);
}
}
}
void Map_write(Map *map, char *fname){
logfmt(LOG_INFO, "Writing map: %s", fname);
FILE *f = fopen(fname, "wb");
int xsize = map->xchunks;
int zsize = map->zchunks;
fwrite(&xsize, sizeof(int), 1, f);
fwrite(&zsize, sizeof(int), 1, f);
for (int xchunk = 0; xchunk < xsize; xchunk++){
for (int zchunk = 0; zchunk < zsize; zchunk++){
Chunk *chunk = Map_get_chunk(map, xchunk, zchunk);
for (int blockpos = 0; blockpos < 16*16*256; blockpos++){
Block b = chunk->blocks[blockpos];
char id = b.id;
char meta = b.metadata;
fwrite(&id, sizeof(char), 1, f);
fwrite(&meta, sizeof(char), 1, f);
}
}
}
fclose(f);
}
Map * Map_read(char *fname){
logfmt(LOG_INFO, "Reading map: %s", fname);
FILE *f = fopen(fname, "rb");
int xsize;
int zsize;
fread(&xsize, sizeof(int), 1, f);
fread(&zsize, sizeof(int), 1, f);
logfmt(LOG_DEBUG, "Map is of size %d %d", xsize, zsize);
Map *map = Map_new_empty(xsize, zsize);
for (int xchunk = 0; xchunk < xsize; xchunk++){
for (int zchunk = 0; zchunk < zsize; zchunk++){
Chunk *chunk = Chunk_new_empty();
Map_set_chunk(map, chunk, xchunk, zchunk);
for (int blockpos = 0; blockpos < 16*16*256; blockpos++){
char id;
char meta;
fread(&id, sizeof(char), 1, f);
fread(&meta, sizeof(char), 1, f);
Block b = {.id = id, .metadata = meta};
chunk->blocks[blockpos] = b;
}
}
}
fclose(f);
return map;
}
void apply_face(char face, int *x, int *y, int *z){
switch (face){
case 0:
*y -= 1;
break;
case 1:
*y += 1;
break;
case 2:
*z -= 1;
break;
case 3:
*z += 1;
break;
case 4:
*x -= 1;
break;
case 5:
*x += 1;
break;
}
}