aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_use.go
blob: 247df72ab053e754b246b4070564ae568a1866cc (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
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
package game

import (
	"fmt"
	"strings"

	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/object"
	"thehouseoficarus/internal/player"
)

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

	lower := strings.ToLower(input)

	sep := ""
	if strings.Contains(lower, " on ") {
		sep = " on "
	} else if strings.Contains(lower, " with ") {
		sep = " with "
	}

	if sep != "" {
		parts := strings.SplitN(lower, sep, 2)
		itemA := strings.TrimSpace(parts[0])
		itemB := strings.TrimSpace(parts[1])
		if itemA == itemB {
			sess.WriteLine("You can't use that with itself.")
			return
		}
		g.doUseItemOnTarget(sess, p, itemA, itemB)
		return
	}

	if len(strings.Fields(input)) >= 2 {
		g.doUseItemOnTarget(sess, p, strings.Fields(input)[0], strings.Join(strings.Fields(input)[1:], " "))
		return
	}

	g.StartAction(sess, "use", input)
}

func (g *Game) doUseItemOnTarget(sess *net.Session, p *player.Player, itemAName, itemBName string) {
	matchesA := g.findInventoryMatches(itemAName, p)
	if len(matchesA) == 0 {
		sess.WriteLine(fmt.Sprintf("You don't have any '%s'.", itemAName))
		return
	}

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

	itemAID := matchesA[0].ID

	matchesB := g.findInventoryMatches(itemBName, p)

	objInstances := g.World.FindObjInstances(p.RoomID, itemBName)
	if len(objInstances) > 0 {
		objSt := &objInstances[0]
		def, _ := g.ObjectStore.Load(objSt.DefID)
		if def == nil {
			g.StartAction(sess, "use", itemBName)
			return
		}

		stationIDs := []string{def.ID}
		recipes, err := g.RecipeStore.FindByStation(itemAID, stationIDs, "")
		if err == nil && len(recipes) > 0 {
			if recipes[0].Type == "smelting" {
				filtered := recipes[:0:0]
				for _, r := range recipes {
					if r.Type != "smelting" {
						continue
					}
					if !r.HasAllItemsQty(p.CountItem) {
						continue
					}
					skillLevel := p.Level(player.SkillName(r.Skill))
					if skillLevel < r.Level {
						continue
					}
					filtered = append(filtered, r)
				}
				if len(filtered) == 0 {
					sess.WriteLine("You can't smelt that here.")
					return
				}
				if len(filtered) == 1 {
					g.startSmelt(sess, p, &filtered[0], def.Name)
					return
				}
				sess.PendingRecipeItem = itemAID
				sess.PendingCookMenu = smeltRecipesToMenuData(filtered)
				sess.State = net.StateSmeltRecipe
				sess.WriteLine("\nWhat would you like to smelt?")
				for i, r := range filtered {
					sess.WriteLine(fmt.Sprintf("  %d) %s", i+1, g.recipeName(sess, &r)))
				}
				sess.WriteLine(fmt.Sprintf("  %d) Nothing", len(filtered)+1))
				return
			}
			if len(recipes) == 1 {
				g.startCook(sess, p, &recipes[0], def.Name)
				return
			}
			sess.PendingRecipeItem = itemAID
			sess.PendingCookMenu = recipesToMenuData(recipes)
			sess.State = net.StateCookRecipe
			sess.WriteLine(fmt.Sprintf("\nWhat would you like to make with %s on the %s?", itemAName, def.Name))
			for i, r := range recipes {
				sess.WriteLine(fmt.Sprintf("  %d) %s", i+1, g.recipeName(sess, &r)))
			}
			return
		}

		g.StartAction(sess, "use", itemBName)
		return
	}

	matchesB = g.findInventoryMatches(itemBName, p)
	if len(matchesB) > 0 {
		uniqueB := uniqueItemNames(matchesB)
		if len(uniqueB) > 1 {
			sess.WriteLine("Which '" + itemBName + "'?")
			for _, name := range uniqueB {
				sess.WriteLine(fmt.Sprintf("  - %s", name))
			}
			return
		}

		itemBID := matchesB[0].ID

		matched := g.findCombineResults(sess, p, itemAID, itemBID)
		if len(matched) == 1 {
			g.produceCombined(sess, p, matched[0])
			return
		}
		if len(matched) > 1 {
			sess.PendingCookMenu = combineMenuData(matched)
			sess.State = net.StateCookRecipe
			sess.WriteLine("\nWhat would you like to make?")
			for i, def := range matched {
				sess.WriteLine(fmt.Sprintf("  %d) %s", i+1, g.itemColorize(sess, def, def.Name)))
			}
			return
		}

		sess.WriteLine("You can't combine those items.")
		return
	}

	sess.WriteLine(fmt.Sprintf("There's no '%s' here to use that on.", itemBName))
}

func (g *Game) findCombineResults(sess *net.Session, p *player.Player, itemAID, itemBID string) []*object.ItemDef {
	items, err := g.ItemStore.LoadAll()
	if err != nil {
		return nil
	}
	var matched []*object.ItemDef
	for _, def := range items {
		if len(def.MadeFrom) == 0 {
			continue
		}
		aEntry := -1
		bEntry := -1
		for ei, entry := range def.MadeFrom {
			for _, id := range entry.Items {
				if id == itemAID && aEntry < 0 {
					aEntry = ei
				}
				if id == itemBID && bEntry < 0 {
					bEntry = ei
				}
			}
		}
		if aEntry >= 0 && bEntry >= 0 && aEntry != bEntry && madeFromItemsAvailable(p, def.MadeFrom) {
			matched = append(matched, def)
		}
	}
	return matched
}

func (g *Game) produceCombined(sess *net.Session, p *player.Player, def *object.ItemDef) {
	freeSlot := p.FirstFreeSlot()
	if freeSlot == -1 {
		sess.WriteLine("Your inventory is too full.")
		return
	}
	for _, entry := range def.MadeFrom {
		for _, id := range entry.Items {
			if p.HasItem(id) {
				p.RemoveItem(id, entry.Qty)
				break
			}
		}
	}
	p.SetInvSlot(freeSlot, &player.InventorySlot{ItemID: def.ID, Quantity: 1})
	g.AccountStore.SaveCharacter(p)
	sess.WriteLine(fmt.Sprintf("You combine the ingredients and create %s.", g.itemColorize(sess, def, def.Name)))
}

func combineMenuData(defs []*object.ItemDef) []map[string]string {
	out := make([]map[string]string, len(defs))
	for i, d := range defs {
		out[i] = map[string]string{"combine_item": d.ID}
	}
	return out
}

func madeFromItemsAvailable(p *player.Player, madeFrom []object.MadeFromEntry) bool {
	for _, entry := range madeFrom {
		found := false
		for _, id := range entry.Items {
			if p.HasItem(id) {
				found = true
				break
			}
		}
		if !found {
			return false
		}
	}
	return true
}