- command.c in full: the command dispatcher with repeat counts, run prefixes, ctrl-run door-stop mode, fight-to-death targeting, and all wizard debug commands; search, help, identify, d_level/u_level, call, current - main.c completed: Run()/playit() with the C double ROGUEOPTS parse; exit()-anywhere becomes a gameEnd panic recovered in Run - save.c + state.c: gob SaveState snapshot with explicit pointer-to- index fixups for equipment, monster rooms, and chase targets (the rs_fix_thing role); save file deleted on restore as in C; SIGHUP/ SIGTERM autosave hook - wizard.c completed (create_obj, show_map), passages.c add_pass - term/: tcell/v2 Terminal — the one third-party dependency — replacing curses and mdport's 900-line key decoder; shell escape via suspend - cmd/rogue: flags -s/-d, SEED (wizard), ROGUEOPTS, ROGUE_WIZARD Tests: full scripted sessions through Run (quit, descend stairs, 3200-move crash sweep, save-command round trip). Notable fixes found by tests: gob silently drops embedded fields of unexported types, and --More-- prompts swallow space-free input scripts.
102 lines
1.9 KiB
Go
102 lines
1.9 KiB
Go
// Command rogue is the Go port of Rogue 5.4.4: Exploring the Dungeons of
|
|
// Doom. It is a faithful function-by-function port of the classic C game;
|
|
// see ARCHITECTURE.md at the repository root.
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"os/user"
|
|
"strconv"
|
|
"syscall"
|
|
"time"
|
|
|
|
"sneak.berlin/go/rogue/game"
|
|
"sneak.berlin/go/rogue/term"
|
|
)
|
|
|
|
func main() {
|
|
scores := flag.Bool("s", false, "print the scoreboard and exit")
|
|
deathDemo := flag.Bool("d", false, "die a random death (demo)")
|
|
flag.Parse()
|
|
|
|
home, _ := os.UserHomeDir()
|
|
|
|
// get options from environment (main.c)
|
|
rogueOpts := os.Getenv("ROGUEOPTS")
|
|
name := ""
|
|
if u, err := user.Current(); err == nil {
|
|
name = u.Username
|
|
}
|
|
|
|
wizard := os.Getenv("ROGUE_WIZARD") != ""
|
|
// dungeon number: SEED for reproducible dungeons (wizard mode in C),
|
|
// else time+pid
|
|
var seed int32
|
|
if env := os.Getenv("SEED"); env != "" && wizard {
|
|
n, _ := strconv.Atoi(env)
|
|
seed = int32(n)
|
|
} else {
|
|
seed = int32(time.Now().Unix()) + int32(os.Getpid())
|
|
}
|
|
|
|
cfg := game.Config{
|
|
Seed: seed,
|
|
Name: name,
|
|
RogueOpts: rogueOpts,
|
|
Home: home,
|
|
ScorePath: home + "/.rogue.scores",
|
|
Wizard: wizard,
|
|
}
|
|
|
|
if *scores {
|
|
g := game.NewGame(cfg)
|
|
g.ShowScores()
|
|
return
|
|
}
|
|
|
|
t, err := term.New()
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
defer t.Fini()
|
|
cfg.Term = t
|
|
|
|
var g *game.RogueGame
|
|
if args := flag.Args(); len(args) == 1 && !*deathDemo {
|
|
// restore a saved game
|
|
g, err = game.Restore(args[0], cfg)
|
|
if err != nil {
|
|
t.Fini()
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
} else {
|
|
g = game.NewGame(cfg)
|
|
}
|
|
|
|
if *deathDemo {
|
|
g.DeathDemo()
|
|
return
|
|
}
|
|
|
|
// SIGHUP/SIGTERM autosave (save.c auto_save)
|
|
sig := make(chan os.Signal, 1)
|
|
signal.Notify(sig, syscall.SIGHUP, syscall.SIGTERM)
|
|
go func() {
|
|
<-sig
|
|
g.AutoSave()
|
|
t.Fini()
|
|
os.Exit(0)
|
|
}()
|
|
|
|
if err := g.Run(); err != nil {
|
|
t.Fini()
|
|
fmt.Fprintln(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
}
|