Merge pull request #7123 from pchote/style-cleanup

Clean up some code style issues.
This commit is contained in:
Oliver Brakmann
2014-12-14 00:06:52 +01:00
6 changed files with 50 additions and 46 deletions

View File

@@ -17,9 +17,10 @@ namespace OpenRA.Mods.D2k
class AttackSwallowInfo : AttackFrontalInfo class AttackSwallowInfo : AttackFrontalInfo
{ {
[Desc("The number of ticks it takes to return underground.")] [Desc("The number of ticks it takes to return underground.")]
public int ReturnTime = 60; public readonly int ReturnTime = 60;
[Desc("The number of ticks it takes to get in place under the target to attack.")] [Desc("The number of ticks it takes to get in place under the target to attack.")]
public int AttackTime = 30; public readonly int AttackTime = 30;
public readonly string WormAttackNotification = "WormAttack"; public readonly string WormAttackNotification = "WormAttack";
@@ -28,13 +29,8 @@ namespace OpenRA.Mods.D2k
class AttackSwallow : AttackFrontal class AttackSwallow : AttackFrontal
{ {
new public readonly AttackSwallowInfo Info;
public AttackSwallow(Actor self, AttackSwallowInfo info) public AttackSwallow(Actor self, AttackSwallowInfo info)
: base(self, info) : base(self, info) { }
{
Info = info;
}
public override void DoAttack(Actor self, Target target) public override void DoAttack(Actor self, Target target)
{ {

View File

@@ -30,6 +30,7 @@ namespace OpenRA.Mods.D2k
readonly RenderUnit renderUnit; readonly RenderUnit renderUnit;
readonly RadarPings radarPings; readonly RadarPings radarPings;
readonly AttackSwallow swallow; readonly AttackSwallow swallow;
readonly AttackSwallowInfo swallowInfo;
readonly IPositionable positionable; readonly IPositionable positionable;
int countdown; int countdown;
@@ -41,9 +42,10 @@ namespace OpenRA.Mods.D2k
this.weapon = weapon; this.weapon = weapon;
positionable = self.Trait<Mobile>(); positionable = self.Trait<Mobile>();
swallow = self.Trait<AttackSwallow>(); swallow = self.Trait<AttackSwallow>();
swallowInfo = (AttackSwallowInfo)swallow.Info;
renderUnit = self.Trait<RenderUnit>(); renderUnit = self.Trait<RenderUnit>();
radarPings = self.World.WorldActor.TraitOrDefault<RadarPings>(); radarPings = self.World.WorldActor.TraitOrDefault<RadarPings>();
countdown = swallow.Info.AttackTime; countdown = swallowInfo.AttackTime;
renderUnit.DefaultAnimation.ReplaceAnim("burrowed"); renderUnit.DefaultAnimation.ReplaceAnim("burrowed");
stance = AttackState.Burrowed; stance = AttackState.Burrowed;
@@ -53,12 +55,14 @@ namespace OpenRA.Mods.D2k
bool WormAttack(Actor worm) bool WormAttack(Actor worm)
{ {
var targetLocation = target.Actor.Location; var targetLocation = target.Actor.Location;
// The target has moved too far away // The target has moved too far away
if ((location - targetLocation).Length > NearEnough) if ((location - targetLocation).Length > NearEnough)
return false; return false;
var lunch = worm.World.ActorMap.GetUnitsAt(targetLocation) var lunch = worm.World.ActorMap.GetUnitsAt(targetLocation)
.Where(t => !t.Equals(worm) && weapon.IsValidAgainst(t, worm)); .Where(t => !t.Equals(worm) && weapon.IsValidAgainst(t, worm));
if (!lunch.Any()) if (!lunch.Any())
return false; return false;
@@ -86,7 +90,7 @@ namespace OpenRA.Mods.D2k
void NotifyPlayer(Player player, WPos location) void NotifyPlayer(Player player, WPos location)
{ {
Sound.PlayNotification(player.World.Map.Rules, player, "Speech", swallow.Info.WormAttackNotification, player.Country.Race); Sound.PlayNotification(player.World.Map.Rules, player, "Speech", swallowInfo.WormAttackNotification, player.Country.Race);
radarPings.Add(() => true, location, Color.Red, 50); radarPings.Add(() => true, location, Color.Red, 50);
} }
@@ -98,37 +102,39 @@ namespace OpenRA.Mods.D2k
return this; return this;
} }
if (stance == AttackState.ReturningUnderground) // Wait for the worm to get back underground // Wait for the worm to get back underground
if (stance == AttackState.ReturningUnderground)
{ {
if (self.World.SharedRandom.Next() % 2 == 0) // There is a 50-50 chance that the worm would just go away // There is a 50-50 chance that the worm would just go away
if (self.World.SharedRandom.Next() % 2 == 0)
{ {
self.CancelActivity(); self.CancelActivity();
self.World.AddFrameEndTask(w => w.Remove(self)); self.World.AddFrameEndTask(w => w.Remove(self));
var wormManager = self.World.WorldActor.TraitOrDefault<WormManager>(); var wormManager = self.World.WorldActor.TraitOrDefault<WormManager>();
if (wormManager != null) if (wormManager != null)
wormManager.DecreaseWorms(); wormManager.DecreaseWorms();
} }
else else
{
renderUnit.DefaultAnimation.ReplaceAnim("idle"); renderUnit.DefaultAnimation.ReplaceAnim("idle");
}
return NextActivity; return NextActivity;
} }
if (stance == AttackState.Burrowed) // Wait for the worm to get in position // Wait for the worm to get in position
if (stance == AttackState.Burrowed)
{ {
// This is so that the worm cancels an attack against a target that has reached solid rock // This is so that the worm cancels an attack against a target that has reached solid rock
if (!positionable.CanEnterCell(target.Actor.Location, null, false)) if (!positionable.CanEnterCell(target.Actor.Location, null, false))
return NextActivity; return NextActivity;
var success = WormAttack(self); if (!WormAttack(self))
if (!success)
{ {
renderUnit.DefaultAnimation.ReplaceAnim("idle"); renderUnit.DefaultAnimation.ReplaceAnim("idle");
return NextActivity; return NextActivity;
} }
countdown = swallow.Info.ReturnTime; countdown = swallowInfo.ReturnTime;
stance = AttackState.ReturningUnderground; stance = AttackState.ReturningUnderground;
} }

View File

@@ -40,12 +40,13 @@ namespace OpenRA.Mods.D2k
class WormManager : ITick class WormManager : ITick
{ {
int countdown;
int wormsPresent;
readonly WormManagerInfo info; readonly WormManagerInfo info;
readonly Lazy<Actor[]> spawnPoints; readonly Lazy<Actor[]> spawnPoints;
readonly Lazy<RadarPings> radarPings; readonly Lazy<RadarPings> radarPings;
int countdown;
int wormsPresent;
public WormManager(WormManagerInfo info, Actor self) public WormManager(WormManagerInfo info, Actor self)
{ {
this.info = info; this.info = info;
@@ -72,9 +73,12 @@ namespace OpenRA.Mods.D2k
var wormLocations = new List<WPos>(); var wormLocations = new List<WPos>();
wormLocations.Add(SpawnWorm(self)); do
while (wormsPresent < info.Minimum) {
// Always spawn at least one worm, plus however many
// more we need to reach the defined minimum count.
wormLocations.Add(SpawnWorm(self)); wormLocations.Add(SpawnWorm(self));
} while (wormsPresent < info.Minimum);
AnnounceWormSign(self, wormLocations); AnnounceWormSign(self, wormLocations);
} }
@@ -87,6 +91,7 @@ namespace OpenRA.Mods.D2k
new OwnerInit(w.Players.First(x => x.PlayerName == info.WormOwnerPlayer)), new OwnerInit(w.Players.First(x => x.PlayerName == info.WormOwnerPlayer)),
new LocationInit(spawnPoint.Location) new LocationInit(spawnPoint.Location)
})); }));
wormsPresent++; wormsPresent++;
return spawnPoint.CenterPosition; return spawnPoint.CenterPosition;
@@ -112,7 +117,6 @@ namespace OpenRA.Mods.D2k
foreach (var wormLocation in wormLocations) foreach (var wormLocation in wormLocations)
radarPings.Value.Add(() => true, wormLocation, Color.Red, 50); radarPings.Value.Add(() => true, wormLocation, Color.Red, 50);
} }
} }

