243 lines
8.0 KiB
Go
243 lines
8.0 KiB
Go
package game
|
|
|
|
import (
|
|
"aeternalis/server/content"
|
|
p "aeternalis/server/internal/protocol"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"slices"
|
|
"strings"
|
|
)
|
|
|
|
func ValidItemAction(r p.ItemActionRequest) bool {
|
|
return len(r.ID) >= 32 && len(r.ID) <= 36 && strings.Trim(r.ID, "0123456789abcdef-") == "" && len(r.CharacterID) == 32 && strings.Trim(r.CharacterID, "0123456789abcdef") == "" && len(r.Item) > 0 && len(r.Item) <= 100 && len(r.Target) <= 100 && len(r.Slot) <= 64 && r.Quantity >= 1 && r.Quantity <= 1000 && slices.Contains([]string{"equip", "destroy", "breakdown", "consume", "use"}, r.Op)
|
|
}
|
|
|
|
// Invoked only on a staged world; the store commits the result and replay receipt
|
|
// together before returning it to the browser.
|
|
func (w *World) ItemAction(c *Character, r p.ItemActionRequest) (p.ItemActionResult, error) {
|
|
out := p.ItemActionResult{ID: r.ID, Returned: map[string]int64{}}
|
|
if !ValidItemAction(r) || c == nil || c.View.Entity.ID != r.CharacterID || !c.Online || c.Sheltered || HouseID(c) != "" || c.View.Entity.State != "alive" || c.View.RemainingSeconds <= 0 || (!w.IsHub() && w.Phase != "gathering" && w.Phase != "cataclysm") {
|
|
return out, errors.New("character cannot use items")
|
|
}
|
|
now := float64(w.now.UnixMilli()) / 1000
|
|
if now < c.ItemReadyAt {
|
|
return out, errors.New("item action cooldown")
|
|
}
|
|
item := ownedItem(c, r.Item)
|
|
if item == nil && c.View.Inventory[r.Item] < r.Quantity {
|
|
return out, errors.New("item not owned in that quantity")
|
|
}
|
|
if item != nil && r.Quantity != 1 {
|
|
return out, errors.New("equipment actions require one instance")
|
|
}
|
|
if r.Op != "use" && r.Target != "" || r.Op != "equip" && r.Slot != "" {
|
|
return out, errors.New("unexpected item target or slot")
|
|
}
|
|
var err error
|
|
switch r.Op {
|
|
case "equip":
|
|
if r.Quantity != 1 {
|
|
return out, errors.New("equip one item")
|
|
}
|
|
err = w.equip(c, r.Item, r.Slot)
|
|
out.Message = "Equipment updated"
|
|
case "destroy":
|
|
if item != nil {
|
|
if item.HouseID != nil {
|
|
return out, errors.New("house keys require the three-second key destruction action")
|
|
}
|
|
w.removeOwnedItem(c, item.ID)
|
|
} else {
|
|
TakeStack(c, r.Item, r.Quantity)
|
|
}
|
|
out.Message = "Item destroyed"
|
|
case "breakdown":
|
|
if item == nil {
|
|
return out, errors.New("only equipment can be broken down")
|
|
}
|
|
err = w.breakdown(c, *item, &out)
|
|
case "consume", "use":
|
|
if r.Quantity != 1 {
|
|
return out, errors.New("use one item at a time")
|
|
}
|
|
err = w.useItem(c, r.Item, r.Target, r.Op == "consume")
|
|
out.Message = "Item used"
|
|
}
|
|
if err != nil {
|
|
return out, err
|
|
}
|
|
c.ItemReadyAt = now + w.Balance.Inventory.ActionCooldown
|
|
w.RefreshStats(c)
|
|
c.Message = out.Message
|
|
w.dirty = true
|
|
return out, nil
|
|
}
|
|
|
|
func (w *World) removeOwnedItem(c *Character, id string) {
|
|
c.View.Items = slices.DeleteFunc(c.View.Items, func(i p.ItemInstance) bool { return i.ID == id })
|
|
for slot, item := range c.View.Equipment {
|
|
if item == id {
|
|
delete(c.View.Equipment, slot)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (w *World) breakdownRecipe(item p.ItemInstance) *content.CraftRecipe {
|
|
if item.HouseID != nil || item.Furniture != nil || w.Balance.Crafting == nil {
|
|
return nil
|
|
}
|
|
if item.Provenance != nil {
|
|
for _, list := range [][]content.CraftRecipe{w.Balance.Crafting.Recipes, w.Balance.Crafting.RecipeHistory} {
|
|
for _, r := range list {
|
|
if r.ID == item.Provenance.RecipeID && r.Revision == item.Provenance.RecipeRevision && r.Outputs[item.DefinitionID] > 0 {
|
|
copy := r
|
|
return ©
|
|
}
|
|
}
|
|
}
|
|
return nil // Never substitute a later recipe revision for the original.
|
|
}
|
|
var found *content.CraftRecipe
|
|
for _, r := range w.Balance.Crafting.Recipes {
|
|
if r.Outputs[item.DefinitionID] > 0 {
|
|
if found != nil {
|
|
return nil
|
|
}
|
|
copy := r
|
|
found = ©
|
|
}
|
|
}
|
|
return found
|
|
}
|
|
|
|
func (w *World) breakdown(c *Character, item p.ItemInstance, out *p.ItemActionResult) error {
|
|
r := w.breakdownRecipe(item)
|
|
if r == nil {
|
|
return errors.New("no unambiguous original recipe is available for this item")
|
|
}
|
|
profession := r.Profession
|
|
if item.Slot == "weapon" {
|
|
profession = "weapon_smithing"
|
|
} else if slices.Contains([]string{"helmet", "mask", "chest", "shoulders", "pants", "boots", "gloves", "cape"}, item.Slot) {
|
|
profession = "armor_making"
|
|
}
|
|
w.refreshCrafting(c)
|
|
level := c.View.Crafting[profession].Level
|
|
if level < 1 {
|
|
return errors.New("breakdown profession unavailable")
|
|
}
|
|
rules := w.Balance.Inventory
|
|
gap := float64(level - max(r.Level, item.Requirements.Level))
|
|
rate := rules.BreakdownBase
|
|
if gap < 0 {
|
|
rate *= math.Exp(gap / rules.BreakdownLevelScale)
|
|
} else {
|
|
rate += (rules.BreakdownMax - rate) * (1 - math.Exp(-gap/rules.BreakdownLevelScale))
|
|
}
|
|
units := int64(0)
|
|
for _, n := range r.Outputs {
|
|
units += n
|
|
}
|
|
if units < 1 {
|
|
return errors.New("invalid original recipe")
|
|
}
|
|
for id, n := range r.Inputs {
|
|
// Divide by the complete output batch: breaking down each output cannot
|
|
// multiply the batch's inputs. Return material stacks only.
|
|
if slices.ContainsFunc(w.Balance.Items, func(i content.ItemDefinition) bool { return i.ID == id }) {
|
|
return errors.New("equipment ingredients cannot be recovered by breakdown")
|
|
}
|
|
amount := int64(math.Floor(float64(n) * rate / 100 / float64(units)))
|
|
if amount > 0 {
|
|
if c.View.Inventory[id] > math.MaxInt64-amount {
|
|
return errors.New("inventory overflow")
|
|
}
|
|
out.Returned[id] = amount
|
|
}
|
|
}
|
|
if len(c.View.DiscoveredRecipes) >= 512 {
|
|
if _, ok := c.View.DiscoveredRecipes[r.ID]; !ok {
|
|
return errors.New("recipe discovery capacity reached")
|
|
}
|
|
}
|
|
w.removeOwnedItem(c, item.ID)
|
|
for id, n := range out.Returned {
|
|
c.View.Inventory[id] += n
|
|
}
|
|
if w.roll(rules.RecipeChance) {
|
|
old, known := c.View.DiscoveredRecipes[r.ID]
|
|
if !known || old.Recipe.Revision < r.Revision {
|
|
raw, _ := json.Marshal(r)
|
|
var recipe p.CraftRecipe
|
|
_ = json.Unmarshal(raw, &recipe)
|
|
d := p.KnownRecipe{Recipe: recipe, DiscoveredAt: float64(w.now.UnixMilli()) / 1000}
|
|
c.View.DiscoveredRecipes[r.ID] = d
|
|
out.Recipe = &d
|
|
}
|
|
}
|
|
out.Message = fmt.Sprintf("Broke down %s (%.1f%% recovery, rounded down)", item.Name, rate)
|
|
if out.Recipe != nil {
|
|
out.Message += "; recipe discovered"
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (w *World) useItem(c *Character, id, target string, consume bool) error {
|
|
if target == "" || consume {
|
|
target = c.View.Entity.ID
|
|
}
|
|
if c.View.Inventory[id] < 1 {
|
|
return errors.New("this item has no supported use effect")
|
|
}
|
|
healing := float64(0)
|
|
if id == "bandage" {
|
|
healing = w.Balance.BandageHealing
|
|
}
|
|
for _, definition := range w.Balance.Gathering.Consumables {
|
|
if definition.ID != id || definition.UseEffect == nil {
|
|
continue
|
|
}
|
|
effect := definition.UseEffect
|
|
if effect.Kind == "heal" {
|
|
healing = effect.Amount
|
|
}
|
|
if effect.Kind == "weapon-coating" {
|
|
weapon := ownedItem(c, target)
|
|
if consume || weapon == nil || weapon.Slot != "weapon" || weapon.Furniture != nil || weapon.HouseID != nil || (weapon.Attack != nil && weapon.Attack.Effect != "damage") {
|
|
return errors.New("select one of your damage weapons to apply this coating")
|
|
}
|
|
TakeStack(c, id, 1)
|
|
weapon.Coating = &p.WeaponCoating{Source: id, Name: definition.Name, ExpiresAt: float64(w.now.UnixMilli())/1000 + definition.Seconds, Damage: effect.Amount}
|
|
return nil
|
|
}
|
|
}
|
|
if healing > 0 {
|
|
var entity *p.Entity
|
|
if other := w.Characters[target]; other != nil && other.Online && !other.Sheltered && HouseID(other) == "" && other.View.Entity.State == "alive" {
|
|
entity = &other.View.Entity
|
|
}
|
|
if mob := w.Mobs[target]; mob != nil && mob.Entity.HP > 0 && !w.IsHub() {
|
|
entity = &mob.Entity
|
|
}
|
|
if entity == nil || entity.HP >= entity.MaxHP {
|
|
return errors.New("target cannot be healed")
|
|
}
|
|
if length3(body(c.View.Entity.Position), body(entity.Position)) > w.Balance.GatherRange {
|
|
return errors.New("target out of reach")
|
|
}
|
|
if _, blocked := w.combatLine(body(c.View.Entity.Position), body(entity.Position), ""); blocked {
|
|
return errors.New("target obstructed")
|
|
}
|
|
TakeStack(c, id, 1)
|
|
entity.HP = math.Min(entity.MaxHP, entity.HP+healing)
|
|
return nil
|
|
}
|
|
if target != c.View.Entity.ID {
|
|
return errors.New("this item can only be used on yourself")
|
|
}
|
|
return w.consumeGathering(c, id)
|
|
}
|