aorecomp

Anarchy Online classic in the browser

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


server/npc_activity_test.go (6199B)


      1 package main
      2 
      3 import (
      4 	"net/http"
      5 	"net/url"
      6 	"testing"
      7 	"time"
      8 )
      9 
     10 func TestNPCActivitySkipsDistantAndUnavailablePlayers(t *testing.T) {
     11 	for _, scenario := range []string{"distant", "diagonal", "other zone", "offline", "loading", "not ready"} {
     12 		t.Run(scenario, func(t *testing.T) {
     13 			world, _, start := newNPCPursuitWorld(t)
     14 			npc := world.characters[1000000]
     15 			npc.health = 10
     16 			switch scenario {
     17 			case "distant":
     18 				world.characters[1].position = [3]float32{202, 0, 0}
     19 			case "diagonal":
     20 				world.characters[1].position = [3]float32{150, 0, 150}
     21 			case "other zone":
     22 				world.characters[1].playfield = 635
     23 			case "offline":
     24 				delete(world.clients, 1)
     25 			case "loading":
     26 				world.clients[1].loading = true
     27 			case "not ready":
     28 				world.clients[1].ready = false
     29 			}
     30 			before := npc.worldState(true, time.Time{})
     31 			attack := npc.nextAttack
     32 
     33 			for tick := 1; tick <= 16; tick++ {
     34 				now := start.Add(time.Duration(tick) * 250 * time.Millisecond)
     35 				world.advanceNPCs(now)
     36 				world.advanceCombat(now)
     37 				world.advanceRegeneration(now)
     38 			}
     39 
     40 			if npc.worldState(true, time.Time{}) != before || npc.nextAttack != attack || npc.fighting != 1 {
     41 				t.Fatal("unobserved NPC advanced its simulation", npc.position, npc.motion, npc.regenerationAt)
     42 			}
     43 		})
     44 	}
     45 }
     46 
     47 func TestNPCActivityResumesNearAnyPlayerWithoutMovementCatchup(t *testing.T) {
     48 	world, _, start := newNPCPursuitWorld(t)
     49 	npc := world.characters[1000000]
     50 	npc.fighting, npc.health = 0, 10
     51 	delete(world.fighting, npc.id)
     52 	npc.motion.moving, npc.motion.returning = true, true
     53 	npc.motion.home = [3]float32{21, 0, 0}
     54 	world.characters[1].position = [3]float32{2000, 0, 0}
     55 	world.advanceNPCs(start.Add(time.Hour))
     56 	before := npc.position
     57 	world.characters[2] = &character{id: 2, playfield: 950, position: [3]float32{201, 0, 0}}
     58 	world.clients[2] = &worldClient{id: 2, ready: true, conn: &combatTestConn{}, npcs: make(map[uint32]bool)}
     59 
     60 	world.advanceNPCs(start.Add(time.Hour + 250*time.Millisecond))
     61 	world.advanceRegeneration(start.Add(time.Hour))
     62 	world.advanceRegeneration(start.Add(time.Hour + 2*time.Second))
     63 
     64 	if before != [3]float32{1, 0, 0} || npc.position[0] <= 1 || npc.position[0] > 2.125 || npc.health != 11 {
     65 		t.Fatal("nearby observer did not resume bounded motion and recovery", before, npc.position, npc.health)
     66 	}
     67 }
     68 
     69 func TestNPCActivityCheckpointsIgnoreDistantPopulation(t *testing.T) {
     70 	data := testNPCData()
     71 	group := &data.Playfields[0].Districts[0].Groups[0]
     72 	group.PlacementStatus = "authored_point"
     73 	for index := 0; index < 1000; index++ {
     74 		group.Positions = append(group.Positions, [3]float32{2000 + float32(index), 14, 56})
     75 	}
     76 	useNPCData(t, data)
     77 	world := newTestServer(t)
     78 	if response := postControlForm(world, "/admin/players", url.Values{"name": {"Explorer"}}); response.Code != http.StatusSeeOther {
     79 		t.Fatal(response.Code, response.Body)
     80 	}
     81 	world.clients[1] = &worldClient{id: 1, ready: true, conn: &combatTestConn{}, npcs: make(map[uint32]bool)}
     82 	start := time.Now()
     83 	world.lastNPCUpdate = start
     84 	if err := world.checkpointWorld(start); err != nil {
     85 		t.Fatal(err)
     86 	}
     87 	distant := world.characters[1000001].worldState(true, time.Time{})
     88 	queries := world.databaseQueries.Load()
     89 
     90 	for tick := 1; tick <= 240; tick++ {
     91 		now := start.Add(time.Duration(tick) * 250 * time.Millisecond)
     92 		world.advanceNPCs(now)
     93 		world.advanceRegeneration(now)
     94 		if tick%4 == 0 {
     95 			if err := world.checkpointWorld(now); err != nil {
     96 				t.Fatal(err)
     97 			}
     98 			if err := world.flushPositions(now); err != nil {
     99 				t.Fatal(err)
    100 			}
    101 		}
    102 	}
    103 
    104 	count := world.databaseQueries.Load() - queries
    105 	if count > 180 || world.characters[1000001].worldState(true, time.Time{}) != distant || world.savedNPCs[1000001].State != distant {
    106 		t.Fatal("distant population caused routine simulation or database traffic", count)
    107 	}
    108 	if world.characters[1000000].regenerationAt.IsZero() || world.savedNPCs[1000000].State.Character.Vitals.RegenerationAt.IsZero() {
    109 		t.Fatal("nearby NPC was not simulated and checkpointed")
    110 	}
    111 	t.Logf("1000 dormant NPCs, one nearby NPC and one player: %d queries over 60 simulated seconds", count)
    112 }
    113 
    114 func TestNPCActivitySavesFinalPositionWhenGraceExpires(t *testing.T) {
    115 	world := newCheckpointWorld(t)
    116 	world.clients[1] = &worldClient{id: 1, ready: true}
    117 	now := time.Now()
    118 	if err := world.checkpointWorld(now); err != nil {
    119 		t.Fatal(err)
    120 	}
    121 	saved := world.savedNPCs[1000000]
    122 	saved.NextSave = now.Add(time.Hour)
    123 	world.savedNPCs[1000000] = saved
    124 	world.characters[1000000].position[0] = 53
    125 	world.characters[1].position[0] = 2000
    126 
    127 	if err := world.checkpointWorld(now.Add(15 * time.Minute)); err != nil {
    128 		t.Fatal(err)
    129 	}
    130 	var x float32
    131 	if err := world.db.QueryRow("SELECT position_x FROM characters WHERE id=1000000").Scan(&x); err != nil {
    132 		t.Fatal(err)
    133 	}
    134 	queries := world.databaseQueries.Load()
    135 	if err := world.checkpointWorld(now.Add(15*time.Minute + time.Second)); err != nil {
    136 		t.Fatal(err)
    137 	}
    138 
    139 	if x != 53 || world.databaseQueries.Load()-queries != 0 {
    140 		t.Fatal("NPC dormancy must save the final state once", x, world.databaseQueries.Load()-queries)
    141 	}
    142 }
    143 
    144 func TestNPCActivityGracePeriodAllowsHealingThenSleeps(t *testing.T) {
    145 	world, _, start := newNPCRegenerationWorld(t)
    146 	npc := world.characters[1000000]
    147 	world.advanceRegeneration(start)
    148 	delete(world.clients, 1)
    149 
    150 	world.advanceRegeneration(start.Add(2 * time.Second))
    151 	world.advanceRegeneration(start.Add(15*time.Minute - time.Second))
    152 	if npc.health != 22 {
    153 		t.Fatal("recently observed NPC stopped healing", npc.health)
    154 	}
    155 	clock := npc.regenerationAt
    156 	world.advanceRegeneration(start.Add(15 * time.Minute))
    157 	world.advanceRegeneration(start.Add(time.Hour))
    158 	if npc.health != 22 || npc.regenerationAt != clock {
    159 		t.Fatal("dormant NPC continued healing", npc.health, npc.regenerationAt)
    160 	}
    161 
    162 	// A returning player refreshes the grace period without replaying missed heals.
    163 	world.clients[1] = &worldClient{id: 1, ready: true, conn: &combatTestConn{}, npcs: make(map[uint32]bool)}
    164 	world.advanceRegeneration(start.Add(time.Hour))
    165 	delete(world.clients, 1)
    166 	world.advanceRegeneration(start.Add(time.Hour + 2*time.Second))
    167 	if npc.health != 24 {
    168 		t.Fatal("returning player did not renew NPC activity", npc.health)
    169 	}
    170 }