aorecomp

Anarchy Online classic in the browser

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


server/weapon_special_animation_test.go (6077B)


      1 package main
      2 
      3 import (
      4 	"bytes"
      5 	"encoding/binary"
      6 	"testing"
      7 	"time"
      8 )
      9 
     10 func TestWeaponSpecialQueuesActionBeforeResultAndLockOnlyForVisibleClients(t *testing.T) {
     11 	for _, name := range []string{"fast", "fling"} {
     12 		t.Run(name, func(t *testing.T) {
     13 			var world *server
     14 			var owner *combatTestConn
     15 			var request []byte
     16 			if name == "fast" {
     17 				world, owner = newFastAttackWorld(t)
     18 				request = fastAttackRequest(1, 1000000)
     19 			} else {
     20 				world, owner = newRangedWorld(t)
     21 				request = flingShotRequest(1, 1000000)
     22 			}
     23 			if err := world.handleCombat(world.clients[1], attackPacket(1, 1000000)[20:]); err != nil {
     24 				t.Fatal(err)
     25 			}
     26 			owner.packets = nil
     27 			observers := make([]*combatTestConn, 5)
     28 			for index := range observers {
     29 				id := uint32(index + 2)
     30 				observers[index] = &combatTestConn{}
     31 				world.characters[id] = &character{id: id, playfield: 950, health: 100}
     32 				world.clients[id] = &worldClient{id: id, conn: observers[index], ready: true, target: true, players: map[uint32]bool{1: true}, npcs: map[uint32]bool{1000000: true}}
     33 			}
     34 			world.clients[3].loading = true
     35 			delete(world.clients[4].npcs, 1000000)
     36 			world.characters[5].playfield = 705
     37 			world.clients[6].ready = false
     38 			expected := targetPacket(append([]byte(nil), request...))
     39 
     40 			for range 2 {
     41 				if err := world.handleWeaponSpecial(world.clients[1], request[20:]); err != nil {
     42 					t.Fatal(err)
     43 				}
     44 			}
     45 
     46 			for _, output := range []*combatTestConn{owner, observers[0], observers[1]} {
     47 				if len(output.packets) < 2 || !bytes.Equal(output.packets[0][16:], expected[16:]) ||
     48 					binary.BigEndian.Uint32(output.packets[1][16:20]) != 0x754f1115 {
     49 					t.Fatalf("missing ordered special action and result: %x", output.packets)
     50 				}
     51 				actions := 0
     52 				for _, packet := range output.packets {
     53 					if binary.BigEndian.Uint32(packet[16:20]) == 0x51492120 {
     54 						actions++
     55 					}
     56 				}
     57 				if actions != 1 {
     58 					t.Fatal("duplicate request replayed a special action")
     59 				}
     60 			}
     61 			if binary.BigEndian.Uint32(owner.packets[2][29:33]) != 170 {
     62 				t.Fatal("skill lock preceded the special action")
     63 			}
     64 			for _, output := range observers[2:] {
     65 				if len(output.packets) != 0 {
     66 					t.Fatalf("special reached an unavailable observer: %x", output.packets)
     67 				}
     68 			}
     69 		})
     70 	}
     71 }
     72 
     73 func TestWeaponSpecialRecoveryWaitsForBothCombatantsToStop(t *testing.T) {
     74 	for _, name := range []string{"fast", "fling"} {
     75 		t.Run(name, func(t *testing.T) {
     76 			var world *server
     77 			var owner *combatTestConn
     78 			var request []byte
     79 			if name == "fast" {
     80 				world, owner = newFastAttackWorld(t)
     81 				request = fastAttackRequest(1, 1000000)
     82 			} else {
     83 				world, owner = newRangedWorld(t)
     84 				request = flingShotRequest(1, 1000000)
     85 			}
     86 			player, target := world.characters[1], world.characters[1000000]
     87 			target.health = 80
     88 			start := time.Now()
     89 			world.clients[1].regenerationAt = start.Add(time.Hour)
     90 			observer := &combatTestConn{}
     91 			world.characters[2] = &character{id: 2, playfield: 950, health: 100}
     92 			world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true, npcs: map[uint32]bool{1000000: true}, regenerationAt: start.Add(time.Hour)}
     93 			if err := world.handleCombat(world.clients[1], attackPacket(1, target.id)[20:]); err != nil {
     94 				t.Fatal(err)
     95 			}
     96 			world.advanceRegeneration(start)
     97 
     98 			if err := world.handleWeaponSpecial(world.clients[1], request[20:]); err != nil {
     99 				t.Fatal(err)
    100 			}
    101 			damaged := target.health
    102 			owner.packets, observer.packets = nil, nil
    103 			if err := world.handleCombat(world.clients[1], stopFightPacket(1)[20:]); err != nil {
    104 				t.Fatal(err)
    105 			}
    106 			world.advanceRegeneration(start.Add(2 * time.Second))
    107 			if target.health != damaged || target.fighting != 1 || player.fighting != 0 || len(owner.packets) != 1 || len(observer.packets) != 1 {
    108 				t.Fatal("stopping the special attacker corrected HP during NPC retaliation")
    109 			}
    110 			player.position = [3]float32{100, 0, 0}
    111 			world.advanceCombat(start.Add(3 * time.Second))
    112 			world.advanceRegeneration(start.Add(4 * time.Second))
    113 			world.advanceRegeneration(start.Add(4 * time.Second))
    114 
    115 			if damaged >= 80 || target.health != damaged+1 || target.fighting != 0 || target.regenerationSeconds != 4 {
    116 				t.Fatalf("special recovery changed damage or recovery phase: damaged=%d target=%+v", damaged, target)
    117 			}
    118 			for _, output := range []*combatTestConn{owner, observer} {
    119 				if len(output.packets) != 3 ||
    120 					binary.BigEndian.Uint32(output.packets[0][16:20]) != 0x4a41203e || binary.BigEndian.Uint32(output.packets[0][24:28]) != 1 ||
    121 					binary.BigEndian.Uint32(output.packets[1][16:20]) != 0x4a41203e || binary.BigEndian.Uint32(output.packets[1][24:28]) != target.id ||
    122 					binary.BigEndian.Uint32(output.packets[2][16:20]) != 0x3710256c || binary.BigEndian.Uint32(output.packets[2][24:28]) != target.id ||
    123 					binary.BigEndian.Uint32(output.packets[2][29:33]) != damaged+1 || !bytes.Equal(output.packets[2][33:45], make([]byte, 12)) {
    124 					t.Fatalf("expected both stops before the quiet HP correction: %x", output.packets)
    125 				}
    126 			}
    127 		})
    128 	}
    129 }
    130 
    131 func TestWeaponSpecialDoesNotQueueAnActionForAnotherCombatTarget(t *testing.T) {
    132 	world, output := newFastAttackWorld(t)
    133 	player := world.characters[1]
    134 	world.characters[1000001] = &character{id: 1000001, npc: true, appearance: 229, playfield: 950, health: 100}
    135 	world.clients[1].npcs[1000001] = true
    136 	if err := world.handleCombat(world.clients[1], attackPacket(1, 1000001)[20:]); err != nil {
    137 		t.Fatal(err)
    138 	}
    139 	attack, recharge := player.nextAttack, player.rechargeUntil
    140 	output.packets = nil
    141 
    142 	err := world.handleWeaponSpecial(world.clients[1], fastAttackRequest(1, 1000000)[20:])
    143 
    144 	if err != nil || world.characters[1000000].health >= 1000 || player.fighting != 1000001 ||
    145 		player.nextAttack != attack || player.rechargeUntil != recharge {
    146 		t.Fatal("special changed the existing fight", err)
    147 	}
    148 	for _, packet := range output.packets {
    149 		if binary.BigEndian.Uint32(packet[16:20]) == 0x51492120 {
    150 			t.Fatal("special queued an action against the wrong native fight target")
    151 		}
    152 	}
    153 }