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

import (
	"fmt"
	"sort"
	"strings"

	"thehouseoficarus/internal/item"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

func (g *Game) executeSmith(sess *net.Session, args []string, rawInput string) {
	g.doSmith(sess, strings.Join(args, " "))
}

func (g *Game) doSmith(sess *net.Session, input string) {
	p := sess.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
	}

	items := g.CraftIndex.ByType("smithing")

	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 !hasSmithingItems(items, barID) {
			sess.WriteLine("You can't smith that.")
			return
		}
		g.showSmithTable(sess, p, barID)
		return
	}

	barTypes := findSmithableBars(items, 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 item := g.findSmithItem(p, barTypes[0], items); item != nil {
				g.startProductionFromItem(sess, p, item, 0)
				return
			}
		}
		g.showSmithTable(sess, p, barTypes[0])
		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) {
	barDef, _ := g.ItemStore.Load(barID)
	barName := barID
	if barDef != nil {
		barName = barDef.Name
	}

	allItems := g.CraftIndex.ByType("smithing")
	var recipes []*item.ItemDef
	for _, item := range allItems {
		if craftMatchesEntry(item.FirstCraft(), barID) {
			recipes = append(recipes, item)
		}
	}

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

	lastFlag := fmt.Sprintf("last_smith_%s", barID)
	g.showProductionTable(sess, p, recipes, titleCase(barName)+" Smithing", "smithing", lastFlag, "Produce", false)
}

func hasSmithingItems(items []*item.ItemDef, barID string) bool {
	for _, item := range items {
		if craftMatchesEntry(item.FirstCraft(), barID) {
			return true
		}
	}
	return false
}

func findSmithableBars(items []*item.ItemDef, p *player.Player) []string {
	barSet := make(map[string]bool)
	for _, item := range items {
		for _, e := range item.FirstCraft().Ingredients {
			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 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) findSmithItem(p *player.Player, barID string, items []*item.ItemDef) *item.ItemDef {
	var makeable []*item.ItemDef
	skillLevel := p.Level(player.Smithing)
	for _, item := range items {
		c := item.FirstCraft()
		if !craftMatchesEntry(c, barID) {
			continue
		}
		if skillLevel >= c.Level && craftHasAllItemsQty(c, p.CountItem) {
			makeable = append(makeable, item)
		}
	}
	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, " ")
}