private IMyPlayer FindPlayer(IMyEngineerToolBase handTool)
        {
            List <IMyPlayer> players = new List <IMyPlayer>();

            MyAPIGateway.Players.GetPlayers(players);

            foreach (IMyPlayer player in players)
            {
                IMyCharacter character = player.Controller.ControlledEntity as IMyCharacter;

                if (character == null)
                {
                    continue;
                }

                // The most inefficient way of finding which player is equiped with what tool.
                // What is needed is either MyCharacter.CurrentWeapon, or MyEngineerToolBase.Owner exposed through the appropriate interface.
                MyObjectBuilder_Character c = character.GetObjectBuilder(true) as MyObjectBuilder_Character;
                if (c != null && c.HandWeapon != null && c.HandWeapon.EntityId == handTool.EntityId)
                {
                    return(player);
                }
            }

            return(null);
        }
        private void Safety_Elapsed(object o, ElapsedEventArgs elapsedEventArgs)
        {
            HashSet <IMyPlayer> keySet = new HashSet <IMyPlayer>(_cache.Keys);
            var correctedEntities      = new Dictionary <IMyPlayer, IMyEngineerToolBase>();

            foreach (IMyPlayer player in keySet)
            {
                IMyEngineerToolBase handTool    = _cache[player];
                IMyPlayer           foundPlayer = FindPlayer(handTool);

                if (player == foundPlayer)
                {
                    continue;
                }

                // remove invalid entries
                _cache.Remove(player);

                // somehow it returns null when a player enters a cockpit or so but if the player enters the cockpit the handtool should be removed from the cache
                // ... still, I can't figure out why it returns null O.o
                if (foundPlayer == null)
                {
                    continue;
                }

                correctedEntities.Add(foundPlayer, handTool);
            }

            foreach (KeyValuePair <IMyPlayer, IMyEngineerToolBase> keyValuePair in correctedEntities)
            {
                _cache.Update(keyValuePair.Key, keyValuePair.Value);
            }
        }
예제 #3
0
 /**
  * Get the owner of a Tool
  */
 private static bool GetPlayerByTool(
     IMyEngineerToolBase tool,
     out IMyPlayer owner,
     out IMyFaction owningFaction) {
   owner = MyAPIGateway.Players.GetPlayerByID(tool.OwnerIdentityId);
   owningFaction = MyAPIGateway.Session.Factions.TryGetPlayerFaction(tool.OwnerIdentityId);
   return owner != null || owningFaction != null;
 }
예제 #4
0
        /// <summary>
        /// Finds out if the entity with the given entityId can damage the given block regarding the given damage type.
        /// </summary>
        /// <param name="attackerEntityId">The entityId of the entity that damages the given block.</param>
        /// <param name="block">The block that is damaged.</param>
        /// <param name="type">The damage type.</param>
        /// <param name="player">If a player is causing the damage, we return it as well.</param>
        /// <returns>True if the block can be damaged.</returns>
        private static bool CanDamageBlock(long attackerEntityId, IMySlimBlock block, MyStringHash type, out IMyPlayer player)
        {
            player = null;
            if (!IsProtected(block))
            {
                return(true);
            }

            if (type == MyDamageType.Grind)
            {
                IMyEntity attackerEntity;
                if (!MyAPIGateway.Entities.TryGetEntityById(attackerEntityId, out attackerEntity))
                {
                    return(false);
                }

                if (attackerEntity is IMyShipGrinder)
                {
                    player = MyAPIGateway.Players.GetPlayerControllingEntity(attackerEntity.GetTopMostParent());

                    if (player == null)
                    {
                        return(false);
                    }

                    return(CanModify(player, block));
                }

                // handTool.OwnerId is the identityId of the character.
                IMyEngineerToolBase handTool = attackerEntity as IMyEngineerToolBase;
                if (handTool != null)
                {
                    //if (MyAPIGateway.Entities.TryGetEntityById(handTool.OwnerId, out attackerEntity))
                    //{
                    //    player = MyAPIGateway.Players.GetPlayerControllingEntity(attackerEntity);
                    //    if (player != null)
                    //    {
                    //        return CanModify(player, block);
                    //    }
                    //}

                    // debatable if this code is any faster or more efficient.
                    var players = new List <IMyPlayer>();
                    MyAPIGateway.Players.GetPlayers(players, p => p != null);
                    player = players.FirstOrDefault(p => p.Character != null && p.Character.EntityId == handTool.OwnerId);
                    if (player != null)
                    {
                        return(CanModify(player, block));
                    }
                }
            }
            // we don't want players to destroy things in protection areas...
            return(false);
        }
        private void Entities_OnEntityAdd(IMyEntity entity)
        {
            IMyEngineerToolBase handTool = entity as IMyEngineerToolBase;

            if (handTool != null)
            {
                // when the entity is created, it is not finished (pos is 0, boundingbox does not exist, etc.)
                // therefore we need to wait a moment or two until we can build up the cache
                // but don't worry we won't allow it to damage sth. in that time (if it is even possible)
                _uninitializedHandTools.Add(handTool);
            }
        }
예제 #6
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);
     }
 }
예제 #7
0
 private 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);
     }
     catch (Exception scrap)
     {
         AiSessionCore.LogError("Damage.IsDoneByPlayer", new Exception("Check gun owner crashed", scrap));
         return(false);
     }
 }
        // this method is called twice when switching weapon, idk why
        private void Entities_OnEntityRemove(IMyEntity entity)
        {
            IMyEngineerToolBase tool = entity as IMyEngineerToolBase;

            if (tool == null || !_cache.ContainsValue(tool))
            {
                return;
            }

            var player = _cache.First(p => p.Value.EntityId == entity.EntityId).Key;

            if (_cache.ContainsKey(player))
            {
                _cache.Remove(player);
            }
        }
