Advertisement
Meridius_IX

Horsefly Drone

Sep 2nd, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.86 KB | None | 0 0
  1. //Meridius_IX's Pirate AI Systems - Horse-Fly Drone
  2.  
  3. double player_detect_range = 25000; //If player is further than this value from drone, it will not execute any behavioral scripting.
  4.  
  5. bool use_raycast_ranged_weapons = false;
  6. bool use_raycast_close_weapons = false;
  7.  
  8. //////////////////////////////////////////////////////
  9. //Do Not Touch Anything Below Here
  10. //////////////////////////////////////////////////////
  11.  
  12. double player_drone_distance = 0;
  13. double player_origin_distance = 0;
  14. double drone_origin_distance = 0;
  15.  
  16. //Raycast Scan Data
  17. MyDetectedEntityInfo scanned_target;
  18. bool valid_scanned_target = false;
  19. double scanned_target_distance = 0;
  20. Vector3D scanned_target_coords = new Vector3D(0,0,0);
  21.  
  22. IMyRemoteControl remote_control;
  23.  
  24. void Main(string argument){
  25.    
  26.     Vector3D world_center = new Vector3D(0,0,0);
  27.     Vector3D player_coords = new Vector3D(0,0,0);
  28.     Vector3D drone_coords = new Vector3D(0,0,0);
  29.     Vector3D origin_coords = new Vector3D(0,0,0);
  30.    
  31.     //Find Remote Control Block(s)
  32.     List<IMyTerminalBlock> remote_control_list = new List<IMyTerminalBlock>();
  33.     GridTerminalSystem.GetBlocksOfType<IMyRemoteControl>(remote_control_list);
  34.    
  35.     if(remote_control_list.Count != 0){
  36.        
  37.         for(int i = 0; i < remote_control_list.Count; i++){
  38.            
  39.             if(remote_control_list[i].IsFunctional == true){
  40.                
  41.                 remote_control = remote_control_list[i] as IMyRemoteControl;
  42.                 break;
  43.                
  44.             }
  45.            
  46.         }
  47.        
  48.     }else{
  49.        
  50.         Echo("No Remote Control Block Found. Script Cannot Execute.");
  51.         return;
  52.        
  53.     }
  54.    
  55.     //Check If Drone is NPC Owned
  56.     remote_control.GetNearestPlayer(out player_coords);
  57.    
  58.     if(player_coords == world_center){
  59.        
  60.         Echo("Drone is not owned by Hostile NPC. Script Cannot Continue.");
  61.         return;
  62.        
  63.     }
  64.    
  65.     drone_coords = remote_control.GetPosition();
  66.     valid_scanned_target = RaycastScan();
  67.    
  68.     //Get & Set Origin Coords
  69.     if(this.Storage == null || this.Storage == ""){
  70.        
  71.         this.Storage = drone_coords.ToString();
  72.         origin_coords = drone_coords;
  73.        
  74.     }else{
  75.        
  76.         Vector3D.TryParse(this.Storage, out origin_coords);
  77.        
  78.     }
  79.    
  80.     //Calculate Distances
  81.     player_drone_distance = MeasureDistance(player_coords, drone_coords);
  82.     player_origin_distance = MeasureDistance(player_coords, origin_coords);
  83.     drone_origin_distance = MeasureDistance(drone_coords, origin_coords);
  84.     scanned_target_distance = MeasureDistance(drone_coords, scanned_target_coords);
  85.  
  86.     if(player_drone_distance > player_detect_range){
  87.        
  88.         Echo("No Player Detected. Aborting Script Execution.");
  89.        
  90.     }
  91.    
  92.     //////////////////////////////////////////////////////
  93.     //Pirate Behavior Start
  94.     //////////////////////////////////////////////////////
  95.    
  96.     if(remote_control.CustomData == ""){
  97.        
  98.         remote_control.CustomData = "0\n";
  99.         remote_control.CustomData += player_coords.ToString();
  100.        
  101.     }
  102.    
  103.     string [] custom_data_values = remote_control.CustomData.Split('\n');
  104.     int movement_timer = 60;
  105.     Vector3D player_coords_random = new Vector3D(0,0,0);
  106.     Int32.TryParse(custom_data_values[0], out movement_timer);
  107.     Vector3D.TryParse(custom_data_values[1], out player_coords_random);
  108.     double drone_player_random_distance = MeasureDistance(player_coords_random, drone_coords);
  109.     movement_timer++;
  110.    
  111.     if(movement_timer >= 60 || drone_player_random_distance < 30){
  112.        
  113.         movement_timer = 0;
  114.         player_coords_random = player_coords;
  115.         Random rnd = new Random();
  116.         int coord_randomizer =  rnd.Next(-350, 350);
  117.         player_coords_random.X += (double)coord_randomizer;
  118.         coord_randomizer =  rnd.Next(-350, 350);
  119.         player_coords_random.Y += (double)coord_randomizer;
  120.         coord_randomizer =  rnd.Next(-350, 350);
  121.         player_coords_random.Z += (double)coord_randomizer;
  122.        
  123.     }
  124.    
  125.     remote_control.ClearWaypoints();
  126.     remote_control.AddWaypoint(player_coords_random, "Player");
  127.     remote_control.SetAutoPilotEnabled(true);
  128.    
  129.     remote_control.CustomData = movement_timer.ToString() + "\n";
  130.     remote_control.CustomData += player_coords_random.ToString();
  131.    
  132.    
  133.     //////////////////////////////////////////////////////
  134.     //Pirate Behavior End
  135.     //////////////////////////////////////////////////////
  136.    
  137. }
  138.  
  139. bool RaycastScan(){
  140.    
  141.     List<IMyTerminalBlock> camera_list = new List<IMyTerminalBlock>();
  142.     GridTerminalSystem.GetBlocksOfType<IMyCameraBlock>(camera_list);
  143.    
  144.     if(camera_list.Count != 0){
  145.        
  146.         for(int i = 0; i < camera_list.Count; i++){
  147.            
  148.             IMyCameraBlock camera = camera_list[i] as IMyCameraBlock;
  149.            
  150.             if(camera.IsFunctional == true){
  151.                
  152.                 camera.EnableRaycast = true;
  153.                
  154.                 if(camera.CanScan(1600)){
  155.                    
  156.                     scanned_target = camera.Raycast(1600, 0, 0);
  157.                    
  158.                     if(scanned_target.Relationship.ToString() != "Owner" || scanned_target.Relationship.ToString() != "FactionShare"){
  159.                        
  160.                         if(scanned_target.Type.ToString() == "LargeGrid" || scanned_target.Type.ToString() == "SmallGrid" || scanned_target.Type.ToString() == "CharacterHuman"){
  161.                            
  162.                             scanned_target_coords = scanned_target.Position;
  163.                             return true;
  164.                            
  165.                         }
  166.                        
  167.                     }
  168.                    
  169.                 }
  170.                
  171.             }
  172.            
  173.         }
  174.        
  175.     }else{
  176.    
  177.         Echo("No Camera Block Found. Cannot Do Raycast Scan.");
  178.         return false;
  179.        
  180.     }
  181.    
  182.     return false;
  183.    
  184. }
  185.  
  186. void WeaponActivation(string weapon_type, string weapon_action){
  187.    
  188.     List<IMyTerminalBlock> ranged_weapons_list = new List<IMyTerminalBlock>();
  189.     List<IMyTerminalBlock> grinders_list = new List<IMyTerminalBlock>();
  190.     List<IMyTerminalBlock> turrets_list = new List<IMyTerminalBlock>();
  191.    
  192.     GridTerminalSystem.GetBlocksOfType<IMyUserControllableGun>(ranged_weapons_list);
  193.     GridTerminalSystem.GetBlocksOfType<IMyShipGrinder>(grinders_list);
  194.     GridTerminalSystem.GetBlocksOfType<IMyLargeMissileTurret>(turrets_list);
  195.     GridTerminalSystem.GetBlocksOfType<IMyLargeGatlingTurret>(turrets_list);
  196.     GridTerminalSystem.GetBlocksOfType<IMyLargeInteriorTurret>(turrets_list);
  197.    
  198.     //Ranged Weapons
  199.     if(weapon_type == "Ranged"){
  200.        
  201.         for(int i = 0; i < ranged_weapons_list.Count; i++){
  202.        
  203.             IMyUserControllableGun weapon = ranged_weapons_list[i] as IMyUserControllableGun;
  204.             if(scanned_target_distance <= 800 && valid_scanned_target == true && use_raycast_ranged_weapons == true){
  205.                
  206.                 weapon.ApplyAction("Shoot_On");
  207.                
  208.             }else{
  209.                
  210.                 weapon.ApplyAction("Shoot_Off");
  211.                
  212.             }
  213.        
  214.         }
  215.        
  216.     }
  217.    
  218.     //Grinder
  219.     if(weapon_type == "Grinder"){
  220.        
  221.         for(int i = 0; i < grinders_list.Count; i++){
  222.        
  223.             if(scanned_target_distance <= 100 && valid_scanned_target == true && use_raycast_close_weapons == true){
  224.                
  225.                 grinders_list[i].ApplyAction("OnOff_On");
  226.                
  227.             }else{
  228.                
  229.                 grinders_list[i].ApplyAction("OnOff_Off");
  230.                
  231.             }
  232.        
  233.         }
  234.        
  235.     }
  236.    
  237.     //Turrets
  238.     if(weapon_type == "Turret"){
  239.        
  240.         for(int i = 0; i < turrets_list.Count; i++){
  241.            
  242.             turrets_list[i].ApplyAction(weapon_action);
  243.        
  244.         }
  245.        
  246.     }
  247.    
  248. }
  249.  
  250. double MeasureDistance(Vector3D point_a, Vector3D point_b){
  251.    
  252.     double result = Math.Round( Vector3D.Distance( point_a, point_b ), 2 );
  253.     return result;
  254.    
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement