Rename combat methods; -1 status codes become named bool results

Refactor step 5, combat: rollEm→rollAttacks; attack, moveMonster, and
chaseStep return (removed bool) instead of the C -1/0 int codes.
Behavior unchanged; suite green.
This commit is contained in:
2026-07-07 02:33:15 +02:00
parent 6d798c56ed
commit ae79fd5e84
3 changed files with 27 additions and 29 deletions

View File

@@ -13,12 +13,12 @@ func (g *RogueGame) runners(int) {
origPos := tp.Pos
wastarget := tp.On(Targeted)
if g.moveMonster(tp) == -1 {
if removed := g.moveMonster(tp); removed {
continue
}
if tp.On(Flying) && distCp(g.Player.Pos, tp.Pos) >= 3 {
if g.moveMonster(tp) == -1 {
if removed := g.moveMonster(tp); removed {
continue
}
}
@@ -38,23 +38,24 @@ func (g *RogueGame) runners(int) {
}
// moveMonster executes a single turn of running for a monster (chase.c
// move_monst). Returns -1 if the monster died or left the level.
func (g *RogueGame) moveMonster(tp *Monster) int {
// move_monst). removed reports that the monster died or left the level
// (the C -1 return).
func (g *RogueGame) moveMonster(tp *Monster) (removed bool) {
if !tp.On(Slowed) || tp.Turn {
if g.chaseStep(tp) == -1 {
return -1
if g.chaseStep(tp) {
return true
}
}
if tp.On(Hasted) {
if g.chaseStep(tp) == -1 {
return -1
if g.chaseStep(tp) {
return true
}
}
tp.Turn = !tp.Turn
return 0
return false
}
// relocate makes the monster's new location be the specified one, updating
@@ -86,9 +87,10 @@ func (g *RogueGame) relocate(th *Monster, newLoc Coord) {
}
}
// chaseStep makes one thing chase another (chase.c do_chase). Returns -1
// if the chaser died in the attempt.
func (g *RogueGame) chaseStep(th *Monster) int {
// chaseStep makes one thing chase another (chase.c do_chase). removed
// reports that the chaser died or left the level in the attempt (the C
// -1 return).
func (g *RogueGame) chaseStep(th *Monster) (removed bool) {
p := &g.Player
stoprun := false // true means we are there
mindist := 32767
@@ -153,7 +155,7 @@ func (g *RogueGame) chaseStep(th *Monster) int {
g.Kamikaze = false
}
return 0
return false
}
}
@@ -188,7 +190,7 @@ func (g *RogueGame) chaseStep(th *Monster) int {
}
} else {
if th.Type == 'F' {
return 0
return false
}
}
@@ -198,7 +200,7 @@ func (g *RogueGame) chaseStep(th *Monster) int {
th.Flags.Clear(Awake)
}
return 0
return false
}
// chase finds the spot for the chaser to move closer to the chasee

View File

@@ -67,7 +67,7 @@ func (g *RogueGame) fight(mp Coord, weap *Object, thrown bool) bool {
didHit := false
g.HasHit = g.Options.Terse && !g.ToDeath
if g.rollEm(&p.Creature, &tp.Creature, weap, thrown) {
if g.rollAttacks(&p.Creature, &tp.Creature, weap, thrown) {
didHit = false
if thrown {
@@ -104,9 +104,10 @@ func (g *RogueGame) fight(mp Coord, weap *Object, thrown bool) bool {
return didHit
}
// attack has the monster attack the player (fight.c attack). Returns -1 if
// the monster removed itself from the level during its own attack.
func (g *RogueGame) attack(mp *Monster) int {
// attack has the monster attack the player (fight.c attack). removed
// reports that the monster took itself off the level during its own
// attack (the C -1 return).
func (g *RogueGame) attack(mp *Monster) (removed bool) {
p := &g.Player
// Since this is an attack, stop running and any healing that was
// going on at the time.
@@ -128,9 +129,8 @@ func (g *RogueGame) attack(mp *Monster) int {
mname := g.setMname(mp)
oldhp := p.Stats.HP
removed := false
if g.rollEm(&mp.Creature, &p.Creature, nil, false) {
if g.rollAttacks(&mp.Creature, &p.Creature, nil, false) {
if mp.Type != 'I' {
if g.HasHit {
g.addmsgf(". ")
@@ -317,11 +317,7 @@ func (g *RogueGame) attack(mp *Monster) int {
g.Count = 0
g.status()
if removed {
return -1
}
return 0
return removed
}
// swing returns true if the swing hits (fight.c swing).
@@ -332,8 +328,8 @@ func (g *RogueGame) swing(atLvl, opArm, wplus int) bool {
return res+wplus >= need
}
// rollEm rolls several attacks (fight.c roll_em).
func (g *RogueGame) rollEm(thatt, thdef *Creature, weap *Object, hurl bool) bool {
// rollAttacks rolls several attacks (fight.c roll_em).
func (g *RogueGame) rollAttacks(thatt, thdef *Creature, weap *Object, hurl bool) bool {
p := &g.Player
att := &thatt.Stats
def := &thdef.Stats

View File

@@ -32,7 +32,7 @@ func TestRollEmParsesMultiAttackDice(t *testing.T) {
// With attacker level 20 vs armor 10, swing always hits
// (rnd(20)+wplus >= (20-20)-10 is always true), so three attacks of
// 1x4 + str bonus 1 each must deal between 6 and 15 damage.
if !g.rollEm(att, def, nil, false) {
if !g.rollAttacks(att, def, nil, false) {
t.Fatal("attack with guaranteed swing missed")
}