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

import (
	"fmt"
	"strings"

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

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

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

	stationDefID, stationName := g.findStation(p.RoomID, []string{"furnace"})
	if stationDefID == "" {
		sess.WriteLine("You need a furnace to smelt.")
		return
	}

	items := 	g.CraftIndex.BySubtype("smelting")

	if input == "" {
		g.showSmeltMenu(sess, p, items, stationDefID, 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 {
		g.showWhichOne(sess, matches)
		return
	}

	itemID := matches[0].ID

	var recipes []*item.ItemDef
	for _, item := range items {
		c := item.FirstCraft()
		if !stationMatch(stationDefID, c.Station) || !craftMatchesEntry(c, itemID) {
			continue
		}
		if !craftHasAllItemsQty(c, p.CountItem) || p.Level(player.SkillName(c.EffectiveSkill())) < c.Level {
			continue
		}
		recipes = append(recipes, item)
	}

	g.showRecipeChoice(sess, p, recipes, "You can't smelt that.", "What would you like to smelt?", "")
}

func (g *Game) showSmeltMenu(sess *net.Session, p *player.Player, items []*item.ItemDef, stationDefID, stationName string) {
	seen := make(map[string]bool)
	var recipes []*item.ItemDef

	for _, item := range items {
		c := item.FirstCraft()
		if !stationMatch(stationDefID, c.Station) {
			continue
		}
		if seen[item.ID] || !craftHasAllItemsQty(c, p.CountItem) || p.Level(player.SkillName(c.EffectiveSkill())) < c.Level {
			continue
		}
		seen[item.ID] = true
		recipes = append(recipes, item)
	}

	g.showRecipeChoice(sess, p, recipes, "You don't have anything you can smelt.", "What would you like to smelt?", "smelt_all")
}