157 lines
4.5 KiB
Go
157 lines
4.5 KiB
Go
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 w.EncumbranceFactor(c) == 0 {
|
|
return errors.New("too encumbered to 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
|
|
speed := r.WalkSpeed * w.EncumbranceFactor(c)
|
|
// Small substeps prevent tunneling through narrow furniture on a delayed tick.
|
|
steps := max(1, int(math.Ceil(dt*speed/.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 * speed * step
|
|
if w.HousePositionValid(room, next) {
|
|
pos.X = next.X
|
|
}
|
|
next = pos
|
|
next.Z += c.MoveZ * speed * 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
|
|
}
|