Skip to content

Instantly share code, notes, and snippets.

@Fank
Last active December 16, 2015 10:29
Show Gist options
  • Save Fank/e148bd61921824103e8d to your computer and use it in GitHub Desktop.
Save Fank/e148bd61921824103e8d to your computer and use it in GitHub Desktop.
void Main(string argument) {
CargoSystem cs = new CargoSystem(GridTerminalSystem);
cs.CollectOreInto("_base Fank Cargo Container 2");
cs.CollectIngotInto("_base Fank Cargo Container 1");
}
public class CargoSystem {
private IMyGridTerminalSystem _gridTerminalSystem = null;
public CargoSystem(IMyGridTerminalSystem GridTerminalSystem) {
_gridTerminalSystem = GridTerminalSystem;
}
public void CollectIngotInto(string cargoString) {
IMyCargoContainer cargoContainer = this._gridTerminalSystem.GetBlockWithName(cargoString) as IMyCargoContainer;
List<IMyInventory> inventories = this.GetInventories(
new List<IMyTerminalBlock>() {
cargoContainer
}
,new List<Type>() {
typeof(IMyCargoContainer)
,typeof(IMyCollector)
,typeof(IMyConveyorSorter)
,typeof(IMyShipConnector)
,typeof(IMyRefinery)
}
);
for (int i = 0; i < inventories.Count; i++) {
var items = inventories[i].GetItems();
for (int j = items.Count - 1; j >= 0; j--) {
if (this.GetType(items[j]) == "Ingot") {
cargoContainer.GetInventory(0).TransferItemFrom(inventories[i], j);
}
}
}
}
public void CollectOreInto(string cargoString) {
IMyCargoContainer cargoContainer = this._gridTerminalSystem.GetBlockWithName(cargoString) as IMyCargoContainer;
List<IMyInventory> inventories = this.GetInventories(
new List<IMyTerminalBlock>() {
cargoContainer
}
,new List<Type>() {
typeof(IMyCargoContainer)
,typeof(IMyCollector)
,typeof(IMyConveyorSorter)
,typeof(IMyShipConnector)
}
);
for (int i = 0; i < inventories.Count; i++) {
var items = inventories[i].GetItems();
for (int j = items.Count - 1; j >= 0; j--) {
if (this.GetType(items[j]) == "Ore") {
cargoContainer.GetInventory(0).TransferItemFrom(inventories[i], j);
}
}
}
}
private List<IMyInventory> GetInventories(List<IMyTerminalBlock> blockBlacklist, List<Type> typeWhitelist) {
List<IMyInventory> inventories = new List<IMyInventory>();
List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
this._gridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(blocks);
for (int type = 0; type < typeWhitelist.Count; type++) {
for (int block = 0; block < blocks.Count; block++) {
if (CompareTypes(typeWhitelist[type], blocks[block].GetType())) {
for (int inventory = 0; inventory < blocks[block].GetInventoryCount(); inventory++) {
inventories.Add(blocks[block].GetInventory(inventory));
}
}
}
}
return inventories;
}
private string GetType(IMyInventoryItem Item) {
var type = Item.Content.TypeId.ToString();
return type.Substring(type.LastIndexOf('_') + 1);
}
private Boolean CompareTypes(Type a, Type b) {
var aString = a.ToString();
aString = aString.Substring(aString.LastIndexOf('.') + 2);
var bString = b.ToString();
bString = bString.Substring(bString.LastIndexOf('.') + 1);
return aString == bString;
}
}
@Failender
Copy link

OMG

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment