aorecomp

Anarchy Online classic in the browser

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


server/npc_threat_test.go (6424B)


      1 package main
      2 
      3 import (
      4 	"encoding/binary"
      5 	"testing"
      6 	"time"
      7 )
      8 
      9 func newThreatWorld(t *testing.T) (*server, *combatTestConn) {
     10 	t.Helper()
     11 	world, conn := newCombatWorld(t)
     12 	world.characters[2] = &character{id: 2, playfield: 950, health: 100, stats: testCharacterStats()}
     13 	world.clients[1].players = map[uint32]bool{2: true}
     14 	world.clients[2] = &worldClient{id: 2, conn: &combatTestConn{}, ready: true, players: map[uint32]bool{1: true}, npcs: map[uint32]bool{1000000: true}}
     15 	return world, conn
     16 }
     17 
     18 func TestNPCSwitchesToHigherDamageWithoutResettingItsAttackClock(t *testing.T) {
     19 	world, conn := newThreatWorld(t)
     20 	npc := world.characters[1000000]
     21 	world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:])
     22 	first := world.characters[1].nextAttack
     23 	world.advanceCombat(first)
     24 	world.handleCombat(world.clients[1], stopFightPacket(1)[20:])
     25 	next := npc.nextAttack
     26 	world.handleCombat(world.clients[2], attackPacket(2, npc.id)[20:])
     27 	second := world.characters[2].nextAttack
     28 	world.advanceCombat(second)
     29 	if npc.fighting != 1 {
     30 		t.Fatal("equal damage must retain the current target")
     31 	}
     32 	// Give the second player another swing while the NPC is in its recharge.
     33 	world.characters[2].nextAttack = second.Add(time.Millisecond)
     34 	conn.packets = nil
     35 
     36 	world.advanceCombat(second.Add(time.Millisecond))
     37 
     38 	if npc.fighting != 2 || npc.nextAttack != next {
     39 		t.Fatalf("higher damage did not switch targets without resetting the clock: target=%d clock=%v want=%v", npc.fighting, npc.nextAttack, next)
     40 	}
     41 	if len(conn.packets) != 3 || binary.BigEndian.Uint32(conn.packets[1][16:20]) != 0x4a41203e ||
     42 		binary.BigEndian.Uint32(conn.packets[2][16:20]) != 0x28494070 || binary.BigEndian.Uint32(conn.packets[2][33:37]) != 2 {
     43 		t.Fatal("retarget must drain queued native hits with StopFight before Attack")
     44 	}
     45 }
     46 
     47 func TestNPCThreatIsIndividualEvenWhenDamageCreditBelongsToATeam(t *testing.T) {
     48 	world, _ := newThreatWorld(t)
     49 	npc := world.characters[1000000]
     50 	team := &playerTeam{id: 7}
     51 	world.characters[1].team, world.characters[2].team = team, team
     52 	world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:])
     53 	world.advanceCombat(world.characters[1].nextAttack)
     54 	world.handleCombat(world.clients[1], stopFightPacket(1)[20:])
     55 	world.handleCombat(world.clients[2], attackPacket(2, npc.id)[20:])
     56 	world.advanceCombat(world.characters[2].nextAttack)
     57 	world.characters[2].nextAttack = npc.nextAttack.Add(-time.Nanosecond)
     58 
     59 	world.advanceCombat(world.characters[2].nextAttack)
     60 
     61 	if npc.fighting != 2 || npc.threat[1] != 5 || npc.threat[2] != 10 || len(npc.damageContributors) != 1 ||
     62 		npc.damageContributors[damageContributor{team: 7}] != 15 || npc.leadingContributor != (damageContributor{team: 7}) {
     63 		t.Fatal("team reward credit replaced individual target selection", npc.threat, npc.damageContributors)
     64 	}
     65 }
     66 
     67 func TestNPCChoosesAnEligibleParticipantWhenItsTargetBecomesUnavailable(t *testing.T) {
     68 	for _, scenario := range []string{"offline", "dead", "other zone", "loading", "not ready", "no interest", "missing actor"} {
     69 		t.Run(scenario, func(t *testing.T) {
     70 			world, _ := newThreatWorld(t)
     71 			npc := world.characters[1000000]
     72 			world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:])
     73 			world.advanceCombat(world.characters[1].nextAttack)
     74 			world.handleCombat(world.clients[1], stopFightPacket(1)[20:])
     75 			world.handleCombat(world.clients[2], attackPacket(2, npc.id)[20:])
     76 			world.advanceCombat(world.characters[2].nextAttack)
     77 			clock := npc.nextAttack
     78 			switch scenario {
     79 			case "offline":
     80 				delete(world.clients, 1)
     81 			case "dead":
     82 				world.characters[1].health = 0
     83 			case "other zone":
     84 				world.characters[1].playfield = 705
     85 			case "loading":
     86 				world.clients[1].loading = true
     87 			case "not ready":
     88 				world.clients[1].ready = false
     89 			case "no interest":
     90 				delete(world.clients[1].npcs, npc.id)
     91 			case "missing actor":
     92 				delete(world.clients, 1)
     93 				delete(world.characters, 1)
     94 			}
     95 
     96 			world.advanceCombat(clock.Add(-time.Nanosecond))
     97 
     98 			if npc.fighting != 2 || npc.nextAttack != clock {
     99 				t.Fatal("an ineligible target blocked a participant or reset the swing clock")
    100 			}
    101 		})
    102 	}
    103 }
    104 
    105 func TestNPCRemovesZonedPlayersFromThreatButKeepsTheirDamageCredit(t *testing.T) {
    106 	t.Chdir(t.TempDir())
    107 	world, _ := newThreatWorld(t)
    108 	npc := world.characters[1000000]
    109 	world.handleCombat(world.clients[1], attackPacket(1, npc.id)[20:])
    110 	world.advanceCombat(world.characters[1].nextAttack)
    111 	world.handleCombat(world.clients[1], stopFightPacket(1)[20:])
    112 	world.handleCombat(world.clients[2], attackPacket(2, npc.id)[20:])
    113 	world.advanceCombat(world.characters[2].nextAttack)
    114 	destination := world.characters[1].userData()
    115 	destination.Playfield = 705
    116 
    117 	err := world.updateUser(destination)
    118 
    119 	if err != nil || npc.fighting != 2 || len(npc.threat) != 1 || npc.threat[2] != 5 ||
    120 		npc.damageContributors[damageContributor{player: 1}] != 5 || npc.damageContributors[damageContributor{player: 2}] != 5 {
    121 		t.Fatal("zoning retained threat or removed lifetime reward credit", err, npc.threat)
    122 	}
    123 }
    124 
    125 func TestNPCDisconnectRemovesThreatAndRetargetsTheRemainingConnection(t *testing.T) {
    126 	world, _ := newThreatWorld(t)
    127 	delete(world.clients, 1)
    128 	startTestService(t, world)
    129 	peer := connectZoningClient(t, world, false)
    130 	clock := time.Now().Add(10 * time.Second)
    131 	callTestZone(t, world, 1, zoneTestCommand(func(state *server) error {
    132 		npc := state.characters[1000000]
    133 		npc.threat = map[uint32]int64{1: 10, 2: 5}
    134 		npc.damageContributors = map[damageContributor]uint32{{player: 1}: 10, {player: 2}: 5}
    135 		state.combatPresence(npc, 1)
    136 		npc.fighting, npc.nextAttack = 1, clock
    137 		state.fighting[npc.id] = npc
    138 		return nil
    139 	}))
    140 
    141 	peer.Close()
    142 	deadline := time.Now().Add(time.Second)
    143 	for {
    144 		var disconnected, retained bool
    145 		callTestZone(t, world, 1, zoneTestCommand(func(state *server) error {
    146 			npc := state.characters[1000000]
    147 			disconnected = state.clients[1] == nil
    148 			retained = npc.fighting == 2 && npc.nextAttack == clock && len(npc.threat) == 1 && npc.threat[2] == 5 &&
    149 				npc.damageContributors[damageContributor{player: 1}] == 10 && npc.damageContributors[damageContributor{player: 2}] == 5
    150 			return nil
    151 		}))
    152 		if disconnected {
    153 			if !retained {
    154 				t.Fatal("actual disconnect retained threat, lost credit, or failed to retarget with its existing clock")
    155 			}
    156 			break
    157 		}
    158 		if time.Now().After(deadline) {
    159 			t.Fatal("session did not disconnect")
    160 		}
    161 		time.Sleep(time.Millisecond)
    162 	}
    163 }