sescripts/Mimine/Ingots.cs

144 lines
4.7 KiB
C#

using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Sandbox.ModAPI.Ingame;
using VRage;
using VRage.Game;
using VRage.Game.ModAPI.Ingame;
namespace IngameScript
{
enum IngotsEnum
{
Iron = 0,
Nickel = 1,
Cobalt = 2,
Magnesium = 3,
Silicon = 4,
Silver = 5,
Gold = 6,
Platinum = 7,
Uranium = 8,
Gravel = 9, // Named 'Stone' in the API.
}
class IngotException : Exception
{
public IngotException(string mess) :
base(mess)
{
}
}
class Ingot
{
public static MyItemType FromEnum(IngotsEnum ingot)
{
var name = "";
switch (ingot)
{
case IngotsEnum.Iron: name = "Iron"; break;
case IngotsEnum.Nickel: name = "Nickel"; break;
case IngotsEnum.Cobalt: name = "Cobalt"; break;
case IngotsEnum.Magnesium: name = "Magnesium"; break;
case IngotsEnum.Silicon: name = "Silicon"; break;
case IngotsEnum.Silver: name = "Silver"; break;
case IngotsEnum.Gold: name = "Gold"; break;
case IngotsEnum.Platinum: name = "Platinum"; break;
case IngotsEnum.Uranium: name = "Uranium"; break;
case IngotsEnum.Gravel: name = "Stone"; break;
}
return MyItemType.MakeIngot(name);
}
public static IngotsEnum FromSubTypeId(string id)
{
IngotsEnum enumValue = IngotsEnum.Iron;
switch (id)
{
case "Iron": enumValue = IngotsEnum.Iron; break;
case "Nickel": enumValue = IngotsEnum.Nickel; break;
case "Cobalt": enumValue = IngotsEnum.Cobalt; break;
case "Magnesium": enumValue = IngotsEnum.Magnesium; break;
case "Silicon": enumValue = IngotsEnum.Silicon; break;
case "Silver": enumValue = IngotsEnum.Silver; break;
case "Gold": enumValue = IngotsEnum.Gold; break;
case "Platinum": enumValue = IngotsEnum.Platinum; break;
case "Uranium": enumValue = IngotsEnum.Uranium; break;
case "Stone": enumValue = IngotsEnum.Gravel; break;
default: throw new IngotException($"Unknown ingot id: {id}");
}
return enumValue;
}
}
internal class Ingots
{
readonly float[] ingots = new float[10];
public Ingots(float iron = 0, float nickel = 0, float cobalt = 0, float magnesium = 0, float silicon = 0, float silver = 0, float gold = 0, float platinum = 0, float uranium = 0, float gravel = 0)
{
ingots[(int)IngotsEnum.Iron] = iron;
ingots[(int)IngotsEnum.Nickel] = nickel;
ingots[(int)IngotsEnum.Cobalt] = cobalt;
ingots[(int)IngotsEnum.Magnesium] = magnesium;
ingots[(int)IngotsEnum.Silicon] = silicon;
ingots[(int)IngotsEnum.Silver] = silver;
ingots[(int)IngotsEnum.Gold] = gold;
ingots[(int)IngotsEnum.Platinum] = platinum;
ingots[(int)IngotsEnum.Uranium] = uranium;
ingots[(int)IngotsEnum.Gravel] = gravel;
}
public float this[IngotsEnum ingot]
{
get { return ingots[(int)ingot]; }
set { ingots[(int)ingot] = value; }
}
public static Ingots operator +(Ingots a, Ingots b)
{
var result = new Ingots();
for (int i = 0; i < result.ingots.Length; i++)
result.ingots[i] = a.ingots[i] + b.ingots[i];
return result;
}
public override string ToString()
{
var sb = new StringBuilder();
foreach (var ingot in Enum.GetValues(typeof(IngotsEnum)).Cast<IngotsEnum>())
{
sb.AppendLine($"{ingot}: {this[ingot]}");
}
return sb.ToString();
}
}
static class IngotsUtils
{
public static Ingots IngotsFromGridContainers(this IMyGridTerminalSystem gridTerminal, IMyCubeGrid grid)
{
var ingots = new Ingots();
foreach (var inventory in gridTerminal.GetAllInventories(grid))
{
var items = new List<MyInventoryItem>();
inventory.GetItems(items);
foreach (var item in items)
if (item.Type.TypeId == "MyObjectBuilder_Ingot")
ingots[Ingot.FromSubTypeId(item.Type.SubtypeId)] += (float)item.Amount;
}
return ingots;
}
}
}