Convert quaff to a per-potion handler table (refactor step 7)

The quaff switch becomes gameData.quaffHandlers, a method-expression
table indexed by PotionKind; each case body moved verbatim into a
quaff* method. Effect order and RNG call sequence unchanged.
This commit is contained in:
2026-07-07 02:44:33 +02:00
parent acef593288
commit cc025eb808
2 changed files with 199 additions and 139 deletions

View File

@@ -41,19 +41,36 @@ func (g *RogueGame) quaff() {
g.leavePack(obj, false, false) g.leavePack(obj, false, false)
switch obj.PotionKind() { if h := g.data.quaffHandlers[obj.PotionKind()]; h != nil {
case PotionConfusion: h(g, trip)
}
g.status()
// Throw the item away
g.callIt(&g.Items.Potions[obj.Which])
}
// The per-potion effect handlers, dispatched through
// gameData.quaffHandlers. Each is one case of the C quaff switch.
func (g *RogueGame) quaffConfusion(trip bool) {
g.applyPotionFuse(PotionConfusion, !trip) g.applyPotionFuse(PotionConfusion, !trip)
case PotionPoison: }
func (g *RogueGame) quaffPoison(bool) {
g.Items.Potions[PotionPoison].Know = true g.Items.Potions[PotionPoison].Know = true
if p.IsWearing(RingSustainStrength) { if g.Player.IsWearing(RingSustainStrength) {
g.msg("you feel momentarily sick") g.msg("you feel momentarily sick")
} else { } else {
g.changeStrength(-(g.rnd(3) + 1)) g.changeStrength(-(g.rnd(3) + 1))
g.msg("you feel very sick now") g.msg("you feel very sick now")
g.comeDown(0) g.comeDown(0)
} }
case PotionHealing: }
func (g *RogueGame) quaffHealing(bool) {
p := &g.Player
g.Items.Potions[PotionHealing].Know = true g.Items.Potions[PotionHealing].Know = true
if p.Stats.HP += g.roll(p.Stats.Lvl, 4); p.Stats.HP > p.Stats.MaxHP { if p.Stats.HP += g.roll(p.Stats.Lvl, 4); p.Stats.HP > p.Stats.MaxHP {
p.Stats.MaxHP++ p.Stats.MaxHP++
@@ -62,19 +79,25 @@ func (g *RogueGame) quaff() {
g.sight(0) g.sight(0)
g.msg("you begin to feel better") g.msg("you begin to feel better")
case PotionGainStrength: }
func (g *RogueGame) quaffGainStrength(bool) {
g.Items.Potions[PotionGainStrength].Know = true g.Items.Potions[PotionGainStrength].Know = true
g.changeStrength(1) g.changeStrength(1)
g.msg("you feel stronger, now. What bulging muscles!") g.msg("you feel stronger, now. What bulging muscles!")
case PotionDetectMonsters: }
p.Flags.Set(SenseMonsters)
func (g *RogueGame) quaffDetectMonsters(bool) {
g.Player.Flags.Set(SenseMonsters)
g.Fuse(DTurnSee, 1, HuhDuration, After) g.Fuse(DTurnSee, 1, HuhDuration, After)
if !g.turnSee(false) { if !g.turnSee(false) {
g.msg("you have a %s feeling for a moment, then it passes", g.msg("you have a %s feeling for a moment, then it passes",
g.chooseStr("normal", "strange")) g.chooseStr("normal", "strange"))
} }
case PotionDetectMagic: }
func (g *RogueGame) quaffDetectMagic(bool) {
// Potion of magic detection. Show the potions and scrolls // Potion of magic detection. Show the potions and scrolls
show := false show := false
@@ -108,7 +131,10 @@ func (g *RogueGame) quaff() {
g.msg("you have a %s feeling for a moment, then it passes", g.msg("you have a %s feeling for a moment, then it passes",
g.chooseStr("normal", "strange")) g.chooseStr("normal", "strange"))
} }
case PotionLSD: }
func (g *RogueGame) quaffLSD(trip bool) {
p := &g.Player
if !trip { if !trip {
if p.On(SenseMonsters) { if p.On(SenseMonsters) {
g.turnSee(false) g.turnSee(false)
@@ -119,8 +145,10 @@ func (g *RogueGame) quaff() {
} }
g.applyPotionFuse(PotionLSD, true) g.applyPotionFuse(PotionLSD, true)
case PotionSeeInvisible: }
show := p.On(CanSeeInvisible)
func (g *RogueGame) quaffSeeInvisible(bool) {
show := g.Player.On(CanSeeInvisible)
g.applyPotionFuse(PotionSeeInvisible, false) g.applyPotionFuse(PotionSeeInvisible, false)
@@ -129,11 +157,17 @@ func (g *RogueGame) quaff() {
} }
g.sight(0) g.sight(0)
case PotionRaiseLevel: }
func (g *RogueGame) quaffRaiseLevel(bool) {
g.Items.Potions[PotionRaiseLevel].Know = true g.Items.Potions[PotionRaiseLevel].Know = true
g.msg("you suddenly feel much more skillful") g.msg("you suddenly feel much more skillful")
g.raiseLevel() g.raiseLevel()
case PotionExtraHealing: }
func (g *RogueGame) quaffExtraHealing(bool) {
p := &g.Player
g.Items.Potions[PotionExtraHealing].Know = true g.Items.Potions[PotionExtraHealing].Know = true
if p.Stats.HP += g.roll(p.Stats.Lvl, 8); p.Stats.HP > p.Stats.MaxHP { if p.Stats.HP += g.roll(p.Stats.Lvl, 8); p.Stats.HP > p.Stats.MaxHP {
if p.Stats.HP > p.Stats.MaxHP+p.Stats.Lvl+1 { if p.Stats.HP > p.Stats.MaxHP+p.Stats.Lvl+1 {
@@ -147,14 +181,19 @@ func (g *RogueGame) quaff() {
g.sight(0) g.sight(0)
g.comeDown(0) g.comeDown(0)
g.msg("you begin to feel much better") g.msg("you begin to feel much better")
case PotionHaste: }
func (g *RogueGame) quaffHaste(bool) {
g.Items.Potions[PotionHaste].Know = true g.Items.Potions[PotionHaste].Know = true
g.After = false g.After = false
if g.addHaste(true) { if g.addHaste(true) {
g.msg("you feel yourself moving much faster") g.msg("you feel yourself moving much faster")
} }
case PotionRestoreStrength: }
func (g *RogueGame) quaffRestoreStrength(bool) {
p := &g.Player
if p.IsRing(Left, RingAddStrength) { if p.IsRing(Left, RingAddStrength) {
addStr(&p.Stats.Str, -p.CurRing[Left].Bonus) addStr(&p.Stats.Str, -p.CurRing[Left].Bonus)
} }
@@ -176,15 +215,14 @@ func (g *RogueGame) quaff() {
} }
g.msg("hey, this tastes great. It make you feel warm all over") g.msg("hey, this tastes great. It make you feel warm all over")
case PotionBlindness:
g.applyPotionFuse(PotionBlindness, true)
case PotionLevitation:
g.applyPotionFuse(PotionLevitation, true)
} }
g.status() func (g *RogueGame) quaffBlindness(bool) {
// Throw the item away g.applyPotionFuse(PotionBlindness, true)
g.callIt(&g.Items.Potions[obj.Which]) }
func (g *RogueGame) quaffLevitation(bool) {
g.applyPotionFuse(PotionLevitation, true)
} }
// raiseLevel: the guy just magically went up a level (potions.c // raiseLevel: the guy just magically went up a level (potions.c

View File

@@ -122,6 +122,11 @@ type gameData struct {
// scoreReasons is the rip.c reason[] scoreboard strings. // scoreReasons is the rip.c reason[] scoreboard strings.
scoreReasons [4]string scoreReasons [4]string
// quaffHandlers dispatches each potion kind to its effect method:
// the cases of the potions.c quaff switch. The bool is trip — was
// the hero hallucinating when the potion went down.
quaffHandlers [NumPotionTypes]func(g *RogueGame, trip bool)
} }
// ripWall is the repeated blank wall line of the tombstone art. // ripWall is the repeated blank wall line of the tombstone art.
@@ -587,6 +592,23 @@ func newGameData() *gameData {
"A total winner", "A total winner",
"killed with Amulet", "killed with Amulet",
}, },
quaffHandlers: [NumPotionTypes]func(*RogueGame, bool){
PotionConfusion: (*RogueGame).quaffConfusion,
PotionLSD: (*RogueGame).quaffLSD,
PotionPoison: (*RogueGame).quaffPoison,
PotionGainStrength: (*RogueGame).quaffGainStrength,
PotionSeeInvisible: (*RogueGame).quaffSeeInvisible,
PotionHealing: (*RogueGame).quaffHealing,
PotionDetectMonsters: (*RogueGame).quaffDetectMonsters,
PotionDetectMagic: (*RogueGame).quaffDetectMagic,
PotionRaiseLevel: (*RogueGame).quaffRaiseLevel,
PotionExtraHealing: (*RogueGame).quaffExtraHealing,
PotionHaste: (*RogueGame).quaffHaste,
PotionRestoreStrength: (*RogueGame).quaffRestoreStrength,
PotionBlindness: (*RogueGame).quaffBlindness,
PotionLevitation: (*RogueGame).quaffLevitation,
},
} }
} }