aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


server/whompah_test.go (6699B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"math"
      6 	"testing"
      7 	"time"
      8 )
      9 
     10 func newWhomPahWorld(t *testing.T) (*server, *combatTestConn) {
     11 	t.Helper()
     12 	world, conn := newCombatWorld(t)
     13 	player := world.newCharacter()
     14 	player.playfield = 705
     15 	player.position = [3]float32{910.12, 16.9, 487.01}
     16 	player.returnDoor = &doorOrigin{1183, 0xc000049f}
     17 	if err := world.saveUser(player); err != nil {
     18 		t.Fatal(err)
     19 	}
     20 	world.zoneLinks = map[string]map[uint32]zoneLinks{"target": {
     21 		705: {Doors: []zoneDoor{{ID: 0xc00502c1, Position: [3]float32{910.12, 16.823326, 484.50986}, Rotation: [4]float32{0, 0, 0, 1}, Destination: 710,
     22 			Arrival: &zoneArrival{Position: [3]float32{388, 17.5, 382.484985}, Rotation: [4]float32{0, 0.70710677, 0, 0.70710677}}}}},
     23 		710: {Doors: []zoneDoor{{ID: 0xc00002c6, Position: [3]float32{381.60974, 17.423323, 382.13492}, Rotation: [4]float32{0, 0.70091057, 0, 0.71324915}, Destination: 705,
     24 			Arrival: &zoneArrival{Position: [3]float32{910, 16.9, 492.003815}, Rotation: [4]float32{0, 0.000038147, 0, 1}}}}},
     25 	}}
     26 	return world, conn
     27 }
     28 
     29 func TestWhomPahAuthoredRouteMappings(t *testing.T) {
     30 	links, err := readZoneLinks()
     31 	if err != nil {
     32 		t.Fatal(err)
     33 	}
     34 
     35 	var routes []zoneDoor
     36 	for _, source := range []struct{ playfield, door uint32 }{{705, 0xc00502c1}, {710, 0xc00002c6}} {
     37 		for _, door := range links["target"][source.playfield].Doors {
     38 			if door.ID == source.door {
     39 				routes = append(routes, door)
     40 			}
     41 		}
     42 	}
     43 
     44 	if len(routes) != 2 || routes[0].Destination != 710 || routes[1].Destination != 705 || routes[0].Arrival == nil || routes[1].Arrival == nil {
     45 		t.Fatal("shipped Entertainment/Trade route is not mapped in both directions")
     46 	}
     47 	if routes[0].Arrival.Position != [3]float32{388, 17.5, 382.484985} || routes[1].Arrival.Position != [3]float32{910, 16.9, 492.003815} ||
     48 		routes[0].Return || routes[1].Return || routes[0].DestinationDoor != 0 || routes[1].DestinationDoor != 0 {
     49 		t.Fatal("line arrivals were confused with shared-interior door destinations")
     50 	}
     51 }
     52 
     53 func TestWhomPahNetworkAcceptsLevelOneOfEveryFaction(t *testing.T) {
     54 	t.Chdir(t.TempDir())
     55 	world, _ := newWhomPahWorld(t)
     56 	links, err := readZoneLinks()
     57 	if err != nil {
     58 		t.Fatal(err)
     59 	}
     60 	world.zoneLinks = links
     61 	player, client := world.characters[1], world.clients[1]
     62 	player.level = 1
     63 	count := 0
     64 	for playfield, zone := range links["target"] {
     65 		for _, door := range zone.Doors {
     66 			if !door.WhomPah {
     67 				continue
     68 			}
     69 			count++
     70 			player.playfield, player.position = playfield, door.Position
     71 			for side := uint16(0); side <= 2; side++ {
     72 				player.appearance = player.appearance&^(3<<9) | side<<9
     73 				dest, err := world.zoneDestination(player, client)
     74 				if err != nil || dest == nil || dest.Playfield != door.Destination || dest.Position != door.Arrival.Position {
     75 					t.Fatalf("level-one side %d could not use Whom-Pah %d/%x: %v", side, playfield, door.ID, err)
     76 				}
     77 			}
     78 		}
     79 	}
     80 	if count != 60 {
     81 		t.Fatalf("got %d authored Whom-Pah routes", count)
     82 	}
     83 }
     84 
     85 func TestWhomPahHeldMovementRoundTripPersistsWithoutBouncing(t *testing.T) {
     86 	t.Chdir(t.TempDir())
     87 	world, first := newWhomPahWorld(t)
     88 	player, old := world.characters[1], world.clients[1]
     89 	now := time.Now()
     90 	world.characters[old.id].movement = heldPlayerMovement(world.characters[old.id], -1, now)
     91 
     92 	world.advanceZoneCrossings(now.Add(1500 * time.Millisecond))
     93 	if player.playfield != 710 || player.position != [3]float32{388, 17.5, 382.484985} || player.returnDoor != nil {
     94 		t.Fatal("held movement failed to reach the Trade arrival line")
     95 	}
     96 	if err := world.flushPositions(now.Add(20 * time.Second)); err != nil {
     97 		t.Fatal(err)
     98 	}
     99 	if restored := newTestInventoryServer(t).characters[1]; restored.playfield != 710 || restored.position != player.position || restored.returnDoor != nil {
    100 		t.Fatal("Trade arrival did not survive restart")
    101 	}
    102 	second := &combatTestConn{}
    103 	world.clients[1] = &worldClient{id: 1, conn: second, target: true, ready: true}
    104 	stale := npcMovementPacket(player, 20)[20:]
    105 	stale = append(stale[:16:16], encodeMovement1491(stale[16:])...)
    106 	if err := world.handleMovement(old, stale); err != nil {
    107 		t.Fatal(err)
    108 	}
    109 	if err := world.handleMovement(world.clients[1], stale); err != nil || player.playfield != 710 || len(second.packets) != 0 {
    110 		t.Fatal("arrival or stale session immediately restarted zoning")
    111 	}
    112 	player.position = [3]float32{381.60974, 17.5, 382.13492}
    113 	returnMove := npcMovementPacket(player, 20)[20:]
    114 	returnMove = append(returnMove[:16:16], encodeMovement1491(returnMove[16:])...)
    115 	if err := world.handleMovement(world.clients[1], returnMove); err != nil {
    116 		t.Fatal(err)
    117 	}
    118 	if err := world.flushPositions(now.Add(40 * time.Second)); err != nil {
    119 		t.Fatal(err)
    120 	}
    121 	restored := newTestInventoryServer(t).characters[1]
    122 
    123 	if restored.playfield != 705 || restored.position != [3]float32{910, 16.9, 492.003815} || restored.returnDoor != nil {
    124 		t.Fatal("return trip lost the Entertainment arrival or retained an interior return context")
    125 	}
    126 	for _, conn := range []*combatTestConn{first, second} {
    127 		if len(conn.packets) != 2 || binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x43197d22 || binary.BigEndian.Uint32(conn.packets[1][16:20]) != 0x3c {
    128 			t.Fatal("route did not produce exactly one native teleport and redirect per leg")
    129 		}
    130 	}
    131 }
    132 
    133 func TestWhomPahDoorPlaneAndSessionEligibility(t *testing.T) {
    134 	for _, scenario := range []struct {
    135 		name     string
    136 		position [3]float32
    137 		arrives  bool
    138 	}{
    139 		{"inside front plane", [3]float32{0, 0, math.Nextafter32(0.8, 0)}, true},
    140 		{"front plane equality", [3]float32{0, 0, 0.8}, false},
    141 		{"inside back plane", [3]float32{0, 0, -math.Nextafter32(0.8, 0)}, true},
    142 		{"back plane equality", [3]float32{0, 0, -0.8}, false},
    143 		{"outside horizontal envelope", [3]float32{2, 0, 0}, false},
    144 		{"outside vertical envelope", [3]float32{0, 8, 0}, false},
    145 		{"dead", [3]float32{}, false},
    146 		{"loading", [3]float32{}, false},
    147 		{"not ready", [3]float32{}, false},
    148 	} {
    149 		t.Run(scenario.name, func(t *testing.T) {
    150 			t.Chdir(t.TempDir())
    151 			world, _ := newWhomPahWorld(t)
    152 			world.zoneLinks["target"][705].Doors[0].Position = [3]float32{}
    153 			world.characters[1].position = scenario.position
    154 			client := world.clients[1]
    155 			world.characters[client.id].movement = heldPlayerMovement(world.characters[client.id], 1, time.Now())
    156 			switch scenario.name {
    157 			case "dead":
    158 				world.characters[1].health = 0
    159 			case "loading":
    160 				client.loading = true
    161 			case "not ready":
    162 				client.ready = false
    163 			}
    164 
    165 			world.advanceZoneCrossings(world.characters[client.id].movement.updatedAt)
    166 
    167 			if (world.characters[1].playfield == 710) != scenario.arrives {
    168 				t.Fatal("door plane or player eligibility disagrees with the selected policy")
    169 			}
    170 		})
    171 	}
    172 }