From e7d6c55d9be62e2000979b04de0645950ec6b9a6 Mon Sep 17 00:00:00 2001 From: sh4dow Date: Sat, 14 Oct 2023 13:37:22 +0200 Subject: [PATCH] added LootableRandom tag --- source/ContractHelper.cs | 20 ++++++++++---------- source/LootableRandom.cs | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 source/LootableRandom.cs diff --git a/source/ContractHelper.cs b/source/ContractHelper.cs index 5241de7..7e2700d 100644 --- a/source/ContractHelper.cs +++ b/source/ContractHelper.cs @@ -63,21 +63,21 @@ private MechComponentDef CheckDefaults(MechComponentDef def) return def; } - if (!def.Is(out var lootable)) + MechComponentDef lootableDef = null; + + if (def.Is(out var lootableR)) { - return null; + string itemID = lootableR.GetChoice(); + if(itemID != null) lootableDef = UnityGameInstance.BattleTechGame.DataManager.GetMechComponentDef(def.ComponentType, itemID); } - var lootableDef = UnityGameInstance.BattleTechGame.DataManager.GetMechComponentDef(def.ComponentType, lootable.ItemID); - if (lootableDef == null) - { - return null; + if (lootableDef == null && def.Is(out var lootable)) { + lootableDef = UnityGameInstance.BattleTechGame.DataManager.GetMechComponentDef(def.ComponentType, lootable.ItemID); } - if (lootableDef.CCFlags().NoSalvage) - { - return null; - } + // if (lootableDef == null) return null; + //if (lootableDef.CCFlags().NoSalvage) return null; + return lootableDef; } diff --git a/source/LootableRandom.cs b/source/LootableRandom.cs new file mode 100644 index 0000000..a1700e2 --- /dev/null +++ b/source/LootableRandom.cs @@ -0,0 +1,39 @@ +using System.Linq; +using CustomComponents; + +namespace CustomSalvage; + +[CustomComponent("LootableRandom")] +public class LootableRandom : SimpleCustomComponent { + public class option { + public string ItemID { get; set; } + public float Weight { get; set; } + } + + public option[] Options { get; set; } + + public string GetChoice() { + if (Options == null || Options.Length == 0) + { + Log.Main.Warning?.Log("invalid LootableRandom tag"); + return null; + } + float total = Options.Sum(o => o.Weight); + if (total < 0) + { + Log.Main.Warning?.Log("invalid LootableRandom tag"); + return null; + } + float choice = UnityEngine.Random.Range(0.0f, total); + Log.Main.Trace?.Log($"GetChoice: total {total}, choice {choice}"); + foreach (var o in Options) + { + Log.Main.Trace?.Log($"item {o.ItemID}, weight {o.Weight}, choice {choice}"); + if (choice <= o.Weight) return o.ItemID; + choice -= o.Weight; + } + + return null; // should never actually be reached + } + +} \ No newline at end of file