Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect to nethack from Node.js?

I'm just starting an AI bot for the game nethack, and I can't bypass the 'human-check' that's in source. The section of code I'm talking about is nethack/sys/unix/unixunix.c:

#ifdef TTY_GRAPHICS
    /* idea from rpick%[email protected]
     * prevent automated rerolling of characters
     * test input (fd0) so that tee'ing output to get a screen dump still
     * works
     * also incidentally prevents development of any hack-o-matic programs
     */
    /* added check for window-system type -dlc */
    if (!strcmp(windowprocs.name, "tty"))
        if (!isatty(0))
        error("You must play from a terminal.");
#endif

I'm working in JavaScript, (more specifically Node.js), and due to the above, it won't let me play from the program, even though I'm spawning a bash shell child process and telling it to start nethack. I need to figure out a way to bypass the above without recompiling the source.

The current code I'm using is:

"use strict";

var env = { TERM: 'tty' };
for (var k in process.env) {
    env[k] = process.env[k];
}

var terminal = require('child_process').spawn('bash', [], {
    env: env,
});

terminal.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
});

terminal.on('exit', function (code) {
        console.log('child process exited with code ' + code);
});

setTimeout(function() {
    terminal.stdin.write('nethack');
    terminal.stdin.end();
}, 1000);

The output of the program is:

stdout: You must play from a terminal.

child process exited with code 1

What Node.js/JavaScript (and not any other language or framework, if possible) black magic could I use to solve this problem?

like image 946
chrisdotcode Avatar asked Mar 22 '12 02:03

chrisdotcode


1 Answers

That's kind of a lame check because ptys will return true in isatty(). Pty stands for Pseudo terminal which allows a program to pretend to be a terminal. This is how Xterm and Screen work. If that check didn't allow those programs through you wouldn't be able to play NetHack in them.

I've never used it but pty.js binds to exactly what you would use in C code and the interface makes sense.

like image 152
Jeffery Grajkowski Avatar answered Sep 28 '22 09:09

Jeffery Grajkowski