| | | 1 | | namespace RaidLoop.Core; |
| | | 2 | | |
| | | 3 | | public enum AttackMode |
| | | 4 | | { |
| | | 5 | | Standard, |
| | | 6 | | Burst, |
| | | 7 | | FullAuto |
| | | 8 | | } |
| | | 9 | | |
| | | 10 | | public interface IRng |
| | | 11 | | { |
| | | 12 | | int Next(int minInclusive, int maxExclusive); |
| | | 13 | | } |
| | | 14 | | |
| | | 15 | | public sealed class RandomRng : IRng |
| | | 16 | | { |
| | | 17 | | private readonly Random _random; |
| | | 18 | | |
| | | 19 | | public RandomRng(Random random) |
| | | 20 | | { |
| | | 21 | | _random = random; |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | public int Next(int minInclusive, int maxExclusive) |
| | | 25 | | { |
| | | 26 | | return _random.Next(minInclusive, maxExclusive); |
| | | 27 | | } |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | public readonly record struct DamageRange(int Min, int Max); |
| | | 31 | | |
| | | 32 | | public enum EncumbranceTier |
| | | 33 | | { |
| | | 34 | | Light, |
| | | 35 | | Medium, |
| | | 36 | | Heavy |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | public static class CombatBalance |
| | | 40 | | { |
| | 1 | 41 | | private static readonly int[] HeavyLoadByStrength = |
| | 1 | 42 | | [ |
| | 1 | 43 | | 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, |
| | 1 | 44 | | 115, 130, 150, 175, 200, 230, 260, 300, 350, 400, |
| | 1 | 45 | | 460, 520, 600, 700, 800, 920, 1040, 1200, 1400 |
| | 1 | 46 | | ]; |
| | | 47 | | |
| | | 48 | | public static int GetAbilityModifier(int score) |
| | | 49 | | { |
| | 36 | 50 | | return PlayerStatRules.GetAbilityModifier(score); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public static int GetRangedAttackBonusFromDexterity(int dexterity) |
| | | 54 | | { |
| | 4 | 55 | | return GetAbilityModifier(dexterity); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | public static int GetDefenseFromDexterity(int dexterity, int? maxDexBonus = null) |
| | | 59 | | { |
| | 6 | 60 | | var dexterityBonus = GetAbilityModifier(dexterity); |
| | 6 | 61 | | if (maxDexBonus.HasValue) |
| | | 62 | | { |
| | 1 | 63 | | dexterityBonus = Math.Min(dexterityBonus, maxDexBonus.Value); |
| | | 64 | | } |
| | | 65 | | |
| | 6 | 66 | | return 10 + dexterityBonus; |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public static int GetMaxHealthFromConstitution(int constitution) |
| | | 70 | | { |
| | 3 | 71 | | return 10 + (2 * Math.Max(0, constitution)); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | public static int GetCarryCapacityFromStrength(int strength) |
| | | 75 | | { |
| | 2 | 76 | | return 10 + Math.Max(0, strength - PlayerStatRules.MinimumScore); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public static int GetMaxEncumbranceFromStrength(int strength) |
| | | 80 | | { |
| | 68 | 81 | | return GetHeavyLoadLimit(strength); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public static EncumbranceTier GetEncumbranceTier(int strength, int carriedWeight) |
| | | 85 | | { |
| | 10 | 86 | | var lightLimit = GetLightLoadLimit(strength); |
| | 10 | 87 | | var mediumLimit = GetMediumLoadLimit(strength); |
| | | 88 | | |
| | 10 | 89 | | if (carriedWeight <= lightLimit) |
| | | 90 | | { |
| | 1 | 91 | | return EncumbranceTier.Light; |
| | | 92 | | } |
| | | 93 | | |
| | 9 | 94 | | if (carriedWeight <= mediumLimit) |
| | | 95 | | { |
| | 5 | 96 | | return EncumbranceTier.Medium; |
| | | 97 | | } |
| | | 98 | | |
| | 4 | 99 | | return EncumbranceTier.Heavy; |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | public static int GetEffectiveDexterityModifier(int dexterity, EncumbranceTier encumbranceTier) |
| | | 103 | | { |
| | 4 | 104 | | var dexterityModifier = GetAbilityModifier(dexterity); |
| | 4 | 105 | | var maxDexterityModifier = encumbranceTier switch |
| | 4 | 106 | | { |
| | 1 | 107 | | EncumbranceTier.Medium => 3, |
| | 2 | 108 | | EncumbranceTier.Heavy => 1, |
| | 1 | 109 | | _ => int.MaxValue |
| | 4 | 110 | | }; |
| | | 111 | | |
| | 4 | 112 | | return Math.Min(dexterityModifier, maxDexterityModifier); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | public static int GetEncumbranceAttackPenalty(EncumbranceTier encumbranceTier) |
| | | 116 | | { |
| | 3 | 117 | | return encumbranceTier switch |
| | 3 | 118 | | { |
| | 1 | 119 | | EncumbranceTier.Medium => 3, |
| | 1 | 120 | | EncumbranceTier.Heavy => 6, |
| | 1 | 121 | | _ => 0 |
| | 3 | 122 | | }; |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | public static int GetCharismaModifier(int charisma) |
| | | 126 | | { |
| | 16 | 127 | | return GetAbilityModifier(charisma); |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | public static Rarity GetMaxShopRarityFromChaBonus(int charismaModifier) |
| | | 131 | | { |
| | 13 | 132 | | return charismaModifier switch |
| | 13 | 133 | | { |
| | 1 | 134 | | >= 4 => Rarity.Legendary, |
| | 1 | 135 | | 3 => Rarity.Epic, |
| | 1 | 136 | | 2 => Rarity.Rare, |
| | 9 | 137 | | 1 => Rarity.Uncommon, |
| | 1 | 138 | | _ => Rarity.Common |
| | 13 | 139 | | }; |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | public static int GetShopPrice(int basePrice, int charismaModifier, bool isBuying) |
| | | 143 | | { |
| | 3 | 144 | | var modifier = Math.Max(0, charismaModifier); |
| | 3 | 145 | | var multiplier = isBuying |
| | 3 | 146 | | ? 1m - (0.05m * modifier) |
| | 3 | 147 | | : 1m + (0.05m * modifier); |
| | | 148 | | |
| | 3 | 149 | | return Math.Max(1, (int)Math.Round(basePrice * multiplier, MidpointRounding.AwayFromZero)); |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | public static bool ResolveAttackRoll(int roll, int attackBonus, int defense) |
| | | 153 | | { |
| | 6 | 154 | | if (roll == 1) |
| | | 155 | | { |
| | 1 | 156 | | return false; |
| | | 157 | | } |
| | | 158 | | |
| | 5 | 159 | | if (roll == 20) |
| | | 160 | | { |
| | 1 | 161 | | return true; |
| | | 162 | | } |
| | | 163 | | |
| | 4 | 164 | | return roll + attackBonus >= defense; |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | public static DamageRange GetDamageRange(string weaponName, AttackMode mode) |
| | | 168 | | { |
| | 17 | 169 | | var normalizedWeapon = NormalizeItemName(weaponName); |
| | 17 | 170 | | var dieCount = GetDamageDieCount(mode); |
| | 17 | 171 | | var dieSize = GetDamageDieSize(normalizedWeapon); |
| | | 172 | | |
| | 17 | 173 | | return new DamageRange(dieCount, dieCount * dieSize); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | public static int RollDamage(string weaponName, AttackMode mode, IRng rng) |
| | | 177 | | { |
| | 2 | 178 | | var dieCount = GetDamageDieCount(mode); |
| | 2 | 179 | | var dieSize = GetDamageDieSize(NormalizeItemName(weaponName)); |
| | 2 | 180 | | var total = 0; |
| | | 181 | | |
| | 16 | 182 | | for (var currentDie = 0; currentDie < dieCount; currentDie++) |
| | | 183 | | { |
| | 6 | 184 | | total += rng.Next(1, dieSize + 1); |
| | | 185 | | } |
| | | 186 | | |
| | 2 | 187 | | return total; |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | public static bool SupportsSingleShot(string weaponName) |
| | | 191 | | { |
| | 10 | 192 | | return NormalizeItemName(weaponName) switch |
| | 10 | 193 | | { |
| | 1 | 194 | | "pkp" => false, |
| | 9 | 195 | | _ => true |
| | 10 | 196 | | }; |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | public static bool SupportsSingleShot(Item? weapon) |
| | | 200 | | { |
| | 3 | 201 | | return SupportsSingleShot(GetItemLookupToken(weapon)); |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | public static bool SupportsBurstFire(string weaponName) |
| | | 205 | | { |
| | 10 | 206 | | return NormalizeItemName(weaponName) switch |
| | 10 | 207 | | { |
| | 1 | 208 | | "makarov" => true, |
| | 1 | 209 | | "ppsh" => true, |
| | 4 | 210 | | "ak74" => true, |
| | 1 | 211 | | "ak47" => true, |
| | 1 | 212 | | "svds" => true, |
| | 1 | 213 | | "pkp" => true, |
| | 0 | 214 | | "Rusty Knife" => false, |
| | 1 | 215 | | _ => false |
| | 10 | 216 | | }; |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | public static bool SupportsBurstFire(Item? weapon) |
| | | 220 | | { |
| | 3 | 221 | | return SupportsBurstFire(GetItemLookupToken(weapon)); |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | public static bool SupportsFullAuto(string weaponName) |
| | | 225 | | { |
| | 10 | 226 | | return NormalizeItemName(weaponName) switch |
| | 10 | 227 | | { |
| | 1 | 228 | | "rusty_knife" => false, |
| | 1 | 229 | | "makarov" => false, |
| | 1 | 230 | | "svds" => false, |
| | 0 | 231 | | "Rusty Knife" => false, |
| | 7 | 232 | | _ => true |
| | 10 | 233 | | }; |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | public static bool SupportsFullAuto(Item? weapon) |
| | | 237 | | { |
| | 3 | 238 | | return SupportsFullAuto(GetItemLookupToken(weapon)); |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | public static int GetBurstAttackPenalty(string weaponName) |
| | | 242 | | { |
| | 7 | 243 | | return NormalizeItemName(weaponName) switch |
| | 7 | 244 | | { |
| | 1 | 245 | | "makarov" => 3, |
| | 1 | 246 | | "ppsh" => 2, |
| | 1 | 247 | | "ak74" => 2, |
| | 1 | 248 | | "ak47" => 2, |
| | 1 | 249 | | "svds" => 2, |
| | 1 | 250 | | "pkp" => 2, |
| | 1 | 251 | | _ => 3 |
| | 7 | 252 | | }; |
| | | 253 | | } |
| | | 254 | | |
| | | 255 | | public static int GetArmorReduction(string armorName) |
| | | 256 | | { |
| | 11 | 257 | | return NormalizeItemName(armorName) switch |
| | 11 | 258 | | { |
| | 1 | 259 | | "nfm_thor" => 6, |
| | 1 | 260 | | "6b43_zabralo_sh_body_armor" => 5, |
| | 2 | 261 | | "fort_defender_2" => 4, |
| | 2 | 262 | | "6b13_assault_armor" => 3, |
| | 2 | 263 | | "bnti_kirasa_n" => 2, |
| | 2 | 264 | | "6b2_body_armor" => 1, |
| | 1 | 265 | | _ => 0 |
| | 11 | 266 | | }; |
| | | 267 | | } |
| | | 268 | | |
| | | 269 | | public static int ApplyArmorReduction(int incomingDamage, int armorReduction) |
| | | 270 | | { |
| | 3 | 271 | | var incoming = Math.Max(0, incomingDamage); |
| | 3 | 272 | | var reduced = incoming - Math.Max(0, armorReduction); |
| | 3 | 273 | | return Math.Max(1, reduced); |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | public static int GetBuyPrice(string itemName) |
| | | 277 | | { |
| | 54 | 278 | | return NormalizeItemName(itemName) switch |
| | 54 | 279 | | { |
| | 1 | 280 | | "bandage" => 60, |
| | 3 | 281 | | "medkit" => 120, |
| | 1 | 282 | | "ammo_box" => 80, |
| | 6 | 283 | | "makarov" => 240, |
| | 4 | 284 | | "ppsh" => 650, |
| | 5 | 285 | | "ak74" => 1250, |
| | 2 | 286 | | "svds" => 2200, |
| | 2 | 287 | | "ak47" => 1500, |
| | 2 | 288 | | "pkp" => 3200, |
| | 5 | 289 | | "6b2_body_armor" => 380, |
| | 3 | 290 | | "bnti_kirasa_n" => 640, |
| | 4 | 291 | | "6b13_assault_armor" => 900, |
| | 2 | 292 | | "fort_defender_2" => 1500, |
| | 2 | 293 | | "6b43_zabralo_sh_body_armor" => 1800, |
| | 2 | 294 | | "nfm_thor" => 2600, |
| | 3 | 295 | | "small_backpack" => 100, |
| | 3 | 296 | | "large_backpack" => 200, |
| | 0 | 297 | | "tactical_backpack" => 300, |
| | 2 | 298 | | "tasmanian_tiger_trooper_35" => 1600, |
| | 2 | 299 | | "6sh118" => 2400, |
| | 0 | 300 | | _ => 100 |
| | 54 | 301 | | }; |
| | | 302 | | } |
| | | 303 | | |
| | | 304 | | public static int GetBuyPrice(Item item) |
| | | 305 | | { |
| | 24 | 306 | | return GetBuyPrice(GetItemLookupToken(item)); |
| | | 307 | | } |
| | | 308 | | |
| | | 309 | | public static int GetMagazineCapacity(string weaponName) |
| | | 310 | | { |
| | 29 | 311 | | return NormalizeItemName(weaponName) switch |
| | 29 | 312 | | { |
| | 2 | 313 | | "rusty_knife" => 0, |
| | 2 | 314 | | "ppsh" => 35, |
| | 16 | 315 | | "ak74" => 30, |
| | 2 | 316 | | "svds" => 20, |
| | 2 | 317 | | "ak47" => 30, |
| | 2 | 318 | | "pkp" => 100, |
| | 0 | 319 | | "Rusty Knife" => 0, |
| | 3 | 320 | | _ => 8 |
| | 29 | 321 | | }; |
| | | 322 | | } |
| | | 323 | | |
| | | 324 | | public static int GetMagazineCapacity(Item? weapon) |
| | | 325 | | { |
| | 14 | 326 | | return GetMagazineCapacity(GetItemLookupToken(weapon)); |
| | | 327 | | } |
| | | 328 | | |
| | | 329 | | public static bool WeaponUsesAmmo(string weaponName) |
| | | 330 | | { |
| | 7 | 331 | | return GetMagazineCapacity(weaponName) > 0; |
| | | 332 | | } |
| | | 333 | | |
| | | 334 | | public static bool WeaponUsesAmmo(Item? weapon) |
| | | 335 | | { |
| | 11 | 336 | | return GetMagazineCapacity(weapon) > 0; |
| | | 337 | | } |
| | | 338 | | |
| | | 339 | | public static int GetBackpackCapacity(string? backpackName) |
| | | 340 | | { |
| | 17 | 341 | | return NormalizeItemName(backpackName ?? string.Empty) switch |
| | 17 | 342 | | { |
| | 2 | 343 | | "6sh118" => 10, |
| | 1 | 344 | | "tasmanian_tiger_trooper_35" => 8, |
| | 2 | 345 | | "tactical_backpack" => 6, |
| | 3 | 346 | | "large_backpack" => 4, |
| | 4 | 347 | | "small_backpack" => 3, |
| | 5 | 348 | | _ => 2 |
| | 17 | 349 | | }; |
| | | 350 | | } |
| | | 351 | | |
| | | 352 | | public static int GetBackpackCapacity(Item? backpack) |
| | | 353 | | { |
| | 6 | 354 | | if (backpack is null) |
| | | 355 | | { |
| | 1 | 356 | | return GetBackpackCapacity((string?)null); |
| | | 357 | | } |
| | | 358 | | |
| | 5 | 359 | | return GetBackpackCapacity(GetItemLookupToken(backpack)); |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | public static bool IsMedkit(Item? item) |
| | | 363 | | { |
| | 19 | 364 | | if (item is null) |
| | | 365 | | { |
| | 0 | 366 | | return false; |
| | | 367 | | } |
| | | 368 | | |
| | 19 | 369 | | if (item.ItemDefId > 0) |
| | | 370 | | { |
| | 11 | 371 | | return item.ItemDefId == 19; |
| | | 372 | | } |
| | | 373 | | |
| | 8 | 374 | | return NormalizeItemName(GetItemLookupToken(item)) == "medkit"; |
| | | 375 | | } |
| | | 376 | | |
| | | 377 | | public static int GetTotalEncumbrance(IEnumerable<Item> items) |
| | | 378 | | { |
| | 132 | 379 | | var total = items.Sum(item => Math.Max(0, item.Weight)); |
| | 26 | 380 | | return total; |
| | | 381 | | } |
| | | 382 | | |
| | | 383 | | public static string NormalizeItemName(string itemName) |
| | | 384 | | { |
| | 196 | 385 | | if (ItemCatalog.TryGet(itemName, out var item) && item is not null) |
| | | 386 | | { |
| | 185 | 387 | | return item.Key; |
| | | 388 | | } |
| | | 389 | | |
| | 11 | 390 | | var normalized = NormalizeWeaponName(itemName); |
| | 11 | 391 | | normalized = NormalizeArmorName(normalized); |
| | | 392 | | |
| | 11 | 393 | | return normalized; |
| | | 394 | | } |
| | | 395 | | |
| | | 396 | | private static string NormalizeWeaponName(string weaponName) |
| | | 397 | | { |
| | 11 | 398 | | return weaponName switch |
| | 11 | 399 | | { |
| | 0 | 400 | | "Makarov" => "makarov", |
| | 0 | 401 | | "PPSH" => "ppsh", |
| | 0 | 402 | | "AK74" => "ak74", |
| | 0 | 403 | | "SVDS" => "svds", |
| | 0 | 404 | | "AK47" => "ak47", |
| | 0 | 405 | | "PKP" => "pkp", |
| | 1 | 406 | | "Hunting Rifle" => "ak74", |
| | 0 | 407 | | "Rusty SMG" => "ppsh", |
| | 0 | 408 | | "Sawed Shotgun" => "ak47", |
| | 0 | 409 | | "Compact Carbine" => "ak74", |
| | 10 | 410 | | _ => weaponName |
| | 11 | 411 | | }; |
| | | 412 | | } |
| | | 413 | | |
| | | 414 | | private static string NormalizeArmorName(string armorName) |
| | | 415 | | { |
| | 11 | 416 | | return armorName switch |
| | 11 | 417 | | { |
| | 0 | 418 | | "6B2 body armor" => "6b2_body_armor", |
| | 0 | 419 | | "BNTI Kirasa-N" => "bnti_kirasa_n", |
| | 0 | 420 | | "6B13 assault armor" => "6b13_assault_armor", |
| | 0 | 421 | | "FORT Defender-2" => "fort_defender_2", |
| | 0 | 422 | | "6B43 Zabralo-Sh body armor" => "6b43_zabralo_sh_body_armor", |
| | 0 | 423 | | "NFM THOR" => "nfm_thor", |
| | 0 | 424 | | "Soft Vest" => "6b2_body_armor", |
| | 0 | 425 | | "Plate Carrier" => "6b13_assault_armor", |
| | 11 | 426 | | _ => armorName |
| | 11 | 427 | | }; |
| | | 428 | | } |
| | | 429 | | |
| | | 430 | | private static int GetLightLoadLimit(int strength) |
| | | 431 | | { |
| | 10 | 432 | | return GetHeavyLoadLimit(strength) / 3; |
| | | 433 | | } |
| | | 434 | | |
| | | 435 | | private static int GetMediumLoadLimit(int strength) |
| | | 436 | | { |
| | 10 | 437 | | return (GetHeavyLoadLimit(strength) * 2) / 3; |
| | | 438 | | } |
| | | 439 | | |
| | | 440 | | private static int GetHeavyLoadLimit(int strength) |
| | | 441 | | { |
| | 92 | 442 | | var normalizedStrength = Math.Max(1, strength); |
| | 92 | 443 | | if (normalizedStrength <= HeavyLoadByStrength.Length) |
| | | 444 | | { |
| | 88 | 445 | | return HeavyLoadByStrength[normalizedStrength - 1]; |
| | | 446 | | } |
| | | 447 | | |
| | 4 | 448 | | return GetHeavyLoadLimit(normalizedStrength - 10) * 4; |
| | | 449 | | } |
| | | 450 | | |
| | | 451 | | private static int GetDamageDieCount(AttackMode mode) |
| | | 452 | | { |
| | 19 | 453 | | return mode switch |
| | 19 | 454 | | { |
| | 9 | 455 | | AttackMode.Standard => 2, |
| | 5 | 456 | | AttackMode.Burst => 3, |
| | 5 | 457 | | AttackMode.FullAuto => 4, |
| | 0 | 458 | | _ => 2 |
| | 19 | 459 | | }; |
| | | 460 | | } |
| | | 461 | | |
| | | 462 | | private static int GetDamageDieSize(string weaponName) |
| | | 463 | | { |
| | 19 | 464 | | return NormalizeItemName(weaponName) switch |
| | 19 | 465 | | { |
| | 4 | 466 | | "ppsh" => 4, |
| | 4 | 467 | | "ak74" => 8, |
| | 2 | 468 | | "svds" => 12, |
| | 4 | 469 | | "ak47" => 10, |
| | 2 | 470 | | "pkp" => 12, |
| | 3 | 471 | | "makarov" => 6, |
| | 0 | 472 | | "rusty_knife" => 6, |
| | 0 | 473 | | "Rusty Knife" => 6, |
| | 0 | 474 | | _ => 6 |
| | 19 | 475 | | }; |
| | | 476 | | } |
| | | 477 | | |
| | | 478 | | private static string GetItemLookupToken(Item? item) |
| | | 479 | | { |
| | 60 | 480 | | if (item is null) |
| | | 481 | | { |
| | 0 | 482 | | return string.Empty; |
| | | 483 | | } |
| | | 484 | | |
| | 60 | 485 | | if (item.ItemDefId > 0 && ItemCatalog.TryGetByItemDefId(item.ItemDefId, out var authoredById) && authoredById is |
| | | 486 | | { |
| | 50 | 487 | | return authoredById.Key; |
| | | 488 | | } |
| | | 489 | | |
| | 10 | 490 | | if (!string.IsNullOrWhiteSpace(item.Key) && ItemCatalog.TryGetByKey(item.Key, out var authoredByKey) && authored |
| | | 491 | | { |
| | 0 | 492 | | return authoredByKey.Key; |
| | | 493 | | } |
| | | 494 | | |
| | 10 | 495 | | return item.Name; |
| | | 496 | | } |
| | | 497 | | } |
| | | 498 | | |