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

import (
	"strings"

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

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

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

	var constructRecipes []action.RecipeDef
	for _, r := range allRecipes {
		if r.Type != "construction" {
			continue
		}
		if !g.constructRecipeVisible(p, &r) {
			continue
		}
		constructRecipes = append(constructRecipes, r)
	}

	if input != "" {
		g.doConstructWithInput(sess, p, constructRecipes, input)
		return
	}

	lastRecipeID, _ := p.Flags["last_construct"].(string)
	if lastRecipeID != "" {
		for i := range constructRecipes {
			if constructRecipes[i].ID == lastRecipeID {
				r := &constructRecipes[i]
				if g.canDoConstructRecipe(p, r) {
					g.startConstructRecipe(sess, p, r, 0)
					return
				}
				break
			}
		}
	}

	available := g.availableConstructRecipes(p, constructRecipes)
	if len(available) == 0 {
		sess.WriteLine("You don't have any materials to construct.")
		return
	}

	if p.OptionBool("construct_all") && len(available) == 1 {
		g.startConstructRecipe(sess, p, &available[0], 0)
		return
	}

	g.showProductionTable(sess, p, available, "Construction", "construction", "last_construct", "Construct", false)
}

func (g *Game) doConstructWithInput(sess *net.Session, p *player.Player, constructRecipes []action.RecipeDef, input string) {
	qty, productName := parseQty(input)
	productName = strings.TrimSpace(productName)

	var matched []action.RecipeDef
	for _, r := range constructRecipes {
		outDef, _ := g.ItemStore.Load(r.Output)
		name := r.Output
		if outDef != nil {
			name = outDef.Name
		}
		if world.WordPrefixMatch(productName, name) {
			matched = append(matched, r)
		}
	}

	if len(matched) == 0 {
		sess.WriteLine("You can't construct that.")
		return
	}

	var available []action.RecipeDef
	for _, r := range matched {
		if g.canDoConstructRecipe(p, &r) {
			available = append(available, r)
		}
	}

	if len(available) == 0 {
		sess.WriteLine("You don't have the materials or level for that.")
		return
	}

	if len(available) == 1 {
		g.startConstructRecipe(sess, p, &available[0], qty)
		return
	}

	g.showProductionTable(sess, p, available, "Construction", "construction", "last_construct", "Construct", false)
}

func (g *Game) constructRecipeVisible(p *player.Player, r *action.RecipeDef) bool {
	if len(r.Station) > 0 {
		stID, _ := g.findStation(p.RoomID, r.Station)
		if stID == "" {
			return false
		}
	}
	if r.Tool != "" && !g.hasToolType(p, r.Tool) {
		return false
	}
	return true
}

func (g *Game) canDoConstructRecipe(p *player.Player, r *action.RecipeDef) bool {
	if p.Level(player.Construction) < r.Level {
		return false
	}
	if !r.HasAllItemsQty(p.CountItem) {
		return false
	}
	if len(r.Station) > 0 {
		stID, _ := g.findStation(p.RoomID, r.Station)
		if stID == "" {
			return false
		}
	}
	if r.Tool != "" && !g.hasToolType(p, r.Tool) {
		return false
	}
	return true
}

func (g *Game) availableConstructRecipes(p *player.Player, allConstruct []action.RecipeDef) []action.RecipeDef {
	var result []action.RecipeDef
	for _, r := range allConstruct {
		if r.HasAllItemsQty(p.CountItem) {
			result = append(result, r)
		}
	}
	return result
}

func (g *Game) startConstructRecipe(sess *net.Session, p *player.Player, recipe *action.RecipeDef, count int) {
	if p.Flags == nil {
		p.Flags = make(map[string]any)
	}
	p.Flags["last_construct"] = recipe.ID
	g.AccountStore.SaveCharacter(p)

	g.startProductionFromRecipe(sess, p, recipe, count)
}