Remote Ship Control and Monitoring 0/5 (1)

posted in: Scripts | 3

This is a duo of scripts that run as a set.

One script runs on your ship (a miner, fighter, etc)

The other one runs on your base.

The two, together, enable communication between your ship and your base through laser antennas.

Ship Version:

const String ANTENNA_NAME = "AM% Laser Antenna";  
const int ID = 2;  
    
void Main()     
{     
    IMyLaserAntenna antenna = getBlockByName(ANTENNA_NAME, ID)  as IMyLaserAntenna;  
    antenna.SetCustomName(getName(ANTENNA_NAME, ID) +  " " + 
                          getPosition(antenna) + " " +  
                          getAutopilot() + " " + 
                          getInventory()); 
}     
   
IMyTerminalBlock getBlockByName(String name, int i) {  
    List<IMyTerminalBlock> work = new List<IMyTerminalBlock>();     
    String searchName = name;  
    searchName = searchName.Replace("%",i.ToString());  
    GridTerminalSystem.SearchBlocksOfName(searchName, work);          
    IMyTerminalBlock b = null;    
    for (int j = 0; j < work.Count; j++)       
    {     
        b = work[j];  
        break;  
    }    
    return b;  
}  
  
String getName(String name, int i) {  
    String searchName = name;  
    searchName = searchName.Replace("%",i.ToString());  
    return searchName;  
}  
  
String getPosition(IMyTerminalBlock b) {  
    return "Pos [X:" + Math.Round(b.GetPosition().GetDim(0),0) +  
           " Y:" + Math.Round(b.GetPosition().GetDim(1),0) +  
           " Z:" + Math.Round(b.GetPosition().GetDim(2),0) + "]"; 
} 
 
String getAutopilot() {  
    String retval = ""; 
    List<IMyTerminalBlock> rem = new List<IMyTerminalBlock>(); 
    GridTerminalSystem.GetBlocksOfType<IMyRemoteControl>(rem);      
    IMyRemoteControl remote = rem[0] as IMyRemoteControl; 
 
    Boolean engaged = false; 
    System.Text.RegularExpressions.Regex invregex = new System.Text.RegularExpressions.Regex(    
                    "Current waypoint: (.*)\\n",    
                    System.Text.RegularExpressions.RegexOptions.Singleline);        
 
    System.Text.RegularExpressions.Match match2 = invregex.Match(remote.DetailedInfo);    
    if (match2.Success) {    
        retval += "Autopilot [On]";  
        engaged = true; 
    } else { 
        retval += "Autopilot [Off]";  
    } 
     
    if (engaged) { 
        retval += " Destination [" + match2.Groups[1].Value + "]"; 
    } 
    return retval; 
} 
 
String getInventory() { 
    List<IMyTerminalBlock> work = new List<IMyTerminalBlock>();   
    List<IMyTerminalBlock> containers = new List<IMyTerminalBlock>();   
  
    List<IMyTerminalBlock> containerList = new List<IMyTerminalBlock>();   
    GridTerminalSystem.GetBlocksOfType<IMyCargoContainer>(work);     
    containers.AddRange(work); work.Clear(); 
    GridTerminalSystem.GetBlocksOfType<IMyShipConnector>(work);     
    containers.AddRange(work); work.Clear(); 
    GridTerminalSystem.GetBlocksOfType<IMyRefinery>(work);     
    containers.AddRange(work); work.Clear(); 
    GridTerminalSystem.GetBlocksOfType<IMyShipDrill>(work);     
    containers.AddRange(work); work.Clear(); 
  
    float maxVolume = 0.0f;  
    float currentVolume = 0.0f;  
    for (int i = 0; i < containers.Count; i++)     
    {     
        if (containers[i].CustomName.StartsWith("Miner")) { 
            var containerInvOwner = containers[i] as VRage.ModAPI.Ingame.IMyInventoryOwner;  
            var containerInventory = containerInvOwner.GetInventory(0);  
            maxVolume += (float)containerInventory.MaxVolume;  
            currentVolume += (float)containerInventory.CurrentVolume;  
        } 
    }  
    float percentageFull = (float)Math.Round(currentVolume / maxVolume * 100,2);  
    return "Inventory [" + percentageFull.ToString("##0.00") + "%" + "]"; 
}

Base version:

const String ANTENNA_NAME = "Laser Antenna (AM%)";  
const String MINER_PANELNAME = "Miner Consolidated Status Panel";  
const String DOCK_COCKPIT = "Dock Control Cockpit"; 
    
