Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Meridius_IX's Pirate AI Systems
- double player_detect_range = 15000; //If player is further than this value from drone, it will not execute any behavioral scripting.
- bool use_raycast_ranged_weapons = true;
- bool use_raycast_close_weapons = false;
- //////////////////////////////////////////////////////
- //Do Not Touch Anything Below Here
- //////////////////////////////////////////////////////
- double player_drone_distance = 0;
- double player_origin_distance = 0;
- double drone_origin_distance = 0;
- //Raycast Scan Data
- MyDetectedEntityInfo scanned_target;
- bool valid_scanned_target = false;
- double scanned_target_distance = 0;
- Vector3D scanned_target_coords = new Vector3D(0,0,0);
- Random random = new Random();
- IMyRemoteControl remote_control;
- void Main(string argument){
- Vector3D world_center = new Vector3D(0,0,0);
- Vector3D player_coords = new Vector3D(0,0,0);
- Vector3D drone_coords = new Vector3D(0,0,0);
- Vector3D origin_coords = new Vector3D(0,0,0);
- //Find Remote Control Block(s)
- List<IMyTerminalBlock> remote_control_list = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyRemoteControl>(remote_control_list);
- if(remote_control_list.Count != 0){
- for(int i = 0; i < remote_control_list.Count; i++){
- if(remote_control_list[i].IsFunctional == true){
- remote_control = remote_control_list[i] as IMyRemoteControl;
- break;
- }
- }
- }else{
- Echo("No Remote Control Block Found. Script Cannot Execute.");
- return;
- }
- //Check If Drone is NPC Owned
- remote_control.GetNearestPlayer(out player_coords);
- if(player_coords == world_center){
- Echo("Drone is not owned by Hostile NPC. Script Cannot Continue.");
- return;
- }
- drone_coords = remote_control.GetPosition();
- //Get & Set Origin Coords
- if(this.Storage == null || this.Storage == ""){
- this.Storage = drone_coords.ToString();
- origin_coords = drone_coords;
- }else{
- Vector3D.TryParse(this.Storage, out origin_coords);
- }
- //Calculate Distances
- player_drone_distance = MeasureDistance(player_coords, drone_coords);
- player_origin_distance = MeasureDistance(player_coords, origin_coords);
- drone_origin_distance = MeasureDistance(drone_coords, origin_coords);
- scanned_target_distance = MeasureDistance(drone_coords, scanned_target_coords);
- if(player_drone_distance > player_detect_range){
- Echo("No players nearby. Aborting Script Execution");
- return;
- }
- //////////////////////////////////////////////////////
- //Pirate Behavior Start
- //////////////////////////////////////////////////////
- string current_mode = "Attack";
- Vector3D retreat_coords = new Vector3D(0,0,0);
- double abort_distance = 700; //Drone will abort bombardment if distance to player is less than this value.
- //Get|Init Remote Custom Data
- if(remote_control.CustomData == ""){
- remote_control.CustomData = current_mode + "\n";
- remote_control.CustomData += retreat_coords.ToString() + "\n";
- }else{
- string [] data_split = remote_control.CustomData.Split('\n');
- current_mode = data_split[0];
- Vector3D.TryParse(data_split[1], out retreat_coords);
- }
- //Mode: DiveBomb
- if(current_mode == "Attack"){
- remote_control.ClearWaypoints();
- remote_control.AddWaypoint(player_coords, "Player");
- remote_control.SetAutoPilotEnabled(true);
- remote_control.ApplyAction("CollisionAvoidance_Off");
- if(player_drone_distance < 1000){
- remote_control.SetValueFloat("SpeedLimit", 5);
- }else{
- remote_control.SetValueFloat("SpeedLimit", 100f);
- }
- valid_scanned_target = RaycastScan();
- WeaponActivation("Ranged", "None");
- if(player_drone_distance < abort_distance){
- current_mode = "CalculateRetreat";
- }
- }
- //Mode: CalculateRetreat
- if(current_mode == "CalculateRetreat"){
- for(int i = 0; i < 10; i++){
- Vector3D random_dir = new Vector3D(0,0,0);
- random_dir.X = RandomNumberBetween(-0.999999, 0.999999);
- random_dir.Y = RandomNumberBetween(-0.999999, 0.999999);
- random_dir.Z = RandomNumberBetween(-0.999999, 0.999999);
- retreat_coords = random_dir * 1000 + player_coords;
- if(MeasureDistance(player_coords, retreat_coords) > 850){
- break;
- }
- }
- current_mode = "Retreat";
- }
- //Mode: Retreat
- if(current_mode == "Retreat"){
- remote_control.ClearWaypoints();
- remote_control.AddWaypoint(retreat_coords, "Retreat");
- remote_control.SetAutoPilotEnabled(true);
- remote_control.ApplyAction("CollisionAvoidance_On");
- remote_control.SetValueFloat("SpeedLimit", 100f);
- valid_scanned_target = RaycastScan();
- WeaponActivation("Ranged", "None");
- double drone_retreat_distance = MeasureDistance(drone_coords, retreat_coords);
- if(drone_retreat_distance < 35){
- remote_control.SetAutoPilotEnabled(false);
- current_mode = "Attack";
- }
- }
- remote_control.CustomData = current_mode + "\n";
- remote_control.CustomData += retreat_coords.ToString() + "\n";
- //////////////////////////////////////////////////////
- //Pirate Behavior End
- //////////////////////////////////////////////////////
- }
- double RandomNumberBetween(double minValue, double maxValue){
- var next = random.NextDouble();
- return minValue + (next * (maxValue - minValue));
- }
- bool RaycastScan(){
- List<IMyTerminalBlock> camera_list = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyCameraBlock>(camera_list);
- if(camera_list.Count != 0){
- for(int i = 0; i < camera_list.Count; i++){
- IMyCameraBlock camera = camera_list[i] as IMyCameraBlock;
- if(camera.IsFunctional == true){
- camera.EnableRaycast = true;
- if(camera.CanScan(800)){
- scanned_target = camera.Raycast(800, 0, 0);
- if(scanned_target.Relationship.ToString() != "Owner" || scanned_target.Relationship.ToString() != "FactionShare"){
- if(scanned_target.Type.ToString() == "LargeGrid" || scanned_target.Type.ToString() == "SmallGrid" || scanned_target.Type.ToString() == "CharacterHuman"){
- scanned_target_coords = scanned_target.Position;
- return true;
- }
- }
- }
- }
- }
- }else{
- Echo("No Camera Block Found. Cannot Do Raycast Scan.");
- return false;
- }
- return false;
- }
- void WeaponActivation(string weapon_type, string weapon_action){
- List<IMyTerminalBlock> ranged_weapons_list = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> grinders_list = new List<IMyTerminalBlock>();
- List<IMyTerminalBlock> turrets_list = new List<IMyTerminalBlock>();
- GridTerminalSystem.GetBlocksOfType<IMyUserControllableGun>(ranged_weapons_list);
- GridTerminalSystem.GetBlocksOfType<IMyShipGrinder>(grinders_list);
- GridTerminalSystem.GetBlocksOfType<IMyLargeMissileTurret>(turrets_list);
- GridTerminalSystem.GetBlocksOfType<IMyLargeGatlingTurret>(turrets_list);
- GridTerminalSystem.GetBlocksOfType<IMyLargeInteriorTurret>(turrets_list);
- //Ranged Weapons
- if(weapon_type == "Ranged"){
- for(int i = 0; i < ranged_weapons_list.Count; i++){
- IMyUserControllableGun weapon = ranged_weapons_list[i] as IMyUserControllableGun;
- if(scanned_target_distance <= 800 && valid_scanned_target == true && use_raycast_ranged_weapons == true){
- weapon.ApplyAction("Shoot_On");
- }else{
- weapon.ApplyAction("Shoot_Off");
- }
- }
- }
- //Grinder
- if(weapon_type == "Grinder"){
- for(int i = 0; i < grinders_list.Count; i++){
- if(scanned_target_distance <= 100 && valid_scanned_target == true && use_raycast_close_weapons == true){
- grinders_list[i].ApplyAction("OnOff_On");
- }else{
- grinders_list[i].ApplyAction("OnOff_Off");
- }
- }
- }
- //Turrets
- if(weapon_type == "Turret"){
- for(int i = 0; i < turrets_list.Count; i++){
- turrets_list[i].ApplyAction(weapon_action);
- }
- }
- }
- double MeasureDistance(Vector3D point_a, Vector3D point_b){
- double result = Math.Round( Vector3D.Distance( point_a, point_b ), 2 );
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement