Add display panel script, right now it just reports on stored raw materials.

This commit is contained in:
Anna Rose 2025-02-22 13:24:29 -05:00
parent d7018f2c97
commit 9f647edb95
8 changed files with 263 additions and 0 deletions

View File

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netframework48</TargetFramework>
<RootNamespace>IngameScript</RootNamespace>
<LangVersion>6</LangVersion>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<Configurations>Release;Debug</Configurations>
<Platforms>x64</Platforms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Mal.Mdk2.PbAnalyzers" Version="2.1.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Mal.Mdk2.PbPackager" Version="2.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Mal.Mdk2.References" Version="2.2.4" />
</ItemGroup>
<ItemGroup>
<None Remove="Instructions.readme" />
<AdditionalFiles Include="Instructions.readme" />
<AdditionalFiles Include="thumb.png" />
</ItemGroup>
<Import Project="..\Mixins\Console\Console.projitems" Label="shared" />
</Project>

View File

@ -0,0 +1,22 @@
; This file is project specific and should be checked in to source control.
[mdk]
; This is a programmable block script project.
; You should not change this.
type=programmableblock
; Toggle trace (on|off) (verbose output)
trace=off
; What type of minification to use (none|trim|stripcomments|lite|full)
; none: No minification
; trim: Removes unused types (NOT members).
; stripcomments: trim + removes comments.
; lite: stripcomments + removes leading/trailing whitespace.
; full: lite + renames identifiers to shorter names.
minify=none
; A list of files and folder to ignore when creating the script.
; This is a comma separated list of glob patterns.
; See https://code.visualstudio.com/docs/editor/glob-patterns
ignores=obj/**/*,MDK/**/*,**/*.debug.cs

View File

@ -0,0 +1,7 @@
; This file is _local_ to your machine and should not be checked in to source control.
[mdk]
; Where to output the script to (auto|specific path)
output=auto
; Override the default binary path (auto|specific path)
binarypath=auto

View File

@ -0,0 +1,5 @@
R e a d m e
-----------
In this file you can include any instructions or other comments you want to have injected onto the
top of your final script. You can safely delete this file if you do not want any such comments.

113
InfoDisplays/OreReport.cs Normal file
View File

@ -0,0 +1,113 @@
// 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("Ingot_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;
}
}
}
}

83
InfoDisplays/Program.cs Normal file
View File

@ -0,0 +1,83 @@
using Sandbox.ModAPI.Ingame;
using System.Collections.Generic;
using VRage.Game.ModAPI.Ingame.Utilities;
namespace IngameScript
{
public partial class Program : MyGridProgram, IConsoleProgram
{
public IConsole Console { get; private set; }
public MyIni Ini { get; private set; }
private OreReport _oreReport = null;
public Program()
{
Ini = new MyIni();
Console = new MainConsole(this, "Info Displays");
// find displays that are configured to show info, create the necessary objects.
// TODO: currently we only support one display per report, and we just use the first one
List<IMyTerminalBlock> displays = new List<IMyTerminalBlock>();
GridTerminalSystem.GetBlocksOfType(displays, blockFilter);
foreach (IMyTerminalBlock display in displays)
{
Ini.TryParse(display.CustomData);
List<MyIniKey> keys = new List<MyIniKey>();
Ini.GetKeys("infoDisplay", keys);
foreach (MyIniKey key in keys)
{
switch (key.Name)
{
case "ingotReport":
if (_oreReport != null) break;
int index = Ini.Get(key).ToInt32();
// extract text surface
if (display is IMyTextSurface)
{
_oreReport = new OreReport(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);
Console.Print($"Showing Ore Report on '{display.CustomName}'");
}
else
{
Console.Print("Tried to configure object without display surface.");
}
break;
default:
Console.Print($"Display type '{key.Name}' not defined.");
break;
}
}
}
Console.UpdateTickCount();
if (_oreReport == null)
{
Console.Print("No configured displays found. Shutting down.");
return;
}
Runtime.UpdateFrequency = UpdateFrequency.Update100;
}
public void Main(string argument, UpdateType updateSource)
{
Console.UpdateTickCount();
if (_oreReport != null)
{
_oreReport.Update();
_oreReport.Draw();
}
}
private bool blockFilter(IMyTerminalBlock block)
{
return block.IsSameConstructAs(this.Me) && MyIni.HasSection(block.CustomData, "infoDisplay");
}
}
}

BIN
InfoDisplays/thumb.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 389 KiB

View File

@ -15,6 +15,8 @@ Project("{8A3CDCC5-4B55-4D87-A415-698A0E1FF06F}") = "Console", "Mixins\Console\C
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DockingController", "DockingController\DockingController.csproj", "{FDF834C0-D680-4FFA-8238-FE1FBDCF5B9D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InfoDisplays", "InfoDisplays\InfoDisplays.csproj", "{4C7C8B10-1E43-453E-A9DA-A3626FE85ACC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -37,6 +39,10 @@ Global
{FDF834C0-D680-4FFA-8238-FE1FBDCF5B9D}.Debug|Any CPU.Build.0 = Debug|x64
{FDF834C0-D680-4FFA-8238-FE1FBDCF5B9D}.Release|Any CPU.ActiveCfg = Release|x64
{FDF834C0-D680-4FFA-8238-FE1FBDCF5B9D}.Release|Any CPU.Build.0 = Release|x64
{4C7C8B10-1E43-453E-A9DA-A3626FE85ACC}.Debug|Any CPU.ActiveCfg = Debug|x64
{4C7C8B10-1E43-453E-A9DA-A3626FE85ACC}.Debug|Any CPU.Build.0 = Debug|x64
{4C7C8B10-1E43-453E-A9DA-A3626FE85ACC}.Release|Any CPU.ActiveCfg = Release|x64
{4C7C8B10-1E43-453E-A9DA-A3626FE85ACC}.Release|Any CPU.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE