aorecomp

Anarchy Online classic in the browser

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


server/nano_control_movement_test.go (11244B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/binary"
      6 	"fmt"
      7 	"math"
      8 	"testing"
      9 	"time"
     10 )
     11 
     12 func newControlPursuitWorld(t *testing.T) (*server, *combatTestConn) {
     13 	t.Helper()
     14 	world, conn := newCombatNanoWorld(t, combatNanoFixture{Profession: 11})
     15 	player, target := world.characters[1], world.characters[1000000]
     16 	player.stats.Abilities = characterAbilitiesStats{10, 10, 10, 10, 10, 10}
     17 	player.stats.Speed.NanoInitiative = 5
     18 	player.position = [3]float32{21, 0, 0}
     19 	player.health = player.maximumHealth()
     20 	player.inventory.LearnedNanos = []uint32{42110, 82518, 82528, 82463}
     21 	target.monsterData, target.level = 17655, 1
     22 	target.combatProfile = gamedata.NPCCombatProfiles[17655][1]
     23 	target.motion = &npcMotion{roaming: true, home: target.position, destination: player.position, moving: true}
     24 	target.running, target.fighting = true, 1
     25 	target.nextAttack = time.Now().Add(1750 * time.Millisecond)
     26 	world.fighting[target.id] = target
     27 	world.mobileNPCs = map[uint32][]*character{950: {target}}
     28 	return world, conn
     29 }
     30 
     31 func TestControlRootStopsRoamingPursuitAndReturnUntilExpiry(t *testing.T) {
     32 	for _, mode := range []string{"roaming", "pursuit", "return"} {
     33 		t.Run(mode, func(t *testing.T) {
     34 			world, _ := newControlPursuitWorld(t)
     35 			target := world.characters[1000000]
     36 			target.movement.velocity = [3]float32{4.5, 0, 0}
     37 			target.movement.speed = 4.5
     38 			at := castTestBuff(t, world, 1, target.id, 42110)
     39 			world.lastNPCUpdate = at
     40 			if mode != "pursuit" {
     41 				target.fighting = 0
     42 				delete(world.fighting, target.id)
     43 				target.threat = nil
     44 			}
     45 			if mode == "return" {
     46 				target.motion.home = [3]float32{21, 0, 0}
     47 				target.motion.returning = true
     48 			}
     49 			if mode == "roaming" {
     50 				// Every random destination must be outside the arrival radius.
     51 				target.motion.home = [3]float32{21, 0, 0}
     52 			}
     53 			target.motion.nextMove = at
     54 			before := target.position
     55 
     56 			for tick := 1; tick <= 4; tick++ {
     57 				world.advanceNPCs(at.Add(time.Duration(tick) * 250 * time.Millisecond))
     58 			}
     59 
     60 			if target.position != before || target.motion.moving || !target.movementRestricted() || target.movement.velocity != [3]float32{} || target.movement.speed != 0 {
     61 				t.Fatal("root moved in simulation", mode, target.position, target.motion)
     62 			}
     63 			deadline := target.activeNanos[0].expiresAt
     64 			world.advanceNanos(deadline)
     65 			world.lastNPCUpdate = deadline
     66 			world.advanceNPCs(deadline.Add(250 * time.Millisecond))
     67 			world.advanceNPCs(deadline.Add(500 * time.Millisecond))
     68 			if target.movementRestricted() || target.position == before {
     69 				t.Fatal("expired root did not resume eligible motion", mode, target.position, target.motion)
     70 			}
     71 		})
     72 	}
     73 }
     74 
     75 func TestControlSnareUsesSignedRunningSpeedAndLeavesWalkingUnchanged(t *testing.T) {
     76 	for _, scenario := range []struct {
     77 		program, profession uint32
     78 		position            float64
     79 	}{{82518, 4, 1.2666667}, {82528, 8, 1.28}, {82463, 8, 1.0066667}} {
     80 		for _, returning := range []bool{false, true} {
     81 			t.Run(fmt.Sprintf("%d/return=%t", scenario.program, returning), func(t *testing.T) {
     82 				world, _ := newControlPursuitWorld(t)
     83 				player, target := world.characters[1], world.characters[1000000]
     84 				player.profession = uint16(scenario.profession)
     85 				at := castTestBuff(t, world, 1, target.id, scenario.program)
     86 				world.lastNPCUpdate = at
     87 				if returning {
     88 					target.fighting = 0
     89 					target.threat = nil
     90 					delete(world.fighting, target.id)
     91 					target.motion.home = [3]float32{21, 0, 0}
     92 					target.motion.returning = true
     93 				}
     94 
     95 				world.advanceNPCs(at.Add(250 * time.Millisecond))
     96 
     97 				if math.Abs(float64(target.position[0])-scenario.position) > 0.00001 || !target.running || !target.motion.moving || target.movementRestricted() {
     98 					t.Fatal("snare did not use native signed run scalar/floor", target.position, target.motion)
     99 				}
    100 				target.fighting = 0
    101 				target.threat = nil
    102 				delete(world.fighting, target.id)
    103 				target.motion.returning = false
    104 				target.running = false
    105 				target.movement.velocity = [3]float32{1.5, 0, 0}
    106 				target.motion.destination = [3]float32{21, 0, 0}
    107 				target.motion.nextMove = at
    108 				before := target.position[0]
    109 				world.advanceNPCs(at.Add(500 * time.Millisecond))
    110 				if math.Abs(float64(target.position[0]-before)-0.375) > 0.00001 {
    111 					t.Fatal("snare changed native walking speed", target.position, before)
    112 				}
    113 			})
    114 		}
    115 	}
    116 }
    117 
    118 func TestControlRootPreservesAnInRangeNPCAttack(t *testing.T) {
    119 	world, _ := newControlPursuitWorld(t)
    120 	player, target := world.characters[1], world.characters[1000000]
    121 	player.position = [3]float32{0, 0, 0}
    122 	castTestBuff(t, world, 1, target.id, 42110)
    123 	before, position := player.health, target.position
    124 
    125 	world.advanceCombat(target.nextAttack)
    126 
    127 	if player.health >= before || target.position != position || !target.movementRestricted() || target.fighting != 1 {
    128 		t.Fatal("root prevented combat or allowed motion", player.health, before, target.position)
    129 	}
    130 }
    131 
    132 func TestFailedRootCastCannotLeakStoppedMovementThroughSharedState(t *testing.T) {
    133 	for _, failure := range []struct{ name, statement string }{
    134 		{"insert", "CREATE TRIGGER fail_root BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(FAIL,'test root'); END"},
    135 		{"ignored insert", "CREATE TRIGGER fail_root BEFORE INSERT ON active_nanos BEGIN SELECT RAISE(IGNORE); END"},
    136 		{"commit", "CREATE TABLE pending_root (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_root AFTER INSERT ON active_nanos BEGIN INSERT INTO pending_root VALUES(999999); END"},
    137 	} {
    138 		t.Run(failure.name, func(t *testing.T) {
    139 			world, _ := newControlPursuitWorld(t)
    140 			player, target, client := world.characters[1], world.characters[1000000], world.clients[1]
    141 			target.movement.velocity = [3]float32{4.5, 0, 0}
    142 			target.movement.speed = 4.5
    143 			before := player.nano
    144 			if err := world.handleNanoCast(client, characterActionPacket(1, [5]uint32{19, 50000, target.id, 53019, 42110})[20:]); err != nil {
    145 				t.Fatal(err)
    146 			}
    147 			deadline := client.nanoCast.readyAt.Add(time.Nanosecond)
    148 			if _, err := world.db.Exec(failure.statement); err != nil {
    149 				t.Fatal(err)
    150 			}
    151 
    152 			world.advanceNanos(deadline)
    153 
    154 			if !target.motion.moving || !target.running || target.movementRestricted() || len(target.activeNanos) != 0 || len(target.threat) != 0 || player.nano != before || client.nanoCast != nil || target.movement.velocity != [3]float32{4.5, 0, 0} || target.movement.speed != 4.5 {
    155 				t.Fatal("failed root mutated live movement, taunt, effect or cost", target.motion, target.activeNanos, target.threat, player.nano, before)
    156 			}
    157 			if _, err := world.db.Exec("DROP TRIGGER fail_root"); err != nil {
    158 				t.Fatal(err)
    159 			}
    160 			castTestBuff(t, world, 1, target.id, 42110)
    161 			cold := newTestInventoryServer(t).characters[target.id]
    162 			if !target.movementRestricted() || !cold.movementRestricted() || target.motion.moving || cold.motion.moving || player.nano != before-17 || target.threat[1] != 13 || target.movement.velocity != [3]float32{} || target.movement.speed != 0 {
    163 				t.Fatal("retried root was not committed exactly once", target.activeNanos, target.threat, player.nano)
    164 			}
    165 		})
    166 	}
    167 }
    168 
    169 func TestFailedRootExpiryKeepsAuthoritativeMovementBlockedUntilRemovalCommits(t *testing.T) {
    170 	for _, failure := range []struct{ name, statement string }{
    171 		{"delete", "CREATE TRIGGER fail_expiry BEFORE DELETE ON active_nanos BEGIN SELECT RAISE(FAIL,'test expiry'); END"},
    172 		{"ignored delete", "CREATE TRIGGER fail_expiry BEFORE DELETE ON active_nanos BEGIN SELECT RAISE(IGNORE); END"},
    173 		{"commit", "CREATE TABLE pending_expiry (player_id INTEGER REFERENCES players(id) DEFERRABLE INITIALLY DEFERRED); CREATE TRIGGER fail_expiry AFTER DELETE ON active_nanos BEGIN INSERT INTO pending_expiry VALUES(999999); END"},
    174 	} {
    175 		t.Run(failure.name, func(t *testing.T) {
    176 			world, _ := newControlPursuitWorld(t)
    177 			target := world.characters[1000000]
    178 			castTestBuff(t, world, 1, target.id, 42110)
    179 			deadline := target.activeNanos[0].expiresAt
    180 			world.lastNPCUpdate = deadline
    181 			before := target.position
    182 			if _, err := world.db.Exec(failure.statement); err != nil {
    183 				t.Fatal(err)
    184 			}
    185 
    186 			world.advanceNanos(deadline)
    187 			world.advanceNPCs(deadline.Add(250 * time.Millisecond))
    188 
    189 			if !target.movementRestricted() || target.position != before || target.motion.moving {
    190 				t.Fatal("failed removal let an expired root move", target.position, target.activeNanos)
    191 			}
    192 			if _, err := world.db.Exec("DROP TRIGGER fail_expiry"); err != nil {
    193 				t.Fatal(err)
    194 			}
    195 			world.advanceNanos(deadline.Add(500 * time.Millisecond))
    196 			world.advanceNPCs(deadline.Add(750 * time.Millisecond))
    197 			world.advanceNPCs(deadline.Add(time.Second))
    198 			if target.movementRestricted() || target.position == before || !target.motion.moving {
    199 				t.Fatal("successful retry did not resume pursuit", target.position, target.activeNanos)
    200 			}
    201 			if len(newTestInventoryServer(t).characters[target.id].activeNanos) != 0 {
    202 				t.Fatal("retried expiry was not durable")
    203 			}
    204 		})
    205 	}
    206 }
    207 
    208 func TestControlRootLateObserverReplaysEffectWithoutARoute(t *testing.T) {
    209 	world, _ := newControlPursuitWorld(t)
    210 	target := world.characters[1000000]
    211 	at := castTestBuff(t, world, 1, target.id, 42110)
    212 	observer := &combatTestConn{}
    213 	world.characters[2] = &character{id: 2, playfield: 950, health: 100}
    214 	world.clients[2] = &worldClient{id: 2, players: map[uint32]bool{1: true}, conn: observer, target: true, ready: true, npcs: make(map[uint32]bool)}
    215 	world.lastNPCUpdate = at
    216 
    217 	world.advanceNPCs(at.Add(250 * time.Millisecond))
    218 
    219 	seenSpawn, roots := false, 0
    220 	for _, packet := range observer.packets {
    221 		key := binary.BigEndian.Uint32(packet[16:20])
    222 		if key == 0x271b3a6b && binary.BigEndian.Uint32(packet[24:28]) == target.id {
    223 			seenSpawn = true
    224 		}
    225 		if key == 0x5e477770 && binary.BigEndian.Uint32(packet[29:33]) == 177 {
    226 			if !seenSpawn || binary.BigEndian.Uint32(packet[37:41]) != 42110 {
    227 				t.Fatal("root replay preceded its actor or used wrong program")
    228 			}
    229 			roots++
    230 		}
    231 		if key == 0x260f3671 {
    232 			t.Fatal("rooted late observer received a moving route")
    233 		}
    234 	}
    235 	if !seenSpawn || roots != 1 || !world.clients[2].npcs[target.id] {
    236 		t.Fatal("late root replay was incomplete", seenSpawn, roots)
    237 	}
    238 }
    239 
    240 func TestControlRootAndSnareDisappearAtomicallyOnLethalWeaponOutcome(t *testing.T) {
    241 	world, _ := newControlPursuitWorld(t)
    242 	player, target, client := world.characters[1], world.characters[1000000], world.clients[1]
    243 	castTestBuff(t, world, 1, target.id, 42110)
    244 	player.profession = 4
    245 	player.inventory.LearnedNanos = append(player.inventory.LearnedNanos, 82518)
    246 	player.nanoRechargeUntil = time.Time{}
    247 	castTestBuff(t, world, 1, target.id, 82518)
    248 	player.position = [3]float32{0, 0, 0}
    249 	target.health = 1
    250 
    251 	if err := world.handleCombat(client, attackPacket(1, target.id)[20:]); err != nil {
    252 		t.Fatal(err)
    253 	}
    254 	world.advanceCombat(player.nextAttack)
    255 	cold := newTestInventoryServer(t).characters[target.id]
    256 
    257 	if target.health != 0 || cold.health != 0 || len(target.activeNanos) != 0 || len(cold.activeNanos) != 0 || target.motion.moving || cold.motion.moving || target.nanoBonus(156) != 0 || target.movementRestricted() {
    258 		t.Fatal("lethal outcome retained a control effect or moving route", target.health, target.activeNanos, cold.activeNanos, target.motion)
    259 	}
    260 }