Add durable recipe discovery and profession crafting
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"maps"
|
||||
"math"
|
||||
"sort"
|
||||
"time"
|
||||
@@ -15,6 +14,7 @@ import (
|
||||
)
|
||||
|
||||
type Character struct {
|
||||
CraftReadyAt float64 `json:"craftReadyAt"`
|
||||
AttackReadyAt float64 `json:"attackReadyAt"`
|
||||
JumpVelocity float64 `json:"jumpVelocity"`
|
||||
GatherReadyAt float64 `json:"gatherReadyAt"`
|
||||
@@ -42,9 +42,10 @@ type Character struct {
|
||||
StatsVersion int `json:"statsVersion"`
|
||||
}
|
||||
type Corpse struct {
|
||||
Entity p.Entity `json:"entity"`
|
||||
Inventory map[string]int64 `json:"inventory"`
|
||||
Items []p.ItemInstance `json:"items"`
|
||||
CraftedStacks []p.CraftedStack `json:"craftedStacks,omitempty"`
|
||||
Entity p.Entity `json:"entity"`
|
||||
Inventory map[string]int64 `json:"inventory"`
|
||||
Items []p.ItemInstance `json:"items"`
|
||||
}
|
||||
type Mob struct {
|
||||
Definition *content.MobDefinition `json:"definition,omitempty"`
|
||||
@@ -186,6 +187,16 @@ func (w *World) Restore(now time.Time) error {
|
||||
if w.ResourceLayouts == nil {
|
||||
w.ResourceLayouts = map[string]ResourceLayout{}
|
||||
}
|
||||
if w.Balance.Crafting == nil {
|
||||
b, e := content.Load()
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
cat := content.Catalog{Items: w.Balance.Items}
|
||||
cat.RestoreCrafting(b)
|
||||
w.Balance.Crafting = cat.Crafting
|
||||
w.Balance.Items = cat.Items
|
||||
}
|
||||
if w.Balance.Combat == nil || w.Balance.CombatRules == nil {
|
||||
b, e := content.Load()
|
||||
if e != nil {
|
||||
@@ -217,6 +228,9 @@ func (w *World) Restore(now time.Time) error {
|
||||
if err := w.restoreEquipment(c); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.restoreCrafting(c); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.restoreGathering(c); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -328,6 +342,9 @@ func (w *World) Join(account, name string) (*Character, error) {
|
||||
if err := w.restoreEquipment(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := w.restoreCrafting(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := w.restoreGathering(c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -356,6 +373,9 @@ func (w *World) Import(c *Character) error {
|
||||
if err := w.checkImportedEquipment(c); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.restoreCrafting(c); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := w.restoreGathering(c); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -501,14 +521,14 @@ func (w *World) Intent(id string, i p.Intent) error {
|
||||
case "gather":
|
||||
return w.gather(c, i.Target)
|
||||
case "craft":
|
||||
return w.craft(c, i.Target)
|
||||
return errors.New("use the crafting workbench to offer ingredients")
|
||||
case "attack":
|
||||
return w.attack(c, i.Target)
|
||||
case "heal":
|
||||
if c.View.Inventory["bandage"] < 1 || c.View.Entity.HP >= c.View.Entity.MaxHP {
|
||||
return errors.New("no bandage needed or available")
|
||||
}
|
||||
c.View.Inventory["bandage"]--
|
||||
TakeStack(c, "bandage", 1)
|
||||
w.RefreshStats(c)
|
||||
c.View.Entity.HP = math.Min(c.View.Entity.MaxHP, c.View.Entity.HP+w.Balance.BandageHealing)
|
||||
w.dirty = true
|
||||
@@ -531,37 +551,6 @@ func (w *World) Intent(id string, i p.Intent) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (w *World) craft(c *Character, name string) error {
|
||||
r, ok := w.Balance.Recipes[name]
|
||||
if !ok {
|
||||
return errors.New("unknown recipe")
|
||||
}
|
||||
for k, v := range r.Inputs {
|
||||
if c.View.Inventory[k] < v {
|
||||
return errors.New("missing crafting materials")
|
||||
}
|
||||
}
|
||||
inventory := maps.Clone(c.View.Inventory)
|
||||
items := append([]p.ItemInstance{}, c.View.Items...)
|
||||
for k, v := range r.Inputs {
|
||||
inventory[k] -= v
|
||||
}
|
||||
for k, v := range r.Outputs {
|
||||
if err := w.addInventoryItems(&inventory, &items, k, v); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
c.View.Inventory, c.View.Items = inventory, items
|
||||
w.RefreshStats(c)
|
||||
if w.AllowsXP() {
|
||||
c.View.Skills["crafting"] += w.Balance.SkillXpPerAction
|
||||
}
|
||||
c.Message = "Crafted " + name
|
||||
w.grantXP(c, w.Balance.Progression.CraftXP)
|
||||
w.evaluateTitles(c)
|
||||
w.dirty = true
|
||||
return nil
|
||||
}
|
||||
func (w *World) damageBoss(c *Character, damage float64) {
|
||||
w.BossHP = math.Max(0, w.BossHP-damage)
|
||||
if w.BossHP == 0 {
|
||||
@@ -595,7 +584,8 @@ func (w *World) kill(c *Character, reason string) {
|
||||
e.Kind = "corpse"
|
||||
e.State = "dead"
|
||||
e.HP = 0
|
||||
w.Corpses[e.ID] = &Corpse{Entity: e, Inventory: c.View.Inventory, Items: c.View.Items}
|
||||
w.Corpses[e.ID] = &Corpse{Entity: e, Inventory: c.View.Inventory, Items: c.View.Items, CraftedStacks: c.View.CraftedStacks}
|
||||
c.View.CraftedStacks = nil
|
||||
c.View.Inventory = map[string]int64{}
|
||||
c.View.Items = []p.ItemInstance{}
|
||||
c.View.Equipment = map[string]string{}
|
||||
|
||||
Reference in New Issue
Block a user