aorecomp

Anarchy Online classic in the browser

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


server/support_beam_quality_test.go (12679B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"slices"
      8 	"testing"
      9 	"time"
     10 )
     11 
     12 func TestSupportBeamQualitiesEnforceWearBoundariesAndRecover(t *testing.T) {
     13 	for _, template := range []uint32{122905, 122906} {
     14 		for _, row := range []struct {
     15 			quality, minimum, maximum, critical, value uint32
     16 			blunt, brawling, dimach                    uint16
     17 		}{
     18 			{1, 6, 25, 15, 3170, 6, 6, 6},
     19 			{2, 6, 27, 18, 3186, 11, 9, 7},
     20 			{10, 7, 47, 38, 4521, 51, 33, 15},
     21 			{11, 8, 50, 40, 4838, 57, 36, 16},
     22 			{19, 9, 70, 60, 8575, 97, 60, 24},
     23 			{20, 9, 72, 63, 9193, 102, 63, 25},
     24 		} {
     25 			for _, failed := range []uint32{0, 4, 107, 142, 144} {
     26 				t.Run(fmt.Sprintf("%d/QL%d/below%d", template, row.quality, failed), func(t *testing.T) {
     27 					world, output := newEquipmentWorld(t)
     28 					player, client := world.characters[1], world.clients[1]
     29 					player.appearance = 0x545
     30 					player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
     31 					player.stats.Melee.TwoHandBlunt = row.blunt
     32 					player.stats.Body.Brawling, player.stats.Body.Dimach = row.brawling, row.dimach
     33 					switch failed {
     34 					case 4:
     35 						player.appearance = 0x515
     36 					case 107:
     37 						player.stats.Melee.TwoHandBlunt--
     38 					case 142:
     39 						player.stats.Body.Brawling--
     40 					case 144:
     41 						player.stats.Body.Dimach--
     42 					}
     43 					player.inventory.Items[0].Template, player.inventory.Items[0].Quality = template, row.quality
     44 					if err := world.saveUser(player); err != nil {
     45 						t.Fatal(err)
     46 					}
     47 					if err := world.saveInventory(player, player.inventory); err != nil {
     48 						t.Fatal(err)
     49 					}
     50 
     51 					started := time.Now()
     52 					if err := world.handleInventory(client, inventoryMovePacket(1, 64, 6)[20:]); err != nil {
     53 						t.Fatal(err)
     54 					}
     55 					if client.equipment != nil {
     56 						finish := client.equipment.readyAt
     57 						if finish.Before(started.Add(4500*time.Millisecond)) || finish.After(time.Now().Add(4500*time.Millisecond)) {
     58 							t.Fatal("SupportBeam did not retain its original equip delay")
     59 						}
     60 						world.advanceEquipment(finish.Add(-time.Nanosecond))
     61 						if player.inventory.Items[0].Slot != 64 {
     62 							t.Fatal("weapon moved before its equip deadline")
     63 						}
     64 						world.advanceEquipment(finish)
     65 					}
     66 					cold := newTestInventoryServer(t).characters[1]
     67 
     68 					expected := ownedItem{ID: 1, Type: 51018, Template: template, Quality: row.quality, Slot: 64, Count: 1}
     69 					if failed == 0 {
     70 						expected.Slot = 6
     71 					}
     72 					if !slices.Equal(player.inventory.Items, []ownedItem{expected}) || !slices.Equal(cold.inventory.Items, []ownedItem{expected}) {
     73 						t.Fatal("wear boundary lost the selected instance or placement", player.inventory.Items, cold.inventory.Items)
     74 					}
     75 					if failed != 0 {
     76 						return
     77 					}
     78 					weapon := cold.attackWeapon()
     79 					if weapon == nil || itemStat(weapon, 286) != row.minimum || itemStat(weapon, 285) != row.maximum || itemStat(weapon, 284) != row.critical ||
     80 						itemStat(weapon, 294) != 350 || itemStat(weapon, 210) != 250 || itemStat(weapon, 211) != 450 || itemStat(weapon, 287) != 3 ||
     81 						itemStat(weapon, 440) != 118 || expected.usesAmmo() || expected.shopPrice() != row.value {
     82 						t.Fatal("selected quality lost damage, timing, ammo, range, damage type or value", weapon)
     83 					}
     84 					low, high := uint32(122905), uint32(122906)
     85 					if row.quality == 1 {
     86 						high = low
     87 					} else if row.quality == 20 {
     88 						low = high
     89 					}
     90 					var delivered []byte
     91 					for _, packet := range output.packets {
     92 						if binary.BigEndian.Uint32(packet[16:20]) == 0x3b1d2268 {
     93 							delivered = packet
     94 						}
     95 					}
     96 					if len(delivered) < 91 || binary.BigEndian.Uint32(delivered[71:75]) != row.quality ||
     97 						binary.BigEndian.Uint32(delivered[79:83]) != low || binary.BigEndian.Uint32(delivered[87:91]) != high {
     98 						t.Fatal("native weapon delivery lost the selected endpoints")
     99 					}
    100 				})
    101 			}
    102 		}
    103 	}
    104 }
    105 
    106 func TestSupportBeamSelectedDamageAndTimingWithoutAmmunition(t *testing.T) {
    107 	for _, row := range []struct{ quality, normal, critical uint32 }{
    108 		{1, 50, 80}, {2, 54, 90}, {10, 94, 170}, {11, 100, 180}, {19, 140, 260}, {20, 144, 270},
    109 	} {
    110 		for _, critical := range []bool{false, true} {
    111 			t.Run(fmt.Sprintf("QL%d/critical%v", row.quality, critical), func(t *testing.T) {
    112 				world, output := newRangedWorld(t)
    113 				player, target := world.characters[1], world.characters[1000000]
    114 				player.appearance = 0x545
    115 				target.position = [3]float32{1, 0, 0}
    116 				player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
    117 				player.stats.Melee.TwoHandBlunt = 400
    118 				player.stats.Body.Brawling, player.stats.Body.Dimach = 100, 100
    119 				player.stats.Speed.MeleeInitiative, player.aggDef = 0, 75
    120 				player.inventory.Items[0].Template, player.inventory.Items[0].Quality, player.inventory.Items[0].Ammo = 122905, row.quality, 0
    121 				if err := world.saveInventory(player, player.inventory); err != nil {
    122 					t.Fatal(err)
    123 				}
    124 				world.combatRoll = func(limit uint32) uint32 {
    125 					if limit == 10000 || limit == 100 && critical {
    126 						return 0
    127 					}
    128 					return limit - 1
    129 				}
    130 
    131 				if err := world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:]); err != nil {
    132 					t.Fatal(err)
    133 				}
    134 				hit := player.nextAttack
    135 				world.advanceCombat(hit)
    136 				cold := newTestInventoryServer(t)
    137 
    138 				damage := row.normal
    139 				if critical {
    140 					damage = row.critical
    141 				}
    142 				if target.health != 1000-damage || player.rechargeUntil.Sub(hit) != 2500*time.Millisecond || player.nextAttack.Sub(hit) != 6000*time.Millisecond ||
    143 					player.inventory.Items[0].Ammo != 0 || cold.characters[1].inventory.Items[0].Ammo != 0 || player.inventory.Items[1].Count != 100 {
    144 					t.Fatal("selected SupportBeam damage, cadence or ammunition differs", target.health, player.inventory)
    145 				}
    146 				found := false
    147 				for _, packet := range output.packets {
    148 					if binary.BigEndian.Uint32(packet[16:20]) == 0x46002f16 && binary.BigEndian.Uint32(packet[24:28]) == player.id {
    149 						found = binary.BigEndian.Uint32(packet[33:37]) == 0xffffffff
    150 					}
    151 				}
    152 				if !found {
    153 					t.Fatal("melee result lost its unlimited-ammunition sentinel")
    154 				}
    155 			})
    156 		}
    157 	}
    158 }
    159 
    160 func TestSupportBeamUsesDuckInsteadOfEvadeClose(t *testing.T) {
    161 	for _, duck := range []uint16{0, 10000} {
    162 		t.Run(fmt.Sprint(duck), func(t *testing.T) {
    163 			world, output := newRangedWorld(t)
    164 			player, target := world.characters[1], world.characters[1000000]
    165 			player.appearance = 0x545
    166 			player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
    167 			player.stats.Melee.TwoHandBlunt = 400
    168 			player.stats.Body.Brawling, player.stats.Body.Dimach = 100, 100
    169 			player.inventory.Items[0].Template, player.inventory.Items[0].Quality, player.inventory.Items[0].Ammo = 122905, 2, 0
    170 			target.position, target.stats.Speed.DuckExplosions, target.stats.Speed.EvadeClose = [3]float32{1, 0, 0}, duck, 10000-duck
    171 			if err := world.saveInventory(player, player.inventory); err != nil {
    172 				t.Fatal(err)
    173 			}
    174 			world.combatRoll = func(limit uint32) uint32 {
    175 				if limit == 10000 {
    176 					return 5000
    177 				}
    178 				return limit - 1
    179 			}
    180 
    181 			if err := world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:]); err != nil {
    182 				t.Fatal(err)
    183 			}
    184 			world.advanceCombat(player.nextAttack)
    185 
    186 			health, opcode := uint32(946), uint32(0x46002f16)
    187 			if duck != 0 {
    188 				health, opcode = 1000, 0x5c654b28
    189 			}
    190 			if target.health != health || !slices.ContainsFunc(output.packets, func(packet []byte) bool {
    191 				return binary.BigEndian.Uint32(packet[16:20]) == opcode && binary.BigEndian.Uint32(packet[24:28]) == player.id
    192 			}) {
    193 				t.Fatal("Support Beam used the wrong defense skill", target.health)
    194 			}
    195 		})
    196 	}
    197 }
    198 
    199 func TestSupportBeamGeneratedPickupEquipRemoveAndSale(t *testing.T) {
    200 	for _, scenario := range []struct {
    201 		template, quality       uint32
    202 		blunt, brawling, dimach uint16
    203 		credits                 uint32
    204 	}{
    205 		{122905, 2, 11, 9, 7, 160}, {122906, 2, 11, 9, 7, 160},
    206 		{122905, 11, 57, 36, 16, 242}, {122906, 11, 57, 36, 16, 242},
    207 	} {
    208 		t.Run(fmt.Sprintf("%d/QL%d", scenario.template, scenario.quality), func(t *testing.T) {
    209 			world, _ := newKillExperienceWorld(t, "unarmed")
    210 			// Controlled membership tests delivery, not historical Leet equipment drops.
    211 			profile := *world.characters[1000000].combatProfile
    212 			profile.Loot = []gamedata.NPCLoot{{Template: scenario.template, Quality: [2]uint32{scenario.quality, scenario.quality}, Chance: 100}}
    213 			world.characters[1000000].combatProfile = &profile
    214 			world.combatRoll = func(uint32) uint32 { return 0 }
    215 			identity := world.nextItem + 1
    216 
    217 			attackForExperience(t, world, "unarmed")
    218 			if err := world.handleLootUse(world.clients[1], lootUseRequest(1)); err != nil {
    219 				t.Fatal(err)
    220 			}
    221 			for range 2 {
    222 				if err := world.handleInventory(world.clients[1], lootTakeRequest()); err != nil {
    223 					t.Fatal(err)
    224 				}
    225 			}
    226 			world = newTestInventoryServer(t)
    227 			player := world.characters[1]
    228 			player.appearance = 0x545
    229 			client := &worldClient{id: 1, conn: &combatTestConn{}, target: true, ready: true}
    230 			world.clients[1] = client
    231 			player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
    232 			player.stats.Melee.TwoHandBlunt = scenario.blunt - 1
    233 			player.stats.Body.Brawling, player.stats.Body.Dimach = scenario.brawling, scenario.dimach
    234 			if err := world.saveUser(player); err != nil {
    235 				t.Fatal(err)
    236 			}
    237 			if err := world.handleInventory(client, inventoryMovePacket(1, 64, 6)[20:]); err != nil || client.equipment != nil ||
    238 				len(player.inventory.Items) != 1 || player.inventory.Items[0].ID != identity || player.inventory.Items[0].Slot != 64 {
    239 				t.Fatal("rejected wear lost the picked-up instance", err)
    240 			}
    241 			player.stats.Melee.TwoHandBlunt++
    242 			if err := world.saveUser(player); err != nil {
    243 				t.Fatal(err)
    244 			}
    245 			for _, move := range [][2]uint32{{64, 6}, {6, 64}, {64, 6}, {6, 64}} {
    246 				request := inventoryMovePacket(1, move[0], move[1])
    247 				if move[0] < 64 {
    248 					request = itemMovePacket(1, move[0], move[1])
    249 				}
    250 				if err := world.handleInventory(client, request[20:]); err != nil || client.equipment == nil {
    251 					t.Fatal("generated selected-quality weapon did not move", err)
    252 				}
    253 				world.advanceEquipment(client.equipment.readyAt)
    254 				cold := newTestInventoryServer(t)
    255 				expected := []ownedItem{{ID: identity, Type: 51018, Template: scenario.template, Quality: scenario.quality, Slot: move[1], Count: 1}}
    256 				if !slices.Equal(player.inventory.Items, expected) || !slices.Equal(cold.characters[1].inventory.Items, expected) ||
    257 					len(cold.characters[1000000].corpse.items) != 0 || cold.characters[1].inventory.Credits != 1 || cold.nextItem != identity {
    258 					t.Fatal("pickup, repeated wear or cold recovery lost or duplicated the weapon")
    259 				}
    260 			}
    261 
    262 			// Sell that same instance after cancellation and a cold reconnect.
    263 			player.appearance = 0x545
    264 			player.position = [3]float32{60, 14.75, 53}
    265 			if err := world.saveUser(player); err != nil {
    266 				t.Fatal(err)
    267 			}
    268 			if err := world.savePosition(player); err != nil {
    269 				t.Fatal(err)
    270 			}
    271 			if err := world.handleShopUse(client, shopUseRequest(1)); err != nil {
    272 				t.Fatal(err)
    273 			}
    274 			add := shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:]
    275 			if err := world.handleTrade(client, add); err != nil {
    276 				t.Fatal(err)
    277 			}
    278 			if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 2, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    279 				t.Fatal(err)
    280 			}
    281 			cold := newTestInventoryServer(t)
    282 			if len(cold.characters[1].inventory.Items) != 1 || cold.characters[1].inventory.Credits != 1 {
    283 				t.Fatal("cancelled sale lost the selected-quality weapon")
    284 			}
    285 			world, player = cold, cold.characters[1]
    286 			client = &worldClient{id: 1, conn: &combatTestConn{}, target: true, ready: true}
    287 			world.clients[1] = client
    288 			if err := world.handleShopUse(client, shopUseRequest(1)); err != nil {
    289 				t.Fatal(err)
    290 			}
    291 			if err := world.handleTrade(client, add); err != nil {
    292 				t.Fatal(err)
    293 			}
    294 			for range 2 {
    295 				if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    296 					t.Fatal(err)
    297 				}
    298 			}
    299 			cold = newTestInventoryServer(t)
    300 
    301 			for _, current := range []*server{world, cold} {
    302 				if len(current.characters[1].inventory.Items) != 0 || current.characters[1].inventory.Credits != scenario.credits ||
    303 					current.characters[1].experience != 100 || current.nextItem != identity {
    304 					t.Fatal("selected-quality sale lost or repeated value, rewards or an instance", current.characters[1].inventory)
    305 				}
    306 				if _, exists := current.items[identity]; exists {
    307 					t.Fatal("sold weapon returned in the shared cache")
    308 				}
    309 			}
    310 		})
    311 	}
    312 }