aorecomp

Anarchy Online classic in the browser

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


server/bau_cyber_armor_test.go (11037B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"fmt"
      8 	"slices"
      9 	"testing"
     10 )
     11 
     12 func TestBauCyberGeneratedPickupKeepsSelectedQuality(t *testing.T) {
     13 	world, output := newKillExperienceWorld(t, "unarmed")
     14 	player, client := world.characters[1], world.clients[1]
     15 	player.stats.Abilities.Intelligence, player.stats.Abilities.Psychic = 261, 236
     16 	if err := world.saveUser(player); err != nil {
     17 		t.Fatal(err)
     18 	}
     19 	// Controlled membership tests delivery, not historical BauCyber creature drops.
     20 	profile := *world.characters[1000000].combatProfile
     21 	profile.Loot = []gamedata.NPCLoot{{Template: 85610, Quality: [2]uint32{100, 100}, Chance: 100}}
     22 	world.characters[1000000].combatProfile = &profile
     23 	world.combatRoll = func(uint32) uint32 { return 0 }
     24 	identity := world.nextItem + 1
     25 	var wire []byte
     26 	for _, value := range []uint32{identity, 85611, 85610, 100, 0} {
     27 		wire = binary.BigEndian.AppendUint32(wire, value)
     28 	}
     29 
     30 	attackForExperience(t, world, "unarmed")
     31 	if err := world.handleLootUse(client, lootUseRequest(1)); err != nil {
     32 		t.Fatal(err)
     33 	}
     34 	shown := slices.ContainsFunc(output.packets, func(packet []byte) bool { return bytes.Contains(packet, wire) })
     35 	for range 2 {
     36 		if err := world.handleInventory(client, lootTakeRequest()); err != nil {
     37 			t.Fatal(err)
     38 		}
     39 	}
     40 	world = newTestInventoryServer(t)
     41 	client = &worldClient{id: 1, conn: output, target: true, ready: true}
     42 	world.clients[1] = client
     43 	if err := world.handleInventory(client, inventoryMovePacket(1, 64, 24)[20:]); err != nil || client.equipment == nil {
     44 		t.Fatal("picked-up gloves did not start wear", err)
     45 	}
     46 	world.advanceEquipment(client.equipment.readyAt)
     47 	worn := newTestInventoryServer(t)
     48 	if err := world.handleInventory(client, itemMovePacket(1, 24, 64)[20:]); err != nil || client.equipment == nil {
     49 		t.Fatal("picked-up gloves did not start removal", err)
     50 	}
     51 	world.advanceEquipment(client.equipment.readyAt)
     52 	removed := newTestInventoryServer(t)
     53 
     54 	expected := ownedItem{ID: identity, Type: 51022, Template: 85610, Quality: 100, Slot: 24, Count: 1}
     55 	if !shown || !slices.Equal(worn.characters[1].inventory.Items, []ownedItem{expected}) || worn.characters[1].equipmentBonus(91) != 125 {
     56 		t.Fatal("generated pickup lost its native endpoints, identity, selected quality or worn AC")
     57 	}
     58 	expected.Slot = 64
     59 	if !slices.Equal(removed.characters[1].inventory.Items, []ownedItem{expected}) || removed.characters[1].equipmentBonus(91) != 0 ||
     60 		len(removed.characters[1000000].corpse.items) != 0 || removed.nextItem != identity {
     61 		t.Fatal("duplicate pickup, removal or cold recovery lost or duplicated the gloves")
     62 	}
     63 }
     64 
     65 func TestBauCyberArmorQualitiesWearRemoveAndRecover(t *testing.T) {
     66 	for _, scenario := range []struct {
     67 		low, high, slot, quality, value uint32
     68 		intelligence, psychic           uint16
     69 		armor                           [8]int32
     70 	}{
     71 		{85530, 85529, 27, 1, 921, 8, 8, [8]int32{3, 3, 3, 1, 3, 3, 1, 3}},
     72 		{85530, 85529, 27, 2, 943, 11, 10, [8]int32{6, 6, 6, 2, 5, 5, 2, 5}},
     73 		{85530, 85529, 27, 20, 8910, 56, 52, [8]int32{62, 62, 55, 13, 40, 47, 13, 47}},
     74 		{85530, 85529, 27, 100, 217836, 260, 235, [8]int32{312, 312, 273, 63, 196, 234, 63, 234}},
     75 		{85530, 85529, 27, 200, 877369, 515, 465, [8]int32{625, 625, 546, 125, 390, 468, 125, 468}},
     76 		{85571, 85570, 18, 1, 1106, 8, 8, [8]int32{3, 3, 3, 1, 3, 3, 1, 3}},
     77 		{85571, 85570, 18, 2, 1132, 11, 10, [8]int32{7, 7, 6, 2, 5, 6, 2, 6}},
     78 		{85571, 85570, 18, 20, 10693, 56, 52, [8]int32{74, 74, 65, 15, 47, 56, 15, 56}},
     79 		{85571, 85570, 18, 100, 261404, 260, 235, [8]int32{375, 375, 328, 75, 234, 281, 75, 281}},
     80 		{85571, 85570, 18, 200, 1052843, 515, 465, [8]int32{750, 750, 656, 150, 468, 562, 150, 562}},
     81 		{85611, 85610, 24, 1, 368, 8, 8, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}},
     82 		{85611, 85610, 24, 2, 376, 11, 10, [8]int32{2, 2, 2, 1, 2, 2, 1, 2}},
     83 		{85611, 85610, 24, 5, 509, 18, 17, [8]int32{6, 6, 5, 2, 4, 5, 2, 5}},
     84 		{85611, 85610, 24, 20, 3563, 56, 52, [8]int32{25, 25, 22, 6, 16, 19, 6, 19}},
     85 		{85611, 85610, 24, 100, 87134, 260, 235, [8]int32{125, 125, 109, 25, 78, 94, 25, 94}},
     86 		{85611, 85610, 24, 101, 88895, 263, 238, [8]int32{126, 126, 110, 26, 79, 94, 26, 94}},
     87 		{85611, 85610, 24, 199, 347431, 512, 463, [8]int32{249, 249, 217, 50, 155, 186, 50, 186}},
     88 		{85611, 85610, 24, 200, 350947, 515, 465, [8]int32{250, 250, 218, 50, 156, 187, 50, 187}},
     89 		{85654, 85653, 30, 1, 553, 8, 8, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}},
     90 		{85654, 85653, 30, 2, 566, 11, 10, [8]int32{3, 3, 3, 1, 2, 2, 1, 2}},
     91 		{85654, 85653, 30, 20, 5346, 56, 52, [8]int32{37, 37, 32, 8, 23, 28, 8, 28}},
     92 		{85654, 85653, 30, 100, 130702, 260, 235, [8]int32{187, 187, 164, 38, 117, 140, 38, 140}},
     93 		{85654, 85653, 30, 200, 526421, 515, 465, [8]int32{375, 375, 328, 75, 234, 281, 75, 281}},
     94 		{85708, 85707, 21, 1, 1475, 8, 8, [8]int32{5, 5, 5, 1, 5, 5, 1, 5}},
     95 		{85708, 85707, 21, 2, 1510, 11, 10, [8]int32{10, 10, 9, 2, 8, 9, 2, 9}},
     96 		{85708, 85707, 21, 20, 14258, 56, 52, [8]int32{100, 100, 88, 20, 64, 76, 20, 76}},
     97 		{85708, 85707, 21, 100, 348539, 260, 235, [8]int32{500, 500, 438, 100, 313, 376, 100, 376}},
     98 		{85708, 85707, 21, 200, 1403790, 515, 465, [8]int32{1000, 1000, 875, 200, 625, 750, 200, 750}},
     99 		{85710, 85709, 21, 1, 1475, 8, 8, [8]int32{5, 5, 5, 1, 5, 5, 1, 5}},
    100 		{85710, 85709, 21, 2, 1510, 11, 10, [8]int32{10, 10, 9, 2, 8, 9, 2, 9}},
    101 		{85710, 85709, 21, 20, 14258, 56, 52, [8]int32{100, 100, 88, 20, 64, 76, 20, 76}},
    102 		{85710, 85709, 21, 100, 348539, 260, 235, [8]int32{500, 500, 438, 100, 313, 376, 100, 376}},
    103 		{85710, 85709, 21, 200, 1403790, 515, 465, [8]int32{1000, 1000, 875, 200, 625, 750, 200, 750}},
    104 		{85759, 85758, 25, 1, 368, 8, 8, [8]int32{1, 1, 1, 1, 1, 1, 1, 1}},
    105 		{85759, 85758, 25, 2, 376, 11, 10, [8]int32{2, 2, 2, 1, 2, 2, 1, 2}},
    106 		{85759, 85758, 23, 2, 376, 11, 10, [8]int32{2, 2, 2, 1, 2, 2, 1, 2}},
    107 		{85759, 85758, 25, 20, 3563, 56, 52, [8]int32{25, 25, 22, 6, 16, 19, 6, 19}},
    108 		{85759, 85758, 25, 100, 87134, 260, 235, [8]int32{125, 125, 109, 25, 78, 94, 25, 94}},
    109 		{85759, 85758, 25, 200, 350947, 515, 465, [8]int32{250, 250, 218, 50, 156, 187, 50, 187}},
    110 	} {
    111 		for _, anchor := range []uint32{scenario.low, scenario.high} {
    112 			t.Run(fmt.Sprintf("%d/QL%d/slot%d", anchor, scenario.quality, scenario.slot), func(t *testing.T) {
    113 				world, output := newEquipmentWorld(t)
    114 				player, client := world.characters[1], world.clients[1]
    115 				player.stats.Abilities.Intelligence, player.stats.Abilities.Psychic = scenario.intelligence+1, scenario.psychic+1
    116 				if err := world.saveUser(player); err != nil {
    117 					t.Fatal(err)
    118 				}
    119 				if granted, err := world.grantItem(player, anchor); err != nil || !granted {
    120 					t.Fatal("grant armor", granted, err)
    121 				}
    122 				player.inventory.Items[1].Quality = scenario.quality
    123 				if err := world.saveInventory(player, player.inventory); err != nil {
    124 					t.Fatal(err)
    125 				}
    126 				weapon := player.inventory.Items[0]
    127 
    128 				for _, ability := range []*uint16{&player.stats.Abilities.Intelligence, &player.stats.Abilities.Psychic} {
    129 					*ability -= 1
    130 					if err := world.handleInventory(client, inventoryMovePacket(1, 65, scenario.slot)[20:]); err != nil || client.equipment != nil || player.inventory.Items[1].Slot != 65 {
    131 						t.Fatal("threshold equality equipped or lost the armor", err)
    132 					}
    133 					*ability += 1
    134 				}
    135 				output.packets = nil
    136 				if err := world.handleInventory(client, inventoryMovePacket(1, 65, scenario.slot)[20:]); err != nil || client.equipment == nil {
    137 					t.Fatal("qualified armor did not start wear", err)
    138 				}
    139 				world.advanceEquipment(client.equipment.readyAt)
    140 				worn := newTestInventoryServer(t)
    141 				low, high := scenario.low, scenario.high
    142 				if scenario.quality == 1 {
    143 					high = low
    144 				} else if scenario.quality == 200 {
    145 					low = high
    146 				}
    147 				var action []byte
    148 				for _, word := range []uint32{low, high, scenario.quality, 1, 6, 102, scenario.slot, 0, 0} {
    149 					action = binary.BigEndian.AppendUint32(action, word)
    150 				}
    151 				if len(output.packets) != 2 || !bytes.Equal(output.packets[1][29:65], action) {
    152 					t.Fatal("native wear lost the selected quality or endpoint pair")
    153 				}
    154 				if err := world.handleInventory(client, itemMovePacket(1, scenario.slot, 111)[20:]); err != nil || client.equipment == nil {
    155 					t.Fatal("armor did not start removal", err)
    156 				}
    157 				world.advanceEquipment(client.equipment.readyAt)
    158 				removed := newTestInventoryServer(t)
    159 
    160 				expected := ownedItem{ID: 2, Type: 51022, Template: anchor, Quality: scenario.quality, Slot: scenario.slot, Count: 1}
    161 				if !slices.Equal(worn.characters[1].inventory.Items, []ownedItem{weapon, expected}) || worn.characters[1].inventory.Items[1].shopPrice() != scenario.value {
    162 					t.Fatal("cold armor identity, quality, value or unrelated item changed")
    163 				}
    164 				expected.Slot = 65
    165 				if !slices.Equal(removed.characters[1].inventory.Items, []ownedItem{weapon, expected}) || removed.nextItem != 2 {
    166 					t.Fatal("removal lost or replaced an instance")
    167 				}
    168 				for index, bonus := range scenario.armor {
    169 					if worn.characters[1].equipmentBonus(90+uint32(index)) != bonus || removed.characters[1].equipmentBonus(90+uint32(index)) != 0 {
    170 						t.Fatal("selected armor effects or their removal changed", index)
    171 					}
    172 				}
    173 			})
    174 		}
    175 	}
    176 }
    177 
    178 func TestBauCyberSaleCancellationAndReplayKeepSelectedQuality(t *testing.T) {
    179 	for _, scenario := range []struct{ quality, sale uint32 }{
    180 		{2, 18}, {100, 4356}, {101, 4444}, {200, 17547},
    181 	} {
    182 		t.Run(fmt.Sprint(scenario.quality), func(t *testing.T) {
    183 			world, _ := newShopWorld(t)
    184 			player, client := world.characters[1], world.clients[1]
    185 			if granted, err := world.grantItem(player, 85611); err != nil || !granted {
    186 				t.Fatal(granted, err)
    187 			}
    188 			player.inventory.Items[1].Quality = scenario.quality
    189 			if err := world.saveInventory(player, player.inventory); err != nil {
    190 				t.Fatal(err)
    191 			}
    192 			before := append([]ownedItem(nil), player.inventory.Items...)
    193 			offer := shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 65})[20:]
    194 
    195 			if err := world.handleTrade(client, offer); err != nil {
    196 				t.Fatal(err)
    197 			}
    198 			if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 2, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    199 				t.Fatal(err)
    200 			}
    201 			cancelled := newTestInventoryServer(t).characters[1].inventory
    202 			if err := world.handleShopUse(client, shopUseRequest(2)); err != nil {
    203 				t.Fatal(err)
    204 			}
    205 			if err := world.handleTrade(client, offer); err != nil {
    206 				t.Fatal(err)
    207 			}
    208 			for range 2 {
    209 				if err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]); err != nil {
    210 					t.Fatal(err)
    211 				}
    212 			}
    213 			sold := newTestInventoryServer(t).characters[1].inventory
    214 
    215 			if !slices.Equal(cancelled.Items, before) || cancelled.Credits != 100 {
    216 				t.Fatal("cancelled sale changed the selected instance or wallet", cancelled)
    217 			}
    218 			if !slices.Equal(sold.Items, before[:1]) || sold.Credits != 100+scenario.sale {
    219 				t.Fatal("accepted or repeated sale changed another item or its selected-quality price", sold)
    220 			}
    221 		})
    222 	}
    223 }