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
231
232
233
234
235
236
237
238
239
|
package game
import (
"strings"
"thehouseoficarus/internal/combat"
"thehouseoficarus/internal/net"
"thehouseoficarus/internal/object"
"thehouseoficarus/internal/player"
)
func (g *Game) executeFletch(sess *net.Session, args []string, rawInput string) {
g.doFletch(sess, strings.Join(args, " "))
}
var fletchLogItems = map[string]bool{
"logs": true, "oak_logs": true, "willow_logs": true,
"maple_logs": true, "yew_logs": true, "magic_logs": true,
}
var fletchGemItems = map[string]bool{
"uncut_sapphire": true, "uncut_emerald": true,
"uncut_ruby": true, "uncut_diamond": true,
}
func fletchNeedsKnife(item *object.ItemDef) bool {
return item.FindCraft(func(c object.CraftDef) bool {
if c.Tool == "knife" {
return true
}
for _, e := range c.Consume {
for _, id := range e.Items {
if fletchLogItems[id] {
if strings.Contains(item.ID, "shaft") || strings.Contains(item.ID, "bow") {
return true
}
}
}
}
return false
}) != nil
}
func fletchNeedsChisel(item *object.ItemDef) bool {
return item.FindCraft(func(c object.CraftDef) bool {
if c.Tool == "chisel" {
return true
}
for _, e := range c.Consume {
for _, id := range e.Items {
if fletchGemItems[id] {
return true
}
}
}
return false
}) != nil
}
func (g *Game) doFletch(sess *net.Session, input string) {
p := sess.Player
if combat.GetCombat(p.Name) != nil {
sess.WriteLine("You can't do that during combat!")
return
}
if p.Action != nil {
p.Action = nil
}
items := g.CraftIndex.ByType("fletching")
if input != "" {
g.doFletchWithInput(sess, p, items, input)
return
}
lastItemID, _ := p.Flags["last_fletch"].(string)
if lastItemID != "" {
for _, item := range items {
if item.ID == lastItemID {
if g.canDoFletchItem(p, item) {
g.startFletchAction(sess, p, item, 0)
return
}
break
}
}
}
available := g.availableFletchItems(p, items)
if len(available) == 0 {
sess.WriteLine("You don't have any materials to fletch.")
return
}
if p.OptionBool("fletch_all") && len(available) == 1 {
g.startFletchAction(sess, p, available[0], 0)
return
}
g.showProductionTable(sess, p, available, "Fletching", "fletching", "last_fletch", "Fletch", true)
}
func (g *Game) doFletchWithInput(sess *net.Session, p *player.Player, items []*object.ItemDef, input string) {
qty, productName := parseQty(input)
productName = strings.TrimSpace(productName)
var matched []*object.ItemDef
for _, item := range items {
if object.WordPrefixMatch(productName, item.Name) {
matched = append(matched, item)
}
}
if len(matched) == 0 {
sess.WriteLine("You can't fletch that.")
return
}
var available []*object.ItemDef
for _, item := range matched {
if g.canDoFletchItem(p, item) {
available = append(available, item)
}
}
if len(available) == 0 {
sess.WriteLine("You don't have the materials for that.")
return
}
if len(available) == 1 {
g.startFletchAction(sess, p, available[0], qty)
return
}
g.showProductionTable(sess, p, available, "Fletching", "fletching", "last_fletch", "Fletch", true)
}
func (g *Game) hasFletchTools(p *player.Player, item *object.ItemDef) bool {
c := item.FirstCraft()
if c.Tool != "" {
return g.hasToolType(p, c.Tool)
}
if fletchNeedsKnife(item) && !g.hasToolType(p, "knife") {
return false
}
if fletchNeedsChisel(item) && !g.hasToolType(p, "chisel") {
return false
}
return true
}
func (g *Game) canDoFletchItem(p *player.Player, item *object.ItemDef) bool {
return item.FindCraft(func(c object.CraftDef) bool {
return p.Level(player.Fletching) >= c.Level &&
craftHasAllItemsQty(&c, p.CountItem)
}) != nil && g.hasFletchTools(p, item)
}
func (g *Game) availableFletchItems(p *player.Player, allItems []*object.ItemDef) []*object.ItemDef {
var result []*object.ItemDef
for _, item := range allItems {
match := item.FindCraft(func(c object.CraftDef) bool {
return craftHasAllItemsQty(&c, p.CountItem)
})
if match != nil && g.hasFletchTools(p, item) {
result = append(result, item)
}
}
return result
}
func (g *Game) findFletchItems(p *player.Player, itemAID, itemBID string) []*object.ItemDef {
items := g.CraftIndex.ByType("fletching")
toolTypeA := ""
toolTypeB := ""
if defA, _ := g.ItemStore.Load(itemAID); defA != nil {
toolTypeA = defA.ToolType
}
if defB, _ := g.ItemStore.Load(itemBID); defB != nil {
toolTypeB = defB.ToolType
}
isToolA := toolTypeA == "knife" || toolTypeA == "chisel"
isToolB := toolTypeB == "knife" || toolTypeB == "chisel"
var matched []*object.ItemDef
for _, item := range items {
c := item.FirstCraft()
if c.Tool != "" {
if c.Tool == toolTypeA && craftMatchesEntry(c, itemBID) {
matched = append(matched, item)
continue
}
if c.Tool == toolTypeB && craftMatchesEntry(c, itemAID) {
matched = append(matched, item)
continue
}
}
aMatch := false
bMatch := false
for _, e := range c.Consume {
for _, id := range e.Items {
if id == itemAID {
aMatch = true
}
if id == itemBID {
bMatch = true
}
}
}
if aMatch && bMatch {
matched = append(matched, item)
continue
}
if isToolA && bMatch {
if (fletchNeedsKnife(item) && toolTypeA == "knife") ||
(fletchNeedsChisel(item) && toolTypeA == "chisel") {
matched = append(matched, item)
continue
}
}
if isToolB && aMatch {
if (fletchNeedsKnife(item) && toolTypeB == "knife") ||
(fletchNeedsChisel(item) && toolTypeB == "chisel") {
matched = append(matched, item)
continue
}
}
}
return matched
}
|