go: port combat, chase driver, traps, zapping, death and scores
- fight.c in full: fight/attack with all eight special monster attacks (aquator rust, ice freeze, rattlesnake poison, wraith/vampire drains, flytrap hold, leprechaun/nymph theft), swing, roll_em dice parsing (with a C-semantics atoi — Go's strconv rejects '1x4/...'), hit/miss message variants, killed, remove_mon - chase.c completed: runners, move_monst, relocate, do_chase with dragon breath, chase target selection (C's dead oroom comparison in relocate is preserved as-is) - move.c in full: do_move with passgo turning, all eight traps, rndmove, rust_armor - sticks.c completed: do_zap (all 14 sticks), drain, fire_bolt with wall bounces; weapons.c completed: missile/do_motion/fall/wield - rip.c: death tombstone, total_winner, killname; C exit() becomes a gameEnd panic that Run will recover - score.go: top-ten scoreboard as a gob file with lock-file protocol replacing the XOR-encrypted C format - wizard.c: whatis/set_know/teleport; daemons.c stomach - monster bestiary moved to per-game state (C mutates the flytrap damage string during play) Tests: combat math, kill/removal bookkeeping, monster chase pursuit, death unwinding, scripted-input headless terminal.
This commit is contained in:
196
game/score.go
Normal file
196
game/score.go
Normal file
@@ -0,0 +1,196 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
// score.h / rip.c score() / save.c rd_score+wr_score — the scoreboard.
|
||||
// The C scorefile was XOR-encrypted structs; the port stores a gob list at
|
||||
// ScorePath (empty path disables persistence but still shows the list).
|
||||
|
||||
// numScores is the C numscores default: the top ten.
|
||||
const numScores = 10
|
||||
|
||||
// ScoreEnt is score.h struct sc_ent.
|
||||
type ScoreEnt struct {
|
||||
UID int
|
||||
Score int
|
||||
Flags int // 0=killed, 1=quit, 2=winner, 3=killed with amulet
|
||||
Monster byte
|
||||
Name string
|
||||
Level int
|
||||
Time int64
|
||||
}
|
||||
|
||||
var scoreReasons = [4]string{
|
||||
"killed",
|
||||
"quit",
|
||||
"A total winner",
|
||||
"killed with Amulet",
|
||||
}
|
||||
|
||||
// rdScore reads the scoreboard file (save.c rd_score).
|
||||
func (g *RogueGame) rdScore() []ScoreEnt {
|
||||
topTen := make([]ScoreEnt, numScores)
|
||||
if g.ScorePath == "" {
|
||||
return topTen
|
||||
}
|
||||
f, err := os.Open(g.ScorePath)
|
||||
if err != nil {
|
||||
return topTen
|
||||
}
|
||||
defer f.Close()
|
||||
var onDisk []ScoreEnt
|
||||
if err := gob.NewDecoder(f).Decode(&onDisk); err != nil {
|
||||
return topTen
|
||||
}
|
||||
copy(topTen, onDisk)
|
||||
return topTen
|
||||
}
|
||||
|
||||
// wrScore writes the scoreboard file (save.c wr_score).
|
||||
func (g *RogueGame) wrScore(topTen []ScoreEnt) {
|
||||
if g.ScorePath == "" {
|
||||
return
|
||||
}
|
||||
// lock_sc/unlock_sc: exclusive-create lock file with stale takeover.
|
||||
lock := g.ScorePath + ".lck"
|
||||
for range 5 {
|
||||
lf, err := os.OpenFile(lock, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o644)
|
||||
if err == nil {
|
||||
lf.Close()
|
||||
defer os.Remove(lock)
|
||||
break
|
||||
}
|
||||
if fi, serr := os.Stat(lock); serr == nil &&
|
||||
time.Since(fi.ModTime()) > 10*time.Second {
|
||||
os.Remove(lock)
|
||||
continue
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
f, err := os.OpenFile(g.ScorePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
gob.NewEncoder(f).Encode(topTen)
|
||||
}
|
||||
|
||||
// score figures the score and posts it (rip.c score). flags -1 means just
|
||||
// display the list (the -s command line option).
|
||||
func (g *RogueGame) score(amount, flags int, monst byte) {
|
||||
if flags >= 0 && g.scr != nil && g.scr.term != nil {
|
||||
g.mvaddstr(NumLines-1, 0, "[Press return to continue]")
|
||||
g.refresh()
|
||||
g.waitFor('\n')
|
||||
}
|
||||
|
||||
topTen := g.rdScore()
|
||||
// Insert her in list if need be
|
||||
ins := -1
|
||||
if !g.NoScore && flags >= 0 {
|
||||
uid := os.Getuid()
|
||||
scp := len(topTen)
|
||||
for i := range topTen {
|
||||
if amount > topTen[i].Score {
|
||||
scp = i
|
||||
break
|
||||
} else if !g.AllScore && flags != 2 &&
|
||||
topTen[i].UID == uid && topTen[i].Flags != 2 {
|
||||
// only one score per nowin uid
|
||||
scp = len(topTen)
|
||||
break
|
||||
}
|
||||
}
|
||||
if scp < len(topTen) {
|
||||
sc2 := len(topTen) - 1
|
||||
if flags != 2 && !g.AllScore {
|
||||
for i := scp; i < len(topTen); i++ {
|
||||
if topTen[i].UID == uid && topTen[i].Flags != 2 {
|
||||
sc2 = i
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
for sc2 > scp {
|
||||
topTen[sc2] = topTen[sc2-1]
|
||||
sc2--
|
||||
}
|
||||
lvl := g.Depth
|
||||
if flags == 2 {
|
||||
lvl = g.MaxDepth
|
||||
}
|
||||
topTen[scp] = ScoreEnt{
|
||||
UID: uid,
|
||||
Score: amount,
|
||||
Flags: flags,
|
||||
Monster: monst,
|
||||
Name: g.Whoami,
|
||||
Level: lvl,
|
||||
Time: time.Now().Unix(),
|
||||
}
|
||||
ins = scp
|
||||
}
|
||||
}
|
||||
|
||||
// Build the list display
|
||||
label := "Rogueists"
|
||||
if g.AllScore {
|
||||
label = "Scores"
|
||||
}
|
||||
lines := []string{
|
||||
fmt.Sprintf("Top Ten %s:", label),
|
||||
" Score Name",
|
||||
}
|
||||
highlight := -1
|
||||
for i := range topTen {
|
||||
scp := &topTen[i]
|
||||
if scp.Score == 0 {
|
||||
break
|
||||
}
|
||||
line := fmt.Sprintf("%2d %5d %s: %s on level %d", i+1,
|
||||
scp.Score, scp.Name, scoreReasons[scp.Flags], scp.Level)
|
||||
if scp.Flags == 0 || scp.Flags == 3 {
|
||||
line += fmt.Sprintf(" by %s", g.killname(scp.Monster, true))
|
||||
}
|
||||
line += "."
|
||||
if i == ins {
|
||||
highlight = len(lines)
|
||||
}
|
||||
lines = append(lines, line)
|
||||
}
|
||||
|
||||
if g.scr != nil && g.scr.term != nil {
|
||||
g.clear()
|
||||
for i, line := range lines {
|
||||
if i == highlight {
|
||||
g.standout()
|
||||
}
|
||||
g.mvaddstr(i, 0, line)
|
||||
if i == highlight {
|
||||
g.standend()
|
||||
}
|
||||
}
|
||||
g.refresh()
|
||||
} else {
|
||||
for _, line := range lines {
|
||||
fmt.Println(line)
|
||||
}
|
||||
}
|
||||
|
||||
// Update the list file
|
||||
if ins >= 0 {
|
||||
g.wrScore(topTen)
|
||||
}
|
||||
}
|
||||
|
||||
// ShowScores implements the -s command line option: print the scoreboard
|
||||
// and nothing else.
|
||||
func (g *RogueGame) ShowScores() {
|
||||
g.NoScore = true
|
||||
g.score(0, -1, 0)
|
||||
}
|
||||
Reference in New Issue
Block a user