Whiplash141

RoboDoom Code Fix

Jul 25th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.13 KB | None | 0 0
  1. double CruiseControlSpeed = 50;
  2. IMyTextPanel lcd;
  3. IMyTextPanel statusLCD;
  4. IMyTextPanel fancyLCD;
  5. int lightState = 0;
  6. double targetAccel = 0;
  7. bool prevState = false;
  8. bool done2 = false;
  9. bool CruiseControl = false;
  10. IMyShipController shipController = null;
  11. Color colorOff = new Color(0, 0, 0);
  12. Color colorOn = new Color(255,255,255);
  13. Color colorRear = new Color(100,0,0,255);
  14. Color colorYel = new Color(175,175,100,255);
  15.  
  16. public void Main(string argument)
  17. {
  18.  
  19.     if (argument == "+5" && CruiseControlSpeed <=  95)
  20.     {
  21.         CruiseControlSpeed = CruiseControlSpeed + 5;
  22.     }
  23.  
  24.     if (argument == "-5" && CruiseControlSpeed >= 15)
  25.     {
  26.         CruiseControlSpeed = CruiseControlSpeed - 5;
  27.     }
  28.  
  29.     if (argument == "CC")
  30.     {
  31.         if (CruiseControl == false)
  32.         {
  33.             CruiseControl = true;  
  34.         }
  35.         else
  36.         {
  37.             CruiseControl = false;
  38.         }
  39.     }
  40.  
  41.     shipController = GridTerminalSystem?.GetBlockWithName("shipController") as IMyShipController;
  42.    
  43.     if (shipController == null) //// error check
  44.     {
  45.         Echo("Aww shit! Aint no damn ship controllers yo!");
  46.         return; //stops running the code
  47.     }
  48.    
  49.     var hdg = shipController.MoveIndicator;
  50.  
  51.     lcd = GridTerminalSystem.GetBlockWithName("Speed Out") as IMyTextPanel;
  52.     statusLCD = GridTerminalSystem.GetBlockWithName("Status LCD") as IMyTextPanel;
  53.     lcd?.WritePublicText(""); //// added quesion marks
  54.  
  55.     var ToggledLights = GridTerminalSystem.GetBlockGroupWithName("LTOGGLE");
  56.     var Toggled = new List<IMyInteriorLight>();
  57.     ToggledLights?.GetBlocksOfType(Toggled);
  58.  
  59.     var RearLights = GridTerminalSystem.GetBlockGroupWithName("REAR");  
  60.     var RedLights = new List<IMyInteriorLight>();  
  61.     RearLights?.GetBlocksOfType(RedLights);
  62.  
  63.     var YellowLights = GridTerminalSystem.GetBlockGroupWithName("YEL");  
  64.     var YelLights = new List<IMyInteriorLight>();  
  65.     YellowLights?.GetBlocksOfType(YelLights);
  66.  
  67.     var BrekLights = GridTerminalSystem.GetBlockGroupWithName("BRK");  
  68.     var BRKLights = new List<IMyInteriorLight>();  
  69.     BrekLights?.GetBlocksOfType(BRKLights);  
  70.  
  71.  
  72.     ////Never differentiate variables by case. They should be descriptive
  73.     //"wheels" should be something like "wheelGroup"
  74.     //"Wheels" should then be named "wheels". Only methods start capital
  75.  
  76.     var wheelGroup = GridTerminalSystem.GetBlockGroupWithName("Wheels");
  77.     var wheels = new List<IMyMotorSuspension>();
  78.     wheelGroup?.GetBlocksOfType(wheels);
  79.  
  80.     var wheelStabilityGroup = GridTerminalSystem.GetBlockGroupWithName("WheelsExtend");  
  81.     var wheelStabilizers = new List<IMyMotorSuspension>();  
  82.     wheelStabilityGroup?.GetBlocksOfType(wheelStabilizers);
  83.  
  84.     ////
  85.     /*
  86.     question mark is a null check
  87.     Means the same as:
  88.     if (wheelGroup != null)
  89.         wheelGroup.GetBlocksOfType(wheels);
  90.  
  91.     */
  92.  
  93.     PrintSpeed(shipController.GetShipSpeed());
  94.     IMyReflectorLight spotlight = GridTerminalSystem.GetBlockWithName("Spotlight R") as IMyReflectorLight;
  95.  
  96.     if (spotlight.Enabled != prevState && spotlight != null) ////null check
  97.     {
  98.         lightState = lightState + 1;
  99.  
  100.         if (lightState > 2)
  101.         {
  102.             lightState = 0;
  103.         }
  104.  
  105.         prevState = spotlight.Enabled;
  106.     }
  107.  
  108.     if (lightState == 0)
  109.     {
  110.         foreach (IMyInteriorLight light in Toggled)
  111.        
  112.             light.SetValue("Color", colorOff);
  113.         }
  114.  
  115.         foreach (IMyInteriorLight light in RedLights)  
  116.         {  
  117.             light.SetValue("Color", colorOff);  
  118.         }
  119.  
  120.         foreach (IMyInteriorLight light in YelLights)    
  121.         {    
  122.             light.SetValue("Color", colorYel);    
  123.         }
  124.  
  125.         IMyReflectorLight spotlight2 = GridTerminalSystem.GetBlockWithName("Spotlight") as IMyReflectorLight;
  126.  
  127.         spotlight2?.SetValue("Color", colorOff); ////null check
  128.  
  129.         statusLCD?.WritePublicText("LIGHTS Off"); ////null check
  130.     }
  131.     else if (lightState == 1)
  132.     {
  133.         foreach (IMyInteriorLight light in Toggled)
  134.         {
  135.             light.SetValue("Color", colorOn);
  136.         }
  137.  
  138.         foreach (IMyInteriorLight light in RedLights)  
  139.         {  
  140.             light.SetValue("Color", colorYel);  
  141.         }
  142.  
  143.         foreach (IMyInteriorLight light in YelLights)    
  144.         {    
  145.             light.SetValue("Color", colorOn);    
  146.         }
  147.  
  148.         statusLCD?.WritePublicText("LIGHTS Low Beam"); ////null check
  149.     }
  150.     else if (lightState == 2)
  151.     {
  152.         IMyReflectorLight spotlight2 = GridTerminalSystem.GetBlockWithName("Spotlight") as IMyReflectorLight;
  153.         spotlight2?.SetValue("Color", colorOn); ////null check
  154.         statusLCD?.WritePublicText("LIGHTS High Beam"); ////null check
  155.     }
  156.  
  157.         if (hdg.Z == -1 && done2 == false)
  158.         {
  159.             foreach(IMyMotorSuspension wheel in wheels)        
  160.                 wheel.SetValue("Height", 40f); ////fixed single sillyness    
  161.  
  162.             done2 = true;
  163.         }    
  164.         else if (hdg.Z == 1 && done2 == false)
  165.         {
  166.             foreach(IMyMotorSuspension wheel in wheels)    
  167.                 wheel.SetValue("Height", -30f); ////fixed single sillyness
  168.  
  169.             done2 = true;
  170.         }
  171.         else
  172.         {
  173.             done2 = false;
  174.         }
  175.  
  176.     if(!shipController.HandBrake || CruiseControl == true)
  177.     {
  178.         foreach(IMyMotorSuspension wheel in wheels)  
  179.             wheel.SetValue("Friction", 0f); ////fixed single sillyness
  180.          
  181.         foreach (IMyInteriorLight light in BRKLights)          
  182.             light.SetValue("Color", colorOff);      
  183.     }
  184.     else
  185.     {
  186.         foreach(IMyMotorSuspension wheel in wheels)  
  187.             wheel.SetValue("Friction", 40f); ////fixed single sillyness
  188.          
  189.         foreach (IMyInteriorLight light in BRKLights)      
  190.             light.SetValue("Color", colorRear);
  191.     }
  192.  
  193.     if (shipController.GetShipSpeed() > 45)
  194.     {
  195.         foreach (IMyMotorSuspension wheel in wheelStabilizers)
  196.         {
  197.             var a = Single.Parse("40");
  198.             wheel.SetValue("Height", a);
  199.         }
  200.         statusLCD.WritePublicText("\nSTABILITY Active\n", true);
  201.     }
  202.     else
  203.     {
  204.         foreach (IMyMotorSuspension wheel in wheelStabilizers)  
  205.         {  
  206.             var a = Single.Parse("-30");
  207.             wheel.SetValue("Height", a);  
  208.         }
  209.         statusLCD.WritePublicText("\nSTABILITY Inactive\n", true);
  210.     }
  211.  
  212.     statusLCD.WritePublicText("Cruise Speed: " + CruiseControlSpeed.ToString(), true);
  213.  
  214.     if (CruiseControl == true)
  215.     {
  216.  
  217.         var AccelThrusterGroup = GridTerminalSystem.GetBlockGroupWithName("ACL");  
  218.         var Accelerators = new List<IMyThrust>();  
  219.         AccelThrusterGroup.GetBlocksOfType(Accelerators);    
  220.  
  221.         statusLCD.WritePublicText(" ACTIVE", true);
  222.  
  223.         if (!shipController.DampenersOverride)
  224.         {
  225.             shipController.ApplyAction("DampenersOverride");
  226.         }
  227.  
  228.         lcd.WritePublicText(" CRUISE", true);
  229.  
  230.         if (shipController.GetShipSpeed() > CruiseControlSpeed)
  231.         {
  232.  
  233.             var val = Single.Parse("40");  
  234.    
  235.             foreach(IMyMotorSuspension wheel in wheels)  
  236.             {    
  237.                 wheel.SetValue("Friction", val);  
  238.             }  
  239.            
  240.             foreach(IMyThrust thruster in Accelerators)
  241.             {
  242.                 var thrustPower = Single.Parse("0");
  243.  
  244.                 thruster.SetValue("Override", thrustPower);
  245.  
  246.             }    
  247.  
  248.         }    
  249.         else
  250.         {
  251.             targetAccel = (CruiseControlSpeed - shipController.GetShipSpeed()) * 100;
  252.  
  253.             foreach(IMyThrust thruster in Accelerators)
  254.                 thruster.SetValue("Override", (float)targetAccel); ////Got rid of the single sillyness
  255.         }
  256.     }
  257.     else
  258.     {
  259.         var AccelThrusterGroup = GridTerminalSystem.GetBlockGroupWithName("ACL");  
  260.         var Accelerators = new List<IMyThrust>();  
  261.         AccelThrusterGroup.GetBlocksOfType(Accelerators);        
  262.  
  263.         foreach(IMyThrust thruster in Accelerators)
  264.             thruster.SetValue("Override", 0f); ////Got rid of the single sillyness
  265.     }
  266.  
  267.     Echo($"Current Inst: {Runtime.CurrentInstructionCount}\nMax Inst:{Runtime.MaxInstructionCount}");
  268.  
  269.     fancyLCD = GridTerminalSystem.GetBlockWithName("Left Center LCD") as IMyTextPanel;
  270.     fancyLCD?.WritePublicText(" Systems ONLINE\n ELEKTRON Speed Display Active\n ZIVATAR Ready to drive"); ////added question mark
  271. }
  272.  
  273. // My Random Crap
  274. static char rgb(byte r, byte g, byte b)
  275. {
  276.     return (char)(0xe100 + (r << 6) + (g << 3) + b);
  277. }
  278.    
  279. void PrintSpeed(double spd)
  280. {
  281.     //ONLY CALL WRITEPUBLICTEXT() ONCE!!!!!11!!!1!!
  282.     string line = new string(rgb(7, 0, 0), 20);
  283.     string speedFill = new string(rgb(7, 7, 0), (int)(spd / 5));
  284.  
  285.     lcd.WritePublicText($"{line}\n{speedFill}\n{line}\nSpeed: {spd:N2} m/s");
  286.  
  287.     if (!shipController.DampenersOverride)  
  288.     {
  289.         lcd?.WritePublicText(" DAMP OFF", true); ////added question mark
  290.     }
  291.  
  292. }
Advertisement
Add Comment
Please, Sign In to add comment