/// <summary> /// Determines if damage was done by player. /// </summary> /// <param name="damage"></param> /// <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 (damage.IsDeformation || damage.AttackerId == 0 || attackerEntity == null) { return(false); } if (attackerEntity is IMyMeteor) { return(false); } if (attackerEntity is IMyWarhead) { return(IsDamagedByPlayerWarhead(attackerEntity as IMyWarhead, out damager)); } if (attackerEntity is IMyEngineerToolBase) { return(IsDamagedByPlayer(attackerEntity as IMyEngineerToolBase, out damager)); } if (attackerEntity is IMyGunBaseUser) { return(IsDamagedByPlayer(attackerEntity as IMyGunBaseUser, out damager)); } attackerEntity = attackerEntity.GetTopMostParent(); if (attackerEntity == null) { AiSessionCore.DebugLog?.WriteToLog("IsDoneByPlayer", $"attackerEntity was NULL"); AiSessionCore.DebugWrite("Damage.IsDoneByPlayer", "Cannot acquire the attacker's topmost entity", antiSpam: false); return(false); } if (!(attackerEntity is IMyCubeGrid)) { return(false); } IMyCubeGrid grid = attackerEntity as IMyCubeGrid; if (grid.IsPirate()) { return(false); } //grid.GetOwnerFaction() return(grid.IsOwnedByNobody() ? IsDamagedByPlayerInNeutralGrid(grid, out damager) : IsDamagedByPlayerGrid(grid, out damager)); } catch (Exception scrap) { AiSessionCore.LogError("Damage.IsDoneByPlayer", new Exception("General crash.", scrap)); return(false); } }
/// <summary> /// Determines if damage was done by player. /// <para/> /// If it's necessary to determine who did the damage, use overload. /// </summary> //public static bool IsDoneByPlayer(this MyDamageInformation damage) //{ // IMyPlayer trash; // return damage.IsDoneByPlayer(out trash); //} private static bool IsDamagedByPlayerWarhead(IMyWarhead warhead, out IMyPlayer damager) { damager = null; try { if (warhead.OwnerId == 0) { damager = MyAPIGateway.Players.GetPlayerById(((MyCubeBlock)warhead).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); } }