Advertisement
Meridius_IX

Territorial Drone

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