sescripts/Mimine/AmmoManager.cs

127 lines
5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sandbox.ModAPI.Ingame;
using VRage;
using VRage.Game;
using VRage.Game.ModAPI.Ingame;
namespace IngameScript
{
internal class AmmoManager
{
readonly IMyGridTerminalSystem gridTerminal;
readonly IMyCubeGrid grid;
readonly Output output;
readonly IList<IMyAssembler> assemblers;
public AmmoManager(IMyGridTerminalSystem gridTerminal, IMyCubeGrid grid, Output output, IList<IMyAssembler> assemblers)
{
this.gridTerminal = gridTerminal;
this.grid = grid;
this.output = output;
this.assemblers = assemblers;
}
public class AmmoStatus
{
public MyFixedPoint NbOfCannonShell { get; set; }
public MyFixedPoint NbOfArtilleryShell { get; set; }
public MyFixedPoint NbOfGatlingAmmo { get; set; }
public AmmoStatus Copy()
{
return this.MemberwiseClone() as AmmoStatus;
}
}
public void BuildAmmo(AmmoStatus status)
{
if (this.assemblers.Count == 0)
return;
var mainAssembler = this.assemblers[0];
var statusWithAssemblerQueues = status.Copy();
// Add the current Queue.
foreach (var assembler in this.gridTerminal.GetBlocks<IMyAssembler>(grid: this.grid))
{
var queue = new List<MyProductionItem>();
assembler.GetQueue(queue);
foreach (var item in queue)
{
if (item.BlueprintId == Constants.CANNON_SHELL_BLUEPRINT_ID)
statusWithAssemblerQueues.NbOfCannonShell += item.Amount;
else if (item.BlueprintId == Constants.ARTILLERY_SHELL_BLUEPRINT_ID)
statusWithAssemblerQueues.NbOfArtilleryShell += item.Amount;
else if (item.BlueprintId == Constants.GATLING_AMMO_BLUEPRINT_ID)
statusWithAssemblerQueues.NbOfGatlingAmmo += item.Amount;
}
}
if (statusWithAssemblerQueues.NbOfCannonShell < Constants.NUMBER_OF_CANNON_SHELL)
{
var toBuild = Constants.NUMBER_OF_CANNON_SHELL - statusWithAssemblerQueues.NbOfCannonShell;
this.output.Print($"Requesting {toBuild} of {Constants.CANNON_SHELL_BLUEPRINT_ID.SubtypeName}");
mainAssembler.AddQueueItem(Constants.CANNON_SHELL_BLUEPRINT_ID, toBuild);
}
if (statusWithAssemblerQueues.NbOfArtilleryShell < Constants.NUMBER_OF_ARTILLERY_SHELL)
{
var toBuild = Constants.NUMBER_OF_ARTILLERY_SHELL - statusWithAssemblerQueues.NbOfArtilleryShell;
this.output.Print($"Requesting {toBuild} of {Constants.ARTILLERY_SHELL_BLUEPRINT_ID.SubtypeName}");
mainAssembler.AddQueueItem(Constants.ARTILLERY_SHELL_BLUEPRINT_ID, toBuild);
}
if (statusWithAssemblerQueues.NbOfGatlingAmmo < Constants.NUMBER_OF_GATLING_AMMO)
{
var toBuild = Constants.NUMBER_OF_GATLING_AMMO - statusWithAssemblerQueues.NbOfGatlingAmmo;
this.output.Print($"Requesting {toBuild} of {Constants.GATLING_AMMO_BLUEPRINT_ID.SubtypeName}");
mainAssembler.AddQueueItem(Constants.GATLING_AMMO_BLUEPRINT_ID, toBuild);
}
}
public void DisplayAmmoStatus(StringBuilder sb, AmmoStatus status)
{
var assaultConnonShell = "Assault Cannon Shell:";
sb.Append(assaultConnonShell);
sb.AppendLine($"{status.NbOfCannonShell}".PadLeft(Constants.CONSOLE_LINE_LENGTH - assaultConnonShell.Length));
var artilleryShell = "Artillery Shell:";
sb.Append(artilleryShell);
sb.AppendLine($"{status.NbOfArtilleryShell}".PadLeft(Constants.CONSOLE_LINE_LENGTH - artilleryShell.Length));
var gatlingAmmo = "Gatling Ammo Box";
sb.Append(gatlingAmmo);
sb.AppendLine($"{status.NbOfGatlingAmmo}".PadLeft(Constants.CONSOLE_LINE_LENGTH - gatlingAmmo.Length));
}
public AmmoStatus GetAmmoStatus()
{
var status = new AmmoStatus();
foreach (var inventory in this.gridTerminal.GetAllInventories(grid))
{
var items = new List<MyInventoryItem>();
inventory.GetItems(items);
foreach (var item in items)
{
if (item.Type.SubtypeId == "MediumCalibreAmmo")
status.NbOfCannonShell += item.Amount;
else if (item.Type.SubtypeId == "LargeCalibreAmmo")
status.NbOfArtilleryShell += item.Amount;
else if (item.Type.SubtypeId == "NATO_25x184mm")
status.NbOfGatlingAmmo += item.Amount;
}
}
return status;
}
}
}