From 7e1f29d4c52551576e4a30aacb3b75f188089c3e Mon Sep 17 00:00:00 2001 From: reaperrr Date: Sat, 5 Sep 2015 15:01:35 +0200 Subject: [PATCH 1/2] Made BlocksProjectiles upgradable --- OpenRA.Game/Traits/TraitsInterfaces.cs | 2 -- OpenRA.Mods.Common/Effects/Bullet.cs | 9 ++++++++- OpenRA.Mods.Common/Effects/Missile.cs | 8 +++++++- OpenRA.Mods.Common/Traits/BlocksProjectiles.cs | 12 ++++++++++-- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/OpenRA.Game/Traits/TraitsInterfaces.cs b/OpenRA.Game/Traits/TraitsInterfaces.cs index 6e2278eb8b..96d4a0a8c5 100644 --- a/OpenRA.Game/Traits/TraitsInterfaces.cs +++ b/OpenRA.Game/Traits/TraitsInterfaces.cs @@ -299,8 +299,6 @@ namespace OpenRA.Traits public interface INotifyBecomingIdle { void OnBecomingIdle(Actor self); } public interface INotifyIdle { void TickIdle(Actor self); } - public interface IBlocksProjectilesInfo : ITraitInfo { } - public interface IBlocksProjectiles { } public interface IRenderInfantrySequenceModifier { bool IsModifyingSequence { get; } diff --git a/OpenRA.Mods.Common/Effects/Bullet.cs b/OpenRA.Mods.Common/Effects/Bullet.cs index 4adc33154f..aa8a3be3a3 100644 --- a/OpenRA.Mods.Common/Effects/Bullet.cs +++ b/OpenRA.Mods.Common/Effects/Bullet.cs @@ -16,6 +16,7 @@ using OpenRA.Effects; using OpenRA.GameRules; using OpenRA.Graphics; using OpenRA.Mods.Common.Graphics; +using OpenRA.Mods.Common.Traits; using OpenRA.Traits; namespace OpenRA.Mods.Common.Effects @@ -171,7 +172,7 @@ namespace OpenRA.Mods.Common.Effects var shouldExplode = height.Length <= 0 // Hit the ground || ticks++ >= length // Flight length reached/exceeded - || (info.Blockable && world.ActorMap.GetUnitsAt(cell).Any(a => a.Info.HasTraitInfo())); // Hit a wall or other blocking obstacle + || BlockedByActor(world, pos); // Hit a wall or other blocking obstacle if (shouldExplode) Explode(world); @@ -211,5 +212,11 @@ namespace OpenRA.Mods.Common.Effects args.Weapon.Impact(Target.FromPos(pos), args.SourceActor, args.DamageModifiers); } + + bool BlockedByActor(World world, WPos pos) + { + return info.Blockable && world.ActorMap.GetUnitsAt(world.Map.CellContaining(pos)) + .Any(a => a.TraitsImplementing().Any(Exts.IsTraitEnabled)); + } } } diff --git a/OpenRA.Mods.Common/Effects/Missile.cs b/OpenRA.Mods.Common/Effects/Missile.cs index 8740be76d1..1695636492 100644 --- a/OpenRA.Mods.Common/Effects/Missile.cs +++ b/OpenRA.Mods.Common/Effects/Missile.cs @@ -201,7 +201,7 @@ namespace OpenRA.Mods.Common.Effects var shouldExplode = (pos.Z < 0) // Hit the ground || (dist.LengthSquared < info.CloseEnough.LengthSquared) // Within range || (info.RangeLimit != 0 && ticks > info.RangeLimit) // Ran out of fuel - || (info.Blockable && world.ActorMap.GetUnitsAt(cell).Any(a => a.Info.HasTraitInfo())) // Hit a wall or other blocking obstacle + || BlockedByActor(world, pos) // Hit a wall or other blocking obstacle || !world.Map.Contains(cell) // This also avoids an IndexOutOfRangeException in GetTerrainInfo below. || (!string.IsNullOrEmpty(info.BoundToTerrainType) && world.Map.GetTerrainInfo(cell).Type != info.BoundToTerrainType); // Hit incompatible terrain @@ -223,6 +223,12 @@ namespace OpenRA.Mods.Common.Effects args.Weapon.Impact(Target.FromPos(pos), args.SourceActor, args.DamageModifiers); } + bool BlockedByActor(World world, WPos pos) + { + return info.Blockable && world.ActorMap.GetUnitsAt(world.Map.CellContaining(pos)) + .Any(a => a.TraitsImplementing().Any(Exts.IsTraitEnabled)); + } + public IEnumerable Render(WorldRenderer wr) { if (info.ContrailLength > 0) diff --git a/OpenRA.Mods.Common/Traits/BlocksProjectiles.cs b/OpenRA.Mods.Common/Traits/BlocksProjectiles.cs index c41398abd6..ac495d3179 100644 --- a/OpenRA.Mods.Common/Traits/BlocksProjectiles.cs +++ b/OpenRA.Mods.Common/Traits/BlocksProjectiles.cs @@ -15,6 +15,14 @@ namespace OpenRA.Mods.Common.Traits { // TODO: Add functionality like a customizable Height that is compared to projectile altitude [Desc("This actor blocks bullets and missiles with 'Blockable' property.")] - public class BlocksProjectilesInfo : TraitInfo, IBlocksProjectilesInfo { } - public class BlocksProjectiles : IBlocksProjectiles { } + public class BlocksProjectilesInfo : UpgradableTraitInfo + { + public override object Create(ActorInitializer init) { return new BlocksProjectiles(init.Self, this); } + } + + public class BlocksProjectiles : UpgradableTrait + { + public BlocksProjectiles(Actor self, BlocksProjectilesInfo info) + : base(info) { } + } } From 4cf27fc207a68f15405f648fa21cff806368b92b Mon Sep 17 00:00:00 2001 From: reaperrr Date: Wed, 30 Sep 2015 22:26:28 +0200 Subject: [PATCH 2/2] Replace duplicate BlockedByActor with BlocksProjectiles.AnyBlockingActorAt --- OpenRA.Mods.Common/Effects/Bullet.cs | 14 ++------------ OpenRA.Mods.Common/Effects/Missile.cs | 12 +++--------- OpenRA.Mods.Common/Traits/BlocksProjectiles.cs | 6 ++++++ 3 files changed, 11 insertions(+), 21 deletions(-) diff --git a/OpenRA.Mods.Common/Effects/Bullet.cs b/OpenRA.Mods.Common/Effects/Bullet.cs index aa8a3be3a3..8953db64c6 100644 --- a/OpenRA.Mods.Common/Effects/Bullet.cs +++ b/OpenRA.Mods.Common/Effects/Bullet.cs @@ -167,12 +167,8 @@ namespace OpenRA.Mods.Common.Effects if (info.ContrailLength > 0) contrail.Update(pos); - var cell = world.Map.CellContaining(pos); - var height = world.Map.DistanceAboveTerrain(pos); - - var shouldExplode = height.Length <= 0 // Hit the ground - || ticks++ >= length // Flight length reached/exceeded - || BlockedByActor(world, pos); // Hit a wall or other blocking obstacle + var shouldExplode = ticks++ >= length // Flight length reached/exceeded + || (info.Blockable && BlocksProjectiles.AnyBlockingActorAt(world, pos)); // Hit a wall or other blocking obstacle if (shouldExplode) Explode(world); @@ -212,11 +208,5 @@ namespace OpenRA.Mods.Common.Effects args.Weapon.Impact(Target.FromPos(pos), args.SourceActor, args.DamageModifiers); } - - bool BlockedByActor(World world, WPos pos) - { - return info.Blockable && world.ActorMap.GetUnitsAt(world.Map.CellContaining(pos)) - .Any(a => a.TraitsImplementing().Any(Exts.IsTraitEnabled)); - } } } diff --git a/OpenRA.Mods.Common/Effects/Missile.cs b/OpenRA.Mods.Common/Effects/Missile.cs index 1695636492..c82d959a6b 100644 --- a/OpenRA.Mods.Common/Effects/Missile.cs +++ b/OpenRA.Mods.Common/Effects/Missile.cs @@ -20,7 +20,7 @@ using OpenRA.Traits; namespace OpenRA.Mods.Common.Effects { - class MissileInfo : IProjectileInfo + public class MissileInfo : IProjectileInfo { public readonly string Image = null; [SequenceReference("Image")] public readonly string Sequence = "idle"; @@ -81,7 +81,7 @@ namespace OpenRA.Mods.Common.Effects public IEffect Create(ProjectileArgs args) { return new Missile(this, args); } } - class Missile : IEffect, ISync + public class Missile : IEffect, ISync { readonly MissileInfo info; readonly ProjectileArgs args; @@ -201,7 +201,7 @@ namespace OpenRA.Mods.Common.Effects var shouldExplode = (pos.Z < 0) // Hit the ground || (dist.LengthSquared < info.CloseEnough.LengthSquared) // Within range || (info.RangeLimit != 0 && ticks > info.RangeLimit) // Ran out of fuel - || BlockedByActor(world, pos) // Hit a wall or other blocking obstacle + || (info.Blockable && BlocksProjectiles.AnyBlockingActorAt(world, pos)) // Hit a wall or other blocking obstacle || !world.Map.Contains(cell) // This also avoids an IndexOutOfRangeException in GetTerrainInfo below. || (!string.IsNullOrEmpty(info.BoundToTerrainType) && world.Map.GetTerrainInfo(cell).Type != info.BoundToTerrainType); // Hit incompatible terrain @@ -223,12 +223,6 @@ namespace OpenRA.Mods.Common.Effects args.Weapon.Impact(Target.FromPos(pos), args.SourceActor, args.DamageModifiers); } - bool BlockedByActor(World world, WPos pos) - { - return info.Blockable && world.ActorMap.GetUnitsAt(world.Map.CellContaining(pos)) - .Any(a => a.TraitsImplementing().Any(Exts.IsTraitEnabled)); - } - public IEnumerable Render(WorldRenderer wr) { if (info.ContrailLength > 0) diff --git a/OpenRA.Mods.Common/Traits/BlocksProjectiles.cs b/OpenRA.Mods.Common/Traits/BlocksProjectiles.cs index ac495d3179..249b2e8ea7 100644 --- a/OpenRA.Mods.Common/Traits/BlocksProjectiles.cs +++ b/OpenRA.Mods.Common/Traits/BlocksProjectiles.cs @@ -24,5 +24,11 @@ namespace OpenRA.Mods.Common.Traits { public BlocksProjectiles(Actor self, BlocksProjectilesInfo info) : base(info) { } + + public static bool AnyBlockingActorAt(World world, WPos pos) + { + return world.ActorMap.GetUnitsAt(world.Map.CellContaining(pos)) + .Any(a => a.TraitsImplementing().Any(Exts.IsTraitEnabled)); + } } }