Add walkable housing interiors and cross-hub visitors

This commit is contained in:
dingo
2026-09-10 10:48:26 -04:00
parent b2dd2cb3ba
commit 1bd5d487e2
28 changed files with 721 additions and 52 deletions
+152
View File
@@ -0,0 +1,152 @@
package game
import (
p "aeternalis/server/internal/protocol"
"errors"
"math"
"slices"
)
// House geometry contains no storage contents, permissions, or account identities.
func (w *World) HouseScene(h p.House) *p.HouseScene {
out := &p.HouseScene{ID: h.ID, Name: h.Name, Size: h.Size, Furniture: []p.RoomFurniture{}, People: []p.Entity{}}
for id, f := range h.Furniture {
if f.Item.Furniture == nil {
continue
}
profile := f.Item.Furniture
height := w.Balance.Housing.TableHeight
if profile.Container {
height = w.Balance.Housing.StorageHeight
}
out.Furniture = append(out.Furniture, p.RoomFurniture{ID: id, Name: f.Item.Name, X: f.X, Z: f.Z, Width: profile.Width, Depth: profile.Depth, Height: height, Container: profile.Container})
}
slices.SortFunc(out.Furniture, func(a, b p.RoomFurniture) int {
if a.ID < b.ID {
return -1
}
if a.ID > b.ID {
return 1
}
return 0
})
return out
}
func roomGround(room *p.HouseScene, pos p.Vec3) float64 {
ground := 0.
for _, f := range room.Furniture {
if math.Abs(pos.X-f.X) < f.Width/2 && math.Abs(pos.Z-f.Z) < f.Depth/2 {
ground = math.Max(ground, f.Height)
}
}
return ground
}
func (w *World) HousePositionValid(room *p.HouseScene, pos p.Vec3) bool {
radius := w.Balance.Housing.PlayerRadius
if math.IsNaN(pos.X) || math.IsNaN(pos.Y) || math.IsNaN(pos.Z) || math.IsInf(pos.X, 0) || math.IsInf(pos.Y, 0) || math.IsInf(pos.Z, 0) || math.Abs(pos.X)+radius > room.Size/2 || math.Abs(pos.Z)+radius > room.Size/2 || pos.Y < 0 || pos.Y > 3 {
return false
}
for _, f := range room.Furniture {
if math.Abs(pos.X-f.X) < f.Width/2+radius && math.Abs(pos.Z-f.Z) < f.Depth/2+radius && pos.Y < f.Height-.001 {
return false
}
}
return true
}
func (w *World) HouseSpawn(room *p.HouseScene) (p.Vec3, bool) {
for z := room.Size/2 - 1.; z >= -room.Size/2+1; z -= .5 {
for x := -room.Size/2 + 1; x <= room.Size/2-1; x += .5 {
pos := p.Vec3{X: x, Z: z}
if w.HousePositionValid(room, pos) {
return pos, true
}
}
}
return p.Vec3{}, false
}
func (w *World) houseIntent(c *Character, i p.Intent) error {
if !w.IsHub() || c.HouseRoom == nil || c.View.HousePosition == nil {
return errors.New("house is synchronizing")
}
switch i.Op {
case "move":
n := math.Max(1, math.Hypot(i.X, i.Z))
c.MoveX = i.X / n
c.MoveZ = i.Z / n
c.LastMove = w.now
return nil
case "jump":
if math.Abs(c.View.HousePosition.Y-roomGround(c.HouseRoom, *c.View.HousePosition)) > .02 || c.JumpVelocity != 0 {
return errors.New("already jumping")
}
c.JumpVelocity = w.Balance.Housing.JumpSpeed
w.dirty = true
return nil
default:
return errors.New("use house controls or leave to use world actions")
}
}
func (w *World) moveHouse(c *Character, dt float64) {
room := c.HouseRoom
if room == nil || c.View.HousePosition == nil || room.ID != HouseID(c) {
return
}
old := *c.View.HousePosition
pos := old
r := w.Balance.Housing
// Small substeps prevent tunneling through narrow furniture on a delayed tick.
steps := max(1, int(math.Ceil(dt*r.WalkSpeed/.1)))
step := dt / float64(steps)
for range steps {
if c.JumpVelocity != 0 || pos.Y > roomGround(room, pos) {
c.JumpVelocity -= r.Gravity * step
pos.Y += c.JumpVelocity * step
}
pos.Y = math.Max(pos.Y, roomGround(room, pos))
if pos.Y >= 3 {
pos.Y = 3
c.JumpVelocity = math.Min(0, c.JumpVelocity)
}
next := pos
next.X += c.MoveX * r.WalkSpeed * step
if w.HousePositionValid(room, next) {
pos.X = next.X
}
next = pos
next.Z += c.MoveZ * r.WalkSpeed * step
if w.HousePositionValid(room, next) {
pos.Z = next.Z
}
ground := roomGround(room, pos)
if pos.Y <= ground {
pos.Y = ground
c.JumpVelocity = 0
}
}
if pos != old {
c.View.HousePosition = &pos
w.dirty = true
}
}
func (w *World) HouseSnapshot(c *Character) *p.HouseScene {
if HouseID(c) == "" || c.HouseRoom == nil {
return nil
}
out := *c.HouseRoom
out.People = []p.Entity{}
// Local movement is current to this simulation tick. Foreign visitors are read
// from their owning world's leased presence heartbeat, never simulated here.
for _, v := range c.HouseRoom.People {
if _, local := w.Characters[v.ID]; !local {
out.People = append(out.People, v)
}
}
for _, other := range w.orderedCharacters() {
if HouseID(other) == out.ID && other.Online && other.View.HousePosition != nil {
v := other.View.Entity
v.Position = *other.View.HousePosition
out.People = append(out.People, v)
}
}
return &out
}
@@ -0,0 +1,65 @@
package game
import (
p "aeternalis/server/internal/protocol"
"math"
"testing"
"time"
)
func TestIndoorMovementCollisionJumpAndReturn(t *testing.T) {
w, c := fixture(t)
w.Kind = "hub"
w.Phase = "hub"
house := ID()
c.View.HouseID = &house
c.HouseRoom = &p.HouseScene{ID: house, Size: 12, Furniture: []p.RoomFurniture{{ID: ID(), Width: 2, Depth: 2, Height: .8}}}
pos := p.Vec3{X: -3}
c.View.HousePosition = &pos
hub := c.View.Entity.Position
xp := c.View.Experience
if e := w.Intent(c.View.Entity.ID, p.Intent{Seq: 1, Op: "move", X: 1}); e != nil {
t.Fatal(e)
}
for range 40 {
w.moveHouse(c, .05)
}
if c.View.HousePosition.X > -1.34 || c.View.HousePosition.X < -1.5 {
t.Fatal("walked through table", *c.View.HousePosition)
}
if e := w.Intent(c.View.Entity.ID, p.Intent{Seq: 2, Op: "jump"}); e != nil {
t.Fatal(e)
}
if e := w.Intent(c.View.Entity.ID, p.Intent{Seq: 3, Op: "jump"}); e == nil {
t.Fatal("air jump accepted")
}
top := 0.
for range 12 {
w.moveHouse(c, .05)
top = math.Max(top, c.View.HousePosition.Y)
}
if top < .8 || c.View.HousePosition.X < -.8 {
t.Fatal("could not jump onto furniture", top, *c.View.HousePosition)
}
for range 100 {
w.moveHouse(c, .05)
}
if c.View.HousePosition.X > 6-w.Balance.Housing.PlayerRadius || c.View.HousePosition.Y != 0 {
t.Fatal("room boundary or landing failed", *c.View.HousePosition)
}
if e := w.Intent(c.View.Entity.ID, p.Intent{Seq: 4, Op: "attack"}); e == nil {
t.Fatal("house combat accepted")
}
if c.View.Entity.Position != hub || c.View.Experience != xp {
t.Fatal("indoor movement changed hub position or XP")
}
c.LastMove = w.now.Add(-time.Second)
w.Advance(w.now, .05)
if c.MoveX != 0 {
t.Fatal("stale indoor move did not stop")
}
LeaveHouse(c)
if c.View.HousePosition != nil || c.HouseRoom != nil || c.View.Entity.Position != hub || c.JumpVelocity != 0 {
t.Fatal("exit did not clear room state")
}
}
+22 -1
View File
@@ -16,7 +16,14 @@ func HouseID(c *Character) string {
}
return *c.View.HouseID
}
func LeaveHouse(c *Character) { c.View.HouseID = nil; c.MoveX = 0; c.MoveZ = 0 }
func LeaveHouse(c *Character) {
c.View.HouseID = nil
c.View.HousePosition = nil
c.HouseRoom = nil
c.JumpVelocity = 0
c.MoveX = 0
c.MoveZ = 0
}
func (w *World) restoreHousing() error {
if w.Balance.Housing == nil {
b, e := content.Load()
@@ -28,6 +35,20 @@ func (w *World) restoreHousing() error {
cat.RestoreFurniture(b)
w.Balance.Items = cat.Items
}
if w.Balance.Housing.WalkSpeed == 0 {
b, e := content.Load()
if e != nil {
return e
}
r := *w.Balance.Housing
r.WalkSpeed = b.Housing.WalkSpeed
r.PlayerRadius = b.Housing.PlayerRadius
r.JumpSpeed = b.Housing.JumpSpeed
r.Gravity = b.Housing.Gravity
r.TableHeight = b.Housing.TableHeight
r.StorageHeight = b.Housing.StorageHeight
w.Balance.Housing = &r
}
for _, c := range w.Characters {
LeaveHouse(c)
c.View.DestroyKey = nil
+14 -10
View File
@@ -14,15 +14,16 @@ import (
)
type Character struct {
XPCurveVersion int `json:"xpCurveVersion"`
PendingCast *PendingCast `json:"pendingCast,omitempty"`
RevivalCorpse string `json:"revivalCorpse,omitempty"`
RevivalAllowed bool `json:"revivalAllowed"`
DeathCount int64 `json:"deathCount"`
CraftReadyAt float64 `json:"craftReadyAt"`
AttackReadyAt float64 `json:"attackReadyAt"`
JumpVelocity float64 `json:"jumpVelocity"`
GatherReadyAt float64 `json:"gatherReadyAt"`
HouseRoom *p.HouseScene `json:"-"`
XPCurveVersion int `json:"xpCurveVersion"`
PendingCast *PendingCast `json:"pendingCast,omitempty"`
RevivalCorpse string `json:"revivalCorpse,omitempty"`
RevivalAllowed bool `json:"revivalAllowed"`
DeathCount int64 `json:"deathCount"`
CraftReadyAt float64 `json:"craftReadyAt"`
AttackReadyAt float64 `json:"attackReadyAt"`
JumpVelocity float64 `json:"jumpVelocity"`
GatherReadyAt float64 `json:"gatherReadyAt"`
View p.Character `json:"view"`
Account string `json:"account"`
@@ -526,7 +527,7 @@ func (w *World) Intent(id string, i p.Intent) error {
}
c.View.ACK = i.Seq
if HouseID(c) != "" {
return errors.New("leave the house to use world actions")
return w.houseIntent(c, i)
}
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
@@ -791,6 +792,8 @@ func (w *World) Advance(now time.Time, dt float64) {
}
if HouseID(c) == "" {
w.moveCharacter(c, dt)
} else {
w.moveHouse(c, dt)
}
for _, ch := range w.neighbors(c.View.Entity.Position) {
active[ch.Key] = true
@@ -859,6 +862,7 @@ func (w *World) Advance(now time.Time, dt float64) {
func (w *World) Snapshot(id string, known map[string]bool) p.Snapshot {
c := w.Characters[id]
s := p.Snapshot{Type: "snapshot", Version: 1, Tick: w.Tick, World: w.Info(), Self: c.View, Message: c.Message, Entities: []p.Entity{}, Chunks: []p.Chunk{}, ChunkKeys: []string{}, UnavailableResources: []string{}}
s.House = w.HouseSnapshot(c)
s.GatherRisks = map[string]p.GatheringRisk{}
allowed := map[string]bool{}
for _, ch := range w.neighbors(c.View.Entity.Position) {