apowers313 / NetHackJS

The original NetHack rogue-like game built as a JavaScript WebAssembly module
Other
8 stars 0 forks source link

NetHack

This is the ACTUAL NetHack game, originally written in 1982 and one of the longest-standing open source collaborations. This isn't a wrapper around the binary NetHack, but the game itself compiled into WebAssembly (WASM) using emscripten. This module should run anywhere WebAssembly is supported including node.js and modern browsers.

Since NetHack typically uses the TTY to show depictions of the game and that won't work for WebAssembly, you are required to implement the graphics portion of NetHack to make this work. This allows a wide variety of UIs to be created, both text and graphics based as well as using keyboard and mouse to control the game. The API for implementing graphics is described below.

Install

npm install nethack

API

The main module returns a setup function: startNethack(uiCallback, moduleOptions).

There are a number of auxilary functions and variables that may help with building your applications. All of these are under globalThis.nethackOptions. Use console.log(globalThis.nethackOptions) for a full list of options. Some worth mentioning are:

Example

let nethackStart = require("nethack");

nethackStart(doGraphics);

let winCount = 0;
async function doGraphics(name, ... args) {
    console.log(`shim graphics: ${name} [${args}]`);

    switch(name) {
    case "shim_create_nhwindow":
        winCount++;
        console.log("creating window", args, "returning", winCount);
        return winCount;
    case "shim_yn_function":
    case "shim_message_menu":
        return 121; // return 'y' to all questions
    case "shim_nhgetch":
    case "shim_nh_poskey":
        return 0; // simulates a mouse click on "exit up the stairs"
    default:
        return 0;
    }
}

Other Notes