aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_search.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/game/cmd_search.go')
-rw-r--r--internal/game/cmd_search.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/internal/game/cmd_search.go b/internal/game/cmd_search.go
new file mode 100644
index 0000000..341685e
--- /dev/null
+++ b/internal/game/cmd_search.go
@@ -0,0 +1,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)
+}