Add physical hub portals and shared housing entrances
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package game
|
||||
|
||||
import (
|
||||
p "aeternalis/server/internal/protocol"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHubEntrancesScopeProximityAndMigration(t *testing.T) {
|
||||
w, c := fixture(t)
|
||||
entrances := func() map[string]p.Entity {
|
||||
out := map[string]p.Entity{}
|
||||
for _, e := range w.Snapshot(c.View.Entity.ID, nil).Entities {
|
||||
if e.Kind == "portal" || e.Kind == "house-door" {
|
||||
out[e.ID] = e
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
if len(entrances()) != 0 {
|
||||
t.Fatal("survival world exposed hub entrances before victory")
|
||||
}
|
||||
w.Kind = "hub"
|
||||
w.Phase = "hub"
|
||||
c.View.Entity.Position = w.PortalPosition()
|
||||
if e := w.Departure(c, "hub"); e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
visible := entrances()
|
||||
if len(visible) != 2 || visible["housing-entrance"].Position != w.HousingEntrancePosition() || visible["hub-portal"].Position != w.PortalPosition() {
|
||||
t.Fatal("hub entrance identity or location missing", visible)
|
||||
}
|
||||
c.View.Entity.Position.Y += w.Balance.GatherRange + 1
|
||||
if w.Departure(c, "hub") == nil {
|
||||
t.Fatal("portal ignored vertical distance")
|
||||
}
|
||||
c.View.Entity.Position = w.HousingEntrancePosition()
|
||||
if !w.NearEntrance(c, w.HousingEntrancePosition()) || w.Departure(c, "hub") == nil {
|
||||
t.Fatal("different entrance bypassed portal proximity")
|
||||
}
|
||||
c.View.HouseID = stringPtr(ID())
|
||||
if len(entrances()) != 0 || w.Departure(c, "hub") == nil {
|
||||
t.Fatal("indoor character accessed world entrances")
|
||||
}
|
||||
c.View.HouseID = nil
|
||||
c.View.Entity.Position.X = 600
|
||||
if len(entrances()) != 0 {
|
||||
t.Fatal("entrances escaped chunk interest bounds")
|
||||
}
|
||||
c.View.Entity.Position = w.PortalPosition()
|
||||
w.Balance.Entrances = nil
|
||||
raw, _ := json.Marshal(w)
|
||||
var restored World
|
||||
_ = json.Unmarshal(raw, &restored)
|
||||
if e := restored.Restore(w.now); e != nil {
|
||||
t.Fatal("old snapshot migration failed", e)
|
||||
}
|
||||
rc := restored.Characters[c.View.Entity.ID]
|
||||
_ = restored.Connect(rc.View.Entity.ID)
|
||||
if restored.Balance.Entrances == nil || restored.Departure(rc, "hub") != nil {
|
||||
t.Fatal("restore lost portal eligibility")
|
||||
}
|
||||
if restored.Departure(nil, "hub") == nil {
|
||||
t.Fatal("missing character traveled")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEntranceObstructionUsesServerGeometry(t *testing.T) {
|
||||
w, c := fixture(t)
|
||||
w.Kind = "hub"
|
||||
w.Phase = "hub"
|
||||
c.View.Entity.Position = w.PortalPosition()
|
||||
c.View.Entity.Position.X -= 2
|
||||
position := w.PortalPosition()
|
||||
position.X -= 1
|
||||
width, depth, height := .4, 3., 4.
|
||||
w.Structures["obstacle"] = &Structure{Entity: p.Entity{ID: "obstacle", Kind: "structure", Position: position, Width: &width, Depth: &depth, Height: &height, HP: 100}}
|
||||
if w.Departure(c, "hub") == nil {
|
||||
t.Fatal("portal ignored an obstruction")
|
||||
}
|
||||
delete(w.Structures, "obstacle")
|
||||
if e := w.Departure(c, "hub"); e != nil {
|
||||
t.Fatal("clear portal remained blocked", e)
|
||||
}
|
||||
}
|
||||
@@ -10,23 +10,42 @@ func (w *World) AllowsXP() bool {
|
||||
return w.Kind == "survival" && (w.Phase == "gathering" || w.Phase == "cataclysm")
|
||||
}
|
||||
func (w *World) PortalPosition() p.Vec3 { return w.terrain.Position(0, 0) }
|
||||
func (w *World) HousingEntrancePosition() p.Vec3 {
|
||||
return w.terrain.Position(w.Balance.Entrances.HousingX, w.Balance.Entrances.HousingZ)
|
||||
}
|
||||
|
||||
// Interactions use the same authoritative terrain height, distance and obstruction
|
||||
// rules as other world actions. A client click never supplies the server position.
|
||||
func (w *World) NearEntrance(c *Character, position p.Vec3) bool {
|
||||
if c == nil || HouseID(c) != "" || length3(c.View.Entity.Position, position) > w.Balance.GatherRange {
|
||||
return false
|
||||
}
|
||||
_, blocked := w.combatLine(body(c.View.Entity.Position), body(position), "")
|
||||
return !blocked
|
||||
}
|
||||
|
||||
// Escape is an explicit one-item action. It grants no victory/survival credit.
|
||||
func (w *World) Departure(c *Character, route string) error {
|
||||
if c == nil {
|
||||
return errors.New("character cannot travel")
|
||||
}
|
||||
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") {
|
||||
if !c.Online || c.Sheltered || c.View.RemainingSeconds <= 0 || (c.View.Entity.State != "alive" && c.View.Entity.State != "eligible") {
|
||||
return errors.New("character cannot travel")
|
||||
}
|
||||
if w.IsHub() {
|
||||
if route != "hub" {
|
||||
return errors.New("choose hub travel")
|
||||
}
|
||||
if !w.NearEntrance(c, w.PortalPosition()) {
|
||||
return errors.New("stand next to the hub world portal")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if w.Phase == "won" && c.View.Entity.State == "eligible" {
|
||||
if route != "portal" || distance(c.View.Entity.Position, w.PortalPosition()) > w.Balance.GatherRange {
|
||||
if route != "portal" || !w.NearEntrance(c, w.PortalPosition()) {
|
||||
return errors.New("stand next to the victory portal")
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -125,6 +125,17 @@ func New(id string, seed int64, now time.Time, b content.Balance) *World {
|
||||
return w
|
||||
}
|
||||
func (w *World) Restore(now time.Time) error {
|
||||
if w.Balance.Entrances == nil {
|
||||
defaults, err := content.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w.Balance.Entrances = defaults.Entrances
|
||||
w.dirty = true
|
||||
}
|
||||
if err := w.Balance.Entrances.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
if w.Balance.Inventory == nil {
|
||||
defaults, err := content.Load()
|
||||
if err != nil {
|
||||
@@ -957,8 +968,15 @@ func (w *World) Snapshot(id string, known map[string]bool) p.Snapshot {
|
||||
s.Entities = append(s.Entities, p.Entity{ID: area.ID, Kind: "area", Name: area.Attack.Name, Position: area.Position, State: area.Attack.Effect, Radius: &radius, HP: 1, MaxHP: 1})
|
||||
}
|
||||
}
|
||||
if w.Phase == "won" && visible(w.PortalPosition()) {
|
||||
s.Entities = append(s.Entities, p.Entity{ID: "hub-portal", Kind: "portal", Name: "Portal to the hub", Position: w.PortalPosition(), HP: 1, MaxHP: 1, State: "open"})
|
||||
if HouseID(c) == "" && (w.Phase == "won" || w.IsHub()) && visible(w.PortalPosition()) {
|
||||
name := "Portal to the hub"
|
||||
if w.IsHub() {
|
||||
name = "World portal"
|
||||
}
|
||||
s.Entities = append(s.Entities, p.Entity{ID: "hub-portal", Kind: "portal", Name: name, Position: w.PortalPosition(), HP: 1, MaxHP: 1, State: "open"})
|
||||
}
|
||||
if HouseID(c) == "" && w.IsHub() && visible(w.HousingEntrancePosition()) {
|
||||
s.Entities = append(s.Entities, p.Entity{ID: "housing-entrance", Kind: "house-door", Name: "Housing entrance", Position: w.HousingEntrancePosition(), HP: 1, MaxHP: 1, State: "open"})
|
||||
}
|
||||
sort.Slice(s.Entities, func(i, j int) bool { return s.Entities[i].ID < s.Entities[j].ID })
|
||||
return s
|
||||
|
||||
Reference in New Issue
Block a user