aorecomp

Anarchy Online classic in the browser

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


server/npc_engagement_range_test.go (4420B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestProfiledNPCRetainsEngagementOutsideWeaponRange(t *testing.T) {
     12 	for _, profile := range []struct {
     13 		monster uint32
     14 		level   uint16
     15 		rangeM  float32
     16 	}{
     17 		{17655, 1, 10}, {17655, 2, 10}, {30365, 1, 5}, {30365, 2, 5}, {30354, 4, 12},
     18 	} {
     19 		for _, roaming := range []bool{false, true} {
     20 			t.Run(fmt.Sprintf("%d/%d/roaming=%t", profile.monster, profile.level, roaming), func(t *testing.T) {
     21 				world, conn := newRangedWorld(t)
     22 				player, npc := world.characters[1], world.characters[1000000]
     23 				npc.monsterData, npc.level, npc.health = profile.monster, profile.level, 100
     24 				npc.combatProfile = gamedata.NPCCombatProfiles[profile.monster][profile.level]
     25 				if roaming {
     26 					npc.motion = &npcMotion{roaming: true, home: npc.position}
     27 				}
     28 				if err := world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:]); err != nil {
     29 					t.Fatal(err)
     30 				}
     31 				first := player.nextAttack
     32 				world.advanceCombat(first)
     33 				deadline := npc.nextAttack
     34 				conn.packets = nil
     35 
     36 				world.advanceCombat(first.Add(250 * time.Millisecond))
     37 				world.advanceCombat(first.Add(3950 * time.Millisecond))
     38 				world.advanceCombat(first.Add(4200 * time.Millisecond))
     39 
     40 				if npc.fighting != player.id || npc.nextAttack != deadline || player.health != 100 || world.fighting[npc.id] != npc {
     41 					t.Fatal("out-of-range NPC lost engagement, restarted its deadline or dealt damage", npc.fighting, npc.nextAttack, player.health)
     42 				}
     43 				for _, packet := range conn.packets {
     44 					key := binary.BigEndian.Uint32(packet[16:20])
     45 					if binary.BigEndian.Uint32(packet[24:28]) == npc.id && (key == 0x28494070 || key == 0x4a41203e) {
     46 						t.Fatal("another ranged hit restarted or stopped native NPC combat")
     47 					}
     48 				}
     49 
     50 				player.position[0] = npc.position[0] - profile.rangeM
     51 				world.advanceCombat(first.Add(4500 * time.Millisecond))
     52 				health := player.health
     53 				world.advanceCombat(first.Add(4500 * time.Millisecond))
     54 
     55 				if health >= 100 || player.health != health || npc.fighting != player.id || !npc.nextAttack.After(first.Add(4500*time.Millisecond)) {
     56 					t.Fatal("returning to range did not deliver exactly one due retaliation", health, player.health, npc.nextAttack)
     57 				}
     58 			})
     59 		}
     60 	}
     61 }
     62 
     63 func TestFixedNPCRangedEngagementUsesCombatRecovery(t *testing.T) {
     64 	world, _ := newRangedWorld(t)
     65 	player, npc := world.characters[1], world.characters[1000000]
     66 	npc.monsterData, npc.level, npc.health = 30354, 4, 20
     67 	npc.combatProfile = gamedata.NPCCombatProfiles[30354][4]
     68 	if err := world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:]); err != nil {
     69 		t.Fatal(err)
     70 	}
     71 	first := player.nextAttack
     72 	world.advanceCombat(first)
     73 	world.advanceRegeneration(first)
     74 	damaged := npc.health
     75 
     76 	world.advanceCombat(first.Add(250 * time.Millisecond))
     77 	world.advanceRegeneration(first.Add(2 * time.Second))
     78 
     79 	if npc.health != damaged || npc.fighting != player.id {
     80 		t.Fatal("engaged fixed NPC took the idle health branch", damaged, npc.health, npc.fighting)
     81 	}
     82 
     83 	for seconds := 4; seconds <= 60; seconds += 2 {
     84 		world.advanceRegeneration(first.Add(time.Duration(seconds) * time.Second))
     85 	}
     86 
     87 	if npc.health != damaged+3 {
     88 		t.Fatal("fixed engagement did not retain the native combat recovery counter", damaged, npc.health)
     89 	}
     90 }
     91 
     92 func TestFixedNPCRangedEngagementEndsWhenTargetUnavailable(t *testing.T) {
     93 	for _, reason := range []string{"disconnect", "loading", "dead", "zone"} {
     94 		t.Run(reason, func(t *testing.T) {
     95 			world, conn := newRangedWorld(t)
     96 			player, npc := world.characters[1], world.characters[1000000]
     97 			npc.monsterData, npc.level, npc.health = 30354, 4, 100
     98 			npc.combatProfile = gamedata.NPCCombatProfiles[30354][4]
     99 			if err := world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:]); err != nil {
    100 				t.Fatal(err)
    101 			}
    102 			world.advanceCombat(player.nextAttack)
    103 			next := npc.nextAttack
    104 			conn.packets = nil
    105 			switch reason {
    106 			case "disconnect":
    107 				delete(world.clients, player.id)
    108 			case "loading":
    109 				world.clients[player.id].loading = true
    110 			case "dead":
    111 				player.health = 0
    112 			case "zone":
    113 				player.playfield = 705
    114 			}
    115 
    116 			world.advanceCombat(next.Add(250 * time.Millisecond))
    117 
    118 			if npc.fighting != 0 || world.fighting[npc.id] != nil || !npc.nextAttack.IsZero() {
    119 				t.Fatal("unavailable target retained fixed NPC combat", npc.fighting, npc.nextAttack)
    120 			}
    121 		})
    122 	}
    123 }