void Main()     
{     
    List<IMyTerminalBlock> work = new List<IMyTerminalBlock>();  
    GridTerminalSystem.SearchBlocksOfName(DOCK_COCKPIT, work);  
    double[] cockpitPosition = new double[3];   
    IMyTerminalBlock cockpit = work[0];  
    cockpitPosition[0] = cockpit.GetPosition().GetDim(0);  
    cockpitPosition[1] = cockpit.GetPosition().GetDim(1); 
    cockpitPosition[2] = cockpit.GetPosition().GetDim(2); 
 
    List<String> list = new List<String>();    
    for (int i=1; i <=3; i++) {  
          
        double[] minerPosition = new double[3];   
        IMyLaserAntenna antenna = getBlockByName(ANTENNA_NAME, i)  as IMyLaserAntenna;  
        Boolean connected = false;  
          
        if (antenna != null) {  
            list.Add(" "); 
            list.Add("Miner " + i + " Status");  
            list.Add("--------------");  
            System.Text.RegularExpressions.Regex posregex = new System.Text.RegularExpressions.Regex(    
                            "Pos \\[X:(.*) Y:(.*) Z:(.*?)\\]",    
                            System.Text.RegularExpressions.RegexOptions.Singleline);        
  
            System.Text.RegularExpressions.Match match = posregex.Match(antenna.DetailedInfo);    
            if (match.Success) {    
                connected = true;  
                double.TryParse(match.Groups[1].Value, out minerPosition[0]);     
                double.TryParse(match.Groups[2].Value, out minerPosition[1]);     
                double.TryParse(match.Groups[3].Value, out minerPosition[2]);   
                list.Add("Position: " + match.Groups[1].Value + " / " +  
                         match.Groups[2].Value + " / " +  
                         match.Groups[3].Value + "    " +  
                         getDistance(minerPosition, cockpitPosition));  
            }    
  
            System.Text.RegularExpressions.Regex invregex = new System.Text.RegularExpressions.Regex(    
                            "Inventory \\[(.*?)\\]",    
                            System.Text.RegularExpressions.RegexOptions.Singleline);        
   
            System.Text.RegularExpressions.Match match2 = invregex.Match(antenna.DetailedInfo);    
            if (match2.Success) {    
                connected = true;  
                list.Add("Inventory: " + match2.Groups[1].Value);  
            }  
             
            System.Text.RegularExpressions.Regex apregex = new System.Text.RegularExpressions.Regex(    
                            "Autopilot \\[(.*?)\\]",    
                            System.Text.RegularExpressions.RegexOptions.Singleline);        
   
            System.Text.RegularExpressions.Match match3 = apregex.Match(antenna.DetailedInfo);    
            if (match3.Success) {    
                connected = true;  
                list.Add("Autopilot: " + match3.Groups[1].Value);  
            }  
 
            System.Text.RegularExpressions.Regex dstregex = new System.Text.RegularExpressions.Regex(    
                            "Destination \\[(.*?)\\]",    
                            System.Text.RegularExpressions.RegexOptions.Singleline);        
   
            System.Text.RegularExpressions.Match match4 = dstregex.Match(antenna.DetailedInfo);    
            if (match4.Success) {    
                connected = true;  
                list.Add("Destination: " + match4.Groups[1].Value);  
            }  
        }   
        if (!connected) {  
            list.Add("** Connection to miner lost **");  
        }           
    }    
    work = new List<IMyTerminalBlock>();  
    GridTerminalSystem.SearchBlocksOfName(MINER_PANELNAME, work);  
    IMyTextPanel panel = work[0] as IMyTextPanel;  
    panel.WritePublicText(String.Join("\n",list.ToArray()), false);    
    panel.ShowTextureOnScreen();      
    panel.ShowPublicTextOnScreen();     
}     
   
IMyTerminalBlock getBlockByName(String name, int i) {  
    String searchName = name;  
    searchName = searchName.Replace("%",i.ToString());  
    List<IMyTerminalBlock> work = new List<IMyTerminalBlock>();  
    GridTerminalSystem.SearchBlocksOfName(searchName, work);          
    IMyTerminalBlock b = null;    
    for (int j = 0; j < work.Count; j++)       
    {     
        b = work[j];  
        break;  
    }    
    return b;  
}  
 
String getDistance(double[] p1, double[] p2) { 
    double d = Math.Sqrt((Math.Pow(p1[0] - p2[0],2) + Math.Pow(p1[1] - p2[1],2) + Math.Pow(p1[2] - p2[2],2))); 
    return "Distance: " + d.ToString("###,###,##0.00");     
} 
  

Please rate this post

3 Responses

  1. NugunsKnight

    Error with line 83
    VRage.ModAPI.Ingame.IMyInventoryOwner

  2. i know this isnt the right place for this but i cant find any forms anywhere..
    iv been playing around with scripts for a while now and iv been wanting to make a lcd ai sort of.
    what i want to do is basicly to start of with it would “whats your name” on the screen. then i would have a offset screen to input my name. i would put something like “jake” and it wold print on the first screen “hello jake”.
    how would one do this?

    • Hummm… that’s a good question. Unfortunately, the game does not allow us to request, via script, input from the user, so the only “solution” would be to use a “menu”ing system to allow input. Something like the way you typically input text on video game consoles: a screen with all the letters of the alphabet, you use the controller to pick the letter, and a button to select, with a “done” option on the screen as well.

      It would be kind of clunky, but should work.

Leave a Reply