From 444bc30f2c0b7d945d8792ad77828490e6e6a484 Mon Sep 17 00:00:00 2001 From: sneak Date: Wed, 22 Jul 2026 22:03:31 +0700 Subject: [PATCH] Decompose ringOn and totalWinner (refactor step 7) ringOn gains pickRingHand and chooseTerse messages; totalWinner's appraisal switch becomes objectWorth with loreWorth/ringWorth/ wandWorth. Behavior unchanged. --- game/rings.go | 47 +++++++------- game/rip.go | 167 ++++++++++++++++++++++++++++---------------------- 2 files changed, 119 insertions(+), 95 deletions(-) diff --git a/game/rings.go b/game/rings.go index c62a00f..1e7bc2a 100644 --- a/game/rings.go +++ b/game/rings.go @@ -14,11 +14,8 @@ func (g *RogueGame) ringOn() { } if obj.Kind != KindRing { - if !g.Options.Terse { - g.msg("it would be difficult to wrap that around a finger") - } else { - g.msg("not a ring") - } + g.msg("%s", g.chooseTerse("not a ring", + "it would be difficult to wrap that around a finger")) return } @@ -28,24 +25,8 @@ func (g *RogueGame) ringOn() { return } - var ring int - - switch { - case p.CurRing[Left] == nil && p.CurRing[Right] == nil: - if ring = g.gethand(); ring < 0 { - return - } - case p.CurRing[Left] == nil: - ring = Left - case p.CurRing[Right] == nil: - ring = Right - default: - if !g.Options.Terse { - g.msg("you already have a ring on each hand") - } else { - g.msg("wearing two") - } - + ring := g.pickRingHand() + if ring < 0 { return } @@ -68,6 +49,26 @@ func (g *RogueGame) ringOn() { g.msg("%s (%c)", g.inventoryName(obj, true), obj.PackCh) } +// pickRingHand chooses the hand for a new ring, asking when both are +// free; negative aborts (rings.c ring_on). +func (g *RogueGame) pickRingHand() int { + p := &g.Player + + switch { + case p.CurRing[Left] == nil && p.CurRing[Right] == nil: + return g.gethand() + case p.CurRing[Left] == nil: + return Left + case p.CurRing[Right] == nil: + return Right + default: + g.msg("%s", g.chooseTerse("wearing two", + "you already have a ring on each hand")) + + return -1 + } +} + // ringOff takes off a ring (rings.c ring_off). func (g *RogueGame) ringOff() { p := &g.Player diff --git a/game/rip.go b/game/rip.go index 5e504cb..8910efa 100644 --- a/game/rip.go +++ b/game/rip.go @@ -115,78 +115,7 @@ func (g *RogueGame) totalWinner() { line := 1 for _, obj := range p.Pack { - worth := 0 - it := &g.Items - - switch obj.Kind { - case KindFood: - worth = 2 * obj.Count - case KindWeapon: - worth = it.Weapons[obj.Which].Worth - worth *= 3*(obj.HPlus+obj.DPlus) + obj.Count - obj.Flags.Set(Known) - case KindArmor: - worth = it.Armors[obj.Which].Worth - worth += (9 - obj.ArmorClass) * 100 - worth += 10 * (g.data.aClass[obj.Which] - obj.ArmorClass) - obj.Flags.Set(Known) - case KindScroll: - op := &it.Scrolls[obj.Which] - - worth = op.Worth * obj.Count - if !op.Know { - worth /= 2 - } - - op.Know = true - case KindPotion: - op := &it.Potions[obj.Which] - - worth = op.Worth * obj.Count - if !op.Know { - worth /= 2 - } - - op.Know = true - case KindRing: - op := &it.Rings[obj.Which] - worth = op.Worth - - if obj.RingKind() == RingAddStrength || obj.RingKind() == RingIncreaseDamage || - obj.RingKind() == RingProtection || obj.RingKind() == RingDexterity { - if obj.Bonus > 0 { - worth += obj.Bonus * 100 - } else { - worth = 10 - } - } - - if !obj.Flags.Has(Known) { - worth /= 2 - } - - obj.Flags.Set(Known) - - op.Know = true - case KindWand: - op := &it.Sticks[obj.Which] - worth = op.Worth - - worth += 20 * obj.Charges - if !obj.Flags.Has(Known) { - worth /= 2 - } - - obj.Flags.Set(Known) - - op.Know = true - case KindAmulet: - worth = 1000 - } - - if worth < 0 { - worth = 0 - } + worth := g.objectWorth(obj) g.scr.Std.MvPrintwf(line, 0, "%c) %5d %s", obj.PackCh, worth, g.inventoryName(obj, false)) @@ -200,6 +129,100 @@ func (g *RogueGame) totalWinner() { g.myExit() } +// objectWorth appraises one pack item on the way out, marking it known +// (the switch of rip.c total_winner). +func (g *RogueGame) objectWorth(obj *Object) int { + it := &g.Items + worth := 0 + + switch obj.Kind { + case KindFood: + worth = 2 * obj.Count + case KindWeapon: + worth = it.Weapons[obj.Which].Worth + worth *= 3*(obj.HPlus+obj.DPlus) + obj.Count + obj.Flags.Set(Known) + case KindArmor: + worth = it.Armors[obj.Which].Worth + worth += (9 - obj.ArmorClass) * 100 + worth += 10 * (g.data.aClass[obj.Which] - obj.ArmorClass) + obj.Flags.Set(Known) + case KindScroll: + worth = loreWorth(&it.Scrolls[obj.Which], obj.Count) + case KindPotion: + worth = loreWorth(&it.Potions[obj.Which], obj.Count) + case KindRing: + worth = g.ringWorth(obj) + case KindWand: + worth = g.wandWorth(obj) + case KindAmulet: + worth = 1000 + } + + if worth < 0 { + worth = 0 + } + + return worth +} + +// loreWorth appraises a scroll or potion, halved when unidentified, and +// identifies it (rip.c total_winner). +func loreWorth(op *ObjInfo, count int) int { + worth := op.Worth * count + if !op.Know { + worth /= 2 + } + + op.Know = true + + return worth +} + +// ringWorth appraises a ring: bonus rings gain by their bonus, cursed +// ones are junk (rip.c total_winner). +func (g *RogueGame) ringWorth(obj *Object) int { + op := &g.Items.Rings[obj.Which] + worth := op.Worth + + if obj.RingKind() == RingAddStrength || obj.RingKind() == RingIncreaseDamage || + obj.RingKind() == RingProtection || obj.RingKind() == RingDexterity { + if obj.Bonus > 0 { + worth += obj.Bonus * 100 + } else { + worth = 10 + } + } + + if !obj.Flags.Has(Known) { + worth /= 2 + } + + obj.Flags.Set(Known) + + op.Know = true + + return worth +} + +// wandWorth appraises a wand or staff by its charges (rip.c +// total_winner). +func (g *RogueGame) wandWorth(obj *Object) int { + op := &g.Items.Sticks[obj.Which] + worth := op.Worth + + worth += 20 * obj.Charges + if !obj.Flags.Has(Known) { + worth /= 2 + } + + obj.Flags.Set(Known) + + op.Know = true + + return worth +} + // killname converts a code to a monster name (rip.c killname). func (g *RogueGame) killname(monst byte, doart bool) string { var (