server/nano_range_catalog_test.go (7436B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 _ "embed" 6 "encoding/json" 7 "reflect" 8 "slices" 9 "testing" 10 ) 11 12 //go:embed testdata/nano-range-nanos.json 13 var nanoRangeData []byte 14 15 type nanoRangeFixture struct { 16 combatNanoFixture 17 Stats [][2]uint32 18 Functions []gamedata.ItemFunction 19 CrystalStats [][2]uint32 `json:"crystal_stats"` 20 CrystalFunctions []gamedata.ItemFunction `json:"crystal_functions"` 21 } 22 23 func nanoRangeFixtures(t *testing.T) []nanoRangeFixture { 24 t.Helper() 25 var fixtures []nanoRangeFixture 26 if err := json.Unmarshal(nanoRangeData, &fixtures); err != nil { 27 t.Fatal(err) 28 } 29 return fixtures 30 } 31 32 func TestNanoRangeCatalogMatchesCompleteOriginalContracts(t *testing.T) { 33 fixtures := nanoRangeFixtures(t) 34 35 if len(fixtures) != 10 { 36 t.Fatal("incomplete independent range program census", len(fixtures)) 37 } 38 for _, fixture := range fixtures { 39 if !statBuffNano(fixture.Program) || nanoCrystalProgram(fixture.Crystal) != fixture.Program { 40 t.Fatal("complete original range contract or uploader rejected", fixture.Program, fixture.Crystal) 41 } 42 for _, field := range [][2]uint32{{0, fixture.Flags}, {30, fixture.Can}, {8, fixture.Duration}, {54, fixture.NCU}, {75, fixture.Strain}, 43 {210, fixture.RechargeDelay}, {287, fixture.Range}, {294, fixture.AttackDelay}, {407, fixture.NanoCost}} { 44 if nanoStat(fixture.Program, field[0]) != field[1] { 45 t.Fatal("original range casting metadata changed", fixture.Program, field) 46 } 47 } 48 definition := gamedata.NanoTemplates[fixture.Program] 49 var actions []gamedata.ItemAction 50 var weights []gamedata.ItemEntry 51 var getter [][2]uint32 52 var functions []gamedata.ItemFunction 53 var modifiers [][2]int32 54 for _, section := range definition.Sections { 55 if section.Actions != nil { 56 actions = append(actions, *section.Actions...) 57 } 58 if section.Entries != nil { 59 weights = append(weights, *section.Entries...) 60 } 61 if section.Stats != nil { 62 getter = append(getter, *section.Stats...) 63 } 64 if section.Functions != nil { 65 functions = append(functions, *section.Functions...) 66 for _, function := range *section.Functions { 67 modifiers = append(modifiers, [2]int32{*function.Arguments[0].Integer, *function.Arguments[1].Integer}) 68 } 69 } 70 } 71 if !slices.Equal(definition.Stats, fixture.Stats) || !slices.Equal(modifiers, fixture.Modifiers) || 72 !reflect.DeepEqual(functions, fixture.Functions) || !reflect.DeepEqual(actions, fixture.Actions) || 73 !reflect.DeepEqual(weights, fixture.Weights) || !slices.Equal(getter, fixture.Getter) { 74 t.Fatal("complete original range stats, effects or casting dictionaries changed", fixture.Program) 75 } 76 crystal := gamedata.ItemTemplates[fixture.Crystal] 77 var crystalActions []gamedata.ItemAction 78 var crystalFunctions []gamedata.ItemFunction 79 for _, section := range crystal.Sections { 80 if section.Actions != nil { 81 crystalActions = append(crystalActions, *section.Actions...) 82 } 83 if section.Functions != nil { 84 crystalFunctions = append(crystalFunctions, *section.Functions...) 85 } 86 } 87 if *crystal.Quality != fixture.Quality || !slices.Equal(crystal.Stats, fixture.CrystalStats) || 88 !reflect.DeepEqual(crystalActions, fixture.CrystalActions) || !reflect.DeepEqual(crystalFunctions, fixture.CrystalFunctions) { 89 t.Fatal("complete original range uploader stats, criteria or functions changed", fixture.Crystal) 90 } 91 } 92 } 93 94 func TestNanoRangeCatalogPreservesOriginalSearchAndRejectionBoundaries(t *testing.T) { 95 expectedRawPrograms := []uint32{32030, 32032, 32033, 32034, 32035, 32036, 32037, 32038, 32039, 32040, 32041, 55748, 96 95407, 95412, 95415, 95442, 95443, 95444, 95445, 95446, 95447, 95448, 97 117207, 117208, 117209, 117210, 117211, 117212, 117213, 117214, 117215, 117216, 117217, 98 117218, 117219, 117220, 117221, 117222, 117223, 117224, 117225, 117226, 117227, 117228} 99 expectedRawCrystals := []uint32{32071, 32073, 32074, 32075, 32076, 32077, 32078, 32079, 32080, 32081, 32082, 100 38512, 38515, 38520, 55756, 95422, 95423, 95427, 95449, 95450, 95451, 95452, 95453, 95454, 95455, 101 117231, 117232, 117233, 117234, 117235, 117236, 117237, 117238, 117239, 117240, 117241, 102 117242, 117243, 117244, 117245, 117246, 117247, 117248, 117249, 117250, 117251, 117252} 103 var rawPrograms, rangePrograms, programs, rawCrystals, crystals []uint32 104 var expectedPrograms, expectedCrystals []uint32 105 for _, fixture := range nanoRangeFixtures(t) { 106 expectedPrograms = append(expectedPrograms, fixture.Program) 107 expectedCrystals = append(expectedCrystals, fixture.Crystal) 108 } 109 110 for program, definition := range gamedata.NanoTemplates { 111 var rawCandidate, rangeCandidate bool 112 for _, section := range definition.Sections { 113 if section.Functions == nil { 114 continue 115 } 116 for _, function := range *section.Functions { 117 for _, argument := range function.Arguments { 118 if argument.Integer != nil && *argument.Integer == 381 { 119 rawCandidate = true 120 } 121 } 122 if (function.ID == 53012 || function.ID == 53045) && len(function.Arguments) > 0 && 123 function.Arguments[0].Integer != nil && *function.Arguments[0].Integer == 381 { 124 rangeCandidate = true 125 } 126 } 127 } 128 if rawCandidate { 129 rawPrograms = append(rawPrograms, program) 130 } 131 if rangeCandidate { 132 rangePrograms = append(rangePrograms, program) 133 if statBuffNano(program) { 134 programs = append(programs, program) 135 } 136 } 137 } 138 for crystal, definition := range gamedata.ItemTemplates { 139 candidate := false 140 for _, section := range definition.Sections { 141 if section.Functions == nil { 142 continue 143 } 144 for _, function := range *section.Functions { 145 if function.ID == 53019 && len(function.Arguments) > 0 && function.Arguments[0].Integer != nil && 146 slices.Contains(rawPrograms, uint32(*function.Arguments[0].Integer)) { 147 candidate = true 148 } 149 } 150 } 151 if candidate { 152 rawCrystals = append(rawCrystals, crystal) 153 if slices.Contains(programs, nanoCrystalProgram(crystal)) { 154 crystals = append(crystals, crystal) 155 } 156 } 157 } 158 for _, values := range [][]uint32{rawPrograms, rangePrograms, programs, rawCrystals, crystals, expectedPrograms, expectedCrystals} { 159 slices.Sort(values) 160 } 161 162 if len(rawPrograms) != 44 || !slices.Equal(rawPrograms, expectedRawPrograms) || len(rawCrystals) != 47 || !slices.Equal(rawCrystals, expectedRawCrystals) { 163 t.Fatal("original integer-381 record or uploader search boundary changed", rawPrograms, rawCrystals) 164 } 165 if len(rangePrograms) != 43 || len(programs) != 10 || len(crystals) != 10 || 166 !slices.Equal(programs, expectedPrograms) || !slices.Equal(crystals, expectedCrystals) { 167 t.Fatal("range admission differs from complete original program and uploader census", rangePrograms, programs, crystals) 168 } 169 // Glacial Cloak's HP amount 381 is a search hit, with no range modifier. 170 if slices.Contains(rangePrograms, 55748) || statBuffNano(55748) || nanoCrystalProgram(55756) != 0 { 171 t.Fatal("HP amount 381 was confused with a range stat selector") 172 } 173 for _, program := range rangePrograms { 174 if !slices.Contains(expectedPrograms, program) && statBuffNano(program) { 175 t.Fatal("a special profession contract admitted a partial range effect", program) 176 } 177 } 178 for _, crystal := range rawCrystals { 179 if !slices.Contains(expectedCrystals, crystal) && crystal != 55756 && nanoCrystalProgram(crystal) != 0 { 180 t.Fatal("a special profession or bulk uploader admitted a partial contract", crystal) 181 } 182 } 183 }