aorecomp

Anarchy Online classic in the browser

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


server/gamedata/catalog_test.go (11736B)


      1 package gamedata_test
      2 
      3 import (
      4 	"ao/server/gamedata"
      5 	"encoding/json"
      6 	"os"
      7 	"strings"
      8 	"testing"
      9 )
     10 
     11 func TestCuratedNamesAgreeWithMaintainedGroups(t *testing.T) {
     12 	data, err := os.ReadFile("../../tools/data/npc_assignments.json")
     13 	if err != nil {
     14 		t.Fatal(err)
     15 	}
     16 	var assignments map[string]struct {
     17 		Variants []struct {
     18 			Name  string
     19 			Asset struct {
     20 				Kind string
     21 				ID   uint32
     22 			}
     23 			Appearance uint16
     24 			Confidence string
     25 			Groups     []string
     26 		}
     27 	}
     28 	if err := json.Unmarshal(data, &assignments); err != nil {
     29 		t.Fatal(err)
     30 	}
     31 	expected := make(map[string]npcGroupChoice)
     32 	for _, entry := range assignments {
     33 		for _, variant := range entry.Variants {
     34 			status := variant.Confidence
     35 			if status == "medium" || status == "low" || status == "fallback" {
     36 				status = "best_effort"
     37 			}
     38 			for _, id := range variant.Groups {
     39 				expected[id] = npcGroupChoice{variant.Name, variant.Asset.Kind, variant.Asset.ID, variant.Appearance, status}
     40 			}
     41 		}
     42 	}
     43 	seen := make(map[uint32]map[string]bool)
     44 	seenTemplates := make(map[string]bool)
     45 	monsters := make(map[uint32]bool)
     46 	for _, monster := range gamedata.Monsters {
     47 		monsters[monster.MonsterData] = true
     48 	}
     49 
     50 	for _, playfield := range gamedata.NPCs.Playfields {
     51 		mapping := gamedata.NPCNames.Playfields[playfield.ID]
     52 		seen[playfield.ID] = make(map[string]bool)
     53 		for _, district := range playfield.Districts {
     54 			for _, group := range district.Groups {
     55 				choice, exists := expected[group.ID]
     56 				if !exists || group.Name != choice.name || group.Appearance != choice.appearance || group.AppearanceStatus != choice.status || group.Comment == "" ||
     57 					choice.kind == "monster_data" && group.MonsterData != choice.asset || choice.kind == "static_mesh" && (group.StaticMesh != choice.asset || group.MonsterData != 0) {
     58 					t.Fatalf("NPC %s does not retain its context-specific selected identity", group.ID)
     59 				}
     60 				if evidence, ok := gamedata.NPCNames.Templates[group.TemplateCode]; ok {
     61 					seenTemplates[group.TemplateCode] = true
     62 					if evidence.StaticMesh != 0 && (group.StaticMesh != evidence.StaticMesh || group.MonsterData != 0 || group.AppearanceStatus != "static_mesh") {
     63 						t.Fatalf("resource %s does not retain its selected static mesh", group.ID)
     64 					}
     65 				}
     66 				_, ok := mapping.Names[group.TemplateCode]
     67 				if !ok {
     68 					continue
     69 				}
     70 				seen[playfield.ID][group.TemplateCode] = true
     71 			}
     72 		}
     73 	}
     74 	for playfield, mapping := range gamedata.NPCNames.Playfields {
     75 		if mapping.Confidence == "" || gamedata.NPCNames.Sources[mapping.Source] == "" {
     76 			t.Fatalf("playfield %d has incomplete name evidence", playfield)
     77 		}
     78 		for code := range mapping.Names {
     79 			if !seen[playfield][code] {
     80 				t.Fatalf("name evidence %d/%s has no maintained NPC group", playfield, code)
     81 			}
     82 		}
     83 	}
     84 	for code, evidence := range gamedata.NPCNames.Templates {
     85 		if !seenTemplates[code] || evidence.Name == "" || evidence.Confidence == "" || evidence.Comment == "" || gamedata.NPCNames.Sources[evidence.Source] == "" {
     86 			t.Fatalf("template %s has missing population or evidence", code)
     87 		}
     88 		if evidence.MonsterData != 0 && !monsters[evidence.MonsterData] {
     89 			t.Fatalf("template %s references an unavailable appearance", code)
     90 		}
     91 	}
     92 }
     93 
     94 type npcGroupChoice struct {
     95 	name, kind string
     96 	asset      uint32
     97 	appearance uint16
     98 	status     string
     99 }
    100 
    101 func TestHumanNPCBodiesHaveHumanAppearances(t *testing.T) {
    102 	humanModels := make(map[uint32]bool)
    103 	for _, monster := range gamedata.Monsters {
    104 		if monster.MeshFilename == nil {
    105 			continue
    106 		}
    107 		for _, prefix := range []string{"solitus_", "opifex_", "nanomage_", "athrox_"} {
    108 			if strings.HasPrefix(*monster.MeshFilename, prefix) {
    109 				humanModels[monster.MonsterData] = true
    110 			}
    111 		}
    112 	}
    113 
    114 	lieutenant := gamedata.NPCGroups["710/district/1/fixed/8"]
    115 
    116 	for _, group := range gamedata.NPCGroups {
    117 		if humanModels[group.MonsterData] && (group.Appearance>>4&7 < 1 || group.Appearance>>4&7 > 4) {
    118 			t.Fatalf("human body %d on NPC %s has nonhuman appearance %d", group.MonsterData, group.ID, group.Appearance)
    119 		}
    120 	}
    121 	if lieutenant.MonsterData != 26088 || lieutenant.Appearance != 277 {
    122 		t.Fatalf("Male Lieutenant has body %d and appearance %d", lieutenant.MonsterData, lieutenant.Appearance)
    123 	}
    124 }
    125 
    126 func TestClientReferencedNPCNames(t *testing.T) {
    127 	cases := []struct {
    128 		item uint32
    129 		code string
    130 		name string
    131 	}{
    132 		{163503, "BR01", "Mini Bronto"},
    133 		{163506, "PR20", "Probe 2002-2"},
    134 		{163516, "VU01", "Vulture Squabbler"},
    135 		{163517, "NI01", "71-Alpha Ninjadroid"},
    136 		{163521, "H126", "Season Enforcer"},
    137 		{152790, "FGOI", "Figment of Imagination"},
    138 		{152791, "FEOF", "Figment of Imagination"},
    139 	}
    140 	for _, test := range cases {
    141 		linked := false
    142 		for _, item := range gamedata.Items.Items {
    143 			if item.ID != test.item {
    144 				continue
    145 			}
    146 			for _, section := range item.Sections {
    147 				if section.Functions == nil {
    148 					continue
    149 				}
    150 				for _, function := range *section.Functions {
    151 					for _, argument := range function.Arguments {
    152 						if argument.Code != nil && *argument.Code == test.code {
    153 							linked = true
    154 						}
    155 					}
    156 				}
    157 			}
    158 		}
    159 
    160 		if !linked || gamedata.NPCNames.Templates[test.code].Name != test.name {
    161 			t.Fatalf("NPC %s lost its name or client item %d reference", test.code, test.item)
    162 		}
    163 	}
    164 }
    165 
    166 func TestResearchedNPCIdentities(t *testing.T) {
    167 	cases := []struct {
    168 		playfield uint32
    169 		code      string
    170 		name      string
    171 		model     uint32
    172 		status    string
    173 	}{
    174 		{127, "SS01", "Bloodcreeper", 30379, "inferred_species"},
    175 		{127, "CARO", "Carbonrich Rock", 0, "static_mesh"},
    176 		{1931, "CULT", "Cultist", 26103, "inferred_species"},
    177 		{625, "ST50", "Stankel", 85351, "best_effort"},
    178 	}
    179 	for _, test := range cases {
    180 		found := false
    181 		for _, playfield := range gamedata.NPCs.Playfields {
    182 			if playfield.ID != test.playfield {
    183 				continue
    184 			}
    185 			for _, district := range playfield.Districts {
    186 				for _, group := range district.Groups {
    187 					if group.TemplateCode != test.code {
    188 						continue
    189 					}
    190 					found = true
    191 					if group.Name != test.name || group.MonsterData != test.model || group.AppearanceStatus != test.status {
    192 						t.Fatalf("NPC %s has inconsistent identity or appearance evidence", group.ID)
    193 					}
    194 				}
    195 			}
    196 		}
    197 		if !found {
    198 			t.Fatalf("missing researched NPC %d/%s", test.playfield, test.code)
    199 		}
    200 	}
    201 }
    202 
    203 func TestNPCsLinkToAuthoredSourceRecords(t *testing.T) {
    204 	sources := make(map[uint32]*gamedata.NPCPopulationSource)
    205 	for _, source := range gamedata.PopulationSources {
    206 		sources[source.Playfield] = source
    207 	}
    208 
    209 	groups := 0
    210 	for _, playfield := range gamedata.NPCs.Playfields {
    211 		if playfield.Source != sources[playfield.ID] {
    212 			t.Fatalf("playfield %d has a different source record", playfield.ID)
    213 		}
    214 		for _, district := range playfield.Districts {
    215 			if district.Source == nil || district.Source.Name != district.Name {
    216 				t.Fatalf("playfield %d district %q has no matching source", playfield.ID, district.Name)
    217 			}
    218 			for _, group := range district.Groups {
    219 				groups++
    220 				if gamedata.NPCGroups[group.ID] == nil || gamedata.NPCGroups[group.ID].TemplateCode != group.TemplateCode {
    221 					t.Fatalf("NPC group %q cannot be resolved by its stable ID", group.ID)
    222 				}
    223 				if group.PlacementStatus == "fixed_point" {
    224 					if group.FixedSource == nil || group.FixedSource.TemplateHash != group.TemplateCode || group.Source != nil {
    225 						t.Fatalf("fixed group %s has no matching source", group.TemplateCode)
    226 					}
    227 				} else if group.Source == nil || group.Source.TemplateHash != group.TemplateCode || group.FixedSource != nil {
    228 					t.Fatalf("group %s has no matching source", group.TemplateCode)
    229 				}
    230 			}
    231 		}
    232 	}
    233 
    234 	if len(sources) != 370 || groups == 0 {
    235 		t.Fatalf("got %d source records and %d linked groups", len(sources), groups)
    236 	}
    237 	if gamedata.NPCGroups["1931/district/0/fixed/152"].Name != "Guardian of Tomorrow" {
    238 		t.Fatal("stable NPC definition ID changed")
    239 	}
    240 	training := sources[950]
    241 	if training.RecordFileOffset != 338582419 || training.PayloadSHA256 != gamedata.PopulationEvidence.PayloadSHA256[950] {
    242 		t.Fatal("training area source provenance changed")
    243 	}
    244 	if training.Districts[1].NPCMinLevel != 1 || training.Districts[1].NPCMaxLevel != 2 || training.Districts[1].RespawnTime != 2 {
    245 		t.Fatal("authored level range or timing changed")
    246 	}
    247 	if training.Districts[1].SpawnPoints[0] != [4]float64{178.33633422851562, 17.465845108032227, 49.43410873413086, 2} {
    248 		t.Fatal("authored position or radius changed")
    249 	}
    250 }
    251 
    252 func TestItemCatalogRetainsDefinitionsAndUndecodedEvidence(t *testing.T) {
    253 	counts := make(map[uint32]int)
    254 	var missingQuality []uint32
    255 	undecoded := make(map[uint32]uint32)
    256 
    257 	for _, item := range gamedata.Items.Items {
    258 		counts[item.RecordType]++
    259 		if item.Quality == nil {
    260 			missingQuality = append(missingQuality, item.ID)
    261 		}
    262 		if item.Undecoded != nil {
    263 			undecoded[item.ID] = item.Undecoded.Offset
    264 			if item.Undecoded.Reason != "trailing bytes" || item.Undecoded.BytesHex != "00" {
    265 				t.Fatalf("item %d lost undecoded source bytes", item.ID)
    266 			}
    267 		}
    268 	}
    269 	first := gamedata.Items.Items[0]
    270 
    271 	if len(gamedata.Items.Items) != 87611 || counts[1000020] != 83245 || counts[1040005] != 4366 {
    272 		t.Fatalf("item coverage changed: %v", counts)
    273 	}
    274 	if first.ID != 21601 || first.Name != "Flamethrower Ammunition" || first.RecordFileOffset != 118546975 || first.Quality == nil || *first.Quality != 1 {
    275 		t.Fatal("first item definition changed")
    276 	}
    277 	if first.PayloadSHA256 != "efd5c6c9571c00ad211e4193a39320419f79c5285961c14f38c90ec9b2fa299c" || first.Stats[2] != [2]uint32{0, 2151678979} {
    278 		t.Fatal("item payload provenance or unsigned stat value changed")
    279 	}
    280 	if len(missingQuality) != 4 || missingQuality[0] != 41553 || missingQuality[1] != 41554 || missingQuality[2] != 41892 || missingQuality[3] != 82029 {
    281 		t.Fatalf("missing source quality values changed: %v", missingQuality)
    282 	}
    283 	if len(undecoded) != 3 || undecoded[120802] != 529 || undecoded[120827] != 198 || undecoded[203911] != 779 {
    284 		t.Fatalf("undecoded source ranges changed: %v", undecoded)
    285 	}
    286 	if gamedata.Items.Evidence.CatalogSHA256 != "8cf18f7606ca08fd2090ae82de1020d069ce53b1d9a2bf3e6be6c3a546595044" {
    287 		t.Fatal("item extraction evidence changed")
    288 	}
    289 }
    290 
    291 func TestMonsterCatalogRetainsUnresolvedMeshes(t *testing.T) {
    292 	unresolved := 0
    293 
    294 	for _, monster := range gamedata.Monsters {
    295 		if monster.MeshFilename == nil {
    296 			unresolved++
    297 		}
    298 	}
    299 	first := gamedata.Monsters[0]
    300 
    301 	if len(gamedata.Monsters) != 290 || unresolved != 5 {
    302 		t.Fatalf("got %d monster definitions, %d unresolved meshes", len(gamedata.Monsters), unresolved)
    303 	}
    304 	if first.MonsterData != 17627 || first.Stats[12] != 15263 || first.RecordFileOffset != 166310049 {
    305 		t.Fatal("monster source stats or offset changed")
    306 	}
    307 }
    308 
    309 func TestMonsterCatalogRetainsDeathAnimationResources(t *testing.T) {
    310 	count := 0
    311 
    312 	for _, monster := range gamedata.Monsters {
    313 		if len(monster.DeathAnimations) != 0 {
    314 			count++
    315 		}
    316 	}
    317 
    318 	leet := gamedata.MonsterTemplates[17655]
    319 	if count != 287 || len(leet.DeathAnimations) != 1 || leet.DeathAnimations[0] != 18107 {
    320 		t.Fatal("shipped creature death animations changed")
    321 	}
    322 }
    323 
    324 func TestMapCoordinatesRetainBinaryProvenance(t *testing.T) {
    325 	coordinates := gamedata.MapCoordinates
    326 
    327 	entertainment := coordinates.Playfields[705]
    328 	_, trainingHasOrigin := coordinates.Playfields[950]
    329 
    330 	if len(coordinates.Playfields) != 46 || entertainment != [2]int32{43756, 26526} || trainingHasOrigin {
    331 		t.Fatal("recovered playfield origins changed")
    332 	}
    333 	if coordinates.PlanetBounds != [4]int32{31022, 24880, 49773, 48995} {
    334 		t.Fatal("planet bounds changed")
    335 	}
    336 	if coordinates.Source.SHA256 != "3c937161de8f690351c3c08e158431417cee9ced77a41b677e7123b61a8c9883" || coordinates.Source.MarkerTransform != "0x1007df00" {
    337 		t.Fatal("map coordinate source provenance changed")
    338 	}
    339 }