Advertisement
Meridius_IX

Sniper Drone

Sep 2nd, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.14 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 = 700; //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.         if(player_drone_distance < 1000){
  126.            
  127.             remote_control.SetValueFloat("SpeedLimit", 5);
  128.            
  129.         }else{
  130.            
  131.             remote_control.SetValueFloat("SpeedLimit", 100f);
  132.            
  133.         }
  134.         valid_scanned_target = RaycastScan();
  135.         WeaponActivation("Ranged", "None");
  136.        
  137.         if(player_drone_distance < abort_distance){
  138.            
  139.             current_mode = "CalculateRetreat";
  140.            
  141.         }
  142.        
  143.     }
  144.  
  145.     //Mode: CalculateRetreat
  146.     if(current_mode == "CalculateRetreat"){
  147.        
  148.         for(int i = 0; i < 10; i++){
  149.            
  150.             Vector3D random_dir = new Vector3D(0,0,0);
  151.             random_dir.X = RandomNumberBetween(-0.999999, 0.999999);
  152.             random_dir.Y = RandomNumberBetween(-0.999999, 0.999999);
  153.             random_dir.Z = RandomNumberBetween(-0.999999, 0.999999);
  154.             retreat_coords = random_dir * 1000 + player_coords;
  155.             if(MeasureDistance(player_coords, retreat_coords) > 850){
  156.                
  157.                 break;
  158.                
  159.             }
  160.            
  161.            
  162.         }
  163.        
  164.         current_mode = "Retreat";
  165.        
  166.     }
  167.    
  168.     //Mode: Retreat
  169.     if(current_mode == "Retreat"){
  170.        
  171.         remote_control.ClearWaypoints();
  172.         remote_control.AddWaypoint(retreat_coords, "Retreat");
  173.         remote_control.SetAutoPilotEnabled(true);
  174.         remote_control.ApplyAction("CollisionAvoidance_On");
  175.         remote_control.SetValueFloat("SpeedLimit", 100f);
  176.         valid_scanned_target = RaycastScan();
  177.         WeaponActivation("Ranged", "None");
  178.        
  179.         double drone_retreat_distance = MeasureDistance(drone_coords, retreat_coords);
  180.         if(drone_retreat_distance < 35){
  181.            
  182.             remote_control.SetAutoPilotEnabled(false);
  183.             current_mode = "Attack";
  184.            
  185.         }
  186.        
  187.     }
  188.    
  189.     remote_control.CustomData = current_mode + "\n";
  190.     remote_control.CustomData += retreat_coords.ToString() + "\n";
  191.    
  192.     //////////////////////////////////////////////////////
  193.     //Pirate Behavior End
  194.     //////////////////////////////////////////////////////
  195.    
  196. }
  197.  
  198. double RandomNumberBetween(double minValue, double maxValue){
  199.    
  200.     var next = random.NextDouble();
  201.     return minValue + (next * (maxValue - minValue));
  202.    
  203. }
  204.  
  205. bool RaycastScan(){
  206.    
  207.     List<IMyTerminalBlock> camera_list = new List<IMyTerminalBlock>();
  208.     GridTerminalSystem.GetBlocksOfType<IMyCameraBlock>(camera_list);
  209.    
  210.     if(camera_list.Count != 0){
  211.        
  212.         for(int i = 0; i < camera_list.Count; i++){
  213.            
  214.             IMyCameraBlock camera = camera_list[i] as IMyCameraBlock;
  215.            
  216.             if(camera.IsFunctional == true){
  217.                
  218.                 camera.EnableRaycast = true;
  219.                
  220.                 if(camera.CanScan(800)){
  221.                    
  222.                     scanned_target = camera.Raycast(800, 0, 0);
  223.                    
  224.                     if(scanned_target.Relationship.ToString() != "Owner" || scanned_target.Relationship.ToString() != "FactionShare"){
  225.                        
  226.                         if(scanned_target.Type.ToString() == "LargeGrid" || scanned_target.Type.ToString() == "SmallGrid" || scanned_target.Type.ToString() == "CharacterHuman"){
  227.                            
  228.                             scanned_target_coords = scanned_target.Position;
  229.                             return true;
  230.                            
  231.                         }
  232.                        
  233.                     }
  234.                    
  235.                 }
  236.                
  237.             }
  238.            
  239.         }
  240.        
  241.     }else{
  242.    
  243.         Echo("No Camera Block Found. Cannot Do Raycast Scan.");
  244.         return false;
  245.        
  246.     }
  247.    
  248.     return false;
  249.    
  250. }
  251.  
  252. void WeaponActivation(string weapon_type, string weapon_action){
  253.    
  254.     List<IMyTerminalBlock> ranged_weapons_list = new List<IMyTerminalBlock>();
  255.     List<IMyTerminalBlock> grinders_list = new List<IMyTerminalBlock>();
  256.     List<IMyTerminalBlock> turrets_list = new List<IMyTerminalBlock>();
  257.    
  258.     GridTerminalSystem.GetBlocksOfType<IMyUserControllableGun>(ranged_weapons_list);
  259.     GridTerminalSystem.GetBlocksOfType<IMyShipGrinder>(grinders_list);
  260.     GridTerminalSystem.GetBlocksOfType<IMyLargeMissileTurret>(turrets_list);
  261.     GridTerminalSystem.GetBlocksOfType<IMyLargeGatlingTurret>(turrets_list);
  262.     GridTerminalSystem.GetBlocksOfType<IMyLargeInteriorTurret>(turrets_list);
  263.    
  264.     //Ranged Weapons
  265.     if(weapon_type == "Ranged"){
  266.        
  267.         for(int i = 0; i < ranged_weapons_list.Count; i++){
  268.        
  269.             IMyUserControllableGun weapon = ranged_weapons_list[i] as IMyUserControllableGun;
  270.             if(scanned_target_distance <= 800 && valid_scanned_target == true && use_raycast_ranged_weapons == true){
  271.                
  272.                 weapon.ApplyAction("Shoot_On");
  273.                
  274.             }else{
  275.                
  276.                 weapon.ApplyAction("Shoot_Off");
  277.                
  278.             }
  279.        
  280.         }
  281.        
  282.     }
  283.    
  284.     //Grinder
  285.     if(weapon_type == "Grinder"){
  286.        
  287.         for(int i = 0; i < grinders_list.Count; i++){
  288.        
  289.             if(scanned_target_distance <= 100 && valid_scanned_target == true && use_raycast_close_weapons == true){
  290.                
  291.                 grinders_list[i].ApplyAction("OnOff_On");
  292.                
  293.             }else{
  294.                
  295.                 grinders_list[i].ApplyAction("OnOff_Off");
  296.                
  297.             }
  298.        
  299.         }
  300.        
  301.     }
  302.    
  303.     //Turrets
  304.     if(weapon_type == "Turret"){
  305.        
  306.         for(int i = 0; i < turrets_list.Count; i++){
  307.            
  308.             turrets_list[i].ApplyAction(weapon_action);
  309.        
  310.         }
  311.        
  312.     }
  313.    
  314. }
  315.  
  316. double MeasureDistance(Vector3D point_a, Vector3D point_b){
  317.    
  318.     double result = Math.Round( Vector3D.Distance( point_a, point_b ), 2 );
  319.     return result;
  320.    
  321. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement