aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_use.go
blob: a2cddaf45099b762ea1e04288607bcaba3b4a893 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package game

import (
	"fmt"
	"strings"

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

func (g *Game) executeUse(sess *net.Session, args []string, rawInput string) {
	if g.tryFinishingBlow(sess, strings.Join(args, " ")) {
		return
	}
	g.doUse(sess, strings.Join(args, " "))
}

func (g *Game) doUse(sess *net.Session, input string) {
	if strings.TrimSpace(input) == "" {
		g.doHelp(sess, "use")
		return
	}

	p := sess.Player
	g.cancelAction(p)

	if g.tryRecharge(sess, p, input) {
		return
	}

	lower := strings.ToLower(input)

	if lower == "bank" || lower == "bank booth" || strings.HasPrefix(lower, "bank ") {
		g.doBank(sess)
		return
	}

	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 {
		if g.useRoomObject(sess, p, input) {
			return
		}
		g.doUseItemOnTarget(sess, p, strings.Fields(input)[0], strings.Join(strings.Fields(input)[1:], " "))
		return
	}

	if g.useRoomObject(sess, p, input) {
		return
	}

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

func (g *Game) useRoomObject(sess *net.Session, p *player.Player, input string) bool {
	objInstances := g.World.FindObjInstances(p.RoomID, input)
	if len(objInstances) == 0 {
		return false
	}
	objSt := &objInstances[0]
	def, _ := g.ObjectStore.Load(objSt.DefID)
	if def == nil {
		return false
	}
	recipes := g.stationRecipes(p, objSt.DefID)
	if len(recipes) > 0 {
		g.showRecipeMenu(sess, p, recipes, def.Name)
		return true
	}
	bt := def.BehaviorType()
	if bt != "" {
		g.startAction(sess, bt, def.Name)
		return true
	}
	for _, ui := range def.UseInteractions {
		if ui.Item != "" {
			continue
		}
		if ui.Condition != nil && !g.checkCondition(sess, ui.Condition) {
			continue
		}
		if ui.Message != "" {
			sess.WriteLine(ui.Message)
		}
		if ui.Action != nil {
			g.applyNodeAction(sess, ui.Action)
		}
		g.broadcastAction(sess, "\n%s uses the %s.", p.Name, def.Name)
		return true
	}
	if len(def.UseInteractions) > 0 {
		sess.WriteLine(fmt.Sprintf("Use what on the %s?", def.Name))
		return true
	}
	sess.WriteLine(fmt.Sprintf("You can't use the %s.", def.Name))
	return true
}

func (g *Game) stationRecipes(p *player.Player, stationDefID string) []*object.ItemDef {
	items := g.CraftIndex.ByStation(stationDefID)
	var results []*object.ItemDef
	for _, item := range items {
		c := item.FirstCraft()
		if !craftHasAllItemsQty(c, p.CountItem) {
			continue
		}
		skillName := player.SkillName(c.EffectiveSkill())
		if p.Level(skillName) < c.Level {
			continue
		}
		if c.Tool != "" && !g.hasToolType(p, c.Tool) {
			continue
		}
		if stationDefID == "anvil" && !g.hasToolType(p, "hammer") {
			continue
		}
		results = append(results, item)
	}
	return results
}

func (g *Game) showRecipeMenu(sess *net.Session, p *player.Player, items []*object.ItemDef, stationName string) {
	if len(items) == 0 {
		sess.WriteLine(fmt.Sprintf("You don't have anything you can make at the %s.", stationName))
		return
	}
	if len(items) == 1 {
		g.promptHowMany(sess, items[0].ID)
		return
	}
	needTypes := false
	for i := range items {
		for j := i + 1; j < len(items); j++ {
			if len(items[i].Craft) > 0 && len(items[j].Craft) > 0 && items[i].FirstCraft().Type != items[j].FirstCraft().Type {
				needTypes = true
				break
			}
		}
		if needTypes {
			break
		}
	}
	menu := itemMenuData(itemIDs(items))
	sess.PendingMenu = menu
	sess.State = net.StateRecipeChoice
	var names []string
	for _, item := range items {
		cType := ""
		if len(item.Craft) > 0 {
			cType = item.FirstCraft().Type
		}
		if cType == "" {
			cType = "combine"
		}
		typeLabel := strings.ToUpper(cType[:1]) + cType[1:]
		if needTypes {
			names = append(names, fmt.Sprintf("%s (%s)", g.itemName(sess, item), typeLabel))
		} else {
			names = append(names, g.itemName(sess, item))
		}
	}
	g.showMenuTable(sess, fmt.Sprintf("What would you like to make at the %s?", stationName), names)
}

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 {
		g.showWhichOne(sess, matchesA)
		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
		}

		recipes := g.CraftIndex.FindByStationInput(def.ID, itemAID)

		if len(recipes) > 0 {
			cType := ""
			if len(recipes[0].Craft) > 0 {
				cType = recipes[0].FirstCraft().Type
			}
			if cType == "smelting" {
				var filtered []*object.ItemDef
				for _, item := range recipes {
					c := item.FirstCraft()
					if c.Type != "smelting" {
						continue
					}
					if !craftHasAllItemsQty(c, p.CountItem) {
						continue
					}
					skillLevel := p.Level(player.SkillName(c.EffectiveSkill()))
					if skillLevel < c.Level {
						continue
					}
					filtered = append(filtered, item)
				}
				if len(filtered) == 0 {
					sess.WriteLine("You can't smelt that here.")
					return
				}
				if len(filtered) == 1 {
					g.promptHowMany(sess, filtered[0].ID)
					return
				}
			sess.PendingMenu = itemMenuData(itemIDs(filtered))
				sess.State = net.StateRecipeChoice
				var names []string
				for _, item := range filtered {
					names = append(names, g.itemName(sess, item))
				}
				g.showMenuTable(sess, "What would you like to smelt?", names)
				return
			}
			if cType == "smithing" {
				if !g.hasToolType(p, "hammer") {
					sess.WriteLine("You need a hammer to smith.")
					return
				}
				g.showSmithTable(sess, p, itemAID)
				return
			}
			if len(recipes) == 1 {
				g.promptHowMany(sess, recipes[0].ID)
				return
			}
		sess.PendingMenu = itemMenuData(itemIDs(recipes))
			sess.State = net.StateRecipeChoice
			var names []string
			for _, item := range recipes {
				names = append(names, g.itemName(sess, item))
			}
			g.showMenuTable(sess, fmt.Sprintf("What would you like to make with %s on the %s?", itemAName, def.Name), names)
			return
		}

		for _, ui := range def.UseInteractions {
			if ui.Item != itemAID {
				continue
			}
			if ui.Condition != nil && !g.checkCondition(sess, ui.Condition) {
				continue
			}
			if ui.Message != "" {
				sess.WriteLine(ui.Message)
			}
			if ui.Action != nil {
				g.applyNodeAction(sess, ui.Action)
			}
			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 + "'?")
			seen := make(map[string]bool)
			for _, m := range matchesB {
				if seen[m.Name] {
					continue
				}
				seen[m.Name] = true
				def, _ := g.ItemStore.Load(m.ID)
				sess.WriteLine(fmt.Sprintf("  - %s", g.itemColorize(sess, def, m.Name)))
			}
			return
		}

		itemBID := matchesB[0].ID

		craftItems := g.findCraftItems(p, itemAID, itemBID)
		fletchItems := g.findFletchItems(p, itemAID, itemBID)

		if len(craftItems) > 0 && len(fletchItems) > 0 {
			var menu []map[string]string
			var names []string
			for _, item := range craftItems {
				menu = append(menu, map[string]string{"item_id": item.ID})
				names = append(names, fmt.Sprintf("%s (Crafting)", item.Name))
			}
			for _, item := range fletchItems {
				menu = append(menu, map[string]string{"item_id": item.ID})
				names = append(names, fmt.Sprintf("%s (Fletching)", item.Name))
			}
			sess.PendingMenu = menu
			sess.State = net.StateRecipeChoice
			g.showMenuTable(sess, "What would you like to make?", names)
			return
		}

		if len(craftItems) > 0 {
			var available []*object.ItemDef
			for _, item := range craftItems {
				if g.canDoItem(p, item, player.Crafting) {
					available = append(available, item)
				}
			}
			if len(available) == 1 {
				g.startItem(sess, p, available[0], 0, "last_craft")
				return
			}
			if len(available) > 1 {
				g.showProductionTable(sess, p, available, "Crafting", "crafting", "last_craft", "Craft", false)
				return
			}
		}

		if len(fletchItems) > 0 {
			var available []*object.ItemDef
			for _, item := range fletchItems {
				if g.canDoFletchItem(p, item) {
					available = append(available, item)
				}
			}
			if len(available) == 1 {
				g.startFletchAction(sess, p, available[0], 0)
				return
			}
			if len(available) > 1 {
				g.showProductionTable(sess, p, available, "Fletching", "fletching", "last_fletch", "Fletch", true)
				return
			}
		}

		matched := g.CraftIndex.FindByTwoInputs(itemAID, itemBID)
		var matchable []*object.ItemDef
		for _, item := range matched {
			if craftHasAllItemsQty(item.FirstCraft(), p.CountItem) {
				matchable = append(matchable, item)
			}
		}
		if len(matchable) == 1 {
			g.promptHowMany(sess, matchable[0].ID)
			return
		}
		if len(matchable) > 1 {
		sess.PendingMenu = itemMenuData(itemIDs(matchable))
			sess.State = net.StateRecipeChoice
			var names []string
			for _, item := range matchable {
				names = append(names, g.itemName(sess, item))
			}
			g.showMenuTable(sess, "What would you like to make?", names)
			return
		}

		if g.isGrimyHerb(itemAID) && itemBID == "vial_of_water" {
			sess.WriteLine("Don't put a grimy herb in there! Clean it first!")
			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) isGrimyHerb(itemID string) bool {
	items := g.CraftIndex.ByType("clean")
	for _, item := range items {
		for _, e := range item.FirstCraft().Consume {
			for _, id := range e.Items {
				if id == itemID {
					return true
				}
			}
		}
	}
	return false
}