aorecomp

Anarchy Online classic in the browser

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


server/starter_missions_test.go (11055B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"math"
      8 	"reflect"
      9 	"testing"
     10 	"time"
     11 )
     12 
     13 var starterMissionBackyards = []struct {
     14 	booth          uint32
     15 	originator     byte
     16 	playfield      uint32
     17 	returnPosition [3]float32
     18 }{
     19 	{3221228472, 5, 3000, [3]float32{89.30216, 115.14444, 153.66087}},
     20 	{3221228482, 5, 3010, [3]float32{89.30216, 115.14444, 153.66087}},
     21 	{3221228532, 1, 3060, [3]float32{75.40743, 37.53481, 95.98035}},
     22 	{3221228552, 1, 3080, [3]float32{75.40743, 37.53481, 95.98035}},
     23 	{3221228572, 3, 3100, [3]float32{48.203575, 25.373432, 47.70779}},
     24 }
     25 
     26 func newBackyardMissionWorld(t *testing.T, boothID uint32) (*server, *combatTestConn) {
     27 	t.Helper()
     28 	world, output := newEquipmentWorld(t)
     29 	world.geometry = missionGeometryFixture(t)
     30 	world.nextNPC = 2000000
     31 	world.combatRoll = func(uint32) uint32 { return 0 }
     32 	player, booth := world.characters[1], gamedata.MissionBooths[boothID]
     33 	player.playfield, player.position, player.geometry = booth.Playfield, booth.Position, world.geometry
     34 	player.inventory.Credits = 5
     35 	if err := world.saveUser(player); err != nil {
     36 		t.Fatal(err)
     37 	}
     38 	if err := world.saveInventory(player, player.inventory); err != nil {
     39 		t.Fatal(err)
     40 	}
     41 	output.packets = nil
     42 	return world, output
     43 }
     44 
     45 func TestStarterMissionBackyardsCompleteAtTheirOwnBooths(t *testing.T) {
     46 	for _, backyard := range starterMissionBackyards {
     47 		t.Run(fmt.Sprint(backyard.playfield), func(t *testing.T) {
     48 			world, _ := newBackyardMissionWorld(t, backyard.booth)
     49 			player, client := world.characters[1], world.clients[1]
     50 			request := missionOffersRequest()
     51 			request[28] = backyard.originator
     52 			binary.BigEndian.PutUint32(request[33:37], backyard.booth)
     53 
     54 			if err := world.handleMissionOffers(client, request); err != nil {
     55 				t.Fatal(err)
     56 			}
     57 			if client.missionOffer == nil {
     58 				t.Fatal("authored starter booth has no offer")
     59 			}
     60 			if err := world.handleMissionAcceptance(client, missionAcceptanceRequest(*client.missionOffer)); err != nil {
     61 				t.Fatal(err)
     62 			}
     63 			cold := reloadMissionEntranceWorld(t)
     64 			mission := player.missions[0]
     65 
     66 			if player.inventory.Credits != 4 || len(player.inventory.Items) != 2 || mission.world != 65536 ||
     67 				mission.offer.booth != backyard.booth || mission.offer.originator != backyard.originator ||
     68 				mission.offer.definition.Playfield != backyard.playfield || mission.offer.definition.Entrance != backyard.booth ||
     69 				mission.offer.definition.RewardItem != 85596 || mission.offer.definition.RewardQuality != 1 ||
     70 				!reflect.DeepEqual(mission, cold.characters[1].missions[0]) || len(cold.mobileNPCs[mission.world]) != 5 {
     71 				t.Fatal("starter fee, selected reward, exterior or population changed on acceptance/restart", mission)
     72 			}
     73 
     74 			player.position = mission.offer.definition.Position
     75 			if err := world.handleMovement(client, playerMovementRequest(player, 1)); err != nil {
     76 				t.Fatal(err)
     77 			}
     78 			client.ready, world.clients[1] = true, client
     79 			objective := world.worldItems[player.missions[0].objective]
     80 			player.position, player.movement = objective.position, playerMovement{}
     81 			if err := world.handleInventory(client, worldPickupRequest(1, player.missions[0].objective)); err != nil {
     82 				t.Fatal(err)
     83 			}
     84 			player.position = world.geometry.mission(mission.world).exitPosition
     85 			if err := world.handleMovement(client, playerMovementRequest(player, 1)); err != nil {
     86 				t.Fatal(err)
     87 			}
     88 
     89 			if player.playfield != backyard.playfield {
     90 				t.Fatal("mission exit selected a different backyard", player.playfield)
     91 			}
     92 			for axis, expected := range backyard.returnPosition {
     93 				if math.Abs(float64(player.position[axis]-expected)) > .001 {
     94 					t.Fatal("mission exit lost the original entrance transform", player.position)
     95 				}
     96 			}
     97 
     98 			client.ready, world.clients[1] = true, client
     99 			player.position = gamedata.MissionBooths[backyard.booth].Position
    100 			returned := missionReturnRequest(1)
    101 			wrongBooth := uint32(3221228472)
    102 			if backyard.booth == wrongBooth {
    103 				wrongBooth = 3221228482
    104 			}
    105 			binary.BigEndian.PutUint32(returned[52:56], wrongBooth)
    106 			if err := world.handleMissionReturn(client, returned); err != nil {
    107 				t.Fatal(err)
    108 			}
    109 			if len(player.missions) != 1 || len(player.inventory.Items) != 3 || player.inventory.Credits != 4 {
    110 				t.Fatal("another backyard's booth consumed the objective or awarded a reward")
    111 			}
    112 			binary.BigEndian.PutUint32(returned[20:24], 2)
    113 			binary.BigEndian.PutUint32(returned[52:56], backyard.booth)
    114 			if err := world.handleMissionReturn(client, returned); err != nil {
    115 				t.Fatal(err)
    116 			}
    117 			binary.BigEndian.PutUint32(returned[20:24], 3)
    118 			if err := world.handleMissionReturn(client, returned); err != nil {
    119 				t.Fatal(err)
    120 			}
    121 			cold = reloadMissionEntranceWorld(t)
    122 
    123 			if player.inventory.Credits != 504 || len(player.missions) != 0 || player.experience != 0 ||
    124 				!reflect.DeepEqual(player.inventory, cold.characters[1].inventory) || len(player.inventory.Items) != 2 ||
    125 				player.inventory.Items[1] != (ownedItem{ID: 4, Type: 51022, Template: 85596, Quality: 1, Count: 1, Slot: 65}) ||
    126 				!cold.missionExpiry[0].completed || cold.characters[1].playfield != backyard.playfield {
    127 				t.Fatal("starter hand-in lost or duplicated a selected reward or retained its key/book", player.inventory)
    128 			}
    129 		})
    130 	}
    131 }
    132 
    133 func TestStarterMissionBackyardsRetireToTheirOwnEntrances(t *testing.T) {
    134 	for _, backyard := range starterMissionBackyards {
    135 		for _, expiry := range []bool{false, true} {
    136 			t.Run(fmt.Sprintf("%d/expiry=%v", backyard.playfield, expiry), func(t *testing.T) {
    137 				world, _ := newBackyardMissionWorld(t, backyard.booth)
    138 				player, client := world.characters[1], world.clients[1]
    139 				request := missionOffersRequest()
    140 				request[28] = backyard.originator
    141 				binary.BigEndian.PutUint32(request[33:37], backyard.booth)
    142 				if err := world.handleMissionOffers(client, request); err != nil {
    143 					t.Fatal(err)
    144 				}
    145 				if client.missionOffer == nil {
    146 					t.Fatal("authored starter booth has no offer")
    147 				}
    148 				if err := world.handleMissionAcceptance(client, missionAcceptanceRequest(*client.missionOffer)); err != nil {
    149 					t.Fatal(err)
    150 				}
    151 				mission := player.missions[0]
    152 				player.position = mission.offer.definition.Position
    153 				if err := world.handleMovement(client, playerMovementRequest(player, 1)); err != nil {
    154 					t.Fatal(err)
    155 				}
    156 				client.ready, world.clients[1] = true, client
    157 
    158 				if expiry {
    159 					world.advanceMissions(mission.expiresAt.Add(time.Second))
    160 				} else if err := world.handleMissionRemoval(client, missionRemovalPacket(1, mission.id)[20:]); err != nil {
    161 					t.Fatal(err)
    162 				}
    163 				cold := reloadMissionEntranceWorld(t)
    164 
    165 				if player.playfield != backyard.playfield || len(player.missions) != 0 || len(player.inventory.Items) != 1 ||
    166 					player.inventory.Credits != 4 || len(cold.missionExpiry) != 0 || len(cold.mobileNPCs[mission.world]) != 0 ||
    167 					!reflect.DeepEqual(player.userData(), cold.characters[1].userData()) {
    168 					t.Fatal("starter retirement lost its exterior or retained rewards/assignment/population", player.userData())
    169 				}
    170 				for axis, expected := range backyard.returnPosition {
    171 					if math.Abs(float64(player.position[axis]-expected)) > .001 {
    172 						t.Fatal("retirement lost the original entrance transform", player.position)
    173 					}
    174 				}
    175 			})
    176 		}
    177 	}
    178 }
    179 
    180 func TestStarterMissionBoothsPreserveOriginalTemplateAdmission(t *testing.T) {
    181 	for _, backyard := range starterMissionBackyards {
    182 		t.Run(fmt.Sprint(backyard.playfield), func(t *testing.T) {
    183 			world, output := newBackyardMissionWorld(t, backyard.booth)
    184 			player, client := world.characters[1], world.clients[1]
    185 			sequence := uint32(0)
    186 
    187 			// Original template118128 has no faction or level Use criteria.
    188 			for _, level := range []uint16{1, 26} {
    189 				for side := uint16(0); side <= 2; side++ {
    190 					player.level = level
    191 					player.appearance = player.appearance&^(3<<9) | side<<9
    192 					sequence++
    193 					if err := world.handleMissionBoothUse(client, missionBoothUseRequest(backyard.booth, sequence)); err != nil {
    194 						t.Fatal(err)
    195 					}
    196 				}
    197 			}
    198 
    199 			if len(output.packets) != 6 || player.inventory.Credits != 5 || len(player.inventory.Items) != 1 || len(player.missions) != 0 {
    200 				t.Fatal("opening a starter booth changed ownership or allocated a mission")
    201 			}
    202 			for _, packet := range output.packets {
    203 				if binary.BigEndian.Uint32(packet[29:33]) != 1 {
    204 					t.Fatal("starter booth acquired an unauthored faction/level restriction", packet)
    205 				}
    206 			}
    207 		})
    208 	}
    209 }
    210 
    211 func TestStarterMissionBackyardsKeepIndependentOwnersAcrossRetirement(t *testing.T) {
    212 	world, _ := newBackyardMissionWorld(t, 3221228482)
    213 	data := world.characters[1].userData()
    214 	data.ID, data.Name = 2, "Other backyard owner"
    215 	data.Playfield, data.Position = 3100, gamedata.MissionBooths[3221228572].Position
    216 	other := data.character()
    217 	other.geometry, other.inventory.Credits = world.geometry, 5
    218 	world.characters[2] = other
    219 	world.clients[2] = &worldClient{id: 2, target: true, ready: true, conn: &combatTestConn{}}
    220 	if err := world.saveUser(other); err != nil {
    221 		t.Fatal(err)
    222 	}
    223 	if err := world.saveInventory(other, other.inventory); err != nil {
    224 		t.Fatal(err)
    225 	}
    226 
    227 	for _, owner := range []struct {
    228 		id, booth  uint32
    229 		originator byte
    230 	}{{1, 3221228482, 5}, {2, 3221228572, 3}} {
    231 		client := world.clients[owner.id]
    232 		request := missionOffersRequest()
    233 		binary.BigEndian.PutUint32(request[8:12], owner.id)
    234 		binary.BigEndian.PutUint32(request[33:37], owner.booth)
    235 		request[28] = owner.originator
    236 		if err := world.handleMissionOffers(client, request); err != nil {
    237 			t.Fatal(err)
    238 		}
    239 		if client.missionOffer == nil {
    240 			t.Fatal("another backyard's accepted mission blocked generation", owner.id)
    241 		}
    242 		request = missionAcceptanceRequest(*client.missionOffer)
    243 		binary.BigEndian.PutUint32(request[8:12], owner.id)
    244 		if err := world.handleMissionAcceptance(client, request); err != nil {
    245 			t.Fatal(err)
    246 		}
    247 		player := world.characters[owner.id]
    248 		player.position = player.missions[0].offer.definition.Position
    249 		if err := world.handleMovement(client, playerMovementRequest(player, 1)); err != nil {
    250 			t.Fatal(err)
    251 		}
    252 		client.ready, world.clients[owner.id] = true, client
    253 	}
    254 	cold := reloadMissionEntranceWorld(t)
    255 	retained := other.userData()
    256 	if err := world.handleMissionRemoval(world.clients[1], missionRemovalPacket(1, 2)[20:]); err != nil {
    257 		t.Fatal(err)
    258 	}
    259 	retired := reloadMissionEntranceWorld(t)
    260 
    261 	if cold.characters[1].playfield != 65536 || cold.characters[2].playfield != 65537 ||
    262 		cold.characters[1].missions[0].offer.definition.Playfield != 3010 || cold.characters[2].missions[0].offer.definition.Playfield != 3100 ||
    263 		retired.characters[1].playfield != 3010 || len(retired.characters[1].missions) != 0 ||
    264 		!reflect.DeepEqual(retained, retired.characters[2].userData()) || len(retired.mobileNPCs[65536]) != 0 ||
    265 		len(retired.mobileNPCs[65537]) != 5 || len(retired.characters[2].missions) != 1 || retired.characters[2].inventory.Credits != 4 {
    266 		t.Fatal("backyard bindings merged owners, changed their exterior or retired another assignment")
    267 	}
    268 }