From e739b4e64dc053423f99f51f5d19b1b93ee56bb7 Mon Sep 17 00:00:00 2001 From: annabunches Date: Sat, 22 Feb 2025 23:05:21 -0500 Subject: [PATCH] Modify the Resource Report in preparation for more customization and the ability to track more resources. --- InfoDisplays/OreReport.cs | 113 ----------------------- InfoDisplays/Program.cs | 6 +- InfoDisplays/ResourceReport.cs | 161 +++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+), 116 deletions(-) delete mode 100644 InfoDisplays/OreReport.cs create mode 100644 InfoDisplays/ResourceReport.cs diff --git a/InfoDisplays/OreReport.cs b/InfoDisplays/OreReport.cs deleted file mode 100644 index bab822b..0000000 --- a/InfoDisplays/OreReport.cs +++ /dev/null @@ -1,113 +0,0 @@ -// 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 _inventories = new List(); - - private Dictionary _ingotTypes = new Dictionary - { - ["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 _supply = new Dictionary(); - - 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 invBlocks = new List(); - 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 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; - } - } - } -} \ No newline at end of file diff --git a/InfoDisplays/Program.cs b/InfoDisplays/Program.cs index d463ffc..03611d9 100644 --- a/InfoDisplays/Program.cs +++ b/InfoDisplays/Program.cs @@ -9,7 +9,7 @@ namespace IngameScript public IConsole Console { get; private set; } public MyIni Ini { get; private set; } - private OreReport _oreReport = null; + private ResourceReport _oreReport = null; public Program() { @@ -35,12 +35,12 @@ namespace IngameScript // extract text surface if (display is IMyTextSurface) { - _oreReport = new OreReport(display as IMyTextSurface, this); + _oreReport = new ResourceReport(display as IMyTextSurface, this); Console.Print($"Showing Ore Report on '{display.CustomName}'"); } else if (display is IMyTextSurfaceProvider) { - _oreReport = new OreReport(((IMyTextSurfaceProvider)display).GetSurface(index), this); + _oreReport = new ResourceReport(((IMyTextSurfaceProvider)display).GetSurface(index), this); Console.Print($"Showing Ore Report on '{display.CustomName}'"); } else diff --git a/InfoDisplays/ResourceReport.cs b/InfoDisplays/ResourceReport.cs new file mode 100644 index 0000000..9c783f5 --- /dev/null +++ b/InfoDisplays/ResourceReport.cs @@ -0,0 +1,161 @@ +// Keeps track of all construction materials (ingots and ores) on the grid +// TODO: also track unprocessed stuff? +using System; +using System.Collections.Generic; +using System.Text; +using Sandbox.ModAPI.Ingame; +using VRage.Game.GUI.TextPanel; +using VRage.Game.ModAPI.Ingame; + +namespace IngameScript +{ + partial class Program + { + [Flags] + public enum ResourceType + { + Ore = 0x1, + Ingot = 0x2, + Component = 0x4, + } + + public class Resource + { + public string Name { get; private set; } + public string DisplayName { get; private set; } + public ResourceType Type { get; private set; } + public float OreAmount { get; set; } = 0f; + public float IngotAmount { get; set; } = 0f; + public float ComponentAmount { get; set; } = 0f; + + public MyItemType OreType { get; private set; } + public MyItemType IngotType { get; private set; } + public MyItemType ComponentType { get; private set; } + + private StringBuilder _builder = new StringBuilder(); + + public Resource(string name, string displayName = "", ResourceType type = ResourceType.Ore | ResourceType.Ingot) + { + Name = name; + DisplayName = displayName == "" ? name : displayName; + Type = type; + if (Type.HasFlag(ResourceType.Ore)) OreType = MyItemType.MakeOre(Name); + if (Type.HasFlag(ResourceType.Ingot)) IngotType = MyItemType.MakeIngot(Name); + if (Type.HasFlag(ResourceType.Ingot)) ComponentType = MyItemType.MakeComponent(Name); + } + + public void Reset() + { + OreAmount = 0f; + IngotAmount = 0f; + ComponentAmount = 0f; + } + + public override string ToString() + { + _builder.Clear(); + _builder.Append(DisplayName); + _builder.Append(": "); + if (Type.HasFlag(ResourceType.Ore)) + { + _builder.Append(OreAmount); + _builder.Append(' '); + } + if (Type.HasFlag(ResourceType.Ingot)) + { + _builder.Append(IngotAmount); + _builder.Append(' '); + } + if (Type.HasFlag(ResourceType.Component)) + { + _builder.Append(ComponentAmount); + _builder.Append(' '); + } + return _builder.ToString(); + } + } + + // TODO: factor out the display code once it is more complex + public class ResourceReport + { + private Program _program; + private IConsole _console; + private IMyTextSurface _display; + private StringBuilder _builder = new StringBuilder(); + private List _inventories = new List(); + + // TODO: make tracked resources configurable, and make it configurable/displayable from multiple screens... + // (and probably add support for *components* as well, oy) + private Dictionary _resources = new Dictionary + { + ["Iron"] = new Resource("Iron"), + ["Silicon"] = new Resource("Silicon"), + ["Nickel"] = new Resource("Nickel"), + ["Cobalt"] = new Resource("Cobalt"), + ["Gold"] = new Resource("Gold"), + ["Silver"] = new Resource("Silver"), + ["Platinum"] = new Resource("Platinum"), + ["Magnesium"] = new Resource("Magnesium"), + ["Uranium"] = new Resource("Uranium"), + ["Stone"] = new Resource("Stone", "", ResourceType.Ore), + ["Ice"] = new Resource("Ice", "", ResourceType.Ore), + ["Gravel"] = new Resource("Gravel", "", ResourceType.Ingot), + ["Scrap"] = new Resource("Scrap"), + ["Prototech"] = new Resource("PrototechScrap", "Prototech Scrap", ResourceType.Ingot), + }; + + public ResourceReport(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..."); + + // find all the inventories + List invBlocks = new List(); + 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 (Resource res in _resources.Values) res.Reset(); + foreach (IMyInventory inv in _inventories) + { + foreach (Resource res in _resources.Values) + { + res.OreAmount += (float)inv.GetItemAmount(res.OreType); + res.IngotAmount += (float)inv.GetItemAmount(res.IngotType); + } + } + } + + // TODO: just text for now; turn this into a nice graphical display eventually + public void Draw() + { + _builder.Clear(); + _builder.AppendLine("RESOURCE REPORT"); + _builder.AppendLine(); + foreach (Resource res in _resources.Values) + { + _builder.AppendLine(res.ToString()); + } + _display.WriteText(_builder.ToString()); + } + + private bool blockFilter(IMyTerminalBlock block) + { + return block.IsSameConstructAs(_program.Me) && block.InventoryCount > 0; + } + } + } +} \ No newline at end of file