Whiplash141

Whip's Mouse Aimed Rotor Turret Script v1

Dec 23rd, 2016
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.88 KB | None | 0 0
  1. /*
  2. /// Whip's Mouse-Aimed Rotor Turret Script v1 - 12/23/16    
  3. */
  4.  
  5. //=============================================================
  6. //You can change these variables if you really want to.
  7. //You do not need to if you just want to use the vanilla script.
  8. //=============================================================
  9.  
  10.     const string strGroupTag = "Turret Group"; //name tag of turret groups
  11.     const string strElevationRotorName = "Elevation"; //name of elevation (vertical) rotor for specific turret
  12.     const string strAzimuthRotorName = "Azimuth"; //name of azimuth (horiontal) rotor for specific turret
  13.  
  14.     double mouseSpeedModifier = 0.5; //scales mouse input by this factor
  15.  
  16. ////////////////////////////////////////////////////
  17. //=================================================
  18. //No touchey anything below here
  19. //=================================================
  20. ////////////////////////////////////////////////////
  21.     const double updatesPerSecond = 60;
  22.  
  23.     const double timeMax = 1 / updatesPerSecond;
  24.  
  25.     double timeElapsed = 0;
  26.  
  27.     IMyMotorStator elevationRotor;
  28.     IMyMotorStator azimuthRotor;
  29.     IMyShipController shipController;
  30.  
  31.     List<IMyTerminalBlock> listWeaponsOrTools = new List<IMyTerminalBlock>(); //need to clear this
  32.  
  33.     const double rad2deg = 180 / Math.PI;
  34.  
  35. void Main(string arg)
  36. {
  37.     timeElapsed += Runtime.TimeSinceLastRun.TotalSeconds;
  38.     trackSymbol += Runtime.TimeSinceLastRun.TotalSeconds;
  39.  
  40.     if (timeElapsed >= timeMax)
  41.     {
  42.         Echo("WMI Turret Control Systems Online... " + RunningSymbol());
  43.  
  44.         List<IMyBlockGroup> groups = new List<IMyBlockGroup>();
  45.         List<IMyBlockGroup> turretGroups = new List<IMyBlockGroup>();
  46.  
  47.         GridTerminalSystem.GetBlockGroups(groups);
  48.  
  49.         foreach (IMyBlockGroup thisGroup in groups)
  50.         {
  51.             if (thisGroup.Name.Contains(strGroupTag))
  52.             {
  53.                 turretGroups.Add(thisGroup);
  54.             }
  55.         }
  56.  
  57.  
  58.         Echo("\nTurret List:");
  59.         if (turretGroups.Count == 0)
  60.         {
  61.             Echo("Error: No turret groups found!");
  62.             return;
  63.         }
  64.  
  65.  
  66.         foreach (IMyBlockGroup thisGroup in turretGroups)
  67.         {
  68.             var blockList = new List<IMyTerminalBlock>();
  69.             thisGroup.GetBlocks(blockList);
  70.             GrabBlocks(blockList);
  71.  
  72.             //GrabBlocks( thisGroup.Blocks ); //old way that doesnt work anymore
  73.  
  74.             bool setupError = false;
  75.             if (shipController == null)
  76.             {
  77.                 Echo("Error: No control seat or remote control found for group '" + thisGroup.Name + "'");
  78.                 setupError = true;
  79.             }
  80.  
  81.             if (listWeaponsOrTools.Count == 0)
  82.             {
  83.                 Echo("Warning: No weapons for group '" + thisGroup.Name + "'");
  84.             }
  85.  
  86.             if (azimuthRotor == null)
  87.             {
  88.                 Echo("Error: No azimuth rotor for group '" + thisGroup.Name + "'");
  89.                 setupError = true;
  90.             }
  91.  
  92.             if (elevationRotor == null)
  93.             {
  94.                 Echo("Error: No elevation rotor for group '" + thisGroup.Name + "'");
  95.                 setupError = true;
  96.             }
  97.  
  98.             if (setupError)
  99.             {
  100.                 StopRotorMovement();
  101.                 shipController = null;
  102.                 azimuthRotor = null;
  103.                 elevationRotor = null;
  104.                 listWeaponsOrTools.Clear();
  105.             }
  106.             else
  107.             {
  108.                 //control rotors
  109.                 RotorControl(shipController);
  110.                 listWeaponsOrTools.Clear(); //clear our lists
  111.                 Echo(thisGroup.Name + " is online"); //write successful groups to
  112.             }
  113.         }
  114.  
  115.         //reset time count
  116.         timeElapsed = 0;
  117.  
  118.         turretGroups.Clear();
  119.     }
  120. }
  121.  
  122.  
  123.  
  124. void GrabBlocks(List<IMyTerminalBlock> blocks)
  125. {
  126.     foreach (IMyTerminalBlock thisBlock in blocks)
  127.     {
  128.         if (thisBlock is IMyMotorStator)
  129.         {
  130.             if (thisBlock.CustomName.Contains(strElevationRotorName))
  131.             {
  132.                 elevationRotor = thisBlock as IMyMotorStator;
  133.             }
  134.             else if (thisBlock.CustomName.Contains(strAzimuthRotorName))
  135.             {
  136.                 azimuthRotor = thisBlock as IMyMotorStator;
  137.             }
  138.             else
  139.             {
  140.                 continue;
  141.             }
  142.         }
  143.         else if (thisBlock is IMyUserControllableGun && !(thisBlock is IMyLargeTurretBase))
  144.         {
  145.             listWeaponsOrTools.Add(thisBlock); //we only want to control out fixed guns
  146.         }
  147.         else if (thisBlock is IMyShipController)
  148.         {
  149.             shipController = thisBlock as IMyShipController; //grabs designator turret
  150.         }
  151.         else if (thisBlock is IMyShipToolBase)
  152.         {
  153.             listWeaponsOrTools.Add(thisBlock);
  154.         }
  155.         else if (thisBlock is IMyLightingBlock)
  156.         {
  157.             listWeaponsOrTools.Add(thisBlock);
  158.         }
  159.         else if (thisBlock is IMyCameraBlock)
  160.         {
  161.             listWeaponsOrTools.Add(thisBlock);
  162.         }
  163.     }
  164. }
  165.  
  166. void RotorControl(IMyShipController turretController)
  167. {
  168.     //get orientation of reference
  169.     IMyTerminalBlock turretReference = listWeaponsOrTools[0];
  170.     Vector3D turretFrontVec = turretReference.WorldMatrix.Forward;
  171.     Vector3D absUpVec = azimuthRotor.WorldMatrix.Up;
  172.     Vector3D turretSideVec = elevationRotor.WorldMatrix.Up;
  173.     Vector3D turretFrontCrossSide = turretFrontVec.Cross(turretSideVec);
  174.  
  175.     //check elevation rotor orientation w.r.t. reference
  176.     double yawMult = 1;
  177.     double pitchMult = 1;
  178.  
  179.  
  180.     if (absUpVec.Dot(turretFrontCrossSide) < 0)
  181.     {
  182.         //turretUpVec = turretFrontCrossSide;
  183.         //turretLeftVec = turretSideVec;
  184.         pitchMult = -1;
  185.     }
  186.     /*else
  187.     {
  188.         turretUpVec = -1 * turretFrontCrossSide;
  189.         turretLeftVec = -1 * turretSideVec;
  190.     }*/
  191.  
  192.     Vector3D WASDinputVec = turretController.MoveIndicator;
  193.     var mouseInput = turretController.RotationIndicator;
  194.  
  195.     double pitchSpeed = mouseSpeedModifier * mouseInput.Y * yawMult;
  196.     double elevationSpeed = mouseSpeedModifier  * - mouseInput.X * pitchMult;
  197.  
  198.     //control rotors
  199.     azimuthRotor.SetValue("Velocity", (float)pitchSpeed);
  200.     elevationRotor.SetValue("Velocity", (float)elevationSpeed);
  201.  
  202.     //control weapons
  203.     if (WASDinputVec.Y > 0)
  204.     {
  205.         WeaponControl(true);
  206.     }
  207.     else
  208.     {
  209.         WeaponControl(false);
  210.     }
  211.  
  212. }
  213.  
  214. void WeaponControl(bool shouldEnableWeaponsAndTools)
  215. {
  216.     if (shouldEnableWeaponsAndTools)
  217.     {
  218.         for (int i = 0; i < listWeaponsOrTools.Count; i++)
  219.         {
  220.             var weapon = listWeaponsOrTools[i] as IMyUserControllableGun;
  221.             if (weapon != null)
  222.             {
  223.                 //if (weapon.GetValue<bool>("Shoot") == false)
  224.                 weapon.ApplyAction("Shoot_On");
  225.             }
  226.  
  227.             var tool = listWeaponsOrTools[i] as IMyShipToolBase;
  228.             if (tool != null)
  229.             {
  230.                 tool.ApplyAction("OnOff_On");
  231.             }
  232.  
  233.             var light = listWeaponsOrTools[i] as IMyLightingBlock;
  234.             if (light != null)
  235.             {
  236.                 light.ApplyAction("OnOff_On");
  237.             }
  238.         }
  239.     }
  240.     else
  241.     {
  242.         for (int i = 0; i < listWeaponsOrTools.Count; i++)
  243.         {
  244.             var weapon = listWeaponsOrTools[i] as IMyUserControllableGun;
  245.             if (weapon != null)
  246.             {
  247.                 //if (weapon.GetValue<bool>("Shoot") == false)
  248.                 weapon.ApplyAction("Shoot_Off");
  249.             }
  250.  
  251.             var tool = listWeaponsOrTools[i] as IMyShipToolBase;
  252.             if (tool != null)
  253.             {
  254.                 tool.ApplyAction("OnOff_Off");
  255.             }
  256.  
  257.             var light = listWeaponsOrTools[i] as IMyLightingBlock;
  258.             if (light != null)
  259.             {
  260.                 light.ApplyAction("OnOff_Off");
  261.             }
  262.         }
  263.     }
  264.  
  265. }
  266.  
  267. void StopRotorMovement()
  268. {
  269.     if (azimuthRotor != null)
  270.     {
  271.         azimuthRotor.SetValue("Velocity", 0f);
  272.     }
  273.  
  274.     if (elevationRotor != null)
  275.     {
  276.         elevationRotor.SetValue("Velocity", 0f);
  277.     }
  278.  
  279.     for (int i = 0; i < listWeaponsOrTools.Count; i++)
  280.     {
  281.         var thisWeapon = listWeaponsOrTools[0] as IMyUserControllableGun;
  282.         if (thisWeapon != null) thisWeapon.ApplyAction("Shoot_Off");
  283.     }
  284. }
  285.  
  286. //Whip's Running Symbol Method
  287. double trackSymbol = 0;
  288. string strRunningSymbol = "";
  289.  
  290. string RunningSymbol()
  291. {
  292.     if (trackSymbol < .2d)
  293.         strRunningSymbol = "|";
  294.     else if (trackSymbol < .4d)
  295.         strRunningSymbol = "/";
  296.     else if (trackSymbol < .6d)
  297.         strRunningSymbol = "--";
  298.     else if (trackSymbol < .8d)
  299.         strRunningSymbol = "\\";
  300.     else
  301.     {
  302.         trackSymbol = 0;
  303.         strRunningSymbol = "|";
  304.     }
  305.  
  306.     return strRunningSymbol;
  307. }
Advertisement
Add Comment
Please, Sign In to add comment