Пример #1
0
		/// <summary>
		/// Adds an effect to the grid.
		/// </summary>
		/// <param name="duration">The length of time the effect will last for.</param>
		/// <param name="strength">The amount of effect available.</param>
		/// <param name="effectOwner">The player that will be allowed to control the block. By default, no one can access.</param>
		/// <returns>Strength remaining after effect is applied</returns>
		protected int AddEffect(TimeSpan duration, int strength, long effectOwner = long.MinValue)
		{
			if (strength < MinCost)
			{
				m_logger.debugLog("strength: " + strength + ", below minimum: " + MinCost, "AddEffect()");
				return strength;
			}

			int applied = 0;
			foreach (MyObjectBuilderType type in m_affects)
			{
				var blockGroup = m_cache.GetBlocksOfType(type);
				if (blockGroup != null)
					foreach (int i in Enumerable.Range(0, blockGroup.Count).OrderBy(x => Globals.Random.Next()))
					{
						IMyCubeBlock block = blockGroup[i];

						if (!block.IsWorking || m_allAffected.Contains(block))
						{
							m_logger.debugLog("cannot disrupt: " + block, "AddEffect()");
							continue;
						}
						m_logger.debugLog("disrupting: " + block, "AddEffect()");
						int change = StartEffect(block, strength);
						if (change != 0)
						{
							strength -= change;
							applied += change;
							MyCubeBlock cubeBlock = block as MyCubeBlock;
							MyIDModule idMod = new MyIDModule() { Owner = cubeBlock.IDModule.Owner, ShareMode = cubeBlock.IDModule.ShareMode };
							m_affected.Add(block, idMod);
							m_allAffected.Add(block);

							block.SetDamageEffect(true);
							cubeBlock.ChangeOwner(effectOwner, MyOwnershipShareModeEnum.Faction);

							if (strength < MinCost)
								goto FinishedBlocks;
						}
					}
				else
					m_logger.debugLog("no blocks of type: " + type, "AddEffect()");
			}
FinishedBlocks:
			if (applied != 0)
			{
				m_logger.debugLog("Added new effect, strength: " + applied, "AddEffect()");
				m_effects.Add(DateTime.UtcNow.Add(duration), applied);
				SetNextExpire();
			}
			return strength;
		}
Пример #2
0
        protected int AddEffect(TimeSpan duration, int strength, long effectOwner)
        {
            int applied = 0;
            foreach (MyObjectBuilderType type in m_affects)
            {
                var blockGroup = m_cache.GetBlocksOfType(type);
                if (blockGroup != null)
                    foreach (IMyCubeBlock block in blockGroup)
                    {
                        if (!block.IsWorking || m_affected.ContainsKey(block))
                        {
                            m_logger.debugLog("cannot disrupt: " + block, "AddEffect()");
                            continue;
                        }
                        m_logger.debugLog("disrupting: " + block, "AddEffect()");
                        int change = StartEffect(block, strength);
                        if (change != 0)
                        {
                            strength -= change;
                            applied += change;
                            MyCubeBlock cubeBlock = block as MyCubeBlock;
                            MyIDModule idMod = new MyIDModule() { Owner = cubeBlock.IDModule.Owner, ShareMode = cubeBlock.IDModule.ShareMode };
                            m_affected.Add(block, idMod);

                            block.SetDamageEffect(true);
                            cubeBlock.ChangeOwner(effectOwner, MyOwnershipShareModeEnum.Faction);

                            if (strength < 1)
                                goto FinishedBlocks;
                        }
                    }
                else
                    m_logger.debugLog("no blocks of type: " + type, "AddEffect()");
            }
            FinishedBlocks:
            if (applied != 0)
            {
                m_logger.debugLog("Added new effect, strength: " + applied, "AddEffect()");
                m_effects.Add(DateTime.UtcNow.Add(duration), applied);
                SetNextExpire();
            }
            return strength;
        }
