-
Notifications
You must be signed in to change notification settings - Fork 113
Fix terminal size at 80x24, write bzip2'ed ttyrecs #97
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -139,7 +139,7 @@ | |||||||||||||
#define TIMED_DELAY | ||||||||||||||
#endif | ||||||||||||||
|
||||||||||||||
/* #define AVOID_WIN_IOCTL */ /* ensure USE_WIN_IOCTL remains undefined */ | ||||||||||||||
#define AVOID_WIN_IOCTL /* ensure USE_WIN_IOCTL remains undefined */ | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment could probably be added to, to help future clarity.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. formatting of the comment can obvs change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see where you're coming from, but I'd like to stick with the policy of not unduly changing the original NetHack source files, and this is one of them. |
||||||||||||||
|
||||||||||||||
/* | ||||||||||||||
* If you define MAIL, then the player will be notified of new mail | ||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,7 +201,7 @@ def __init__( | |
|
||
if self.savedir: | ||
self._ttyrec_pattern = os.path.join( | ||
self.savedir, "nle.%i.%%i.ttyrec" % os.getpid() | ||
self.savedir, "nle.%i.%%i.ttyrec.bz2" % os.getpid() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pid is always going to be unique within the folder? Are we saving all ttyrecs to there or only the last N? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently we are either saving all ttyrecs or none. The pid is going to be the process id of the user's process which won't be reassigned until that process stops. |
||
) | ||
ttyrec = self._ttyrec_pattern % 0 | ||
else: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
|
||
#include <assert.h> | ||
#include <sys/time.h> | ||
|
||
#include <string.h> | ||
#include <sys/time.h> | ||
|
||
#define NEED_VARARGS | ||
#include "hack.h" | ||
|
@@ -11,6 +10,10 @@ | |
|
||
#include "nle.h" | ||
|
||
#ifdef NLE_BZ2_TTYRECS | ||
#include <bzlib.h> | ||
#endif | ||
|
||
#define STACK_SIZE (1 << 15) // 32KiB | ||
|
||
#ifndef __has_feature | ||
|
@@ -31,6 +34,12 @@ init_nle(FILE *ttyrec) | |
assert(ttyrec != NULL); | ||
nle->ttyrec = ttyrec; | ||
heiner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#ifdef NLE_BZ2_TTYRECS | ||
int bzerror; | ||
nle->ttyrec_bz2 = BZ2_bzWriteOpen(&bzerror, ttyrec, 9, 0, 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the 9? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The block size. 9 is the highest and also what alt.org uses. http://www.cs.cmu.edu/afs/cs/project/pscico-guyb/realworld/99/code/bzip2-0.9.5c/manual_3.html#SEC30 |
||
assert(bzerror == BZ_OK); | ||
#endif | ||
|
||
nle->outbuf_write_ptr = nle->outbuf; | ||
nle->outbuf_write_end = nle->outbuf + sizeof(nle->outbuf); | ||
|
||
|
@@ -59,6 +68,20 @@ mainloop(fcontext_transfer_t ctx_transfer) | |
unixmain(1, argv); | ||
} | ||
|
||
boolean | ||
write_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) == 0); | ||
#endif | ||
return TRUE; | ||
} | ||
|
||
boolean | ||
write_header(int length, unsigned char channel) | ||
{ | ||
|
@@ -70,18 +93,9 @@ write_header(int length, unsigned char channel) | |
buffer[1] = tv.tv_usec; | ||
buffer[2] = length; | ||
|
||
nle_ctx_t *nle = current_nle_ctx; | ||
|
||
/* Assumes little endianness */ | ||
if (fwrite(buffer, sizeof(int), 3, nle->ttyrec) == 0) { | ||
assert(FALSE); | ||
return FALSE; | ||
} | ||
|
||
if (fputc((int) channel, nle->ttyrec) != (int) channel) { | ||
assert(FALSE); | ||
return FALSE; | ||
} | ||
write_data(buffer, 3 * sizeof(int)); | ||
write_data(&channel, 1); | ||
|
||
return TRUE; | ||
} | ||
|
@@ -102,12 +116,17 @@ nle_fflush(FILE *stream) | |
ssize_t length = nle->outbuf_write_ptr - nle->outbuf; | ||
if (length == 0) | ||
return 0; | ||
/* TODO(heiner): Given that we do our own buffering, consider | ||
* using file descriptors instead of the ttyrec FILE*. */ | ||
|
||
write_header(length, 0); | ||
fwrite(nle->outbuf, 1, length, nle->ttyrec); | ||
write_data(nle->outbuf, length); | ||
|
||
nle->outbuf_write_ptr = nle->outbuf; | ||
|
||
#ifdef NLE_BZ2_TTYRECS | ||
return 0; | ||
#else | ||
return fflush(nle->ttyrec); | ||
#endif | ||
} | ||
|
||
/* | ||
|
@@ -219,6 +238,10 @@ init_random(int FDECL((*fn), (int) )) | |
nle_ctx_t * | ||
nle_start(nle_obs *obs, FILE *ttyrec, nle_seeds_init_t *seed_init) | ||
{ | ||
/* Set CO and LI to control ttyrec output size. */ | ||
CO = 80; | ||
LI = 24; | ||
|
||
nle_ctx_t *nle = init_nle(ttyrec); | ||
nle->observation = obs; | ||
nle_seeds_init = seed_init; | ||
|
@@ -244,7 +267,7 @@ nle_step(nle_ctx_t *nle, nle_obs *obs) | |
current_nle_ctx = nle; | ||
nle->observation = obs; | ||
write_header(1, 1); | ||
fputc(obs->action, nle->ttyrec); | ||
write_data(&obs->action, 1); | ||
fcontext_transfer_t t = jump_fcontext(nle->generatorcontext, obs); | ||
nle->generatorcontext = t.ctx; | ||
nle->done = (t.data == NULL); | ||
|
@@ -256,7 +279,6 @@ nle_step(nle_ctx_t *nle, nle_obs *obs) | |
void | ||
nle_end(nle_ctx_t *nle) | ||
{ | ||
nle_fflush(stdout); | ||
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. */ | ||
|
@@ -265,6 +287,13 @@ nle_end(nle_ctx_t *nle) | |
dlb_cleanup(); | ||
} | ||
} | ||
nle_fflush(stdout); | ||
|
||
#ifdef NLE_BZ2_TTYRECS | ||
int bzerror; | ||
BZ2_bzWriteClose(&bzerror, nle->ttyrec_bz2, 0, NULL, NULL); | ||
assert(bzerror == BZ_OK); | ||
#endif | ||
|
||
destroy_fcontext_stack(&nle->stack); | ||
free(nle); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious: why aren't we doing ^^^ ? We never write to the file if the macro is set. Do we not just run the risk of confusion by keeping this pointer around?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. Who exactly is responsible for this
FILE
is a bit of a tricky question. For now, I'd like to keep it around for debugging and to allow flushing (although we don't do that currently).