Chrishel

etopsirhc's brake light script

Feb 1st, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.80 KB | None | 0 0
  1. //
  2. // Set each of these 4 tags to the apropriate blocks in the menu
  3. // Select the looping sound you wish to use  for the reverse warning (in the block)
  4. //
  5. // If things arent working right set DEBUG to true then recompile.
  6. //
  7. public string CockpitTag = "[Seat]";
  8. public string BrakeLightTag = "[Brake]";
  9. public string ReverseLightTag = "[Reverse]";
  10. public string ReverseSoundBlockTag = "[Warning]";
  11.  
  12. public bool DEBUG = false;
  13.  
  14. public IMyCockpit cockpit = null;
  15. public IMyRemoteControl altCockpit = null;
  16. public List<IMyTerminalBlock> brakeLights = null;
  17. public List<IMyTerminalBlock> reverseLights = null;
  18. public IMySoundBlock warning = null;
  19.  
  20. public bool brake = false;
  21. public bool brakeSet = false;
  22. public bool reverse = false;
  23. public bool reverseSet = false;
  24.  
  25. public Program()
  26. {
  27.     Runtime.UpdateFrequency = UpdateFrequency.Update1;
  28.     List<IMyTerminalBlock> temp = new List<IMyTerminalBlock>();
  29.     GridTerminalSystem.SearchBlocksOfName(CockpitTag, temp);
  30.     foreach (var tb in temp)
  31.     {
  32.         if (tb.GetType().ToString() == "Sandbox.Game.Entities.MyCockpit")
  33.         {
  34.             cockpit = (IMyCockpit)tb;
  35.             break;
  36.         }
  37.         if (tb.GetType().ToString() == "Sandbox.Game.Entities.MyRemoteControl")
  38.         {
  39.             altCockpit = (IMyRemoteControl)tb;
  40.             break;
  41.         }
  42.     }
  43.     temp.Clear();
  44.     GridTerminalSystem.SearchBlocksOfName(ReverseSoundBlockTag, temp);
  45.     foreach (var tb in temp)
  46.     {
  47.         try
  48.         {
  49.             warning = (IMySoundBlock)tb;
  50.             break;
  51.         }
  52.         catch (InvalidCastException e)
  53.         {
  54.         }
  55.     }
  56.     temp.Clear();
  57.     if (warning != null)
  58.     {
  59.         warning.LoopPeriod = 1800f;
  60.     }
  61.     brakeLights = new List<IMyTerminalBlock>();
  62.     reverseLights = new List<IMyTerminalBlock>();
  63.     GridTerminalSystem.SearchBlocksOfName(BrakeLightTag, brakeLights);
  64.     GridTerminalSystem.SearchBlocksOfName(ReverseLightTag, reverseLights);
  65.  
  66.     debug("=DEBUG MODE=");
  67.     debug((cockpit != null) ? "Controled through cockpit." : "Controled through control block.");
  68.     debug(string.Format("Found {0} brake lights.", brakeLights.Count));
  69.     debug(string.Format("Found {0} reverse lights.", reverseLights.Count));
  70.     debug((warning != null) ? "Found the sound block." : "No sound block found.");
  71. }
  72.  
  73. public void Main(string arg)
  74. {
  75.     if (cockpit != null)
  76.     {
  77.         if (cockpit.MoveIndicator.Y > .2f)
  78.         {
  79.             brake = true;
  80.         }
  81.         else
  82.         {
  83.             brake = false;
  84.         }
  85.  
  86.         if (cockpit.MoveIndicator.Z > .2f)
  87.         {
  88.             reverse = true;
  89.         }
  90.         else
  91.         {
  92.             reverse = false;
  93.         }
  94.     }
  95.     else if (altCockpit != null)
  96.     {
  97.         if (altCockpit.MoveIndicator.Y > .2f)
  98.         {
  99.             brake = true;
  100.         }
  101.         else
  102.         {
  103.             brake = false;
  104.         }
  105.  
  106.         if (altCockpit.MoveIndicator.Z > .2f)
  107.         {
  108.             reverse = true;
  109.         }
  110.         else
  111.         {
  112.             reverse = false;
  113.         }
  114.     }
  115.     if (brake && !brakeSet)
  116.     {
  117.         brakeSet = true;
  118.         foreach (IMyTerminalBlock light in brakeLights)
  119.         {
  120.             if (light != null)
  121.             {
  122.                 if (light.HasAction("OnOff_On"))
  123.                 {
  124.                     light.ApplyAction("OnOff_On");
  125.                 }
  126.             }
  127.         }
  128.     }
  129.  
  130.     if (!brake && brakeSet)
  131.     {
  132.         brakeSet = false;
  133.         foreach (IMyTerminalBlock light in brakeLights)
  134.         {
  135.             if (light != null)
  136.             {
  137.                 if (light.HasAction("OnOff_Off"))
  138.                 {
  139.                     light.ApplyAction("OnOff_Off");
  140.                 }
  141.             }
  142.         }
  143.     }
  144.  
  145.     if (reverse && !reverseSet)
  146.     {
  147.         reverseSet = true;
  148.         if (warning != null)
  149.         {
  150.             warning.Play();
  151.         }
  152.         foreach (IMyTerminalBlock light in reverseLights)
  153.         {
  154.             if (light != null)
  155.             {
  156.                 if (light.HasAction("OnOff_On"))
  157.                 {
  158.                     light.ApplyAction("OnOff_On");
  159.                 }
  160.             }
  161.         }
  162.     }
  163.  
  164.     if (!reverse && reverseSet)
  165.     {
  166.         reverseSet = false;
  167.         if (warning != null)
  168.         {
  169.             warning.Stop();
  170.         }
  171.         foreach (IMyTerminalBlock light in reverseLights)
  172.         {
  173.             if (light != null)
  174.             {
  175.                 if (light.HasAction("OnOff_Off"))
  176.                 {
  177.                     light.ApplyAction("OnOff_Off");
  178.                 }
  179.             }
  180.         }
  181.     }
  182. }
  183.  
  184. void debug(string s)
  185. {
  186.     if (DEBUG)
  187.     {
  188.         Echo(s);
  189.     }
  190. }
  191. // Written by etopsirhc
Advertisement
Add Comment
Please, Sign In to add comment