Add housing storage, guest permissions, and timed key destruction

This commit is contained in:
dingo
2026-09-10 05:47:22 -04:00
parent 454f3300ca
commit 6903d5ece8
41 changed files with 2386 additions and 183 deletions
+3
View File
@@ -75,6 +75,9 @@ func (w *World) AdminItems(c *Character, op, value string) error {
}
return w.grantItems(c, id, n)
}
if item := ownedItem(c, id); item != nil && item.HouseID != nil {
return errors.New("house keys require the holder's three-second destruction action")
}
if op != "remove-item" {
return errors.New("unknown item operation")
}
+2 -2
View File
@@ -66,7 +66,7 @@ func (w *World) sourceActive(source string, player bool) bool {
}
if player {
c := w.Characters[source]
return c != nil && c.View.Entity.State == "alive" && !c.Sheltered
return c != nil && HouseID(c) == "" && c.View.Entity.State == "alive" && !c.Sheltered
}
m := w.Mobs[source]
return m != nil && m.Entity.HP > 0
@@ -75,7 +75,7 @@ func (w *World) combatTargets(player bool, effect string) []p.Entity {
out := []p.Entity{}
if (player && effect == "heal") || (!player && effect == "damage") {
for _, c := range w.orderedCharacters() {
if c.Online && c.View.Entity.State == "alive" && !c.Sheltered {
if HouseID(c) == "" && c.Online && c.View.Entity.State == "alive" && !c.Sheltered {
out = append(out, c.View.Entity)
}
}
+1 -1
View File
@@ -145,7 +145,7 @@ func (w *World) CraftProfessions(c *Character) []p.CraftProgress {
// requirement requests do not consume materials or advance the random stream.
func (w *World) AttemptCraft(c *Character, req p.CraftRequest) (p.CraftResult, error) {
result := p.CraftResult{ID: req.ID, Consumed: map[string]int64{}, Created: []string{}}
if c == nil || !c.Online || c.Sheltered || c.View.Entity.State != "alive" || c.View.RemainingSeconds <= 0 || (!w.IsHub() && w.Phase != "gathering" && w.Phase != "cataclysm") {
if c == nil || HouseID(c) != "" || !c.Online || c.Sheltered || c.View.Entity.State != "alive" || c.View.RemainingSeconds <= 0 || (!w.IsHub() && w.Phase != "gathering" && w.Phase != "cataclysm") {
return result, errors.New("character cannot craft")
}
now := float64(w.now.UnixMilli()) / 1000
+22
View File
@@ -19,6 +19,9 @@ func validEquipmentSlot(slot string) bool {
return false
}
func itemFitsSlot(item p.ItemInstance, slot string) bool {
if item.HouseID != nil || item.Furniture != nil {
return false
}
if !validEquipmentSlot(slot) {
return false
}
@@ -58,6 +61,10 @@ func (w *World) addInventoryItems(inventory *map[string]int64, items *[]p.ItemIn
for n := int64(0); n < quantity; n++ {
item := p.ItemInstance{ID: ID(), DefinitionID: definition.ID, Name: definition.Name, Description: definition.Description, Slot: definition.Slot, Weight: definition.Weight,
WeaponType: stringPtr(definition.WeaponType), Requirements: p.ItemRequirements{Level: definition.Requirements.Level, Stats: map[string]float64{}, Titles: append([]string{}, definition.Requirements.Titles...)}, Modifiers: []p.StatModifier{}}
if definition.Furniture != nil {
f := definition.Furniture
item.Furniture = &p.FurnitureProfile{Width: f.Width, Depth: f.Depth, Container: f.Container}
}
for stat, value := range definition.Requirements.Stats {
item.Requirements.Stats[stat] = value
}
@@ -133,6 +140,15 @@ func validateOwnedItems(items []p.ItemInstance, seen map[string]bool) error {
return errors.New("invalid or duplicate persisted equipment")
}
seen[item.ID] = true
if item.Furniture != nil {
f := item.Furniture
if math.IsNaN(f.Width) || math.IsNaN(f.Depth) || f.Width < .5 || f.Width > 4 || f.Depth < .5 || f.Depth > 4 {
return errors.New("invalid saved furniture")
}
}
if item.HouseID != nil && (len(*item.HouseID) != 32 || item.DefinitionID != "house_key") {
return errors.New("invalid saved house key")
}
for _, value := range item.Requirements.Stats {
if math.IsNaN(value) || math.IsInf(value, 0) || value < 0 {
return errors.New("invalid equipment requirement")
@@ -198,6 +214,9 @@ func (w *World) restoreCorpseEquipment() error {
return nil
}
func equipmentRequirementError(c *Character, item p.ItemInstance, stats map[string]p.StatValue) error {
if item.HouseID != nil || item.Furniture != nil {
return errors.New("house items cannot be equipped")
}
if c.View.Level < item.Requirements.Level {
return fmt.Errorf("requires level %d", item.Requirements.Level)
}
@@ -291,6 +310,9 @@ func (w *World) dropItem(c *Character, target string) error {
index := -1
for i, item := range c.View.Items {
if item.ID == target {
if c.View.DestroyKey != nil && c.View.DestroyKey.KeyID == target {
c.View.DestroyKey = nil
}
index = i
name = item.Name
break
+132
View File
@@ -0,0 +1,132 @@
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
}
+72
View File
@@ -0,0 +1,72 @@
package game
import (
"aeternalis/server/content"
p "aeternalis/server/internal/protocol"
"encoding/json"
"math"
"testing"
"time"
)
func TestHouseInventoryPreservesCraftedProvenance(t *testing.T) {
b, _ := content.Load()
w := New("craft-origin", 1, time.Now(), b)
c, _ := w.Join("crafter", "Crafter")
c.View.Inventory["bandage"] = 5
provenance := p.CraftProvenance{At: 1, CharacterID: c.View.Entity.ID, CrafterName: "Crafter", Level: 1, Profession: "alchemist", RecipeID: "recipe_bandage", RecipeRevision: 1, WorldID: w.ID}
c.View.CraftedStacks = []p.CraftedStack{{ID: ID(), Item: "bandage", Quantity: 5, Provenance: provenance}}
from, to := HouseInventoryOf(c), EmptyHouseInventory()
if e := MoveHouseItem(&from, &to, "bandage", 2); e != nil {
t.Fatal(e)
}
if from.Inventory["bandage"] != 3 || to.Inventory["bandage"] != 2 || to.CraftedStacks[0].Provenance != provenance || from.CraftedStacks[0].ID == to.CraftedStacks[0].ID {
t.Fatal("split damaged provenance or duplicated batch ID")
}
seen := map[string]bool{}
if e := ValidateHouseInventory(from, seen); e != nil {
t.Fatal(e)
}
if e := ValidateHouseInventory(to, seen); e != nil {
t.Fatal(e)
}
if e := w.AdminItems(c, "grant-item", "storage_chest:1"); e != nil {
t.Fatal(e)
}
item := c.View.Items[len(c.View.Items)-1]
h := p.House{Size: 12, Furniture: map[string]p.HouseFurniture{item.ID: {Item: item, X: 0, Z: 0}}}
if FurnitureFits(h, "", item, 0, 0) || FurnitureFits(h, "", item, 6, 0) || FurnitureFits(h, "", item, math.NaN(), 0) || !FurnitureFits(h, item.ID, item, 3, 0) {
t.Fatal("furniture collision validation")
}
before, _ := json.Marshal(to)
if e := MoveHouseItem(&from, &to, "bandage", 999); e == nil {
t.Fatal("overdraw accepted")
}
after, _ := json.Marshal(to)
if string(before) != string(after) {
t.Fatal("overdraw changed destination")
}
}
func TestHousingDailyLimitAndNoMovement(t *testing.T) {
b, _ := content.Load()
now := time.Date(2026, 9, 10, 12, 0, 0, 0, time.UTC)
w := New("daily-house", 1, now, b)
c, _ := w.Join("owner", "Owner")
_ = w.Connect(c.View.Entity.ID)
w.Kind = "hub"
w.Phase = "hub"
id := ID()
c.View.HouseID = &id
before := c.View.Entity.Position
c.MoveX = 1
c.LastMove = now
c.DailySeconds = b.DailyPlaySeconds - 1
w.Advance(now.Add(500*time.Millisecond), .5)
if c.View.Entity.Position != before || c.DailySeconds != b.DailyPlaySeconds-.5 {
t.Fatal("housing moved avatar or stopped daily budget")
}
w.Advance(now.Add(2*time.Second), 1.5)
if c.Online || c.View.HouseID != nil || c.View.RemainingSeconds != 0 {
t.Fatal("daily cap did not exit housing")
}
}
+3
View File
@@ -13,6 +13,9 @@ func (w *World) PortalPosition() p.Vec3 { return w.terrain.Position(0, 0) }
// Escape is an explicit one-item action. It grants no victory/survival credit.
func (w *World) Departure(c *Character, route string) error {
if HouseID(c) != "" {
return errors.New("leave the house before traveling")
}
if c == nil || !c.Online || c.Sheltered || c.View.RemainingSeconds <= 0 || (c.View.Entity.State != "alive" && c.View.Entity.State != "eligible") {
return errors.New("character cannot travel")
}
+17 -2
View File
@@ -190,6 +190,9 @@ func (w *World) Restore(now time.Time) error {
}
w.dirty = true
}
if e := w.restoreHousing(); e != nil {
return e
}
if e := w.restoreBuilding(); e != nil {
return e
}
@@ -467,6 +470,10 @@ func (w *World) Connect(id string) error {
return nil
}
func (w *World) Disconnect(id string) {
if c := w.Characters[id]; c != nil {
c.View.DestroyKey = nil
LeaveHouse(c)
}
if c := w.Characters[id]; c != nil {
c.Online = false
c.MoveX = 0
@@ -517,6 +524,9 @@ func (w *World) Intent(id string, i p.Intent) error {
return errors.New("invalid intent")
}
c.View.ACK = i.Seq
if HouseID(c) != "" {
return errors.New("leave the house to use world actions")
}
if c.Sheltered && ((i.Op == "move" && (i.X != 0 || i.Z != 0)) || (i.Op != "move" && i.Op != "heal" && i.Op != "allocate-stat" && i.Op != "choose-vital" && i.Op != "equip" && i.Op != "unequip" && i.Op != "drop")) {
c.Sheltered = false
w.dirty = true
@@ -622,6 +632,8 @@ func (w *World) kill(c *Character, reason string) {
e.Position = w.terrain.Position(e.Position.X, e.Position.Z)
c.View.Entity.Position = e.Position
c.DeathCount++
c.View.DestroyKey = nil
LeaveHouse(c)
c.PendingCast = nil
c.RevivalAllowed = false
e.ID = "corpse:" + e.ID + ":" + ID()
@@ -776,7 +788,9 @@ func (w *World) Advance(now time.Time, dt float64) {
c.MoveX = 0
c.MoveZ = 0
}
w.moveCharacter(c, dt)
if HouseID(c) == "" {
w.moveCharacter(c, dt)
}
for _, ch := range w.neighbors(c.View.Entity.Position) {
active[ch.Key] = true
if !w.IsHub() && w.Phase != "won" {
@@ -795,6 +809,7 @@ func (w *World) Advance(now time.Time, dt float64) {
if !w.IsHub() && w.Phase != "won" {
w.updateMobs(active, dt)
}
w.updateHousingActions()
w.updatePowers()
if w.Phase != "won" {
w.updateCombat(dt)
@@ -892,7 +907,7 @@ func (w *World) Snapshot(id string, known map[string]bool) p.Snapshot {
}
}
for _, other := range w.Characters {
if other.View.Entity.ID != id && other.Online && other.View.Entity.State != "dead" && visible(other.View.Entity.Position) {
if HouseID(other) == "" && other.View.Entity.ID != id && other.Online && other.View.Entity.State != "dead" && visible(other.View.Entity.Position) {
s.Entities = append(s.Entities, other.View.Entity)
}
}