aorecomp

Anarchy Online classic in the browser

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


server/npc_outdoor_surface_test.go (3662B)


      1 package main
      2 
      3 import (
      4 	"math"
      5 	"testing"
      6 	"time"
      7 )
      8 
      9 func newNPCOutdoorSurfaceWorld(t *testing.T) (*server, *character, time.Time) {
     10 	t.Helper()
     11 	world := newTestServer(t)
     12 	var npc *character
     13 	for _, actor := range world.characters {
     14 		if actor.npcDefinitionID == "950/district/5/group/0/district_center_fallback" && (npc == nil || actor.id < npc.id) {
     15 			npc = actor
     16 		}
     17 	}
     18 	player := world.newCharacter()
     19 	player.position = [3]float32{138, 13.5, 98}
     20 	world.characters = map[uint32]*character{player.id: player, npc.id: npc}
     21 	world.mobileNPCs = map[uint32][]*character{950: {npc}}
     22 	world.clients[player.id] = &worldClient{id: player.id, ready: true, conn: &combatTestConn{}, npcs: map[uint32]bool{npc.id: true}}
     23 	now := time.Now()
     24 	world.lastNPCUpdate = now
     25 	npc.motion.nextMove = now.Add(time.Hour)
     26 	return world, npc, now
     27 }
     28 
     29 func TestNPCOutdoorRoamingUsesNativeWaterContact(t *testing.T) {
     30 	world, npc, now := newNPCOutdoorSurfaceWorld(t)
     31 	npc.motion.destination = [3]float32{134, 14, 98}
     32 	npc.motion.moving, npc.motion.nextMove = true, now
     33 
     34 	for step := 1; step <= 20; step++ {
     35 		advanceNavigationForTest(t, world, now.Add(time.Duration(step)*250*time.Millisecond), world.advanceNPCs)
     36 	}
     37 
     38 	if npc.position[1] < 13.4 || npc.position[1] > 13.6 || !npc.movement.deepWater || npc.movement.components[0] != 4 || npc.motion.home != [3]float32{130, 14, 98} {
     39 		t.Fatal("roaming followed submerged terrain instead of the native water contact", npc.position, npc.movement)
     40 	}
     41 }
     42 
     43 func TestNPCOutdoorPursuitUsesNativeWaterContact(t *testing.T) {
     44 	world, npc, now := newNPCOutdoorSurfaceWorld(t)
     45 	npc.fighting = 1
     46 	npc.nextAttack = now.Add(time.Second)
     47 	deadline, health := npc.nextAttack, npc.health
     48 
     49 	for step := 1; step <= 20; step++ {
     50 		advanceNavigationForTest(t, world, now.Add(time.Duration(step)*250*time.Millisecond), world.advanceNPCs)
     51 	}
     52 
     53 	if npc.position[0] < 132 || npc.position[1] < 13.4 || npc.position[1] > 13.6 || !npc.movement.deepWater || npc.movement.components[0] != 4 ||
     54 		npc.fighting != 1 || npc.nextAttack != deadline || npc.health != health {
     55 		t.Fatal("pursuit lost its native water contact or encounter state", npc.position, npc.movement, npc.fighting)
     56 	}
     57 }
     58 
     59 func TestNPCOutdoorReturnLeavesWaterAndRetainsHomeAfterColdLoad(t *testing.T) {
     60 	world, npc, now := newNPCOutdoorSurfaceWorld(t)
     61 	npc.motion.destination = [3]float32{134, 14, 98}
     62 	npc.motion.moving, npc.motion.nextMove = true, now
     63 	for step := 1; step <= 20; step++ {
     64 		advanceNavigationForTest(t, world, now.Add(time.Duration(step)*250*time.Millisecond), world.advanceNPCs)
     65 	}
     66 	if err := world.flushWorld(); err != nil {
     67 		t.Fatal(err)
     68 	}
     69 	cold := newTestInventoryServer(t)
     70 	cold.characters[1] = &character{id: 1, playfield: npc.playfield, position: [3]float32{138, 13.5, 98}, health: 100}
     71 	cold.clients[1] = &worldClient{id: 1, ready: true, conn: &combatTestConn{}, npcs: make(map[uint32]bool)}
     72 	cold.geometry, cold.terrain = world.geometry, world.terrain
     73 	cold.lastNPCUpdate = now.Add(5 * time.Second)
     74 	returning := cold.characters[npc.id]
     75 	returning.motion.returning = true
     76 
     77 	for step := 21; step <= 80 && returning.motion.returning; step++ {
     78 		advanceNavigationForTest(t, cold, now.Add(time.Duration(step)*250*time.Millisecond), cold.advanceNPCs)
     79 	}
     80 
     81 	if returning.motion.returning || returning.motion.moving || returning.movement.deepWater || returning.movement.components[0] != 2 ||
     82 		math.Abs(float64(returning.position[0]-130)) >= 1 || returning.position[1] < 13.3 || returning.motion.home != [3]float32{130, 14, 98} || returning.health != 20 {
     83 		t.Fatal("cold return did not leave water at its retained home", returning.position, returning.motion, returning.movement)
     84 	}
     85 }