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

import (
	"fmt"
	"strconv"
	"strings"

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

type smeltableEntry struct {
	ItemName string
	Recipe   action.RecipeDef
}

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

	stationName := g.findFurnace(p.RoomID)
	if stationName == "" {
		sess.WriteLine("You need a furnace to smelt.")
		return
	}

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

	if input == "" {
		g.showSmeltMenu(sess, p, allRecipes, stationName)
		return
	}

	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 {
		sess.WriteLine("Which one?")
		for _, name := range unique {
			sess.WriteLine(fmt.Sprintf("  - %s", name))
		}
		return
	}

	itemID := matches[0].ID

	var recipes []action.RecipeDef
	for _, r := range allRecipes {
		if r.Type != "smelting" {
			continue
		}
		if !smeltStationOK(stationName, r.Station) {
			continue
		}
		if !r.MatchesEntry(itemID) {
			continue
		}
		if !r.HasAllItemsQty(p.CountItem) {
			continue
		}
		skillLevel := p.Level(player.SkillName(r.Skill))
		if skillLevel < r.Level {
			continue
		}
		recipes = append(recipes, r)
	}

	if len(recipes) == 0 {
		sess.WriteLine("You can't smelt that here.")
		return
	}

	if len(recipes) == 1 {
		g.startSmelt(sess, p, &recipes[0], stationName)
		return
	}

	sess.PendingRecipeItem = itemID
	sess.PendingCookMenu = smeltRecipesToMenuData(recipes)
	sess.State = net.StateSmeltRecipe
	sess.WriteLine("\nWhat would you like to smelt?")
	for i, r := range recipes {
		sess.WriteLine(fmt.Sprintf("  %d) %s", i+1, g.recipeName(sess, &r)))
	}
	sess.WriteLine(fmt.Sprintf("  %d) Nothing", len(recipes)+1))
}

func (g *Game) showSmeltMenu(sess *net.Session, p *player.Player, allRecipes []action.RecipeDef, stationName string) {
	seen := make(map[string]bool)
	var smeltables []smeltableEntry

	for _, r := range allRecipes {
		if r.Type != "smelting" {
			continue
		}
		if !smeltStationOK(stationName, r.Station) {
			continue
		}
		if seen[r.ID] {
			continue
		}
		if !r.HasAllItemsQty(p.CountItem) {
			continue
		}
		skillLevel := p.Level(player.SkillName(r.Skill))
		if skillLevel < r.Level {
			continue
		}
		seen[r.ID] = true
		smeltables = append(smeltables, smeltableEntry{g.recipeName(sess, &r), r})
	}

	if len(smeltables) == 0 {
		sess.WriteLine("You don't have anything you can smelt.")
		return
	}

	if len(smeltables) == 1 {
		g.startSmelt(sess, p, &smeltables[0].Recipe, stationName)
		return
	}

	sess.PendingRecipeItem = ""
	sess.State = net.StateSmeltRecipe
	sess.WriteLine("\nWhat would you like to smelt?")
	for i, s := range smeltables {
		sess.WriteLine(fmt.Sprintf("  %d) %s", i+1, s.ItemName))
	}
	sess.WriteLine(fmt.Sprintf("  %d) Nothing", len(smeltables)+1))

	sess.PendingCookMenu = makeSmeltMenuData(smeltables)
}

func (g *Game) handleSmeltRecipe(sess *net.Session, input string) {
	p := sess.Player.(*player.Player)
	input = strings.TrimSpace(input)
	if input == "" {
		return
	}

	choice, err := strconv.Atoi(input)
	if err != nil {
		sess.State = net.StateGame
		sess.PendingRecipeItem = ""
		sess.PendingCookMenu = nil
		sess.WriteLine("You decide not to smelt anything.")
		g.reprompt(sess)
		return
	}

	if len(sess.PendingCookMenu) > 0 {
		menuData := sess.PendingCookMenu
		sess.PendingCookMenu = nil
		if choice <= 0 || choice > len(menuData)+1 {
			sess.WriteLine("Invalid choice.")
			return
		}
		sess.State = net.StateGame
		if choice == len(menuData)+1 {
			sess.WriteLine("You decide not to smelt anything.")
			g.reprompt(sess)
			return
		}
		entry := menuData[choice-1]

		all, loadErr := g.RecipeStore.LoadAll()
		if loadErr != nil {
			g.reprompt(sess)
			return
		}
		for _, r := range all {
			if r.ID == entry["recipe_id"] {
				stationName := g.findFurnace(p.RoomID)
				g.startSmelt(sess, p, &r, stationName)
				return
			}
		}
		g.reprompt(sess)
		return
	}

	sess.State = net.StateGame
	g.reprompt(sess)
}

func smeltStationOK(stationName string, recipeStations []string) bool {
	for _, rs := range recipeStations {
		if rs == "furnace" && stationName == "furnace" {
			return true
		}
	}
	return false
}

func (g *Game) findFurnace(roomID int) string {
	for _, obj := range g.World.AllObjInstances(roomID) {
		if obj.Depleted {
			continue
		}
		if obj.DefID == "furnace" {
			return "furnace"
		}
	}
	return ""
}

func makeSmeltMenuData(smeltables []smeltableEntry) []map[string]string {
	out := make([]map[string]string, len(smeltables))
	for i, s := range smeltables {
		out[i] = map[string]string{"recipe_id": s.Recipe.ID}
	}
	return out
}

func smeltRecipesToMenuData(recipes []action.RecipeDef) []map[string]string {
	out := make([]map[string]string, len(recipes))
	for i, r := range recipes {
		out[i] = map[string]string{"recipe_id": r.ID}
	}
	return out
}