Sitelet https://github.com/oosawak/NetHack-rust
Skip to content
ย 
ย 

Latest commit

ย 

History

18,818 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

NetHack Rust + WASM + wgpu

๐Ÿš€ Active Development | Phase 6.2 Complete: WASM Browser Demo Ready

โš ๏ธ ้–‹็™บไธญ / Work In Progress

ใ“ใฎใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆใฏ็พๅœจๆดป็™บใซ้–‹็™บไธญใงใ™ใ€‚ๆฉŸ่ƒฝใฏ้šๆ™‚่ฟฝๅŠ ใƒปๅค‰ๆ›ดใ•ใ‚Œใพใ™ใ€‚ This project is under active development. Features are added and changed frequently.


๐ŸŽฏ Project Vision

  • โœ… Reuse Existing C Code โ€” Stable game logic, AI, dungeon generation
  • ๐ŸŽจ Modern Graphics โ€” wgpu 3D rendering with multiple view modes
  • ๐ŸŒ Cross-Platform โ€” Desktop (Linux/Mac/Windows), WebAssembly, Unity Plugin
  • ๐ŸŽฎ Incremental Build โ€” FFI โ†’ Game Bridge โ†’ Graphics โ†’ Multi-Platform

Target Platforms

Platform Status Estimated Phase
Desktop (Linux/Mac/Windows) ๐Ÿ”„ In Progress (Phase 5) Phase 5.4
WebAssembly (Browser) โœ… Build Complete (Phase 6) Phase 6.2
Unity Native Plugin ๐Ÿ“‹ Planned Phase 7

๐Ÿ“Š Implementation Progress

Phase 0: Workspace Setup                    โœ… DONE
Phase 1: FFI Bindings (nethack-sys)         โœ… DONE (139 C files linked)
Phase 2: Game Bridge                        โœ… DONE (Player state, Game logic)
Phase 3: C Globals & Game State             โœ… DONE (GameBridge, state mgmt)
Phase 4: Desktop Graphics Pipeline          โœ… DONE
  4.1: wgpu Rendering                       โœ… DONE (GPU setup, shaders, render pass)
  4.2: Game State โ†’ Vertices                โœ… DONE (Player cube, dungeon floor)
  4.3: Camera Integration (5 views)         โœ… DONE (TopDown, Isometric, etc.)
  4.4: Input System                         โœ… DONE (Arrow keys โ†’ movement)
Phase 5: Dungeon Entity Rendering           โœ… DONE
  5.0: Infrastructure                       โœ… DONE (FFI wrappers, renderer stubs)
  5.1: Fix Linker & Enable Monster Render   โœ… DONE (svl extern, static lib, wrappers)
  5.2: Item Rendering                       โœ… DONE (Cyan cubes, OBJ_FLOOR enumeration)
  5.3: Dungeon Features (Traps & Stairs)    โœ… DONE (Purple traps, green/blue stairs)
Phase 6: WASM Build & Browser Demo          โœ… DONE
  6.1: WASM Target Setup                    โœ… DONE (wasm-bindgen, wasm-pack build)
  6.2: Browser Example & Server             โœ… DONE (examples/wasm.html, run_server.sh)
  6.3: Local Testing Environment            โœ… DONE (Auto-detecting HTTP server)
Phase 7: Unity Plugin (cdylib)              ๐Ÿ“‹ Planned

Component Status

Component Status Details
nethack-sys โœ… Complete FFI bindings, C wrapper functions, libnhmain integration
nethack-core โœ… Complete Camera (5 modes), Input, GameRenderer, Monster render enabled
nethack-render โœ… Complete wgpu pipeline, WGSL shaders
nethack-desktop ๐Ÿ”„ Active winit + wgpu, input handling, monster/item rendering
nethack-wasm โœ… Complete WASM build (28KB binary), wasm-bindgen API, no C library
Tests โœ… 17 Passing camera, input, game_renderer, world, all passing release/debug

๐Ÿš€ Quick Start

Build Requirements

# Rust (1.70+)
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Build tools (platform-specific)
# Linux:   sudo apt install build-essential
# macOS:   xcode-select --install
# Windows: Visual Studio Build Tools

Build & Run

# Clone and enter project
git clone https://github.com/oosawak/NetHack.git
cd NetHack

# Build all crates
cargo build

# Run desktop app
cargo run -p nethack-desktop

# Run tests
cargo test -p nethack-core

Build for WebAssembly (WASM)

# Run setup script to install wasm-pack and related tools
./setup_wasm.sh

# Build WASM module
wasm-pack build crates/nethack-wasm --target web --release

