diff options
| author | historia <[not public]> | 2026-06-11 04:46:27 -0400 |
|---|---|---|
| committer | historia <[not public]> | 2026-06-11 08:58:37 +0000 |
| commit | 1b9e2da3b3c438d8dc53d3489725dd5ba0022777 (patch) | |
| tree | 62264f24420bf4cfaa734942c65cc8dd45ba3d3b /internal/game/cmd_wear.go | |
| parent | 06c02a697e5daf8832a462de5970dd4c0e13a02c (diff) | |
| download | thehouseoficarus-1b9e2da3b3c438d8dc53d3489725dd5ba0022777.tar.gz | |
feat: web client, get/drop updates and fixes
Diffstat (limited to 'internal/game/cmd_wear.go')
| -rw-r--r-- | internal/game/cmd_wear.go | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/internal/game/cmd_wear.go b/internal/game/cmd_wear.go new file mode 100644 index 0000000..c4b5d12 --- /dev/null +++ b/internal/game/cmd_wear.go @@ -0,0 +1,146 @@ +package game + +import ( + "fmt" + "strings" + + "thirdcollapse/internal/net" + "thirdcollapse/internal/object" + "thirdcollapse/internal/player" +) + +func (g *Game) doWear(sess *net.Session, input string) { + p := sess.Player.(*player.Player) + + lower := strings.ToLower(strings.TrimSpace(input)) + if lower == "all" { + g.doWearAll(sess) + return + } + + if lower == "" { + sess.WriteLine("Wear what?") + return + } + + g.CancelAction(p) + + matches := g.findInventoryMatches(input, p) + if len(matches) == 0 { + sess.WriteLine(fmt.Sprintf("You don't have any '%s'.", input)) + 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.", match.Name)) + return + } + + existingItemID, slotOccupied := p.Equipment[def.EquipSlot] + + if slotOccupied { + freeSlot := p.FirstFreeSlot() + if freeSlot == -1 && existingItemID != slot.ItemID { + sess.WriteLine("Your inventory is full.") + return + } + if freeSlot >= 0 { + p.SetInvSlot(freeSlot, &player.InventorySlot{ItemID: existingItemID, Quantity: 1}) + } + } + + if slot.Quantity > 1 { + slot.Quantity-- + } else { + p.SetInvSlot(match.Slot, nil) + } + + p.Equipment[def.EquipSlot] = slot.ItemID + g.AccountStore.SaveCharacter(p) + + verb := "wear" + if def.WeaponType != "" { + verb = "wield" + } + sess.WriteLine(fmt.Sprintf("You %s %s.", verb, match.Name)) +} + +func (g *Game) doWearAll(sess *net.Session) { + p := sess.Player.(*player.Player) + g.CancelAction(p) + + bestPerSlot := make(map[object.EquipSlot]struct { + itemID string + value int + invSlot int + }) + + for i := 0; i < 28; i++ { + slot := p.InvSlot(i) + if slot == nil { + continue + } + def, err := g.ItemStore.Load(slot.ItemID) + if err != nil || def.EquipSlot == "" { + continue + } + current, exists := bestPerSlot[def.EquipSlot] + if !exists || def.Value > current.value { + bestPerSlot[def.EquipSlot] = struct { + itemID string + value int + invSlot int + }{slot.ItemID, def.Value, 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 { + continue + } + + if existing, ok := p.Equipment[eqSlot]; ok { + freeSlot := p.FirstFreeSlot() + if freeSlot == -1 { + continue + } + p.SetInvSlot(freeSlot, &player.InventorySlot{ItemID: existing, Quantity: 1}) + } + + invSlot := p.InvSlot(best.invSlot) + if invSlot != nil && invSlot.Quantity > 1 { + invSlot.Quantity-- + } else { + p.SetInvSlot(best.invSlot, nil) + } + + p.Equipment[eqSlot] = best.itemID + def, _ := g.ItemStore.Load(best.itemID) + name := best.itemID + if def != nil { + name = def.Name + } + equipped = append(equipped, name) + } + + if len(equipped) == 0 { + sess.WriteLine("Nothing new to wear.") + } else { + g.AccountStore.SaveCharacter(p) + sess.Write("You wear: ") + innerPickupReport(sess, equipped) + } +} |
