aorecomp

Anarchy Online classic in the browser

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


server/npc_data_test.go (5455B)


      1 package main
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"io/fs"
      6 	"os"
      7 	"testing"
      8 	"testing/fstest"
      9 )
     10 
     11 func testNPCData() gamedata.NPCData {
     12 	return gamedata.NPCData{
     13 		AppearanceStatuses: map[string]gamedata.Evidence{
     14 			"best_effort":          {Confidence: "provisional", Comment: "Reviewed uncertain name and body."},
     15 			"static_mesh":          {Confidence: "selected_asset", Comment: "Original resource item mesh."},
     16 			"placeholder_creature": {Confidence: "placeholder", Comment: "Identity unresolved."},
     17 			"inferred_species":     {Confidence: "inferred", Comment: "Selected species mapping."},
     18 		},
     19 		PlacementStatuses: map[string]gamedata.Evidence{
     20 			"fixed_point":    {Confidence: "authored", Comment: "Exact authored XYZ."},
     21 			"authored_point": {Confidence: "approximate", Comment: "Inferred allocation."},
     22 		},
     23 		LevelPolicy: gamedata.Evidence{Confidence: "inferred", Comment: "Cycles through inclusive level ranges."},
     24 		Playfields: []gamedata.NPCPlayfield{{ID: 950, Name: "Omni Training", Districts: []gamedata.NPCDistrict{{Name: "Test district", Groups: []gamedata.NPCGroup{{
     25 			ID: "test/npc", TemplateCode: "TEST", Name: "Edited NPC", MonsterData: 17655, Appearance: 229, Levels: [2]uint16{7, 7},
     26 			AppearanceStatus: "placeholder_creature", PlacementStatus: "fixed_point",
     27 			Comment: "Leet appearance until the original identity is recovered.", Positions: [][3]float32{{52, 14, 56}},
     28 		}}}}}},
     29 	}
     30 }
     31 
     32 type npcFixtureResources struct {
     33 	files    fstest.MapFS
     34 	database fs.FS
     35 }
     36 
     37 func (resources npcFixtureResources) Open(name string) (fs.File, error) {
     38 	// Keep synthetic terrain and links while loading required original geometry.
     39 	switch name {
     40 	case "anarchy-assets-14.9.1/cd_image/data/db/ResourceDatabase.idx",
     41 		"anarchy-assets-14.9.1/cd_image/data/db/ResourceDatabase.dat":
     42 		return resources.database.Open(name)
     43 	}
     44 	return resources.files.Open(name)
     45 }
     46 
     47 func useNPCData(t *testing.T, data gamedata.NPCData) {
     48 	t.Helper()
     49 	previousNPCs, previousGroups, previousResources := gamedata.NPCs, gamedata.NPCGroups, resources
     50 	gamedata.NPCs = data
     51 	gamedata.NPCGroups = make(map[string]*gamedata.NPCGroup)
     52 	for _, playfield := range data.Playfields {
     53 		for _, district := range playfield.Districts {
     54 			for index := range district.Groups {
     55 				group := &district.Groups[index]
     56 				gamedata.NPCGroups[group.ID] = group
     57 			}
     58 		}
     59 	}
     60 	resources = npcFixtureResources{database: previousResources, files: fstest.MapFS{
     61 		"terrain.bin":     {Data: append([]byte("AOTERR02"), 0, 0, 0, 0)},
     62 		"zone_links.yaml": {Data: []byte("original: {}\ntarget: {}\n")},
     63 	}}
     64 	t.Chdir(t.TempDir())
     65 	t.Cleanup(func() { gamedata.NPCs, gamedata.NPCGroups, resources = previousNPCs, previousGroups, previousResources })
     66 }
     67 
     68 func useDiskResources(t *testing.T) {
     69 	t.Helper()
     70 	previous := resources
     71 	resources = os.DirFS(".")
     72 	t.Cleanup(func() { resources = previous })
     73 }
     74 
     75 func useResources(t *testing.T, files fs.FS) {
     76 	t.Helper()
     77 	previous := resources
     78 	resources = files
     79 	t.Cleanup(func() { resources = previous })
     80 }
     81 
     82 func TestGuideNameKeepsUnresolvedAppearance(t *testing.T) {
     83 	data := testNPCData()
     84 	data.Playfields[0].ID = 1931
     85 	data.Playfields[0].Districts[0].Groups[0].Name = "Acolyte Seleen"
     86 	useNPCData(t, data)
     87 	var acolyte *character
     88 
     89 	spawns, err := readNPCSpawns()
     90 	if err != nil {
     91 		t.Fatal(err)
     92 	}
     93 	for index := range spawns {
     94 		if spawns[index].playfield == 1931 && spawns[index].name == "Acolyte Seleen" {
     95 			acolyte = spawns[index]
     96 		}
     97 	}
     98 
     99 	if acolyte == nil || acolyte.monsterData != 17655 || acolyte.appearance != 229 || acolyte.combatProfile != nil {
    100 		t.Fatal("guide name lost its unresolved appearance or dungeon placement")
    101 	}
    102 }
    103 
    104 func TestNPCDefinitionsRetainSourceEvidence(t *testing.T) {
    105 	data := gamedata.NPCs
    106 
    107 	mapping := gamedata.NPCNames.Playfields[1931]
    108 	source := gamedata.NPCNames.Sources[mapping.Source]
    109 
    110 	if data.Comment == "" || data.LevelPolicy.Comment == "" || data.AppearanceStatuses["placeholder_creature"].Confidence != "placeholder" {
    111 		t.Fatal("NPC provenance is unavailable to data audits")
    112 	}
    113 	if mapping.Names["GUOT"] != "Guardian of Tomorrow" || mapping.Confidence != "inferred" || mapping.Comment == "" || source != "https://www.ao-universe.com/guides/classic-ao/location-guides-5/temple-of-three-winds-loot-table" {
    114 		t.Fatalf("NPC name evidence: %+v, %s", mapping, source)
    115 	}
    116 }
    117 
    118 func TestNPCDefinitionsRejectUnclassifiedAppearance(t *testing.T) {
    119 	data := testNPCData()
    120 	data.Playfields[0].Districts[0].Groups[0].AppearanceStatus = "typo"
    121 	useNPCData(t, data)
    122 
    123 	_, err := readNPCSpawns()
    124 
    125 	if err == nil || err.Error() != "NPC data: 950/Test district/TEST needs defined appearance and placement statuses" {
    126 		t.Fatalf("invalid NPC classification: %v", err)
    127 	}
    128 }
    129 
    130 func TestNPCInstanceKeepsDefinitionID(t *testing.T) {
    131 	useNPCData(t, testNPCData())
    132 
    133 	world := newTestServer(t)
    134 
    135 	if world.characters[1000000].npcDefinitionID != "test/npc" {
    136 		t.Fatal("live NPC lost the definition ID used by Go behavior code")
    137 	}
    138 	if len(world.characters) != 1 || len(world.terrain) != 0 || len(world.geometry.playfields) != 0 || len(world.geometry.interiors) != 4 {
    139 		t.Fatal("NPC fixture lost its isolated population or required interior geometry")
    140 	}
    141 }
    142 
    143 func readNPCSpawns() ([]*character, error) {
    144 	populations, err := readPopulations()
    145 	if err != nil {
    146 		return nil, err
    147 	}
    148 	var spawns []*character
    149 	for _, playfield := range gamedata.NPCs.Playfields {
    150 		spawns = append(spawns, populations[playfield.ID].spawns()...)
    151 	}
    152 	return spawns, nil
    153 }