aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_drop.go
blob: 2f79a2fd6c517a25a8a8534833b3ae40d6bf97d4 (plain)
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
package game

import (
	"fmt"
	"strings"

	"thirdcollapse/internal/net"
	"thirdcollapse/internal/player"
)

func (g *Game) doDrop(sess *net.Session, input string) {
	p := sess.Player.(*player.Player)

	if input == "all" {
		count := 0
		for _, slot := range p.Inventory {
			if slot != nil && slot.Quantity > 0 {
				count++
			}
		}
		if count == 0 {
			sess.WriteLine("You have nothing to drop.")
			return
		}
		sess.State = net.StateDropAllConfirm
		sess.WriteLine(fmt.Sprintf("\nDrop all %d items? [y/N]", count))
		return
	}

	matches := g.findInventoryMatches(input, p)
	if len(matches) == 0 {
		sess.WriteLine(fmt.Sprintf("You don't have any '%s'.", input))
		return
	}

	unique := uniqueItemNames(matches)
	if len(unique) > 1 {
		sess.WriteLine("Which one?")
		for _, name := range unique {
			sess.WriteLine(fmt.Sprintf("  - %s", name))
		}
		return
	}

	itemID := matches[0].ID
	slotIdx := matches[0].Slot
	slot := p.InvSlot(slotIdx)
	def, _ := g.ItemStore.Load(itemID)

	qty := 1
	name := itemID
	if def != nil {
		name = def.Name
	}

	if slot.Quantity > qty {
		slot.Quantity -= qty
	} else {
		p.SetInvSlot(slotIdx, nil)
	}

	g.World.AddGroundItem(p.RoomID, itemID, qty)
	g.AccountStore.SaveCharacter(p)
	sess.WriteLine(fmt.Sprintf("You drop a %s.", name))
}

func (g *Game) handleDropAllConfirm(sess *net.Session, input string) {
	p, ok := sess.Player.(*player.Player)
	if !ok {
		sess.State = net.StateGame
		sess.Write("\r\n> ")
		return
	}

	input = strings.TrimSpace(strings.ToLower(input))
	if input != "y" && input != "yes" {
		sess.State = net.StateGame
		sess.Write("\r\n> ")
		return
	}

	var dropped []string
	for i := 0; i < 28; i++ {
		slot := p.InvSlot(i)
		if slot == nil || slot.Quantity <= 0 {
			continue
		}
		g.World.AddGroundItem(p.RoomID, slot.ItemID, slot.Quantity)
		def, _ := g.ItemStore.Load(slot.ItemID)
		name := slot.ItemID
		if def != nil {
			name = def.Name
		}
		dropped = append(dropped, name)
		p.SetInvSlot(i, nil)
	}

	g.AccountStore.SaveCharacter(p)
	sess.State = net.StateGame

	if len(dropped) == 0 {
		sess.WriteLine("\nYou have nothing to drop.")
	} else {
		sess.Write(fmt.Sprintf("\nYou drop your "))
		innerPickupReport(sess, dropped)
		sess.WriteLine(".")
	}
	sess.Write("\r\n> ")
}