Пример #3
0
        public static MyDetectedEntityInfo Create(MyEntity entity, long sensorOwner, Vector3D?hitPosition = null)
        {
            if (entity == null)
            {
                return(new MyDetectedEntityInfo());
            }

            MatrixD orientation = MatrixD.Zero;
            Vector3 velocity    = Vector3D.Zero;
            int     timeStamp   = MySandboxGame.TotalGamePlayTimeInMilliseconds;
            MyDetectedEntityType             type;
            BoundingBoxD                     boundingBox = entity.PositionComp.WorldAABB;
            MyRelationsBetweenPlayerAndBlock relationship;
            string name;

            if (entity.Physics != null)
            {
                orientation = entity.Physics.GetWorldMatrix().GetOrientation();
                velocity    = entity.Physics.LinearVelocity;
            }

            //using GetTopMostParent in case we are looking at a MyCubeBlock; we want the grid the block is on
            var grid = entity.GetTopMostParent() as MyCubeGrid;

            if (grid != null)
            {
                if (grid.GridSizeEnum == MyCubeSize.Small)
                {
                    type = MyDetectedEntityType.SmallGrid;
                }
                else
                {
                    type = MyDetectedEntityType.LargeGrid;
                }

                if (grid.BigOwners.Count == 0)
                {
                    relationship = MyRelationsBetweenPlayerAndBlock.NoOwnership;
                }
                else
                {
                    relationship = MyIDModule.GetRelation(sensorOwner, grid.BigOwners[0], MyOwnershipShareModeEnum.Faction);
                }

                if (relationship == MyRelationsBetweenPlayerAndBlock.Owner || relationship == MyRelationsBetweenPlayerAndBlock.FactionShare)
                {
                    name = grid.DisplayName;
                }
                else
                {
                    if (grid.GridSizeEnum == MyCubeSize.Small)
                    {
                        name = MyTexts.GetString(MySpaceTexts.DetectedEntity_SmallGrid);
                    }
                    else
                    {
                        name = MyTexts.GetString(MySpaceTexts.DetectedEntity_LargeGrid);
                    }
                }

                return(new MyDetectedEntityInfo(grid.EntityId, name, type, hitPosition, orientation, velocity, relationship, boundingBox, timeStamp));
            }

            var character = entity as MyCharacter;

            if (character != null)
            {
                if (character.IsPlayer)
                {
                    type = MyDetectedEntityType.CharacterHuman;
                }
                else
                {
                    type = MyDetectedEntityType.CharacterOther;
                }

                relationship = MyIDModule.GetRelation(sensorOwner, character.GetPlayerIdentityId(), MyOwnershipShareModeEnum.Faction);

                if (relationship == MyRelationsBetweenPlayerAndBlock.Owner || relationship == MyRelationsBetweenPlayerAndBlock.FactionShare)
                {
                    name = character.DisplayNameText;
                }
                else
                {
                    if (character.IsPlayer)
                    {
                        name = MyTexts.GetString(MySpaceTexts.DetectedEntity_CharacterHuman);
                    }
                    else
                    {
                        name = MyTexts.GetString(MySpaceTexts.DetectedEntity_CharacterOther);
                    }
                }

                BoundingBoxD bound = character.Model.BoundingBox.Transform(character.WorldMatrix);

                return(new MyDetectedEntityInfo(entity.EntityId, name, type, hitPosition, orientation, velocity, relationship, bound, timeStamp));
            }

            relationship = MyRelationsBetweenPlayerAndBlock.Neutral;

            var floating = entity as MyFloatingObject;

            if (floating != null)
            {
                type = MyDetectedEntityType.FloatingObject;
                name = floating.Item.Content.SubtypeName;
                return(new MyDetectedEntityInfo(entity.EntityId, name, type, hitPosition, orientation, velocity, relationship, boundingBox, timeStamp));
            }

            var backpack = entity as MyInventoryBagEntity;

            if (backpack != null)
            {
                type = MyDetectedEntityType.FloatingObject;
                name = backpack.DisplayName;
                return(new MyDetectedEntityInfo(entity.EntityId, name, type, hitPosition, orientation, velocity, relationship, boundingBox, timeStamp));
            }

            var planet = entity as MyPlanet;

            if (planet != null)
            {
                type = MyDetectedEntityType.Planet;
                name = MyTexts.GetString(MySpaceTexts.DetectedEntity_Planet);
                //shrink the planet's bounding box to only encompass terrain
                boundingBox = BoundingBoxD.CreateFromSphere(new BoundingSphereD(planet.PositionComp.GetPosition(), planet.MaximumRadius));
                return(new MyDetectedEntityInfo(entity.EntityId, name, type, hitPosition, orientation, velocity, relationship, boundingBox, timeStamp));
            }

            var voxelPhysics = entity as MyVoxelPhysics;

            if (voxelPhysics != null)
            {
                type = MyDetectedEntityType.Planet;
                name = MyTexts.GetString(MySpaceTexts.DetectedEntity_Planet);
                //shrink the planet's bounding box to only encompass terrain
                boundingBox = BoundingBoxD.CreateFromSphere(new BoundingSphereD(voxelPhysics.Parent.PositionComp.GetPosition(), voxelPhysics.Parent.MaximumRadius));
                return(new MyDetectedEntityInfo(entity.EntityId, name, type, hitPosition, orientation, velocity, relationship, boundingBox, timeStamp));
            }

            var voxel = entity as MyVoxelMap;

            if (voxel != null)
            {
                type = MyDetectedEntityType.Asteroid;
                name = MyTexts.GetString(MySpaceTexts.DetectedEntity_Asteroid);
                return(new MyDetectedEntityInfo(entity.EntityId, name, type, hitPosition, orientation, velocity, relationship, boundingBox, timeStamp));
            }

            var meteor = entity as MyMeteor;

            if (meteor != null)
            {
                type = MyDetectedEntityType.Meteor;
                name = MyTexts.GetString(MySpaceTexts.DetectedEntity_Meteor);
                return(new MyDetectedEntityInfo(entity.EntityId, name, type, hitPosition, orientation, velocity, relationship, boundingBox, timeStamp));
            }

            var missile = entity as MyMissile;

            if (missile != null)
            {
                type = MyDetectedEntityType.Missile;
                name = entity.DisplayName;
                return(new MyDetectedEntityInfo(entity.EntityId, name, type, hitPosition, orientation, velocity, relationship, boundingBox, timeStamp));
            }

            //error case
            return(new MyDetectedEntityInfo(0, String.Empty, MyDetectedEntityType.Unknown, null, new MatrixD(), new Vector3(), MyRelationsBetweenPlayerAndBlock.NoOwnership, new BoundingBoxD(), MySandboxGame.TotalGamePlayTimeInMilliseconds));
        }
