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) }