Advertisement
Whiplash141

Whip's Fixed Weapon Drone Code [SpaceEngineers]

Sep 20th, 2015
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 KB | None | 0 0
  1.    
  2. /*    
  3. /// Whip's Fixed Weapon Drone Mod - rev 3.0 - 9/21/15 ///  
  4. */  
  5.    
  6. double range = 400; //range in meters
  7. double degree_tolerance = 5;
  8. string message;  
  9. string range_m = "0";  
  10. string angle_m =  "0";  
  11. string shoot_m = "null";  
  12.    
  13. List<IMyTerminalBlock> remotes = new List<IMyTerminalBlock>();  
  14. List<IMyTerminalBlock> blocks = new List<IMyTerminalBlock>();  
  15. List<IMyTerminalBlock> weapons = new List<IMyTerminalBlock>();  
  16. List<IMyTerminalBlock> antennas = new List<IMyTerminalBlock>();  
  17.    
  18. void Main()  
  19. {    
  20.     IMyTimerBlock frontRefrence = GridTerminalSystem.GetBlockWithName("Timer - Drone Front") as IMyTimerBlock;
  21.     if (frontRefrence == null)
  22.     {
  23.         message = "ERROR: No front refrence set";
  24.         Echo("ERROR: No front refrence set");
  25.         return;
  26.     }
  27.     else
  28.     {
  29.         Echo("WMI Drone Systems Online");
  30.     }
  31.      
  32.     GridTerminalSystem.GetBlocksOfType<IMyTerminalBlock>(blocks);    
  33.     for (int i=0 ; i < blocks.Count ; i++)  
  34.     {  
  35.         if(blocks[i] is IMyLargeTurretBase) continue; //ignore turrets  
  36.         if(!(blocks[i] is IMyUserControllableGun)) continue; //ignore everything not a weapon  
  37.         weapons.Add(blocks[i] as IMyUserControllableGun); //add weapons to list  
  38.     }  
  39.        
  40.     GridTerminalSystem.GetBlocksOfType<IMyRemoteControl>(remotes);    
  41.     if (remotes.Count > 0)    
  42.     {    
  43.         var remote = remotes[0] as IMyRemoteControl;    
  44.         remote.ClearWaypoints();  
  45.        
  46.     //making empty vector for us to populate  
  47.         Vector3D playerPosition = new Vector3D(0, 0, 0);
  48.         Vector3D currentPosition = new Vector3D(0, 0, 0);  
  49.         Vector3D currentFront = new Vector3D(0, 0, 0);
  50.         Vector3D shipVector = new Vector3D(0, 0, 0);
  51.         Vector3D playerVector = new Vector3D(0, 0, 0);
  52.         Vector3D difference = new Vector3D(0, 0, 0);
  53.        
  54.         bool success = remote.GetNearestPlayer(out playerPosition);
  55.         remote.ApplyAction("CollisionAvoidance_On");  
  56.         if (success)    
  57.         {    
  58.             remote.AddWaypoint(playerPosition, "Player"); //follow player  
  59.             remote.SetAutoPilotEnabled(true); //enable autopilot
  60.            
  61.         //populating our vectors
  62.             currentPosition = remote.GetPosition(); //position vector of ship
  63.             currentFront = frontRefrence.GetPosition(); //position vector of front of ship
  64.             shipVector = Vector3D.Subtract(currentFront, currentPosition); //direction vector of ship
  65.             playerVector = Vector3D.Subtract(playerPosition, currentPosition); //direction vector of player
  66.  
  67.         //some gross vector math  
  68.             double dist = Vector3D.Distance(currentPosition, playerPosition);
  69.             range_m = dist.ToString(); //gives us distance between player and ship  
  70.  
  71.             double dotProduct;  
  72.             Vector3D.Dot(ref shipVector, ref playerVector, out dotProduct);
  73.            
  74.             double x = dotProduct / shipVector.Length() / playerVector.Length();    
  75.             double theta_deg = Math.Acos(x) * 180f / Math.PI; //angle between ship and player      
  76.             angle_m = theta_deg.ToString(); //debug
  77.            
  78.         //in range
  79.             if (dist < range && theta_deg < degree_tolerance)  
  80.             {    
  81.                 for (int i=0 ; i < weapons.Count ; i++)  
  82.                 {  
  83.                     var weaponToShoot = weapons[i] as IMyUserControllableGun;  
  84.                     weaponToShoot.ApplyAction("Shoot_On");  
  85.                     shoot_m = "In Range";  
  86.                 }  
  87.             }
  88.         //not in range
  89.             else
  90.             {  
  91.                 for (int i=0 ; i < weapons.Count ; i++)  
  92.                 {  
  93.                     var weaponToShoot = weapons[i] as IMyUserControllableGun;  
  94.                     weaponToShoot.ApplyAction("Shoot_Off");  
  95.                     shoot_m = "Out of Range";  
  96.                 }  
  97.             }  
  98.         }
  99.        
  100.     //for debugging since we can't access the ship once it is owned
  101.         GridTerminalSystem.GetBlocksOfType<IMyRadioAntenna>(antennas);  
  102.         var antenna = antennas[0] as IMyRadioAntenna;  
  103.         message = "Antenna\nRange: " + range_m    
  104.             + "\nAngle: " + angle_m  
  105.             + "\nStatus: " + shoot_m;
  106.         antenna.SetCustomName(message);  
  107.     }    
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement