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

@@ -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.")]
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";
[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.")]
public readonly string RearmSound = null;
// HACK: Temporarily kept until Rearm activity is gone for good
[Desc("Time to reload per ReloadCount on airfield etc.")]
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 class AmmoPool : INotifyAttack, IPips, ITick, ISync
public class AmmoPool : INotifyCreated, INotifyAttack, IPips, ISync
{
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;
public int PreviousAmmo;
[Sync] int currentAmmo;
public AmmoPool(Actor self, AmmoPoolInfo info)
{
Info = info;
if (Info.InitialAmmo < Info.Ammo && Info.InitialAmmo >= 0)
CurrentAmmo = Info.InitialAmmo;
currentAmmo = Info.InitialAmmo;
else
CurrentAmmo = Info.Ammo;
RemainingTicks = Info.SelfReloadDelay;
PreviousAmmo = GetAmmoCount();
currentAmmo = Info.Ammo;
}
public int GetAmmoCount() { return CurrentAmmo; }
public bool FullAmmo() { return CurrentAmmo == Info.Ammo; }
public bool HasAmmo() { return CurrentAmmo > 0; }
public int GetAmmoCount() { return currentAmmo; }
public bool FullAmmo() { return currentAmmo == Info.Ammo; }
public bool HasAmmo() { return currentAmmo > 0; }
public bool GiveAmmo()
{
if (CurrentAmmo >= Info.Ammo)
if (currentAmmo >= Info.Ammo)
return false;
++CurrentAmmo;
++currentAmmo;
return true;
}
public bool TakeAmmo()
{
if (CurrentAmmo <= 0)
if (currentAmmo <= 0)
return false;
--CurrentAmmo;
--currentAmmo;
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)
{
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 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)
{
var pips = Info.PipCount >= 0 ? Info.PipCount : Info.Ammo;
return Enumerable.Range(0, pips).Select(i =>
(CurrentAmmo * pips) / Info.Ammo > i ?
(currentAmmo * pips) / Info.Ammo > i ?
Info.PipType : Info.PipTypeEmpty);
}
}

View File

@@ -107,6 +107,7 @@ namespace OpenRA.Mods.Common.Traits
readonly Actor self;
Turreted turret;
AmmoPool ammoPool;
ReloadAmmoPool reloadAmmoPool;
BodyOrientation coords;
INotifyBurstComplete[] notifyBurstComplete;
INotifyAttack[] notifyAttacks;
@@ -157,6 +158,7 @@ namespace OpenRA.Mods.Common.Traits
{
turret = self.TraitsImplementing<Turreted>().FirstOrDefault(t => t.Name == Info.Turret);
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>();
notifyBurstComplete = self.TraitsImplementing<INotifyBurstComplete>().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 AllowExplode { get { return !IsReloading; } }
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()
{
return ammoPools.Any(x => !x.Info.SelfReloads && !x.FullAmmo());
return ammoPools.Any(x => !x.SelfReloads && !x.FullAmmo());
}
public string VoicePhraseForOrder(Actor self, Order order)