Пример #4
0
        /// <summary>
        /// Adds a disruption effect to a grid.
        /// </summary>
        /// <param name="grid">Grid that will be disrupted</param>
        /// <param name="duration">Duration of disruption</param>
        /// <param name="strength">Strength of disruption (in hackyness)</param>
        /// <param name="effectOwner">The owner of the disruption.</param>
        public void Start(IMyCubeGrid grid, TimeSpan duration, ref float strength, long effectOwner)
        {
            this.m_logger = new Logger(GetType().Name, () => grid.DisplayName);

            if (strength < MinCost)
            {
                m_logger.debugLog("strength: " + strength + ", below minimum: " + MinCost);
                return;
            }

            CubeGridCache cache = CubeGridCache.GetFor(grid);
            float applied = 0;
            if (!EffectOwnerCanAccess)
                effectOwner = long.MinValue;
            m_effectOwner = effectOwner;
            foreach (MyObjectBuilderType type in BlocksAffected)
            {
                var blockGroup = cache.GetBlocksOfType(type);
                if (blockGroup != null && blockGroup.Count != 0)
                {
                    foreach (IMyCubeBlock block in blockGroup.OrderBy(OrderBy))
                    {
                        if (!block.IsWorking || m_allAffected.Contains(block))
                        {
                            m_logger.debugLog("cannot disrupt: " + block);
                            continue;
                        }
                        float cost = BlockCost(block);
                        if (cost > strength)
                        {
                            m_logger.debugLog("cannot disrupt block: " + block + ", cost: " + cost + " is greater than strength available: " + strength);
                            continue;
                        }

                        StartEffect(block);
                        m_logger.debugLog("disrupting: " + block + ", cost: " + cost + ", remaining strength: " + strength);
                        strength -= cost;
                        applied += cost;
                        MyCubeBlock cubeBlock = block as MyCubeBlock;
                        MyIDModule idMod = new MyIDModule() { Owner = cubeBlock.IDModule.Owner, ShareMode = cubeBlock.IDModule.ShareMode };
                        m_affected.Add(block, idMod);
                        m_allAffected.Add(block);

                        block.SetDamageEffect(true);
                        cubeBlock.ChangeOwner(effectOwner, MyOwnershipShareModeEnum.Faction);

                        if (strength < MinCost)
                            goto FinishedBlocks;
                    }
                }
                else
                    m_logger.debugLog("no blocks of type: " + type);
            }
            FinishedBlocks:
            if (m_affected.Count != 0)
            {
                m_logger.debugLog("Added new effect, strength: " + applied);
                m_expire = Globals.ElapsedTime.Add(duration);

                UpdateManager.Register(UpdateFrequency, UpdateEffect); // don't unregister on grid close, blocks can still be valid
                AllDisruptions.Add(this);
            }
        }
