forked from tleguern/openroguelike
-
Notifications
You must be signed in to change notification settings - Fork 1
/
level.c
323 lines (304 loc) · 7.17 KB
/
level.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
/*
* Copyright (c) 2013,2018 Tristan Le Guern <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "config.h"
#include "creature.h"
#include "level.h"
#include "pathfind.h"
#include "rng.h"
#include "ui.h"
enum operand {
OP_NAME,
OP_TYPE,
OP_SIZE,
OP_POSITION,
OP_MAP,
OP__MAX,
};
static const char *operandmaps[] = {
"name",
"type",
"size",
"position",
"map",
};
/*
* Fill a struct coordinate from a string with the specific format "%i %i"
* The source string is modified due to strsep(3).
*/
static int
string_to_coordinate(char *src, struct coordinate *c)
{
char *x, *y;
const char *errstr;
y = src;
strsep(&src, " ");
x = src;
errstr = NULL;
c->y = (int)strtonum(y, 0, MAXROWS, &errstr);
if (NULL != errstr)
return(-1);
c->x = (int)strtonum(x, 0, MAXCOLS, &errstr);
if (NULL != errstr)
return(-1);
return(0);
}
bool
tile_is_empty(struct tile *t) {
if ((T_EMPTY == t->type || T_UPSTAIR == t->type
|| T_DOWNSTAIR == t->type) && NULL == t->creature)
return(true);
return(false);
}
bool
tile_is_wall(struct tile *t) {
if (T_WALL == t->type)
return(true);
return(false);
}
void
level_init(struct level *l) {
l->type = L_NONE;
l->visited = false;
l->entrymessage = NULL;
for (int y = 0; y < MAXROWS; y++) {
for (int x = 0; x < MAXCOLS; x++) {
l->tile[y][x].type = T_EMPTY;
l->tile[y][x].creature = NULL;
}
}
}
void
level_load(struct level *l, const char *filename)
{
struct coordinate size, position = {0, 0};
size_t linez;
ssize_t linelen;
const char *errstr;
char *line;
FILE *s;
errstr = NULL;
line = NULL;
linez = 0;
linelen = 0;
size.x = -1;
size.y = -1;
if (NULL == (s = fopen(filename, "r"))) {
errstr = strerror(errno);
goto clean;
}
/*
* Read the key at the begining of each line, with a special
* treatment for map.
*/
while (-1 != (linelen = getline(&line, &linez, s))) {
enum operand op;
int y;
char *linep, *key, *value;
line[linelen - 1] = '\0';
linep = line;
if (strsep(&linep, ":") == NULL) {
errstr = "malformed line";
goto closeclean;
}
key = line;
for (op = 0; op < OP__MAX; op++) {
if (strcmp(operandmaps[op], key) == 0) {
break;
}
}
if (OP__MAX == op) {
errstr = "unknown operand";
goto closeclean;
} else if (OP_MAP != op) {
value = linep;
if (' ' != value[0]) {
errstr = "malformed value";
goto closeclean;
}
value++;
}
switch (op) {
case OP_NAME:
/* This name is more a label than a real attribute */
continue;
case OP_TYPE:
if (strcmp("cave", value) == 0) {
l->type = L_CAVE;
} else if (strcmp("static", value) == 0) {
l->type = L_STATIC;
} else {
errstr = "unknown type";
goto closeclean;
}
break;
case OP_SIZE:
if (-1 == string_to_coordinate(value, &size)) {
errstr = "invalid coordinate for \"size\"";
goto closeclean;
}
break;
case OP_POSITION:
if (-1 == string_to_coordinate(value, &position)) {
errstr = "invalid coordinate for \"position\"";
goto closeclean;
}
break;
case OP_MAP:
if (-1 == size.x || -1 == size.y) {
errstr = "\"size\" should be defined"
" before \"map\"";
goto closeclean;
}
y = position.y;
linez = 0;
linelen = 0;
line = NULL;
while (-1 != (linelen = getline(&line, &linez, s))) {
if (linelen - 1 != size.x) {
errstr = "bad value for size.x";
goto closeclean;
}
line[linelen - 1] = '\0';
for (int x = 0; x < size.x; x++) {
int lx;
lx = x + position.x;
if ('#' == line[x]) {
l->tile[y][lx].type = T_WALL;
} else if ('<' == line[x]) {
l->tile[y][lx].type = T_UPSTAIR;
} else if ('>' == line[x]) {
l->tile[y][lx].type = T_DOWNSTAIR;
} else if (' ' == line[x]) {
l->tile[y][lx].type = T_EMPTY;
}
}
y += 1;
}
break;
case OP__MAX:
default:
errstr = "unknown operand";
goto closeclean;
}
}
fclose(s);
free(line);
line = NULL;
return;
closeclean:
fclose(s);
free(line);
line = NULL;
clean:
ui_cleanup();
fprintf(stderr, "%s: %s\n", filename, errstr);
exit(EXIT_FAILURE);
}
void
coordinate_init(struct coordinate *c)
{
c->y = -1;
c->x = -1;
}
void
coordinate_copy(struct coordinate *dest, struct coordinate *src)
{
dest->y = src->y;
dest->x = src->x;
}
int
level_add_stairs(struct level *l, bool build_upstair, bool build_downstair)
{
struct coordinate upstair, downstair;
struct coordinate existing_upstair, existing_downstair;
int count = 0;
coordinate_init(&existing_upstair);
coordinate_init(&existing_downstair);
/*
* Look if stairs are already present on the map, which could happen
* with level_load().
*/
for (int y = 1; y < MAXROWS; y++) {
for (int x = 1; x < MAXCOLS; x++) {
if (l->tile[y][x].type == T_UPSTAIR) {
existing_upstair.y = y;
existing_upstair.x = x;
log_debug("Found upward stairs\n");
}
if (l->tile[y][x].type == T_DOWNSTAIR) {
existing_downstair.y = y;
existing_downstair.x = x;
log_debug("Found downward stairs\n");
}
}
}
coordinate_copy(&upstair, &existing_upstair);
coordinate_copy(&downstair, &existing_downstair);
do {
log_debug("Try to add stairs (%i)\n", count);
count += 1;
if (-1 == existing_upstair.y)
upstair.y = rng_rand_uniform(MAXROWS);
if (-1 == existing_upstair.x)
upstair.x = rng_rand_uniform(MAXCOLS);
if (-1 == existing_downstair.y)
downstair.y = rng_rand_uniform(MAXROWS);
if (-1 == existing_downstair.x)
downstair.x = rng_rand_uniform(MAXCOLS);
/* Ensure stairs are not too close */
if (abs((upstair.y + upstair.x) - (downstair.y + downstair.x)) < 50)
continue;
if (! tile_is_empty(&(l->tile[upstair.y][upstair.x])))
continue;
if (! tile_is_empty(&(l->tile[downstair.y][downstair.x])))
continue;
if (false == are_coordinate_reachable(l, &upstair, &downstair))
continue;
if (build_upstair)
l->tile[upstair.y][upstair.x].type = T_UPSTAIR;
if (build_downstair)
l->tile[downstair.y][downstair.x].type = T_DOWNSTAIR;
break;
} while (count < 50);
if (count == 50) {
log_debug("Can't generate stairs for this level\n");
return(-1);
}
return(0);
}
int
level_find(struct level *l, enum tile_type tile, struct coordinate *coord)
{
for (int y = 0; y < MAXROWS; y++) {
for (int x = 0; x < MAXCOLS; x++) {
if (tile == l->tile[y][x].type) {
coord->y = y;
coord->x = x;
return(0);
}
}
}
coord->y = -1;
coord->x = -1;
return(-1);
}