Tuesday, March 10, 2015

Space Engineers and stone removal

This is one of those cases where the editor just isn't wide enough. Attached is some code that I wrote for a friend because I know some C#. It is for the starting miner ship, but it's not completely functional because it does not sort all items to the connector. You need a second connector to dump waste. I only had a basic understanding of the game's systems. Good luck!
Code:
 void Main() 
 { 
    var blockTerminal = new List<IMyTerminalBlock>();
    var blockProduction = new List<IMyTerminalBlock>();  
    
    GridTerminalSystem.GetBlocksOfType<IMyCargoContainer>(blockTerminal); 
    GridTerminalSystem.GetBlocksOfType<IMyProductionBlock>(blockProduction); 
    
    //now get the Wastedump connector.  You need to use an object that can eject stuff 
    IMyShipConnector wasteboxport = GridTerminalSystem.GetBlockWithName("Connector") as IMyShipConnector;    
    var wastebox = wasteboxport as Sandbox.ModAPI.InterfacesIMyInventoryOwner;     //this is the transfer target
    
    blockTerminal.AddRange(blockProduction);  //merge lists, notice the "AddRange" due to the class type
    blockTerminal.Add(wasteboxport);  //merge lists, note the "Add"


    int i_counterBlockTerminal;
    int i_counter_source;
    int i_counter_inventory;
    
    if(blockTerminal.Count==0)
    {
    ;;
    }else
    {
        for (i_counterBlockTerminal=0; i_counterBlockTerminal<blockTerminal.Count; i_counterBlockTerminal++)//loop through each block
        {
            var inventory_source = blockTerminal[i_counterBlockTerminal] as Sandbox.ModAPI.InterfacesIMyInventoryOwner; 
       //         throw new Exception("inventory_source.InventoryCount: "+inventory_source.InventoryCount);
            for(i_counter_source=0; i_counter_source<inventory_source.InventoryCount; i_counter_source++)//loop through each item of the selected block            
            {       // also, because inventory is of type IMyInventoryOwner, I had to make the "Count" method InventoryCount
                   var inventory_items = inventory_source.GetInventory(i_counter_source).GetItems(); 
                   // throw new Exception("inventory_items.Count: "+inventory_items.Count);
                for(i_counter_inventory=0; i_counter_inventory<inventory_items.Count; i_counter_inventory++) 
                {
                    var item_name = inventory_items[i_counter_inventory].ToString().Split('x');   //need to split the "amount x name" string
                    //throw new Exception("blockTerminal["+i_counterBlockTerminal+"]inventory_source["+i_counter_source+"]: "+item_name[0]);
                    string item_type = item_name[1];
                    if (item_type.Contains(@"Ingot/Stone") || item_type.Contains(@"Ore/Stone"))
                    {
                        //move everything into the waste connector
                        //throw new Exception("item_type: "+item_type);
                        inventory_source.GetInventory(i_counter_source).TransferItemTo(wastebox.GetInventory(0), i_counter_inventory, null, true, null); 
                    }  
                }

            }
        }
        //the end of moving things to the connector
        wasteboxport.GetActionWithName("ThrowOut").Apply(wasteboxport); 
    }
 }