Пример #5
0
        private MyOwnershipShareModeEnum GetShare()
        {
            MyIDModule module = this.TryGetEntityIdModule();

            return((module != null) ? module.ShareMode : MyOwnershipShareModeEnum.None);
        }
Пример #6
0
        public static MyDetectedEntityInfo Create(MyEntity entity, long sensorOwner, Vector3D?hitPosition = new Vector3D?())
        {
            MyDetectedEntityType             type;
            MyRelationsBetweenPlayerAndBlock neutral;
            string displayName;

            if (entity == null)
            {
                return(new MyDetectedEntityInfo());
            }
            MatrixD      zero     = MatrixD.Zero;
            Vector3      velocity = (Vector3)Vector3D.Zero;
            int          totalGamePlayTimeInMilliseconds = MySandboxGame.TotalGamePlayTimeInMilliseconds;
            BoundingBoxD worldAABB = entity.PositionComp.WorldAABB;

            if (entity.Physics != null)
            {
                zero     = entity.Physics.GetWorldMatrix().GetOrientation();
                velocity = entity.Physics.LinearVelocity;
            }
            MyCubeGrid topMostParent = entity.GetTopMostParent(null) as MyCubeGrid;

            if (topMostParent != null)
            {
                type    = (topMostParent.GridSizeEnum != MyCubeSize.Small) ? MyDetectedEntityType.LargeGrid : MyDetectedEntityType.SmallGrid;
                neutral = (topMostParent.BigOwners.Count != 0) ? MyIDModule.GetRelation(sensorOwner, topMostParent.BigOwners[0], MyOwnershipShareModeEnum.Faction, MyRelationsBetweenPlayerAndBlock.Enemies, MyRelationsBetweenFactions.Enemies, MyRelationsBetweenPlayerAndBlock.FactionShare) : MyRelationsBetweenPlayerAndBlock.NoOwnership;
                if ((neutral == MyRelationsBetweenPlayerAndBlock.Owner) || (neutral == MyRelationsBetweenPlayerAndBlock.FactionShare))
                {
                    displayName = topMostParent.DisplayName;
                }
                else
                {
                    displayName = (topMostParent.GridSizeEnum != MyCubeSize.Small) ? MyTexts.GetString(MySpaceTexts.DetectedEntity_LargeGrid) : MyTexts.GetString(MySpaceTexts.DetectedEntity_SmallGrid);
                }
                zero = topMostParent.WorldMatrix.GetOrientation();
                return(new MyDetectedEntityInfo(topMostParent.EntityId, displayName, type, hitPosition, zero, topMostParent.Physics.LinearVelocity, neutral, worldAABB, (long)totalGamePlayTimeInMilliseconds));
            }
            MyCharacter character = entity as MyCharacter;

            if (character != null)
            {
                type    = !character.IsPlayer ? MyDetectedEntityType.CharacterOther : MyDetectedEntityType.CharacterHuman;
                neutral = MyIDModule.GetRelation(sensorOwner, character.GetPlayerIdentityId(), MyOwnershipShareModeEnum.Faction, MyRelationsBetweenPlayerAndBlock.Enemies, MyRelationsBetweenFactions.Enemies, MyRelationsBetweenPlayerAndBlock.FactionShare);
                if ((neutral == MyRelationsBetweenPlayerAndBlock.Owner) || (neutral == MyRelationsBetweenPlayerAndBlock.FactionShare))
                {
                    displayName = character.DisplayNameText;
                }
                else
                {
                    displayName = !character.IsPlayer ? MyTexts.GetString(MySpaceTexts.DetectedEntity_CharacterOther) : MyTexts.GetString(MySpaceTexts.DetectedEntity_CharacterHuman);
                }
                return(new MyDetectedEntityInfo(entity.EntityId, displayName, type, hitPosition, zero, velocity, neutral, character.Model.BoundingBox.Transform(character.WorldMatrix), (long)totalGamePlayTimeInMilliseconds));
            }
            neutral = MyRelationsBetweenPlayerAndBlock.Neutral;
            MyFloatingObject obj2 = entity as MyFloatingObject;

            if (obj2 != null)
            {
                displayName = obj2.Item.Content.SubtypeName;
                return(new MyDetectedEntityInfo(entity.EntityId, displayName, MyDetectedEntityType.FloatingObject, hitPosition, zero, velocity, neutral, worldAABB, (long)totalGamePlayTimeInMilliseconds));
            }
            MyInventoryBagEntity entity2 = entity as MyInventoryBagEntity;

            if (entity2 != null)
            {
                displayName = entity2.DisplayName;
                return(new MyDetectedEntityInfo(entity.EntityId, displayName, MyDetectedEntityType.FloatingObject, hitPosition, zero, velocity, neutral, worldAABB, (long)totalGamePlayTimeInMilliseconds));
            }
            MyPlanet planet = entity as MyPlanet;

            if (planet != null)
            {
                displayName = MyTexts.GetString(MySpaceTexts.DetectedEntity_Planet);
                return(new MyDetectedEntityInfo(entity.EntityId, displayName, MyDetectedEntityType.Planet, hitPosition, zero, velocity, neutral, BoundingBoxD.CreateFromSphere(new BoundingSphereD(planet.PositionComp.GetPosition(), (double)planet.MaximumRadius)), (long)totalGamePlayTimeInMilliseconds));
            }
            MyVoxelPhysics physics = entity as MyVoxelPhysics;

            if (physics != null)
            {
                displayName = MyTexts.GetString(MySpaceTexts.DetectedEntity_Planet);
                return(new MyDetectedEntityInfo(entity.EntityId, displayName, MyDetectedEntityType.Planet, hitPosition, zero, velocity, neutral, BoundingBoxD.CreateFromSphere(new BoundingSphereD(physics.Parent.PositionComp.GetPosition(), (double)physics.Parent.MaximumRadius)), (long)totalGamePlayTimeInMilliseconds));
            }
            if (entity is MyVoxelMap)
            {
                displayName = MyTexts.GetString(MySpaceTexts.DetectedEntity_Asteroid);
                return(new MyDetectedEntityInfo(entity.EntityId, displayName, MyDetectedEntityType.Asteroid, hitPosition, zero, velocity, neutral, worldAABB, (long)totalGamePlayTimeInMilliseconds));
            }
            if (entity is MyMeteor)
            {
                displayName = MyTexts.GetString(MySpaceTexts.DetectedEntity_Meteor);
                return(new MyDetectedEntityInfo(entity.EntityId, displayName, MyDetectedEntityType.Meteor, hitPosition, zero, velocity, neutral, worldAABB, (long)totalGamePlayTimeInMilliseconds));
            }
            if (entity is MyMissile)
            {
                return(new MyDetectedEntityInfo(entity.EntityId, entity.DisplayName, MyDetectedEntityType.Missile, hitPosition, zero, velocity, neutral, worldAABB, (long)totalGamePlayTimeInMilliseconds));
            }
            Vector3D?nullable    = null;
            MatrixD  orientation = new MatrixD();
            Vector3  vector2     = new Vector3();

            return(new MyDetectedEntityInfo(0L, string.Empty, MyDetectedEntityType.Unknown, nullable, orientation, vector2, MyRelationsBetweenPlayerAndBlock.NoOwnership, new BoundingBoxD(), (long)MySandboxGame.TotalGamePlayTimeInMilliseconds));
        }