-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.cpp
425 lines (347 loc) · 8 KB
/
gen.cpp
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
/*
* OPAL's playable almost indefectibly.
* Copyright (C) 2019 Esote
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define _DEFAULT_SOURCE
#define _BSD_SOURCE
#include <endian.h>
#undef _BSD_SOURCE
#undef _DEFAULT_SOURCE
#include <sys/stat.h>
#include <err.h>
#include "floor.h"
#include "globs.h"
static bool save_things(FILE *const);
static bool load_things(FILE *const);
static void init_fresh();
static void place_player();
static int valid_player(int const, int const);
static char const *const DIRECTORY = "/.opal";
static char const *const FILEPATH = "/dungeon";
static int constexpr DF_L = 8;
static char const *const MARK = "OPAL-DUNGEON";
static int constexpr MARK_L = 12;
static int constexpr NEW_ROOM_COUNT = 8;
static int constexpr ROOM_RETRIES = 150;
static std::vector<stair> stairs_up;
static std::vector<stair> stairs_dn;
static uint16_t room_count;
static std::vector<room> rooms;
static uint16_t stair_up_count;
static uint16_t stair_dn_count;
tile tiles[HEIGHT][WIDTH];
std::string
opal_path()
{
char *home;
#ifdef _GNU_SOURCE
home = secure_getenv("HOME");
#else
home = getenv("HOME");
#endif
if (home == NULL) {
#ifdef _GNU_SOURCE
errx(1, "getenv");
#else
errx(1, "secure_getenv");
#endif
}
return std::string(home) + DIRECTORY;
}
bool
save_dungeon()
{
struct stat st;
FILE *f;
bool ret;
std::string path = opal_path();
if (stat(path.c_str(), &st) == -1) {
if (errno == ENOENT) {
if (mkdir(path.c_str(), 0700) == -1) {
err(1, "save mkdir");
}
} else {
err(1, "save stat");
}
}
path += FILEPATH;
if (!(f = fopen(path.c_str(), "w"))) {
err(1, "save fopen");
}
ret = save_things(f);
if (fclose(f) == EOF) {
err(1, "save fclose, (save_things=%d)", ret);
}
return ret;
}
bool
load_dungeon()
{
FILE *f;
bool ret;
std::string path = opal_path() + FILEPATH;
if (!(f = fopen(path.c_str(), "r"))) {
err(1, "load fopen");
}
ret = load_things(f);
if (fclose(f) == EOF) {
err(1, "load fclose, (load_things=%d)", ret);
}
return ret;
}
void
clear_tiles()
{
for (uint8_t i = 0; i < HEIGHT; ++i) {
for (uint8_t j = 0; j < WIDTH; ++j) {
tiles[i][j] = {};
tiles[i][j].x = j;
tiles[i][j].y = i;
if (i == 0 || j == 0 || i == HEIGHT - 1
|| j == WIDTH - 1) {
tiles[i][j].h = std::numeric_limits<uint8_t>::max();
tiles[i][j].d = std::numeric_limits<int32_t>::max();
tiles[i][j].dt = std::numeric_limits<int32_t>::max();
} else {
tiles[i][j].c = ROCK;
tiles[i][j].h = rr.rrand<uint8_t>(1,
std::numeric_limits<uint8_t>::max() - 1);
}
}
}
}
void
arrange_new()
{
room_count = NEW_ROOM_COUNT;
stair_up_count = rr.rrand<uint16_t>(1, (uint16_t)((room_count / 4) + 1));
stair_dn_count = rr.rrand<uint16_t>(1, (uint16_t)((room_count / 4) + 1));
rooms.resize(room_count);
stairs_up.resize(stair_up_count);
stairs_dn.resize(stair_dn_count);
init_fresh();
}
void
arrange_loaded()
{
for (auto const &r : rooms) {
draw_room(r);
}
for (auto const &s : stairs_up) {
tiles[s.y][s.x].c = STAIR_UP;
}
for (auto const &s : stairs_dn) {
tiles[s.y][s.x].c = STAIR_DN;
}
for (int i = 1; i < HEIGHT - 1; ++i) {
for (int j = 1; j < WIDTH - 1; ++j) {
if (tiles[i][j].h == 0 && tiles[i][j].c == ROCK) {
tiles[i][j].c = CORRIDOR;
}
}
}
}
void
arrange_renew()
{
clear_tiles();
rooms.clear();
stairs_up.clear();
stairs_dn.clear();
arrange_new();
}
static bool
save_things(FILE *const f)
{
uint32_t const ver = htobe32(0);
uint32_t const filesize = htobe32((uint32_t)(1708 + (room_count * 4)
+ (stair_up_count * 2) + (stair_dn_count * 2)));
/* type marker */
if (fwrite(MARK, MARK_L, 1, f) != 1) {
return false;
}
/* version */
if (fwrite(&ver, sizeof(uint32_t), 1, f) != 1) {
return false;
}
/* size */
if (fwrite(&filesize, sizeof(uint32_t), 1, f) != 1) {
return false;
}
/* player coords */
if (fwrite(&player.x, sizeof(uint8_t), 1, f) != 1) {
return false;
}
if (fwrite(&player.y, sizeof(uint8_t), 1, f) != 1) {
return false;
}
/* hardness */
for (std::size_t i = 0; i < HEIGHT; ++i) {
for (std::size_t j = 0; j < WIDTH; ++j) {
if (fwrite(&tiles[i][j].h, sizeof(uint8_t), 1, f) != 1) {
return false;
}
}
}
/* room num */
room_count = htobe16(room_count);
if (fwrite(&room_count, sizeof(uint16_t), 1, f) != 1) {
return false;
}
room_count = be16toh(room_count);
/* room data */
for (auto const &r : rooms) {
if (fwrite(&r, sizeof(room), 1, f) != 1) {
return false;
}
}
/* stairs_up num */
stair_up_count = htobe16(stair_up_count);
if (fwrite(&stair_up_count, sizeof(uint16_t), 1, f) != 1) {
return false;
}
stair_up_count = be16toh(stair_up_count);
/* stars_up coords */
for (auto const &s : stairs_up) {
if (fwrite(&s, sizeof(stair), 1, f) != 1) {
return false;
}
}
/* stairs_dn num */
stair_dn_count = htobe16(stair_dn_count);
if (fwrite(&stair_dn_count, sizeof(uint16_t), 1, f) != 1) {
return false;
}
stair_dn_count = be16toh(stair_dn_count);
/* stairs_dn coords */
for (auto const &s : stairs_dn) {
if (fwrite(&s, sizeof(stair), 1, f) != 1) {
return false;
}
}
return true;
}
static bool
load_things(FILE *const f)
{
/* skip type marker, version, and size */
if (fseek(f, MARK_L + 2 * sizeof(uint32_t), SEEK_SET) == -1) {
return false;
}
/* player coords */
if (fread(&player.x, sizeof(uint8_t), 1, f) != 1) {
return false;
}
if (fread(&player.y, sizeof(uint8_t), 1, f) != 1) {
return false;
}
/* hardness */
for (std::size_t i = 0; i < HEIGHT; ++i) {
for (std::size_t j = 0; j < WIDTH; ++j) {
if (fread(&tiles[i][j].h, sizeof(uint8_t), 1, f) != 1) {
return false;
}
}
}
/* room num */
if (fread(&room_count, sizeof(uint16_t), 1, f) != 1) {
return false;
}
room_count = be16toh(room_count);
rooms.resize(room_count);
/* room data */
for (auto &r : rooms) {
if (fread(&r, sizeof(room), 1, f) != 1) {
return false;
}
}
/* stair_up num */
if (fread(&stair_up_count, sizeof(uint16_t), 1, f) != 1) {
return false;
}
stair_up_count = be16toh(stair_up_count);
stairs_up.resize(stair_up_count);
/* stair_up coords */
for (auto &s : stairs_up) {
if (fread(&s, sizeof(stair), 1, f) != 1) {
return false;
}
}
/* stair_dn num */
if (fread(&stair_dn_count, sizeof(uint16_t), 1, f) != 1) {
return false;
}
stair_dn_count = be16toh(stair_dn_count);
stairs_dn.resize(stair_dn_count);
/* stair_dn_coords */
for (auto &s : stairs_dn) {
if (fread(&s, sizeof(stair), 1, f) != 1) {
return false;
}
}
return true;
}
static void
init_fresh()
{
std::size_t i = 0;
std::size_t retries = 0;
for (auto it = rooms.begin(); it != rooms.end()
&& retries < ROOM_RETRIES; ++it) {
if (!gen_room(*it)) {
retries++;
it--;
} else {
i++;
draw_room(*it);
}
}
if (i < room_count) {
if (i == 0) {
errx(1, "unable to place any rooms");
}
room_count = (uint16_t)i;
rooms.resize(room_count);
}
for (i = 0; i < room_count - 1U; ++i) {
gen_corridor(rooms[i], rooms[i+1]);
}
for (auto &s : stairs_up) {
gen_stair(s, true);
}
for (auto &s : stairs_dn) {
gen_stair(s, true);
}
place_player();
}
static void
place_player()
{
uint8_t x, y;
do {
x = rr.rrand<uint8_t>(1, WIDTH - 2);
y = rr.rrand<uint8_t>(1, HEIGHT - 2);
} while (!valid_player(y, x));
player.x = x;
player.y = y;
}
static int
valid_player(int const y, int const x)
{
return tiles[y][x].h == 0
&& tiles[y + 1][x].h == 0 && tiles[y - 1][x].h == 0
&& tiles[y][x + 1].h == 0 && tiles[y][x - 1].h == 0;
}