-
Notifications
You must be signed in to change notification settings - Fork 10
/
bmap_server.c
374 lines (307 loc) · 9.61 KB
/
bmap_server.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
* bmap_server.c
* XBolo
*
* Created by Robert Chrzanowski on 10/11/09.
* Copyright 2009 Robert Chrzanowski. All rights reserved.
*
*/
#include "bmap_server.h"
#include "server.h"
#include "terrain.h"
#include "errchk.h"
#include <stdlib.h>
#include <string.h>
/* server functions */
static ssize_t serverloadmapsize();
int serverloadmap(const void *buf, size_t nbytes) {
int i, x, y;
const struct BMAP_Preamble *preamble;
const struct BMAP_PillInfo *pillInfos;
const struct BMAP_BaseInfo *baseInfos;
const struct BMAP_StartInfo *startInfos;
const void *runData;
int runDataLen;
int offset;
TRY
/* wipe the map clean */
for (y = 0; y < WIDTH; y++) {
for (x = 0; x < WIDTH; x++) {
server.terrain[y][x] = defaultterrain(x, y);
}
}
if (nbytes < sizeof(struct BMAP_Preamble)) LOGFAIL(ECORFILE)
preamble = buf;
if (strncmp((char *)preamble->ident, MAP_FILE_IDENT, MAP_FILE_IDENT_LEN) != 0)
LOGFAIL(ECORFILE)
if (preamble->version != CURRENT_MAP_VERSION) LOGFAIL(EINCMPAT)
if (preamble->npills > MAXPILLS) LOGFAIL(ECORFILE)
if (preamble->nbases > MAXBASES) LOGFAIL(ECORFILE)
if (preamble->nstarts > MAX_STARTS) LOGFAIL(ECORFILE)
if (nbytes <
sizeof(struct BMAP_Preamble) +
preamble->npills*sizeof(struct BMAP_PillInfo) +
preamble->nbases*sizeof(struct BMAP_BaseInfo) +
preamble->nstarts*sizeof(struct BMAP_StartInfo))
LOGFAIL(ECORFILE)
pillInfos = (struct BMAP_PillInfo *)(preamble + 1);
baseInfos = (struct BMAP_BaseInfo *)(pillInfos + preamble->npills);
startInfos = (struct BMAP_StartInfo *)(baseInfos + preamble->nbases);
runData = (void *)(startInfos + preamble->nstarts);
runDataLen =
nbytes - (sizeof(struct BMAP_Preamble) +
preamble->npills*sizeof(struct BMAP_PillInfo) +
preamble->nbases*sizeof(struct BMAP_BaseInfo) +
preamble->nstarts*sizeof(struct BMAP_StartInfo));
server.npills = preamble->npills;
server.nbases = preamble->nbases;
server.nstarts = preamble->nstarts;
/* load the pillinfo */
for (i = 0; i < preamble->npills; i++) {
server.pills[i].x = pillInfos[i].x;
server.pills[i].y = pillInfos[i].y;
server.pills[i].owner = NEUTRAL; /* ignore pill owner */
server.pills[i].armour = pillInfos[i].armour;
server.pills[i].speed = (pillInfos[i].speed*MAXTICKSPERSHOT)/50;
if (server.pills[i].speed > MAXTICKSPERSHOT) {
server.pills[i].speed = MAXTICKSPERSHOT;
}
server.pills[i].counter = 0;
}
/* load the baseinfo */
for (i = 0; i < preamble->nbases; i++) {
server.bases[i].x = baseInfos[i].x;
server.bases[i].y = baseInfos[i].y;
server.bases[i].owner = NEUTRAL; /* ignore base owner */
server.bases[i].armour = baseInfos[i].armour;
server.bases[i].shells = baseInfos[i].shells;
server.bases[i].mines = baseInfos[i].mines;
server.bases[i].counter = 0;
}
/* load the startinfo */
for (i = 0; i < preamble->nstarts; i++) {
server.starts[i].x = startInfos[i].x;
server.starts[i].y = startInfos[i].y;
server.starts[i].dir = startInfos[i].dir;
}
offset = 0;
for (;;) { /* write runs */
struct BMAP_Run run;
if (offset + sizeof(struct BMAP_Run) > runDataLen) {
break; /* ran out of bytes */
// LOGFAIL(ECORFILE)
}
run = *(struct BMAP_Run *)(runData + offset);
/* if last run */
if (run.datalen == 4 && run.y == 0xff && run.startx == 0xff && run.endx == 0xff) {
if (offset + run.datalen != runDataLen) {
/* left over bytes extra game data??? ignore for now */
}
break;
}
if (offset + run.datalen > runDataLen) LOGFAIL(ECORFILE)
if (writerun(run, runData + offset + sizeof(struct BMAP_Run), server.terrain) == -1) LOGFAIL(errno)
offset += run.datalen;
}
/* clear all starting locations */
for (i = 0; i < server.nstarts; i++) {
server.terrain[server.starts[i].y][server.starts[i].x] = kSeaTerrain;
}
for (i = 0; i < server.npills; i++) {
switch (server.terrain[server.pills[i].y][server.pills[i].x]) {
case kSeaTerrain:
case kBoatTerrain:
case kWallTerrain:
case kRiverTerrain:
case kForestTerrain:
case kDamagedWallTerrain0:
case kDamagedWallTerrain1:
case kDamagedWallTerrain2:
case kDamagedWallTerrain3:
case kMinedSeaTerrain:
case kMinedForestTerrain:
server.terrain[server.pills[i].y][server.pills[i].x] = kGrassTerrain0;
break;
case kMinedSwampTerrain:
server.terrain[server.pills[i].y][server.pills[i].x] = kSwampTerrain0;
break;
case kMinedCraterTerrain:
server.terrain[server.pills[i].y][server.pills[i].x] = kCraterTerrain;
break;
case kMinedRoadTerrain:
server.terrain[server.pills[i].y][server.pills[i].x] = kRoadTerrain;
break;
case kMinedRubbleTerrain:
server.terrain[server.pills[i].y][server.pills[i].x] = kRubbleTerrain0;
case kMinedGrassTerrain:
server.terrain[server.pills[i].y][server.pills[i].x] = kGrassTerrain0;
break;
case kSwampTerrain0:
case kSwampTerrain1:
case kSwampTerrain2:
case kSwampTerrain3:
case kCraterTerrain:
case kRoadTerrain:
case kRubbleTerrain0:
case kRubbleTerrain1:
case kRubbleTerrain2:
case kRubbleTerrain3:
case kGrassTerrain0:
case kGrassTerrain1:
case kGrassTerrain2:
case kGrassTerrain3:
break;
default:
assert(0);
break;
}
}
for (i = 0; i < server.nbases; i++) {
switch (server.terrain[server.bases[i].y][server.bases[i].x]) {
case kSeaTerrain:
case kBoatTerrain:
case kWallTerrain:
case kRiverTerrain:
case kForestTerrain:
case kDamagedWallTerrain0:
case kDamagedWallTerrain1:
case kDamagedWallTerrain2:
case kDamagedWallTerrain3:
case kMinedSeaTerrain:
case kMinedForestTerrain:
server.terrain[server.bases[i].y][server.bases[i].x] = kGrassTerrain0;
break;
case kMinedSwampTerrain:
server.terrain[server.bases[i].y][server.bases[i].x] = kSwampTerrain0;
break;
case kMinedCraterTerrain:
server.terrain[server.bases[i].y][server.bases[i].x] = kCraterTerrain;
break;
case kMinedRoadTerrain:
server.terrain[server.bases[i].y][server.bases[i].x] = kRoadTerrain;
break;
case kMinedRubbleTerrain:
server.terrain[server.bases[i].y][server.bases[i].x] = kRubbleTerrain0;
case kMinedGrassTerrain:
server.terrain[server.bases[i].y][server.bases[i].x] = kGrassTerrain0;
break;
case kSwampTerrain0:
case kSwampTerrain1:
case kSwampTerrain2:
case kSwampTerrain3:
case kCraterTerrain:
case kRoadTerrain:
case kRubbleTerrain0:
case kRubbleTerrain1:
case kRubbleTerrain2:
case kRubbleTerrain3:
case kGrassTerrain0:
case kGrassTerrain1:
case kGrassTerrain2:
case kGrassTerrain3:
break;
default:
assert(0);
break;
}
}
CLEANUP
ERRHANDLER(0, -1)
END
}
ssize_t serversavemap(void **data) {
size_t y, x;
int i;
struct BMAP_Preamble *preamble;
struct BMAP_PillInfo *pillInfos;
struct BMAP_BaseInfo *baseInfos;
struct BMAP_StartInfo *startInfos;
void *runData;
struct BMAP_Run *run;
int offset;
ssize_t size;
*data = NULL;
TRY
/* find the size of the map */
if ((size = serverloadmapsize()) == -1) LOGFAIL(errno)
/* allocate memory */
if ((*data = malloc(size)) == NULL) LOGFAIL(errno)
/* zero the bytes */
bzero(*data, size);
/* initialize the pointers */
preamble = *data;
pillInfos = (struct BMAP_PillInfo *)(preamble + 1);
baseInfos = (struct BMAP_BaseInfo *)(pillInfos + server.npills);
startInfos = (struct BMAP_StartInfo *)(baseInfos + server.nbases);
runData = (void *)(startInfos + server.nstarts);
/* write the preamble */
bcopy(MAP_FILE_IDENT, preamble->ident, MAP_FILE_IDENT_LEN);
preamble->version = CURRENT_MAP_VERSION;
preamble->npills = server.npills;
preamble->nbases = server.nbases;
preamble->nstarts = server.nstarts;
/* write the pillinfo */
for (i = 0; i < server.npills; i++) {
pillInfos[i].x = server.pills[i].x;
pillInfos[i].y = server.pills[i].y;
pillInfos[i].owner = server.pills[i].owner;
pillInfos[i].armour = server.pills[i].armour;
pillInfos[i].speed = server.pills[i].speed;
}
/* write the baseinfo */
for (i = 0; i < server.nbases; i++) {
baseInfos[i].x = server.bases[i].x;
baseInfos[i].y = server.bases[i].y;
baseInfos[i].owner = server.bases[i].owner;
baseInfos[i].armour = server.bases[i].armour;
baseInfos[i].shells = server.bases[i].shells;
baseInfos[i].mines = server.bases[i].mines;
}
/* write the startinfo */
for (i = 0; i < server.nstarts; i++) {
startInfos[i].x = server.starts[i].x;
startInfos[i].y = server.starts[i].y;
startInfos[i].dir = server.starts[i].dir;
}
offset = 0;
y = 0;
x = 0;
while (y < WIDTH && x < WIDTH) {
int r;
run = runData + offset;
if ((r = readrun(&y, &x, run, run + 1, server.terrain)) == -1) LOGFAIL(errno)
if (r == 1) break;
offset += run->datalen;
}
data = NULL;
CLEANUP
if (data != NULL && *data != NULL) {
free(*data);
*data = NULL;
}
ERRHANDLER(size, -1)
END
}
ssize_t serverloadmapsize() {
size_t x, y, len;
TRY
x = 0;
y = 0;
len =
sizeof(struct BMAP_Preamble) +
server.npills*sizeof(struct BMAP_PillInfo) +
server.nbases*sizeof(struct BMAP_BaseInfo) +
server.nstarts*sizeof(struct BMAP_StartInfo);
for (;;) {
ssize_t r;
struct BMAP_Run run;
char buf[256];
if ((r = readrun(&y, &x, &run, buf, server.terrain)) == -1) LOGFAIL(errno)
len += run.datalen;
/* if this is the last run */
if (r == 1) SUCCESS
}
CLEANUP
ERRHANDLER(len, -1)
END
}