aorecomp

Anarchy Online classic in the browser

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


server/aggdef_equipment_test.go (13662B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	_ "embed"
      7 	"encoding/binary"
      8 	"encoding/json"
      9 	"fmt"
     10 	"reflect"
     11 	"slices"
     12 	"testing"
     13 	"time"
     14 )
     15 
     16 //go:embed testdata/aggdef-weapons.json
     17 var aggDefWeaponData []byte
     18 
     19 func TestAggDefEquipmentOriginalWeaponCatalogAndTimings(t *testing.T) {
     20 	var fixtures []struct {
     21 		Definition gamedata.Item
     22 		Modifiers  map[uint32]int32 `json:"onwear_modifiers"`
     23 		Initiative int32            `json:"effective_initiative"`
     24 		Expected   []struct {
     25 			Base     int32 `json:"base51"`
     26 			Bonus    int32 `json:"gear51"`
     27 			Attack   int   `json:"attack_cs"`
     28 			Recharge int   `json:"recharge_cs"`
     29 		} `json:"expectations"`
     30 	}
     31 	if err := json.Unmarshal(aggDefWeaponData, &fixtures); err != nil {
     32 		t.Fatal(err)
     33 	}
     34 	var actual, expected []uint32
     35 	for template, definition := range gamedata.ItemTemplates {
     36 		if definition.HeaderWords[0] != 51018 {
     37 			continue
     38 		}
     39 		for _, section := range definition.Sections {
     40 			if section.Type != 2 || section.Subtype != 14 {
     41 				continue
     42 			}
     43 			for _, function := range *section.Functions {
     44 				if (function.ID == 53012 || function.ID == 53045) && *function.Arguments[0].Integer == 51 {
     45 					actual = append(actual, template)
     46 				}
     47 			}
     48 		}
     49 	}
     50 	for _, fixture := range fixtures {
     51 		expected = append(expected, fixture.Definition.ID)
     52 	}
     53 	slices.Sort(actual)
     54 	slices.Sort(expected)
     55 	if len(fixtures) != 16 || !slices.Equal(actual, expected) {
     56 		t.Fatal("AggDef weapon identities differ from the original-record census", actual, expected)
     57 	}
     58 
     59 	for _, fixture := range fixtures {
     60 		for _, scenario := range fixture.Expected {
     61 			t.Run(fmt.Sprintf("%d/%d", fixture.Definition.ID, scenario.Base), func(t *testing.T) {
     62 				world, _ := newAggDefEquipmentWorld(t, fixture.Definition.ID)
     63 				player, client := world.characters[1], world.clients[1]
     64 				definition := gamedata.ItemTemplates[fixture.Definition.ID]
     65 				if definition.RecordType != fixture.Definition.RecordType || definition.HeaderWords != fixture.Definition.HeaderWords ||
     66 					definition.PayloadSHA256 != fixture.Definition.PayloadSHA256 || *definition.Quality != *fixture.Definition.Quality ||
     67 					!reflect.DeepEqual(definition.Stats, fixture.Definition.Stats) || !reflect.DeepEqual(definition.Sections, fixture.Definition.Sections) ||
     68 					!reflect.DeepEqual(definition.Undecoded, fixture.Definition.Undecoded) {
     69 					t.Fatal("original full weapon contract changed")
     70 				}
     71 				player.aggDef = scenario.Base
     72 				player.inventory.Items[1].Ammo = 3
     73 				if err := world.saveInventory(player, player.inventory); err != nil {
     74 					t.Fatal(err)
     75 				}
     76 				world.characters[1000000].health = 50000
     77 				if err := world.handleInventory(client, inventoryMovePacket(1, 65, 6)[20:]); err != nil || client.equipment == nil {
     78 					t.Fatal("authored weapon could not begin installation", err)
     79 				}
     80 				world.advanceEquipment(client.equipment.readyAt)
     81 				before := time.Now()
     82 				world.handleCombat(client, attackPacket(1, 1000000)[20:])
     83 				after := time.Now()
     84 				first := player.nextAttack
     85 				world.advanceCombat(first)
     86 
     87 				attack, recharge := time.Duration(scenario.Attack)*10*time.Millisecond, time.Duration(scenario.Recharge)*10*time.Millisecond
     88 				if first.Before(before.Add(attack)) || first.After(after.Add(attack)) || player.rechargeUntil != first.Add(recharge) ||
     89 					player.nextAttack != first.Add(attack+recharge) || player.inventory.Items[1].Ammo != 2 || player.aggDef != scenario.Base ||
     90 					player.equipmentBonus(51) != scenario.Bonus {
     91 					t.Fatal("public weapon clock differs from the original effective-stat formula", first.Sub(before), player.rechargeUntil.Sub(first), attack, recharge)
     92 				}
     93 				for stat, value := range fixture.Modifiers {
     94 					if player.equipmentBonus(stat) != value {
     95 						t.Fatal("weapon timing dropped or duplicated an authored companion", stat, value)
     96 					}
     97 				}
     98 				if initiative, _ := player.effectiveStat(119); initiative != fixture.Initiative {
     99 					t.Fatal("authored ability companion changed the expected initiative trickle", initiative, fixture.Initiative)
    100 				}
    101 			})
    102 		}
    103 	}
    104 }
    105 
    106 func newAggDefEquipmentWorld(t *testing.T, template uint32) (*server, *combatTestConn) {
    107 	t.Helper()
    108 	world, conn := newEquipmentWorld(t)
    109 	player := world.characters[1]
    110 	player.level, player.profession, player.nano = 200, 8, 5000
    111 	player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
    112 	player.stats.Speed = characterSpeedStats{}
    113 	player.stats.Ranged = characterRangedStats{Bow: 3000, Pistol: 3000, AssaultRifle: 3000,
    114 		SubmachineGun: 3000, Shotgun: 3000, Rifle: 3000, RangedEnergy: 3000,
    115 		FlingShot: 3000, AimedShot: 3000, Burst: 3000, FullAuto: 3000}
    116 	player.stats.MiscWeapons.HeavyWeapons = 3000
    117 	player.stats.NanoAiding.BiologicalMetamorphosis = 1000
    118 	player.stats.NanoAiding.MatterMetamorphosis = 1000
    119 	player.inventory.LearnedNanos = []uint32{27169}
    120 	if err := world.saveUser(player); err != nil {
    121 		t.Fatal(err)
    122 	}
    123 	if granted, err := world.grantItem(player, template); err != nil || !granted {
    124 		t.Fatal(granted, err)
    125 	}
    126 	return world, conn
    127 }
    128 
    129 func TestAggDefEquipmentControlsPublicAttackClockAndColdRestore(t *testing.T) {
    130 	for _, scenario := range []struct {
    131 		base             int32
    132 		attack, recharge time.Duration
    133 	}{
    134 		{-100, 2920 * time.Millisecond, 3000 * time.Millisecond},
    135 		{0, 1920 * time.Millisecond, 2000 * time.Millisecond},
    136 		{100, time.Second, time.Second},
    137 	} {
    138 		t.Run(fmt.Sprint(scenario.base), func(t *testing.T) {
    139 			world, conn := newAggDefEquipmentWorld(t, 124210)
    140 			player, client := world.characters[1], world.clients[1]
    141 			request := n3Packet(0x76793f0d, 50000, 1, binary.BigEndian.AppendUint32(nil, uint32(scenario.base)))
    142 			if err := world.handleCombat(client, request[20:]); err != nil {
    143 				t.Fatal(err)
    144 			}
    145 			if err := world.handleInventory(client, inventoryMovePacket(1, 65, 6)[20:]); err != nil || client.equipment == nil {
    146 				t.Fatal("weapon installation failed", err)
    147 			}
    148 			if player.equipmentBonus(51) != 0 {
    149 				t.Fatal("pending weapon already changed timing")
    150 			}
    151 
    152 			world.advanceEquipment(client.equipment.readyAt)
    153 			before := time.Now()
    154 			if err := world.handleCombat(client, attackPacket(1, 1000000)[20:]); err != nil {
    155 				t.Fatal(err)
    156 			}
    157 			after := time.Now()
    158 			first := player.nextAttack
    159 			cold := newTestInventoryServer(t).characters[1]
    160 			attack, recharge := cold.attackDelays()
    161 			world.handleCombat(client, stopFightPacket(1)[20:])
    162 			world.handleInventory(client, itemMovePacket(1, 6, 111)[20:])
    163 			world.advanceEquipment(client.equipment.readyAt)
    164 			removed := newTestInventoryServer(t).characters[1]
    165 
    166 			if first.Before(before.Add(scenario.attack)) || first.After(after.Add(scenario.attack)) ||
    167 				attack != scenario.attack || recharge != scenario.recharge || cold.aggDef != scenario.base ||
    168 				cold.equipmentBonus(51) != 5 || cold.equipmentBonus(1) != 50 || cold.equipmentBonus(91) != 50 {
    169 				t.Fatal("authored AggDef or companion modifiers omitted, capped or duplicated", attack, recharge, cold.aggDef)
    170 			}
    171 			if removed.aggDef != scenario.base || removed.equipmentBonus(51) != 0 || removed.equipmentBonus(1) != 0 ||
    172 				removed.equipmentBonus(91) != 0 || !reflect.DeepEqual(removed.inventory.Items, player.inventory.Items) ||
    173 				len(player.inventory.Items) != 2 || player.inventory.Items[1].ID != 2 || player.inventory.Items[1].Slot != 65 {
    174 				t.Fatal("removal lost an instance or retained wear effects")
    175 			}
    176 			if binary.BigEndian.Uint32(conn.packets[0][29:33]) != 51 || int32(binary.BigEndian.Uint32(conn.packets[0][33:37])) != scenario.base {
    177 				t.Fatal("slider replication included derived equipment modifiers")
    178 			}
    179 		})
    180 	}
    181 }
    182 
    183 func TestAggDefEquipmentDoesNotChangeBaseModeNanoQueue(t *testing.T) {
    184 	for _, scenario := range []struct {
    185 		base  int32
    186 		delay time.Duration
    187 	}{
    188 		{-100, 3200000047}, {-99, 2200000047}, {99, 2200000047}, {100, 1199999928},
    189 	} {
    190 		t.Run(fmt.Sprint(scenario.base), func(t *testing.T) {
    191 			world, _ := newAggDefEquipmentWorld(t, 124210)
    192 			player, client := world.characters[1], world.clients[1]
    193 			player.aggDef = scenario.base
    194 			world.handleInventory(client, inventoryMovePacket(1, 65, 6)[20:])
    195 			world.advanceEquipment(client.equipment.readyAt)
    196 
    197 			before := time.Now()
    198 			err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, 1, 53019, 27169})[20:])
    199 			after := time.Now()
    200 
    201 			if err != nil || client.nanoCast == nil || client.nanoCast.readyAt.Before(before.Add(scenario.delay)) ||
    202 				client.nanoCast.readyAt.After(after.Add(scenario.delay)) || player.aggDef != scenario.base || player.equipmentBonus(51) != 5 {
    203 				t.Fatal("nano queue did not use signed base AggDef / 100", err, client.nanoCast)
    204 			}
    205 		})
    206 	}
    207 }
    208 
    209 func TestAggDefEquipmentSliderChangePreservesStartedPhases(t *testing.T) {
    210 	world, _ := newAggDefEquipmentWorld(t, 124210)
    211 	player, client := world.characters[1], world.clients[1]
    212 	player.inventory.Items[1].Ammo = 3
    213 	if err := world.saveInventory(player, player.inventory); err != nil {
    214 		t.Fatal(err)
    215 	}
    216 	world.characters[1000000].health = 50000
    217 	world.handleInventory(client, inventoryMovePacket(1, 65, 6)[20:])
    218 	world.advanceEquipment(client.equipment.readyAt)
    219 	world.handleCombat(client, attackPacket(1, 1000000)[20:])
    220 	first := player.nextAttack
    221 
    222 	request := n3Packet(0x76793f0d, 50000, 1, []byte{0xff, 0xff, 0xff, 0x9c})
    223 	world.handleCombat(client, request[20:])
    224 	if player.nextAttack != first {
    225 		t.Fatal("slider change rewrote an already started attack")
    226 	}
    227 	world.advanceCombat(first.Add(-time.Nanosecond))
    228 	if player.inventory.Items[1].Ammo != 3 {
    229 		t.Fatal("weapon fired before its frozen deadline")
    230 	}
    231 	world.advanceCombat(first)
    232 	if player.rechargeUntil != first.Add(3*time.Second) || player.nextAttack != first.Add(5920*time.Millisecond) {
    233 		t.Fatal("next weapon phase omitted the current equipment AggDef")
    234 	}
    235 	world.handleCombat(client, stopFightPacket(1)[20:])
    236 	world.handleCombat(client, n3Packet(0x76793f0d, 50000, 1, []byte{0, 0, 0, 100})[20:])
    237 	world.handleCombat(client, attackPacket(1, 1000000)[20:])
    238 
    239 	if player.rechargeUntil != first.Add(3*time.Second) || player.nextAttack != first.Add(4*time.Second) ||
    240 		player.inventory.Items[1].Ammo != 2 || player.aggDef != 100 {
    241 		t.Fatal("restart spent held recharge or failed to apply the uncapped equipment sum")
    242 	}
    243 }
    244 
    245 func TestAggDefEquipmentDelayedRequirementFailurePreservesItems(t *testing.T) {
    246 	world, conn := newAggDefEquipmentWorld(t, 124210)
    247 	player, client := world.characters[1], world.clients[1]
    248 	before := slices.Clone(player.inventory.Items)
    249 	stored, err := readTestItemStore(world)
    250 	if err != nil {
    251 		t.Fatal(err)
    252 	}
    253 
    254 	world.handleInventory(client, inventoryMovePacket(1, 65, 6)[20:])
    255 	player.stats.Ranged.Rifle = 0
    256 	world.advanceEquipment(client.equipment.readyAt)
    257 	after, readErr := readTestItemStore(world)
    258 	cold := newTestInventoryServer(t).characters[1]
    259 
    260 	if readErr != nil || !bytes.Equal(stored, after) || !reflect.DeepEqual(player.inventory.Items, before) ||
    261 		!reflect.DeepEqual(cold.inventory.Items, before) || player.equipmentBonus(51) != 0 || cold.equipmentBonus(51) != 0 || len(conn.packets) != 2 {
    262 		t.Fatal("failed delayed qualification published bonuses or lost the weapon", readErr)
    263 	}
    264 }
    265 
    266 func TestAggDefEquipmentSaveFailureAndRetryPublishWholeContract(t *testing.T) {
    267 	for _, removal := range []bool{false, true} {
    268 		t.Run(fmt.Sprint(removal), func(t *testing.T) {
    269 			world, _ := newAggDefEquipmentWorld(t, 124210)
    270 			player, client := world.characters[1], world.clients[1]
    271 			request := inventoryMovePacket(1, 65, 6)
    272 			bonus := int32(0)
    273 			if removal {
    274 				world.handleInventory(client, request[20:])
    275 				world.advanceEquipment(client.equipment.readyAt)
    276 				request, bonus = itemMovePacket(1, 6, 111), 5
    277 			}
    278 			before := slices.Clone(player.inventory.Items)
    279 			stored, err := readTestItemStore(world)
    280 			if err != nil {
    281 				t.Fatal(err)
    282 			}
    283 			if _, err := world.db.Exec("CREATE TRIGGER fail_aggdef BEFORE INSERT ON item_locations BEGIN SELECT RAISE(ABORT, 'test move'); END"); err != nil {
    284 				t.Fatal(err)
    285 			}
    286 
    287 			world.handleInventory(client, request[20:])
    288 			world.advanceEquipment(client.equipment.readyAt)
    289 			after, readErr := readTestItemStore(world)
    290 			attack, recharge := player.attackDelays()
    291 
    292 			if readErr != nil || !bytes.Equal(stored, after) || !reflect.DeepEqual(player.inventory.Items, before) ||
    293 				player.equipmentBonus(51) != bonus || player.equipmentBonus(1) != bonus*10 || player.equipmentBonus(91) != bonus*10 {
    294 				t.Fatal("failed move published a partial contract", readErr)
    295 			}
    296 			if removal && (attack != 1920*time.Millisecond || recharge != 2*time.Second) {
    297 				t.Fatal("failed removal changed effective attack timing", attack, recharge)
    298 			}
    299 			if _, err := world.db.Exec("DROP TRIGGER fail_aggdef"); err != nil {
    300 				t.Fatal(err)
    301 			}
    302 			world.handleInventory(client, request[20:])
    303 			world.advanceEquipment(client.equipment.readyAt)
    304 			cold := newTestInventoryServer(t).characters[1]
    305 			if player.equipmentBonus(51) != 5-bonus || cold.equipmentBonus(51) != 5-bonus ||
    306 				cold.equipmentBonus(1) != (5-bonus)*10 || cold.equipmentBonus(91) != (5-bonus)*10 ||
    307 				len(cold.inventory.Items) != 2 || cold.inventory.Items[1].ID != 2 {
    308 				t.Fatal("retry did not commit exactly one whole contract")
    309 			}
    310 		})
    311 	}
    312 }
    313 
    314 func TestAggDefEquipmentUnsupportedBraceletsRemainRejected(t *testing.T) {
    315 	for _, template := range []uint32{84140, 84188} {
    316 		definition := gamedata.ItemTemplates[template]
    317 		item := ownedItem{ID: 1, Type: 51022, Template: template, Quality: *definition.Quality, Count: 1, Slot: 26}
    318 
    319 		admitted, bonus := item.isStatEquipment(), item.equipmentBonus(51, 0)
    320 
    321 		if admitted || bonus != 0 {
    322 			t.Fatal("AggDef support admitted a bracelet with unsupported53068 effects", template)
    323 		}
    324 	}
    325 }