Add ReloadAmmoPool and adapt AmmoPool

Refactored and simplified Rearm activity.
Uses local Reload now.

Removed AmmoPool.SelfReloads.
This commit is contained in:
reaperrr
2017-06-23 14:47:26 +02:00
committed by Paul Chote
parent a017018bee
commit 6f95080aa4
12 changed files with 152 additions and 85 deletions

View File

@@ -21,7 +21,6 @@ namespace OpenRA.Mods.Common.Activities
readonly Target target;
readonly Aircraft aircraft;
readonly AttackPlane attackPlane;
readonly AmmoPool[] ammoPools;
int ticksUntilTurn;
@@ -30,7 +29,6 @@ namespace OpenRA.Mods.Common.Activities
this.target = target;
aircraft = self.Trait<Aircraft>();
attackPlane = self.TraitOrDefault<AttackPlane>();
ammoPools = self.TraitsImplementing<AmmoPool>().ToArray();
ticksUntilTurn = attackPlane.AttackPlaneInfo.AttackTurnDelay;
}
@@ -46,8 +44,8 @@ namespace OpenRA.Mods.Common.Activities
if (!target.IsValidFor(self))
return NextActivity;
// TODO: This should check whether there is ammo left that is actually suitable for the target
if (ammoPools.All(x => !x.Info.SelfReloads && !x.HasAmmo()))
// If all valid weapons have depleted their ammo, return to RearmBuilding to reload and then resume the activity
if (attackPlane.Armaments.All(x => x.OutOfAmmo || !x.Weapon.IsValidAgainst(target, self.World, self)))
return ActivityUtils.SequenceActivities(new ReturnToBase(self, aircraft.Info.AbortOnResupply), this);
if (attackPlane != null)

View File

@@ -21,7 +21,6 @@ namespace OpenRA.Mods.Common.Activities
{
readonly Aircraft helicopter;
readonly AttackHeli attackHeli;
readonly AmmoPool[] ammoPools;
readonly bool attackOnlyVisibleTargets;
Target target;
@@ -46,7 +45,6 @@ namespace OpenRA.Mods.Common.Activities
Target = target;
helicopter = self.Trait<Aircraft>();
attackHeli = self.Trait<AttackHeli>();
ammoPools = self.TraitsImplementing<AmmoPool>().ToArray();
this.attackOnlyVisibleTargets = attackOnlyVisibleTargets;
}
@@ -74,8 +72,8 @@ namespace OpenRA.Mods.Common.Activities
return new HeliFly(self, newTarget);
}
// If any AmmoPool is depleted and no weapon is valid against target, return to helipad to reload and then resume the activity
if (ammoPools.Any(x => !x.Info.SelfReloads && !x.HasAmmo()) && !attackHeli.HasAnyValidWeapons(target))
// If all valid weapons have depleted their ammo, return to RearmBuilding to reload and then resume the activity
if (attackHeli.Armaments.All(x => x.OutOfAmmo || !x.Weapon.IsValidAgainst(target, self.World, self)))
return ActivityUtils.SequenceActivities(new HeliReturnToBase(self, helicopter.Info.AbortOnResupply), this);
var dist = targetPos - pos;

View File

@@ -109,7 +109,7 @@ namespace OpenRA.Mods.Common.Activities
return true;
return heli.Info.RearmBuildings.Contains(dest.Info.Name) && self.TraitsImplementing<AmmoPool>()
.Any(p => !p.Info.SelfReloads && !p.FullAmmo());
.Any(p => !p.SelfReloads && !p.FullAmmo());
}
}
}

View File

@@ -105,7 +105,7 @@ namespace OpenRA.Mods.Common.Activities
return true;
return planeInfo.RearmBuildings.Contains(dest.Info.Name) && self.TraitsImplementing<AmmoPool>()
.Any(p => !p.Info.SelfReloads && !p.FullAmmo());
.Any(p => !p.SelfReloads && !p.FullAmmo());
}
public override Activity Tick(Actor self)

View File

@@ -20,21 +20,15 @@ namespace OpenRA.Mods.Common.Activities
public class Rearm : Activity
{
readonly AmmoPool[] ammoPools;
readonly Dictionary<AmmoPool, int> ammoPoolsReloadTimes;
public Rearm(Actor self)
{
ammoPools = self.TraitsImplementing<AmmoPool>().Where(p => !p.Info.SelfReloads).ToArray();
if (ammoPools == null)
return;
ammoPoolsReloadTimes = ammoPools.ToDictionary(x => x, y => y.Info.ReloadDelay);
ammoPools = self.TraitsImplementing<AmmoPool>().Where(p => !p.SelfReloads).ToArray();
}
public override Activity Tick(Actor self)
{
if (IsCanceled || ammoPoolsReloadTimes == null)
if (IsCanceled)
return NextActivity;
var needsReloading = false;
@@ -46,30 +40,32 @@ namespace OpenRA.Mods.Common.Activities
needsReloading = true;
if (--ammoPoolsReloadTimes[pool] > 0)
continue;
Reload(self, pool);
}
return needsReloading ? this : NextActivity;
}
void Reload(Actor self, AmmoPool ammoPool)
{
if (--ammoPool.RemainingTicks == 0)
{
// HACK to check if we are on the helipad/airfield/etc.
var hostBuilding = self.World.ActorMap.GetActorsAt(self.Location)
.FirstOrDefault(a => a.Info.HasTraitInfo<BuildingInfo>());
if (hostBuilding == null || !hostBuilding.IsInWorld)
return NextActivity;
if (!pool.GiveAmmo())
continue;
return;
foreach (var host in hostBuilding.TraitsImplementing<INotifyRearm>())
host.Rearming(hostBuilding, self);
var sound = pool.Info.RearmSound;
if (sound != null)
Game.Sound.PlayToPlayer(SoundType.World, self.Owner, sound, self.CenterPosition);
ammoPool.RemainingTicks = ammoPool.Info.ReloadDelay;
if (!string.IsNullOrEmpty(ammoPool.Info.RearmSound))
Game.Sound.PlayToPlayer(SoundType.World, self.Owner, ammoPool.Info.RearmSound, self.CenterPosition);
ammoPoolsReloadTimes[pool] = pool.Info.ReloadDelay;
ammoPool.GiveAmmo(self, ammoPool.Info.ReloadCount);
}
return needsReloading ? this : NextActivity;
}
}
}