diff --git a/Projects/TerraTech/TerraTech/UISnapshotPanelBuyAll.cs b/Projects/TerraTech/TerraTech/UISnapshotPanelBuyAll.cs index 4289791..f50b61e 100644 --- a/Projects/TerraTech/TerraTech/UISnapshotPanelBuyAll.cs +++ b/Projects/TerraTech/TerraTech/UISnapshotPanelBuyAll.cs @@ -43,87 +43,118 @@ namespace TerraTech { m_TechAvailLookup = m_TechAvailLookupTraverse.GetValue>(); } + private Dictionary CalculateMissingBlocks( + Dictionary blockAvailability, bool isSpawning) { + Dictionary missing = new Dictionary(); + + foreach (var kvp in blockAvailability) { + int numMissing; + if (isSpawning) + numMissing = kvp.Value.numRequired - kvp.Value.numInInventory; + else + numMissing = kvp.Value.numRequired - kvp.Value.numInInventory - kvp.Value.numOnPlayerTech; + + if (numMissing > 0 || !isSpawning) + missing.Add(kvp.Key, isSpawning ? numMissing : kvp.Value.numRequired); + } + return missing; + } + + private int CalculateTotalCost(Dictionary missingBlocks) { + int totalCost = 0; + RecipeManager recipeManager = Singleton.Manager.inst; + + foreach (var kvp in missingBlocks) { + int cost = recipeManager.GetBlockBuyPrice(kvp.Key) * kvp.Value; + totalCost += cost; + + if (Main.debugBuyAll.Value) + Console.WriteLine("{0} of {1} would cost {2}, total now {3}", kvp.Value, kvp.Key, cost, totalCost); + } + return totalCost; + } + + private bool TryPurchaseBlocks(Dictionary missingBlocks, int totalCost) { + ManPlayer player = Singleton.Manager.inst; + if (player.GetCurrentMoney() < totalCost) { + if (Main.debugBuyAll.Value) + Console.WriteLine("Not enough money, have {0} but need {1}, nothing to do", + player.GetCurrentMoney(), totalCost); + return false; + } + + player.PayMoney(totalCost); + foreach (var kvp in missingBlocks) { + if (Main.debugBuyAll.Value) + Console.WriteLine("Buying {0} of {1}", kvp.Value, kvp.Key); + player.PlayerInventory.HostAddItem(kvp.Key, kvp.Value); + } + return true; + } + + private bool ProcessPurchase( + Dictionary blockAvailability, bool isSpawning) { + try { + if (blockAvailability == null) { + if (Main.debugBuyAll.Value) + Console.WriteLine("Block availability is null (wtf?), nothing to do"); + return false; + } + + var missingBlocks = CalculateMissingBlocks(blockAvailability, isSpawning); + int totalCost = CalculateTotalCost(missingBlocks); + + if (totalCost > 0) { + if (Main.debugBuyAll.Value) + Console.WriteLine("Total cost: {0}", totalCost); + return TryPurchaseBlocks(missingBlocks, totalCost); + } + + if (Main.debugBuyAll.Value) + Console.WriteLine("No blocks missing or no cost calculated"); + return false; + } catch (Exception e) { + if (Main.debugBuyAll.Value) + Console.WriteLine("Error during purchase processing: {0}", e); + return false; + } + } + + private Dictionary GetCurrentBlockAvailability() { + SnapshotLiveData selectedSnapshotData = UISnapshotPanelBuyAll.selectedData; + Snapshot selectedSnapshot = selectedSnapshotData.m_Snapshot; + if (selectedSnapshot == null) { + if (Main.debugBuyAll.Value) + Console.WriteLine("Selected snapshot is null wtf??: {0}", gameObject.name); + return null; + } + + TechDataAvailValidation techDataAvail; + if (!m_TechAvailLookup.TryGetValue(selectedSnapshot, out techDataAvail)) { + if (Main.debugBuyAll.Value) + Console.WriteLine("Failed to find TechDataAvailValidation for snapshot: {0}", selectedSnapshot); + return null; + } + + var trav = Traverse.Create(techDataAvail); + var mBlockAvailabilityField = trav.Field("m_BlockAvailability"); + var mBlockAvailability = + mBlockAvailabilityField + .GetValue>(); + + return mBlockAvailability; + } + public void OnPointerClick(PointerEventData eventData) { if (Main.debugBuyAll.Value) Console.WriteLine("UISnapshotPanel.OnPointerClick: {0} {1}", gameObject.name, eventData.button); try { if (eventData.button == PointerEventData.InputButton.Right) { - SnapshotLiveData selectedSnapshotData = UISnapshotPanelBuyAll.selectedData; - if (selectedSnapshotData == null) { - if (Main.debugBuyAll.Value) - Console.WriteLine("Selected snapshot data is null wtf??: {0} {1}", gameObject.name, - eventData.button); - return; - } - - Snapshot selectedSnapshot = selectedSnapshotData.m_Snapshot; - if (selectedSnapshot == null) { - if (Main.debugBuyAll.Value) - Console.WriteLine("Selected snapshot is null wtf??: {0} {1}", gameObject.name, - eventData.button); - return; - } - - TechDataAvailValidation techDataAvail; - bool ok = m_TechAvailLookup.TryGetValue(selectedSnapshot, out techDataAvail); - if (!ok) { - if (Main.debugBuyAll.Value) - Console.WriteLine("Failed to find TechDataAvailValidation for snapshot: {0}", - selectedSnapshot); - return; - } - - var trav = Traverse.Create(techDataAvail); - var m_BlockAvailabilityField = trav.Field("m_BlockAvailability"); - var m_BlockAvailability = - m_BlockAvailabilityField - .GetValue>(); - - Dictionary missing = new Dictionary(); - int totalCost = 0; - RecipeManager recipeManager = Singleton.Manager.inst; - foreach (var kvp in m_BlockAvailability) { - if (kvp.Value.numRequired - kvp.Value.numInInventory > 0) { - int numMissing = kvp.Value.numRequired - kvp.Value.numInInventory; - missing.Add(kvp.Key, numMissing); - if (Main.debugBuyAll.Value) - Console.WriteLine("Missing {0} of {1}", numMissing, kvp.Key); - - int cost = recipeManager.GetBlockBuyPrice(kvp.Key) * numMissing; - totalCost += cost; - if (Main.debugBuyAll.Value) - Console.WriteLine("{0} of {1} would cost {2}, total now {3}", numMissing, kvp.Key, cost, - totalCost); - } - } - - if (totalCost > 0) { - if (Main.debugBuyAll.Value) - Console.WriteLine("Total cost: {0}", totalCost); - } - else { - if (Main.debugBuyAll.Value) - Console.WriteLine("No blocks missing, nothing to do"); - return; - } - - ManPlayer player = Singleton.Manager.inst; - if (player.GetCurrentMoney() < totalCost) { - if (Main.debugBuyAll.Value) - Console.WriteLine("Not enough money, have {0} but need {1}, nothing to do", - player.GetCurrentMoney(), totalCost); - return; - } - - player.PayMoney(totalCost); - foreach (var kvp in missing) { - if (Main.debugBuyAll.Value) - Console.WriteLine("Buying {0} of {1}", kvp.Value, kvp.Key); - player.PlayerInventory.HostAddItem(kvp.Key, kvp.Value); - } + // For now, we're using the spawn new tech logic (isSpawning = true) + // You can add another button/input handler that calls this with isSpawning = false for morphing + ProcessPurchase(GetCurrentBlockAvailability(), true); } - } - catch (Exception e) { + } catch (Exception e) { if (Main.debugBuyAll.Value) Console.WriteLine("Shit exploded fml: {0}", e); }