Don't crash on multiple Attack* traits

This is important for actors that use upgrades to switch between attack types.
This commit is contained in:
Pavel Penev
2016-02-13 18:46:42 +02:00
parent 65942ecfbf
commit 07947179cd
2 changed files with 48 additions and 17 deletions

View File

@@ -8,6 +8,7 @@
*/ */
#endregion #endregion
using System;
using System.Linq; using System.Linq;
using OpenRA.Activities; using OpenRA.Activities;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
@@ -18,8 +19,11 @@ namespace OpenRA.Mods.Common.Activities
/* non-turreted attack */ /* non-turreted attack */
public class Attack : Activity public class Attack : Activity
{ {
[Flags]
enum AttackStatus { UnableToAttack, NeedsToTurn, NeedsToMove, Attacking }
protected readonly Target Target; protected readonly Target Target;
readonly AttackBase attack; readonly AttackBase[] attackTraits;
readonly IMove move; readonly IMove move;
readonly IFacing facing; readonly IFacing facing;
readonly IPositionable positionable; readonly IPositionable positionable;
@@ -27,6 +31,9 @@ namespace OpenRA.Mods.Common.Activities
WDist minRange; WDist minRange;
WDist maxRange; WDist maxRange;
Activity turnActivity;
Activity moveActivity;
AttackStatus attackStatus = AttackStatus.UnableToAttack;
public Attack(Actor self, Target target, bool allowMovement, bool forceAttack) public Attack(Actor self, Target target, bool allowMovement, bool forceAttack)
{ {
@@ -34,7 +41,7 @@ namespace OpenRA.Mods.Common.Activities
this.forceAttack = forceAttack; this.forceAttack = forceAttack;
attack = self.Trait<AttackBase>(); attackTraits = self.TraitsImplementing<AttackBase>().ToArray();
facing = self.Trait<IFacing>(); facing = self.Trait<IFacing>();
positionable = self.Trait<IPositionable>(); positionable = self.Trait<IPositionable>();
@@ -43,9 +50,25 @@ namespace OpenRA.Mods.Common.Activities
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
var ret = InnerTick(self, attack); turnActivity = moveActivity = null;
attack.IsAttacking = ret == this; attackStatus = AttackStatus.UnableToAttack;
return ret;
foreach (var attack in attackTraits.Where(x => !x.IsTraitDisabled))
{
var activity = InnerTick(self, attack);
attack.IsAttacking = activity == this;
}
if (attackStatus.HasFlag(AttackStatus.Attacking))
return this;
if (attackStatus.HasFlag(AttackStatus.NeedsToTurn))
return turnActivity;
if (attackStatus.HasFlag(AttackStatus.NeedsToMove))
return moveActivity;
return NextActivity;
} }
protected virtual Activity InnerTick(Actor self, AttackBase attack) protected virtual Activity InnerTick(Actor self, AttackBase attack)
@@ -81,13 +104,21 @@ namespace OpenRA.Mods.Common.Activities
// Try to move within range, drop the target otherwise // Try to move within range, drop the target otherwise
if (move == null) if (move == null)
return NextActivity; return NextActivity;
return ActivityUtils.SequenceActivities(move.MoveWithinRange(Target, minRange, maxRange), this);
attackStatus |= AttackStatus.NeedsToMove;
moveActivity = ActivityUtils.SequenceActivities(move.MoveWithinRange(Target, minRange, maxRange), this);
return NextActivity;
} }
var desiredFacing = (Target.CenterPosition - self.CenterPosition).Yaw.Facing; var desiredFacing = (Target.CenterPosition - self.CenterPosition).Yaw.Facing;
if (facing.Facing != desiredFacing) if (facing.Facing != desiredFacing)
return ActivityUtils.SequenceActivities(new Turn(self, desiredFacing), this); {
attackStatus |= AttackStatus.NeedsToTurn;
turnActivity = ActivityUtils.SequenceActivities(new Turn(self, desiredFacing), this);
return NextActivity;
}
attackStatus |= AttackStatus.Attacking;
attack.DoAttack(self, Target, armaments); attack.DoAttack(self, Target, armaments);
return this; return this;

View File

@@ -27,17 +27,15 @@ namespace OpenRA.Mods.Common.Traits
public class CombatDebugOverlay : IPostRender, INotifyDamage, INotifyCreated public class CombatDebugOverlay : IPostRender, INotifyDamage, INotifyCreated
{ {
readonly DeveloperMode devMode; readonly DeveloperMode devMode;
readonly HealthInfo healthInfo; readonly HealthInfo healthInfo;
readonly Lazy<BodyOrientation> coords;
IBlocksProjectiles[] allBlockers; IBlocksProjectiles[] allBlockers;
Lazy<AttackBase> attack;
Lazy<BodyOrientation> coords;
public CombatDebugOverlay(Actor self) public CombatDebugOverlay(Actor self)
{ {
healthInfo = self.Info.TraitInfoOrDefault<HealthInfo>(); healthInfo = self.Info.TraitInfoOrDefault<HealthInfo>();
attack = Exts.Lazy(() => self.TraitOrDefault<AttackBase>()); coords = Exts.Lazy(self.Trait<BodyOrientation>);
coords = Exts.Lazy(() => self.Trait<BodyOrientation>());
var localPlayer = self.World.LocalPlayer; var localPlayer = self.World.LocalPlayer;
devMode = localPlayer != null ? localPlayer.PlayerActor.Trait<DeveloperMode>() : null; devMode = localPlayer != null ? localPlayer.PlayerActor.Trait<DeveloperMode>() : null;
@@ -71,14 +69,16 @@ namespace OpenRA.Mods.Common.Traits
TargetLineRenderable.DrawTargetMarker(wr, hc, hb); TargetLineRenderable.DrawTargetMarker(wr, hc, hb);
} }
// No armaments to draw foreach (var attack in self.TraitsImplementing<AttackBase>().Where(x => !x.IsTraitDisabled))
if (attack.Value == null) DrawArmaments(self, attack, wr, wcr, iz);
return; }
void DrawArmaments(Actor self, AttackBase attack, WorldRenderer wr, RgbaColorRenderer wcr, float iz)
{
var c = Color.White; var c = Color.White;
// Fire ports on garrisonable structures // Fire ports on garrisonable structures
var garrison = attack.Value as AttackGarrisoned; var garrison = attack as AttackGarrisoned;
if (garrison != null) if (garrison != null)
{ {
var bodyOrientation = coords.Value.QuantizeOrientation(self, self.Orientation); var bodyOrientation = coords.Value.QuantizeOrientation(self, self.Orientation);
@@ -98,7 +98,7 @@ namespace OpenRA.Mods.Common.Traits
return; return;
} }
foreach (var a in attack.Value.Armaments) foreach (var a in attack.Armaments)
{ {
foreach (var b in a.Barrels) foreach (var b in a.Barrels)
{ {