SHARE
TWEET

Radar

a guest Jan 12th, 2017 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. IMyRemoteControl remote;
  2. IMyTextPanel lcdCoords, lcdRadar, lcdLog, lcdEstado;
  3. IMyGyro gyro;
  4. IMyGravityGenerator grav1, grav2;
  5.  
  6. List<IMyTerminalBlock> controllerBlocks = new List<IMyTerminalBlock>();
  7. List<IMyTerminalBlock> forwardThrusters = new List<IMyTerminalBlock>();
  8. List<IMyTerminalBlock> backwardThrusters = new List<IMyTerminalBlock>();
  9.  
  10. Vector3D last;
  11. string[] GPS1, GPS2;
  12. //bool acelerando;
  13. int punto = 0;
  14. double x, y, z;
  15. double speed;
  16. bool finalizado = false;
  17. int ngps = 1;
  18.  
  19. public Program()
  20. {
  21.  
  22.     // The constructor, called only once every session and
  23.     // always before any other method is called. Use it to
  24.     // initialize your script.
  25.     //
  26.     // The constructor is optional and can be removed if not
  27.     // needed.
  28.  
  29.     //acelerando = false;
  30.     if (gyro == null)
  31.     {
  32.         List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  33.         GridTerminalSystem.GetBlocksOfType<IMyGyro>(blocks);
  34.         if (blocks.Count > 0)
  35.             gyro = blocks[0] as IMyGyro;
  36.     }
  37.     if (remote == null)
  38.     {
  39.         List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  40.         GridTerminalSystem.GetBlocksOfType<IMyRemoteControl>(blocks);
  41.         if (blocks.Count > 0)
  42.             remote = blocks[0] as IMyRemoteControl;
  43.     }
  44.     if (lcdCoords == null)
  45.     {
  46.         lcdCoords = GridTerminalSystem.GetBlockWithName("LCD Coords") as IMyTextPanel;
  47.     }
  48.     if (lcdRadar == null)
  49.     {
  50.         lcdRadar = GridTerminalSystem.GetBlockWithName("LCD Radar") as IMyTextPanel;
  51.     }
  52.     if (lcdLog == null)
  53.     {
  54.         lcdLog = GridTerminalSystem.GetBlockWithName("LCD Log") as IMyTextPanel;
  55.     }
  56.     if (lcdEstado == null)
  57.     {
  58.         lcdEstado = GridTerminalSystem.GetBlockWithName("LCD Estado") as IMyTextPanel;
  59.     }
  60.  
  61.     if ((lcdCoords == null) || (lcdRadar == null) || (lcdLog == null) || (lcdEstado == null))
  62.     {
  63.         Echo("Unable to find a LCD");
  64.         return;
  65.     }
  66.  
  67.     if (grav1 == null)
  68.     {
  69.         List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  70.         GridTerminalSystem.GetBlocksOfType<IMyGravityGenerator>(blocks);
  71.         if (blocks.Count > 0)
  72.             grav1 = blocks[0] as IMyGravityGenerator;
  73.     }
  74.     if (grav2 == null)
  75.     {
  76.         List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();
  77.         GridTerminalSystem.GetBlocksOfType<IMyGravityGenerator>(blocks);
  78.         if (blocks.Count > 0)
  79.             grav2 = blocks[1] as IMyGravityGenerator;
  80.     }
  81.  
  82.     if (controllerBlocks.Count == 0)
  83.     {
  84.         GridTerminalSystem.GetBlocksOfType<IMyShipController>(controllerBlocks);
  85.     }
  86.     if (backwardThrusters.Count == 0)
  87.     {
  88.         GridTerminalSystem.SearchBlocksOfName("Backward", backwardThrusters);
  89.     }
  90.     if (forwardThrusters.Count == 0)
  91.     {
  92.         GridTerminalSystem.SearchBlocksOfName("Forward", forwardThrusters);
  93.     }
  94.  
  95.     string[] loadData = remote.CustomName.Split(',');
  96.     if (loadData.Length == 2)
  97.     {
  98.         Int32.TryParse(loadData[0], out ngps);
  99.         Int32.TryParse(loadData[1], out punto);
  100.     }
  101.     if (ngps < 1) ngps = 1;
  102. }
  103.  
  104. public void Save()
  105. {
  106.  
  107.     // Called when the program needs to save its state. Use
  108.     // this method to save your state to the Storage field
  109.     // or some other means.
  110.     //
  111.     // This method is optional and can be removed if not
  112.     // needed.
  113.  
  114.     remote.SetCustomName(ngps.ToString() + "," + punto.ToString());
  115. }
  116.  
  117.  
  118.  
  119. public void Main(string argument)
  120. {
  121.  
  122.     // The main entry point of the script, invoked every time
  123.     // one of the programmable block's Run actions are invoked.
  124.     //
  125.     // The method itself is required, but the argument above
  126.     // can be removed if not needed.
  127.  
  128.     if (argument != "")
  129.     {
  130.         punto = 0;
  131.         if (Int32.TryParse(argument, out ngps))
  132.             {
  133.             lcdLog.WritePublicText("Iniciando con argumento: '"+argument+"'");
  134.             }
  135.         if (ngps == 0) ngps = 1;
  136.         finalizado = false;
  137.     }
  138.  
  139.     if (finalizado) { return; }
  140.  
  141.     Vector3D vector1 = remote.WorldMatrix.Forward;
  142.     GetSpeed();
  143.     if (((punto % 10) == 0) || (punto < 10))
  144.     {
  145.         if (((vector1 - Vector3D.Normalize(new Vector3D(x, y, z) - remote.GetPosition())).Length() > 0.1))
  146.         {
  147.             string[] GPS1 = lcdCoords.GetPublicText().Split(new string[] { "\n" }, StringSplitOptions.RemoveEmptyEntries);
  148.             if (ngps > GPS1.Length)
  149.             {
  150.                 lcdLog.WritePublicText("\nFINALIZADO", true);
  151.                 finalizado = true;
  152.                 return;
  153.             }
  154.             string[] GPS2 = GPS1[ngps - 1].Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
  155.             if (GPS2.Length < 5) { ngps++; return; }
  156.             x = Convert.ToDouble(GPS2[2]);
  157.             y = Convert.ToDouble(GPS2[3]);
  158.             z = Convert.ToDouble(GPS2[4]);
  159.             remote.ClearWaypoints();
  160.             remote.AddWaypoint(new Vector3D(x, y, z), "Objetivo: " + GPS1[ngps - 1]);
  161.             lcdLog.WritePublicText("\nNuevo rumbo: " + GPS1[ngps - 1], true);
  162.             remote.SetAutoPilotEnabled(true);
  163.             if (punto < 10) punto = 10;
  164.         }
  165.         else
  166.         {
  167.             remote.SetAutoPilotEnabled(false);
  168.             if (speed < 250)
  169.             {
  170.                 //TurnOffDampeners();
  171.                 for (int n = 0; n < backwardThrusters.Count; n++)
  172.                 {
  173.                     IMyThrust thrust = backwardThrusters[n] as IMyThrust;
  174.                     thrust.GetActionWithName("OnOff_Off").Apply(thrust);
  175.                     //lcdLog.WritePublicText("\nApagando thruster " + thrust.CustomName, true);
  176.                 }
  177.                 for (int n = 0; n < forwardThrusters.Count; n++)
  178.                 {
  179.                     IMyThrust thrust = forwardThrusters[n] as IMyThrust;
  180.                     thrust.SetValueFloat("Override", 12000f);
  181.                     //lcdLog.WritePublicText("\nAcelerando thruster " + thrust.CustomName, true);
  182.                 }
  183.             }
  184.             punto++;
  185.         }
  186.     }
  187.     else
  188.     {
  189.         if (speed > 299.9)
  190.         {
  191.             for (int n = 0; n < forwardThrusters.Count; n++)
  192.             {
  193.                 IMyThrust thrust = forwardThrusters[n] as IMyThrust;
  194.                 thrust.SetValueFloat("Override", 0f);
  195.                 //lcdLog.WritePublicText("\nDesactivando aceleracion en thruster " + thrust.CustomName, true);
  196.             }
  197.         }
  198.         else if (speed > 250)
  199.         {
  200.             IMyThrust thrust = forwardThrusters[0] as IMyThrust;
  201.             thrust.SetValueFloat("Override", 4f);
  202.         }
  203.  
  204.         if ((remote.GetPosition() - new Vector3D(x, y, z)).Length() < 5000)
  205.         {
  206.             lcdLog.WritePublicText("\nAlcanzado el punto " + ngps.ToString(), true);
  207.             for (int n = 0; n < backwardThrusters.Count; n++)
  208.             {
  209.                 IMyThrust thrust = backwardThrusters[n] as IMyThrust;
  210.                 thrust.GetActionWithName("OnOff_On").Apply(thrust);
  211.             }
  212.             for (int n = 0; n < forwardThrusters.Count; n++)
  213.             {
  214.                 IMyThrust thrust = forwardThrusters[n] as IMyThrust;
  215.                 thrust.SetValueFloat("Override", 0f);
  216.             }
  217.             punto = ngps * 1000000;
  218.             ngps++;
  219.             return;
  220.         }
  221.         else if (punto > 10) { punto++; }
  222.         if ((lcdRadar.GetPublicText().Trim() != "") || (lcdRadar.GetPrivateText().Trim() != ""))
  223.         {
  224.             lcdLog.WritePublicText("Contacto detectado en " + remote.GetPosition().ToString() + "\n", true);
  225.             if ((lcdRadar.GetPublicText().Trim() != lcdLog.GetPrivateText().Trim()) || (lcdLog.GetPrivateText().Trim() == ""))
  226.                 lcdLog.WritePrivateText(lcdRadar.GetPublicText() + "\n", true);
  227.             if ((lcdRadar.GetPrivateText().Trim() != lcdLog.GetPrivateText().Trim()) || (lcdLog.GetPrivateText().Trim() == ""))
  228.                 lcdLog.WritePrivateText(lcdRadar.GetPrivateText() + "\n", true);
  229.         }
  230.     }
  231.  
  232.     lcdEstado.WritePublicText("Punto: " + punto.ToString());
  233.     lcdEstado.WritePublicText("\nDesviacion: " + (vector1 - Vector3D.Normalize(new Vector3D(x, y, z) - remote.GetPosition())).Length().ToString(), true);
  234.     lcdEstado.WritePublicText("\nVelocidad = " + speed.ToString(), true);
  235.     lcdEstado.WritePublicText("\n" + vector1.ToString(), true);
  236.     lcdEstado.WritePublicText("\nDist: " + (new Vector3D(x, y, z) - remote.GetPosition()).Length().ToString(), true);
  237.     lcdEstado.WritePublicText("\n" + Vector3D.Normalize(new Vector3D(x, y, z) - remote.GetPosition()).ToString(), true);
  238.     lcdEstado.WritePublicText("\n" + (vector1 - Vector3D.Normalize(new Vector3D(x, y, z) - remote.GetPosition())).
  239.         Length().ToString(), true);
  240.     last = vector1;
  241.     Save();
  242. }
  243.  
  244. void GetSpeed()
  245. {
  246.     for (int i = 0; i < controllerBlocks.Count; i++)
  247.     {
  248.         IMyShipController controller = controllerBlocks[i] as IMyShipController;
  249.         if (controller != null)
  250.         {
  251.             if (i == 0) speed = controller.GetShipSpeed();
  252.         }
  253.     }
  254. }
  255.  
  256. void TurnOffDampeners()
  257. {
  258.     // Disable dampeners on all control blocks
  259.     for (int i = 0; i < controllerBlocks.Count; i++)
  260.     {
  261.         IMyShipController controller = controllerBlocks[i] as IMyShipController;
  262.         if (controller != null)
  263.         {
  264.             if (i == 0) speed = controller.GetShipSpeed();
  265.             if (controller.DampenersOverride == true)
  266.             {
  267.                 lcdLog.WritePublicText("\nApagando dampeners...", true);
  268.                 controller.GetActionWithName("DampenersOverride").Apply(controller);
  269.             }
  270.         }
  271.     }
  272. }
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top