aorecomp

Anarchy Online classic in the browser

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


server/pet_attack_test.go (12005B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func newPetAttackWorld(t *testing.T) (*server, *combatTestConn) {
     12 	t.Helper()
     13 	world, output := newPetSummonWorld(t)
     14 	client := world.clients[1]
     15 	world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:])
     16 	world.advanceNanos(client.nanoCast.readyAt.Add(time.Nanosecond))
     17 	target := world.characters[1000000]
     18 	target.monsterData, target.level = 17655, 1
     19 	target.combatProfile = gamedata.NPCCombatProfiles[17655][1]
     20 	target.motion = &npcMotion{home: target.position}
     21 	world.mobileNPCs = map[uint32][]*character{950: {target}}
     22 	client.selectedTarget = [2]uint32{50000, target.id}
     23 	output.packets = nil
     24 	return world, output
     25 }
     26 
     27 func petAttackRequest() []byte {
     28 	request := petFollowRequest(1)
     29 	binary.BigEndian.PutUint32(request[36:], 7)
     30 	return request[20:]
     31 }
     32 
     33 func TestPetAttackPursuesAndCreditsOwnerWhileEnemyRetaliatesAgainstPet(t *testing.T) {
     34 	world, _ := newPetAttackWorld(t)
     35 	pet, target, owner := world.characters[1000001], world.characters[1000000], world.characters[1]
     36 	target.position, target.health = [3]float32{40, 0, 0}, 32
     37 	ownerHealth := owner.health
     38 
     39 	if err := world.handlePetCommand(world.clients[1], petAttackRequest()); err != nil {
     40 		t.Fatal(err)
     41 	}
     42 	deadline := pet.nextAttack
     43 	world.handlePetCommand(world.clients[1], petAttackRequest())
     44 	if pet.nextAttack != deadline {
     45 		t.Fatal("repeated command restarted the attack phase")
     46 	}
     47 	world.advanceCombat(deadline)
     48 	if target.health != 32 || pet.nextAttack != deadline {
     49 		t.Fatal("pet hit outside range or restarted its pending swing", target.health, pet.nextAttack, deadline)
     50 	}
     51 	start := pet.movement.updatedAt
     52 	for tick := 1; tick <= 120 && target.health == 32; tick++ {
     53 		now := start.Add(time.Duration(tick) * 100 * time.Millisecond)
     54 		advanceNavigationForTest(t, world, now, world.advancePlayerMovement)
     55 		world.advanceCombat(now)
     56 	}
     57 	if target.health != 16 || target.fighting != pet.id || target.threat[pet.id] != 16 ||
     58 		target.damageContributors[damageContributor{player: 1}] != 16 || pet.position[0] <= 2 || owner.fighting != 0 {
     59 		t.Fatal("pursuit, individual retaliation or owner credit failed", target.health, target.fighting, pet.position)
     60 	}
     61 	for tick := 1; tick <= 50 && pet.health == 17; tick++ {
     62 		now := target.nextAttack.Add(time.Duration(tick) * 100 * time.Millisecond)
     63 		advanceNavigationForTest(t, world, now, world.advancePlayerMovement)
     64 		advanceNavigationForTest(t, world, now, world.advanceNPCs)
     65 		world.advanceCombat(now)
     66 	}
     67 	if pet.health != 9 || owner.health != ownerHealth {
     68 		t.Fatal("enemy did not damage the pet independently of its owner", pet.health, owner.health)
     69 	}
     70 	world.advanceCombat(pet.nextAttack)
     71 	cold := newServerForTest(t)
     72 
     73 	for _, current := range []*server{world, cold} {
     74 		corpse := current.characters[1000000].corpse
     75 		if current.characters[1000000].health != 0 || corpse == nil || corpse.looter != 1 || corpse.credits != 5 ||
     76 			current.characters[1].experience != 100 || len(current.characters[1].pets) != 1 || current.characters[1].nano != 25 ||
     77 			current.characters[1000001].attackWeapon().ID != 120637 {
     78 			t.Fatal("pet kill lost reward, owner or cold weapon profile", corpse)
     79 		}
     80 	}
     81 	if pet.fighting != 0 || !pet.nextAttack.IsZero() || len(world.fighting) != 0 {
     82 		t.Fatal("dead target retained an encounter")
     83 	}
     84 }
     85 
     86 func TestPetAttackRejectsInvalidTargetsAndUnavailableOwners(t *testing.T) {
     87 	for _, scenario := range []string{"no selection", "corpse identity", "unknown", "player", "pet", "human", "dead", "zone", "hidden", "owner dead", "loading", "offline", "expired"} {
     88 		t.Run(scenario, func(t *testing.T) {
     89 			world, output := newPetAttackWorld(t)
     90 			client, pet, target := world.clients[1], world.characters[1000001], world.characters[1000000]
     91 			switch scenario {
     92 			case "no selection":
     93 				client.selectedTarget = [2]uint32{}
     94 			case "corpse identity":
     95 				client.selectedTarget[0] = 51050
     96 			case "unknown":
     97 				client.selectedTarget[1] = 999
     98 			case "player":
     99 				client.selectedTarget[1] = 1
    100 			case "pet":
    101 				client.selectedTarget[1] = pet.id
    102 			case "human":
    103 				target.appearance = 277
    104 			case "dead":
    105 				target.health = 0
    106 			case "zone":
    107 				target.playfield++
    108 			case "hidden":
    109 				delete(client.npcs, target.id)
    110 			case "owner dead":
    111 				world.characters[1].health = 0
    112 			case "loading":
    113 				client.loading = true
    114 			case "offline":
    115 				delete(world.clients, 1)
    116 			case "expired":
    117 				pet.pet.expiresAt = time.Now().Add(-time.Second)
    118 			}
    119 
    120 			err := world.handlePetCommand(client, petAttackRequest())
    121 
    122 			if err != nil || pet.fighting != 0 || pet.motion != nil || len(output.packets) != 0 || len(world.fighting) != 0 {
    123 				t.Fatal("invalid pet attack changed combat or motion", err)
    124 			}
    125 		})
    126 	}
    127 }
    128 
    129 func TestPetCombatStopsWhenOwnerBecomesUnavailable(t *testing.T) {
    130 	for _, scenario := range []string{"dead", "zone", "loading", "offline", "expired"} {
    131 		t.Run(scenario, func(t *testing.T) {
    132 			world, _ := newPetAttackWorld(t)
    133 			pet, target := world.characters[1000001], world.characters[1000000]
    134 			world.handlePetCommand(world.clients[1], petAttackRequest())
    135 			world.advanceCombat(pet.nextAttack)
    136 			health := target.health
    137 			switch scenario {
    138 			case "dead":
    139 				world.characters[1].health = 0
    140 			case "zone":
    141 				world.characters[1].playfield++
    142 			case "loading":
    143 				world.clients[1].loading = true
    144 			case "offline":
    145 				delete(world.clients, 1)
    146 			case "expired":
    147 				pet.pet.expiresAt = time.Now().Add(-time.Second)
    148 			}
    149 
    150 			world.advanceCombat(pet.nextAttack)
    151 			advanceNavigationForTest(t, world, time.Now().Add(time.Second), world.advancePlayerMovement)
    152 
    153 			if pet.fighting != 0 || target.fighting != 0 || target.health != health || pet.health != 17 || pet.motion.returning {
    154 				t.Fatal("unavailable owner left pet combat or pursuit running")
    155 			}
    156 		})
    157 	}
    158 }
    159 
    160 func TestPetDeathCommitsRemovalAndAllowsAnotherSummon(t *testing.T) {
    161 	for _, fault := range []string{"none", "ignored deletion", "cascade failure", "commit failure"} {
    162 		t.Run(fault, func(t *testing.T) {
    163 			world, output := newPetAttackWorld(t)
    164 			pet, target, owner := world.characters[1000001], world.characters[1000000], world.characters[1]
    165 			world.handlePetCommand(world.clients[1], petAttackRequest())
    166 			world.advanceCombat(pet.nextAttack)
    167 			pet.health = 1
    168 			query := "CREATE TRIGGER reject_pet_death BEFORE DELETE ON characters WHEN OLD.id=1000001 BEGIN SELECT RAISE(IGNORE); END"
    169 			if fault == "cascade failure" {
    170 				query = "CREATE TRIGGER reject_pet_death BEFORE DELETE ON pets BEGIN SELECT RAISE(ABORT,'pet death'); END"
    171 			}
    172 			if fault == "commit failure" {
    173 				if _, err := world.db.Exec("CREATE TABLE pet_death_failure (id INTEGER REFERENCES characters(id) DEFERRABLE INITIALLY DEFERRED)"); err != nil {
    174 					t.Fatal(err)
    175 				}
    176 				query = "CREATE TRIGGER reject_pet_death AFTER DELETE ON characters WHEN OLD.id=1000001 BEGIN INSERT INTO pet_death_failure VALUES (999); END"
    177 			}
    178 			if fault != "none" {
    179 				if _, err := world.db.Exec(query); err != nil {
    180 					t.Fatal(err)
    181 				}
    182 			}
    183 			before, err := readTestItemStore(world)
    184 			if err != nil {
    185 				t.Fatal(err)
    186 			}
    187 			output.packets = nil
    188 
    189 			world.advanceCombat(target.nextAttack)
    190 			if fault != "none" {
    191 				after, err := readTestItemStore(world)
    192 				if err != nil || !bytes.Equal(before, after) || pet.health != 1 || !owner.pets[pet.id] || world.characters[pet.id] != pet {
    193 					t.Fatal("failed lethal hit changed storage or pet ownership", err)
    194 				}
    195 				for _, packet := range output.packets {
    196 					if binary.BigEndian.Uint32(packet[16:20]) == 0x36510078 || binary.BigEndian.Uint32(packet[16:20]) == 0x58742a0f {
    197 						t.Fatal("failed death published removal")
    198 					}
    199 				}
    200 				if _, err := world.db.Exec("DROP TRIGGER reject_pet_death"); err != nil {
    201 					t.Fatal(err)
    202 				}
    203 				world.handlePetCommand(world.clients[1], petAttackRequest())
    204 				world.advanceCombat(pet.nextAttack)
    205 				world.advanceCombat(target.nextAttack)
    206 			}
    207 			cold := newServerForTest(t)
    208 
    209 			if world.characters[pet.id] != nil || cold.characters[pet.id] != nil || len(owner.pets) != 0 || len(cold.characters[1].pets) != 0 ||
    210 				len(world.fighting) != 0 || len(world.petExpiry) != 0 || len(world.corpseExpiry) != 0 || owner.experience != 0 || world.nextNPC != 1000002 {
    211 				t.Fatal("lethal retaliation retained pet ownership, combat or generated rewards")
    212 			}
    213 			owner.nanoRechargeUntil = time.Time{}
    214 			world.handleNanoCast(world.clients[1], characterActionPacket(1, [5]uint32{19, 0, 0, 53019, 43324})[20:])
    215 			cast := world.clients[1].nanoCast
    216 			if cast == nil {
    217 				t.Fatal("dead pet blocked the next summon")
    218 			}
    219 			world.advanceNanos(cast.readyAt.Add(time.Nanosecond))
    220 			if len(owner.pets) != 1 || !owner.pets[1000002] || owner.nano != 15 {
    221 				t.Fatal("replacement summon lost identity or cost")
    222 			}
    223 		})
    224 	}
    225 }
    226 
    227 func TestPetDamageCreditsTheOwnersTeamAtEachHit(t *testing.T) {
    228 	world, _ := newPetAttackWorld(t)
    229 	owner, pet, target := world.characters[1], world.characters[1000001], world.characters[1000000]
    230 	owner.team = &playerTeam{id: 7}
    231 
    232 	world.handlePetCommand(world.clients[1], petAttackRequest())
    233 	world.advanceCombat(pet.nextAttack)
    234 	owner.team = nil
    235 	// Leave the enemy's actual retaliation in the same shared combat tick.
    236 	world.advanceCombat(pet.nextAttack)
    237 
    238 	if target.threat[pet.id] != 32 || target.threat[owner.id] != 0 ||
    239 		target.damageContributors[damageContributor{team: 7}] != 16 || target.damageContributors[damageContributor{player: 1}] != 16 ||
    240 		target.leadingContributor != (damageContributor{team: 7}) || target.fighting != pet.id {
    241 		t.Fatal("pet threat and per-hit owner/team reward credit were conflated", target.threat, target.damageContributors)
    242 	}
    243 }
    244 
    245 func TestPetKillRewardFailureRollsBackAndRetries(t *testing.T) {
    246 	world, _ := newPetAttackWorld(t)
    247 	owner, pet, target := world.characters[1], world.characters[1000001], world.characters[1000000]
    248 	target.health = 16
    249 	if _, err := world.db.Exec("CREATE TRIGGER reject_pet_reward BEFORE UPDATE OF experience ON characters WHEN NEW.id=1 BEGIN SELECT RAISE(ABORT,'reward'); END"); err != nil {
    250 		t.Fatal(err)
    251 	}
    252 	before, err := readTestItemStore(world)
    253 	if err != nil {
    254 		t.Fatal(err)
    255 	}
    256 
    257 	world.handlePetCommand(world.clients[1], petAttackRequest())
    258 	world.advanceCombat(pet.nextAttack)
    259 	after, err := readTestItemStore(world)
    260 	if err != nil || !bytes.Equal(before, after) || target.health != 16 || target.corpse != nil || owner.experience != 0 || len(target.damageContributors) != 0 {
    261 		t.Fatal("failed pet reward partially committed its kill", err)
    262 	}
    263 	if _, err := world.db.Exec("DROP TRIGGER reject_pet_reward"); err != nil {
    264 		t.Fatal(err)
    265 	}
    266 	world.handlePetCommand(world.clients[1], petAttackRequest())
    267 	world.advanceCombat(pet.nextAttack)
    268 	cold := newServerForTest(t)
    269 
    270 	if target.health != 0 || target.corpse.looter != 1 || owner.experience != 100 || cold.characters[1].experience != 100 || cold.characters[1000000].corpse.looter != 1 {
    271 		t.Fatal("pet reward retry lost or repeated the kill")
    272 	}
    273 }
    274 
    275 func TestPetPursuitInterestReplaysRouteAndCombatAfterSpawn(t *testing.T) {
    276 	world, output := newPetAttackWorld(t)
    277 	client, pet := world.clients[1], world.characters[1000001]
    278 	world.characters[1000000].position = [3]float32{40, 0, 0}
    279 	world.handlePetCommand(client, petAttackRequest())
    280 	now := pet.movement.updatedAt.Add(250 * time.Millisecond)
    281 	advanceNavigationForTest(t, world, now, world.advancePlayerMovement)
    282 	delete(client.npcs, pet.id)
    283 	output.packets = nil
    284 
    285 	advanceNavigationForTest(t, world, now, world.advanceNPCs)
    286 
    287 	spawn, route, attack := -1, -1, -1
    288 	for index, packet := range output.packets {
    289 		if binary.BigEndian.Uint32(packet[24:28]) != pet.id {
    290 			continue
    291 		}
    292 		switch binary.BigEndian.Uint32(packet[16:20]) {
    293 		case 0x271b3a6b:
    294 			spawn = index
    295 		case 0x260f3671:
    296 			route = index
    297 		case 0x28494070:
    298 			attack = index
    299 		}
    300 	}
    301 	if spawn < 0 || route <= spawn || attack <= route {
    302 		t.Fatal("pet interest omitted or reordered its pursuit route and encounter", spawn, route, attack)
    303 	}
    304 }