aorecomp

Anarchy Online classic in the browser

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


server/npc_regeneration_test.go (12892B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"bytes"
      6 	"encoding/binary"
      7 	"fmt"
      8 	"testing"
      9 	"time"
     10 )
     11 
     12 func newNPCRegenerationWorld(t *testing.T) (*server, *combatTestConn, time.Time) {
     13 	t.Helper()
     14 	world, conn := newCombatWorld(t)
     15 	world.characters[1000000].monsterData = 17655
     16 	world.characters[1000000].level = 1
     17 	// Leave room for recovery independently of production encounter health.
     18 	profile := *gamedata.NPCCombatProfiles[17655][1]
     19 	profile.Health = 100
     20 	world.characters[1000000].combatProfile = &profile
     21 	world.characters[1000000].health = 20
     22 	start := time.Unix(100, 0)
     23 	// Keep player recovery out of the NPC packet assertions.
     24 	world.clients[1].regenerationAt = start.Add(time.Hour)
     25 	return world, conn, start
     26 }
     27 
     28 func TestNPCRegenerationUsesOneIdleCheckDespiteJitterAndBacklogs(t *testing.T) {
     29 	world, _, start := newNPCRegenerationWorld(t)
     30 
     31 	world.advanceRegeneration(start)
     32 	world.advanceRegeneration(start.Add(2*time.Second - time.Nanosecond))
     33 	if world.characters[1000000].health != 20 {
     34 		t.Fatal("recovered before the two-second check")
     35 	}
     36 	world.advanceRegeneration(start.Add(2200 * time.Millisecond))
     37 	world.advanceRegeneration(start.Add(2200 * time.Millisecond))
     38 	world.advanceRegeneration(start.Add(4 * time.Second))
     39 	world.advanceRegeneration(start.Add(time.Hour))
     40 	world.advanceRegeneration(start.Add(time.Hour))
     41 
     42 	if world.characters[1000000].health != 23 || world.characters[1000000].regenerationSeconds != 6 || world.characters[1000000].regenerationAt != start.Add(time.Hour) {
     43 		t.Fatalf("replayed or shifted recovery: %+v", world.characters[1000000])
     44 	}
     45 }
     46 
     47 func TestNPCRegenerationUsesNativeDefaultsWithoutASpeciesOrLevelGate(t *testing.T) {
     48 	for _, scenario := range []struct {
     49 		monster uint32
     50 		level   uint16
     51 	}{{30365, 1}, {17655, 7}, {30365, 300}} {
     52 		if !t.Run(fmt.Sprintf("monster%d/level%d", scenario.monster, scenario.level), func(t *testing.T) {
     53 			world, _, start := newNPCRegenerationWorld(t)
     54 			npc := world.characters[1000000]
     55 			npc.monsterData, npc.level, npc.combatProfile = scenario.monster, scenario.level, nil
     56 
     57 			world.advanceRegeneration(start)
     58 			world.advanceRegeneration(start.Add(2 * time.Second))
     59 			if npc.health != 21 {
     60 				t.Fatalf("unprofiled NPC %d level %d did not recover while idle: %d", scenario.monster, scenario.level, npc.health)
     61 			}
     62 			npc.fighting = 1
     63 			for second := 4; second <= 60; second += 2 {
     64 				world.advanceRegeneration(start.Add(time.Duration(second) * time.Second))
     65 			}
     66 
     67 			if npc.health != 24 || npc.nano != 0 || npc.regenerationSeconds != 60 {
     68 				t.Fatalf("unprofiled NPC %d level %d did not retain the native combat recovery phase: %+v", scenario.monster, scenario.level, npc)
     69 			}
     70 		}) {
     71 			return
     72 		}
     73 	}
     74 }
     75 
     76 func TestNPCRegenerationUsesPeriodicWorldCheckpoint(t *testing.T) {
     77 	world, _ := newRangedWorld(t)
     78 	npc := world.characters[1000000]
     79 	npc.monsterData, npc.level, npc.combatProfile, npc.health = 30365, 1, nil, 87
     80 	start := time.Now()
     81 	world.clients[1].regenerationAt = start.Add(time.Hour)
     82 	if err := world.flushWorld(); err != nil {
     83 		t.Fatal(err)
     84 	}
     85 
     86 	world.advanceRegeneration(start)
     87 	world.advanceRegeneration(start.Add(2 * time.Second))
     88 	var before uint32
     89 	if err := world.db.QueryRow("SELECT health FROM characters WHERE id=1000000").Scan(&before); err != nil {
     90 		t.Fatal(err)
     91 	}
     92 	if err := world.checkpointWorld(start.Add(time.Minute)); err != nil {
     93 		t.Fatal(err)
     94 	}
     95 	restored := newTestInventoryServer(t)
     96 
     97 	if before != 87 || npc.health != 88 || restored.characters[1000000].health != 88 {
     98 		t.Fatalf("recovery did not wait for its checkpoint: saved=%d live=%d restored=%d", before, npc.health, restored.characters[1000000].health)
     99 	}
    100 }
    101 
    102 func TestNPCRegenerationCombatUsesNativeDeltaAndCounter(t *testing.T) {
    103 	world, conn, start := newNPCRegenerationWorld(t)
    104 	world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
    105 	world.characters[1000000].fighting = 1
    106 	world.fighting[1000000] = world.characters[1000000]
    107 	conn.packets = nil
    108 
    109 	world.advanceRegeneration(start)
    110 	for second := 2; second <= 58; second += 2 {
    111 		world.advanceRegeneration(start.Add(time.Duration(second) * time.Second))
    112 	}
    113 	if world.characters[1000000].health != 20 {
    114 		t.Fatal("combat health recovered before counter 60")
    115 	}
    116 	if len(conn.packets) != 0 {
    117 		t.Fatal("unchanged combat health published a correction")
    118 	}
    119 	world.advanceRegeneration(start.Add(60 * time.Second))
    120 
    121 	if world.characters[1000000].health != 23 || len(conn.packets) != 1 ||
    122 		binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x3710256c || binary.BigEndian.Uint32(conn.packets[0][29:33]) != 23 {
    123 		t.Fatalf("combat recovery was not published: hp=%d packets=%x", world.characters[1000000].health, conn.packets)
    124 	}
    125 }
    126 
    127 func TestNPCRegenerationDisengagementDoesNotHealTwiceOrResetPhase(t *testing.T) {
    128 	world, conn, start := newNPCRegenerationWorld(t)
    129 	world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
    130 	world.characters[1000000].fighting = 1
    131 	world.fighting[1000000] = world.characters[1000000]
    132 	world.advanceRegeneration(start)
    133 	world.advanceRegeneration(start.Add(2 * time.Second))
    134 	world.characters[1].health = 0
    135 	conn.packets = nil
    136 
    137 	world.advanceCombat(start.Add(3 * time.Second))
    138 	world.advanceRegeneration(start.Add(3 * time.Second))
    139 	if world.characters[1000000].health != 20 || world.characters[1000000].fighting != 0 || len(conn.packets) != 2 {
    140 		t.Fatal("disengagement healed immediately or failed to stop both actors")
    141 	}
    142 	world.advanceRegeneration(start.Add(4 * time.Second))
    143 	world.advanceRegeneration(start.Add(4 * time.Second))
    144 
    145 	if world.characters[1000000].health != 21 || world.characters[1000000].regenerationSeconds != 4 || len(conn.packets) != 3 || binary.BigEndian.Uint32(conn.packets[2][16:20]) != 0x3710256c {
    146 		t.Fatalf("disengagement recovery: hp=%d packets=%x", world.characters[1000000].health, conn.packets)
    147 	}
    148 }
    149 
    150 func TestNPCRegenerationSendsQuietHealthOnlyToReadyObserversWithInterest(t *testing.T) {
    151 	world, first, start := newNPCRegenerationWorld(t)
    152 	observers := make([]*combatTestConn, 5)
    153 	for i := range observers {
    154 		id := uint32(i + 2)
    155 		observers[i] = &combatTestConn{}
    156 		world.characters[id] = &character{id: id, playfield: 950}
    157 		world.clients[id] = &worldClient{id: id, conn: observers[i], target: true, ready: true, npcs: map[uint32]bool{1000000: true}, regenerationAt: start.Add(time.Hour)}
    158 	}
    159 	world.clients[3].loading = true
    160 	world.clients[4].ready = false
    161 	world.characters[5].playfield = 705
    162 	delete(world.clients[6].npcs, 1000000)
    163 
    164 	world.advanceRegeneration(start)
    165 	world.advanceRegeneration(start.Add(2 * time.Second))
    166 
    167 	if len(first.packets) != 1 || len(observers[0].packets) != 1 || !bytes.Equal(first.packets[0][16:], observers[0].packets[0][16:]) || !bytes.Equal(first.packets[0][29:45], []byte{0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) || binary.BigEndian.Uint32(first.packets[0][24:28]) != 1000000 {
    168 		t.Fatalf("quiet correction layout: %x", first.packets)
    169 	}
    170 	if len(observers[1].packets) != 1 || !bytes.Equal(first.packets[0][16:], observers[1].packets[0][16:]) {
    171 		t.Fatal("loading observer lost a correction after its actor snapshot")
    172 	}
    173 	for _, conn := range observers[2:] {
    174 		if len(conn.packets) != 0 {
    175 			t.Fatalf("correction leaked outside interest: %x", conn.packets)
    176 		}
    177 	}
    178 }
    179 
    180 func TestNPCRegenerationPublishesRecoveryDuringIncomingWeaponQueue(t *testing.T) {
    181 	world, conn, start := newNPCRegenerationWorld(t)
    182 	world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:])
    183 	conn.packets = nil
    184 
    185 	world.advanceRegeneration(start)
    186 	world.advanceRegeneration(start.Add(2 * time.Second))
    187 
    188 	if world.characters[1000000].health != 21 || len(conn.packets) != 1 ||
    189 		binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x3710256c || binary.BigEndian.Uint32(conn.packets[0][29:33]) != 21 {
    190 		t.Fatalf("incoming-only recovery was not published: hp=%d packets=%x", world.characters[1000000].health, conn.packets)
    191 	}
    192 }
    193 
    194 func TestNPCRegenerationRetainsBothAttackClocksWhenPublishingRecovery(t *testing.T) {
    195 	world, conn, start := newNPCRegenerationWorld(t)
    196 	peer := &combatTestConn{}
    197 	world.characters[2] = &character{id: 2, playfield: 950, health: 100}
    198 	world.clients[2] = &worldClient{id: 2, conn: peer, target: true, ready: true,
    199 		npcs: map[uint32]bool{1000000: true}, regenerationAt: start.Add(time.Hour)}
    200 	if err := world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]); err != nil {
    201 		t.Fatal(err)
    202 	}
    203 	player, npc := world.characters[1], world.characters[1000000]
    204 	world.advanceCombat(player.nextAttack)
    205 	world.advanceRegeneration(start)
    206 	npc.regenerationSeconds = 58
    207 	clocks := [4]time.Time{player.nextAttack, player.rechargeUntil, npc.nextAttack, npc.rechargeUntil}
    208 	conn.packets, peer.packets = nil, nil
    209 
    210 	world.advanceRegeneration(start.Add(2 * time.Second))
    211 	world.advanceRegeneration(start.Add(2 * time.Second))
    212 
    213 	if npc.health != 18 || npc.regenerationSeconds != 60 || player.fighting != npc.id || npc.fighting != 1 ||
    214 		clocks != [4]time.Time{player.nextAttack, player.rechargeUntil, npc.nextAttack, npc.rechargeUntil} {
    215 		t.Fatal("recovery changed its amount/phase, engagement or attack clocks")
    216 	}
    217 	if len(conn.packets) != 1 || len(peer.packets) != 1 || !bytes.Equal(conn.packets[0][16:], peer.packets[0][16:]) ||
    218 		binary.BigEndian.Uint32(conn.packets[0][16:20]) != 0x3710256c ||
    219 		binary.BigEndian.Uint32(conn.packets[0][24:28]) != npc.id ||
    220 		!bytes.Equal(conn.packets[0][29:45], []byte{0, 0, 0, 18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}) {
    221 		t.Fatal("combat recovery did not publish one zero-feedback snapshot to both observers")
    222 	}
    223 }
    224 
    225 func TestNPCRegenerationClampsNativeIdleGainBoundaries(t *testing.T) {
    226 	for _, scenario := range []struct{ maximum, initial, want uint32 }{
    227 		{100, 99, 100}, {120, 100, 101}, {121, 100, 101}, {239, 100, 101}, {240, 100, 102}, {480, 478, 480},
    228 	} {
    229 		if !t.Run(fmt.Sprintf("max%d/initial%d", scenario.maximum, scenario.initial), func(t *testing.T) {
    230 			world, _, start := newNPCRegenerationWorld(t)
    231 			world.characters[1000000].combatProfile = &gamedata.NPCCombatProfile{Health: scenario.maximum}
    232 			world.characters[1000000].health = scenario.initial
    233 
    234 			world.advanceRegeneration(start)
    235 			world.advanceRegeneration(start.Add(2 * time.Second))
    236 
    237 			if world.characters[1000000].health != scenario.want {
    238 				t.Fatalf("max %d: got %d want %d", scenario.maximum, world.characters[1000000].health, scenario.want)
    239 			}
    240 		}) {
    241 			return
    242 		}
    243 	}
    244 }
    245 
    246 func TestNPCRegenerationSkipsDeadAndPlayerCharacters(t *testing.T) {
    247 	for _, scenario := range []string{"dead", "player"} {
    248 		t.Run(scenario, func(t *testing.T) {
    249 			world, conn, start := newNPCRegenerationWorld(t)
    250 			switch scenario {
    251 			case "dead":
    252 				world.characters[1000000].health = 0
    253 			case "player":
    254 				world.characters[1000000].npc = false
    255 			}
    256 			initial := world.characters[1000000].health
    257 
    258 			world.advanceRegeneration(start)
    259 			world.advanceRegeneration(start.Add(60 * time.Second))
    260 
    261 			if world.characters[1000000].health != initial || world.characters[1000000].nano != 0 || len(conn.packets) != 0 {
    262 				t.Fatalf("unexpected recovery: %+v packets=%x", world.characters[1000000], conn.packets)
    263 			}
    264 		})
    265 	}
    266 }
    267 
    268 func TestNPCRegenerationStaysDormantWithoutObservers(t *testing.T) {
    269 	world, conn, start := newNPCRegenerationWorld(t)
    270 	delete(world.clients, 1)
    271 
    272 	world.advanceRegeneration(start)
    273 	world.advanceRegeneration(start.Add(2 * time.Second))
    274 
    275 	if world.characters[1000000].health != 20 || !world.characters[1000000].regenerationAt.IsZero() || len(conn.packets) != 0 {
    276 		t.Fatal("unobserved NPC advanced recovery")
    277 	}
    278 }
    279 
    280 func TestNPCRegenerationRespawnStartsWithFreshResourcesAndClock(t *testing.T) {
    281 	world, _, start := newNPCRegenerationWorld(t)
    282 	world.advanceRegeneration(start)
    283 	world.advanceRegeneration(start.Add(2 * time.Second))
    284 	world.characters[1000000].health = 0
    285 	world.npcRespawns = []npcRespawn{{npc: world.characters[1000000], at: start.Add(4 * time.Second)}}
    286 	world.nextNPC = 1000001
    287 
    288 	world.advanceNPCRespawns(start.Add(4 * time.Second))
    289 	world.advanceRegeneration(start.Add(4 * time.Second))
    290 
    291 	if world.characters[1000000].health != 0 || world.characters[1000001].health != 100 ||
    292 		world.characters[1000001].regenerationSeconds != 0 || world.characters[1000001].regenerationAt != start.Add(4*time.Second) {
    293 		t.Fatal("replacement inherited the dead life's resources or recovery phase")
    294 	}
    295 }
    296 
    297 func TestNPCRegenerationCompletesRecoveryDuringActivityGracePeriod(t *testing.T) {
    298 	world, _, start := newNPCRegenerationWorld(t)
    299 	npc := world.characters[1000000]
    300 	// Just below the next heal-delta threshold gives the longest idle recovery.
    301 	npc.combatProfile = &gamedata.NPCCombatProfile{Health: 239}
    302 	npc.health = 1
    303 	world.advanceRegeneration(start)
    304 	delete(world.clients, 1)
    305 
    306 	for second := 2; second <= 476; second += 2 {
    307 		world.advanceRegeneration(start.Add(time.Duration(second) * time.Second))
    308 	}
    309 
    310 	if npc.health != 239 {
    311 		t.Fatal("NPC went dormant before finishing idle recovery", npc.health)
    312 	}
    313 }