aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_search.go
blob: 341685e5947a6ed99811846a28a0fd7d31c3edbc (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
package game

import (
	"fmt"
	"strings"

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

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

	lower := strings.ToLower(strings.TrimSpace(input))
	if lower == "" {
		sess.WriteLine("Search what?")
		return
	}

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

	itemID := matches[0].ID
	slotIdx := matches[0].Slot

	if itemID != "birds_nest" {
		sess.WriteLine(fmt.Sprintf("You can't search %s.", matches[0].Name))
		return
	}

	slot := p.InvSlot(slotIdx)
	if slot == nil || slot.ItemID != "birds_nest" {
		return
	}

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

	dt, err := g.BehaviorStore.LoadDropTable("birds_nest_drop")
	if err != nil || len(dt.Drops) == 0 {
		sess.WriteLine("You search the bird's nest but find nothing.")
		g.AccountStore.SaveCharacter(p)
		return
	}

	drop := g.BehaviorStore.ResolveDrop(dt.Drops)
	if drop == nil {
		sess.WriteLine("You search the bird's nest but find nothing.")
		g.AccountStore.SaveCharacter(p)
		return
	}

	qty := drop.Quantity
	if qty <= 0 {
		qty = 1
	}

	if drop.ItemID == "credits" {
		p.Credits += qty
		sess.WriteLine(fmt.Sprintf("You search the bird's nest and find %d credits.", qty))
	} else {
		freeSlot := p.FirstFreeSlot()
		if freeSlot == -1 {
			g.World.AddGroundItem(p.RoomID, drop.ItemID, qty)
			name := drop.ItemID
			if def, err := g.ItemStore.Load(drop.ItemID); err == nil {
				name = def.Name
			}
			sess.WriteLine(fmt.Sprintf("You search the bird's nest and find %s. It falls to the ground.", name))
		} else {
			p.SetInvSlot(freeSlot, &player.InventorySlot{ItemID: drop.ItemID, Quantity: qty})
			name := drop.ItemID
			if def, err := g.ItemStore.Load(drop.ItemID); err == nil {
				name = def.Name
			}
			sess.WriteLine(fmt.Sprintf("You search the bird's nest and find %s.", name))
		}
	}

	g.AccountStore.SaveCharacter(p)
}