View File

@@ -35,14 +35,15 @@ namespace OpenRA.Mods.RA
public abstract class AttackBase : IIssueOrder, IResolveOrder, IOrderVoice, ISync public abstract class AttackBase : IIssueOrder, IResolveOrder, IOrderVoice, ISync
{ {
[Sync] public bool IsAttacking { get; internal set; } [Sync] public bool IsAttacking { get; internal set; }
public IEnumerable<Armament> Armaments { get { return GetArmaments(); } } public IEnumerable<Armament> Armaments { get { return getArmaments(); } }
public readonly AttackBaseInfo Info;
protected Lazy<IFacing> facing; protected Lazy<IFacing> facing;
protected Lazy<Building> building; protected Lazy<Building> building;
protected Lazy<IPositionable> positionable; protected Lazy<IPositionable> positionable;
protected Func<IEnumerable<Armament>> GetArmaments; protected Func<IEnumerable<Armament>> getArmaments;
readonly Actor self; readonly Actor self;
public readonly AttackBaseInfo Info;
public AttackBase(Actor self, AttackBaseInfo info) public AttackBase(Actor self, AttackBaseInfo info)
{ {
@@ -52,7 +53,7 @@ namespace OpenRA.Mods.RA
var armaments = Exts.Lazy(() => self.TraitsImplementing<Armament>() var armaments = Exts.Lazy(() => self.TraitsImplementing<Armament>()
.Where(a => info.Armaments.Contains(a.Info.Name))); .Where(a => info.Armaments.Contains(a.Info.Name)));
GetArmaments = () => armaments.Value; getArmaments = () => armaments.Value;
facing = Exts.Lazy(() => self.TraitOrDefault<IFacing>()); facing = Exts.Lazy(() => self.TraitOrDefault<IFacing>());
building = Exts.Lazy(() => self.TraitOrDefault<Building>()); building = Exts.Lazy(() => self.TraitOrDefault<Building>());
@@ -145,11 +146,8 @@ namespace OpenRA.Mods.RA
public bool HasAnyValidWeapons(Target t) public bool HasAnyValidWeapons(Target t)
{ {
if (Info.AttackRequiresEnteringCell) if (Info.AttackRequiresEnteringCell && !positionable.Value.CanEnterCell(t.Actor.Location, null, false))
{ return false;
if (!positionable.Value.CanEnterCell(t.Actor.Location, null, false))
return false;
}
return Armaments.Any(a => a.Weapon.IsValidAgainst(t, self.World, self)); return Armaments.Any(a => a.Weapon.IsValidAgainst(t, self.World, self));
} }

View File

@@ -29,13 +29,13 @@ namespace OpenRA.Mods.RA
public class AttackGarrisonedInfo : AttackFollowInfo, Requires<CargoInfo> public class AttackGarrisonedInfo : AttackFollowInfo, Requires<CargoInfo>
{ {
[Desc("Fire port offsets in local coordinates.")] [Desc("Fire port offsets in local coordinates.")]
public readonly WVec[] PortOffsets = {}; public readonly WVec[] PortOffsets = { };
[Desc("Fire port yaw angles.")] [Desc("Fire port yaw angles.")]
public readonly WAngle[] PortYaws = {}; public readonly WAngle[] PortYaws = { };
[Desc("Fire port yaw cone angle.")] [Desc("Fire port yaw cone angle.")]
public readonly WAngle[] PortCones = {}; public readonly WAngle[] PortCones = { };
public readonly string MuzzlePalette = "effect"; public readonly string MuzzlePalette = "effect";
@@ -54,7 +54,6 @@ namespace OpenRA.Mods.RA
Dictionary<Actor, IPositionable> paxPos; Dictionary<Actor, IPositionable> paxPos;
Dictionary<Actor, RenderSprites> paxRender; Dictionary<Actor, RenderSprites> paxRender;
public AttackGarrisoned(Actor self, AttackGarrisonedInfo info) public AttackGarrisoned(Actor self, AttackGarrisonedInfo info)
: base(self, info) : base(self, info)
{ {
@@ -66,8 +65,7 @@ namespace OpenRA.Mods.RA
paxPos = new Dictionary<Actor, IPositionable>(); paxPos = new Dictionary<Actor, IPositionable>();
paxRender = new Dictionary<Actor, RenderSprites>(); paxRender = new Dictionary<Actor, RenderSprites>();
GetArmaments = () => armaments; getArmaments = () => armaments;
if (info.PortOffsets.Length == 0) if (info.PortOffsets.Length == 0)
throw new InvalidOperationException("PortOffsets must have at least one entry."); throw new InvalidOperationException("PortOffsets must have at least one entry.");
@@ -110,7 +108,6 @@ namespace OpenRA.Mods.RA
armaments.RemoveAll(a => a.Actor == passenger); armaments.RemoveAll(a => a.Actor == passenger);
} }
FirePort SelectFirePort(Actor self, WAngle targetYaw) FirePort SelectFirePort(Actor self, WAngle targetYaw)
{ {
// Pick a random port that faces the target // Pick a random port that faces the target
@@ -171,6 +168,7 @@ namespace OpenRA.Mods.RA
muzzles.Add(muzzleFlash); muzzles.Add(muzzleFlash);
muzzleAnim.PlayThen(sequence, () => muzzles.Remove(muzzleFlash)); muzzleAnim.PlayThen(sequence, () => muzzles.Remove(muzzleFlash));
} }
foreach (var npa in self.TraitsImplementing<INotifyAttack>()) foreach (var npa in self.TraitsImplementing<INotifyAttack>())
npa.Attacking(self, target, a, barrel); npa.Attacking(self, target, a, barrel);
} }
@@ -179,6 +177,7 @@ namespace OpenRA.Mods.RA
public IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr) public IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr)
{ {
var pal = wr.Palette(info.MuzzlePalette); var pal = wr.Palette(info.MuzzlePalette);
// Display muzzle flashes // Display muzzle flashes
foreach (var m in muzzles) foreach (var m in muzzles)
foreach (var r in m.Render(self, wr, pal, 1f)) foreach (var r in m.Render(self, wr, pal, 1f))

View File

@@ -27,14 +27,15 @@ namespace OpenRA.Mods.RA
class AttackWander : INotifyIdle class AttackWander : INotifyIdle
{ {
readonly AttackMove attackMove;
readonly AttackWanderInfo info;
int ticksIdle; int ticksIdle;
int effectiveMoveRadius; int effectiveMoveRadius;
readonly AttackMove attackMove;
readonly AttackWanderInfo Info;
public AttackWander(Actor self, AttackWanderInfo info) public AttackWander(Actor self, AttackWanderInfo info)
{ {
Info = info; this.info = info;
effectiveMoveRadius = info.WanderMoveRadius; effectiveMoveRadius = info.WanderMoveRadius;
attackMove = self.TraitOrDefault<AttackMove>(); attackMove = self.TraitOrDefault<AttackMove>();
} }
@@ -47,16 +48,16 @@ namespace OpenRA.Mods.RA
if (!self.World.Map.Contains(targetCell)) if (!self.World.Map.Contains(targetCell))
{ {
// If MoveRadius is too big there might not be a valid cell to order the attack to (if actor is on a small island and can't leave) // If MoveRadius is too big there might not be a valid cell to order the attack to (if actor is on a small island and can't leave)
if (++ticksIdle % Info.MoveReductionRadiusScale == 0) if (++ticksIdle % info.MoveReductionRadiusScale == 0)
effectiveMoveRadius--; effectiveMoveRadius--;
return; // We'll be back the next tick; better to sit idle for a few seconds than prolongue this tick indefinitely with a loop return; // We'll be back the next tick; better to sit idle for a few seconds than prolong this tick indefinitely with a loop
} }
attackMove.ResolveOrder(self, new Order("AttackMove", self, false) { TargetLocation = targetCell }); attackMove.ResolveOrder(self, new Order("AttackMove", self, false) { TargetLocation = targetCell });
ticksIdle = 0; ticksIdle = 0;
effectiveMoveRadius = Info.WanderMoveRadius; effectiveMoveRadius = info.WanderMoveRadius;
} }
} }
} }