aorecomp

Anarchy Online classic in the browser

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


server/armor_overequipping_test.go (14253B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestArmorOverequippingDustResourcesDamageAndRestart(t *testing.T) {
     12 	for _, scenario := range []struct {
     13 		computerLiteracy uint16
     14 		health, damage   uint32
     15 	}{
     16 		{850, 121, 80}, {650, 99, 110}, {450, 76, 140}, {250, 54, 170}, {100, 31, 200},
     17 	} {
     18 		t.Run(fmt.Sprint(scenario.computerLiteracy), func(t *testing.T) {
     19 			world, _ := newEquipmentWorld(t)
     20 			player, client := world.characters[1], world.clients[1]
     21 			player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
     22 			player.stats.Body.Adventuring, player.stats.TradeRepair.ComputerLiteracy = 1000, 850
     23 			if err := world.saveUser(player); err != nil {
     24 				t.Fatal(err)
     25 			}
     26 			if granted, err := world.grantItem(player, 200461); err != nil || !granted {
     27 				t.Fatal("Dust Collector grant failed", err)
     28 			}
     29 			if err := world.handleInventory(client, inventoryMovePacket(1, 65, 19)[20:]); err != nil || client.equipment == nil {
     30 				t.Fatal("qualified wear failed", err)
     31 			}
     32 			world.advanceEquipment(client.equipment.readyAt)
     33 			npc := world.characters[1000000]
     34 			npc.combatProfile = &gamedata.NPCCombatProfile{Weapon: [2]uint32{120911, 120911}}
     35 			profile := player.userData()
     36 			profile.Stats.TradeRepair.ComputerLiteracy = scenario.computerLiteracy
     37 
     38 			err := world.updateUser(profile)
     39 			damage := npc.weaponDamage(player, false, func(uint32) uint32 { return 145 })
     40 			restarted := newTestInventoryServer(t).characters[1]
     41 
     42 			if err != nil || player.maximumHealth() != scenario.health || restarted.maximumHealth() != scenario.health || damage != scenario.damage {
     43 				t.Fatalf("OE resources/damage: err=%v HP=%d/%d damage=%d, want HP%d damage%d", err, player.maximumHealth(), restarted.maximumHealth(), damage, scenario.health, scenario.damage)
     44 			}
     45 			if player.maximumNano() != 119 || restarted.maximumNano() != 119 || player.effectiveSkill(136, 5, [6]uint32{0, 0, 0, 0, 100, 0}) != 20 {
     46 				t.Fatal("OE reduced an unaffected nano or skill bonus")
     47 			}
     48 			if len(restarted.inventory.Items) != 2 || restarted.inventory.Items[1].ID != 2 || restarted.inventory.Items[1].Slot != 19 {
     49 				t.Fatalf("OE changed ownership or equipment: %+v", restarted.inventory.Items)
     50 			}
     51 		})
     52 	}
     53 }
     54 
     55 func TestArmorOverequippingMetaplastTiersAndPerItemRounding(t *testing.T) {
     56 	for _, scenario := range []struct {
     57 		ability          uint16
     58 		bodyAC, sleeveAC int32
     59 	}{{7, 5, 1}, {5, 4, 1}, {4, 3, 1}, {2, 2, 1}, {1, 0, 0}} {
     60 		for _, stat := range []string{"strength", "agility"} {
     61 			t.Run(fmt.Sprintf("%s/%d", stat, scenario.ability), func(t *testing.T) {
     62 				world, _ := newEquipmentWorld(t)
     63 				player, client := world.characters[1], world.clients[1]
     64 				for _, template := range []uint32{85686, 85742, 85742} {
     65 					if granted, err := world.grantItem(player, template); err != nil || !granted {
     66 						t.Fatal("armor grant failed", err)
     67 					}
     68 				}
     69 				for index, slot := range []uint32{21, 23, 25} {
     70 					if err := world.handleInventory(client, inventoryMovePacket(1, uint32(65+index), slot)[20:]); err != nil || client.equipment == nil {
     71 						t.Fatal("armor wear failed", err)
     72 					}
     73 					world.advanceEquipment(client.equipment.readyAt)
     74 				}
     75 				profile := player.userData()
     76 				if stat == "strength" {
     77 					profile.Stats.Abilities.Strength = scenario.ability
     78 				} else {
     79 					profile.Stats.Abilities.Agility = scenario.ability
     80 				}
     81 
     82 				err := world.updateUser(profile)
     83 				armor := player.equipmentBonus(90)
     84 				profile.Stats.Abilities.Strength, profile.Stats.Abilities.Agility = 8, 8
     85 				recovery := world.updateUser(profile)
     86 				restarted := newTestInventoryServer(t).characters[1]
     87 
     88 				if err != nil || recovery != nil || armor != scenario.bodyAC+2*scenario.sleeveAC || player.equipmentBonus(90) != 7 || restarted.equipmentBonus(90) != 7 {
     89 					t.Fatalf("per-item armor rounding/recovery: %v %v AC=%d recovered=%d", err, recovery, armor, restarted.equipmentBonus(90))
     90 				}
     91 				if len(restarted.inventory.Items) != 4 || restarted.inventory.Items[1].Slot != 21 || restarted.inventory.Items[2].Slot != 23 || restarted.inventory.Items[3].Slot != 25 {
     92 					t.Fatal("OE removed or exchanged an equipped instance", restarted.inventory.Items)
     93 				}
     94 			})
     95 		}
     96 	}
     97 }
     98 
     99 func TestArmorOverequippingUsesInterpolatedRequirementsAtEachBoundary(t *testing.T) {
    100 	for _, scenario := range []struct {
    101 		quality  uint32
    102 		strength uint16
    103 		armor    int32
    104 	}{
    105 		{100, 186, 312}, {100, 185, 234}, {100, 140, 234}, {100, 139, 156},
    106 		{100, 93, 156}, {100, 92, 78}, {100, 47, 78}, {100, 46, 0},
    107 		{200, 369, 625}, {200, 368, 469}, {200, 367, 469},
    108 		{200, 277, 469}, {200, 276, 313}, {200, 275, 313},
    109 	} {
    110 		t.Run(fmt.Sprintf("QL%d/%d", scenario.quality, scenario.strength), func(t *testing.T) {
    111 			world, _ := newEquipmentWorld(t)
    112 			player, client := world.characters[1], world.clients[1]
    113 			player.stats.Abilities.Strength, player.stats.Abilities.Agility = 461, 511
    114 			if granted, err := world.grantItem(player, 85512); err != nil || !granted {
    115 				t.Fatal("pants grant failed", err)
    116 			}
    117 			player.inventory.Items[1].Quality = scenario.quality
    118 			if err := world.handleInventory(client, inventoryMovePacket(1, 65, 27)[20:]); err != nil || client.equipment == nil {
    119 				t.Fatal("pants wear failed", err)
    120 			}
    121 			world.advanceEquipment(client.equipment.readyAt)
    122 			profile := player.userData()
    123 			profile.Stats.Abilities.Strength = scenario.strength
    124 
    125 			err := world.updateUser(profile)
    126 
    127 			if err != nil || player.equipmentBonus(90) != scenario.armor {
    128 				t.Fatalf("interpolated OE boundary: %v AC=%d want %d", err, player.equipmentBonus(90), scenario.armor)
    129 			}
    130 		})
    131 	}
    132 }
    133 
    134 func TestArmorOverequippingLevelRequirementAlsoReducesAddedDamage(t *testing.T) {
    135 	for _, scenario := range []struct {
    136 		level  uint16
    137 		bonus  int32
    138 		damage uint32
    139 	}{{119, 400, 63}, {96, 400, 63}, {95, 300, 48}, {71, 200, 33}, {47, 100, 18}, {23, 0, 3}} {
    140 		t.Run(fmt.Sprint(scenario.level), func(t *testing.T) {
    141 			world, _ := newRangedWorld(t)
    142 			player, client := world.characters[1], world.clients[1]
    143 			player.level, player.stats.Body.Adventuring = 120, 1000
    144 			if granted, err := world.grantItem(player, 158790); err != nil || !granted {
    145 				t.Fatal("Robust grant failed", err)
    146 			}
    147 			if err := world.handleInventory(client, inventoryMovePacket(1, 65, 19)[20:]); err != nil || client.equipment == nil {
    148 				t.Fatal("Robust wear failed", err)
    149 			}
    150 			world.advanceEquipment(client.equipment.readyAt)
    151 			profile := player.userData()
    152 			profile.Level = scenario.level
    153 
    154 			err := world.updateUser(profile)
    155 			damage := player.weaponDamage(world.characters[1000000], false, func(uint32) uint32 { return 0 })
    156 
    157 			if err != nil || player.equipmentBonus(1) != scenario.bonus || damage != scenario.damage {
    158 				t.Fatalf("level OE did not affect HP/damage together: %v HPbonus=%d damage=%d", err, player.equipmentBonus(1), damage)
    159 			}
    160 		})
    161 	}
    162 }
    163 
    164 func TestArmorOverequippingRestorationKeepsOwnerAndObserverFoundations(t *testing.T) {
    165 	for _, left := range []bool{false, true} {
    166 		t.Run(fmt.Sprint(left), func(t *testing.T) {
    167 			world, connections := newResourceBackpackTeam(t)
    168 			player := world.characters[1]
    169 			player.level = 120
    170 			player.stats.Abilities = characterAbilitiesStats{10, 10, 10, 355, 10, 10}
    171 			player.stats.Body.Adventuring, player.stats.TradeRepair.ComputerLiteracy = 1000, 591
    172 			if err := world.saveUser(player); err != nil {
    173 				t.Fatal(err)
    174 			}
    175 			start := time.Unix(100, 0)
    176 			for _, client := range world.clients {
    177 				client.regenerationAt = start
    178 			}
    179 			world.advanceRegeneration(start.Add(2 * time.Second))
    180 			if player.maximumHealth() != 1179 {
    181 				t.Fatal("Dust Collector was not initially at tier 1", player.maximumHealth())
    182 			}
    183 			if left {
    184 				if err := world.handleTeamAction(world.clients[1], teamRequest(1, 24, 0, 0)); err != nil {
    185 					t.Fatal(err)
    186 				}
    187 			}
    188 			for _, conn := range connections {
    189 				conn.packets = nil
    190 			}
    191 			stats := player.baseStats()
    192 			stats.Abilities.Intelligence = 356
    193 
    194 			first := world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:])
    195 			before := player.maximumHealth()
    196 			stats.Abilities.Intelligence = 357
    197 			err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(stats)[20:])
    198 
    199 			if first != nil || err != nil || before != 1179 || player.maximumHealth() != 1201 || player.stats.ImprovementPoints != 78 {
    200 				t.Fatal("training did not cross the native trickle boundary", first, err, before, player.maximumHealth(), player.stats.ImprovementPoints)
    201 			}
    202 			for index, want := range []uint32{1111, 1201, 1111} {
    203 				var found []uint32
    204 				for _, packet := range connections[index].packets {
    205 					if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[24:28]) == 1 && binary.BigEndian.Uint32(packet[29:33]) == 1 {
    206 						found = append(found, binary.BigEndian.Uint32(packet[33:37]))
    207 					}
    208 				}
    209 				if len(found) != 1 || found[0] != want {
    210 					t.Fatalf("observer %d maximum foundation=%v want%d", index+1, found, want)
    211 				}
    212 			}
    213 		})
    214 	}
    215 }
    216 
    217 func TestArmorOverequippingTitleRequirementIgnoresProfessionEquality(t *testing.T) {
    218 	for _, scenario := range []struct {
    219 		level uint16
    220 		armor int32
    221 	}{{100, 750}, {49, 563}, {1, 188}} {
    222 		t.Run(fmt.Sprint(scenario.level), func(t *testing.T) {
    223 			world, _ := newEquipmentWorld(t)
    224 			player, client := world.characters[1], world.clients[1]
    225 			player.level, player.profession, player.stats.Body.Adventuring = 100, 6, 1000
    226 			if granted, err := world.grantItem(player, 206731); err != nil || !granted {
    227 				t.Fatal("Survival grant failed", err)
    228 			}
    229 			if err := world.handleInventory(client, inventoryMovePacket(1, 65, 19)[20:]); err != nil || client.equipment == nil {
    230 				t.Fatal("Survival wear failed", err)
    231 			}
    232 			world.advanceEquipment(client.equipment.readyAt)
    233 			profile := player.userData()
    234 			profile.Level, profile.Profession = scenario.level, 1
    235 
    236 			err := world.updateUser(profile)
    237 
    238 			if err != nil || player.equipmentBonus(90) != scenario.armor || player.equipmentBonus(136) != 33 {
    239 				t.Fatalf("title/profession OE selection: %v AC=%d Perception=%d", err, player.equipmentBonus(90), player.equipmentBonus(136))
    240 			}
    241 		})
    242 	}
    243 }
    244 
    245 func TestArmorOverequippingDoesNotUseThePreUniverseWieldRequirements(t *testing.T) {
    246 	world, _ := newEquipmentWorld(t)
    247 	player, client := world.characters[1], world.clients[1]
    248 	player.level = 150
    249 	player.stats.Body.Adventuring, player.stats.TradeRepair.ComputerLiteracy, player.stats.NanoAiding.TimeAndSpace = 1000, 1000, 1000
    250 	if granted, err := world.grantItem(player, 168658); err != nil || !granted {
    251 		t.Fatal("Pre-Universe grant failed", err)
    252 	}
    253 	if err := world.handleInventory(client, inventoryMovePacket(1, 65, 20)[20:]); err != nil || client.equipment == nil {
    254 		t.Fatal("Pre-Universe wear failed", err)
    255 	}
    256 	world.advanceEquipment(client.equipment.readyAt)
    257 	profile := player.userData()
    258 	profile.Level = 1
    259 	profile.Stats.Body.Adventuring, profile.Stats.TradeRepair.ComputerLiteracy, profile.Stats.NanoAiding.TimeAndSpace = 0, 0, 0
    260 
    261 	err := world.updateUser(profile)
    262 	restarted := newTestInventoryServer(t).characters[1]
    263 
    264 	if err != nil || player.equipmentBonus(92) != 30 || restarted.equipmentBonus(92) != 30 || player.maximumNCU() != 13 {
    265 		t.Fatal("action-8-only requirements incorrectly reduced clothing bonuses", err, player.equipmentBonus(92))
    266 	}
    267 }
    268 
    269 func TestArmorOverequippingSelectsRifleAndRangeButPreservesPioneerEffects(t *testing.T) {
    270 	for _, template := range []uint32{157685, 156831} {
    271 		t.Run(fmt.Sprint(template), func(t *testing.T) {
    272 			world, _ := newEquipmentWorld(t)
    273 			player, client := world.characters[1], world.clients[1]
    274 			player.stats.Body.Adventuring = 1000
    275 			if granted, err := world.grantItem(player, template); err != nil || !granted {
    276 				t.Fatal("backpack grant failed", err)
    277 			}
    278 			if err := world.handleInventory(client, inventoryMovePacket(1, 65, 19)[20:]); err != nil || client.equipment == nil {
    279 				t.Fatal("backpack wear failed", err)
    280 			}
    281 			world.advanceEquipment(client.equipment.readyAt)
    282 			profile := player.userData()
    283 			profile.Stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
    284 			profile.Stats.Body.Adventuring = 0
    285 
    286 			err := world.updateUser(profile)
    287 
    288 			if err != nil {
    289 				t.Fatal(err)
    290 			}
    291 			if template == 157685 {
    292 				if player.effectiveSkill(119, 5, [6]uint32{0, 60, 0, 0, 40, 0}) != 5 || player.equipmentBonus(380) != 0 {
    293 					t.Fatal("OE did not reduce rifle and range modifiers")
    294 				}
    295 			} else if player.effectiveAbility(16) != 2 || player.equipmentBonus(360) != -3 || player.equipmentBonus(123) != 10 || player.equipmentBonus(319) != 1 {
    296 				t.Fatal("OE stripped unaffected ability, skill, XP or signed scale bonuses")
    297 			}
    298 		})
    299 	}
    300 }
    301 
    302 func TestArmorOverequippingRemovalClampsVitalsAndKeepsObserverBasis(t *testing.T) {
    303 	world, connections := newResourceBackpackTeam(t)
    304 	player, client := world.characters[1], world.clients[1]
    305 	player.stats.TradeRepair.ComputerLiteracy = 650
    306 	player.health = 117
    307 	if err := world.saveUser(player); err != nil {
    308 		t.Fatal(err)
    309 	}
    310 	start := time.Unix(100, 0)
    311 	for _, other := range world.clients {
    312 		other.regenerationAt = start
    313 	}
    314 	world.advanceRegeneration(start.Add(2 * time.Second))
    315 	for _, conn := range connections {
    316 		conn.packets = nil
    317 	}
    318 
    319 	err := world.handleInventory(client, itemMovePacket(1, 19, 111)[20:])
    320 	if client.equipment == nil {
    321 		t.Fatal("overequipped removal was rejected", err)
    322 	}
    323 	world.advanceEquipment(client.equipment.readyAt)
    324 	restarted := newTestInventoryServer(t).characters[1]
    325 
    326 	if err != nil || player.maximumHealth() != 49 || player.health != 49 || restarted.health != 49 || restarted.inventory.Items[0].ID != 1 || restarted.inventory.Items[0].Slot != 64 {
    327 		t.Fatal("overequipped removal lost ownership or durable vitals", err, player.health, restarted.inventory.Items)
    328 	}
    329 	for index, want := range []uint32{49, 139, 49} {
    330 		var found []uint32
    331 		for _, packet := range connections[index].packets {
    332 			if binary.BigEndian.Uint32(packet[16:20]) == 0x2b333d6e && binary.BigEndian.Uint32(packet[24:28]) == 1 && binary.BigEndian.Uint32(packet[29:33]) == 1 {
    333 				found = append(found, binary.BigEndian.Uint32(packet[33:37]))
    334 			}
    335 		}
    336 		if len(found) != 1 || found[0] != want {
    337 			t.Fatalf("observer %d removal foundation=%v want%d", index+1, found, want)
    338 		}
    339 	}
    340 }