# Output: crates/nethack-wasm/pkg/
#   - nethack_wasm.wasm (28KB binary)
#   - nethack_wasm.js (JavaScript bindings)
#   - nethack_wasm.d.ts (TypeScript types)
#   - package.json (npm package)

Note: The WASM build uses Rust-only game logic (no C library). It's ideal for quick browser testing and deployment.

Controls

  • Arrow Keys โ€” Move player (โ†‘โ†“โ†โ†’)
  • 1-5 Keys โ€” Switch camera view
    • 1 = TopDown
    • 2 = Isometric
    • 3 = FirstPerson
    • 4 = ThirdPerson
    • 5 = Cinematic
  • Q โ€” Quit

๐Ÿ—๏ธ Architecture

Desktop (C library):
libnetHack.a (C library)
    โ†“ FFI
nethack-sys (auto-generated bindings)
    โ†“
nethack-core (Game layer: camera, input, rendering logic)
    โ†“
nethack-render (Graphics layer: wgpu + WGSL)
    โ†“
nethack-desktop (Desktop: winit event loop)

WASM (Rust-only):
nethack-core (Rust game logic - no C library)
    โ†“
nethack-wasm (wasm-bindgen JavaScript API)
    โ†“
Browser (WebAssembly binary + JavaScript)

Rendering Pipeline

  1. Input: winit โ†’ KeyCode โ†’ Key โ†’ GameCommand
  2. Update: execute_command() โ†’ player position
  3. Render: GameRenderer.update_from_game_state()
  4. GPU: WgpuRenderer.render() โ†’ wgpu RenderPass
  5. Frame: Output to window

Entity Types

  • Player: Yellow cube at (ux, uy)
  • Dungeon: Gray tiles (10ร—10 visible radius)
  • Monsters: Red (hostile) / Yellow (peaceful) cubes, auto-rendered from C library
  • Items: Cyan cubes (infrastructure in place, stub implementation)

๐Ÿ“ Project Structure

crates/
โ”œโ”€โ”€ nethack-sys/       # FFI layer (bindgen + C wrappers)
โ”œโ”€โ”€ nethack-core/      # Game logic (camera, input, rendering)
โ”œโ”€โ”€ nethack-render/    # Graphics (wgpu + WGSL shaders)
โ”œโ”€โ”€ nethack-desktop/   # Desktop app (winit + event loop)
โ”œโ”€โ”€ nethack-wasm/      # WASM target (planned)
โ””โ”€โ”€ nethack-unity/     # Unity plugin (planned)

docs/
โ”œโ”€โ”€ ARCHITECTURE.md    # Detailed technical design
โ”œโ”€โ”€ RENDERING_STRATEGY.md
โ””โ”€โ”€ FFI_DESIGN.md

๐ŸŽฎ Current Capabilities

โœ… Working:

  • wgpu rendering pipeline with proper GPU setup
  • WGSL vertex/fragment shaders
  • Player position tracking from C library
  • 5 camera view modes with real-time switching
  • Arrow key movement with boundary checking
  • Game state updates each frame
  • Proper window management with winit
  • Input โ†’ GameCommand โ†’ execution flow
  • Monster rendering from C library (red/yellow colored cubes)
  • Monster enumeration via safe FFI wrapper (get_monster_count/by_index)
  • Item rendering from C library (cyan colored cubes)
  • Item enumeration via safe FFI wrapper (get_object_count/by_index, OBJ_FLOOR filter)
  • Trap rendering from C library (purple cubes, tiny size)
  • Trap enumeration via safe FFI wrapper (get_trap_count/by_index)
  • Stairway rendering from C library (green=up, blue=down)
  • Stairway enumeration via safe FFI wrapper (get_stair_count/by_index)

๐Ÿ”„ In Progress:

  • Game turn cycle integration
  • More dungeon features (doors, etc.)

๐Ÿ“‹ Planned:

  • WASM target for browser play
  • Unity native plugin
  • More game entities (doors, etc.)
  • Sound and music
  • Save/load game state
  • UI overlays (inventory, status)

๐Ÿ”— Key References

  • NetHack Sources: /home/oosawak/Workspace/NetHack/src/ (C files)
  • FFI Wrapper: crates/nethack-sys/wrapper.{c,h}
  • Rendering: crates/nethack-render/src/renderer.rs
  • Desktop: crates/nethack-desktop/src/main.rs
  • Architecture: See ARCHITECTURE.md for full technical details

๐Ÿ“ Recent Changes

