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

@@ -500,9 +500,9 @@ namespace OpenRA.Mods.Common.AI
if (aircraftInfo == null) if (aircraftInfo == null)
return true; return true;
var ammoPoolsInfo = actorInfo.TraitInfos<AmmoPoolInfo>(); // If the aircraft has at least 1 AmmoPool and defines 1 or more RearmBuildings, check if we have enough of those
var hasAmmoPool = actorInfo.TraitInfos<AmmoPoolInfo>().Any();
if (ammoPoolsInfo.Any(x => !x.SelfReloads)) if (hasAmmoPool && aircraftInfo.RearmBuildings.Count > 0)
{ {
var countOwnAir = CountUnits(actorInfo.Name, Player); var countOwnAir = CountUnits(actorInfo.Name, Player);
var countBuildings = aircraftInfo.RearmBuildings.Sum(b => CountBuilding(b, Player)); var countBuildings = aircraftInfo.RearmBuildings.Sum(b => CountBuilding(b, Player));

View File

@@ -120,7 +120,7 @@ namespace OpenRA.Mods.Common.AI
protected static bool ReloadsAutomatically(Actor a) protected static bool ReloadsAutomatically(Actor a)
{ {
var ammoPools = a.TraitsImplementing<AmmoPool>(); var ammoPools = a.TraitsImplementing<AmmoPool>();
return ammoPools.All(x => x.Info.SelfReloads); return ammoPools.All(x => x.SelfReloads);
} }
protected static bool IsRearm(Actor a) protected static bool IsRearm(Actor a)

View File

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

View File

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

View File

@@ -109,7 +109,7 @@ namespace OpenRA.Mods.Common.Activities
return true; return true;
return heli.Info.RearmBuildings.Contains(dest.Info.Name) && self.TraitsImplementing<AmmoPool>() 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 true;
return planeInfo.RearmBuildings.Contains(dest.Info.Name) && self.TraitsImplementing<AmmoPool>() 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) public override Activity Tick(Actor self)

View File

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

View File

@@ -359,6 +359,7 @@
<Compile Include="Traits\JamsMissiles.cs" /> <Compile Include="Traits\JamsMissiles.cs" />
<Compile Include="Traits\KillsSelf.cs" /> <Compile Include="Traits\KillsSelf.cs" />
<Compile Include="Traits\AmmoPool.cs" /> <Compile Include="Traits\AmmoPool.cs" />
<Compile Include="Traits\ReloadAmmoPool.cs" />
<Compile Include="Traits\Mobile.cs" /> <Compile Include="Traits\Mobile.cs" />
<Compile Include="Traits\Modifiers\FrozenUnderFog.cs" /> <Compile Include="Traits\Modifiers\FrozenUnderFog.cs" />
<Compile Include="Traits\Modifiers\HiddenUnderFog.cs" /> <Compile Include="Traits\Modifiers\HiddenUnderFog.cs" />

View File

@@ -18,7 +18,7 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Actor has a limited amount of ammo, after using it all the actor must reload in some way.")] [Desc("Actor has a limited amount of ammo, after using it all the actor must reload in some way.")]
public class AmmoPoolInfo : ITraitInfo public class AmmoPoolInfo : ITraitInfo
{ {
[Desc("Name of this ammo pool, used to link armaments to this pool.")] [Desc("Name of this ammo pool, used to link armaments and reload traits to this pool.")]
public readonly string Name = "primary"; public readonly string Name = "primary";
[Desc("How much ammo does this pool contain when fully loaded.")] [Desc("How much ammo does this pool contain when fully loaded.")]
@@ -42,62 +42,69 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Sound to play for each reloaded ammo magazine.")] [Desc("Sound to play for each reloaded ammo magazine.")]
public readonly string RearmSound = null; public readonly string RearmSound = null;
// HACK: Temporarily kept until Rearm activity is gone for good
[Desc("Time to reload per ReloadCount on airfield etc.")] [Desc("Time to reload per ReloadCount on airfield etc.")]
public readonly int ReloadDelay = 50; public readonly int ReloadDelay = 50;
[Desc("Whether or not ammo is replenished on its own.")]
public readonly bool SelfReloads = false;
[Desc("Time to reload per ReloadCount when actor 'SelfReloads'.")]
public readonly int SelfReloadDelay = 50;
[Desc("Whether or not reload timer should be reset when ammo has been fired.")]
public readonly bool ResetOnFire = false;
public object Create(ActorInitializer init) { return new AmmoPool(init.Self, this); } public object Create(ActorInitializer init) { return new AmmoPool(init.Self, this); }
} }
public class AmmoPool : INotifyAttack, IPips, ITick, ISync public class AmmoPool : INotifyCreated, INotifyAttack, IPips, ISync
{ {
public readonly AmmoPoolInfo Info; public readonly AmmoPoolInfo Info;
[Sync] public int CurrentAmmo; ConditionManager conditionManager;
int emptyToken = ConditionManager.InvalidConditionToken;
bool selfReloads;
// HACK: Temporarily needed until Rearm activity is gone for good
[Sync] public int RemainingTicks; [Sync] public int RemainingTicks;
public int PreviousAmmo; [Sync] int currentAmmo;
public AmmoPool(Actor self, AmmoPoolInfo info) public AmmoPool(Actor self, AmmoPoolInfo info)
{ {
Info = info; Info = info;
if (Info.InitialAmmo < Info.Ammo && Info.InitialAmmo >= 0) if (Info.InitialAmmo < Info.Ammo && Info.InitialAmmo >= 0)
CurrentAmmo = Info.InitialAmmo; currentAmmo = Info.InitialAmmo;
else else
CurrentAmmo = Info.Ammo; currentAmmo = Info.Ammo;
RemainingTicks = Info.SelfReloadDelay;
PreviousAmmo = GetAmmoCount();
} }
public int GetAmmoCount() { return CurrentAmmo; } public int GetAmmoCount() { return currentAmmo; }
public bool FullAmmo() { return CurrentAmmo == Info.Ammo; } public bool FullAmmo() { return currentAmmo == Info.Ammo; }
public bool HasAmmo() { return CurrentAmmo > 0; } public bool HasAmmo() { return currentAmmo > 0; }
public bool GiveAmmo() public bool GiveAmmo()
{ {
if (CurrentAmmo >= Info.Ammo) if (currentAmmo >= Info.Ammo)
return false; return false;
++CurrentAmmo; ++currentAmmo;
return true; return true;
} }
public bool TakeAmmo() public bool TakeAmmo()
{ {
if (CurrentAmmo <= 0) if (currentAmmo <= 0)
return false; return false;
--CurrentAmmo; --currentAmmo;
return true; return true;
} }
// This mostly serves to avoid complicated ReloadAmmoPool look-ups in various other places.
// TODO: Investigate removing this when the Rearm activity is replaced with a condition-based solution.
public bool SelfReloads { get { return selfReloads; } }
void INotifyCreated.Created(Actor self)
{
conditionManager = self.TraitOrDefault<ConditionManager>();
selfReloads = self.TraitsImplementing<ReloadAmmoPool>().Any(r => r.Info.AmmoPool == Info.Name && r.Info.RequiresCondition == null);
// HACK: Temporarily needed until Rearm activity is gone for good
RemainingTicks = Info.ReloadDelay;
}
void INotifyAttack.Attacking(Actor self, Target target, Armament a, Barrel barrel) void INotifyAttack.Attacking(Actor self, Target target, Armament a, Barrel barrel)
{ {
if (a != null && a.Info.AmmoPoolName == Info.Name) if (a != null && a.Info.AmmoPoolName == Info.Name)
@@ -106,35 +113,12 @@ namespace OpenRA.Mods.Common.Traits
void INotifyAttack.PreparingAttack(Actor self, Target target, Armament a, Barrel barrel) { } void INotifyAttack.PreparingAttack(Actor self, Target target, Armament a, Barrel barrel) { }
void ITick.Tick(Actor self)
{
if (!Info.SelfReloads)
return;
// Resets the tick counter if ammo was fired.
if (Info.ResetOnFire && GetAmmoCount() < PreviousAmmo)
{
RemainingTicks = Info.SelfReloadDelay;
PreviousAmmo = GetAmmoCount();
}
if (!FullAmmo() && --RemainingTicks == 0)
{
RemainingTicks = Info.SelfReloadDelay;
for (var i = 0; i < Info.ReloadCount; i++)
GiveAmmo();
PreviousAmmo = GetAmmoCount();
}
}
public IEnumerable<PipType> GetPips(Actor self) public IEnumerable<PipType> GetPips(Actor self)
{ {
var pips = Info.PipCount >= 0 ? Info.PipCount : Info.Ammo; var pips = Info.PipCount >= 0 ? Info.PipCount : Info.Ammo;
return Enumerable.Range(0, pips).Select(i => return Enumerable.Range(0, pips).Select(i =>
(CurrentAmmo * pips) / Info.Ammo > i ? (currentAmmo * pips) / Info.Ammo > i ?
Info.PipType : Info.PipTypeEmpty); Info.PipType : Info.PipTypeEmpty);
} }
} }

View File

@@ -107,6 +107,7 @@ namespace OpenRA.Mods.Common.Traits
readonly Actor self; readonly Actor self;
Turreted turret; Turreted turret;
AmmoPool ammoPool; AmmoPool ammoPool;
ReloadAmmoPool reloadAmmoPool;
BodyOrientation coords; BodyOrientation coords;
INotifyBurstComplete[] notifyBurstComplete; INotifyBurstComplete[] notifyBurstComplete;
INotifyAttack[] notifyAttacks; INotifyAttack[] notifyAttacks;
@@ -157,6 +158,7 @@ namespace OpenRA.Mods.Common.Traits
{ {
turret = self.TraitsImplementing<Turreted>().FirstOrDefault(t => t.Name == Info.Turret); turret = self.TraitsImplementing<Turreted>().FirstOrDefault(t => t.Name == Info.Turret);
ammoPool = self.TraitsImplementing<AmmoPool>().FirstOrDefault(la => la.Info.Name == Info.AmmoPoolName); ammoPool = self.TraitsImplementing<AmmoPool>().FirstOrDefault(la => la.Info.Name == Info.AmmoPoolName);
reloadAmmoPool = self.TraitsImplementing<ReloadAmmoPool>().FirstOrDefault(ra => ra.Info.AmmoPool == Info.AmmoPoolName);
coords = self.Trait<BodyOrientation>(); coords = self.Trait<BodyOrientation>();
notifyBurstComplete = self.TraitsImplementing<INotifyBurstComplete>().ToArray(); notifyBurstComplete = self.TraitsImplementing<INotifyBurstComplete>().ToArray();
notifyAttacks = self.TraitsImplementing<INotifyAttack>().ToArray(); notifyAttacks = self.TraitsImplementing<INotifyAttack>().ToArray();
@@ -339,7 +341,7 @@ namespace OpenRA.Mods.Common.Traits
} }
} }
public virtual bool OutOfAmmo { get { return ammoPool != null && !ammoPool.Info.SelfReloads && !ammoPool.HasAmmo(); } } public virtual bool OutOfAmmo { get { return ammoPool != null && !ammoPool.HasAmmo() && (reloadAmmoPool == null || reloadAmmoPool.IsTraitDisabled); } }
public virtual bool IsReloading { get { return FireDelay > 0 || IsTraitDisabled; } } public virtual bool IsReloading { get { return FireDelay > 0 || IsTraitDisabled; } }
public virtual bool AllowExplode { get { return !IsReloading; } } public virtual bool AllowExplode { get { return !IsReloading; } }
bool IExplodeModifier.ShouldExplode(Actor self) { return AllowExplode; } bool IExplodeModifier.ShouldExplode(Actor self) { return AllowExplode; }

View File

@@ -0,0 +1,88 @@
#region Copyright & License Information
/*
* Copyright 2007-2017 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation. For more information,
* see COPYING.
*/
#endregion
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Reloads an ammo pool.")]
public class ReloadAmmoPoolInfo : PausableConditionalTraitInfo
{
[Desc("Reload ammo pool with this name.")]
public readonly string AmmoPool = "primary";
[Desc("Reload time in ticks per Count.")]
public readonly int Delay = 50;
[Desc("How much ammo is reloaded after Delay.")]
public readonly int Count = 1;
[Desc("Whether or not reload timer should be reset when ammo has been fired.")]
public readonly bool ResetOnFire = false;
[Desc("Play this sound each time ammo is reloaded.")]
public readonly string Sound = null;
public override object Create(ActorInitializer init) { return new ReloadAmmoPool(this); }
public override void RulesetLoaded(Ruleset rules, ActorInfo ai)
{
if (ai.TraitInfos<AmmoPoolInfo>().Count(ap => ap.Name == AmmoPool) != 1)
throw new YamlException("ReloadsAmmoPool.AmmoPool requires exactly one AmmoPool with matching Name!");
base.RulesetLoaded(rules, ai);
}
}
public class ReloadAmmoPool : PausableConditionalTrait<ReloadAmmoPoolInfo>, ITick, INotifyCreated, INotifyAttack, ISync
{
AmmoPool ammoPool;
[Sync] int remainingTicks;
public ReloadAmmoPool(ReloadAmmoPoolInfo info)
: base(info) { }
void INotifyCreated.Created(Actor self)
{
ammoPool = self.TraitsImplementing<AmmoPool>().Single(ap => ap.Info.Name == Info.AmmoPool);
remainingTicks = Info.Delay;
}
void INotifyAttack.Attacking(Actor self, Target target, Armament a, Barrel barrel)
{
if (Info.ResetOnFire)
remainingTicks = Info.Delay;
}
void INotifyAttack.PreparingAttack(Actor self, Target target, Armament a, Barrel barrel) { }
void ITick.Tick(Actor self)
{
if (IsTraitPaused || IsTraitDisabled)
return;
Reload(self, Info.Delay, Info.Count, Info.Sound);
}
protected virtual void Reload(Actor self, int reloadDelay, int reloadCount, string sound)
{
if (!ammoPool.FullAmmo() && --remainingTicks == 0)
{
remainingTicks = reloadDelay;
if (!string.IsNullOrEmpty(sound))
Game.Sound.PlayToPlayer(SoundType.World, self.Owner, sound, self.CenterPosition);
ammoPool.GiveAmmo(self, reloadCount);
}
}
}
}

View File

@@ -77,7 +77,7 @@ namespace OpenRA.Mods.Common.Traits
bool CanRearm() bool CanRearm()
{ {
return ammoPools.Any(x => !x.Info.SelfReloads && !x.FullAmmo()); return ammoPools.Any(x => !x.SelfReloads && !x.FullAmmo());
} }
public string VoicePhraseForOrder(Actor self, Order order) public string VoicePhraseForOrder(Actor self, Order order)