aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_fletch.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/game/cmd_fletch.go')
-rw-r--r--internal/game/cmd_fletch.go181
1 files changed, 83 insertions, 98 deletions
diff --git a/internal/game/cmd_fletch.go b/internal/game/cmd_fletch.go
index 5a4bc19..088ce63 100644
--- a/internal/game/cmd_fletch.go
+++ b/internal/game/cmd_fletch.go
@@ -3,9 +3,9 @@ package game
import (
"strings"
- "thehouseoficarus/internal/action"
"thehouseoficarus/internal/combat"
"thehouseoficarus/internal/net"
+ "thehouseoficarus/internal/object"
"thehouseoficarus/internal/player"
)
@@ -23,34 +23,38 @@ var fletchGemItems = map[string]bool{
"uncut_ruby": true, "uncut_diamond": true,
}
-func fletchNeedsKnife(r *action.RecipeDef) bool {
- if r.Tool == "knife" {
- return true
- }
- for _, e := range r.Consume {
- for _, id := range e.Items {
- if fletchLogItems[id] {
- if strings.Contains(r.Output, "shaft") || strings.Contains(r.Output, "bow") {
- return 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
+ return false
+ }) != nil
}
-func fletchNeedsChisel(r *action.RecipeDef) bool {
- if r.Tool == "chisel" {
- return true
- }
- for _, e := range r.Consume {
- for _, id := range e.Items {
- if fletchGemItems[id] {
- return true
+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
+ return false
+ }) != nil
}
func (g *Game) doFletch(sess *net.Session, input string) {
@@ -66,32 +70,19 @@ func (g *Game) doFletch(sess *net.Session, input string) {
p.ActionState = nil
}
- allRecipes, err := g.RecipeStore.LoadAll()
- if err != nil {
- sess.WriteLine("Error loading recipes.")
- return
- }
-
- var fletchRecipes []action.RecipeDef
- for _, r := range allRecipes {
- if r.Type != "fletching" {
- continue
- }
- fletchRecipes = append(fletchRecipes, r)
- }
+ items := g.CraftIndex.ByType("fletching")
if input != "" {
- g.doFletchWithInput(sess, p, fletchRecipes, input)
+ g.doFletchWithInput(sess, p, items, input)
return
}
- lastRecipeID, _ := p.Flags["last_fletch"].(string)
- if lastRecipeID != "" {
- for i := range fletchRecipes {
- if fletchRecipes[i].ID == lastRecipeID {
- r := &fletchRecipes[i]
- if g.canDoFletchRecipe(p, r) {
- g.startFletchAction(sess, p, r, 0)
+ 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
@@ -99,33 +90,28 @@ func (g *Game) doFletch(sess *net.Session, input string) {
}
}
- available := g.availableFletchRecipes(p, fletchRecipes)
+ 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)
+ 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, fletchRecipes []action.RecipeDef, input string) {
+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 []action.RecipeDef
- for _, r := range fletchRecipes {
- outDef, _ := g.ItemStore.Load(r.Output)
- name := r.Output
- if outDef != nil {
- name = outDef.Name
- }
- if action.WordPrefixMatch(productName, name) {
- matched = append(matched, r)
+ var matched []*object.ItemDef
+ for _, item := range items {
+ if object.WordPrefixMatch(productName, item.Name) {
+ matched = append(matched, item)
}
}
@@ -134,10 +120,10 @@ func (g *Game) doFletchWithInput(sess *net.Session, p *player.Player, fletchReci
return
}
- var available []action.RecipeDef
- for _, r := range matched {
- if g.canDoFletchRecipe(p, &r) {
- available = append(available, r)
+ var available []*object.ItemDef
+ for _, item := range matched {
+ if g.canDoFletchItem(p, item) {
+ available = append(available, item)
}
}
@@ -147,48 +133,49 @@ func (g *Game) doFletchWithInput(sess *net.Session, p *player.Player, fletchReci
}
if len(available) == 1 {
- count := qty
- g.startFletchAction(sess, p, &available[0], count)
+ 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, r *action.RecipeDef) bool {
- if r.Tool != "" {
- return g.hasToolType(p, r.Tool)
+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(r) && !g.hasToolType(p, "knife") {
+ if fletchNeedsKnife(item) && !g.hasToolType(p, "knife") {
return false
}
- if fletchNeedsChisel(r) && !g.hasToolType(p, "chisel") {
+ if fletchNeedsChisel(item) && !g.hasToolType(p, "chisel") {
return false
}
return true
}
-func (g *Game) canDoFletchRecipe(p *player.Player, r *action.RecipeDef) bool {
- return p.Level(player.Fletching) >= r.Level &&
- r.HasAllItemsQty(p.CountItem) &&
- g.hasFletchTools(p, r)
+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) availableFletchRecipes(p *player.Player, allFletch []action.RecipeDef) []action.RecipeDef {
- var result []action.RecipeDef
- for _, r := range allFletch {
- if r.HasAllItemsQty(p.CountItem) && g.hasFletchTools(p, &r) {
- result = append(result, r)
+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) findFletchRecipes(p *player.Player, itemAID, itemBID string) []action.RecipeDef {
- allRecipes, err := g.RecipeStore.LoadAll()
- if err != nil {
- return nil
- }
+func (g *Game) findFletchItems(p *player.Player, itemAID, itemBID string) []*object.ItemDef {
+ items := g.CraftIndex.ByType("fletching")
toolTypeA := ""
toolTypeB := ""
@@ -201,26 +188,24 @@ func (g *Game) findFletchRecipes(p *player.Player, itemAID, itemBID string) []ac
isToolA := toolTypeA == "knife" || toolTypeA == "chisel"
isToolB := toolTypeB == "knife" || toolTypeB == "chisel"
- var matched []action.RecipeDef
- for _, r := range allRecipes {
- if r.Type != "fletching" {
- continue
- }
+ var matched []*object.ItemDef
+ for _, item := range items {
+ c := item.FirstCraft()
- if r.Tool != "" {
- if r.Tool == toolTypeA && r.MatchesEntry(itemBID) {
- matched = append(matched, r)
+ if c.Tool != "" {
+ if c.Tool == toolTypeA && craftMatchesEntry(c, itemBID) {
+ matched = append(matched, item)
continue
}
- if r.Tool == toolTypeB && r.MatchesEntry(itemAID) {
- matched = append(matched, r)
+ if c.Tool == toolTypeB && craftMatchesEntry(c, itemAID) {
+ matched = append(matched, item)
continue
}
}
aMatch := false
bMatch := false
- for _, e := range r.Consume {
+ for _, e := range c.Consume {
for _, id := range e.Items {
if id == itemAID {
aMatch = true
@@ -232,21 +217,21 @@ func (g *Game) findFletchRecipes(p *player.Player, itemAID, itemBID string) []ac
}
if aMatch && bMatch {
- matched = append(matched, r)
+ matched = append(matched, item)
continue
}
if isToolA && bMatch {
- if (fletchNeedsKnife(&r) && toolTypeA == "knife") ||
- (fletchNeedsChisel(&r) && toolTypeA == "chisel") {
- matched = append(matched, r)
+ if (fletchNeedsKnife(item) && toolTypeA == "knife") ||
+ (fletchNeedsChisel(item) && toolTypeA == "chisel") {
+ matched = append(matched, item)
continue
}
}
if isToolB && aMatch {
- if (fletchNeedsKnife(&r) && toolTypeB == "knife") ||
- (fletchNeedsChisel(&r) && toolTypeB == "chisel") {
- matched = append(matched, r)
+ if (fletchNeedsKnife(item) && toolTypeB == "knife") ||
+ (fletchNeedsChisel(item) && toolTypeB == "chisel") {
+ matched = append(matched, item)
continue
}
}