Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- /// Whip's Mouse-Aimed Rotor Turret Script v1 - 12/23/16
- */
- //=============================================================
- //You can change these variables if you really want to.
- //You do not need to if you just want to use the vanilla script.
- //=============================================================
- const string strGroupTag = "Turret Group"; //name tag of turret groups
- const string strElevationRotorName = "Elevation"; //name of elevation (vertical) rotor for specific turret
- const string strAzimuthRotorName = "Azimuth"; //name of azimuth (horiontal) rotor for specific turret
- double mouseSpeedModifier = 0.5; //scales mouse input by this factor
- ////////////////////////////////////////////////////
- //=================================================
- //No touchey anything below here
- //=================================================
- ////////////////////////////////////////////////////
- const double updatesPerSecond = 60;
- const double timeMax = 1 / updatesPerSecond;
- double timeElapsed = 0;
- IMyMotorStator elevationRotor;
- IMyMotorStator azimuthRotor;
- IMyShipController shipController;
- List<IMyTerminalBlock> listWeaponsOrTools = new List<IMyTerminalBlock>(); //need to clear this
- const double rad2deg = 180 / Math.PI;
- void Main(string arg)
- {
- timeElapsed += Runtime.TimeSinceLastRun.TotalSeconds;
- trackSymbol += Runtime.TimeSinceLastRun.TotalSeconds;
- if (timeElapsed >= timeMax)
- {
- Echo("WMI Turret Control Systems Online... " + RunningSymbol());
- List<IMyBlockGroup> groups = new List<IMyBlockGroup>();
- List<IMyBlockGroup> turretGroups = new List<IMyBlockGroup>();
- GridTerminalSystem.GetBlockGroups(groups);
- foreach (IMyBlockGroup thisGroup in groups)
- {
- if (thisGroup.Name.Contains(strGroupTag))
- {
- turretGroups.Add(thisGroup);
- }
- }
- Echo("\nTurret List:");
- if (turretGroups.Count == 0)
- {
- Echo("Error: No turret groups found!");
- return;
- }
- foreach (IMyBlockGroup thisGroup in turretGroups)
- {
- var blockList = new List<IMyTerminalBlock>();
- thisGroup.GetBlocks(blockList);
- GrabBlocks(blockList);
- //GrabBlocks( thisGroup.Blocks ); //old way that doesnt work anymore
- bool setupError = false;
- if (shipController == null)
- {
- Echo("Error: No control seat or remote control found for group '" + thisGroup.Name + "'");
- setupError = true;
- }
- if (listWeaponsOrTools.Count == 0)
- {
- Echo("Warning: No weapons for group '" + thisGroup.Name + "'");
- }
- if (azimuthRotor == null)
- {
- Echo("Error: No azimuth rotor for group '" + thisGroup.Name + "'");
- setupError = true;
- }
- if (elevationRotor == null)
- {
- Echo("Error: No elevation rotor for group '" + thisGroup.Name + "'");
- setupError = true;
- }
- if (setupError)
- {
- StopRotorMovement();
- shipController = null;
- azimuthRotor = null;
- elevationRotor = null;
- listWeaponsOrTools.Clear();
- }
- else
- {
- //control rotors
- RotorControl(shipController);
- listWeaponsOrTools.Clear(); //clear our lists
- Echo(thisGroup.Name + " is online"); //write successful groups to
- }
- }
- //reset time count
- timeElapsed = 0;
- turretGroups.Clear();
- }
- }
- void GrabBlocks(List<IMyTerminalBlock> blocks)
- {
- foreach (IMyTerminalBlock thisBlock in blocks)
- {
- if (thisBlock is IMyMotorStator)
- {
- if (thisBlock.CustomName.Contains(strElevationRotorName))
- {
- elevationRotor = thisBlock as IMyMotorStator;
- }
- else if (thisBlock.CustomName.Contains(strAzimuthRotorName))
- {
- azimuthRotor = thisBlock as IMyMotorStator;
- }
- else
- {
- continue;
- }
- }
- else if (thisBlock is IMyUserControllableGun && !(thisBlock is IMyLargeTurretBase))
- {
- listWeaponsOrTools.Add(thisBlock); //we only want to control out fixed guns
- }
- else if (thisBlock is IMyShipController)
- {
- shipController = thisBlock as IMyShipController; //grabs designator turret
- }
- else if (thisBlock is IMyShipToolBase)
- {
- listWeaponsOrTools.Add(thisBlock);
- }
- else if (thisBlock is IMyLightingBlock)
- {
- listWeaponsOrTools.Add(thisBlock);
- }
- else if (thisBlock is IMyCameraBlock)
- {
- listWeaponsOrTools.Add(thisBlock);
- }
- }
- }
- void RotorControl(IMyShipController turretController)
- {
- //get orientation of reference
- IMyTerminalBlock turretReference = listWeaponsOrTools[0];
- Vector3D turretFrontVec = turretReference.WorldMatrix.Forward;
- Vector3D absUpVec = azimuthRotor.WorldMatrix.Up;
- Vector3D turretSideVec = elevationRotor.WorldMatrix.Up;
- Vector3D turretFrontCrossSide = turretFrontVec.Cross(turretSideVec);
- //check elevation rotor orientation w.r.t. reference
- double yawMult = 1;
- double pitchMult = 1;
- if (absUpVec.Dot(turretFrontCrossSide) < 0)
- {
- //turretUpVec = turretFrontCrossSide;
- //turretLeftVec = turretSideVec;
- pitchMult = -1;
- }
- /*else
- {
- turretUpVec = -1 * turretFrontCrossSide;
- turretLeftVec = -1 * turretSideVec;
- }*/
- Vector3D WASDinputVec = turretController.MoveIndicator;
- var mouseInput = turretController.RotationIndicator;
- double pitchSpeed = mouseSpeedModifier * mouseInput.Y * yawMult;
- double elevationSpeed = mouseSpeedModifier * - mouseInput.X * pitchMult;
- //control rotors
- azimuthRotor.SetValue("Velocity", (float)pitchSpeed);
- elevationRotor.SetValue("Velocity", (float)elevationSpeed);
- //control weapons
- if (WASDinputVec.Y > 0)
- {
- WeaponControl(true);
- }
- else
- {
- WeaponControl(false);
- }
- }
- void WeaponControl(bool shouldEnableWeaponsAndTools)
- {
- if (shouldEnableWeaponsAndTools)
- {
- for (int i = 0; i < listWeaponsOrTools.Count; i++)
- {
- var weapon = listWeaponsOrTools[i] as IMyUserControllableGun;
- if (weapon != null)
- {
- //if (weapon.GetValue<bool>("Shoot") == false)
- weapon.ApplyAction("Shoot_On");
- }
- var tool = listWeaponsOrTools[i] as IMyShipToolBase;
- if (tool != null)
- {
- tool.ApplyAction("OnOff_On");
- }
- var light = listWeaponsOrTools[i] as IMyLightingBlock;
- if (light != null)
- {
- light.ApplyAction("OnOff_On");
- }
- }
- }
- else
- {
- for (int i = 0; i < listWeaponsOrTools.Count; i++)
- {
- var weapon = listWeaponsOrTools[i] as IMyUserControllableGun;
- if (weapon != null)
- {
- //if (weapon.GetValue<bool>("Shoot") == false)
- weapon.ApplyAction("Shoot_Off");
- }
- var tool = listWeaponsOrTools[i] as IMyShipToolBase;
- if (tool != null)
- {
- tool.ApplyAction("OnOff_Off");
- }
- var light = listWeaponsOrTools[i] as IMyLightingBlock;
- if (light != null)
- {
- light.ApplyAction("OnOff_Off");
- }
- }
- }
- }
- void StopRotorMovement()
- {
- if (azimuthRotor != null)
- {
- azimuthRotor.SetValue("Velocity", 0f);
- }
- if (elevationRotor != null)
- {
- elevationRotor.SetValue("Velocity", 0f);
- }
- for (int i = 0; i < listWeaponsOrTools.Count; i++)
- {
- var thisWeapon = listWeaponsOrTools[0] as IMyUserControllableGun;
- if (thisWeapon != null) thisWeapon.ApplyAction("Shoot_Off");
- }
- }
- //Whip's Running Symbol Method
- double trackSymbol = 0;
- string strRunningSymbol = "";
- string RunningSymbol()
- {
- if (trackSymbol < .2d)
- strRunningSymbol = "|";
- else if (trackSymbol < .4d)
- strRunningSymbol = "/";
- else if (trackSymbol < .6d)
- strRunningSymbol = "--";
- else if (trackSymbol < .8d)
- strRunningSymbol = "\\";
- else
- {
- trackSymbol = 0;
- strRunningSymbol = "|";
- }
- return strRunningSymbol;
- }
Advertisement
Add Comment
Please, Sign In to add comment