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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
package game
import (
"fmt"
"math/rand"
"strconv"
"strings"
"thehouseoficarus/internal/action"
"thehouseoficarus/internal/engine"
"thehouseoficarus/internal/net"
"thehouseoficarus/internal/object"
"thehouseoficarus/internal/player"
)
type recipeEntry struct {
ItemName string
Recipe action.RecipeDef
}
func recipeMenuData(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
}
func entryMenuData(entries []recipeEntry) []map[string]string {
out := make([]map[string]string, len(entries))
for i, e := range entries {
out[i] = map[string]string{"recipe_id": e.Recipe.ID}
}
return out
}
func (g *Game) promptHowMany(sess *net.Session, recipeID string) {
sess.PendingRecipeID = recipeID
sess.State = net.StateHowMany
sess.Write("How many (return for all)?: ")
}
func (g *Game) handleHowMany(sess *net.Session, input string) {
p := sess.Player.(*player.Player)
recipeID := sess.PendingRecipeID
sess.PendingRecipeID = ""
sess.State = net.StateGame
input = strings.TrimSpace(input)
var count int
if input == "" {
count = 0
} else if n, err := strconv.Atoi(input); err == nil && n > 0 {
count = n
} else {
sess.WriteLine("Never mind.")
g.reprompt(sess)
return
}
var recipe *action.RecipeDef
if strings.HasPrefix(recipeID, "combine_") {
itemID := strings.TrimPrefix(recipeID, "combine_")
itemDef, err := g.ItemStore.Load(itemID)
if err != nil || len(itemDef.MadeFrom) == 0 {
g.reprompt(sess)
return
}
recipe = g.buildCombineRecipe(itemDef)
} else {
all, err := g.RecipeStore.LoadAll()
if err != nil {
g.reprompt(sess)
return
}
for _, r := range all {
if r.ID == recipeID {
recipe = &r
break
}
}
}
if recipe == nil {
g.reprompt(sess)
return
}
g.startProductionFromRecipe(sess, p, recipe, count)
}
func (g *Game) buildCombineRecipe(def *object.ItemDef) *action.RecipeDef {
var consume []action.ConsumeEntry
for _, mf := range def.MadeFrom {
consume = append(consume, action.ConsumeEntry{
Items: mf.Items,
Qty: mf.Qty,
Byproducts: mf.Byproducts,
})
}
wait := def.Ticks
if wait <= 0 {
wait = 2
}
return &action.RecipeDef{
ID: "combine_" + def.ID,
Type: "combine",
Wait: wait,
Consume: consume,
Output: def.ID,
Message: fmt.Sprintf("You create %s.", def.Name),
}
}
func (g *Game) startProduction(sess *net.Session, p *player.Player, recipe *action.RecipeDef, actionType, displayVerb, startMsg, endMsg string, count int) {
g.CancelAction(p)
if recipe.Skill != "" {
skillLevel := p.Level(player.SkillName(recipe.Skill))
if skillLevel < recipe.Level {
sess.WriteLine(fmt.Sprintf("You need level %d %s to do that.", recipe.Level, recipe.Skill))
return
}
}
if !recipe.HasAllItemsQty(p.CountItem) {
sess.WriteLine("You don't have the required materials.")
return
}
outputQty := recipe.OutputQty
if outputQty <= 0 {
outputQty = 1
}
canPlace := false
outDef, _ := g.ItemStore.Load(recipe.Output)
if outDef != nil && outDef.Stackable {
for i := 0; i < 28; i++ {
slot := p.InvSlot(i)
if slot != nil && slot.ItemID == recipe.Output {
canPlace = true
break
}
}
}
if !canPlace && p.FirstFreeSlot() == -1 {
sess.WriteLine("Your inventory is too full!")
return
}
wait := recipe.Wait
if wait <= 0 {
wait = 4
}
outputName := recipe.Output
if outDef != nil {
outputName = outDef.Name
}
p.Action = &action.Action{
Type: actionType,
TargetID: recipe.ID,
TargetName: outputName,
Data: map[string]any{
"recipe_id": recipe.ID,
"phase": 0,
"wait": wait,
"start_msg": startMsg,
"end_msg": endMsg,
"remaining": count,
},
WaitLeft: engine.ToTicks(1),
}
p.ActionState = &ActionState{Type: ActionProducing, TargetName: outputName, Verb: displayVerb}
}
func (g *Game) startProductionFromRecipe(sess *net.Session, p *player.Player, recipe *action.RecipeDef, count int) {
_, stationName := g.findStation(p.RoomID, recipe.Station)
if stationName == "" {
stationName = "inventory"
}
firstItem := recipe.FirstItemName(func(id string) (string, bool) {
def, err := g.ItemStore.Load(id)
if err != nil {
return id, false
}
return def.Name, true
})
var actionType, displayVerb, startMsg, endMsg string
switch recipe.Type {
case "cooking":
actionType = "cook"
displayVerb = "cooking"
if stationName != "inventory" {
startMsg = fmt.Sprintf("You start cooking %s on the %s.", firstItem, stationName)
} else {
startMsg = fmt.Sprintf("You start making %s.", firstItem)
}
endMsg = "You've cooked everything you can."
case "smelting":
actionType = "smelt"
displayVerb = "smelting"
startMsg = fmt.Sprintf("You begin to process %s in the furnace.", firstItem)
endMsg = "You've processed all the ore in your inventory."
case "smithing":
actionType = "smith"
displayVerb = "smithing"
startMsg = fmt.Sprintf("You hammer the %s on the anvil...", firstItem)
endMsg = "You've used all the bars you can."
case "combine":
actionType = "combine"
displayVerb = "combining"
startMsg = fmt.Sprintf("You start combining %s.", firstItem)
endMsg = "You've run out of materials."
default:
actionType = "craft"
displayVerb = "crafting"
startMsg = fmt.Sprintf("You start crafting with %s.", firstItem)
endMsg = "You've run out of materials."
}
g.startProduction(sess, p, recipe, actionType, displayVerb, startMsg, endMsg, count)
}
func (g *Game) loadProductionRecipe(recipeID string) *action.RecipeDef {
all, err := g.RecipeStore.LoadAll()
if err == nil {
for _, r := range all {
if r.ID == recipeID {
return &r
}
}
}
if strings.HasPrefix(recipeID, "combine_") {
itemID := strings.TrimPrefix(recipeID, "combine_")
if itemDef, err := g.ItemStore.Load(itemID); err == nil && len(itemDef.MadeFrom) > 0 {
return g.buildCombineRecipe(itemDef)
}
}
return nil
}
func (g *Game) advanceProduction(sess *net.Session, p *player.Player) bool {
recipeID := p.Action.Data["recipe_id"].(string)
phase := p.Action.Data["phase"].(int)
wait := p.Action.Data["wait"].(float64)
startMsg, _ := p.Action.Data["start_msg"].(string)
endMsg, _ := p.Action.Data["end_msg"].(string)
remaining, _ := p.Action.Data["remaining"].(int)
recipe := g.loadProductionRecipe(recipeID)
if recipe == nil {
g.CancelAction(p)
return false
}
if phase == 0 {
if startMsg != "" {
sess.WriteLine(fmt.Sprintf("\n%s", startMsg))
}
p.Action.Data["phase"] = 1
p.Action.WaitLeft = engine.ToTicks(wait)
return true
}
skillLevel := p.Level(player.SkillName(recipe.Skill))
chance := 1.0
if recipe.Success != nil {
chance = action.SuccessChance(*recipe.Success, skillLevel, recipe.Level)
}
if rand.Float64() < chance {
outputQty := recipe.OutputQty
if outputQty <= 0 {
outputQty = 1
}
byproducts := g.collectByproducts(p, recipe)
placed := false
outDef, _ := g.ItemStore.Load(recipe.Output)
if outDef != nil && outDef.Stackable {
for i := 0; i < 28; i++ {
slot := p.InvSlot(i)
if slot != nil && slot.ItemID == recipe.Output {
recipe.ConsumeAll(p.HasItem, p.RemoveItem)
slot.Quantity += outputQty
placed = true
break
}
}
}
if !placed {
freeSlot := p.FirstFreeSlot()
if freeSlot == -1 {
sess.WriteLine("Your inventory is too full!")
g.CancelAction(p)
return false
}
recipe.ConsumeAll(p.HasItem, p.RemoveItem)
p.SetInvSlot(freeSlot, &player.InventorySlot{ItemID: recipe.Output, Quantity: outputQty})
}
for _, bp := range byproducts {
if slot := p.FirstFreeSlot(); slot >= 0 {
p.SetInvSlot(slot, &player.InventorySlot{ItemID: bp, Quantity: 1})
}
}
if recipe.XP > 0 {
if newLevel := p.AddSkillXP(player.SkillName(recipe.Skill), recipe.XP); newLevel > 0 {
sess.WriteLine(g.colorize(sess, "level_up", fmt.Sprintf("*** You are now level %d %s! ***", newLevel, recipe.Skill)))
}
}
g.AccountStore.SaveCharacter(p)
msg := recipe.Message
if msg == "" {
outputName := recipe.Output
if outDef != nil {
outputName = outDef.Name
}
msg = fmt.Sprintf("You produce %s.", outputName)
}
if p.OptionBool("xp_drops") && recipe.XP > 0 {
abbr := player.SkillAbbr[player.SkillName(recipe.Skill)]
msg += g.colorize(sess, "xp", fmt.Sprintf(" (+%dxp %s)", recipe.XP, abbr))
}
sess.WriteLine(msg)
} else {
recipe.ConsumeAll(p.HasItem, p.RemoveItem)
if recipe.Fail != "" {
freeSlot := p.FirstFreeSlot()
if freeSlot >= 0 {
p.SetInvSlot(freeSlot, &player.InventorySlot{ItemID: recipe.Fail, Quantity: 1})
}
}
g.AccountStore.SaveCharacter(p)
msg := recipe.FailMessage
if msg == "" {
msg = "You fail and the materials are lost."
}
sess.WriteLine(g.colorize(sess, "damage", msg))
}
if remaining > 0 {
remaining--
p.Action.Data["remaining"] = remaining
if remaining <= 0 {
if endMsg != "" {
sess.WriteLine(fmt.Sprintf("\n%s", endMsg))
}
g.CancelAction(p)
return false
}
}
if g.canContinueProduction(p, recipe) {
p.Action.WaitLeft = engine.ToTicks(wait)
return true
}
if endMsg != "" {
sess.WriteLine(fmt.Sprintf("\n%s", endMsg))
}
g.CancelAction(p)
return false
}
func (g *Game) collectByproducts(p *player.Player, recipe *action.RecipeDef) []string {
var byproducts []string
for _, e := range recipe.Consume {
if len(e.Byproducts) == 0 {
continue
}
for i, id := range e.Items {
if p.HasItem(id) && i < len(e.Byproducts) && e.Byproducts[i] != "" {
byproducts = append(byproducts, e.Byproducts[i])
break
}
}
}
return byproducts
}
func (g *Game) canContinueProduction(p *player.Player, recipe *action.RecipeDef) bool {
if !recipe.HasAllItemsQty(p.CountItem) {
return false
}
outputQty := recipe.OutputQty
if outputQty <= 0 {
outputQty = 1
}
outDef, _ := g.ItemStore.Load(recipe.Output)
if outDef != nil && outDef.Stackable {
for i := 0; i < 28; i++ {
slot := p.InvSlot(i)
if slot != nil && slot.ItemID == recipe.Output {
return true
}
}
}
return p.FirstFreeSlot() >= 0
}
func (g *Game) handleRecipeChoice(sess *net.Session, input string) {
input = strings.TrimSpace(input)
if input == "" {
return
}
choice, err := strconv.Atoi(input)
if err != nil {
sess.State = net.StateGame
sess.PendingMenu = nil
sess.WriteLine("Never mind.")
g.reprompt(sess)
return
}
if len(sess.PendingMenu) > 0 {
menuData := sess.PendingMenu
sess.PendingMenu = nil
if choice <= 0 || choice > len(menuData)+1 {
sess.WriteLine("Invalid choice.")
return
}
sess.State = net.StateGame
if choice == len(menuData)+1 {
sess.WriteLine("Never mind.")
g.reprompt(sess)
return
}
entry := menuData[choice-1]
if cid, ok := entry["combine_item"]; ok {
g.promptHowMany(sess, "combine_"+cid)
return
}
if barID, ok := entry["bar_id"]; ok {
p := sess.Player.(*player.Player)
allRecipes, _ := g.RecipeStore.LoadAll()
g.showSmithTable(sess, p, barID, allRecipes)
return
}
if rid, ok := entry["recipe_id"]; ok {
g.promptHowMany(sess, rid)
return
}
g.reprompt(sess)
return
}
sess.State = net.StateGame
g.reprompt(sess)
}
|