Phase 6.2: Browser Example & Local Server (Latest - Current)

  • โœ… Created examples/wasm.html with full-featured Canvas demo
    • Real-time vertex rendering from WASM game state
    • FPS counter and player position tracking
    • Camera mode switching buttons (1-5 keys support)
    • Responsive sidebar with game stats
  • โœ… Implemented examples/run_server.sh auto-detecting HTTP server
    • Works with Python 3, Python 2, Node.js, or Ruby
    • Clear startup instructions with browser URL
    • Verifies WASM binary before starting
  • โœ… Created examples/verify.sh for environment validation
    • Checks WASM binary presence and size
    • Validates JavaScript bindings
    • Confirms all example files are in place
  • โœ… Created examples/README.md with user guide
    • Quick start instructions
    • Control mapping
    • Camera mode descriptions
    • Troubleshooting guide

Ready to test: Users can now run ./examples/run_server.sh and open http://localhost:8000/examples/wasm.html in their browser!

Phase 6: WASM Build Complete

  • โœ… Setup wasm32-unknown-unknown target with wasm-pack
  • โœ… Created nethack-wasm crate with wasm-bindgen API
  • โœ… Conditional compilation: WASM uses Rust-only logic (no C library)
  • โœ… Implemented Game struct: init(), player_x/y, move_player(), render()
  • โœ… Vertex rendering: render() returns flat f32 array for JavaScript
  • โœ… Successfully built WASM binary (28KB) with wasm-pack
  • โ„น๏ธ Note: C library not compiled for WASM - pure Rust game logic only

Phase 5.3: Dungeon Features (Traps & Stairs)

  • โœ… Implemented trap enumeration from C library (gf.ftrap list)
  • โœ… Added safe FFI wrappers: get_trap_count(), get_trap_by_index()
  • โœ… Implemented stairway enumeration (gs.stairs list)
  • โœ… Added safe FFI wrappers: get_stair_count(), get_stair_by_index()
  • โœ… Traps render as tiny (0.15 size) purple cubes
  • โœ… Stairs render with direction distinction: green (up), blue (down)
  • โœ… Fixed FFI header conflicts with NetHack includes
  • โœ… All 17 tests passing, release binary 14MB

Phase 5.2: Item Rendering

  • โœ… Implemented item enumeration from C library
  • โœ… Added safe FFI wrappers: get_object_count(), get_object_by_index()
  • โœ… Items render as cyan cubes (distinct from monsters and player)
  • โœ… Proper OBJ_FLOOR filtering to show only dungeon floor items

Phase 5.1: Monster Rendering & Linker Fix

  • Fixed critical linker error by declaring extern struct instance_globals_saved_l svl
  • Resolved missing symbol issues via static library archive (libnetHack.a)
  • Implemented monster enumeration and rendering (red=hostile, yellow=peaceful)

๐Ÿ› ๏ธ Development Notes

Building the C Library

The build process automatically:

  1. Compiles 139 C files to libnetHack.a
  2. Generates FFI bindings via bindgen
  3. Links to all Rust crates

No manual C build needed - cargo build handles everything.

Adding Features

When adding new game features:

  1. Create C wrapper function in wrapper.c
  2. Update wrapper.h with declaration
  3. Add to build.rs allowlist
  4. Use safely in Rust code via FFI

Testing

# Run all tests
cargo test

# Run specific test
cargo test -p nethack-core camera::tests::test_camera_switch

# Run with output
cargo test -- --nocapture

๐Ÿ“ Recent Changes

Phase 5.1 - Fix Linker Error & Enable Monster Rendering:

  • Fixed undefined symbol svl linker error by declaring extern struct instance_globals_saved_l svl
  • Modified wrapper.c to directly access svl.level.monlist in get_monster_count/by_index
  • Implemented static library archive creation in build.rs for proper object file linking
  • Compiled sys/libnh/libnhmain.c with LIBNH flags to resolve symbol conflicts
  • Added wrapper functions for chdirx and whoami platform compatibility
  • All 17 tests passing in both debug and release modes
  • Release binary compiles to 14MB executable

Phase 5.0 - Monster Infrastructure:

  • Created C wrapper functions for monster enumeration
  • Extended GameRenderer with rendering methods
  • Prepared stubs for C library integration
  • Monster rendering code already enabled in game_renderer.rs

๐Ÿ’ก Notes

  • All work on master branch with regular commits
  • Tests must pass before committing
  • Build output is clean (warnings are cosmetic)
  • Release binary is 14MB, Debug binary is 160MB (includes all dependencies)
  • Monster rendering is enabled - all monsters in game will be rendered as colored cubes

๐Ÿ“„ License

NetHack is licensed under the NetHack General Public License. Rust code contributions follow the same license.


Want to contribute? See ARCHITECTURE.md for detailed technical overview and next steps!

About

Official NetHack Git Repository

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages