Esempio n. 1
0
        /// <summary>
        /// Determines if damage was done by player.
        /// </summary>
        /// <param name="Damager">Provides player who did the damage. Null if damager object is not a player.</param>
        public static bool IsDoneByPlayer(this MyDamageInformation Damage, out IMyPlayer Damager)
        {
            Damager = null;
            try
            {
                IMyEntity AttackerEntity = MyAPIGateway.Entities.GetEntityById(Damage.AttackerId);
                if (AttackerEntity == null)
                {
                    AISessionCore.DebugWrite("Damage.IsDoneByPlayer", "Attacker entity was not found.", AntiSpam: false);
                    return(false);
                }

                if (AttackerEntity is IMyMeteor)
                {
                    return(false);
                }
                if (AttackerEntity is IMyWarhead)
                {
                    return(IsDamagedByPlayerWarhead(AttackerEntity as IMyWarhead, out Damager));
                }
                if (AttackerEntity is IMyGunBaseUser)
                {
                    return(IsDamagedByPlayer(AttackerEntity as IMyGunBaseUser, out Damager));
                }
                if (AttackerEntity is IMyEngineerToolBase)
                {
                    return(IsDamagedByPlayer(AttackerEntity as IMyEngineerToolBase, out Damager));
                }

                AttackerEntity = AttackerEntity.GetTopMostParent();

                if (AttackerEntity == null)
                {
                    AISessionCore.DebugWrite("Damage.IsDoneByPlayer", "Cannot acquire the attacker's topmost entity", AntiSpam: false);
                    return(false);
                }

                if (AttackerEntity is IMyCubeGrid)
                {
                    IMyCubeGrid Grid = AttackerEntity as IMyCubeGrid;
                    if (Grid.IsPirate())
                    {
                        return(false);
                    }
                    if (Grid.IsOwnedByNobody())
                    {
                        return(IsDamagedByPlayerInNeutralGrid(Grid, out Damager));
                    }

                    return(IsDamagedByPlayerGrid(Grid, out Damager));
                }

                return(false);
            }
            catch (Exception Scrap)
            {
                AISessionCore.LogError("Damage.IsDoneByPlayer", new Exception("General crash.", Scrap));
                return(false);
            }
        }
Esempio n. 2
0
 public static void DeclareWar(this IMyFaction OurFaction, IMyFaction HostileFaction, bool Print = false)
 {
     MyAPIGateway.Session.Factions.DeclareWar(OurFaction.FactionId, HostileFaction.FactionId);
     if (Print)
     {
         AISessionCore.DebugWrite($"{OurFaction.Tag}", $"Declared war on {HostileFaction.Tag}", AntiSpam: false);
     }
 }
Esempio n. 3
0
 public static void ProposePeace(this IMyFaction OurFaction, IMyFaction HostileFaction, bool Print = false)
 {
     MyAPIGateway.Session.Factions.SendPeaceRequest(OurFaction.FactionId, HostileFaction.FactionId);
     if (Print)
     {
         AISessionCore.DebugWrite($"{OurFaction.Tag}", $"Proposed peace to {HostileFaction.Tag}", AntiSpam: false);
     }
 }
Esempio n. 4
0
        static bool IsDamagedByPlayerInNeutralGrid(IMyCubeGrid Grid, out IMyPlayer Damager)
        {
            Damager = null;
            try
            {
                Damager = Grid.FindControllingPlayer();
                if (Damager != null)
                {
                    return(!Damager.IsBot);
                }

                try
                {
                    List <MyCubeBlock> CubeBlocks = Grid.GetBlocks <MyCubeBlock>(x => x.BuiltBy != 0);
                    if (CubeBlocks.Count != 0)
                    {
                        var ThatCunningGrieferID = CubeBlocks[0].BuiltBy;
                        Damager = MyAPIGateway.Players.GetPlayerByID(ThatCunningGrieferID);
                        return(Damager != null);
                    }
                    else
                    {
                        List <IMySlimBlock> SlimBlocks = Grid.GetBlocks(Selector: x => x.GetBuiltBy() != 0, BlockLimit: 50);
                        if (SlimBlocks.Count == 0)
                        {
                            return(false);                       // We give up on this one
                        }
                        else
                        {
                            try
                            {
                                Damager = MyAPIGateway.Players.GetPlayerByID(SlimBlocks.First().GetBuiltBy());
                                if (Damager != null)
                                {
                                    Grid.DebugWrite("Damage.IsDoneByPlayer.FindBuilderBySlimBlocks", $"Found damager player from slim block. Damager is {Damager.DisplayName}");
                                }
                                return(Damager != null);
                            }
                            catch (Exception Scrap)
                            {
                                AISessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check grid via SlimBlocks BuiltBy crashed.", Scrap));
                                return(false);
                            }
                        }
                    }
                }
                catch (Exception Scrap)
                {
                    AISessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check grid via BuiltBy crashed.", Scrap));
                    return(false);
                }
            }
            catch (Exception Scrap)
            {
                AISessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check neutral grid crashed", Scrap));
                return(false);
            }
        }
Esempio n. 5
0
 public static void AcceptPeace(this IMyFaction OurFaction, IMyFaction HostileFaction, bool Print = false)
 {
     MyAPIGateway.Session.Factions.AcceptPeace(HostileFaction.FactionId, OurFaction.FactionId);
     MyAPIGateway.Session.Factions.AcceptPeace(OurFaction.FactionId, HostileFaction.FactionId);
     if (Print)
     {
         AISessionCore.DebugWrite($"{OurFaction.Tag}", $"Accepted peace from {HostileFaction.Tag}", AntiSpam: false);
     }
 }
Esempio n. 6
0
 virtual public void Shutdown()
 {
     Closed = true;
     if (HasModdedThrusters)
     {
         DemultiplyThrusters();
     }
     AISessionCore.RemoveDamageHandler(Grid);
 }
Esempio n. 7
0
 static bool IsDamagedByPlayer(IMyEngineerToolBase Tool, out IMyPlayer Damager)
 {
     Damager = null;
     try
     {
         Damager = MyAPIGateway.Players.GetPlayerByID(Tool.OwnerId);
         return(Damager != null ? !Damager.IsBot : false);
     }
     catch (Exception Scrap)
     {
         AISessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check gun owner crashed", Scrap));
         return(false);
     }
 }
Esempio n. 8
0
 static bool IsDamagedByPlayer(IMyEngineerToolBase Tool, out IMyPlayer Damager)
 {
     Damager = null;
     try
     {
         Damager = MyAPIGateway.Players.GetPlayerByID(Tool.OwnerIdentityId);
         //AISessionCore.DebugWrite($"ToolDamage.IsDamagedByPlayer", $"Getting player from tool. ID: {Tool.OwnerId}, IdentityID: {Tool.OwnerIdentityId}, player: {(Damager != null ? Damager.DisplayName : "null")}", false);
         return(Damager != null ? !Damager.IsBot : false);
     }
     catch (Exception Scrap)
     {
         AISessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check gun owner crashed", Scrap));
         return(false);
     }
 }
Esempio n. 9
0
        virtual protected void RegisterHostileAction(IMyPlayer Player, TimeSpan TruceDelay)
        {
            try
            {
                #region Sanity checks
                if (Player == null)
                {
                    Grid.DebugWrite("RegisterHostileAction", "Error: Damager is null.");
                    return;
                }
                ;

                if (OwnerFaction == null)
                {
                    OwnerFaction = Grid.GetOwnerFaction();
                }

                if (OwnerFaction == null || !OwnerFaction.IsNPC())
                {
                    Grid.DebugWrite("RegisterHostileAction", $"Error: {(OwnerFaction == null ? "can't find own faction" : "own faction isn't recognized as NPC.")}");
                    return;
                }
                #endregion

                IMyFaction HostileFaction = Player.GetFaction();
                if (HostileFaction == null)
                {
                    Grid.DebugWrite("RegisterHostileAction", "Error: can't find damager's faction");
                    return;
                }
                else if (HostileFaction == OwnerFaction)
                {
                    OwnerFaction.Kick(Player);
                    return;
                }

                AISessionCore.DeclareWar(OwnerFaction, HostileFaction, TruceDelay);
                if (OwnerFaction.IsLawful())
                {
                    AISessionCore.DeclareWar(Diplomacy.Police, HostileFaction, TruceDelay);
                    AISessionCore.DeclareWar(Diplomacy.Army, HostileFaction, TruceDelay);
                }
            }
            catch (Exception Scrap)
            {
                LogError("RegisterHostileAction", Scrap);
            }
        }
