aorecomp

Anarchy Online classic in the browser

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


server/early_melee_test.go (6473B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"fmt"
      6 	"testing"
      7 	"time"
      8 )
      9 
     10 func TestAdvancedBaseballbatQualitiesEnforceEveryWearBoundaryAndRecover(t *testing.T) {
     11 	for _, template := range []uint32{122691, 122692} {
     12 		for _, scenario := range []struct {
     13 			quality, maximum, critical, value, multi uint32
     14 			blunt, sneak, fast, brawling, dimach     uint16
     15 		}{
     16 			{1, 8, 11, 2848, 22, 6, 6, 6, 6, 6},
     17 			{2, 9, 12, 2863, 23, 11, 8, 8, 9, 7},
     18 			{10, 17, 16, 4062, 31, 48, 27, 27, 31, 14},
     19 			{11, 18, 17, 4347, 31, 53, 29, 29, 34, 15},
     20 			{19, 26, 21, 7704, 39, 90, 48, 48, 56, 22},
     21 			{20, 27, 22, 8259, 40, 95, 50, 50, 59, 23},
     22 		} {
     23 			for _, failed := range []uint32{0, 102, 146, 147, 142, 144} {
     24 				t.Run(fmt.Sprintf("%d/QL%d/below%d", template, scenario.quality, failed), func(t *testing.T) {
     25 					world, output := newEquipmentWorld(t)
     26 					player, client := world.characters[1], world.clients[1]
     27 					player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
     28 					player.stats.Melee.OneHandBlunt, player.stats.Melee.SneakAttack, player.stats.Melee.FastAttack = scenario.blunt, scenario.sneak, scenario.fast
     29 					player.stats.Body.Brawling, player.stats.Body.Dimach = scenario.brawling, scenario.dimach
     30 					switch failed {
     31 					case 102:
     32 						player.stats.Melee.OneHandBlunt--
     33 					case 146:
     34 						player.stats.Melee.SneakAttack--
     35 					case 147:
     36 						player.stats.Melee.FastAttack--
     37 					case 142:
     38 						player.stats.Body.Brawling--
     39 					case 144:
     40 						player.stats.Body.Dimach--
     41 					}
     42 					player.inventory.Items[0].Template, player.inventory.Items[0].Quality = template, scenario.quality
     43 					if err := world.saveUser(player); err != nil {
     44 						t.Fatal(err)
     45 					}
     46 					if err := world.saveInventory(player, player.inventory); err != nil {
     47 						t.Fatal(err)
     48 					}
     49 
     50 					if err := world.handleInventory(client, inventoryMovePacket(1, 64, 6)[20:]); err != nil {
     51 						t.Fatal(err)
     52 					}
     53 					if client.equipment != nil {
     54 						world.advanceEquipment(client.equipment.readyAt)
     55 					}
     56 					cold := newTestInventoryServer(t)
     57 
     58 					item := cold.characters[1].inventory.Items[0]
     59 					if item.ID != 1 || item.Template != template || item.Quality != scenario.quality || item.Count != 1 {
     60 						t.Fatal("wear changed the selected instance", item)
     61 					}
     62 					if failed != 0 {
     63 						if item.Slot != 64 || player.inventory.Items[0].Slot != 64 || client.equipment != nil {
     64 							t.Fatal("equality at one wear threshold equipped the weapon", failed, item)
     65 						}
     66 						return
     67 					}
     68 					weapon := cold.characters[1].attackWeapon()
     69 					if item.Slot != 6 || weapon == nil || itemStat(weapon, 286) != 3 || itemStat(weapon, 285) != scenario.maximum ||
     70 						itemStat(weapon, 284) != scenario.critical || itemStat(weapon, 101) != scenario.multi || item.shopPrice() != scenario.value ||
     71 						itemStat(weapon, 294) != 150 || itemStat(weapon, 210) != 114 || itemStat(weapon, 211) != 150 || item.usesAmmo() {
     72 						t.Fatal("selected weapon lost its original damage, delays, value or ammunition contract", weapon, item)
     73 					}
     74 					low, high := uint32(122691), uint32(122692)
     75 					if scenario.quality == 1 {
     76 						high = low
     77 					} else if scenario.quality == 20 {
     78 						low = high
     79 					}
     80 					found := false
     81 					for _, packet := range output.packets {
     82 						if binary.BigEndian.Uint32(packet[16:20]) == 0x3b1d2268 {
     83 							found = len(packet) >= 91 && binary.BigEndian.Uint32(packet[71:75]) == scenario.quality &&
     84 								binary.BigEndian.Uint32(packet[79:83]) == low && binary.BigEndian.Uint32(packet[87:91]) == high
     85 						}
     86 					}
     87 					if !found {
     88 						t.Fatal("native wear delivery lost the selected endpoints")
     89 					}
     90 				})
     91 			}
     92 		}
     93 	}
     94 }
     95 
     96 func TestAdvancedBaseballbatRejectsUnsupportedQualitiesWithoutLosingTheItem(t *testing.T) {
     97 	for _, selection := range [][2]uint32{{122691, 0}, {122691, 21}, {122692, 0}, {122692, 21}, {122693, 2}} {
     98 		t.Run(fmt.Sprint(selection), func(t *testing.T) {
     99 			world, _ := newEquipmentWorld(t)
    100 			player := world.characters[1]
    101 			player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
    102 			player.stats.Melee.OneHandBlunt, player.stats.Melee.SneakAttack, player.stats.Melee.FastAttack = 1000, 1000, 1000
    103 			player.stats.Body.Brawling, player.stats.Body.Dimach = 1000, 1000
    104 			player.inventory.Items[0].Template, player.inventory.Items[0].Quality = selection[0], selection[1]
    105 
    106 			if err := world.handleInventory(world.clients[1], inventoryMovePacket(1, 64, 6)[20:]); err != nil {
    107 				t.Fatal(err)
    108 			}
    109 
    110 			if world.clients[1].equipment != nil || player.inventory.Items[0].ID != 1 || player.inventory.Items[0].Slot != 64 ||
    111 				player.inventory.Items[0].Template != selection[0] || player.inventory.Items[0].Quality != selection[1] {
    112 				t.Fatal("unsupported interpolation consumed or equipped an item", selection)
    113 			}
    114 		})
    115 	}
    116 }
    117 
    118 func TestAdvancedBaseballbatUsesSelectedNormalAndFastDamageWithoutAmmunition(t *testing.T) {
    119 	for _, action := range []string{"normal", "fast"} {
    120 		for _, critical := range []bool{false, true} {
    121 			t.Run(fmt.Sprintf("%s/critical=%v", action, critical), func(t *testing.T) {
    122 				world, _ := newRangedWorld(t)
    123 				player, npc := world.characters[1], world.characters[1000000]
    124 				npc.position = [3]float32{1, 0, 0}
    125 				player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
    126 				player.stats.Melee.OneHandBlunt, player.stats.Melee.SneakAttack, player.stats.Melee.FastAttack = 11, 8, 8
    127 				player.stats.Body.Brawling, player.stats.Body.Dimach = 9, 7
    128 				player.inventory.Items[0].Template, player.inventory.Items[0].Quality, player.inventory.Items[0].Ammo = 122691, 2, 0
    129 				if err := world.saveInventory(player, player.inventory); err != nil {
    130 					t.Fatal(err)
    131 				}
    132 				world.combatRoll = func(bound uint32) uint32 {
    133 					if bound == 10000 {
    134 						return 0
    135 					}
    136 					if critical {
    137 						return 0
    138 					}
    139 					return bound - 1
    140 				}
    141 
    142 				if action == "fast" {
    143 					if err := world.handleWeaponSpecial(world.clients[1], fastAttackRequest(1, npc.id)[20:]); err != nil {
    144 						t.Fatal(err)
    145 					}
    146 				} else {
    147 					if err := world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:]); err != nil {
    148 						t.Fatal(err)
    149 					}
    150 					world.advanceCombat(player.nextAttack.Add(time.Nanosecond))
    151 				}
    152 
    153 				if !critical && npc.health != 991 || critical && npc.health != 979 || player.inventory.Items[0].Ammo != 0 || player.inventory.Items[1].Count != 100 {
    154 					t.Fatal("melee damage used an endpoint or consumed bullets", npc.health, player.inventory.Items)
    155 				}
    156 			})
    157 		}
    158 	}
    159 }