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

import (
	"fmt"
	"sort"
	"strings"

	"thehouseoficarus/internal/action"
	"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
	}

	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.findSmithRecipe(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
	}

	lastFlag := fmt.Sprintf("last_smith_%s", barID)
	g.showProductionTable(sess, p, recipes, titleCase(barName)+" Smithing", "smithing", lastFlag, "Produce", 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 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) findSmithRecipe(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, " ")
}