Refactors LimitedAmmo to AmmoPool.
Removes Reloads trait. This enables adding multiple AmmoPools via @ differentiators and Name which adds the possibility to assign each armament to a specific ammo pool. Furthermore, this moves all Reloads functionality onto AmmoPool. Now a combination of all three is possible on a single actor: no limited ammo, limited ammo that can reload on its own, and limited ammo which needs to be reloaded at a rearm actor. Additionally moves RearmSound from Minelayer to AmmoPool.
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Activities;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
@@ -18,30 +20,38 @@ namespace OpenRA.Mods.Common.Activities
|
||||
public class FlyAttack : Activity
|
||||
{
|
||||
readonly Target target;
|
||||
readonly AttackPlane attackPlane;
|
||||
readonly IEnumerable<AmmoPool> ammoPools;
|
||||
Activity inner;
|
||||
int ticksUntilTurn = 50;
|
||||
|
||||
public FlyAttack(Target target) { this.target = target; }
|
||||
public FlyAttack(Actor self, Target target)
|
||||
{
|
||||
this.target = target;
|
||||
attackPlane = self.TraitOrDefault<AttackPlane>();
|
||||
ammoPools = self.TraitsImplementing<AmmoPool>();
|
||||
}
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
if (!target.IsValidFor(self))
|
||||
return NextActivity;
|
||||
|
||||
var limitedAmmo = self.TraitOrDefault<LimitedAmmo>();
|
||||
if (limitedAmmo != null && !limitedAmmo.HasAmmo())
|
||||
// Move to the next activity only if all ammo pools are depleted and none reload automatically
|
||||
// TODO: This should check whether there is ammo left that is actually suitable for the target
|
||||
if (ammoPools != null && ammoPools.All(x => !x.Info.SelfReloads && !x.HasAmmo()))
|
||||
return NextActivity;
|
||||
|
||||
var attack = self.TraitOrDefault<AttackPlane>();
|
||||
if (attack != null)
|
||||
attack.DoAttack(self, target);
|
||||
if (attackPlane != null)
|
||||
attackPlane.DoAttack(self, target);
|
||||
|
||||
if (inner == null)
|
||||
{
|
||||
if (IsCanceled)
|
||||
return NextActivity;
|
||||
|
||||
if (target.IsInRange(self.CenterPosition, attack.Armaments.Select(a => a.Weapon.MinRange).Min()))
|
||||
// TODO: This should fire each weapon at its maximum range
|
||||
if (target.IsInRange(self.CenterPosition, attackPlane.Armaments.Select(a => a.Weapon.MinRange).Min()))
|
||||
inner = Util.SequenceActivities(new FlyTimed(ticksUntilTurn), new Fly(self, target), new FlyTimed(ticksUntilTurn));
|
||||
else
|
||||
inner = Util.SequenceActivities(new Fly(self, target), new FlyTimed(ticksUntilTurn));
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Activities;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Traits;
|
||||
@@ -16,21 +19,29 @@ namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class HeliAttack : Activity
|
||||
{
|
||||
Target target;
|
||||
public HeliAttack(Target target) { this.target = target; }
|
||||
readonly Target target;
|
||||
readonly Helicopter helicopter;
|
||||
readonly AttackHeli attackHeli;
|
||||
readonly IEnumerable<AmmoPool> ammoPools;
|
||||
|
||||
public HeliAttack(Actor self, Target target)
|
||||
{
|
||||
this.target = target;
|
||||
helicopter = self.Trait<Helicopter>();
|
||||
attackHeli = self.Trait<AttackHeli>();
|
||||
ammoPools = self.TraitsImplementing<AmmoPool>();
|
||||
}
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
if (IsCanceled || !target.IsValidFor(self))
|
||||
return NextActivity;
|
||||
|
||||
var limitedAmmo = self.TraitOrDefault<LimitedAmmo>();
|
||||
var reloads = self.TraitOrDefault<Reloads>();
|
||||
if (limitedAmmo != null && !limitedAmmo.HasAmmo() && reloads == null)
|
||||
// If all ammo pools are depleted and none reload automatically, return to helipad to reload and then move to next activity
|
||||
// TODO: This should check whether there is ammo left that is actually suitable for the target
|
||||
if (ammoPools != null && ammoPools.All(x => !x.Info.SelfReloads && !x.HasAmmo()))
|
||||
return Util.SequenceActivities(new HeliReturn(), NextActivity);
|
||||
|
||||
var helicopter = self.Trait<Helicopter>();
|
||||
var attack = self.Trait<AttackHeli>();
|
||||
var dist = target.CenterPosition - self.CenterPosition;
|
||||
|
||||
// Can rotate facing while ascending
|
||||
@@ -41,10 +52,12 @@ namespace OpenRA.Mods.Common.Activities
|
||||
return this;
|
||||
|
||||
// Fly towards the target
|
||||
if (!target.IsInRange(self.CenterPosition, attack.GetMaximumRange()))
|
||||
// TODO: Fix that the helicopter won't do anything if it has multiple weapons with different ranges
|
||||
// and the weapon with the longest range is out of ammo
|
||||
if (!target.IsInRange(self.CenterPosition, attackHeli.GetMaximumRange()))
|
||||
helicopter.SetPosition(self, helicopter.CenterPosition + helicopter.FlyStep(desiredFacing));
|
||||
|
||||
attack.DoAttack(self, target);
|
||||
attackHeli.DoAttack(self, target);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -8,53 +8,64 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Activities;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class Rearm : Activity
|
||||
{
|
||||
readonly LimitedAmmo limitedAmmo;
|
||||
int ticksPerPip = 25 * 2;
|
||||
int remainingTicks = 25 * 2;
|
||||
string sound = null;
|
||||
readonly IEnumerable<AmmoPool> ammoPools;
|
||||
readonly Dictionary<AmmoPool, int> ammoPoolsReloadTimes;
|
||||
|
||||
public Rearm(Actor self, string sound = null)
|
||||
public Rearm(Actor self)
|
||||
{
|
||||
limitedAmmo = self.TraitOrDefault<LimitedAmmo>();
|
||||
if (limitedAmmo != null)
|
||||
ticksPerPip = limitedAmmo.ReloadTimePerAmmo();
|
||||
remainingTicks = ticksPerPip;
|
||||
this.sound = sound;
|
||||
ammoPools = self.TraitsImplementing<AmmoPool>().Where(p => !p.Info.SelfReloads);
|
||||
|
||||
if (ammoPools == null)
|
||||
return;
|
||||
|
||||
ammoPoolsReloadTimes = ammoPools.ToDictionary(x => x, y => y.Info.ReloadTicks);
|
||||
}
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
if (IsCanceled || limitedAmmo == null)
|
||||
if (IsCanceled || ammoPoolsReloadTimes == null)
|
||||
return NextActivity;
|
||||
|
||||
if (--remainingTicks == 0)
|
||||
var needsReloading = false;
|
||||
|
||||
foreach (var pool in ammoPools)
|
||||
{
|
||||
var hostBuilding = self.World.ActorMap.GetUnitsAt(self.Location)
|
||||
.FirstOrDefault(a => a.HasTrait<RenderBuilding>());
|
||||
if (pool.FullAmmo())
|
||||
continue;
|
||||
|
||||
needsReloading = true;
|
||||
|
||||
if (--ammoPoolsReloadTimes[pool] > 0)
|
||||
continue;
|
||||
|
||||
// HACK to check if we are on the helipad/airfield/etc.
|
||||
var hostBuilding = self.World.ActorMap.GetUnitsAt(self.Location).FirstOrDefault(a => a.HasTrait<RenderBuilding>());
|
||||
|
||||
if (hostBuilding == null || !hostBuilding.IsInWorld)
|
||||
return NextActivity;
|
||||
|
||||
if (!limitedAmmo.GiveAmmo())
|
||||
return NextActivity;
|
||||
if (!pool.GiveAmmo())
|
||||
continue;
|
||||
|
||||
hostBuilding.Trait<RenderBuilding>().PlayCustomAnim(hostBuilding, "active");
|
||||
|
||||
var sound = pool.Info.RearmSound;
|
||||
if (sound != null)
|
||||
Sound.Play(sound, self.CenterPosition);
|
||||
|
||||
remainingTicks = limitedAmmo.ReloadTimePerAmmo();
|
||||
ammoPoolsReloadTimes[pool] = pool.Info.ReloadTicks;
|
||||
}
|
||||
|
||||
return this;
|
||||
return needsReloading ? this : NextActivity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user