1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
|
package game
import (
"fmt"
"strings"
"thehouseoficarus/internal/item"
"thehouseoficarus/internal/net"
"thehouseoficarus/internal/player"
)
func (g *Game) executeWear(sess *net.Session, args []string, rawInput string) {
g.doWear(sess, strings.Join(args, " "))
}
func (g *Game) checkRequirements(sess *net.Session, p *player.Player, def *item.ItemDef) string {
if len(def.Requirements) == 0 {
return ""
}
var unmet []string
for skill, level := range def.Requirements {
if p.Level(player.SkillName(skill)) < level {
unmet = append(unmet, fmt.Sprintf("%d %s", level, skill))
}
}
if len(unmet) == 0 {
return ""
}
return fmt.Sprintf("You need %s to wear %s.", strings.Join(unmet, ", "), g.itemColorize(sess, def, def.Name))
}
func (g *Game) doWear(sess *net.Session, input string) {
p := sess.Player
lower := strings.ToLower(strings.TrimSpace(input))
if lower == "all" {
g.doWearAll(sess)
return
}
if lower == "" {
sess.WriteLine("Wear what?")
return
}
g.cancelAction(p)
qty, itemName := parseQty(input)
matches := g.findInventoryMatches(itemName, p)
if len(matches) == 0 {
sess.WriteLine(fmt.Sprintf("You don't have any '%s'.", itemName))
return
}
unique := uniqueItemNames(matches)
if len(unique) > 1 {
sess.WriteLine("Equip what?")
seen := make(map[string]bool)
for _, m := range matches {
if seen[m.Name] {
continue
}
seen[m.Name] = true
def, _ := g.ItemStore.Load(m.ID)
sess.WriteLine(fmt.Sprintf(" - %s", g.itemColorize(sess, def, m.Name)))
}
return
}
match := matches[0]
slot := p.InvSlot(match.Slot)
if slot == nil {
return
}
def, err := g.ItemStore.Load(slot.ItemID)
if err != nil || def.EquipSlot() == "" {
sess.WriteLine(fmt.Sprintf("You can't wear %s.", g.itemColorize(sess, def, match.Name)))
return
}
if msg := g.checkRequirements(sess, p, def); msg != "" {
sess.WriteLine(msg)
return
}
if def.EquipSlot() == item.SlotAmmo && def.Stackable {
g.equipAmmo(sess, p, match.Slot, slot, def, qty)
return
}
existingItemID, slotOccupied := p.Equipment[def.EquipSlot()]
if slotOccupied {
freeSlot := p.FirstFreeSlot()
// The equipped item must always be returned to the inventory before
// the new one is removed; with no free slot, refuse rather than
// silently destroy an identical equipped copy.
if freeSlot == -1 {
sess.WriteLine("Your inventory is full.")
return
}
p.SetInvSlot(freeSlot, g.displacedEquipSlot(p, existingItemID))
}
if slot.Quantity > 1 {
slot.Quantity--
} else {
p.SetInvSlot(match.Slot, nil)
}
if slot.Quality > 0 && slot.MaxQuality > 0 {
if p.EquipQuality == nil {
p.EquipQuality = make(map[string]int)
}
p.EquipQuality[slot.ItemID] = slot.Quality
}
p.Equipment[def.EquipSlot()] = slot.ItemID
g.saveCharacter(p)
verb := "wear"
if def.EquipmentType() != "" {
verb = "wield"
}
sess.WriteLine(fmt.Sprintf("You %s %s.", verb, g.itemColorize(sess, def, match.Name)))
}
// displacedEquipSlot builds the inventory slot that receives an item
// displaced by a swap. The item's equipped quality (EquipQuality, kept
// because the equipment entry alone cannot carry a charge) is stamped onto
// the returned stack and cleared from the map, so the returned item keeps
// merging and charging like one carried normally; leaving the entry behind
// would let act_burn charge a stale record or a zeroed slot.
func (g *Game) displacedEquipSlot(p *player.Player, itemID string) *player.InventorySlot {
slot := &player.InventorySlot{ItemID: itemID, Quantity: 1}
if q, ok := p.EquipQuality[itemID]; ok {
slot.Quality = q
if def, _ := g.ItemStore.Load(itemID); def != nil {
slot.MaxQuality = def.MaxQuality
}
delete(p.EquipQuality, itemID)
return slot
}
// No charge record: keep a max_quality item quality-bearing but empty so
// consumeFireTool refuses it instead of treating it as an infinite
// firesteel. Stamping full quality here would refill the item.
if def, _ := g.ItemStore.Load(itemID); def != nil && def.MaxQuality > 0 {
slot.MaxQuality = def.MaxQuality
}
return slot
}
func (g *Game) equipAmmo(sess *net.Session, p *player.Player, invIdx int, slot *player.InventorySlot, def *item.ItemDef, qty int) {
transferQty := slot.Quantity
if qty > 0 && qty < transferQty {
transferQty = qty
}
existingAmmoID, hasAmmo := p.Equipment[item.SlotAmmo]
if hasAmmo && existingAmmoID != slot.ItemID {
// The equipped stack stays equipped when there is nowhere to put
// it; equipping the new stack would otherwise strand the old one.
if !g.unequipAmmo(p) {
sess.WriteLine("Your inventory is full.")
return
}
}
if slot.Quantity <= transferQty {
p.SetInvSlot(invIdx, nil)
} else {
slot.Quantity -= transferQty
}
if hasAmmo && existingAmmoID == slot.ItemID {
p.AmmoQty += transferQty
} else {
p.Equipment[item.SlotAmmo] = slot.ItemID
p.AmmoQty = transferQty
}
g.saveCharacter(p)
displayName := g.itemColorize(sess, def, def.Name)
sess.WriteLine(fmt.Sprintf("You equip %s (x%d).", displayName, p.AmmoQty))
}
// unequipAmmo returns the equipped ammo to the inventory and reports whether
// the equipped stack is now stored. With nowhere to put it the ammo stays
// equipped: the equipment entry is only cleared once the stack has actually
// been merged into an inventory stack or placed in a free slot.
func (g *Game) unequipAmmo(p *player.Player) bool {
ammoID, ok := p.Equipment[item.SlotAmmo]
if !ok || p.AmmoQty <= 0 {
return true
}
quality, maxQuality := g.pickedStackProps(ammoID)
for i := 0; i < player.MaxInventorySlots; i++ {
s := p.InvSlot(i)
// Only a stack with the properties a fresh stack gets may absorb the
// equipped stack (see pickedStackProps).
if s != nil && s.ItemID == ammoID && s.Quality == quality && s.MaxQuality == maxQuality {
s.Quantity += p.AmmoQty
delete(p.Equipment, item.SlotAmmo)
p.AmmoQty = 0
return true
}
}
freeSlot := p.FirstFreeSlot()
if freeSlot < 0 {
return false
}
p.SetInvSlot(freeSlot, g.newInventorySlot(ammoID, p.AmmoQty))
delete(p.Equipment, item.SlotAmmo)
p.AmmoQty = 0
return true
}
func (g *Game) doWearAll(sess *net.Session) {
p := sess.Player
g.cancelAction(p)
bestPerSlot := make(map[item.EquipSlot]struct {
itemID string
value int
invSlot int
})
for i := 0; i < player.MaxInventorySlots; i++ {
slot := p.InvSlot(i)
if slot == nil {
continue
}
def, err := g.ItemStore.Load(slot.ItemID)
if err != nil || def.EquipSlot() == "" {
continue
}
if g.checkRequirements(sess, p, def) != "" {
continue
}
current, exists := bestPerSlot[def.EquipSlot()]
val := def.Value
if def.EquipSlot() == item.SlotAmmo && def.Stackable {
val = def.Value * slot.Quantity
}
if !exists || val > current.value {
bestPerSlot[def.EquipSlot()] = struct {
itemID string
value int
invSlot int
}{slot.ItemID, val, i}
}
}
if len(bestPerSlot) == 0 {
sess.WriteLine("You have nothing to wear.")
return
}
var equipped []string
for eqSlot, best := range bestPerSlot {
if existing, ok := p.Equipment[eqSlot]; ok && existing == best.itemID {
if eqSlot != item.SlotAmmo {
continue
}
}
if eqSlot == item.SlotAmmo {
bestDef, _ := g.ItemStore.Load(best.itemID)
if bestDef != nil && bestDef.Stackable {
invSlot := p.InvSlot(best.invSlot)
if invSlot == nil {
continue
}
existingID, hasAmmo := p.Equipment[item.SlotAmmo]
// No free slot for the equipped stack: leave it equipped and
// skip this item rather than delete the old stack.
if hasAmmo && existingID != best.itemID && !g.unequipAmmo(p) {
continue
}
transferQty := invSlot.Quantity
p.SetInvSlot(best.invSlot, nil)
if hasAmmo && existingID == best.itemID {
p.AmmoQty += transferQty
} else {
p.Equipment[item.SlotAmmo] = best.itemID
p.AmmoQty = transferQty
}
equipped = append(equipped, g.itemColorize(sess, bestDef, bestDef.Name))
continue
}
}
if existing, ok := p.Equipment[eqSlot]; ok {
freeSlot := p.FirstFreeSlot()
if freeSlot == -1 {
continue
}
p.SetInvSlot(freeSlot, g.displacedEquipSlot(p, existing))
}
invSlot := p.InvSlot(best.invSlot)
if invSlot != nil && invSlot.Quantity > 1 {
invSlot.Quantity--
} else {
p.SetInvSlot(best.invSlot, nil)
}
if invSlot != nil && invSlot.Quality > 0 && invSlot.MaxQuality > 0 {
if p.EquipQuality == nil {
p.EquipQuality = make(map[string]int)
}
p.EquipQuality[best.itemID] = invSlot.Quality
}
p.Equipment[eqSlot] = best.itemID
def, _ := g.ItemStore.Load(best.itemID)
name := best.itemID
if def != nil {
name = def.Name
}
equipped = append(equipped, g.itemColorize(sess, def, name))
}
if len(equipped) == 0 {
sess.WriteLine("Nothing new to wear.")
} else {
g.saveCharacter(p)
sess.Write("You wear: ")
formatPickupList(sess, equipped)
}
}
|