Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions source/ContractHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,21 @@ private MechComponentDef CheckDefaults(MechComponentDef def)
return def;
}

if (!def.Is<LootableDefault>(out var lootable))
MechComponentDef lootableDef = null;

if (def.Is<LootableRandom>(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<LootableDefault>(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;
}

Expand Down
39 changes: 39 additions & 0 deletions source/LootableRandom.cs
Original file line number Diff line number Diff line change
@@ -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
}

}