Guest User

Immersive Car Lights v2

a guest
Jul 18th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.42 KB | None | 0 0
  1. const string cockpitName = "Cockpit";               // Drivers Cockpit Name.
  2. const string remotecontrolName = "Remote Control";
  3. const string headLight = "Head light";              // Head Light Name.
  4. const string brakeLight = "Brake light";           // Brake Light Name.
  5. const string leftIndicator = "Left Indicator";     // Left Indicator Name.
  6. const string rightIndicator = "Right Indicator"; // Right Indicator Name.
  7. const string reverseLight = "ReverseLight";     // Reverse Light Name.
  8. const string speedometer = "Speedometer";   // LCD to display a speedometer on your vehicle. This will be in meters per second.
  9.  
  10. // Maximum speed before turn indicators stop turning on.
  11. int indicatorSpeed = 10; // this is in m/s
  12.  
  13. // Let Program use other grids?
  14. bool useSubGrid = false;
  15.  
  16. List<IMyTerminalBlock> allCockpit = new List<IMyTerminalBlock>();      
  17. List<IMyTerminalBlock> allRemoteControl = new List<IMyTerminalBlock>();    
  18. List<IMyTerminalBlock> allLights = new List<IMyTerminalBlock>();  
  19. List<IMyTerminalBlock> allLcd = new List<IMyTerminalBlock>();  
  20.  
  21. Vector3D position = new Vector3D(0,0,0);    
  22. Vector3 WASDControl;  
  23.  
  24. public Program()    
  25. {    
  26.     Runtime.UpdateFrequency = UpdateFrequency.Update10;  
  27. }      
  28.  
  29. void Main()      
  30. {      
  31.     if (useSubGrid == true)
  32.     {
  33.         GridTerminalSystem.GetBlocksOfType<IMyCockpit>(allCockpit);
  34.         GridTerminalSystem.GetBlocksOfType<IMyRemoteControl>(allRemoteControl);    
  35.         GridTerminalSystem.GetBlocksOfType<IMyLightingBlock>(allLights);  
  36.         GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(allLcd);  
  37.     }
  38.     else if (useSubGrid == false)
  39.     {
  40.         GridTerminalSystem.GetBlocksOfType<IMyCockpit>(allCockpit, b => b.CubeGrid == Me.CubeGrid);
  41.         GridTerminalSystem.GetBlocksOfType<IMyRemoteControl>(allRemoteControl, b => b.CubeGrid == Me.CubeGrid);    
  42.         GridTerminalSystem.GetBlocksOfType<IMyLightingBlock>(allLights, b => b.CubeGrid == Me.CubeGrid);  
  43.         GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(allLcd, b => b.CubeGrid == Me.CubeGrid);  
  44.     }
  45.    
  46.     bool handBrake = false;
  47.     bool lightsOn = false;
  48.     double mySpeed = get_speed();
  49.     StringBuilder outputSpeed = new StringBuilder();
  50.    
  51.     outputSpeed.Append("Speed: \n" + mySpeed.ToString() + " m/s");
  52.  
  53.     foreach(IMyTextPanel lcd in allLcd)
  54.     {
  55.         if (lcd.CustomName.Contains(speedometer))
  56.         {
  57.             lcd.WritePublicText(outputSpeed.ToString());
  58.             lcd.ShowPublicTextOnScreen();
  59.         }
  60.     }
  61.    
  62.     foreach(IMyCockpit cockpit in allCockpit)    
  63.     {    
  64.         if (cockpit.CustomName.Contains(cockpitName))
  65.         {
  66.             handBrake = cockpit.GetValue<bool>("HandBrake");  
  67.             WASDControl = cockpit.MoveIndicator;
  68.         }
  69.         /*********
  70.         A => X:-1
  71.         D => X: 1
  72.         C => Y:-1
  73.         Sp => Y: 1
  74.         W => Z:-1
  75.         S => Z: 1
  76.         **********/
  77.     }    
  78.     foreach(IMyRemoteControl remotecontrol in allRemoteControl)    
  79.     {    
  80.         if (remotecontrol.CustomName.Contains(remotecontrolName))
  81.         {
  82.             handBrake = remotecontrol.GetValue<bool>("HandBrake");  
  83.             WASDControl = remotecontrol.MoveIndicator;
  84.         }
  85.         /*********
  86.         A => X:-1
  87.         D => X: 1
  88.         C => Y:-1
  89.         Sp => Y: 1
  90.         W => Z:-1
  91.         S => Z: 1
  92.         **********/
  93.     }    
  94.  
  95.     foreach (IMyLightingBlock light in allLights)    
  96.     {    
  97.         if (light.CustomName.Contains(headLight))
  98.         {
  99.             lightsOn = light.GetValue<bool>("OnOff");
  100.         }
  101.     }
  102.     foreach (IMyLightingBlock light in allLights)
  103.     {
  104.         if (light.CustomName.Contains(leftIndicator) || light.CustomName.Contains(rightIndicator))
  105.         {
  106.             light.SetValue("Color", new Color(255, 255, 0));      
  107.             light.SetValue("Blink Interval", Convert.ToSingle(1));
  108.             light.SetValue("Blink Lenght", Convert.ToSingle(20));
  109.         }
  110.         if (light.CustomName.Contains(brakeLight))
  111.         {
  112.             light.SetValue("Color", new Color(255, 0, 0));
  113.         }
  114.        
  115.         if (mySpeed < indicatorSpeed)
  116.         {
  117.             if (WASDControl.X < 0)
  118.             {
  119.                 if (light.CustomName.Contains(leftIndicator))
  120.                 {
  121.                     light.ApplyAction("OnOff_On");
  122.                 }
  123.                 else if (light.CustomName.Contains(rightIndicator))
  124.                 {
  125.                     light.ApplyAction("OnOff_Off");
  126.                 }
  127.             }
  128.             if (WASDControl.X > 0)
  129.             {
  130.                 if (light.CustomName.Contains(rightIndicator))
  131.                 {
  132.                     light.ApplyAction("OnOff_On");
  133.                 }
  134.                 else if (light.CustomName.Contains(leftIndicator))
  135.                 {
  136.                     light.ApplyAction("OnOff_Off");
  137.                 }
  138.             }
  139.         }
  140.         if (light.CustomName.Contains(leftIndicator) || light.CustomName.Contains(rightIndicator))
  141.         {
  142.             if (WASDControl.X == 0)
  143.             {
  144.                
  145.                 light.ApplyAction("OnOff_Off");
  146.                
  147.             }
  148.            
  149.             else if (mySpeed >= indicatorSpeed)
  150.             {
  151.                
  152.                 light.ApplyAction("OnOff_Off");
  153.             }
  154.         }
  155.        
  156.        
  157.         if (light.CustomName.Contains(reverseLight))
  158.         {
  159.             light.SetValue("Color", new Color(255, 255, 255));
  160.             light.SetValue("Intensity", Convert.ToSingle(5));  
  161.             light.SetValue("Radius", Convert.ToSingle(3));
  162.            
  163.             if (mySpeed > 0 && WASDControl.Z > 0)
  164.             {
  165.                 light.ApplyAction("OnOff_On");
  166.             }
  167.             else
  168.             {
  169.                 light.ApplyAction("OnOff_Off");
  170.             }
  171.         }
  172.         if (light.CustomName.Contains(brakeLight))
  173.         {
  174.             if (handBrake == true && lightsOn == false)
  175.             {
  176.                
  177.                 light.ApplyAction("OnOff_On");
  178.                 light.SetValue("Intensity", Convert.ToSingle(5));  
  179.                 light.SetValue("Radius", Convert.ToSingle(3));
  180.                
  181.             }
  182.             else if (handBrake == true && lightsOn == true)
  183.             {
  184.                
  185.                 light.ApplyAction("OnOff_On");
  186.                 light.SetValue("Intensity", Convert.ToSingle(5));  
  187.                 light.SetValue("Radius", Convert.ToSingle(3));
  188.             }
  189.             else if (handBrake == false && lightsOn == false)
  190.             {
  191.                
  192.                 if(WASDControl.Y > 0)//Press space
  193.                 {
  194.                     light.ApplyAction("OnOff_On");
  195.                     light.SetValue("Intensity", Convert.ToSingle(5));  
  196.                     light.SetValue("Radius", Convert.ToSingle(3));
  197.                 }
  198.                 else //Space is free
  199.                 {
  200.                     light.ApplyAction("OnOff_Off");
  201.                     light.SetValue("Intensity", Convert.ToSingle(0));  
  202.                     light.SetValue("Radius", Convert.ToSingle(0));
  203.                 }
  204.             }
  205.             else if (handBrake == false && lightsOn == true)
  206.             {
  207.                
  208.                 if(WASDControl.Y > 0)//Press space
  209.                 {
  210.                     light.ApplyAction("OnOff_On");
  211.                     light.SetValue("Intensity", Convert.ToSingle(5));  
  212.                     light.SetValue("Radius", Convert.ToSingle(3));
  213.                 }
  214.                 else//Space is free
  215.                 {
  216.                     light.ApplyAction("OnOff_On");
  217.                     light.SetValue("Intensity", Convert.ToSingle(1));  
  218.                     light.SetValue("Radius", Convert.ToSingle(2));
  219.                 }
  220.             }
  221.         }
  222.     }    
  223.     allCockpit.Clear();
  224.     allRemoteControl.Clear();
  225.     allLights.Clear();
  226. }
  227.  
  228. double get_speed()    
  229. {    
  230.     Vector3D current_position = Me.GetPosition(); // the position of this programmable block
  231.     double speed = ((current_position-position)*6).Length();    
  232.     // double speed = (((current_position-position)*60)*60/1000).Length(); // KMH  
  233.     // double speed = ((current_position-position)*60).Length(); // M/H  
  234.     // double speed = ((current_position-position)*6).Length(); // M/S  
  235.    
  236.     position = current_position; // update the global variable, which will be used on the next run    
  237.     speed = Math.Round(speed,0);  
  238.  
  239.     return speed;    
  240. }
Advertisement
Add Comment
Please, Sign In to add comment