-
Notifications
You must be signed in to change notification settings - Fork 3
/
game.c
340 lines (322 loc) · 7.46 KB
/
game.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
/*
* Copyright (c) 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 "config.h"
#include <err.h>
#include <glob.h>
#include <limits.h>
#include <pwd.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "level.h"
#include "ui.h"
#include "creature.h"
#include "options.h"
#include "world.h"
#include "rng.h"
static void usage(void);
static const char *filename = ".roguelikerc";
static int
config_file_read(const char *configfile)
{
size_t linez = 0;
ssize_t linelen = 0;
char *line = NULL;
FILE *s;
log_debug("--- Configuration file ---\n");
log_debug("Reading configuration file %s\n", configfile);
if (NULL == (s = fopen(configfile, "r"))) {
log_debug("Can't open, aborting\n");
return(-1);
}
while (-1 != (linelen = getline(&line, &linez, s))) {
char *linep, *key, *value;
int found = 0;
line[linelen - 1] = '\0';
linep = line;
if ('#' == linep[0])
continue;
while (' ' == linep[0] || '\t' == linep[0]) {
linep++;
}
if (strlen(linep) == 0)
continue;
key = strsep(&linep, ":");
if (NULL == linep) {
log_debug("bad line -- %s\n", line);
free(line);
return(-1);
}
value = linep;
while (' ' == value[0] || '\t' == value[0]) {
value++;
}
for (int i = 0; i < O__MAX; i++) {
if (strcmp(optionsmap[i].name, key) != 0) {
continue;
}
if (strcmp(value, "true") == 0) {
optionsmap[i].value = true;
} else if (strcmp(value, "false") == 0) {
optionsmap[i].value = false;
} else {
log_debug("option %s has wrong value -- %s\n",
key, value);
free(line);
return(-1);
}
found = 1;
log_debug("Found option %s=%s\n", key, value);
break;
}
if (found == 1)
continue;
for (int i = NONCONFIGURABLEKEYS; i < K__MAX; i++) {
if (strcmp(keybindingsmap[i].name, key) != 0) {
continue;
}
if (strlen(value) > 1) {
log_debug("key %s has wrong value -- %s\n",
key, value);
free(line);
return(-1);
}
keybindingsmap[i].key = value[0];
}
}
free(line);
line = NULL;
return(0);
}
int
main(int argc, char *argv[])
{
int ch, is_running;
bool debug = false;
uint32_t seed;
glob_t gl;
char path[PATH_MAX];
struct creature p;
struct world w;
char *configfile = NULL;
const char *errstr;
struct level *lp;
struct passwd *pw;
while ((ch = getopt(argc, argv, "df:s:")) != -1) {
switch (ch) {
case 'd':
debug = true;
break;
case 'f':
configfile = optarg;
break;
case 's':
seed = strtonum(optarg, 0, UINT32_MAX, &errstr);
if (errstr != NULL) {
errx(1, "invalid seed value");
}
rng_set_seed(seed);
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (true == debug) {
log_open("debug.log");
}
if (NULL == configfile) {
pw = getpwuid(getuid());
snprintf(path, sizeof(path), "%s/%s", pw->pw_dir, filename);
} else {
glob(configfile, GLOB_NOCHECK | GLOB_TILDE, NULL, &gl);
snprintf(path, sizeof(path), "%s", gl.gl_pathv[0]);
globfree(&gl);
}
if (-1 == config_file_read(path)) {
goto exit;
}
log_debug("--- rng ---\n");
rng_init();
log_debug("Seed is %li\n", rng_get_seed());
log_debug("--- ui ---\n");
ui_init();
/* Check for 23 because of ripoffline */
if ((ui_get_lines() < 23) || (ui_get_cols() < 80)) {
ui_cleanup();
fprintf(stderr, "must be displayed on 80x24 screen.");
fprintf(stderr, "LINES: %i, COLS: %i\n",
ui_get_lines(), ui_get_cols());
goto exit;
}
is_running = -1;
log_debug("--- world ---\n");
world_init(&w);
lp = world_first(&w);
log_debug("--- creature (hero) ---\n");
creature_init(&p, R_HUMAN);
creature_place_at_stair(&p, lp, true);
log_debug("--- start game ---\n");
do {
int key, noaction;
noaction = 0;
if (false == lp->visited) {
if (true == debug)
ui_message("Seed: %u", rng_get_seed());
else if (NULL == lp->entrymessage)
ui_clearmessage();
else
ui_message(lp->entrymessage);
lp->visited = true;
}
ui_draw(lp);
p.actionpoints += p.speed;
while (p.actionpoints >= 5) {
if (-1 != is_running) {
key = is_running;
} else {
key = keybinding_resolve(ui_get_input());
}
if (key == K__MAX) {
continue;
}
switch (key) {
case K_RUNLEFT:
is_running = K_LEFT;
/* FALLTHROUGH */
case K_LEFT:
noaction = creature_move_left(&p, lp);
break;
case K_RUNDOWN:
is_running = K_DOWN;
/* FALLTHROUGH */
case K_DOWN:
noaction = creature_move_down(&p, lp);
break;
case K_RUNUP:
is_running = K_UP;
/* FALLTHROUGH */
case K_UP:
noaction = creature_move_up(&p, lp);
break;
case K_RUNRIGHT:
is_running = K_RIGHT;
/* FALLTHROUGH */
case K_RIGHT:
noaction = creature_move_right(&p, lp);
break;
case K_RUNUPLEFT:
is_running = K_UPLEFT;
/* FALLTHROUGH */
case K_UPLEFT:
noaction = creature_move_upleft(&p, lp);
break;
case K_RUNUPRIGHT:
is_running = K_UPRIGHT;
/* FALLTHROUGH */
case K_UPRIGHT:
noaction = creature_move_upright(&p, lp);
break;
case K_RUNDOWNLEFT:
is_running = K_DOWNLEFT;
/* FALLTHROUGH */
case K_DOWNLEFT:
noaction = creature_move_downleft(&p, lp);
break;
case K_RUNDOWNRIGHT:
is_running = K_DOWNRIGHT;
/* FALLTHROUGH */
case K_DOWNRIGHT:
noaction = creature_move_downright(&p, lp);
break;
case K_UPSTAIR:
if (lp == world_first(&w)) {
noaction = -1;
break;
}
noaction = creature_climb_upstair(&p, lp, world_prev(&w));
lp = world_current(&w);
break;
case K_DOWNSTAIR:
noaction = creature_climb_downstair(&p, lp, world_next(&w));
lp = world_current(&w);
break;
case K_REST:
noaction = creature_rest(&p);
break;
case K_LOOKHERE:
ui_look(lp, p.y, p.x);
ui_draw(lp);
noaction = -1;
break;
case K_LOOKELSEWHERE:
ui_look_elsewhere(lp, p.y, p.x);
ui_draw(lp);
noaction = -1;
break;
case K_OPTIONMENU:
ui_menu_options();
ui_draw(lp);
noaction = -1;
break;
case K_HELPMENU:
ui_menu_help();
ui_draw(lp);
noaction = -1;
break;
default:
noaction = -1;
break;
}
if (noaction == -1) {
is_running = -1;
continue;
}
p.actionpoints -= 5;
}
/* Monsters' turn */
for (int32_t i = 0; i < w.creaturesz; i++) {
struct creature *c;
c = w.creatures[i];
c->actionpoints += c->speed;
while (c->actionpoints >= 5) {
creature_do_something(c, world_first(&w));
c->actionpoints -= 5;
}
}
/* Add a slight delay when running */
if (-1 != is_running) {
ui_pause(0, 100);
}
} while (1);
exit:
log_close();
world_free(&w);
ui_cleanup();
return(0);
}
static void
usage(void)
{
fprintf(stderr, "usage: %s [-d] [-f file] [-s seed]\n", getprogname());
exit(1);
}