예제 #9
0
        private void CombatDamageHandler(object target, MyDamageInformation info)
        {
            if (info.Amount == 0)
            {
                return;
            }

            CombatDescription log = new CombatDescription
            {
                Damage    = info.Amount,
                Type      = info.Type.String,
                Timestamp = Tools.DateTime
            };

            if (target is IMySlimBlock)
            {
                IMySlimBlock slim = target as IMySlimBlock;
                log.Integrity         = slim.Integrity;
                log.VictimGridId      = slim.CubeGrid.EntityId;
                log.VictimGridBlockId = Tools.GetBlockId(slim.Position);
            }
            else if (target is IMyCharacter)
            {
                IMyCharacter character = target as IMyCharacter;

                // characters keep getting hit after death we dont want to log that
                if (character.Name == AlreadyLogged)
                {
                    return;
                }

                if (character.Integrity <= 0)
                {
                    character.Name = AlreadyLogged;
                }
                log.VictimEntityId = character.EntityId;
                log.Integrity      = character.Integrity;
            }
            else if (target is IMyFloatingObject)
            {
                IMyFloatingObject obj = (target as IMyFloatingObject);
                log.VictimEntityId = obj.EntityId;
                log.Integrity      = obj.Integrity;
            }
            else
            {
                ActivityCollector.Log.Error($"Unrecognised Victim {target.GetType()}");
            }


            IMyEntity entity = MyAPIGateway.Entities.GetEntityById(info.AttackerId);

            if (entity == null)
            {
            }
            else if (entity is IMyCubeBlock)
            {
                IMyCubeBlock cube = entity as IMyCubeBlock;

                log.AttackerGridId   = cube.CubeGrid.EntityId;
                log.AttackerEntityId = cube.EntityId;
            }
            else if (entity is IMyCharacter)
            {
                try
                {
                    IMyCharacter character = entity as IMyCharacter;

                    if (character.Name != null) // hacks continued
                    {
                        long missileId = -1;
                        long.TryParse(entity.Name, out missileId);
                        if (missileId != -1)
                        {
                            IMyEntity missileEntity = MyAPIGateway.Entities.GetEntityById(missileId);

                            if (missileEntity != null)
                            {
                                if (missileEntity is IMyCubeGrid)
                                {
                                    IMyCubeGrid grid = missileEntity as IMyCubeGrid;

                                    log.AttackerGridId = grid.EntityId;
                                }
                                else
                                {
                                    IMyCubeBlock cube = missileEntity as IMyCubeBlock;

                                    log.AttackerGridId      = cube.CubeGrid.EntityId;
                                    log.AttackerGridBlockId = Tools.GetBlockId(cube.Position);
                                }
                            }
                            else
                            {
                                ActivityCollector.Log.Error($"missiles parent grid was not found!");
                            }
                        }
                        else
                        {
                            ActivityCollector.Log.Error($"Entity of type {entity.GetType()} failed to parse id {entity.Name}");
                        }
                    }
                    else
                    {
                        log.AttackerEntityId = character.EntityId;
                    }
                }
                catch (Exception e)
                {
                    ActivityCollector.Log.Error(e);
                }
            }
            else if (entity is IMyGunBaseUser) // player tools
            {
                IMyGunBaseUser gun = entity as IMyGunBaseUser;

                log.AttackerEntityId = gun.Weapon.EntityId;
            }
            else if (entity is IMyEngineerToolBase)
            {
                IMyEngineerToolBase toolbase = entity as IMyEngineerToolBase;

                log.AttackerEntityId = toolbase.EntityId;
            }
            else if (entity is IMySlimBlock)
            {
                IMySlimBlock slim = entity as IMySlimBlock;

                log.AttackerGridId = slim.CubeGrid.EntityId;

                if (slim.FatBlock != null)
                {
                    log.AttackerEntityId = slim.FatBlock.EntityId;
                }
                else
                {
                }
            }
            else if (entity is IMyCubeGrid)
            {
                IMyCubeGrid grid = entity as IMyCubeGrid;

                log.AttackerGridId = grid.EntityId;
            }
            else if (entity is MyVoxelBase)
            {
                MyVoxelBase voxel = entity as MyVoxelBase;
                log.AttackerEntityId = entity.EntityId;
            }
            else if (entity.GetType().Name == "MyMissile")
            {
                long missileId = -1;
                long.TryParse(entity.Name, out missileId);
                if (missileId != -1)
                {
                    IMyEntity missileEntity = MyAPIGateway.Entities.GetEntityById(missileId);

                    if (missileEntity != null)
                    {
                        if (missileEntity is IMyCubeGrid)
                        {
                            IMyCubeGrid grid = missileEntity as IMyCubeGrid;
                            log.AttackerGridId = grid.EntityId;
                        }
                        else
                        {
                            IMyCubeBlock cube = missileEntity as IMyCubeBlock;
                            log.AttackerGridId   = cube.CubeGrid.EntityId;
                            log.AttackerEntityId = cube.EntityId;
                        }
                    }
                    else
                    {
                        ActivityCollector.Log.Error($"Missles parent grid was not found!");
                    }
                }
                else
                {
                    ActivityCollector.Log.Error($"Entity of type {entity.GetType()} failed to parse id {entity.Name}");
                }
            }
            else
            {
                ActivityCollector.Log.Error($"Unknown attacker entity of type: {entity.GetType()}");
            }

            SQLQueryData.WriteToDatabase(log);
        }