go: port command loop, save/restore, tcell terminal, and the binary
- 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.
This commit is contained in:
68
game/game.go
68
game/game.go
@@ -94,6 +94,10 @@ type RogueGame struct {
|
||||
LastPick *Object
|
||||
LLastPick *Object
|
||||
lastDelt Coord // misc.c get_dir static last_delt
|
||||
// command.c statics
|
||||
countCh byte
|
||||
direction byte
|
||||
newCount bool
|
||||
|
||||
// dungeon generation working state
|
||||
maze mazeState // rooms.c maze statics
|
||||
@@ -131,6 +135,9 @@ type RogueGame struct {
|
||||
LastScore int
|
||||
AllScore bool
|
||||
ScorePath string
|
||||
|
||||
rogueOpts string // the ROGUEOPTS string, re-parsed by playit as in C
|
||||
restored bool // game came from a save file; Run skips setup
|
||||
}
|
||||
|
||||
// NewGame builds a game from cfg, seeds the RNG, and randomizes the item
|
||||
@@ -159,10 +166,13 @@ func NewGame(cfg Config) *RogueGame {
|
||||
g.Msgs.SaveMsg = true
|
||||
g.scr = NewScreen(cfg.Term)
|
||||
g.FileName = cfg.Home + "/rogue.save"
|
||||
g.rogueOpts = cfg.RogueOpts
|
||||
if cfg.Wizard {
|
||||
g.Player.Flags.Set(SeeMonst)
|
||||
}
|
||||
// TODO(port): parse cfg.RogueOpts once options.go lands.
|
||||
if cfg.RogueOpts != "" {
|
||||
g.ParseOpts(cfg.RogueOpts)
|
||||
}
|
||||
|
||||
g.Monsters = monsterTable
|
||||
g.Items.Group = 2 // weapons.c: int group = 2
|
||||
@@ -179,6 +189,62 @@ func NewGame(cfg Config) *RogueGame {
|
||||
return g
|
||||
}
|
||||
|
||||
// Run plays the game to its end: the back half of main.c main() plus
|
||||
// playit(). It returns after death, victory, quitting, or saving.
|
||||
func (g *RogueGame) Run() (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
if _, ok := r.(gameEnd); ok {
|
||||
return // normal game over / save exit
|
||||
}
|
||||
panic(r)
|
||||
}
|
||||
}()
|
||||
if !g.restored {
|
||||
g.NewLevel() // draw current level
|
||||
// Start up daemons and fuses
|
||||
g.StartDaemon(DRunners, 0, After)
|
||||
g.StartDaemon(DDoctor, 0, After)
|
||||
g.Fuse(DSwander, 0, wanderTime(g), After)
|
||||
g.StartDaemon(DStomach, 0, After)
|
||||
}
|
||||
g.playit()
|
||||
return nil
|
||||
}
|
||||
|
||||
// playit is the main loop of the program (main.c playit).
|
||||
func (g *RogueGame) playit() {
|
||||
// set up defaults for modern terminals: curses' md_hasclreol() is
|
||||
// always true, so the C default inventory style applies
|
||||
if !g.restored {
|
||||
g.Options.InvType = InvClear
|
||||
}
|
||||
// parse environment declaration of options (C parses ROGUEOPTS again
|
||||
// here, letting it override the terminal defaults)
|
||||
if g.rogueOpts != "" {
|
||||
g.ParseOpts(g.rogueOpts)
|
||||
}
|
||||
|
||||
g.Oldpos = g.Player.Pos
|
||||
g.Oldrp = g.roomin(g.Player.Pos)
|
||||
for g.Playing {
|
||||
g.command() // command execution
|
||||
}
|
||||
g.endit()
|
||||
}
|
||||
|
||||
// endit exits the game (main.c endit).
|
||||
func (g *RogueGame) endit() {
|
||||
g.fatal("Okay, bye bye!\n")
|
||||
}
|
||||
|
||||
// fatal prints a message and leaves (main.c fatal).
|
||||
func (g *RogueGame) fatal(s string) {
|
||||
g.mvaddstr(NumLines-2, 0, s)
|
||||
g.refresh()
|
||||
g.myExit(0)
|
||||
}
|
||||
|
||||
// quit has the player make certain, then exits (main.c quit). The final
|
||||
// scoring display arrives with the endgame phase.
|
||||
func (g *RogueGame) quit(int) {
|
||||
|
||||
Reference in New Issue
Block a user