aorecomp

Anarchy Online classic in the browser

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


server/npc_combat_test.go (6953B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"encoding/hex"
      7 	"fmt"
      8 	"net"
      9 	"testing"
     10 	"time"
     11 )
     12 
     13 func TestNPCProfileNativeSpawnUsesLeetBiteAfterRestart(t *testing.T) {
     14 	for _, level := range []uint16{1, 2, 6, 10} {
     15 		t.Run(fmt.Sprint(level), func(t *testing.T) {
     16 			data := testNPCData()
     17 			data.Playfields[0].Districts[0].Groups[0].AppearanceStatus = "inferred_species"
     18 			data.Playfields[0].Districts[0].Groups[0].Levels = [2]uint16{level, level}
     19 			useNPCData(t, data)
     20 			world := newTestServer(t)
     21 			if err := world.saveUser(world.newCharacter()); err != nil {
     22 				t.Fatal(err)
     23 			}
     24 
     25 			for range 2 {
     26 				shutdown := startTestService(t, world)
     27 				client, connection := net.Pipe()
     28 				client.SetDeadline(time.Now().Add(5 * time.Second))
     29 				done := make(chan error, 1)
     30 				go func() { defer connection.Close(); done <- world.serveWorldConnection(connection) }()
     31 				cookie, _ := hex.DecodeString("dfdf00010001002000000001000000020000001b000000010000000000000000")
     32 				client.Write(cookie)
     33 				var weapon []byte
     34 				spawned := false
     35 				for {
     36 					packet, err := readPacket(client)
     37 					if err != nil {
     38 						client.Close()
     39 						t.Fatal(err)
     40 					}
     41 					key := binary.BigEndian.Uint32(packet[16:20])
     42 					if key == 0x271b3a6b && binary.BigEndian.Uint32(packet[24:28]) == 1000000 {
     43 						spawned = true
     44 						end := int(binary.BigEndian.Uint16(packet[6:8]))
     45 						expected := map[uint16]string{
     46 							1: "000007e20001d84e0001d84f0000006400000001", 2: "000007e20001d84e0001d84f0000006400000002",
     47 							6: "000007e20001d84e0001d84f0000006400000006", 10: "000007e20001d84e0001d84f000000640000000a",
     48 						}[level]
     49 						if hex.EncodeToString(packet[end-20:end]) != expected {
     50 							client.Close()
     51 							t.Fatalf("initial native Leet Bite weapon: %x", packet[end-20:end])
     52 						}
     53 					}
     54 					if key == 0x1d3c0f1c {
     55 						if !spawned {
     56 							client.Close()
     57 							t.Fatal("weapon arrived before its NPC")
     58 						}
     59 						weapon = packet
     60 					}
     61 					if key == 0x5f52412e {
     62 						break
     63 					}
     64 				}
     65 				client.Close()
     66 				if err := <-done; err != nil {
     67 					t.Fatal(err)
     68 				}
     69 
     70 				expected := map[uint16]string{
     71 					1:  "000007e20001d84e0001d84f0000006400000001000000000000000000000000000000000000004b",
     72 					2:  "000007e20001d84e0001d84f0000006400000002000000000000000000000000000000000000004b",
     73 					6:  "000007e20001d84e0001d84f0000006400000006000000000000000000000000000000000000004b",
     74 					10: "000007e20001d84e0001d84f000000640000000a000000000000000000000000000000000000004b",
     75 				}[level]
     76 				if len(weapon) == 0 || binary.BigEndian.Uint32(weapon[24:28]) != 1000000 ||
     77 					hex.EncodeToString(weapon[29:69]) != expected {
     78 					t.Fatalf("native Leet Bite profile: %x", weapon)
     79 				}
     80 				var health uint32
     81 				callTestZone(t, world, 1, zoneTestCommand(func(state *server) error {
     82 					health = state.characters[1000000].health
     83 					return nil
     84 				}))
     85 				if health != map[uint16]uint32{1: 20, 2: 20, 6: 60, 10: 100}[level] {
     86 					t.Fatal("profile did not initialize the NPC's health")
     87 				}
     88 				if err := shutdown(); err != nil {
     89 					t.Fatal(err)
     90 				}
     91 				world = newServerForTest(t)
     92 			}
     93 		})
     94 	}
     95 }
     96 
     97 func TestNPCProfileCombatUsesCatalogTimingAndDamageForBothObservers(t *testing.T) {
     98 	data := testNPCData()
     99 	data.Playfields[0].Districts[0].Groups[0].AppearanceStatus = "inferred_species"
    100 	data.Playfields[0].Districts[0].Groups[0].Levels = [2]uint16{1, 1}
    101 	useNPCData(t, data)
    102 	world := newTestServer(t)
    103 	player := world.newCharacter()
    104 	observer := world.newCharacter()
    105 	first, second := &combatTestConn{}, &combatTestConn{}
    106 	world.clients[player.id] = &worldClient{id: player.id, conn: first, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
    107 	world.clients[observer.id] = &worldClient{id: observer.id, conn: second, target: true, ready: true, players: map[uint32]bool{player.id: true}, npcs: map[uint32]bool{1000000: true}}
    108 	npc := world.characters[1000000]
    109 
    110 	if err := world.handleCombat(world.clients[player.id], attackPacket(player.id, npc.id)[20:]); err != nil {
    111 		t.Fatal(err)
    112 	}
    113 	start := player.nextAttack
    114 	world.advanceCombat(start)
    115 	if npc.nextAttack != start.Add(1750*time.Millisecond) {
    116 		t.Fatalf("Leet Bite attack deadline: %v", npc.nextAttack.Sub(start))
    117 	}
    118 	world.advanceCombat(start.Add(1750*time.Millisecond - time.Nanosecond))
    119 	if player.health != 34 {
    120 		t.Fatal("Leet Bite landed before its attack phase ended")
    121 	}
    122 	world.advanceCombat(start.Add(1750 * time.Millisecond))
    123 	if npc.nextAttack != start.Add(5250*time.Millisecond) {
    124 		t.Fatalf("Leet Bite recharge and next attack: %v", npc.nextAttack.Sub(start))
    125 	}
    126 	world.advanceCombat(start.Add(5250 * time.Millisecond))
    127 
    128 	var damage []uint32
    129 	for index, packet := range first.packets {
    130 		if len(second.packets) <= index || !bytes.Equal(packet[16:], second.packets[index][16:]) {
    131 			t.Fatal("observers received different combat results")
    132 		}
    133 		if binary.BigEndian.Uint32(packet[16:20]) == 0x46002f16 && binary.BigEndian.Uint32(packet[24:28]) == npc.id {
    134 			damage = append(damage, binary.BigEndian.Uint32(packet[29:33]))
    135 			if damage[len(damage)-1] < 4 || damage[len(damage)-1] > 8 || binary.BigEndian.Uint32(packet[37:41]) != 0 {
    136 				t.Fatalf("Leet Bite damage or native slot: %x", packet)
    137 			}
    138 		}
    139 	}
    140 	if len(damage) != 2 || len(second.packets) != len(first.packets) || player.health != 34-damage[0]-damage[1] {
    141 		t.Fatalf("Leet Bite results: %v, health %d", damage, player.health)
    142 	}
    143 }
    144 
    145 func TestNPCProfileDoesNotApplyToOtherLevelsOrSpecies(t *testing.T) {
    146 	for _, scenario := range []struct {
    147 		name    string
    148 		monster uint32
    149 		level   uint16
    150 	}{
    151 		{"other level", 17655, 11},
    152 		{"extended level", 17655, 300},
    153 		{"other species", 17627, 1},
    154 	} {
    155 		t.Run(scenario.name, func(t *testing.T) {
    156 			data := testNPCData()
    157 			data.Playfields[0].Districts[0].Groups[0].AppearanceStatus = "inferred_species"
    158 			data.Playfields[0].Districts[0].Groups[0].MonsterData = scenario.monster
    159 			data.Playfields[0].Districts[0].Groups[0].Levels = [2]uint16{scenario.level, scenario.level}
    160 			useNPCData(t, data)
    161 			world := newTestServer(t)
    162 			player := world.newCharacter()
    163 			conn := &combatTestConn{}
    164 			world.clients[player.id] = &worldClient{id: player.id, conn: conn, target: true, ready: true, npcs: map[uint32]bool{1000000: true}}
    165 
    166 			world.handleCombat(world.clients[player.id], attackPacket(player.id, 1000000)[20:])
    167 			start := player.nextAttack
    168 			world.advanceCombat(start)
    169 			world.advanceCombat(start.Add(time.Second))
    170 			world.clients[player.id].send(characterPacket(world.characters[1000000]))
    171 
    172 			if player.health != 29 || world.characters[1000000].nextAttack != start.Add(3*time.Second) {
    173 				t.Fatal("a Leet profile changed an unlisted creature's prototype behavior")
    174 			}
    175 			packet := conn.packets[len(conn.packets)-1]
    176 			end := int(binary.BigEndian.Uint16(packet[6:8]))
    177 			if hex.EncodeToString(packet[end-20:end]) != "000007e20000aac00000aac10000006400000001" {
    178 				t.Fatal("an unlisted creature received the Leet Bite template")
    179 			}
    180 		})
    181 	}
    182 }