Files
rgoue/game/io.go
sneak 2eff377a73 Un-overload Object fields and pre-parse damage dice (refactor step 3)
Object.Arm carried four meanings in C (o_arm/o_charges/o_goldval plus
ring bonuses); it is now four fields: ArmorClass, Charges, GoldValue,
and Bonus. Stats.Arm becomes Stats.ArmorClass. The Charges()/GoldVal()
accessor pair is gone.

Damage dice strings ("1x4/1x2") are parsed once into DiceSpec — at
table definition for the bestiary and weapon tables, at creation for
items — instead of re-parsed with atoi on every swing as fight.c
roll_em did. ParseDice preserves the C parse semantics exactly,
including the junk-tolerant "%%%x0" bestiary placeholder and the
"000x0" flytrap reset (regression-tested in dice_test.go); the
flytrap's growing grip becomes DiceSpec{{VfHit, 1}}.

Save format bumps to 5.4.4-go3 (field renames and retypes would
silently zero under gob's match-by-name decoding).

No behavior change; full suite green.
2026-07-06 23:07:57 +02:00

232 lines
5.4 KiB
Go

package game
import (
"fmt"
"strings"
)
// io.c — the message line, the status line, and character input.
// maxMsg is io.c MAXMSG: how much message fits before --More--.
const maxMsg = NumCols - len("--More--") - 1
// MsgLine is the io.c message machinery: the static msgbuf/newpos pair plus
// the related globals (mpos, huh, and the message-behavior flags).
type MsgLine struct {
buf strings.Builder // msgbuf
newpos int
Mpos int // where cursor is on top line
Huh string // the last message printed
SaveMsg bool // remember last msg
LowerMsg bool // messages should start w/lower case
MsgEsc bool // check for ESC from msg's --More--
}
// Msg displays a message at the top of the screen (io.c msg). It returns
// Escape if the player escaped out of a --More--, ^Escape otherwise (the C
// convention: callers compare against ESCAPE).
func (g *RogueGame) msg(format string, a ...any) int {
// if the string is "", just clear the line
if format == "" {
g.move(0, 0)
g.clrtoeol()
g.Msgs.Mpos = 0
return ^Escape
}
// otherwise add to the message and flush it out
g.doadd(format, a...)
return g.endmsg()
}
// addmsg adds things to the current message (io.c addmsg).
func (g *RogueGame) addmsg(format string, a ...any) {
g.doadd(format, a...)
}
// endmsg displays a new msg, giving the player a chance to see the previous
// one if it is up there with the --More-- (io.c endmsg).
func (g *RogueGame) endmsg() int {
m := &g.Msgs
if m.SaveMsg {
m.Huh = m.buf.String()
}
if m.Mpos != 0 {
g.look(false)
g.mvaddstr(0, m.Mpos, "--More--")
g.refresh()
if !m.MsgEsc {
g.waitFor(' ')
} else {
for {
ch := g.readchar()
if ch == ' ' {
break
}
if ch == Escape {
m.buf.Reset()
m.Mpos = 0
m.newpos = 0
return Escape
}
}
}
}
// All messages should start with uppercase, except ones that start
// with a pack addressing character
out := m.buf.String()
if len(out) > 0 && isLower(out[0]) && !m.LowerMsg &&
!(len(out) > 1 && out[1] == ')') {
out = string(toUpper(out[0])) + out[1:]
}
g.mvaddstr(0, 0, out)
g.clrtoeol()
m.Mpos = m.newpos
m.newpos = 0
m.buf.Reset()
g.refresh()
return ^Escape
}
// doadd performs an add onto the message buffer (io.c doadd).
func (g *RogueGame) doadd(format string, a ...any) {
m := &g.Msgs
s := fmt.Sprintf(format, a...)
if len(s)+m.newpos >= maxMsg {
g.endmsg()
}
m.buf.WriteString(s)
m.newpos = m.buf.Len()
}
// stepOk returns true if it is ok to step on ch (io.c step_ok).
func stepOk(ch byte) bool {
switch ch {
case ' ', '|', '-':
return false
default:
return !isAlpha(ch)
}
}
// readchar reads and returns a character, checking for gross input errors
// (io.c readchar).
func (g *RogueGame) readchar() byte {
ch := g.scr.term.ReadChar()
if ch == 3 { // ^C
g.quit(0)
return 27
}
return ch
}
// statusCache is the set of static shadow variables in io.c status() that
// suppress redundant status-line redraws.
type statusCache struct {
hpwidth int
hungry int
lvl int
pur int
hp int
arm int
str int
exp int
init bool
}
var hungerStateName = [...]string{"", "Hungry", "Weak", "Faint"}
// status displays the important stats line, keeping the cursor where it was
// (io.c status).
func (g *RogueGame) status() {
s := &g.statusCache
p := &g.Player
// If nothing has changed since the last status, don't bother.
temp := p.Stats.ArmorClass
if p.CurArmor != nil {
temp = p.CurArmor.ArmorClass
}
if s.init && s.hp == p.Stats.HP && s.exp == p.Stats.Exp &&
s.pur == p.Purse && s.arm == temp && s.str == p.Stats.Str &&
s.lvl == g.Depth && s.hungry == p.HungryState && !g.StatMsg {
return
}
s.init = true
s.arm = temp
oy, ox := g.scr.Std.GetYX()
if s.hp != p.Stats.MaxHP {
s.hp = p.Stats.MaxHP
s.hpwidth = 0
for t := p.Stats.MaxHP; t != 0; t /= 10 {
s.hpwidth++
}
}
// Save current status
s.lvl = g.Depth
s.pur = p.Purse
s.hp = p.Stats.HP
s.str = p.Stats.Str
s.exp = p.Stats.Exp
s.hungry = p.HungryState
line := fmt.Sprintf(
"Level: %d Gold: %-5d Hp: %*d(%*d) Str: %2d(%d) Arm: %-2d Exp: %d/%d %s",
g.Depth, p.Purse, s.hpwidth, p.Stats.HP, s.hpwidth, p.Stats.MaxHP,
p.Stats.Str, p.MaxStats.Str, 10-s.arm, p.Stats.Lvl, p.Stats.Exp,
hungerStateName[p.HungryState])
if g.StatMsg {
g.move(0, 0)
g.msg("%s", line)
} else {
g.move(StatLine, 0)
g.addstr(line)
}
g.clrtoeol()
g.move(oy, ox)
}
// waitFor sits around until the guy types the right key (io.c wait_for).
func (g *RogueGame) waitFor(ch byte) {
if ch == '\n' {
for {
c := g.readchar()
if c == '\n' || c == '\r' {
return
}
}
}
for g.readchar() != ch {
}
}
// showWin displays a window and waits before returning (io.c show_win).
func (g *RogueGame) showWin(message string) {
g.scr.Hw.MvAddStr(0, 0, message)
g.scr.Hw.Move(g.Player.Pos.Y, g.Player.Pos.X)
g.scr.RefreshWin(g.scr.Hw)
g.waitFor(' ')
g.refresh()
}
// ASCII helpers standing in for <ctype.h>; the C game only ever handles
// 7-bit characters.
func isAlpha(c byte) bool { return isUpper(c) || isLower(c) }
func isUpper(c byte) bool { return c >= 'A' && c <= 'Z' }
func isLower(c byte) bool { return c >= 'a' && c <= 'z' }
func isDigit(c byte) bool { return c >= '0' && c <= '9' }
func isPrint(c byte) bool { return c >= ' ' && c < 0x7f }
func toUpper(c byte) byte {
if isLower(c) {
return c - 'a' + 'A'
}
return c
}
func toLower(c byte) byte {
if isUpper(c) {
return c - 'A' + 'a'
}
return c
}