forked from NetHack-LE/nle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnle.c
More file actions
629 lines (530 loc) · 14.6 KB
/
nle.c
File metadata and controls
629 lines (530 loc) · 14.6 KB
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#include <assert.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <tmt.h>
#define NEED_VARARGS
#ifdef MONITOR_HEAP
#undef MONITOR_HEAP
#endif
#include "hack.h"
#include "dlb.h"
#include "nle.h"
#include "nlernd.h"
#ifdef NLE_BZ2_TTYRECS
#include <bzlib.h>
#endif
#define STACK_SIZE (1 << 16) /* 64KiB */
static size_t
effective_stack_size(void)
{
/* create_fcontext_stack() uses the first page as a guard page.
*
* On systems with 64KiB pages (e.g. some aarch64 kernels),
* STACK_SIZE=64KiB results in a single page mapping, leaving no usable
* stack space after the guard page. Ensure at least 2 pages (guard +
* usable).
*/
size_t stack_size = STACK_SIZE;
long page_size = sysconf(_SC_PAGESIZE);
if (page_size > 0 && stack_size < 2u * (size_t) page_size) {
stack_size = 2u * (size_t) page_size;
}
return stack_size;
}
#ifndef __has_feature
#define __has_feature(x) 0 /* Compatibility with non-clang compilers. */
#endif
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
#include <sanitizer/asan_interface.h>
#endif
extern int unixmain(int, char **);
signed char
vt_char_color_extract(TMTCHAR *c)
{
/* We pick out the colors in the enum tmt_color_t. These match the order
* found standard in IBM color graphics, and are the same order as those
* found in src/color.h. */
/* TODO: We no longer need *signed* chars. Let's change the dtype of
* tty_chars when we change the API next. */
signed char color;
if (c->a.fg == TMT_COLOR_DEFAULT) {
/* Need to make a choice for default color. To stay compatible with
NetHack, choose black for the "null glyph", gray otherwise. */
color = (c->c == ' ') ? CLR_BLACK : CLR_GRAY; /* 0 or 7 */
} else if (c->a.fg < TMT_COLOR_MAX) {
color = c->a.fg - TMT_COLOR_BLACK + CLR_BLACK; /* TMT color offset. */
if (c->a.bold) {
color |= BRIGHT;
}
} else {
fprintf(stderr, "Illegal color %d\n", (int) c->a.fg);
color = CLR_GRAY;
}
/* The above is 0..15. For "reverse" colors (bg/fg swap), let's
* use 16..31. */
if (c->a.reverse) {
color += CLR_MAX;
}
return color;
}
void
nle_vt_callback(tmt_msg_t m, TMT *vt, const void *a, void *p)
{
const TMTSCREEN *s = tmt_screen(vt);
const TMTPOINT *cur = tmt_cursor(vt);
nle_ctx_t *nle = (nle_ctx_t *) p;
if (!nle || !nle->observation) {
return;
}
switch (m) {
case TMT_MSG_BELL:
break;
case TMT_MSG_UPDATE:
for (size_t r = 0; r < s->nline; r++) {
if (s->lines[r]->dirty) {
for (size_t c = 0; c < s->ncol; c++) {
size_t offset = (r * NLE_TERM_CO) + c;
TMTCHAR *tmt_c = &(s->lines[r]->chars[c]);
if (nle->observation->tty_chars) {
nle->observation->tty_chars[offset] = tmt_c->c;
}
if (nle->observation->tty_colors) {
nle->observation->tty_colors[offset] =
vt_char_color_extract(tmt_c);
}
}
}
}
tmt_clean(vt);
break;
case TMT_MSG_ANSWER:
break;
case TMT_MSG_MOVED:
if (nle->observation->tty_cursor) {
/* cast from size_t is safe from overflow, since r,c < 256 */
nle->observation->tty_cursor[0] = (unsigned char) cur->r;
nle->observation->tty_cursor[1] = (unsigned char) cur->c;
}
break;
case TMT_MSG_CURSOR:
break;
}
}
nle_ctx_t *
init_nle(FILE *ttyrec, nle_obs *obs)
{
nle_ctx_t *nle = malloc(sizeof(nle_ctx_t));
nle->ttyrec = ttyrec;
#ifdef NLE_BZ2_TTYRECS
if (nle->ttyrec) {
int bzerror;
nle->ttyrec_bz2 = BZ2_bzWriteOpen(&bzerror, ttyrec, 9, 0, 0);
assert(bzerror == BZ_OK);
}
#endif
nle->observation = obs;
TMT *vterminal = tmt_open(LI, CO, nle_vt_callback, nle, NULL, true);
assert(vterminal);
nle->vterminal = vterminal;
nle->outbuf_write_ptr = nle->outbuf;
nle->outbuf_write_end = nle->outbuf + sizeof(nle->outbuf);
return nle;
}
nle_settings settings;
/* TODO: Consider copying the relevant parts of main() in unixmain.c. */
void
mainloop(fcontext_transfer_t ctx_transfer)
{
current_nle_ctx->returncontext = ctx_transfer.ctx;
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
/* ASan isn't happy with fcontext's assembly.
* See: https://bugs.llvm.org/show_bug.cgi?id=27627 and
* https://github.com/boostorg/coroutine/issues/30#issuecomment-325578344
* TODO: I don't understand why __sanitizer_(start/finish)_switch_fiber
* doesn't work here.
*/
fcontext_stack_t *stack = ¤t_nle_ctx->stack;
ASAN_UNPOISON_MEMORY_REGION((char *) stack->sptr - stack->ssize,
stack->ssize);
#endif
size_t len = strnlen(settings.hackdir, sizeof(settings.hackdir));
if (len == 0) {
error("HACKDIR must not be empty");
return;
}
if (len >= sizeof(settings.hackdir) - 1) {
error("HACKDIR too long");
return;
}
if (settings.hackdir[len - 1] != '/') {
settings.hackdir[len] = '/';
settings.hackdir[len + 1] = '\0';
} else {
settings.hackdir[len] = '\0';
}
char *scoreprefix = (settings.scoreprefix[0] != '\0')
? settings.scoreprefix
: settings.hackdir;
fqn_prefix[SYSCONFPREFIX] = settings.hackdir;
fqn_prefix[CONFIGPREFIX] = settings.hackdir;
fqn_prefix[HACKPREFIX] = settings.hackdir;
fqn_prefix[SAVEPREFIX] = settings.hackdir;
fqn_prefix[LEVELPREFIX] = settings.hackdir;
fqn_prefix[BONESPREFIX] = settings.hackdir;
fqn_prefix[SCOREPREFIX] = scoreprefix;
fqn_prefix[LOCKPREFIX] = settings.hackdir;
fqn_prefix[TROUBLEPREFIX] = settings.hackdir;
fqn_prefix[DATAPREFIX] = settings.hackdir;
char *argv[1] = { "nethack" };
unixmain(1, argv);
}
boolean
write_ttyrec_data(void *buf, int length)
{
nle_ctx_t *nle = current_nle_ctx;
#ifdef NLE_BZ2_TTYRECS
int bzerror;
BZ2_bzWrite(&bzerror, nle->ttyrec_bz2, buf, length);
assert(bzerror == BZ_OK);
#else
assert(fwrite(buf, 1, length, nle->ttyrec) == length);
#endif
return TRUE;
}
boolean
write_ttyrec_header(int length, unsigned char channel)
{
struct timeval tv;
gettimeofday(&tv, NULL);
int buffer[3];
buffer[0] = tv.tv_sec;
buffer[1] = tv.tv_usec;
buffer[2] = length;
/* Assumes little endianness */
write_ttyrec_data(buffer, 3 * sizeof(int));
write_ttyrec_data(&channel, 1);
return TRUE;
}
/* win/tty only calls fflush(stdout). */
int
nle_fflush(FILE *stream)
{
/* Only act on fflush(stdout). */
if (stream != stdout) {
fprintf(stderr,
"Warning: nle_flush called with unexpected FILE pointer %p ",
stream);
return fflush(stream);
}
nle_ctx_t *nle = current_nle_ctx;
ssize_t length = nle->outbuf_write_ptr - nle->outbuf;
if (length == 0)
return 0;
if (nle->ttyrec) {
write_ttyrec_header(length, 0);
write_ttyrec_data(nle->outbuf, length);
}
nle_obs *obs = nle->observation;
if (obs->tty_chars || obs->tty_colors || obs->tty_cursor) {
tmt_write(nle->vterminal, nle->outbuf, length);
}
nle->outbuf_write_ptr = nle->outbuf;
#ifdef NLE_BZ2_TTYRECS
return 0;
#else
return nle->ttyrec ? fflush(nle->ttyrec) : 0;
#endif
}
/*
* NetHack prints most of its output via putchar. We do our
* own buffering.
*/
int
nle_putchar(int c)
{
nle_ctx_t *nle = current_nle_ctx;
if (nle->outbuf_write_ptr >= nle->outbuf_write_end) {
nle_fflush(stdout);
}
*nle->outbuf_write_ptr++ = c;
return c;
}
/*
* Used in place of xputs from termcap.c. Not using
* the tputs padding logic from tclib.c.
*/
void
nle_xputs(const char *str)
{
int c;
const char *p = str;
if (!p || !*p)
return;
while ((c = *p++) != '\0') {
nle_putchar(c);
}
}
/*
* puts seems to be called only by tty_raw_print and tty_raw_print_bold.
* We could probably override this in winrl instead.
*/
int
nle_puts(const char *str)
{
if (!*str) /* At exit, an empty string gets printed in tty_raw_print. */
return 0;
int val = fputs(str, stdout);
putc('\n', stdout); /* puts includes a newline, fputs doesn't */
return val;
}
/* Necessary for initial observation struct. */
nle_obs *
nle_get_obs()
{
return current_nle_ctx->observation;
}
void *
nle_yield(void *notdone)
{
nle_fflush(stdout);
fcontext_transfer_t t =
jump_fcontext(current_nle_ctx->returncontext, notdone);
#if __has_feature(address_sanitizer) || defined(__SANITIZE_ADDRESS__)
fcontext_stack_t *stack = ¤t_nle_ctx->stack;
ASAN_UNPOISON_MEMORY_REGION((char *) stack->sptr - stack->ssize,
stack->ssize);
#endif
if (notdone)
current_nle_ctx->returncontext = t.ctx;
return t.data;
}
void
nethack_exit(int status)
{
if (status) {
fprintf(stderr, "NetHack exit with status %i\n", status);
}
nle_yield(NULL);
}
/* Called in really_done() in end.c to get "how". */
void
nle_done(int how)
{
nle_ctx_t *nle = current_nle_ctx;
nle->observation->how_done = how;
}
char *
nle_ttyrecname()
{
return settings.ttyrecname;
}
int
nle_spawn_monsters()
{
return settings.spawn_monsters;
}
char *
nle_getenv(const char *name)
{
if (strcmp(name, "TERM") == 0) {
return "ansi";
}
if (strcmp(name, "NETHACKOPTIONS") == 0) {
return settings.options;
}
/* Don't return anything for "SHOPTYPE" or "SPLEVTYPE". */
return (char *) 0;
}
FILE *
nle_fopen_wizkit_file()
{
size_t len = strnlen(settings.wizkit, sizeof(settings.wizkit));
if (!len) {
return (FILE *) 0;
}
return fmemopen(settings.wizkit, len, "r");
}
nle_ctx_t *
nle_start(nle_obs *obs, FILE *ttyrec, nle_settings *settings_p)
{
/* Set CO and LI to control ttyrec output size. */
CO = NLE_TERM_CO;
LI = NLE_TERM_LI;
settings = *settings_p;
nle_ctx_t *nle = init_nle(ttyrec, obs);
/* Initialise the level generation RNG */
nle_init_lgen_rng();
nle->stack = create_fcontext_stack(effective_stack_size());
nle->generatorcontext =
make_fcontext(nle->stack.sptr, nle->stack.ssize, mainloop);
current_nle_ctx = nle;
fcontext_transfer_t t = jump_fcontext(nle->generatorcontext, NULL);
nle->generatorcontext = t.ctx;
nle->done = (t.data == NULL);
obs->done = nle->done;
if (nle->ttyrec) {
if (obs->blstats) {
/* See comment in `nle_step`. We record the score in line with
* the state to ensure s,r -> a -> s', r'. These lines ensure
* we don't skip the first reward. */
write_ttyrec_header(4, 2);
write_ttyrec_data(&obs->blstats[9], 4);
}
}
return nle;
}
nle_ctx_t *
nle_step(nle_ctx_t *nle, nle_obs *obs)
{
current_nle_ctx = nle;
nle->observation = obs;
if (nle->ttyrec) {
write_ttyrec_header(1, 1);
write_ttyrec_data(&obs->action, 1);
}
fcontext_transfer_t t = jump_fcontext(nle->generatorcontext, obs);
nle->generatorcontext = t.ctx;
nle->done = (t.data == NULL);
obs->done = nle->done;
if (nle->ttyrec) {
/* NLE ttyrec version 3 stores the action and in-game score in
* different channels of the ttyrec. These channels are:
* - 0: the terminal instructions (classic ttyrec)
* - 1: the keypress/action (1 byte)
* - 2: the in-game score (4 bytes)
*
* We could either the note the in-game score every time we flush the
* terminal instructions to screen, (eg writing [ 0 2 0 2 <step> 1 0 2
* <step> 1 ]) or we can note it _just_ before resuming the game,
* assuming no chicanery has happened to the score after it is written
* to the array `blstats`, (eg writing [ 0 2 <step> 1 0 2 <step> 1 0 2
* <step> ]). We chose the latter for compression & simplicity
* reasons.
*
* Note: blstats[9] == botl_score which is used for score/reward fns.
* see winrl.cc
*/
if (obs->blstats) {
write_ttyrec_header(4, 2);
write_ttyrec_data(&obs->blstats[9], 4);
}
}
return nle;
}
void
nle_end(nle_ctx_t *nle)
{
if (!nle->done) {
/* Reset without closing nethack. Need free memory, etc.
* this is what nh_terminate in end.c does. I hope it's enough. */
if (!program_state.panicking) {
freedynamicdata();
dlb_cleanup();
}
}
nle_fflush(stdout);
#ifdef NLE_BZ2_TTYRECS
if (nle->ttyrec) {
int bzerror;
BZ2_bzWriteClose(&bzerror, nle->ttyrec_bz2, 0, NULL, NULL);
assert(bzerror == BZ_OK);
}
#endif
tmt_close(nle->vterminal);
destroy_fcontext_stack(&nle->stack);
free(nle);
}
/* From unixtty.c */
/* fatal error */
/*VARARGS1*/
void error
VA_DECL(const char *, s)
{
VA_START(s);
VA_INIT(s, const char *);
if (iflags.window_inited)
exit_nhwindows((char *) 0); /* for tty, will call settty() */
fprintf(stderr, s, VA_ARGS);
fprintf(stderr, "\n");
VA_END();
nethack_exit(EXIT_FAILURE);
}
/* From unixtty.c */
char erase_char, intr_char, kill_char;
void
gettty()
{
/* Should set erase_char, intr_char, kill_char */
}
void
settty(const char *s)
{
end_screen();
if (s)
raw_print(s);
}
void
setftty()
{
start_screen();
iflags.cbreak = ON;
iflags.echo = OFF;
}
void
intron()
{
}
void
introff()
{
}
#ifdef __linux__ /* via Jesse Thilo and Ben Gertzfield */
#include <sys/ioctl.h>
#include <sys/vt.h>
int linux_flag_console = 0;
void NDECL(linux_mapon);
void NDECL(linux_mapoff);
void NDECL(check_linux_console);
void NDECL(init_linux_cons);
void
linux_mapon()
{
#ifdef TTY_GRAPHICS
if (WINDOWPORT("tty") && linux_flag_console) {
write(1, "\033(B", 3);
}
#endif
}
void
linux_mapoff()
{
#ifdef TTY_GRAPHICS
if (WINDOWPORT("tty") && linux_flag_console) {
write(1, "\033(U", 3);
}
#endif
}
void
check_linux_console()
{
struct vt_mode vtm;
if (isatty(0) && ioctl(0, VT_GETMODE, &vtm) >= 0) {
linux_flag_console = 1;
}
}
void
init_linux_cons()
{
#ifdef TTY_GRAPHICS
if (WINDOWPORT("tty") && linux_flag_console) {
atexit(linux_mapon);
linux_mapoff();
#ifdef TEXTCOLOR
/*if (has_colors())*/ /* Assume true in NLE. */
iflags.use_color = TRUE;
#endif
}
#endif
}
#endif /* __linux__ */