Decompose wizard.go createObj and whatis (refactor step 7)

createObj gains createWeaponArmor/createRing; whatis gains whatisPick
for its prompt loop. wizard.go is complexity-clean. Behavior unchanged.
This commit is contained in:
2026-07-22 22:07:31 +07:00
parent 730d91d160
commit b68836dde0

View File

@@ -27,50 +27,9 @@ func (g *RogueGame) createObj() {
switch obj.Kind {
case KindWeapon, KindArmor:
g.msg("blessing? (+,-,n)")
bless := g.readchar()
g.Msgs.Mpos = 0
if bless == '-' {
obj.Flags.Set(Cursed)
}
if obj.Kind == KindWeapon {
g.initWeapon(obj, WeaponKind(obj.Which))
if bless == '-' {
obj.HPlus -= g.rnd(3) + 1
}
if bless == '+' {
obj.HPlus += g.rnd(3) + 1
}
} else {
obj.ArmorClass = g.data.aClass[obj.Which]
if bless == '-' {
obj.ArmorClass += g.rnd(3) + 1
}
if bless == '+' {
obj.ArmorClass -= g.rnd(3) + 1
}
}
g.createWeaponArmor(obj)
case KindRing:
switch obj.RingKind() {
case RingProtection, RingAddStrength, RingDexterity, RingIncreaseDamage:
g.msg("blessing? (+,-,n)")
bless := g.readchar()
g.Msgs.Mpos = 0
if bless == '-' {
obj.Flags.Set(Cursed)
obj.Bonus = -1
} else {
obj.Bonus = g.rnd(2) + 1
}
case RingAggravateMonsters, RingTeleportation:
obj.Flags.Set(Cursed)
}
g.createRing(obj)
case KindWand:
g.fixStick(obj)
case KindGold:
@@ -85,6 +44,61 @@ func (g *RogueGame) createObj() {
g.addPack(obj, false)
}
// createWeaponArmor sets up a wizard-created weapon or armor with an
// optional blessing (the weapon/armor arm of wizard.c create_obj).
func (g *RogueGame) createWeaponArmor(obj *Object) {
g.msg("blessing? (+,-,n)")
bless := g.readchar()
g.Msgs.Mpos = 0
if bless == '-' {
obj.Flags.Set(Cursed)
}
if obj.Kind == KindWeapon {
g.initWeapon(obj, WeaponKind(obj.Which))
if bless == '-' {
obj.HPlus -= g.rnd(3) + 1
}
if bless == '+' {
obj.HPlus += g.rnd(3) + 1
}
return
}
obj.ArmorClass = g.data.aClass[obj.Which]
if bless == '-' {
obj.ArmorClass += g.rnd(3) + 1
}
if bless == '+' {
obj.ArmorClass -= g.rnd(3) + 1
}
}
// createRing sets up a wizard-created ring, prompting for a bonus on
// the bonus rings (the ring arm of wizard.c create_obj).
func (g *RogueGame) createRing(obj *Object) {
switch obj.RingKind() {
case RingProtection, RingAddStrength, RingDexterity, RingIncreaseDamage:
g.msg("blessing? (+,-,n)")
bless := g.readchar()
g.Msgs.Mpos = 0
if bless == '-' {
obj.Flags.Set(Cursed)
obj.Bonus = -1
} else {
obj.Bonus = g.rnd(2) + 1
}
case RingAggravateMonsters, RingTeleportation:
obj.Flags.Set(Cursed)
}
}
// showMap prints out the whole map for the wizard (wizard.c show_map).
func (g *RogueGame) showMap() {
hw := g.scr.Hw
@@ -117,34 +131,8 @@ func (g *RogueGame) whatis(insist bool, kind ObjectKind) {
return
}
var obj *Object
for {
obj, _ = g.promptPackItem("identify", kind)
if !insist {
break
}
if g.NObjs == 0 {
return
}
if obj == nil {
g.msg("you must identify something")
continue
}
if !matchesFilter(kind, obj) {
g.msg("you must identify a %s", kind)
continue
}
break
}
if obj == nil {
obj, ok := g.whatisPick(insist, kind)
if !ok {
return
}
@@ -164,6 +152,37 @@ func (g *RogueGame) whatis(insist bool, kind ObjectKind) {
g.msg("%s", g.inventoryName(obj, false))
}
// whatisPick prompts for the item to identify, re-asking until a
// matching one is chosen when insist is set; ok is false when the
// player gives up (the prompt loop of wizard.c whatis).
func (g *RogueGame) whatisPick(insist bool, kind ObjectKind) (*Object, bool) {
for {
obj, _ := g.promptPackItem("identify", kind)
if !insist {
return obj, obj != nil
}
if g.NObjs == 0 {
return nil, false
}
if obj == nil {
g.msg("you must identify something")
continue
}
if !matchesFilter(kind, obj) {
g.msg("you must identify a %s", kind)
continue
}
return obj, true
}
}
// setKnow sets things up when we really know what a thing is (wizard.c
// set_know).
func setKnow(obj *Object, info []ObjInfo) {