server/signed_stats_test.go (10104B)
1 package main 2 3 import ( 4 "ao/server/gamedata" 5 "fmt" 6 "testing" 7 "time" 8 ) 9 10 func syntheticNanoModifiers(t *testing.T, modifiers [][2]int32) []activeNano { 11 t.Helper() 12 var functions []gamedata.ItemFunction 13 for index := range modifiers { 14 functions = append(functions, gamedata.ItemFunction{ID: 53045, Arguments: []gamedata.ItemArgument{ 15 {Integer: &modifiers[index][0]}, {Integer: &modifiers[index][1]}, 16 }}) 17 } 18 gamedata.NanoTemplates[0] = &gamedata.Item{Sections: []gamedata.ItemSection{{Type: 2, Functions: &functions}}} 19 t.Cleanup(func() { delete(gamedata.NanoTemplates, 0) }) 20 return []activeNano{{program: 0}} 21 } 22 23 func TestSignedAbilityNanoContributionsStackAndRemove(t *testing.T) { 24 player := &character{stats: testCharacterStats()} 25 player.stats.Abilities.Psychic = 6 26 // These authored programs are calculation inputs, not admitted casts or procs. 27 player.activeNanos = []activeNano{{program: 207287}} 28 29 negative := player.effectiveAbility(21) 30 player.activeNanos = append(player.activeNanos, activeNano{program: 27134}) 31 overlapped := player.effectiveAbility(21) 32 player.activeNanos = player.activeNanos[1:] 33 positive := player.effectiveAbility(21) 34 player.activeNanos = nil 35 36 if int64(negative) != -11 || overlapped != 1 || positive != 18 || player.effectiveAbility(21) != 6 || player.stats.Abilities.Psychic != 6 { 37 t.Fatalf("signed contributions did not restore raw ability: %d/%d/%d/%d", negative, overlapped, positive, player.effectiveAbility(21)) 38 } 39 } 40 41 func TestSignedSkillTrickleMatchesOriginalGetterFixture(t *testing.T) { 42 for _, scenario := range []struct { 43 name string 44 psychic uint16 45 ability, first, next int32 46 weights [6]uint32 47 want int64 48 }{ 49 {"negative ability", 6, -17, 0, 0, [6]uint32{0, 0, 0, 0, 0, 100}, 3}, 50 {"negative direct skill", 6, -17, -8, 0, [6]uint32{0, 0, 0, 0, 0, 100}, -5}, 51 {"overlapping direct skill", 6, -17, -8, 12, [6]uint32{0, 0, 0, 0, 0, 100}, 7}, 52 {"removed negative skill", 6, -17, 0, 12, [6]uint32{0, 0, 0, 0, 0, 100}, 15}, 53 {"mixed signed weights", 6, -17, 0, 0, [6]uint32{0, 0, 0, 50, 0, 50}, 5}, 54 {"restored abilities", 6, 0, 0, 0, [6]uint32{0, 0, 0, 50, 0, 50}, 6}, 55 {"positive float boundary", 8, 0, 0, 0, [6]uint32{0, 0, 0, 0, 0, 100}, 6}, 56 {"negative float boundary", 8, -16, 0, 0, [6]uint32{0, 0, 0, 0, 0, 100}, 4}, 57 } { 58 t.Run(scenario.name, func(t *testing.T) { 59 player := &character{stats: testCharacterStats()} 60 player.stats.Abilities = characterAbilitiesStats{6, 6, 6, 6, 6, scenario.psychic} 61 // Synthetic effects isolate arithmetic without claiming catalog admission. 62 player.activeNanos = syntheticNanoModifiers(t, [][2]int32{{21, scenario.ability}, {162, scenario.first}, {162, scenario.next}}) 63 64 got := player.effectiveSkill(162, 5, scenario.weights) 65 66 if int64(got) != scenario.want { 67 t.Fatalf("got %d, want %d", got, scenario.want) 68 } 69 }) 70 } 71 } 72 73 func TestSignedNegativeAbilitiesDoNotPassEquipmentRequirements(t *testing.T) { 74 player := &character{level: 200, profession: 8, playfield: 950, stats: testCharacterStats()} 75 player.activeNanos = syntheticNanoModifiers(t, [][2]int32{{16, -100}, {17, -100}, {18, -100}, {19, -100}, {20, -100}, {21, -100}}) 76 77 for _, item := range []ownedItem{ 78 {Type: 51018, Template: 209283, Quality: 1}, 79 {Type: 51018, Template: 123742, Quality: 1}, 80 {Type: 51022, Template: 101173, Quality: 1}, 81 {Type: 51022, Template: 36783, Quality: 1}, 82 {Type: 51022, Template: 36779, Quality: 1}, 83 {Type: 51022, Template: 85512, Quality: 1}, 84 {Type: 51022, Template: 117294, Quality: 15}, 85 {Type: 51022, Template: 117322, Quality: 30}, 86 {Type: 51017, Template: 200461, Quality: 200}, 87 } { 88 if player.canEquip(item) { 89 t.Fatalf("negative effective requirement admitted template %d", item.Template) 90 } 91 positive := *player 92 positive.activeNanos = nil 93 positive.stats.Abilities = characterAbilitiesStats{5000, 5000, 5000, 5000, 5000, 5000} 94 if !positive.canEquip(item) { 95 t.Fatalf("positive requirement control rejected template %d", item.Template) 96 } 97 } 98 if player.nanoRequirementsMet(29618, player) || player.nanoRequirementsMet(27169, player) { 99 t.Fatal("negative effective skills admitted a nano") 100 } 101 } 102 103 func TestSignedNonpositiveTrickleDoesNotCreateATrainingCap(t *testing.T) { 104 player := &character{level: 3, stats: testCharacterStats()} 105 player.stats.Abilities = characterAbilitiesStats{6, 6, 6, 6, 6, 6} 106 player.activeNanos = []activeNano{{program: 207287}} 107 108 maximum := player.trainingMaximum(132) 109 110 if maximum != 17 { 111 t.Fatalf("negative untruncated ability bound replaced the title/level cap: %d", maximum) 112 } 113 } 114 115 func TestSignedPsychicTrickleFeedsResourcesAndRegenerationInterval(t *testing.T) { 116 player := &character{appearance: 1 << 4, level: 1, stats: testCharacterStats()} 117 player.stats.Abilities = characterAbilitiesStats{6, 6, 6, 6, 6, 6} 118 player.activeNanos = []activeNano{{program: 207287}} 119 120 health, nano := player.maximumHealth(), player.maximumNano() 121 healthInterval, nanoInterval := player.regenerationIntervals() 122 player.activeNanos = nil 123 restoredHealth, restoredNano := player.maximumHealth(), player.maximumNano() 124 125 if health != 34 || nano != 26 || healthInterval != 29 || nanoInterval != 30 || restoredHealth != 34 || restoredNano != 32 { 126 t.Fatalf("signed Psychic resource calculations: %d/%d intervals %d/%d, restored %d/%d", health, nano, healthInterval, nanoInterval, restoredHealth, restoredNano) 127 } 128 } 129 130 func TestSignedDirectInitiativeAndSpecialSkillKeepTheirSign(t *testing.T) { 131 player := &character{stats: testCharacterStats()} 132 player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1} 133 player.activeNanos = syntheticNanoModifiers(t, [][2]int32{{120, -10}, {149, -10}, {150, -200}}) 134 135 attack, recharge := player.attackDelays() 136 cast := player.nanoCastDelay(29618) 137 lock := player.weaponSpecialLockSeconds(gamedata.ItemTemplates[123742], 150) 138 139 if attack != 2*time.Second || recharge != 2010*time.Millisecond || cast != 2634999990*time.Nanosecond || lock != 21 { 140 t.Fatalf("signed timing: %v/%v cast %v lock %d", attack, recharge, cast, lock) 141 } 142 } 143 144 func TestSignedNegativeLiteracyFloorsOnlyFinalSaleLine(t *testing.T) { 145 for _, scenario := range []struct { 146 literacy int32 147 credits uint64 148 }{{-39, 605}, {-40, 599}, {-80, 593}, {-4000, 1}, {-4040, 0}} { 149 item := ownedItem{Type: 51018, Template: 209283, Quality: 1, Count: 1} 150 151 credits := shopSalePrice(item, 5, scenario.literacy) 152 153 if credits != scenario.credits { 154 t.Fatalf("literacy %d: got %d, want %d", scenario.literacy, credits, scenario.credits) 155 } 156 } 157 } 158 159 func TestSignedLiteracyReachesDurableVendorSale(t *testing.T) { 160 world, _ := newShopWorld(t) 161 player, client := world.characters[1], world.clients[1] 162 player.stats.Abilities.Intelligence = 1 163 player.stats.TradeRepair.ComputerLiteracy = 0 164 player.activeNanos = syntheticNanoModifiers(t, [][2]int32{{161, -40}}) 165 166 err := world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 4, [2]uint32{50000, 1}, [2]uint32{104, 64})[20:]) 167 if err != nil { 168 t.Fatal(err) 169 } 170 err = world.handleTrade(client, shopTradePacket([2]uint32{50000, 1}, 1, [2]uint32{}, [2]uint32{})[20:]) 171 restored := newTestInventoryServer(t).characters[1] 172 173 if err != nil || player.inventory.Credits != 699 || restored.inventory.Credits != 699 || len(player.inventory.Items) != 0 || len(restored.inventory.Items) != 0 { 174 t.Fatalf("signed sale was not committed: %d/%d, %v", player.inventory.Credits, restored.inventory.Credits, err) 175 } 176 } 177 178 func TestSignedNegativeBodyDevelopmentRegeneratesWithoutUnsignedWrap(t *testing.T) { 179 for _, full := range []bool{false, true} { 180 if !t.Run(fmt.Sprint(full), func(t *testing.T) { 181 world, _, start := newRegenerationWorld(t) 182 player := world.characters[1] 183 player.level = 200 184 player.activeNanos = syntheticNanoModifiers(t, [][2]int32{{152, -407}}) 185 if full { 186 player.health, player.nano = player.maximumHealth(), player.maximumNano() 187 } 188 189 for second := 2; second <= 58; second += 2 { 190 world.advanceRegeneration(start.Add(time.Duration(second) * time.Second)) 191 } 192 193 if !full && (player.health != 19 || player.nano != 0) { 194 t.Fatalf("signed recovery changed resources to %d/%d", player.health, player.nano) 195 } 196 if full && (player.health != 1010 || player.nano != 831) { 197 t.Fatalf("negative computed gain drained a full resource: %d/%d", player.health, player.nano) 198 } 199 }) { 200 return 201 } 202 } 203 } 204 205 func TestSignedNegativeMartialArtsDoesNotWrapDamageScaling(t *testing.T) { 206 world, _ := newCombatWorld(t) 207 player := world.characters[1] 208 player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1} 209 player.activeNanos = syntheticNanoModifiers(t, [][2]int32{{100, -105}}) 210 211 rating := player.attackRating() 212 damage := player.weaponDamage(world.characters[1000000], false, func(limit uint32) uint32 { return limit - 1 }) 213 214 if rating != -100 || damage != 4 { 215 t.Fatalf("signed attack rating or provisional scaling wrapped: %d/%d", rating, damage) 216 } 217 } 218 219 func TestDirectSkillNanosFeedWeaponRequirementsRatingAndRemoval(t *testing.T) { 220 player := &character{level: 100, profession: 8, stats: testCharacterStats(), inventory: playerInventory{ 221 Items: []ownedItem{{ID: 1, Type: 51018, Template: 205736, Quality: 100, Slot: 6, Count: 1}}, 222 }} 223 player.stats.Abilities = characterAbilitiesStats{1, 1, 1, 1, 1, 1} 224 player.stats.TradeRepair.Psychology = 248 225 player.activeNanos = []activeNano{{program: 207284}} 226 227 first, accepted := player.attackRating(), player.canEquip(player.inventory.Items[0]) 228 player.activeNanos = append(player.activeNanos, activeNano{program: 207288}) 229 overlapped := player.attackRating() 230 player.activeNanos = player.activeNanos[1:] 231 remaining, rejected := player.attackRating(), player.canEquip(player.inventory.Items[0]) 232 player.activeNanos = nil 233 234 if first != 300 || !accepted || overlapped != 322 || remaining != 270 || rejected || player.attackRating() != 248 || player.stats.TradeRepair.Psychology != 248 { 235 t.Fatalf("direct skill effects did not reach weapon rules: %d/%t/%d/%d/%t/%d", first, accepted, overlapped, remaining, rejected, player.attackRating()) 236 } 237 }