aorecomp

Anarchy Online classic in the browser

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


server/npc_population_levels_test.go (5722B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"fmt"
      6 	"slices"
      7 	"testing"
      8 	"time"
      9 )
     10 
     11 func TestNPCPopulationStartupIncludesBothStarterLeetLevelsAfterRestart(t *testing.T) {
     12 	world := newTestServer(t)
     13 	var before []uint16
     14 	for id := uint32(1000000); id < world.nextNPC; id++ {
     15 		npc := world.characters[id]
     16 		if npc != nil && npc.npcDefinitionID == "950/district/1/group/1/authored_point" {
     17 			before = append(before, npc.level)
     18 		}
     19 	}
     20 
     21 	if err := world.flushWorld(); err != nil {
     22 		t.Fatal(err)
     23 	}
     24 	cold := newTestInventoryServer(t)
     25 	var after []uint16
     26 	for id := uint32(1000000); id < cold.nextNPC; id++ {
     27 		npc := cold.characters[id]
     28 		if npc != nil && npc.npcDefinitionID == "950/district/1/group/1/authored_point" {
     29 			after = append(after, npc.level)
     30 			if npc.combatProfile == nil || npc.maximumHealth() != 20 {
     31 				t.Fatal("starter level lost its exact combat profile", npc.level)
     32 			}
     33 		}
     34 	}
     35 
     36 	if !slices.Equal(before, []uint16{1, 2, 1}) || !slices.Equal(after, before) {
     37 		t.Fatal("authored 1..2 starter population collapsed to its minimum or changed on restart", before, after)
     38 	}
     39 }
     40 
     41 func TestNPCPopulationLevelRangesKeepEveryAuthoredEndpoint(t *testing.T) {
     42 	groups, placements := 0, 0
     43 
     44 	for _, playfield := range gamedata.NPCs.Playfields {
     45 		for _, district := range playfield.Districts {
     46 			for _, group := range district.Groups {
     47 				expected := [2]uint16{district.Source.NPCMinLevel, district.Source.NPCMaxLevel}
     48 				if group.FixedSource != nil {
     49 					expected = [2]uint16{group.FixedSource.MinLevel, group.FixedSource.MaxLevel}
     50 				}
     51 				if group.Levels != expected || group.Levels[0] == 0 || group.Levels[0] > group.Levels[1] {
     52 					t.Fatal("maintained population lost an original level endpoint", group.ID, group.Levels, expected)
     53 				}
     54 				groups++
     55 				placements += len(group.Positions)
     56 			}
     57 		}
     58 	}
     59 	if groups != 11965 || placements != 68609 {
     60 		t.Fatal("level ranges changed population coverage", groups, placements)
     61 	}
     62 }
     63 
     64 func TestNPCPopulationRangesIncludeExtendedLevelsWithoutChangingPlacement(t *testing.T) {
     65 	for _, scenario := range []struct {
     66 		levels [2]uint16
     67 		want   []uint16
     68 	}{
     69 		{[2]uint16{1, 2}, []uint16{1, 2, 1, 2}},
     70 		{[2]uint16{2, 4}, []uint16{2, 3, 4, 2}},
     71 		{[2]uint16{5, 6}, []uint16{5, 6, 5, 6}},
     72 		{[2]uint16{10, 11}, []uint16{10, 11, 10, 11}},
     73 		{[2]uint16{300, 301}, []uint16{300, 301, 300, 301}},
     74 		{[2]uint16{65534, 65535}, []uint16{65534, 65535, 65534, 65535}},
     75 		{[2]uint16{1, 65535}, []uint16{1, 2, 3, 4}},
     76 		{[2]uint16{400, 400}, []uint16{400, 400, 400, 400}},
     77 	} {
     78 		t.Run(fmt.Sprint(scenario.levels), func(t *testing.T) {
     79 			data := testNPCData()
     80 			data.Playfields[0].Districts[0].Groups[0].AppearanceStatus = "inferred_species"
     81 			data.Playfields[0].Districts[0].Groups[0].Levels = scenario.levels
     82 			data.Playfields[0].Districts[0].Groups[0].Positions = [][3]float32{{52, 14, 56}, {54, 14, 56}, {56, 14, 56}, {58, 14, 56}}
     83 			useNPCData(t, data)
     84 
     85 			spawns, err := readNPCSpawns()
     86 			if err != nil {
     87 				t.Fatal(err)
     88 			}
     89 
     90 			var levels []uint16
     91 			for index, spawn := range spawns {
     92 				levels = append(levels, spawn.level)
     93 				if spawn.id != 1000000+uint32(index) || spawn.position != data.Playfields[0].Districts[0].Groups[0].Positions[index] || spawn.npcDefinitionID != "test/npc" {
     94 					t.Fatal("level allocation changed placement or definition", spawn)
     95 				}
     96 				if (spawn.combatProfile != nil) != (spawn.level <= 10) {
     97 					t.Fatal("unlisted level inherited a supported profile", spawn.level)
     98 				}
     99 			}
    100 			if !slices.Equal(levels, scenario.want) {
    101 				t.Fatal("range lost an endpoint, overflowed or failed to cycle", levels, scenario.want)
    102 			}
    103 		})
    104 	}
    105 }
    106 
    107 func TestNPCPopulationRejectsInvalidLevelRanges(t *testing.T) {
    108 	for _, levels := range [][2]uint16{{0, 0}, {0, 1}, {2, 1}} {
    109 		t.Run(fmt.Sprint(levels), func(t *testing.T) {
    110 			data := testNPCData()
    111 			data.Playfields[0].Districts[0].Groups[0].Levels = levels
    112 			useNPCData(t, data)
    113 
    114 			spawns, err := readNPCSpawns()
    115 
    116 			if err == nil || spawns != nil {
    117 				t.Fatal("invalid range created a population", levels, spawns, err)
    118 			}
    119 		})
    120 	}
    121 }
    122 
    123 func TestNPCPopulationSecondLevelRetainsSelectedLifeThroughRespawn(t *testing.T) {
    124 	data := testNPCData()
    125 	data.Playfields[0].Districts[0].Groups[0].AppearanceStatus = "inferred_species"
    126 	data.Playfields[0].Districts[0].Groups[0].Levels = [2]uint16{1, 2}
    127 	data.Playfields[0].Districts[0].Groups[0].Positions = [][3]float32{{52, 14, 56}, {54, 14, 56}}
    128 	useNPCData(t, data)
    129 	world := newTestServer(t)
    130 	player := world.newCharacter()
    131 	player.position = [3]float32{54, 14, 56}
    132 	client := &worldClient{id: player.id, conn: &combatTestConn{}, target: true, ready: true, npcs: map[uint32]bool{1000000: true, 1000001: true}}
    133 	world.clients[player.id] = client
    134 	world.characters[1000001].health = 1
    135 	if err := world.saveUser(player); err != nil {
    136 		t.Fatal(err)
    137 	}
    138 	if err := world.flushWorld(); err != nil {
    139 		t.Fatal(err)
    140 	}
    141 
    142 	if err := world.handleCombat(client, attackPacket(player.id, 1000001)[20:]); err != nil {
    143 		t.Fatal(err)
    144 	}
    145 	death := player.nextAttack
    146 	world.advanceCombat(death)
    147 	world.advanceNPCRespawns(death.Add(100 * time.Second))
    148 	cold := newTestInventoryServer(t)
    149 
    150 	for _, current := range []*server{world, cold} {
    151 		npc := current.characters[1000002]
    152 		if npc == nil || npc.level != 2 || npc.combatProfile != gamedata.NPCCombatProfiles[17655][2] || npc.health != 20 || npc.npcDefinitionID != "test/npc" || npc.position != [3]float32{54, 14, 56} {
    153 			t.Fatal("replacement lost the selected level, profile or authored position", npc)
    154 		}
    155 		if current.characters[1000001].level != 2 || current.characters[1000001].corpse == nil || len(current.npcRespawns) != 0 {
    156 			t.Fatal("replacement changed the old life or retained its consumed timer")
    157 		}
    158 	}
    159 }