From 0b412f4a1848930d514da172c50f4f4b92343cb7 Mon Sep 17 00:00:00 2001 From: Chomp Date: Mon, 20 Jan 2025 23:13:17 +0000 Subject: [PATCH] BotGen fixes --- .../Core/Controllers/RagfairController.cs | 2 +- Libraries/Core/Generators/BotLootGenerator.cs | 5 +- Libraries/Core/Generators/PMCLootGenerator.cs | 3 ++ Libraries/Core/Helpers/ItemHelper.cs | 11 +++-- .../Core/Helpers/WeightedRandomHelper.cs | 49 +++++++++++++++++-- .../Core/Services/RagfairPriceService.cs | 12 +---- Libraries/Core/Utils/RandomUtil.cs | 14 +++--- 7 files changed, 68 insertions(+), 28 deletions(-) diff --git a/Libraries/Core/Controllers/RagfairController.cs b/Libraries/Core/Controllers/RagfairController.cs index 8a08545d..f687c42c 100644 --- a/Libraries/Core/Controllers/RagfairController.cs +++ b/Libraries/Core/Controllers/RagfairController.cs @@ -729,7 +729,7 @@ public class RagfairController } else { - requirementsPriceInRub += _ragfairPriceService.GetDynamicPriceForItem(requestedItemTpl).Value * item.Count.Value; + requirementsPriceInRub += _itemHelper.GetDynamicItemPrice(requestedItemTpl).Value * item.Count.Value; } } diff --git a/Libraries/Core/Generators/BotLootGenerator.cs b/Libraries/Core/Generators/BotLootGenerator.cs index 0c6f37ef..2007ce9b 100644 --- a/Libraries/Core/Generators/BotLootGenerator.cs +++ b/Libraries/Core/Generators/BotLootGenerator.cs @@ -793,10 +793,9 @@ public class BotLootGenerator( public void RandomiseMoneyStackSize(string botRole, TemplateItem itemTemplate, Item moneyItem) { // Get all currency weights for this bot type - var currencyWeights = _botConfig.CurrencyStackSize[botRole]; - if (currencyWeights is null) + if (!_botConfig.CurrencyStackSize.TryGetValue(botRole, out var currencyWeights)) { - currencyWeights = new(); + currencyWeights = _botConfig.CurrencyStackSize["default"]; } var currencyWeight = currencyWeights[moneyItem.Template]; diff --git a/Libraries/Core/Generators/PMCLootGenerator.cs b/Libraries/Core/Generators/PMCLootGenerator.cs index 2bb2a292..fef27bc0 100644 --- a/Libraries/Core/Generators/PMCLootGenerator.cs +++ b/Libraries/Core/Generators/PMCLootGenerator.cs @@ -59,6 +59,7 @@ public class PMCLootGenerator // Hydrate loot dictionary if empty if (_pocketLootPool is null) { + _pocketLootPool = new Dictionary(); var items = _databaseService.GetItems(); var pmcPriceOverrides = _databaseService.GetBots().Types[botRole.ToLower() == "pmcbear" ? "bear" : "usec"].BotInventory.Items.Pockets; @@ -138,6 +139,7 @@ public class PMCLootGenerator // Hydrate loot dictionary if empty if (_vestLootPool is null) { + _vestLootPool = new Dictionary(); var items = _databaseService.GetItems(); var pmcPriceOverrides = _databaseService.GetBots().Types[botRole.ToLower() == "pmcbear" ? "bear" : "usec"].BotInventory.Items.TacticalVest; @@ -219,6 +221,7 @@ public class PMCLootGenerator // Hydrate loot dictionary if empty if (_backpackLootPool is null) { + _backpackLootPool = new Dictionary(); var items = _databaseService.GetItems(); var pmcPriceOverrides = _databaseService.GetBots().Types[botRole.ToLower() == "pmcbear" ? "bear" : "usec"].BotInventory.Items.Backpack; diff --git a/Libraries/Core/Helpers/ItemHelper.cs b/Libraries/Core/Helpers/ItemHelper.cs index f43b16df..4bf7caf5 100644 --- a/Libraries/Core/Helpers/ItemHelper.cs +++ b/Libraries/Core/Helpers/ItemHelper.cs @@ -406,7 +406,7 @@ public class ItemHelper( var staticPrice = GetStaticItemPrice(tpl); var dynamicPrice = GetDynamicItemPrice(tpl); - return Math.Max(staticPrice ?? 0, dynamicPrice); + return Math.Max(staticPrice ?? 0d, dynamicPrice ?? 0d); } /// @@ -430,9 +430,14 @@ public class ItemHelper( /// /// Items tpl id to look up price /// Price in roubles (undefined if not found) - public double GetDynamicItemPrice(string tpl) + public double? GetDynamicItemPrice(string tpl) { - return _databaseService.GetPrices()[tpl]; + if (_databaseService.GetPrices().TryGetValue(tpl, out var price)) + { + return price; + } + + return null; } /// diff --git a/Libraries/Core/Helpers/WeightedRandomHelper.cs b/Libraries/Core/Helpers/WeightedRandomHelper.cs index a33b72ca..bc44be15 100644 --- a/Libraries/Core/Helpers/WeightedRandomHelper.cs +++ b/Libraries/Core/Helpers/WeightedRandomHelper.cs @@ -89,16 +89,59 @@ public class WeightedRandomHelper( /// Values to reduce public void ReduceWeightValues(Dictionary weightedDict) { - throw new NotImplementedException(); + // No values, nothing to reduce + if (weightedDict.Count == 0) + { + return; + } + + // Only one value, set to 1 and exit + if (weightedDict.Count == 1) + { + var kvp = weightedDict.FirstOrDefault(); + weightedDict[kvp.Key] = 1; + + return; + } + + var weights = weightedDict.Values.ToList(); + var commonDivisor = CommonDivisor(weights); + + // No point in dividing by 1 + if (commonDivisor == 1) + { + return; + } + + foreach (var kvp in weightedDict) { + weightedDict[kvp.Key] /= commonDivisor; + } } + /** + * Get the common divisor between all values in the passed in list and returns it + */ protected double CommonDivisor(List numbers) { - throw new NotImplementedException(); + var result = numbers[0]; + for (var i = 1; i < numbers.Count; i++) + { + result = Gcd(result, numbers[i]); + } + + return result; } protected double Gcd(double a, double b) { - throw new NotImplementedException(); + var x = a; + var y = b; + while (y != 0) + { + var temp = y; + y = x % y; + x = temp; + } + return x; } } diff --git a/Libraries/Core/Services/RagfairPriceService.cs b/Libraries/Core/Services/RagfairPriceService.cs index 9920e44c..d7f56c12 100644 --- a/Libraries/Core/Services/RagfairPriceService.cs +++ b/Libraries/Core/Services/RagfairPriceService.cs @@ -70,7 +70,7 @@ public class RagfairPriceService( public double GetFleaPriceForItem(string tplId) { // Get dynamic price (templates/prices), if that doesnt exist get price from static array (templates/handbook) - var itemPrice = GetDynamicPriceForItem(tplId) ?? GetStaticPriceForItem(tplId); + var itemPrice = _itemHelper.GetDynamicItemPrice(tplId) ?? GetStaticPriceForItem(tplId); if (itemPrice is null) { var itemFromDb = _itemHelper.GetItem(tplId); @@ -98,16 +98,6 @@ public class RagfairPriceService( throw new NotImplementedException(); } - /// - /// get the dynamic (flea) price for an item - /// - /// item template id to look up - /// price in roubles - public double? GetDynamicPriceForItem(string itemTpl) - { - return _databaseService.GetPrices()[itemTpl]; ; - } - /// /// Grab the static (handbook) for an item by its tplId /// diff --git a/Libraries/Core/Utils/RandomUtil.cs b/Libraries/Core/Utils/RandomUtil.cs index b66e372a..8751405d 100644 --- a/Libraries/Core/Utils/RandomUtil.cs +++ b/Libraries/Core/Utils/RandomUtil.cs @@ -418,21 +418,21 @@ public class RandomUtil /// A secure random number between 0 (inclusive) and 1 (exclusive). private static double GetSecureRandomNumber() { - byte[] buffer = new byte[6]; // 48 bits - using (RandomNumberGenerator rng = RandomNumberGenerator.Create()) + var buffer = new byte[6]; // 48 bits + using (var rng = RandomNumberGenerator.Create()) { rng.GetBytes(buffer); } - // Convert the byte array to unsigned long - ulong integer = 0; - for (int i = 0; i < buffer.Length; i++) + // Convert byte array to unsigned long + ulong value = 0; + for (var i = 0; i < buffer.Length; i++) { - integer |= (ulong)buffer[i] << (8 * (buffer.Length - 1 - i)); + value |= (ulong)buffer[i] << (8 * (buffer.Length - 1 - i)); } const ulong maxInteger = 281474976710656; // 2^48 - return (double)integer / maxInteger; + return (double)value / maxInteger; } ///