-
Notifications
You must be signed in to change notification settings - Fork 0
/
g_wolf.cc
466 lines (350 loc) · 11.6 KB
/
g_wolf.cc
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//------------------------------------------------------------------------
// LEVEL building - Wolf3d format
//------------------------------------------------------------------------
//
// Oblige Level Maker
//
// Copyright (C) 2006-2016 Andrew Apted
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
//------------------------------------------------------------------------
#include "fmt/format.h"
#include "hdr_fltk.h"
#include "hdr_lua.h"
#include "hdr_ui.h"
#include "headers.h"
#include "lib_file.h"
#include "lib_util.h"
#include "m_lua.h"
#include "main.h"
#define TEMP_GAMEFILE "GAMEMAPS.TMP"
#define TEMP_HEADFILE "MAPHEAD.TMP"
#define RLEW_TAG 0xABCD
#define NO_TILE 48
#define NO_OBJ 0
/* private data */
static FILE *map_fp;
static FILE *head_fp;
static int write_errors_seen;
static int current_map; // 1 to 60
static int current_offset;
static u16_t *solid_plane;
static u16_t *thing_plane;
static std::string level_name;
#define PL_START 2
//------------------------------------------------------------------------
// WOLF OUTPUT
//------------------------------------------------------------------------
static void WF_PutU16(u16_t val, FILE *fp) {
fputc(val & 0xFF, fp);
fputc((val >> 8) & 0xFF, fp);
}
static void WF_PutU32(u32_t val, FILE *fp) {
fputc(val & 0xFF, fp);
fputc((val >> 8) & 0xFF, fp);
fputc((val >> 16) & 0xFF, fp);
fputc((val >> 24) & 0xFF, fp);
}
static void WF_PutNString(const char *str, int max_len, FILE *fp) {
for (; *str && max_len > 0; max_len--) {
fputc(*str++, fp);
}
for (; max_len > 0; max_len--) {
fputc(0, fp);
}
}
int rle_compress_plane(u16_t *plane, int src_len) {
u16_t *dest = plane + PL_START;
const u16_t *src = plane + PL_START;
const u16_t *endp = plane + PL_START + (src_len / 2);
while (src < endp) {
// don't want no Carmackization...
SYS_ASSERT((*src & 0xFF00) != 0xA700);
SYS_ASSERT((*src & 0xFF00) != 0xA800);
// it shouldn't match the RLEW tag either...
SYS_ASSERT(*src != RLEW_TAG);
// determine longest run
int run = 1;
while (src + run < endp && run < 100 && src[run - 1] == src[run]) {
run++;
}
if (run > 3) {
// Note: use src[2] since src may == dest, hence src[0] and src[1]
// would get overwritten by the tag and count.
*dest++ = RLEW_TAG; // tag
*dest++ = run; // count
*dest++ = src[2]; // value
src += run;
} else {
for (; run > 0; run--) {
*dest++ = *src++;
}
}
}
int dest_len = 2 * (dest - plane);
plane[0] = dest_len + 2; // compressed size (bytes)
plane[1] = src_len; // expanded size (bytes)
return dest_len + 4; // total size
}
//------------------------------------------------------------------------
static void WF_WritePlane(u16_t *plane, int *offset, int *length) {
*offset = (int)ftell(map_fp);
*length = rle_compress_plane(plane, 64 * 64 * 2);
if (1 != fwrite(plane, *length, 1, map_fp)) {
if (write_errors_seen < 10) {
write_errors_seen += 1;
LogPrintf("Failure writing to map file! ({} bytes)\n", *length);
}
}
}
static void WF_WriteBlankPlane(int *offset, int *length) {
*offset = (int)ftell(map_fp);
WF_PutU16(3 * 2 + 2, map_fp); // compressed size + 2
WF_PutU16(64 * 64 * 2, map_fp); // expanded size
WF_PutU16(RLEW_TAG, map_fp); // tag
WF_PutU16(64 * 64, map_fp); // count
WF_PutU16(0, map_fp); // value
*length = (int)ftell(map_fp);
*length -= *offset;
}
static void WF_WriteMap(void) {
const auto message = fmt::format("{} {}", OBSIDIAN_TITLE, OBSIDIAN_VERSION);
WF_PutNString(message.c_str(), 64, map_fp);
int plane_offsets[3];
int plane_lengths[3];
WF_WritePlane(solid_plane, plane_offsets + 0, plane_lengths + 0);
WF_WritePlane(thing_plane, plane_offsets + 1, plane_lengths + 1);
WF_WriteBlankPlane(plane_offsets + 2, plane_lengths + 2);
current_offset = (int)ftell(map_fp);
// TODO: validate (error check)
WF_PutU32(plane_offsets[0], map_fp);
WF_PutU32(plane_offsets[1], map_fp);
WF_PutU32(plane_offsets[2], map_fp);
WF_PutU16(plane_lengths[0], map_fp);
WF_PutU16(plane_lengths[1], map_fp);
WF_PutU16(plane_lengths[2], map_fp);
// width and height
WF_PutU16(64, map_fp);
WF_PutU16(64, map_fp);
WF_PutNString(level_name.empty() ? "Custom Map" : level_name.c_str(), 16,
map_fp);
WF_PutNString("!ID!", 4, map_fp);
}
static void WF_WriteHead(void) {
// offset to map data (info struct)
WF_PutU32(current_offset, head_fp);
}
//------------------------------------------------------------------------
// LUA: wolf_block(x, y, plane, value)
//
int WF_wolf_block(lua_State *L) {
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
int plane = luaL_checkinteger(L, 3);
int value = luaL_checkinteger(L, 4);
// adjust and validate coords
SYS_ASSERT(1 <= x && x <= 64);
SYS_ASSERT(1 <= y && y <= 64);
x--;
y = 64 - y;
switch (plane) {
case 1:
solid_plane[PL_START + y * 64 + x] = value;
break;
case 2:
thing_plane[PL_START + y * 64 + x] = value;
break;
default:
// other planes are silently ignored
break;
}
return 0;
}
// LUA: wolf_read(x, y, plane)
//
int WF_wolf_read(lua_State *L) {
int x = luaL_checkinteger(L, 1);
int y = luaL_checkinteger(L, 2);
int plane = luaL_checkinteger(L, 3);
// adjust and validate coords
SYS_ASSERT(1 <= x && x <= 64);
SYS_ASSERT(1 <= y && y <= 64);
x--;
y = 64 - y;
int value = 0;
switch (plane) {
case 1:
value = solid_plane[PL_START + y * 64 + x];
break;
case 2:
value = thing_plane[PL_START + y * 64 + x];
break;
default:
// other planes are silently ignored
break;
}
lua_pushinteger(L, value);
return 1;
}
static void WF_DumpMap(void) {
static const char *turning_points = ">/^\\</v\\";
// static char *player_angles = "^>v<";
bool show_floors = false;
char line_buf[80];
for (int y = 0; y < 64; y++) {
for (int x = 0; x < 64; x++) {
int tile = solid_plane[PL_START + y * 64 + x];
int obj = thing_plane[PL_START + y * 64 + x];
int ch;
if (tile == NO_TILE) {
ch = '#';
} else if (obj >= 19 && obj <= 22) {
ch = 'p'; // player_angles[obj-19];
} else if (tile < 52) {
ch = 'A' + (tile / 2);
} else if (tile < 64) {
ch = (show_floors ? 'A' : '1') + ((tile - 52) / 2);
} else if (90 <= tile && tile <= 101) {
ch = '+';
} else if (show_floors && 108 <= tile && tile <= 143) {
ch = '0' + ((tile - 108) % 10);
} else if (obj == NO_OBJ) {
ch = '.';
} else if ((obj >= 43 && obj <= 56) || obj == 29) {
ch = '$'; // pickup
} else if ((obj >= 23 && obj <= 71) || obj == 124) {
ch = '%'; // scenery
} else if (obj >= 108) {
ch = 'm'; // monster
} else if (90 <= obj && obj <= 97) {
ch = turning_points[obj - 90];
} else {
ch = '?';
}
line_buf[x] = ch;
}
line_buf[64] = 0;
DebugPrintf("{}\n", line_buf);
}
}
static void WF_FreeStuff() {
delete[] solid_plane;
solid_plane = NULL;
delete[] thing_plane;
thing_plane = NULL;
}
//------------------------------------------------------------------------
// PUBLIC INTERFACE
//------------------------------------------------------------------------
class wolf_game_interface_c : public game_interface_c {
private:
std::string file_ext;
public:
wolf_game_interface_c() : file_ext("WL6") {}
~wolf_game_interface_c() {}
bool Start(const char *preset);
bool Finish(bool build_ok);
void BeginLevel();
void EndLevel();
void Property(std::string key, std::string value);
private:
void Rename();
void Tidy();
};
bool wolf_game_interface_c::Start(const char *preset) {
WF_FreeStuff();
write_errors_seen = 0;
map_fp = fopen(TEMP_GAMEFILE, "wb");
if (!map_fp) {
LogPrintf("Unable to create {}:\n{}", TEMP_GAMEFILE, strerror(errno));
Main::ProgStatus(_("Error (create file)"));
return false;
}
head_fp = fopen(TEMP_HEADFILE, "wb");
if (!head_fp) {
fclose(map_fp);
LogPrintf("Unable to create {}:\n{}", TEMP_HEADFILE, strerror(errno));
Main::ProgStatus(_("Error (create file)"));
return false;
}
// the maphead file always begins with the RLE tag
WF_PutU16(RLEW_TAG, head_fp);
// setup local variables
current_map = 1;
current_offset = 0;
solid_plane = new u16_t[64 * 64 + 8]; // extra space for compressor
thing_plane = new u16_t[64 * 64 + 8];
if (main_win) {
main_win->build_box->Prog_Init(0, "");
}
return true;
}
bool wolf_game_interface_c::Finish(bool build_ok) {
WF_FreeStuff();
// set remaining map offsets to zero (no map)
for (; current_map <= 100; current_map++) {
WF_PutU32(0, head_fp);
}
fclose(map_fp);
fclose(head_fp);
map_fp = head_fp = NULL;
if (!build_ok) {
Tidy();
return false;
}
if (write_errors_seen > 0) {
Main::ProgStatus(_("Error (write file)"));
Tidy();
return false;
}
Rename();
return true; // OK!
}
void wolf_game_interface_c::Rename() {
std::filesystem::path gamemaps = fmt::format("GAMEMAPS.{}", file_ext);
std::filesystem::path maphead = fmt::format("MAPHEAD.{}", file_ext);
std::filesystem::remove(gamemaps);
std::filesystem::remove(maphead);
std::filesystem::rename(TEMP_GAMEFILE, gamemaps);
std::filesystem::rename(TEMP_HEADFILE, maphead);
}
void wolf_game_interface_c::Tidy() {
std::filesystem::remove(TEMP_GAMEFILE);
std::filesystem::remove(TEMP_HEADFILE);
}
void wolf_game_interface_c::BeginLevel() {
// clear the planes before use
for (int i = 0; i < 64 * 64; i++) {
solid_plane[PL_START + i] = NO_TILE;
thing_plane[PL_START + i] = NO_OBJ;
}
current_map += 1;
SYS_ASSERT(current_map < 100);
}
void wolf_game_interface_c::EndLevel() {
WF_DumpMap();
WF_WriteMap();
WF_WriteHead();
level_name.clear();
}
void wolf_game_interface_c::Property(std::string key, std::string value) {
if (StringCaseCmp(key, "level_name") == 0) {
level_name = value.c_str();
} else if (StringCaseCmp(key, "file_ext") == 0) {
file_ext = value.c_str();
} else {
LogPrintf("WARNING: unknown WOLF3D property: {}={}\n", key, value);
}
}
game_interface_c *Wolf_GameObject() { return new wolf_game_interface_c(); }
//--- editor settings ---
// vi:ts=4:sw=4:noexpandtab