Implement auto buying missing blocks for a given tech snapshot
This commit is contained in:
@@ -15,6 +15,7 @@ namespace TerraTech {
|
||||
private const string PluginVersion = "1.0.0";
|
||||
|
||||
public static ConfigEntry<bool> debug;
|
||||
public static ConfigEntry<bool> debugBuyAll;
|
||||
public static ConfigEntry<float> xpMultiplier;
|
||||
public static ConfigEntry<float> moneyMultiplier;
|
||||
public static ConfigEntry<float> heartbeatIntervalMultiplier;
|
||||
@@ -25,6 +26,7 @@ namespace TerraTech {
|
||||
|
||||
public void Awake() {
|
||||
debug = Config.Bind("General", "Debug", false);
|
||||
debugBuyAll = Config.Bind("General", "Debug Buy All", false);
|
||||
float min = 0.01f;
|
||||
float max = 32f;
|
||||
|
||||
|
@@ -60,6 +60,7 @@
|
||||
<Compile Include="ModuleFuelTankManager.cs" />
|
||||
<Compile Include="ProjectilePatch.cs" />
|
||||
<Compile Include="TankManager.cs" />
|
||||
<Compile Include="UISnapshotPanelBuyAll.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="0Harmony">
|
||||
@@ -74,6 +75,9 @@
|
||||
<Reference Include="UnityEngine.CoreModule">
|
||||
<HintPath>$(GAME_MANAGED)/UnityEngine.CoreModule.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="UnityEngine.UI">
|
||||
<HintPath>$(GAME_MANAGED)/UnityEngine.UI.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="UnityEngine.Networking">
|
||||
<HintPath>$(GAME_MANAGED)/UnityEngine.Networking.dll</HintPath>
|
||||
</Reference>
|
||||
|
132
Projects/TerraTech/TerraTech/UISnapshotPanelBuyAll.cs
Normal file
132
Projects/TerraTech/TerraTech/UISnapshotPanelBuyAll.cs
Normal file
@@ -0,0 +1,132 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using HarmonyLib;
|
||||
using Snapshots;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace TerraTech {
|
||||
[HarmonyPatch]
|
||||
public class UISnapshotPanelBuyAll {
|
||||
public static UISnapshotPanel panel;
|
||||
public static Traverse panelTraverse;
|
||||
public static SnapshotLiveData selectedData;
|
||||
|
||||
[HarmonyPostfix]
|
||||
[HarmonyPatch(typeof(UISnapshotPanel), "OnPool")]
|
||||
public static void PostfixCreate(UISnapshotPanel __instance) {
|
||||
if (Main.debugBuyAll.Value)
|
||||
Console.WriteLine("UISnapshotPanel.OnPool: {0}", __instance);
|
||||
panel = __instance;
|
||||
panelTraverse = Traverse.Create(__instance);
|
||||
var placeButton = panelTraverse.Field("m_PlaceButton").GetValue<Button>();
|
||||
placeButton.gameObject.AddComponent<RightClickHandler>();
|
||||
}
|
||||
|
||||
[HarmonyPostfix]
|
||||
[HarmonyPatch(typeof(UISnapshotPanel), "OnSelectedChanged")]
|
||||
public static void PostfixCreate(UISnapshotPanel __instance, ref SnapshotLiveData selectedData) {
|
||||
if (Main.debugBuyAll.Value)
|
||||
Console.WriteLine("UISnapshotPanel.OnSelectedChanged: {0}", __instance);
|
||||
UISnapshotPanelBuyAll.selectedData = selectedData;
|
||||
}
|
||||
}
|
||||
|
||||
public class RightClickHandler : MonoBehaviour, IPointerClickHandler {
|
||||
public static Traverse m_TechAvailLookupTraverse;
|
||||
public static Dictionary<Snapshot, TechDataAvailValidation> m_TechAvailLookup;
|
||||
|
||||
private void Awake() {
|
||||
var trav = Traverse.Create(Singleton.Manager<ManSnapshots>.inst);
|
||||
m_TechAvailLookupTraverse = trav.Field("m_TechAvailLookup");
|
||||
m_TechAvailLookup = m_TechAvailLookupTraverse.GetValue<Dictionary<Snapshot, TechDataAvailValidation>>();
|
||||
}
|
||||
|
||||
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<BlockTypes, TechDataAvailValidation.BlockTypeAvailability>>();
|
||||
|
||||
Dictionary<BlockTypes, int> missing = new Dictionary<BlockTypes, int>();
|
||||
int totalCost = 0;
|
||||
RecipeManager recipeManager = Singleton.Manager<RecipeManager>.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<ManPlayer>.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);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (Main.debugBuyAll.Value)
|
||||
Console.WriteLine("Shit exploded fml: {0}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user