Advertisement
Meridius_IX

Strike Drone

Sep 2nd, 2017
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.77 KB | None | 0 0
  1. //Meridius_IX's Pirate AI Systems
  2.  
  3. double player_detect_range = 15000; //If player is further than this value from drone, it will not execute any behavioral scripting.
  4.  
  5. bool use_raycast_ranged_weapons = true;
  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. Random random = new Random();
  23.  
  24. IMyRemoteControl remote_control;
  25.  
  26. void Main(string argument){
  27.    
  28.     Vector3D world_center = new Vector3D(0,0,0);
  29.     Vector3D player_coords = new Vector3D(0,0,0);
  30.     Vector3D drone_coords = new Vector3D(0,0,0);
  31.     Vector3D origin_coords = new Vector3D(0,0,0);
  32.    
  33.     //Find Remote Control Block(s)
  34.     List<IMyTerminalBlock> remote_control_list = new List<IMyTerminalBlock>();
  35.     GridTerminalSystem.GetBlocksOfType<IMyRemoteControl>(remote_control_list);
  36.    
  37.     if(remote_control_list.Count != 0){
  38.        
  39.         for(int i = 0; i < remote_control_list.Count; i++){
  40.            
  41.             if(remote_control_list[i].IsFunctional == true){
  42.                
  43.                 remote_control = remote_control_list[i] as IMyRemoteControl;
  44.                 break;
  45.                
  46.             }
  47.            
  48.         }
  49.        
  50.     }else{
  51.        
  52.         Echo("No Remote Control Block Found. Script Cannot Execute.");
  53.         return;
  54.        
  55.     }
  56.    
  57.     //Check If Drone is NPC Owned
  58.     remote_control.GetNearestPlayer(out player_coords);
  59.    
  60.     if(player_coords == world_center){
  61.        
  62.         Echo("Drone is not owned by Hostile NPC. Script Cannot Continue.");
  63.         return;
  64.        
  65.     }
  66.    
  67.     drone_coords = remote_control.GetPosition();
  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 players nearby. Aborting Script Execution");
  90.         return;
  91.        
  92.     }
  93.        
  94.     //////////////////////////////////////////////////////
  95.     //Pirate Behavior Start
  96.     //////////////////////////////////////////////////////
  97.    
  98.     string current_mode = "Attack";
  99.     Vector3D retreat_coords = new Vector3D(0,0,0);
  100.    
  101.     double abort_distance = 300; //Drone will abort bombardment if distance to player is less than this value.
  102.    
  103.     //Get|Init Remote Custom Data
  104.  
  105.     if(remote_control.CustomData == ""){
  106.        
  107.         remote_control.CustomData = current_mode + "\n";
  108.         remote_control.CustomData += retreat_coords.ToString() + "\n";
  109.        
  110.     }else{
  111.        
  112.         string [] data_split = remote_control.CustomData.Split('\n');
  113.         current_mode = data_split[0];
  114.         Vector3D.TryParse(data_split[1], out retreat_coords);
  115.        
  116.     }
  117.  
  118.     //Mode: DiveBomb
  119.     if(current_mode == "Attack"){
  120.        
  121.         remote_control.ClearWaypoints();
  122.         remote_control.AddWaypoint(player_coords, "Player");
  123.         remote_control.SetAutoPilotEnabled(true);
  124.         remote_control.ApplyAction("CollisionAvoidance_Off");
  125.         valid_scanned_target = RaycastScan();
  126.         WeaponActivation("Ranged", "None");
  127.        
  128.         if(player_drone_distance < abort_distance){
  129.            
  130.             current_mode = "CalculateRetreat";
  131.            
  132.         }
  133.        
  134.     }
  135.  
  136.     //Mode: CalculateRetreat
  137.     if(current_mode == "CalculateRetreat"){
  138.        
  139.         Vector3D random_dir = new Vector3D(0,0,0);
  140.         random_dir.X = RandomNumberBetween(-0.999999, 0.999999);
  141.         random_dir.Y = RandomNumberBetween(-0.999999, 0.999999);
  142.         random_dir.Z = RandomNumberBetween(-0.999999, 0.999999);
  143.         retreat_coords = random_dir * 900 + player_coords;
  144.         current_mode = "Retreat";
  145.        
  146.     }
  147.    
  148.     //Mode: Retreat
  149.     if(current_mode == "Retreat"){
  150.        
  151.         remote_control.ClearWaypoints();
  152.         remote_control.AddWaypoint(retreat_coords, "Retreat");
  153.         remote_control.SetAutoPilotEnabled(true);
  154.         remote_control.ApplyAction("CollisionAvoidance_On");
  155.         valid_scanned_target = RaycastScan();
  156.         WeaponActivation("Ranged", "None");
  157.        
  158.         double drone_retreat_distance = MeasureDistance(drone_coords, retreat_coords);
  159.         if(drone_retreat_distance < 35){
  160.            
  161.             remote_control.SetAutoPilotEnabled(false);
  162.             current_mode = "Attack";
  163.            
  164.         }
  165.        
  166.     }
  167.    
  168.     remote_control.CustomData = current_mode + "\n";
  169.     remote_control.CustomData += retreat_coords.ToString() + "\n";
  170.    
  171.     //////////////////////////////////////////////////////
  172.     //Pirate Behavior End
  173.     //////////////////////////////////////////////////////
  174.    
  175. }
  176.  
  177. double RandomNumberBetween(double minValue, double maxValue){
  178.    
  179.     var next = random.NextDouble();
  180.     return minValue + (next * (maxValue - minValue));
  181.    
  182. }
  183.  
  184. bool RaycastScan(){
  185.    
  186.     List<IMyTerminalBlock> camera_list = new List<IMyTerminalBlock>();
  187.     GridTerminalSystem.GetBlocksOfType<IMyCameraBlock>(camera_list);
  188.    
  189.     if(camera_list.Count != 0){
  190.        
  191.         for(int i = 0; i < camera_list.Count; i++){
  192.            
  193.             IMyCameraBlock camera = camera_list[i] as IMyCameraBlock;
  194.            
  195.             if(camera.IsFunctional == true){
  196.                
  197.                 camera.EnableRaycast = true;
  198.                
  199.                 if(camera.CanScan(800)){
  200.                    
  201.                     scanned_target = camera.Raycast(800, 0, 0);
  202.                    
  203.                     if(scanned_target.Relationship.ToString() != "Owner" || scanned_target.Relationship.ToString() != "FactionShare"){
  204.                        
  205.                         if(scanned_target.Type.ToString() == "LargeGrid" || scanned_target.Type.ToString() == "SmallGrid" || scanned_target.Type.ToString() == "CharacterHuman"){
  206.                            
  207.                             scanned_target_coords = scanned_target.Position;
  208.                             return true;
  209.                            
  210.                         }
  211.                        
  212.                     }
  213.                    
  214.                 }
  215.                
  216.             }
  217.            
  218.         }
  219.        
  220.     }else{
  221.    
  222.         Echo("No Camera Block Found. Cannot Do Raycast Scan.");
  223.         return false;
  224.        
  225.     }
  226.    
  227.     return false;
  228.    
  229. }
  230.  
  231. void WeaponActivation(string weapon_type, string weapon_action){
  232.    
  233.     List<IMyTerminalBlock> ranged_weapons_list = new List<IMyTerminalBlock>();
  234.     List<IMyTerminalBlock> grinders_list = new List<IMyTerminalBlock>();
  235.     List<IMyTerminalBlock> turrets_list = new List<IMyTerminalBlock>();
  236.    
  237.     GridTerminalSystem.GetBlocksOfType<IMyUserControllableGun>(ranged_weapons_list);
  238.     GridTerminalSystem.GetBlocksOfType<IMyShipGrinder>(grinders_list);
  239.     GridTerminalSystem.GetBlocksOfType<IMyLargeMissileTurret>(turrets_list);
  240.     GridTerminalSystem.GetBlocksOfType<IMyLargeGatlingTurret>(turrets_list);
  241.     GridTerminalSystem.GetBlocksOfType<IMyLargeInteriorTurret>(turrets_list);
  242.    
  243.     //Ranged Weapons
  244.     if(weapon_type == "Ranged"){
  245.        
  246.         for(int i = 0; i < ranged_weapons_list.Count; i++){
  247.        
  248.             IMyUserControllableGun weapon = ranged_weapons_list[i] as IMyUserControllableGun;
  249.             if(scanned_target_distance <= 800 && valid_scanned_target == true && use_raycast_ranged_weapons == true){
  250.                
  251.                 weapon.ApplyAction("Shoot_On");
  252.                
  253.             }else{
  254.                
  255.                 weapon.ApplyAction("Shoot_Off");
  256.                
  257.             }
  258.        
  259.         }
  260.        
  261.     }
  262.    
  263.     //Grinder
  264.     if(weapon_type == "Grinder"){
  265.        
  266.         for(int i = 0; i < grinders_list.Count; i++){
  267.        
  268.             if(scanned_target_distance <= 100 && valid_scanned_target == true && use_raycast_close_weapons == true){
  269.                
  270.                 grinders_list[i].ApplyAction("OnOff_On");
  271.                
  272.             }else{
  273.                
  274.                 grinders_list[i].ApplyAction("OnOff_Off");
  275.                
  276.             }
  277.        
  278.         }
  279.        
  280.     }
  281.    
  282.     //Turrets
  283.     if(weapon_type == "Turret"){
  284.        
  285.         for(int i = 0; i < turrets_list.Count; i++){
  286.            
  287.             turrets_list[i].ApplyAction(weapon_action);
  288.        
  289.         }
  290.        
  291.     }
  292.    
  293. }
  294.  
  295. double MeasureDistance(Vector3D point_a, Vector3D point_b){
  296.    
  297.     double result = Math.Round( Vector3D.Distance( point_a, point_b ), 2 );
  298.     return result;
  299.    
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement