space_engineers/InfoDisplays/OreReport.cs
2025-02-22 13:24:54 -05:00

113 lines
4.3 KiB
C#

// Keeps track of all construction materials (ingots) on the grid
// TODO: also track unprocessed stuff?
using System.Collections.Generic;
using System.Text;
using Sandbox.ModAPI.Ingame;
using VRage;
using VRage.Game.GUI.TextPanel;
using VRage.Game.ModAPI.Ingame;
namespace IngameScript
{
partial class Program
{
// TODO: factor out the display code once it is more complex
public class OreReport
{
private Program _program;
private IConsole _console;
private IMyTextSurface _display;
private StringBuilder _builder = new StringBuilder();
private List<IMyInventory> _inventories = new List<IMyInventory>();
private Dictionary<string, MyItemType> _ingotTypes = new Dictionary<string, MyItemType>
{
["Iron"] = MyItemType.MakeIngot("Iron"),
["Silicon"] = MyItemType.MakeIngot("Silicon"),
["Nickel"] = MyItemType.MakeIngot("Nickel"),
["Cobalt"] = MyItemType.MakeIngot("Cobalt"),
["Gold"] = MyItemType.MakeIngot("Gold"),
["Silver"] = MyItemType.MakeIngot("Silver"),
["Platinum"] = MyItemType.MakeIngot("Platinum"),
["Magnesium"] = MyItemType.MakeIngot("Magnesium"),
["Uranium"] = MyItemType.MakeIngot("Uranium"),
["Gravel"] = MyItemType.MakeIngot("Gravel"),
};
private Dictionary<string, float> _supply = new Dictionary<string, float>();
public OreReport(IMyTextSurface display, Program program)
{
_program = program;
_display = display;
_console = new PrefixedConsole(_program.Console, "OreReport");
// Configure display
_display.ContentType = ContentType.TEXT_AND_IMAGE;
_display.WriteText("Initializing...");
// initialize the supply map
// initializeIngotTypes();
foreach (string key in _ingotTypes.Keys)
{
_supply[key] = 0f;
}
// find all the inventories
List<IMyTerminalBlock> invBlocks = new List<IMyTerminalBlock>();
program.GridTerminalSystem.GetBlocksOfType(invBlocks, blockFilter);
foreach (IMyTerminalBlock block in invBlocks)
{
IMyInventory inv = block.GetInventory();
_inventories.Add(inv);
}
_console.Print($"Scanning {_inventories.Count} inventories.");
}
public void Update()
{
foreach (string key in _ingotTypes.Keys)
{
_supply[key] = 0f;
}
foreach (IMyInventory inv in _inventories)
{
foreach (KeyValuePair<string, MyItemType> kvp in _ingotTypes)
{
_supply[kvp.Key] += (float)inv.GetItemAmount(kvp.Value);
}
}
}
// TODO: just text for now; turn this into a nice graphical display eventually
public void Draw()
{
_builder.Clear();
_builder.AppendLine("INGOT REPORT");
_builder.AppendLine();
foreach (string key in _supply.Keys)
{
_builder.Append(key);
_builder.Append(" ");
_builder.Append(_supply[key].ToString());
_builder.AppendLine();
}
_display.WriteText(_builder.ToString());
}
// private void initializeIngotTypes()
// {
// string[] types = { "Iron", "Silicon", "Nickel", "Cobalt", "Gold", "Silver", "Platinum", "Magnesium", "Uranium", "Gravel" };
// foreach (string type in types)
// {
// _ingotTypes[type] = new MyItemType("Ingot", type);
// }
// }
private bool blockFilter(IMyTerminalBlock block)
{
return block.IsSameConstructAs(_program.Me) && block.InventoryCount > 0;
}
}
}
}