Esempio n. 10
0
 virtual protected void RegisterHostileAction(IMyFaction HostileFaction, TimeSpan TruceDelay)
 {
     try
     {
         if (HostileFaction == null)
         {
             Grid.DebugWrite("RegisterHostileAction", "Error: can't find damager's faction");
             return;
         }
         AISessionCore.DeclareWar(OwnerFaction, HostileFaction, TruceDelay);
         if (OwnerFaction.IsLawful())
         {
             AISessionCore.DeclareWar(Diplomacy.Police, HostileFaction, TruceDelay);
             AISessionCore.DeclareWar(Diplomacy.Army, HostileFaction, TruceDelay);
         }
     }
     catch (Exception Scrap)
     {
         LogError("RegisterHostileAction", Scrap);
     }
 }
Esempio n. 11
0
 static bool IsDamagedByPlayerGrid(IMyCubeGrid Grid, out IMyPlayer Damager)
 {
     Damager = null;
     try
     {
         long BiggestOwner = Grid.BigOwners.FirstOrDefault();
         if (BiggestOwner != 0)
         {
             Damager = MyAPIGateway.Players.GetPlayerByID(BiggestOwner);
             return(Damager != null ? !Damager.IsBot : false);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception Scrap)
     {
         AISessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check grid via BigOwners crashed", Scrap));
         return(false);
     }
 }
Esempio n. 12
0
 public static bool IsNPC(this IMyFaction Faction)
 {
     try
     {
         IMyPlayer Owner = MyAPIGateway.Players.GetPlayerByID(Faction.FounderId);
         if (Owner != null)
         {
             return(Owner.IsBot);
         }
         else
         {
             if (Faction.Members.Count() == 0)
             {
                 return(true);
             }
             foreach (var member in Faction.Members)
             {
                 IMyPlayer Member = MyAPIGateway.Players.GetPlayerByID(member.Value.PlayerId);
                 if (Member == null)
                 {
                     continue;
                 }
                 if (!Member.IsBot)
                 {
                     return(false);
                 }
             }
             return(true);
         }
     }
     catch (Exception Scrap)
     {
         AISessionCore.LogError("Faction.IsNPC", Scrap);
         return(false);
     }
 }
Esempio n. 13
0
 static bool IsDamagedByPlayerWarhead(IMyWarhead Warhead, out IMyPlayer Damager)
 {
     Damager = null;
     try
     {
         if (Warhead.OwnerId == 0)
         {
             Damager = MyAPIGateway.Players.GetPlayerByID((Warhead as MyCubeBlock).BuiltBy);
             AISessionCore.DebugWrite("Damage.IsDoneByPlayer", "Attempting to find damager by neutral warhead.");
             return(Damager != null);
         }
         else
         {
             Damager = MyAPIGateway.Players.GetPlayerByID(Warhead.OwnerId);
             AISessionCore.DebugWrite("Damage.IsDoneByPlayer", "Attempting to find damager by warhead owner.");
             return(Damager != null);
         }
     }
     catch (Exception Scrap)
     {
         AISessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check for neutral warheads crashed", Scrap));
         return(false);
     }
 }
Esempio n. 14
0
        virtual public bool Init(IMyRemoteControl RC = null)
        {
            this.RC = RC ?? Term.GetBlocksOfType <IMyRemoteControl>(collect: x => x.IsFunctional).FirstOrDefault();
            if (RC == null)
            {
                return(false);
            }
            DroneName = DroneNameProvider;

            Antennae = Term.GetBlocksOfType <IMyRadioAntenna>(collect: x => x.IsFunctional);

            bool HasSetup = ParseSetup();

            if (!HasSetup)
            {
                return(false);
            }

            AISessionCore.AddDamageHandler(Grid, (Block, Damage) => OnDamaged(Block, Damage));
            Grid.OnBlockAdded += (Block) => OnBlockPlaced(Block);
            OwnerFaction       = Grid.GetOwnerFaction(RecalculateOwners: true);
            BotOperable        = true;
            return(true);
        }