Skip to content
codeswithrohPublic

About

Real-time battle royale with true fog of war, sealed inside MagicBlock's Private Ephemeral Rollup on Solana

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

ย 

History

11 Commits

Folders and files

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

Repository files navigation

Nightfall title screen: a blocky hero by a campfire in a moonlit arena

๐ŸŒ™ Nightfall

A real-time battle royale with true fog of war, where even the server can't see you.

Every player's position is sealed inside MagicBlock's Private Ephemeral Rollup (an Intel TDX enclave). Your wallet can read your own line of sight and nothing else.

Play now Program on Solana devnet

MagicBlock Anchor Rust TypeScript three.js Tests License

Play ยท Watch it ยท How it works ยท Run locally


๐Ÿ“– Table of contents


๐Ÿ’ก The idea

Onchain games have always been played with the cards face up. Every account is public, so any "hidden" information (where your opponent is, what's in their hand) can be read straight from the RPC. That's why fog of war, the backbone of strategy games and battle royales, has been nearly impossible onchain. Dark Forest needed ZK circuits and ran at a crawl.

Nightfall fixes it with MagicBlock's Private Ephemeral Rollups. All positions live in a World account sealed inside a TEE with zero readers. Every move, the program recomputes what each player is allowed to see and writes it to a per-player Vision account that only that player's wallet can read. The result is a real-time, blocky battle royale where you see only what your torch lights, and nobody can cheat their way past that, the host included.

Try to cheat: press X in a match. The X-ray panel queries the TEE with your own token: World โ†’ null, Opponent's Vision โ†’ null, Your Vision โ†’ 155 bytes.


๐ŸŽฌ See it in action

Gameplay: walking through the dark arena, spotting an enemy in the torchlight, fighting
A real devnet match recorded headlessly against MagicBlock's TEE. Every step and swing is a transaction.

Warm-up yard
Warm-up yard. Learn every control on a training dummy while the arena is set up onchain.
Fog of war fight
Fog of war. Only what your torch reaches. Red arcs = footsteps you hear but can't see.
X-ray panel
Try to cheat. The X-ray asks the TEE for the World and other players' Visions: null.
Spectator broadcast following a player
Spectate. Every player, on a short broadcast delay so nobody can feed positions to a player.
Daylight match on Frostpeak
Night or day. Daylight arenas see further, but walls and tall grass still hide you.
Reveal the night
Reveal the night. When it ends, the World unseals and anyone can verify what really happened.
Frostpeak Ruins by day
Three arenas. Moonlit Woods, Frostpeak Ruins and Ember Keep, each with its own blocks, light and layout.
Ember Keep
Your hero. Six original skins, hats and capes, stored onchain in one byte.
Mobile title screen
Plays on phones too: tap to walk, on-screen attack, bow and power buttons.

โœจ Features

Gameplay

  • โš”๏ธ Up to 6 players (humans or bots) in a shrinking-border battle royale. Last one standing takes the onchain pot.
  • ๐Ÿ”ฆ Three-layer fog of war: what you see (torch radius + line of sight), what you remember (dimmed terrain), and what you hear (the TEE tells you the direction of unseen enemies within 8 tiles, never their position).
  • ๐ŸŒพ Tall grass hides you unless someone is right next to you; stone and trees block sight; arrows fly over water.
  • ๐Ÿงฐ 10 loot chests: Diamond Sword, Battle Axe (9 dmg, slow), War Hammer (knockback), Spear (reach 2), Golden Apples, arrows, and powers: Ender Pearl (blink over walls), Cloak (invisible + silent), Flare (far sight).
  • ๐Ÿ—บ๏ธ Three arenas ร— night or day. Spawns fill the corners first, plus 10 s of spawn protection.
  • ๐Ÿค– Bots with personalities (archer / brawler / mixed) that play from their own sealed Vision, so they can't cheat either.

Feel & onboarding

  • โ–ถ๏ธ One button to play. No wallet popups: a devnet burner lives in the browser.
  • ๐Ÿ•๏ธ Warm-up yard while the chain sets up: four quick quests teach every control by doing them, and you choose when to drop in.
  • ๐Ÿ’ฌ Just-in-time coaching: a single tip the first time you see a chest, an enemy, footsteps, or the border.
  • ๐ŸŽฎ Game feel: hit-stop, damage numbers, screen shake, kill streaks, heartbeat at low health, countdown beeps, synthesized night ambience and stereo footsteps. Zero asset files: every block, skin, icon and sound is generated in code.
  • ๐Ÿ‘๏ธ Spectate without a code: an onchain registry lists recent arenas, live first; follow any player with โ†/โ†’.

๐ŸŽฎ How to play

Key Action
WASD / arrows, or click the ground Move (click-to-walk pathfinds for you)
Space Attack: auto-aims. Melee if someone's in reach, bow if they're lined up
F Shoot the bow straight ahead
E Use your power (Pearl / Cloak / Flare)
H ยท X ยท M Help ยท X-ray (try to cheat) ยท Mute

Rules: you only see what your torch lights โ†’ loot chests โ†’ the border waits 60 s then closes a tile every 14 s โ†’ outside it you burn โ†’ last one standing wins.


๐Ÿง  How it works

Architecture

flowchart LR
  subgraph Browser["Player's browser"]
    UI["three.js voxel client + burner wallet"]
  end
  subgraph Solana["Solana devnet - base layer"]
    P["Nightfall program - Anchor 1.0"]
    V["Vault PDA - stakes, never delegated"]
    R["Registry PDA - recent arenas"]
  end
  subgraph TEE["MagicBlock Private ER - Intel TDX"]
    A["Arena - public scoreboard, border, feed"]
    W["World - sealed, NO readers"]
    VI["Vision per player - readable only by its owner"]
    B["Broadcast - public, about 10 s delayed"]
  end
  CR["MagicBlock crank"]
  VRF["MagicBlock VRF"]
  UI -->|create, join, delegate| P
  UI -->|act: move, attack, bow, power| W
  W -->|recomputed every tx| VI
  VI -->|auth-token RPC and websocket| UI
  CR -->|tick: border and broadcast| A
  VRF -->|spawn callback| W
  A -->|commit and undelegate| P
  P -->|claim| V
Loading

Life of a match

sequenceDiagram
  autonumber
  participant H as Host (you)
  participant S as Solana
  participant T as TEE rollup
  participant O as VRF oracle
  H->>S: create_arena (map, day/night) + delegate own Vision
  Note over H: warm-up yard starts: walk, hit, shoot, blink
  H->>S: 3 bots join + delegate their Visions
  H->>S: delegate Arena, World, Broadcast
  H->>T: init_permission ร—N: World=sealed, Vision[i]=player i, Arena/Broadcast=public
  H->>T: request_spawn (refused unless everything is provably sealed)
  O-->>T: spawn_callback(randomness) โ†’ positions written inside the enclave
  loop every move (~100 ms)
    H->>T: act(...) โ†’ rules run โ†’ every Vision rewritten
  end
  T->>T: crank: tick โ†’ border shrinks, broadcast snapshot
  H->>T: reveal (World โ†’ public) ยท settle (commit + undelegate)
  H->>S: claim โ†’ winner paid from the vault
Loading

Why it can't be cheated

Threat Defence
Read positions from the RPC World has an EphemeralPermission with no members; the TEE returns null to everyone, host included
Grant yourself access to someone's Vision init_permission ignores caller input: the program decides members (Vision โ†’ its owner only)
Spawn on the public base layer to learn positions request_spawn / spawn_fallback parse every permission account and refuse unless World and all Visions are sealed
Blind an opponent by omitting their Vision every act/tick must pass all Visions in slot order
Leak through logs no instruction ever logs a position
Spectate your own match to see everyone spectators get a public broadcast written from snapshots ~10 s old, like an esports delay
Rig the spawn spawns come from MagicBlock VRF inside the rollup (TEE-mixed entropy fallback only if the oracle stalls)

MagicBlock primitives used

Primitive Where
Ephemeral Rollup Every step, swing, arrow and power is its own gasless transaction (~100 ms round trip)
Private ER (TEE) Sealed World, per-player Vision, public Arena/Broadcast via EphemeralPermission CPIs
VRF request_spawn โ†’ spawn_callback writes spawns and chest loot straight into the sealed World
Crank / scheduler schedule_ticks puts the border and broadcast on the rollup's scheduler (clients tick as a fallback)
Commit / undelegate settle brings the result back to Solana; claim pays the pot from a never-delegated vault

๐Ÿ›  Tech stack

Layer Tech
Program Rust 1.89 ยท Anchor 1.0.2 ยท ephemeral-rollups-sdk 0.17.3 (access-control, VRF, crank) ยท Agave 3.1.9
Client TypeScript ยท three.js ยท Vite ยท @magicblock-labs/ephemeral-rollups-sdk ยท @solana/web3.js ยท Anchor TS
Art & audio Procedural pixel textures, voxel characters and items, WebAudio synthesis; no asset files
Infra Solana devnet ยท MagicBlock devnet TEE (devnet-tee.magicblock.app) ยท Vercel

๐Ÿš€ Run it yourself

Just play: https://nightfall-blitz.vercel.app. Press PLAY; if your burner needs devnet SOL, the game walks you through the faucet.

Run the client locally

git clone https://github.com/codeswithroh/nightfall && cd nightfall/app
npm install
npm run dev            # http://localhost:5173, talks to the deployed devnet program

Fill a lobby from the terminal (bots funded from ~/.config/solana/id.json)

cd app && npm run guest -- <arena code> 3

Build, test and deploy the program

# toolchain: Agave 3.1.9, Anchor 1.0.2, Rust 1.89 (rust-toolchain.toml pins it)
anchor build
cargo test --manifest-path programs/nightfall/Cargo.toml    # 12 rule tests
anchor deploy --provider.cluster devnet
cd app && npm run sync-idl

โœ… Tests & verification

  • 12 rule tests (programs/nightfall/src/logic.rs): line of sight, tall grass, hearing octants, border timing, spawn spread and protection, every weapon (axe damage + cooldown, hammer knockback stopped by walls, spear reach), chest loot, every power (pearl blinks over walls, cloak hides even adjacent players until you attack, flare range), daylight sight, broadcast delay, all three maps.
  • Devnet end-to-end (app/tests/e2e.ts): two players, real TEE, full lifecycle.
match live, vrf = true
guest peeks host vision โ†’ null โœ… sealed
guest peeks world โ†’ null โœ… sealed
host peeks world โ†’ null โœ… sealed (even the host)
spectator reads public arena โ†’ ok โœ…
player 1 spotted player 0 through the fog at step 18
status 3 winner slot 0 kills [ 1, 0 ]
after reveal, guest reads world โ†’ visible โœ…
winner received 0.02 SOL
โœ… e2e complete
  • Played in the browser end to end: title โ†’ warm-up quests โ†’ drop in โ†’ loot โ†’ fight โ†’ win โ†’ reveal โ†’ settle โ†’ paid, plus a second tab spectating the same match. The previews above were captured from real devnet matches.

๐Ÿ“ Project structure

nightfall/
โ”œโ”€โ”€ programs/nightfall/src/
โ”‚   โ”œโ”€โ”€ lib.rs        # instructions: lobby, delegation, permissions, VRF spawn, act, tick, crank, reveal, settle, claim
โ”‚   โ”œโ”€โ”€ logic.rs      # pure rules: fog, hearing, weapons, powers, border, broadcast (+ tests)
โ”‚   โ”œโ”€โ”€ state.rs      # Arena ยท World ยท Vision ยท Broadcast ยท Registry
โ”‚   โ””โ”€โ”€ map.rs        # generated by scripts/gen-map.py (3 symmetric, connectivity-checked arenas)
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ src/lib/      # NightfallClient: base + TEE connections, auth tokens, reliable send/confirm
โ”‚   โ”œโ”€โ”€ src/game/     # scene, voxel characters, session (HUD, prediction, spectating), warm-up yard, bots, sfx
โ”‚   โ””โ”€โ”€ tests/        # e2e.ts (devnet + TEE), guest.ts (terminal bots)
โ”œโ”€โ”€ scripts/gen-map.py
โ””โ”€โ”€ docs/media/       # README previews

๐Ÿ—บ Roadmap

  • Session keys + Phantom/Backpack for real-wallet play (burner stays as the instant option)
  • Server-side bots so matches keep going if the host closes their tab
  • Ranked seasons with an onchain leaderboard (Magic Actions on settle)
  • Squads (2v2v2) with shared team Visions, a natural fit for permission groups
  • Replays from the revealed World + move history

๐Ÿ™Œ Credits & license

Built for the MagicBlock Solana Blitz hackathon on MagicBlock's Ephemeral Rollups, Private Ephemeral Rollups, VRF and crank scheduler. All art and audio are procedurally generated. The blocky look is a tribute to classic block games; the characters, items and arenas are original.

Released under the MIT License.

About

Real-time battle royale with true fog of war, sealed inside MagicBlock's Private Ephemeral Rollup on Solana

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages