aorecomp

Anarchy Online classic in the browser

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


server/backyard_test.go (9656B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"fmt"
      6 	"math"
      7 	"reflect"
      8 	"strings"
      9 	"testing"
     10 	"time"
     11 )
     12 
     13 var backyardFixtures = []struct {
     14 	name                                                       string
     15 	side                                                       uint32
     16 	city, cityDoor, backyard, exitDoor, training, trainingDoor uint32
     17 	arrival                                                    [3]float32
     18 }{
     19 	{"Omni", 2, 705, 3223782081, 3000, 3221294008, 950, 3876588472, [3]float32{105.302624, 115.5001, 150.95853}},
     20 	{"Neutral", 0, 566, 3222798902, 3060, 3221818356, 954, 3876588532, [3]float32{96.68448, 40.500116, 135.05869}},
     21 	{"Clan", 1, 640, 3224109696, 3100, 3221621788, 952, 3876588572, [3]float32{55.504486, 25.499996, 111.89857}},
     22 }
     23 
     24 func newBackyardTestWorld(t *testing.T) *server {
     25 	t.Helper()
     26 	t.Chdir(t.TempDir())
     27 	world, _ := newCombatWorld(t)
     28 	delete(world.characters, 1000000)
     29 	world.nextCharacter = 1
     30 	var err error
     31 	world.zoneLinks, err = readZoneLinks()
     32 	if err != nil {
     33 		t.Fatal(err)
     34 	}
     35 	player := world.characters[1]
     36 	player.name = "Backyard"
     37 	player.level, player.profession, player.appearance = 1, 1, 0x315
     38 	player.rotation = [4]float32{0, 0, 0, 1}
     39 	player.health = player.maximumHealth()
     40 	if err := world.saveUser(player); err != nil {
     41 		t.Fatal(err)
     42 	}
     43 	return world
     44 }
     45 
     46 func enterBackyardDoor(t *testing.T, world *server, player *character, doorID uint32) {
     47 	t.Helper()
     48 	for _, door := range world.zoneLinks["target"][player.playfield].Doors {
     49 		if door.ID != doorID {
     50 			continue
     51 		}
     52 		pose := *player
     53 		pose.position = door.Position
     54 		packet := npcMovementPacket(&pose, 20)[20:]
     55 		packet = append(packet[:16:16], encodeMovement1491(packet[16:])...)
     56 		client := &worldClient{id: player.id, ready: true, target: true, conn: &combatTestConn{}, npcs: make(map[uint32]bool)}
     57 		world.clients[player.id] = client
     58 		if err := world.handleMovement(client, packet); err != nil {
     59 			t.Fatal(err)
     60 		}
     61 		return
     62 	}
     63 	t.Fatalf("missing door %d in playfield %d", doorID, player.playfield)
     64 }
     65 
     66 func TestBackyardAuthoredLineRoutes(t *testing.T) {
     67 	links, err := readZoneLinks()
     68 	if err != nil {
     69 		t.Fatal(err)
     70 	}
     71 
     72 	entries, exits := 0, 0
     73 	for playfield, zone := range links["target"] {
     74 		for _, door := range zone.Doors {
     75 			if door.Arrival == nil {
     76 				continue
     77 			}
     78 			if strings.Contains(gamedata.Playfields[door.Destination].Name, "Backyard") {
     79 				entries++
     80 			}
     81 			if strings.Contains(gamedata.Playfields[playfield].Name, "Backyard") {
     82 				exits++
     83 			}
     84 		}
     85 	}
     86 
     87 	if entries != 130 || exits != 130 {
     88 		t.Fatalf("backyard line routes: %d entries, %d exits", entries, exits)
     89 	}
     90 	for _, fixture := range backyardFixtures {
     91 		found := false
     92 		for _, door := range links["target"][fixture.city].Doors {
     93 			if door.ID != fixture.cityDoor {
     94 				continue
     95 			}
     96 			found = door.Destination == fixture.backyard && door.Arrival != nil && !door.Return
     97 			if found {
     98 				for index, expected := range fixture.arrival {
     99 					found = found && math.Abs(float64(door.Arrival.Position[index]-expected)) < 0.0001
    100 				}
    101 			}
    102 		}
    103 		if !found {
    104 			t.Fatalf("%s city entrance does not use its authored backyard line", fixture.name)
    105 		}
    106 	}
    107 }
    108 
    109 func TestBackyardCityExitLevelAndCreditBoundaries(t *testing.T) {
    110 	for _, fixture := range backyardFixtures {
    111 		for _, scenario := range []struct {
    112 			level   uint16
    113 			credits uint32
    114 			allowed bool
    115 		}{{1, 0, false}, {1, 999, false}, {1, 1000, true}, {1, 1000000000, true}, {2, 0, true}, {2, 999, true}} {
    116 			t.Run(fmt.Sprintf("%s/level%d/credits%d", fixture.name, scenario.level, scenario.credits), func(t *testing.T) {
    117 				world := newBackyardTestWorld(t)
    118 				player := world.characters[1]
    119 				player.playfield, player.level = fixture.backyard, scenario.level
    120 				inventory := player.inventory
    121 				inventory.Credits = scenario.credits
    122 				if err := world.saveInventory(player, inventory); err != nil {
    123 					t.Fatal(err)
    124 				}
    125 
    126 				enterBackyardDoor(t, world, player, fixture.exitDoor)
    127 				if err := world.flushPositions(time.Now().Add(20 * time.Second)); err != nil {
    128 					t.Fatal(err)
    129 				}
    130 
    131 				expected := fixture.backyard
    132 				if scenario.allowed {
    133 					expected = fixture.city
    134 				}
    135 				if player.playfield != expected || player.inventory.Credits != scenario.credits {
    136 					t.Fatal("exit bypassed its gate, chose the wrong city or charged an unauthored fee")
    137 				}
    138 				if persisted := newTestInventoryServer(t).characters[1]; persisted.inventory.Credits != scenario.credits || scenario.allowed && persisted.playfield != fixture.city {
    139 					t.Fatal("exit did not retain its checkpointed destination and credit balance")
    140 				}
    141 			})
    142 		}
    143 	}
    144 }
    145 
    146 func TestBackyardEveryTrainingRoundTrip(t *testing.T) {
    147 	world := newBackyardTestWorld(t)
    148 	player := world.characters[1]
    149 	for city, zone := range world.zoneLinks["target"] {
    150 		for _, entrance := range zone.Doors {
    151 			if entrance.Arrival == nil || !strings.Contains(gamedata.Playfields[entrance.Destination].Name, "Backyard") {
    152 				continue
    153 			}
    154 			t.Run(fmt.Sprint(entrance.Destination), func(t *testing.T) {
    155 				player.playfield, player.level, player.returnDoor = city, 1, nil
    156 				var training, cityExit zoneDoor
    157 				for _, door := range world.zoneLinks["target"][entrance.Destination].Doors {
    158 					if door.DestinationDoor != 0 {
    159 						training = door
    160 					}
    161 					if door.Arrival != nil {
    162 						cityExit = door
    163 					}
    164 				}
    165 
    166 				enterBackyardDoor(t, world, player, entrance.ID)
    167 				enterBackyardDoor(t, world, player, training.ID)
    168 				if player.playfield != training.Destination || player.returnDoor == nil || *player.returnDoor != (doorOrigin{entrance.Destination, training.ID}) {
    169 					t.Fatal("training entrance lost the source backyard")
    170 				}
    171 				enterBackyardDoor(t, world, player, training.DestinationDoor)
    172 				if player.playfield != entrance.Destination || player.returnDoor != nil {
    173 					t.Fatal("training return reached the wrong backyard")
    174 				}
    175 				enterBackyardDoor(t, world, player, cityExit.ID)
    176 				if player.playfield != entrance.Destination {
    177 					t.Fatal("level-1 player bypassed the city gate")
    178 				}
    179 				player.level = 2
    180 				enterBackyardDoor(t, world, player, cityExit.ID)
    181 
    182 				if player.playfield != city || player.returnDoor != nil || player.inventory.Credits != 0 {
    183 					t.Fatal("eligible exit changed city, retained a return context or charged credits")
    184 				}
    185 			})
    186 		}
    187 	}
    188 }
    189 
    190 func TestBrowserCreationBackyardReturnSurvivesRestart(t *testing.T) {
    191 	world := newTestServer(t)
    192 	cookie := accountVisitor(t, world)
    193 	for _, fixture := range backyardFixtures {
    194 		fields := creationForm()
    195 		fields.Set("name", fixture.name+"Yard")
    196 		fields.Set("side", fmt.Sprint(fixture.side))
    197 		response := creationRequest(world, cookie, fields)
    198 		if response.Code != 204 {
    199 			t.Fatal(response.Body.String())
    200 		}
    201 		cookie = response.Result().Cookies()[0]
    202 		player := world.characters[world.nextCharacter]
    203 		items := append([]ownedItem(nil), player.inventory.Items...)
    204 
    205 		if player.returnDoor == nil || *player.returnDoor != (doorOrigin{fixture.backyard, fixture.trainingDoor}) {
    206 			t.Fatalf("%s creation has no assigned backyard return", fixture.name)
    207 		}
    208 		var trainingExit uint32
    209 		for _, door := range world.zoneLinks["target"][fixture.training].Doors {
    210 			if door.Return {
    211 				trainingExit = door.ID
    212 			}
    213 		}
    214 		enterBackyardDoor(t, world, player, trainingExit)
    215 		if player.playfield != fixture.backyard || player.returnDoor != nil {
    216 			t.Fatal("training exit did not reach its assigned backyard")
    217 		}
    218 		enterBackyardDoor(t, world, player, fixture.exitDoor)
    219 		if player.playfield != fixture.backyard {
    220 			t.Fatal("fresh level-1 player bypassed the city-exit gate")
    221 		}
    222 		enterBackyardDoor(t, world, player, fixture.trainingDoor)
    223 		if err := world.flushPositions(time.Now().Add(20 * time.Second)); err != nil {
    224 			t.Fatal(err)
    225 		}
    226 		persisted := newTestInventoryServer(t).characters[player.id]
    227 
    228 		if persisted.playfield != fixture.training || persisted.returnDoor == nil || *persisted.returnDoor != (doorOrigin{fixture.backyard, fixture.trainingDoor}) ||
    229 			!reflect.DeepEqual(items, persisted.inventory.Items) || persisted.inventory.Credits != 0 || persisted.experience != 0 || persisted.stats.ImprovementPoints != 1500 {
    230 			t.Fatalf("%s round trip lost the return association, issued items or progression", fixture.name)
    231 		}
    232 	}
    233 }
    234 
    235 func TestTrainingReturnKeepsDistinctBackyardsAcrossRestart(t *testing.T) {
    236 	world := newBackyardTestWorld(t)
    237 	world.nextCharacter = 1
    238 	second := world.newCharacter()
    239 	second.playfield = 705
    240 	if err := world.saveUser(second); err != nil {
    241 		t.Fatal(err)
    242 	}
    243 	world.characters[1].playfield = 705
    244 
    245 	enterBackyardDoor(t, world, world.characters[1], 3223782081)
    246 	enterBackyardDoor(t, world, world.characters[1], 3876588472)
    247 	enterBackyardDoor(t, world, second, 3223716545)
    248 	enterBackyardDoor(t, world, second, 3876588473)
    249 	if err := world.flushPositions(time.Now().Add(20 * time.Second)); err != nil {
    250 		t.Fatal(err)
    251 	}
    252 	persisted := newTestInventoryServer(t)
    253 	persisted.zoneLinks = world.zoneLinks
    254 	if persisted.characters[1].returnDoor == nil || *persisted.characters[1].returnDoor != (doorOrigin{3000, 3876588472}) ||
    255 		persisted.characters[2].returnDoor == nil || *persisted.characters[2].returnDoor != (doorOrigin{3001, 3876588473}) {
    256 		t.Fatal("players sharing training lost their independent backyard associations")
    257 	}
    258 	enterBackyardDoor(t, persisted, persisted.characters[1], 0xc00003b6)
    259 	enterBackyardDoor(t, persisted, persisted.characters[2], 0xc00003b6)
    260 
    261 	if persisted.characters[1].playfield != 3000 || persisted.characters[2].playfield != 3001 ||
    262 		persisted.characters[1].returnDoor != nil || persisted.characters[2].returnDoor != nil {
    263 		t.Fatal("shared training exit returned a player to the other player's backyard")
    264 	}
    265 }