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:
@@ -13,12 +13,12 @@ func (g *RogueGame) runners(int) {
|
|||||||
origPos := tp.Pos
|
origPos := tp.Pos
|
||||||
|
|
||||||
wastarget := tp.On(Targeted)
|
wastarget := tp.On(Targeted)
|
||||||
if g.moveMonster(tp) == -1 {
|
if removed := g.moveMonster(tp); removed {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if tp.On(Flying) && distCp(g.Player.Pos, tp.Pos) >= 3 {
|
if tp.On(Flying) && distCp(g.Player.Pos, tp.Pos) >= 3 {
|
||||||
if g.moveMonster(tp) == -1 {
|
if removed := g.moveMonster(tp); removed {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,23 +38,24 @@ func (g *RogueGame) runners(int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// moveMonster executes a single turn of running for a monster (chase.c
|
// moveMonster executes a single turn of running for a monster (chase.c
|
||||||
// move_monst). Returns -1 if the monster died or left the level.
|
// move_monst). removed reports that the monster died or left the level
|
||||||
func (g *RogueGame) moveMonster(tp *Monster) int {
|
// (the C -1 return).
|
||||||
|
func (g *RogueGame) moveMonster(tp *Monster) (removed bool) {
|
||||||
if !tp.On(Slowed) || tp.Turn {
|
if !tp.On(Slowed) || tp.Turn {
|
||||||
if g.chaseStep(tp) == -1 {
|
if g.chaseStep(tp) {
|
||||||
return -1
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if tp.On(Hasted) {
|
if tp.On(Hasted) {
|
||||||
if g.chaseStep(tp) == -1 {
|
if g.chaseStep(tp) {
|
||||||
return -1
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tp.Turn = !tp.Turn
|
tp.Turn = !tp.Turn
|
||||||
|
|
||||||
return 0
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// relocate makes the monster's new location be the specified one, updating
|
// 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
|
// chaseStep makes one thing chase another (chase.c do_chase). removed
|
||||||
// if the chaser died in the attempt.
|
// reports that the chaser died or left the level in the attempt (the C
|
||||||
func (g *RogueGame) chaseStep(th *Monster) int {
|
// -1 return).
|
||||||
|
func (g *RogueGame) chaseStep(th *Monster) (removed bool) {
|
||||||
p := &g.Player
|
p := &g.Player
|
||||||
stoprun := false // true means we are there
|
stoprun := false // true means we are there
|
||||||
mindist := 32767
|
mindist := 32767
|
||||||
@@ -153,7 +155,7 @@ func (g *RogueGame) chaseStep(th *Monster) int {
|
|||||||
g.Kamikaze = false
|
g.Kamikaze = false
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,7 +190,7 @@ func (g *RogueGame) chaseStep(th *Monster) int {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if th.Type == 'F' {
|
if th.Type == 'F' {
|
||||||
return 0
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,7 +200,7 @@ func (g *RogueGame) chaseStep(th *Monster) int {
|
|||||||
th.Flags.Clear(Awake)
|
th.Flags.Clear(Awake)
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// chase finds the spot for the chaser to move closer to the chasee
|
// chase finds the spot for the chaser to move closer to the chasee
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ func (g *RogueGame) fight(mp Coord, weap *Object, thrown bool) bool {
|
|||||||
didHit := false
|
didHit := false
|
||||||
|
|
||||||
g.HasHit = g.Options.Terse && !g.ToDeath
|
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
|
didHit = false
|
||||||
|
|
||||||
if thrown {
|
if thrown {
|
||||||
@@ -104,9 +104,10 @@ func (g *RogueGame) fight(mp Coord, weap *Object, thrown bool) bool {
|
|||||||
return didHit
|
return didHit
|
||||||
}
|
}
|
||||||
|
|
||||||
// attack has the monster attack the player (fight.c attack). Returns -1 if
|
// attack has the monster attack the player (fight.c attack). removed
|
||||||
// the monster removed itself from the level during its own attack.
|
// reports that the monster took itself off the level during its own
|
||||||
func (g *RogueGame) attack(mp *Monster) int {
|
// attack (the C -1 return).
|
||||||
|
func (g *RogueGame) attack(mp *Monster) (removed bool) {
|
||||||
p := &g.Player
|
p := &g.Player
|
||||||
// Since this is an attack, stop running and any healing that was
|
// Since this is an attack, stop running and any healing that was
|
||||||
// going on at the time.
|
// going on at the time.
|
||||||
@@ -128,9 +129,8 @@ func (g *RogueGame) attack(mp *Monster) int {
|
|||||||
|
|
||||||
mname := g.setMname(mp)
|
mname := g.setMname(mp)
|
||||||
oldhp := p.Stats.HP
|
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 mp.Type != 'I' {
|
||||||
if g.HasHit {
|
if g.HasHit {
|
||||||
g.addmsgf(". ")
|
g.addmsgf(". ")
|
||||||
@@ -317,11 +317,7 @@ func (g *RogueGame) attack(mp *Monster) int {
|
|||||||
g.Count = 0
|
g.Count = 0
|
||||||
g.status()
|
g.status()
|
||||||
|
|
||||||
if removed {
|
return removed
|
||||||
return -1
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// swing returns true if the swing hits (fight.c swing).
|
// 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
|
return res+wplus >= need
|
||||||
}
|
}
|
||||||
|
|
||||||
// rollEm rolls several attacks (fight.c roll_em).
|
// rollAttacks rolls several attacks (fight.c roll_em).
|
||||||
func (g *RogueGame) rollEm(thatt, thdef *Creature, weap *Object, hurl bool) bool {
|
func (g *RogueGame) rollAttacks(thatt, thdef *Creature, weap *Object, hurl bool) bool {
|
||||||
p := &g.Player
|
p := &g.Player
|
||||||
att := &thatt.Stats
|
att := &thatt.Stats
|
||||||
def := &thdef.Stats
|
def := &thdef.Stats
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func TestRollEmParsesMultiAttackDice(t *testing.T) {
|
|||||||
// With attacker level 20 vs armor 10, swing always hits
|
// With attacker level 20 vs armor 10, swing always hits
|
||||||
// (rnd(20)+wplus >= (20-20)-10 is always true), so three attacks of
|
// (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.
|
// 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")
|
t.Fatal("attack with guaranteed swing missed")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user