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

import (
	"fmt"
	"sort"
	"strings"

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

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

	stationDefID, _ := g.findStation(p.RoomID, []string{"anvil"})
	if stationDefID == "" {
		sess.WriteLine("You need an anvil to smith.")
		return
	}

	if !g.hasToolType(p, "hammer") {
		sess.WriteLine("You need a hammer to smith.")
		return
	}

	allRecipes, err := g.RecipeStore.LoadAll()
	if err != nil {
		sess.WriteLine("Error loading recipes.")
		return
	}

	if input != "" {
		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 {
			g.showWhichOne(sess, matches)
			return
		}
		barID := matches[0].ID
		if !hasSmithingRecipes(allRecipes, barID) {
			sess.WriteLine("You can't smith that.")
			return
		}
		g.showSmithTable(sess, p, barID, allRecipes)
		return
	}

	barTypes := findSmithableBars(allRecipes, p)
	if len(barTypes) == 0 {
		sess.WriteLine("You don't have any bars to smith.")
		return
	}

	if len(barTypes) == 1 {
		if p.OptionBool("smith_all") {
			if recipe := g.findSingleSmithRecipe(p, barTypes[0], allRecipes); recipe != nil {
				g.startProductionFromRecipe(sess, p, recipe, 0)
				return
			}
		}
		g.showSmithTable(sess, p, barTypes[0], allRecipes)
		return
	}

	sess.PendingMenu = barMenuData(barTypes)
	sess.State = net.StateRecipeChoice
	var names []string
	for _, barID := range barTypes {
		def, _ := g.ItemStore.Load(barID)
		name := barID
		if def != nil {
			name = g.itemColorize(sess, def, def.Name)
		}
		names = append(names, name)
	}
	g.showMenuTable(sess, "Which bars would you like to smith?", names)
}

func (g *Game) showSmithTable(sess *net.Session, p *player.Player, barID string, allRecipes []action.RecipeDef) {
	barDef, _ := g.ItemStore.Load(barID)
	barName := barID
	if barDef != nil {
		barName = barDef.Name
	}

	var recipes []action.RecipeDef
	for _, r := range allRecipes {
		if r.Type == "smithing" && r.MatchesEntry(barID) {
			recipes = append(recipes, r)
		}
	}

	if len(recipes) == 0 {
		sess.WriteLine("There's nothing to smith with that.")
		return
	}

	sort.Slice(recipes, func(i, j int) bool {
		return recipes[i].Level < recipes[j].Level
	})

	mode := g.colorMode(sess)
	skillLevel := p.Level(player.Smithing)
	barCount := p.CountItem(barID)
	dimSpec := color.Parse("240")

	tbl := &Table{
		Title:   titleCase(barName) + " Smithing",
		Columns: []string{"#", "Product", "Inputs", "Level Req."},
	}

	for i, r := range recipes {
		outDef, _ := g.ItemStore.Load(r.Output)
		productName := r.Output
		if outDef != nil {
			productName = outDef.Name
		}

		outputQty := r.OutputQty
		if outputQty <= 0 {
			outputQty = 1
		}
		if outDef != nil && outDef.Stackable && outputQty > 1 {
			productName = fmt.Sprintf("%s x%d", productName, outputQty)
		}

		bars := barsRequired(r, barID)
		inputStr := "1 bar"
		if bars > 1 {
			inputStr = fmt.Sprintf("%d bars", bars)
		}
		levelStr := fmt.Sprint(r.Level)
		numStr := fmt.Sprintf("%d", i+1)

		canMake := barCount >= bars && skillLevel >= r.Level
		if canMake {
			productName = g.itemColorize(sess, outDef, productName)
		} else {
			productName = color.Render(mode, dimSpec, productName)
			inputStr = color.Render(mode, dimSpec, inputStr)
			levelStr = color.Render(mode, dimSpec, levelStr)
			numStr = color.Render(mode, dimSpec, numStr)
		}

		tbl.Rows = append(tbl.Rows, []string{numStr, productName, inputStr, levelStr})
	}

	unicode := p.OptionBool("unicode")
	sess.WriteLine("")
	for _, line := range tbl.Render(unicode) {
		sess.WriteLine(line)
	}

	lastFlag := fmt.Sprintf("last_smith_%s", barID)
	lastRecipeID, _ := p.Flags[lastFlag].(string)
	hint := ""
	if lastRecipeID != "" {
		for _, r := range recipes {
			if r.ID == lastRecipeID {
				if outDef, err := g.ItemStore.Load(r.Output); err == nil {
					hint = outDef.Name
				}
				break
			}
		}
	}

	if hint != "" {
		sess.Write(fmt.Sprintf("Produce what (enter for all %s): ", hint))
	} else {
		sess.Write("Produce what: ")
	}

	sess.PendingRecipeID = barID
	sess.State = net.StateSmithProduct
}

