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
|
||||
|
||||
@@ -31,6 +31,7 @@ func TestHouseVisitorsAcrossHubOwners(t *testing.T) {
|
||||
_ = w.Connect(c.View.Entity.ID)
|
||||
w.Kind = "hub"
|
||||
w.Phase = "hub"
|
||||
c.View.Entity.Position = w.HousingEntrancePosition()
|
||||
l, _, e := db.Acquire(ctx, w)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
|
||||
@@ -27,6 +27,7 @@ func TestHouseDeathTravelAndRecovery(t *testing.T) {
|
||||
_ = hub.Connect(c.View.Entity.ID)
|
||||
hub.Kind = "hub"
|
||||
hub.Phase = "hub"
|
||||
c.View.Entity.Position = hub.HousingEntrancePosition()
|
||||
hl, _, e := db.Acquire(ctx, hub)
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
@@ -62,6 +63,7 @@ func TestHouseDeathTravelAndRecovery(t *testing.T) {
|
||||
t.Fatal(e)
|
||||
}
|
||||
// Traveling cancels the action, retains the exact key, and never creates another owner copy.
|
||||
c.View.Entity.Position = hub.PortalPosition()
|
||||
act(hl, hub, c, HouseRequest{Op: "destroy-key", Item: key})
|
||||
oldWorld := *hub
|
||||
oldWorld.Characters = map[string]*game.Character{c.View.Entity.ID: c}
|
||||
@@ -140,6 +142,7 @@ func TestHouseDeathTravelAndRecovery(t *testing.T) {
|
||||
if v := state(hub, replacement); v.Houses[0].Keys != 2 {
|
||||
t.Fatal("creator recovery invalidated corpse key")
|
||||
}
|
||||
replacement.View.Entity.Position = hub.HousingEntrancePosition()
|
||||
act(hl, hub, replacement, HouseRequest{Op: "enter", HouseID: house})
|
||||
if state(hub, replacement).Room.Floor.Inventory["wood"] != 7 {
|
||||
t.Fatal("recovery recreated or lost contents")
|
||||
|
||||
@@ -301,6 +301,21 @@ func (d *DB) mutateHouse(ctx context.Context, tx *sql.Tx, w *game.World, c *game
|
||||
return houseError("a carried key or invitation is required")
|
||||
}
|
||||
if r.Op == "enter" {
|
||||
if !w.NearEntrance(c, w.HousingEntrancePosition()) {
|
||||
return houseError("stand next to the housing entrance in this hub")
|
||||
}
|
||||
if r.Item != "" {
|
||||
// Explicit key use must name a carried, active key for this exact house;
|
||||
// an invitation cannot make a wrong or destroyed key usable.
|
||||
carried := slices.ContainsFunc(c.View.Items, func(i p.ItemInstance) bool { return i.ID == r.Item && i.HouseID != nil && *i.HouseID == h.View.ID })
|
||||
var active int
|
||||
if e := tx.QueryRowContext(ctx, d.q(`SELECT COUNT(*) FROM house_keys WHERE id=? AND house_id=? AND state='active'`), r.Item, h.View.ID).Scan(&active); e != nil {
|
||||
return e
|
||||
}
|
||||
if !carried || active != 1 {
|
||||
return houseError("use a carried key for this house")
|
||||
}
|
||||
}
|
||||
if c.WorldsSurvived < 1 {
|
||||
return houseError("survive a world before entering housing")
|
||||
}
|
||||
|
||||
@@ -51,6 +51,9 @@ func testHousingLifecycle(t *testing.T, dsn string) {
|
||||
// Seed arrivals before making this fixture a hub; travel itself has separate coverage.
|
||||
w.Kind = "hub"
|
||||
w.Phase = "hub"
|
||||
for _, c := range []*game.Character{owner, guest, coowner} {
|
||||
c.View.Entity.Position = w.HousingEntrancePosition()
|
||||
}
|
||||
save := func() {
|
||||
t.Helper()
|
||||
if e := l.Save(ctx, w, ""); e != nil {
|
||||
@@ -109,7 +112,13 @@ func testHousingLifecycle(t *testing.T, dsn string) {
|
||||
key := owner.View.Items[len(owner.View.Items)-1].ID
|
||||
reject(owner, HouseRequest{Op: "create", Name: "Duplicate"})
|
||||
reject(guest, HouseRequest{Op: "enter", HouseID: house})
|
||||
act(owner, HouseRequest{Op: "enter", HouseID: house})
|
||||
owner.View.Entity.Position.X += 100
|
||||
reject(owner, HouseRequest{Op: "enter", HouseID: house, Item: key})
|
||||
owner.View.Entity.Position = w.HousingEntrancePosition()
|
||||
reject(owner, HouseRequest{Op: "enter", HouseID: house, Item: "unowned-key"})
|
||||
entry := HouseRequest{RequestID: game.ID(), Op: "enter", HouseID: house, Item: key}
|
||||
act(owner, entry)
|
||||
act(owner, entry) // Replaying an entry from inside the room must not enter twice.
|
||||
if e := w.Intent(owner.View.Entity.ID, p.Intent{Seq: 1, Op: "attack"}); e == nil {
|
||||
t.Fatal("world action inside house")
|
||||
}
|
||||
|
||||
@@ -203,7 +203,22 @@ func runTravelTests(t *testing.T, dsn string) {
|
||||
if current == hub {
|
||||
switchTo, switchLease = other, ol
|
||||
}
|
||||
switched, e := lease.PrepareTravel(ctx, current, c.Account, game.ID(), switchTo.ID, "hub")
|
||||
traveler := current.ForAccount(c.Account)
|
||||
traveler.View.Entity.Position.X += 100
|
||||
beforePortal, _ := json.Marshal(current)
|
||||
requestAtPortal := game.ID()
|
||||
if _, e = lease.PrepareTravel(ctx, current, c.Account, requestAtPortal, switchTo.ID, "hub"); !errors.Is(e, ErrTravel) {
|
||||
t.Fatal("remote hub travel accepted", e)
|
||||
}
|
||||
afterPortal, _ := json.Marshal(current)
|
||||
if string(beforePortal) != string(afterPortal) {
|
||||
t.Fatal("remote portal rejection mutated source")
|
||||
}
|
||||
if pending, e := db.PendingTransfer(ctx, c.Account); e != nil || pending != nil {
|
||||
t.Fatal("remote portal rejection created transit escrow", e)
|
||||
}
|
||||
traveler.View.Entity.Position = current.PortalPosition()
|
||||
switched, e := lease.PrepareTravel(ctx, current, c.Account, requestAtPortal, switchTo.ID, "hub")
|
||||
if e != nil {
|
||||
t.Fatal(e)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user