public void GenericDamageHandler(object target, MyDamageInformation info) { if (target == null || !(target is IMySlimBlock)) { return; } IMySlimBlock block = target as IMySlimBlock; IMyCubeGrid grid = block.CubeGrid; long gridId = grid.GetTopMostParent().EntityId; IMyFaction damageeFaction = grid.GetOwningFaction(); IMyPlayer damagerPlayer; IMyFaction damagerFaction; IMyCubeGrid damagerShip; if (damageeFaction != null && info.GetSource(out damagerPlayer, out damagerFaction, out damagerShip)) { if (damagerFaction == null) { return; } EmpireData damagerEmpire = damagerFaction.GetEmpire(); foreach (EmpireManager empire in m_empireManagers) { if (empire.GetData().empireTag == damageeFaction.Tag) { empire.TakeStandingsHit(damagerEmpire); break; } } } }
/** * Determine the source Player and Ship associated with a damage event. * Returns false if the event was environmental, or no player/ship can be determined. */ public static bool GetSource( this MyDamageInformation info, out IMyPlayer damagePlayer, out IMyFaction damageFaction, out IMyCubeGrid damageShip) { damagePlayer = null; damageFaction = null; damageShip = null; IMyEntity entity = MyAPIGateway.Entities.GetEntityById(info.AttackerId); if (entity == null) { return false; } if (entity is IMyMeteor) { return false; } if (entity is IMyThrust) { return false; } if (entity is IMyWarhead) { return GetPlayerByWarhead( entity as IMyWarhead, out damagePlayer, out damageFaction); } entity = entity.GetTopMostParent(); if (entity == null) { return false; } if (entity is IMyCubeGrid) { damageShip = entity as IMyCubeGrid; damagePlayer = damageShip.GetControllingPlayer(); if (damagePlayer == null) { damagePlayer = damageShip.GetOwningPlayer(); } damageFaction = damageShip.GetOwningFaction(); } else { if (entity is IMyEngineerToolBase) { return GetPlayerByTool( entity as IMyEngineerToolBase, out damagePlayer, out damageFaction); } if (entity is IMyGunBaseUser) { return GetPlayerByWeapon( entity as IMyGunBaseUser, out damagePlayer, out damageFaction); } } return damageShip != null || damageFaction != null || damagePlayer != null; }
protected IMyEntity GetClosestEnemy(float maxSearchRadius) { if (!Active) { return null; } IMyFaction selfFaction = m_remote.CubeGrid.GetOwningFaction(); BoundingSphereD bounds = new BoundingSphereD(m_remote.GetPosition(), maxSearchRadius); List<IMyEntity> possibleEnemies = MyAPIGateway.Entities.GetEntitiesInSphere(ref bounds); double minDist = double.MaxValue; IMyEntity closest = null; foreach (IMyEntity entity in possibleEnemies) { if(entity == m_remote.CubeGrid) { continue; } Vector3D position = entity.GetPosition(); IMyFaction objectFaction = null; if (entity is IMyCubeGrid) { IMyCubeGrid grid = entity as IMyCubeGrid; objectFaction = grid.GetOwningFaction(); } else if (entity is IMyPlayer) { IMyPlayer player = entity as IMyPlayer; objectFaction = MyAPIGateway.Session.Factions.TryGetPlayerFaction(player.IdentityId); } if (objectFaction == null) { continue; } MyRelationsBetweenFactions relation = MyAPIGateway.Session.Factions.GetRelationBetweenFactions(selfFaction.FactionId, objectFaction.FactionId); if (relation.Equals(MyRelationsBetweenFactions.Enemies)) { double dist = m_remote.GetPosition().DistanceTo(position); if(dist < minDist) { minDist = dist; closest = entity; } } } return closest; }