133 lines
4.2 KiB
Go
133 lines
4.2 KiB
Go
package game
|
|
|
|
import (
|
|
"aeternalis/server/content"
|
|
p "aeternalis/server/internal/protocol"
|
|
"errors"
|
|
"math"
|
|
)
|
|
|
|
var HousePermissions = []string{"moveFurniture", "openFurniture", "placeFurniture", "depositItems", "pickupItems", "dropItems"}
|
|
|
|
func (w *World) NowSeconds() float64 { return float64(w.now.UnixMilli()) / 1000 }
|
|
func HouseID(c *Character) string {
|
|
if c == nil || c.View.HouseID == nil {
|
|
return ""
|
|
}
|
|
return *c.View.HouseID
|
|
}
|
|
func LeaveHouse(c *Character) { c.View.HouseID = nil; c.MoveX = 0; c.MoveZ = 0 }
|
|
func (w *World) restoreHousing() error {
|
|
if w.Balance.Housing == nil {
|
|
b, e := content.Load()
|
|
if e != nil {
|
|
return e
|
|
}
|
|
w.Balance.Housing = b.Housing
|
|
cat := content.Catalog{Items: w.Balance.Items}
|
|
cat.RestoreFurniture(b)
|
|
w.Balance.Items = cat.Items
|
|
}
|
|
for _, c := range w.Characters {
|
|
LeaveHouse(c)
|
|
c.View.DestroyKey = nil
|
|
}
|
|
return nil
|
|
}
|
|
func (w *World) updateHousingActions() {
|
|
for _, c := range w.Characters {
|
|
if c.View.DestroyKey != nil {
|
|
key := ownedItem(c, c.View.DestroyKey.KeyID)
|
|
if !c.Online || c.View.RemainingSeconds <= 0 || c.Sheltered || c.View.Entity.State == "dead" || key == nil || key.HouseID == nil {
|
|
c.View.DestroyKey = nil
|
|
w.dirty = true
|
|
} else if w.NowSeconds() >= c.View.DestroyKey.FinishesAt {
|
|
w.dirty = true
|
|
}
|
|
}
|
|
if HouseID(c) != "" && (!c.Online || c.View.Entity.State == "dead" || c.View.RemainingSeconds <= 0) {
|
|
LeaveHouse(c)
|
|
w.dirty = true
|
|
}
|
|
}
|
|
}
|
|
func EmptyHouseInventory() p.HouseInventory {
|
|
return p.HouseInventory{Inventory: map[string]int64{}, Items: []p.ItemInstance{}, CraftedStacks: []p.CraftedStack{}, StackNames: map[string]string{}}
|
|
}
|
|
func ValidateHouseInventory(v p.HouseInventory, seen map[string]bool) error {
|
|
if len(v.Inventory) > 512 || len(v.Items) > 512 || len(v.StackNames) > 1024 {
|
|
return errors.New("house inventory limit")
|
|
}
|
|
for _, n := range v.Inventory {
|
|
if n < 0 || n > 1000000000000 {
|
|
return errors.New("invalid stored quantity")
|
|
}
|
|
}
|
|
if e := validateOwnedItems(v.Items, seen); e != nil {
|
|
return e
|
|
}
|
|
return ValidateCraftedStacks(v.Inventory, v.CraftedStacks, seen)
|
|
}
|
|
func HouseInventoryOf(c *Character) p.HouseInventory {
|
|
return p.HouseInventory{Inventory: c.View.Inventory, Items: c.View.Items, CraftedStacks: c.View.CraftedStacks, StackNames: c.View.StackNames}
|
|
}
|
|
func SetHouseInventory(c *Character, v p.HouseInventory) {
|
|
c.View.Inventory = v.Inventory
|
|
c.View.Items = v.Items
|
|
c.View.CraftedStacks = v.CraftedStacks
|
|
c.View.StackNames = v.StackNames
|
|
}
|
|
|
|
// Move one unique instance or a bounded stack quantity; provenance travels with it.
|
|
// Call on staged/transaction-owned inventories and validate before publishing.
|
|
func MoveHouseItem(from, to *p.HouseInventory, id string, quantity int64) error {
|
|
if quantity < 1 || quantity > 1000 || len(to.Items) >= 512 || len(to.Inventory) >= 512 {
|
|
return errors.New("invalid quantity or destination full")
|
|
}
|
|
for i, item := range from.Items {
|
|
if item.ID == id {
|
|
if quantity != 1 {
|
|
return errors.New("unique item quantity must be one")
|
|
}
|
|
to.Items = append(to.Items, item)
|
|
from.Items = append(from.Items[:i:i], from.Items[i+1:]...)
|
|
return nil
|
|
}
|
|
}
|
|
if from.Inventory[id] < quantity || to.Inventory[id] > 1000000000000-quantity {
|
|
return errors.New("item quantity unavailable")
|
|
}
|
|
temp := &Character{}
|
|
SetHouseInventory(temp, *from)
|
|
batches := TakeStack(temp, id, quantity)
|
|
*from = HouseInventoryOf(temp)
|
|
if to.Inventory == nil {
|
|
to.Inventory = map[string]int64{}
|
|
}
|
|
if to.StackNames == nil {
|
|
to.StackNames = map[string]string{}
|
|
}
|
|
to.Inventory[id] += quantity
|
|
to.CraftedStacks = append(to.CraftedStacks, batches...)
|
|
if name := from.StackNames[id]; name != "" {
|
|
to.StackNames[id] = name
|
|
}
|
|
return nil
|
|
}
|
|
func FurnitureFits(h p.House, id string, item p.ItemInstance, x, z float64) bool {
|
|
f := item.Furniture
|
|
if f == nil || math.IsNaN(x) || math.IsNaN(z) || math.IsInf(x, 0) || math.IsInf(z, 0) || math.Abs(x)+f.Width/2 > h.Size/2 || math.Abs(z)+f.Depth/2 > h.Size/2 {
|
|
return false
|
|
}
|
|
for other, v := range h.Furniture {
|
|
if other == id {
|
|
continue
|
|
}
|
|
g := v.Item.Furniture
|
|
if g != nil && math.Abs(x-v.X) < (g.Width+f.Width)/2 && math.Abs(z-v.Z) < (g.Depth+f.Depth)/2 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|