func (g *Game) handleSmithProduct(sess *net.Session, input string) {
	p := sess.Player.(*player.Player)
	barID := sess.PendingRecipeID
	sess.PendingRecipeID = ""
	sess.State = net.StateGame

	input = strings.TrimSpace(input)

	allRecipes, err := g.RecipeStore.LoadAll()
	if err != nil {
		g.reprompt(sess)
		return
	}
	var recipes []action.RecipeDef
	for _, r := range allRecipes {
		if r.Type == "smithing" && r.MatchesEntry(barID) {
			recipes = append(recipes, r)
		}
	}

	sort.Slice(recipes, func(i, j int) bool {
		return recipes[i].Level < recipes[j].Level
	})

	lastFlag := fmt.Sprintf("last_smith_%s", barID)
	lastRecipeID, _ := p.Flags[lastFlag].(string)

	sel, errMsg := g.resolveRecipeByInput(input, recipes, lastRecipeID)
	switch errMsg {
	case "ambiguous":
		sess.WriteLine("That's ambiguous.")
		g.reprompt(sess)
		return
	case "never_mind":
		sess.WriteLine("Never mind.")
		g.reprompt(sess)
		return
	}

	skillLevel := p.Level(player.Smithing)
	if skillLevel < sel.Recipe.Level {
		sess.WriteLine(fmt.Sprintf("You need level %d smithing to make that.", sel.Recipe.Level))
		g.reprompt(sess)
		return
	}
	if !sel.Recipe.HasAllItemsQty(p.CountItem) {
		sess.WriteLine("You don't have enough bars.")
		g.reprompt(sess)
		return
	}

	if p.Flags == nil {
		p.Flags = make(map[string]any)
	}
	p.Flags[lastFlag] = sel.Recipe.ID
	g.AccountStore.SaveCharacter(p)

	g.startProductionFromRecipe(sess, p, sel.Recipe, sel.Count)
}

func (g *Game) hasToolType(p *player.Player, toolType string) bool {
	if itemID, ok := p.Equipment[object.SlotMainHand]; ok {
		if def, err := g.ItemStore.Load(itemID); err == nil && def.ToolType == toolType {
			return true
		}
	}
	for i := 0; i < 28; i++ {
		slot := p.InvSlot(i)
		if slot == nil {
			continue
		}
		if def, err := g.ItemStore.Load(slot.ItemID); err == nil && def.ToolType == toolType {
			return true
		}
	}
	return false
}

func hasSmithingRecipes(allRecipes []action.RecipeDef, barID string) bool {
	for _, r := range allRecipes {
		if r.Type == "smithing" && r.MatchesEntry(barID) {
			return true
		}
	}
	return false
}

func findSmithableBars(allRecipes []action.RecipeDef, p *player.Player) []string {
	barSet := make(map[string]bool)
	for _, r := range allRecipes {
		if r.Type != "smithing" {
			continue
		}
		for _, e := range r.Consume {
			for _, id := range e.Items {
				if p.HasItem(id) {
					barSet[id] = true
				}
			}
		}
	}
	var bars []string
	for id := range barSet {
		bars = append(bars, id)
	}
	sort.Strings(bars)
	return bars
}

func barsRequired(r action.RecipeDef, barID string) int {
	for _, e := range r.Consume {
		for _, id := range e.Items {
			if id == barID {
				qty := e.Qty
				if qty <= 0 {
					qty = 1
				}
				return qty
			}
		}
	}
	return 1
}

func barMenuData(barIDs []string) []map[string]string {
	out := make([]map[string]string, len(barIDs))
	for i, id := range barIDs {
		out[i] = map[string]string{"bar_id": id}
	}
	return out
}

func (g *Game) findSingleSmithRecipe(p *player.Player, barID string, allRecipes []action.RecipeDef) *action.RecipeDef {
	var makeable []*action.RecipeDef
	skillLevel := p.Level(player.Smithing)
	for i, r := range allRecipes {
		if r.Type != "smithing" || !r.MatchesEntry(barID) {
			continue
		}
		if skillLevel >= r.Level && r.HasAllItemsQty(p.CountItem) {
			makeable = append(makeable, &allRecipes[i])
		}
	}
	if len(makeable) == 1 {
		return makeable[0]
	}
	return nil
}

func titleCase(s string) string {
	words := strings.Fields(s)
	for i, w := range words {
		if len(w) > 0 {
			words[i] = strings.ToUpper(w[:1]) + w[1:]
		}
	}
	return strings.Join(words, " ")
}