aorecomp

Anarchy Online classic in the browser

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


server/weapon_miss_test.go (11086B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"fmt"
      8 	"slices"
      9 	"testing"
     10 	"time"
     11 )
     12 
     13 func TestOrdinaryWeaponHitThresholdUsesDefenderAggDef(t *testing.T) {
     14 	for _, scenario := range []struct {
     15 		aggDef int32
     16 		chance uint32
     17 	}{{-100, 6306}, {0, 7883}, {100, 9460}} {
     18 		for _, landed := range []bool{true, false} {
     19 			t.Run(fmt.Sprintf("%d/%t", scenario.aggDef, landed), func(t *testing.T) {
     20 				world, conn := newRangedWorld(t)
     21 				player, target := world.characters[1], world.characters[1000000]
     22 				player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
     23 				player.stats.Ranged.Pistol = 100
     24 				target.stats.Speed.DodgeRanged = 100
     25 				target.aggDef = scenario.aggDef
     26 				var bounds []uint32
     27 				world.combatRoll = func(limit uint32) uint32 {
     28 					bounds = append(bounds, limit)
     29 					if limit == 10000 {
     30 						if landed {
     31 							return scenario.chance - 1
     32 						}
     33 						return scenario.chance
     34 					}
     35 					return limit - 1
     36 				}
     37 
     38 				if err := world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:]); err != nil {
     39 					t.Fatal(err)
     40 				}
     41 				world.advanceCombat(player.nextAttack)
     42 
     43 				wantKey, wantBounds := uint32(0x5c654b28), []uint32{10000}
     44 				if landed {
     45 					wantKey, wantBounds = 0x46002f16, []uint32{10000, 100, 13}
     46 				}
     47 				if !slices.Equal(bounds, wantBounds) || binary.BigEndian.Uint32(conn.packets[1][16:20]) != wantKey ||
     48 					(target.health < 1000) != landed || player.inventory.Items[0].Ammo != 2 {
     49 					t.Fatal("hit threshold or draw sequence", bounds, target.health, conn.packets)
     50 				}
     51 			})
     52 		}
     53 	}
     54 }
     55 
     56 func TestOrdinaryWeaponHitChanceHandlesZeroAndExtremeRatings(t *testing.T) {
     57 	for _, scenario := range []struct {
     58 		attack, defense uint16
     59 		roll            uint32
     60 		landed          bool
     61 	}{{0, 0, 0, false}, {0, 100, 0, false}, {100, 0, 9999, true}, {100, 1, 9999, true}, {1, 60000, 0, false}} {
     62 		t.Run(fmt.Sprint(scenario), func(t *testing.T) {
     63 			world, conn := newRangedWorld(t)
     64 			player, target := world.characters[1], world.characters[1000000]
     65 			player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
     66 			player.stats.Ranged.Pistol = scenario.attack
     67 			target.stats.Speed.DodgeRanged = scenario.defense
     68 			world.combatRoll = func(limit uint32) uint32 {
     69 				if limit == 10000 {
     70 					return scenario.roll
     71 				}
     72 				return limit - 1
     73 			}
     74 
     75 			world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:])
     76 			world.advanceCombat(player.nextAttack)
     77 
     78 			key := uint32(0x5c654b28)
     79 			if scenario.landed {
     80 				key = 0x46002f16
     81 			}
     82 			if binary.BigEndian.Uint32(conn.packets[1][16:20]) != key || (target.health < 1000) != scenario.landed || player.inventory.Items[0].Ammo != 2 {
     83 				t.Fatal("nonpositive or extreme rating outcome", conn.packets)
     84 			}
     85 		})
     86 	}
     87 }
     88 
     89 func TestTrainedEvadeChangesIncomingOutcomeAndSurvivesRestart(t *testing.T) {
     90 	world, conn := newTrainingWorld(t)
     91 	player, npc := world.characters[1], world.characters[1000000]
     92 	npc.position = [3]float32{1, 0, 0}
     93 	npc.fighting, npc.nextAttack = 1, time.Now()
     94 	world.fighting[npc.id] = npc
     95 	world.combatRoll = func(limit uint32) uint32 { return min(5000, limit-1) }
     96 	world.advanceCombat(npc.nextAttack)
     97 	health := player.health
     98 	if health != player.maximumHealth()-5 {
     99 		t.Fatal("untrained baseline did not land the controlled attack", health)
    100 	}
    101 	trained := player.baseStats()
    102 	trained.Speed.EvadeClose = 6
    103 
    104 	err := world.handleSkillTraining(world.clients[1], skillTrainingRequest(trained)[20:])
    105 	conn.packets = nil
    106 	world.advanceCombat(npc.nextAttack)
    107 	cold := newTestInventoryServer(t).characters[1]
    108 
    109 	if err != nil || player.health != health || player.stats.Speed.EvadeClose != 6 || cold.stats.Speed.EvadeClose != 6 ||
    110 		player.stats.ImprovementPoints >= 1500 || cold.stats.ImprovementPoints != player.stats.ImprovementPoints ||
    111 		len(conn.packets) != 1 || binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x5c654b28 {
    112 		t.Fatal("trained evade did not change the same incoming roll or persist", health, player.health, player.stats, err)
    113 	}
    114 }
    115 
    116 func TestRangedMissSaveFailureDoesNotPublishOrSpendTheRound(t *testing.T) {
    117 	world, conn := newRangedWorld(t)
    118 	player, target := world.characters[1], world.characters[1000000]
    119 	target.stats.Speed.DodgeRanged = 100
    120 	world.combatRoll = func(limit uint32) uint32 { return limit - 1 }
    121 	if _, err := world.db.Exec("CREATE TRIGGER fail_miss BEFORE UPDATE OF ammo ON items BEGIN SELECT RAISE(FAIL, 'test miss save failure'); END"); err != nil {
    122 		t.Fatal(err)
    123 	}
    124 	world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:])
    125 	conn.packets = nil
    126 
    127 	world.advanceCombat(player.nextAttack)
    128 	cold := newTestInventoryServer(t)
    129 
    130 	if target.health != 1000 || len(target.threat) != 0 || len(target.damageContributors) != 0 || target.fighting != 0 ||
    131 		player.inventory.Items[0].Ammo != 3 || cold.characters[1].inventory.Items[0].Ammo != 3 || player.fighting != 0 ||
    132 		!player.rechargeUntil.IsZero() || len(conn.packets) != 1 || binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x4a41203e {
    133 		t.Fatal("failed miss debit published state or feedback", conn.packets, player.inventory)
    134 	}
    135 }
    136 
    137 func TestOrdinaryMissDoesNotBreakControlEffects(t *testing.T) {
    138 	world, conn := newRootedControlWorld(t)
    139 	player, target := world.characters[1], world.characters[1000000]
    140 	player.stats.Body.MartialArts = 5
    141 	target.stats.Speed.EvadeClose = 100
    142 	world.combatRoll = func(limit uint32) uint32 {
    143 		if limit != 10000 {
    144 			t.Fatal("a miss rolled damage or a control break", limit)
    145 		}
    146 		return limit - 1
    147 	}
    148 	before := slices.Clone(target.activeNanos)
    149 	world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:])
    150 
    151 	world.advanceCombat(player.nextAttack)
    152 	cold := newTestInventoryServer(t).characters[target.id]
    153 
    154 	if !slices.Equal(before, target.activeNanos) || !target.movementRestricted() || !cold.movementRestricted() || len(target.damageContributors) != 0 {
    155 		t.Fatal("miss broke a control nano or granted damage credit", target.activeNanos, cold.activeNanos)
    156 	}
    157 	for _, packet := range conn.packets {
    158 		if binary.BigEndian.Uint32(packet[16:20]) == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 65 {
    159 			t.Fatal("miss emitted a control-removal action")
    160 		}
    161 	}
    162 }
    163 
    164 func TestPre1555FastAndFlingBypassTheOrdinaryDefenseCheck(t *testing.T) {
    165 	for _, special := range []uint32{147, 150} {
    166 		t.Run(fmt.Sprint(special), func(t *testing.T) {
    167 			var world *server
    168 			var conn *combatTestConn
    169 			var request []byte
    170 			if special == 147 {
    171 				world, conn = newFastAttackWorld(t)
    172 				request = fastAttackRequest(1, 1000000)
    173 			} else {
    174 				world, conn = newRangedWorld(t)
    175 				request = flingShotRequest(1, 1000000)
    176 			}
    177 			target := world.characters[1000000]
    178 			target.stats.Speed.EvadeClose, target.stats.Speed.DodgeRanged = 60000, 60000
    179 			world.combatRoll = func(limit uint32) uint32 {
    180 				if limit == 10000 {
    181 					t.Fatal("period Fast/Fling made an ordinary defensive check")
    182 				}
    183 				return limit - 1
    184 			}
    185 
    186 			err := world.handleWeaponSpecial(world.clients[1], request[20:])
    187 
    188 			if err != nil || target.health >= 1000 || len(world.characters[1].skillLocks) != 1 {
    189 				t.Fatal("special did not land or retain its lock", target.health, err)
    190 			}
    191 			for _, packet := range conn.packets {
    192 				if binary.BigEndian.Uint32(packet[16:20]) == 0x5c654b28 {
    193 					t.Fatal("special emitted ordinary miss feedback")
    194 				}
    195 			}
    196 		})
    197 	}
    198 }
    199 
    200 func TestWeaponDefenseUsesTheAttackingWeaponAuthoredSkill(t *testing.T) {
    201 	for _, stat := range []uint32{153, 154, 155} {
    202 		t.Run(fmt.Sprint(stat), func(t *testing.T) {
    203 			world, conn := newRangedWorld(t)
    204 			player, target := world.characters[1], world.characters[1000000]
    205 			player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1}
    206 			player.stats.Ranged.Pistol = 100
    207 			target.stats.Speed.DuckExplosions, target.stats.Speed.DodgeRanged, target.stats.Speed.EvadeClose = 1, 1, 1
    208 			for _, field := range target.stats.trainingStats() {
    209 				if field.stat == stat {
    210 					*field.value = 100
    211 				}
    212 			}
    213 			original := gamedata.ItemTemplates[123742]
    214 			weapon := *original
    215 			weapon.Sections = slices.Clone(original.Sections)
    216 			kind := uint32(13)
    217 			weights := [][2]uint32{{stat, 100}}
    218 			for index, section := range weapon.Sections {
    219 				if section.Type == 4 && section.Subtype == 4 {
    220 					entries := slices.Clone(*section.Entries)
    221 					for index := range entries {
    222 						if entries[index].Type != nil && *entries[index].Type == kind {
    223 							entries[index].Stats = &weights
    224 						}
    225 					}
    226 					weapon.Sections[index].Entries = &entries
    227 				}
    228 			}
    229 			gamedata.ItemTemplates[123742] = &weapon
    230 			t.Cleanup(func() { gamedata.ItemTemplates[123742] = original })
    231 			world.combatRoll = func(limit uint32) uint32 { return min(7883, limit-1) }
    232 
    233 			world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:])
    234 			world.advanceCombat(player.nextAttack)
    235 
    236 			if target.health != 1000 || binary.BigEndian.Uint32(conn.packets[1][16:20]) != 0x5c654b28 {
    237 				t.Fatal("defense ignored the weapon's authored skill", stat, target.health)
    238 			}
    239 		})
    240 	}
    241 }
    242 
    243 func TestRangedMissPreservesHealthCreditsAndClockAndCommitsAmmunition(t *testing.T) {
    244 	world, conn := newRangedWorld(t)
    245 	player, target := world.characters[1], world.characters[1000000]
    246 	target.health = 1
    247 	target.stats.Speed.DodgeRanged = 100
    248 	target.position = [3]float32{1, 0, 0}
    249 	observer := &combatTestConn{}
    250 	world.characters[2] = &character{id: 2, playfield: 950, health: 100}
    251 	world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true, npcs: map[uint32]bool{target.id: true}}
    252 	world.combatRoll = func(limit uint32) uint32 { return limit - 1 }
    253 	if err := world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:]); err != nil {
    254 		t.Fatal(err)
    255 	}
    256 	now := player.nextAttack
    257 
    258 	world.advanceCombat(now)
    259 	world.advanceCombat(now)
    260 	cold := newTestInventoryServer(t)
    261 
    262 	if target.health != 1 || target.corpse != nil || len(target.damageContributors) != 0 ||
    263 		player.experience != 0 || player.inventory.Credits != 0 || player.inventory.Items[0].Ammo != 2 || cold.characters[1].inventory.Items[0].Ammo != 2 ||
    264 		player.nextAttack != now.Add(3950*time.Millisecond) || player.rechargeUntil != now.Add(1970*time.Millisecond) {
    265 		t.Fatal("miss changed health, rewards, ammunition or cycle", target, player)
    266 	}
    267 	if target.fighting != 1 || len(target.threat) != 1 || target.threat[1] != 0 {
    268 		t.Fatal("miss did not begin retaliation without damage credit", target.threat)
    269 	}
    270 	var miss, observed []byte
    271 	for _, packet := range conn.packets {
    272 		if binary.BigEndian.Uint32(packet[16:20]) == 0x5c654b28 {
    273 			miss = packet[29:53]
    274 		}
    275 		if binary.BigEndian.Uint32(packet[16:20]) == 0x46002f16 || binary.BigEndian.Uint32(packet[16:20]) == 0x3710256c {
    276 			t.Fatal("miss also emitted damage", packet)
    277 		}
    278 	}
    279 	for _, packet := range observer.packets {
    280 		if binary.BigEndian.Uint32(packet[16:20]) == 0x5c654b28 {
    281 			observed = packet[29:53]
    282 		}
    283 	}
    284 	if !bytes.Equal(miss, []byte{0, 0, 0, 2, 0, 0, 0, 6, 0, 0, 195, 80, 0, 0, 0, 1, 0, 0, 195, 80, 0, 15, 66, 64}) || !bytes.Equal(miss, observed) {
    285 		t.Fatalf("miss payload or observer mismatch: %x %x", miss, observed)
    286 	}
    287 }