Decompose ObjectKind.String and doMotion (refactor step 7)

String's tail moves into stringRest; doMotion's erase step moves into
eraseFlight and the inverted loop drops a nesting level. Behavior
unchanged.
This commit is contained in:
2026-07-22 22:06:48 +07:00
parent 71713d68b7
commit 730d91d160
2 changed files with 42 additions and 29 deletions

View File

@@ -77,17 +77,9 @@ func (k ObjectKind) String() string {
return "suit of armor" return "suit of armor"
case KindRing: case KindRing:
return ringName return ringName
case KindWand: default:
return "wand or staff" return k.stringRest()
case KindAmulet:
return "amulet"
case KindGold:
return goldName
case KindRingOrStick:
return "ring, wand or staff"
} }
return "bizarre thing"
} }
// objectKindForGlyph is the reverse of Glyph: what category of item does a // objectKindForGlyph is the reverse of Glyph: what category of item does a
@@ -123,6 +115,23 @@ func (k ObjectKind) MergesInPack() bool {
return k == KindPotion || k == KindScroll || k == KindFood return k == KindPotion || k == KindScroll || k == KindFood
} }
// stringRest names the remaining kinds, including the ring-or-stick
// prompt pseudo-kind (the tail of the C type_name switch).
func (k ObjectKind) stringRest() string {
switch k {
case KindWand:
return "wand or staff"
case KindAmulet:
return "amulet"
case KindGold:
return goldName
case KindRingOrStick:
return "ring, wand or staff"
}
return "bizarre thing"
}
// Object is the _o arm of the C THING union: anything that can lie on the // Object is the _o arm of the C THING union: anything that can lie on the
// floor or ride in a pack. // floor or ride in a pack.
type Object struct { type Object struct {

View File

@@ -35,8 +35,30 @@ func (g *RogueGame) doMotion(obj *Object, ydelta, xdelta int) {
// Come fly with us ... // Come fly with us ...
obj.Pos = p.Pos obj.Pos = p.Pos
for { for {
// Erase the old one g.eraseFlight(obj, p.Pos)
if obj.Pos != p.Pos && g.canSee(obj.Pos.Y, obj.Pos.X) && !g.Options.Terse { // Get the new position
obj.Pos.Y += ydelta
obj.Pos.X += xdelta
ch := g.Level.VisibleChar(obj.Pos.Y, obj.Pos.X)
if !stepOk(ch) || ch == Door {
break
}
// It hasn't hit anything yet, so display it if it's alright.
if g.canSee(obj.Pos.Y, obj.Pos.X) && !g.Options.Terse {
g.mvaddch(obj.Pos.Y, obj.Pos.X, obj.Kind.Glyph())
g.refresh()
}
}
}
// eraseFlight erases a flying object from its current square, unless it
// still sits on the hero (the erase step of weapons.c do_motion).
func (g *RogueGame) eraseFlight(obj *Object, heroPos Coord) {
if obj.Pos == heroPos || !g.canSee(obj.Pos.Y, obj.Pos.X) || g.Options.Terse {
return
}
ch := g.Level.Char(obj.Pos.Y, obj.Pos.X) ch := g.Level.Char(obj.Pos.Y, obj.Pos.X)
if ch == Floor && !g.showFloor() { if ch == Floor && !g.showFloor() {
ch = ' ' ch = ' '
@@ -44,24 +66,6 @@ func (g *RogueGame) doMotion(obj *Object, ydelta, xdelta int) {
g.mvaddch(obj.Pos.Y, obj.Pos.X, ch) g.mvaddch(obj.Pos.Y, obj.Pos.X, ch)
} }
// Get the new position
obj.Pos.Y += ydelta
obj.Pos.X += xdelta
ch := g.Level.VisibleChar(obj.Pos.Y, obj.Pos.X)
if stepOk(ch) && ch != Door {
// It hasn't hit anything yet, so display it if it's alright.
if g.canSee(obj.Pos.Y, obj.Pos.X) && !g.Options.Terse {
g.mvaddch(obj.Pos.Y, obj.Pos.X, obj.Kind.Glyph())
g.refresh()
}
continue
}
break
}
}
// fall drops an item someplace around here (weapons.c fall). // fall drops an item someplace around here (weapons.c fall).
func (g *RogueGame) fall(obj *Object, pr bool) { func (g *RogueGame) fall(obj *Object, pr bool) {