From ee00954f2e0e1acb9cf30b4c69b69d7e8b3fbc95 Mon Sep 17 00:00:00 2001 From: reaperrr Date: Sat, 19 Oct 2019 17:25:25 +0200 Subject: [PATCH] Replace AmmoPool lookup methods with properties And gave the more suitable names while at it. This is more in line with how we do things in other places. --- OpenRA.Mods.Cnc/Activities/LayMines.cs | 3 ++- .../Activities/Air/ReturnToBase.cs | 2 +- OpenRA.Mods.Common/Activities/Resupply.cs | 4 +-- .../Properties/AmmoPoolProperties.cs | 2 +- OpenRA.Mods.Common/Traits/Air/Aircraft.cs | 4 +-- OpenRA.Mods.Common/Traits/AmmoPool.cs | 25 +++++++++---------- .../BotModules/Squads/States/AirStates.cs | 4 +-- OpenRA.Mods.Common/Traits/ReloadAmmoPool.cs | 2 +- OpenRA.Mods.Common/Traits/Repairable.cs | 2 +- 9 files changed, 24 insertions(+), 24 deletions(-) diff --git a/OpenRA.Mods.Cnc/Activities/LayMines.cs b/OpenRA.Mods.Cnc/Activities/LayMines.cs index 0874ddb21e..87b308ddfa 100644 --- a/OpenRA.Mods.Cnc/Activities/LayMines.cs +++ b/OpenRA.Mods.Cnc/Activities/LayMines.cs @@ -66,7 +66,7 @@ namespace OpenRA.Mods.Cnc.Activities if ((minefield == null || minefield.Contains(self.Location)) && CanLayMine(self, self.Location)) { - if (rearmableInfo != null && ammoPools.Any(p => p.Info.Name == minelayer.Info.AmmoPoolName && !p.HasAmmo())) + if (rearmableInfo != null && ammoPools.Any(p => p.Info.Name == minelayer.Info.AmmoPoolName && !p.HasAmmo)) { // Rearm (and possibly repair) at rearm building, then back out here to refill the minefield some more rearmTarget = self.World.Actors.Where(a => self.Owner.Stances[a.Owner] == Stance.Ally && rearmableInfo.RearmActors.Contains(a.Info.Name)) @@ -137,6 +137,7 @@ namespace OpenRA.Mods.Cnc.Activities var pool = ammoPools.FirstOrDefault(x => x.Info.Name == minelayer.Info.AmmoPoolName); if (pool == null) return; + pool.TakeAmmo(self, 1); } diff --git a/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs b/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs index b31140d54a..fa4aa59ce2 100644 --- a/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs +++ b/OpenRA.Mods.Common/Activities/Air/ReturnToBase.cs @@ -60,7 +60,7 @@ namespace OpenRA.Mods.Common.Activities return true; return rearmable != null && rearmable.Info.RearmActors.Contains(dest.Info.Name) - && rearmable.RearmableAmmoPools.Any(p => !p.FullAmmo()); + && rearmable.RearmableAmmoPools.Any(p => !p.HasFullAmmo); } public override bool Tick(Actor self) diff --git a/OpenRA.Mods.Common/Activities/Resupply.cs b/OpenRA.Mods.Common/Activities/Resupply.cs index 5030b039e4..c3f300e859 100644 --- a/OpenRA.Mods.Common/Activities/Resupply.cs +++ b/OpenRA.Mods.Common/Activities/Resupply.cs @@ -69,7 +69,7 @@ namespace OpenRA.Mods.Common.Activities wasRepaired = true; } - var cannotRearmAtHost = rearmable == null || !rearmable.Info.RearmActors.Contains(host.Info.Name) || rearmable.RearmableAmmoPools.All(p => p.FullAmmo()); + var cannotRearmAtHost = rearmable == null || !rearmable.Info.RearmActors.Contains(host.Info.Name) || rearmable.RearmableAmmoPools.All(p => p.HasFullAmmo); if (!cannotRearmAtHost) activeResupplyTypes |= ResupplyType.Rearm; } @@ -277,7 +277,7 @@ namespace OpenRA.Mods.Common.Activities var rearmComplete = true; foreach (var ammoPool in rearmable.RearmableAmmoPools) { - if (!ammoPool.FullAmmo()) + if (!ammoPool.HasFullAmmo) { if (--ammoPool.RemainingTicks <= 0) { diff --git a/OpenRA.Mods.Common/Scripting/Properties/AmmoPoolProperties.cs b/OpenRA.Mods.Common/Scripting/Properties/AmmoPoolProperties.cs index 18b6724564..7f51b9f7c2 100644 --- a/OpenRA.Mods.Common/Scripting/Properties/AmmoPoolProperties.cs +++ b/OpenRA.Mods.Common/Scripting/Properties/AmmoPoolProperties.cs @@ -37,7 +37,7 @@ namespace OpenRA.Mods.Common.Scripting if (pool == null) throw new LuaException("Invalid ammopool name {0} queried on actor {1}.".F(poolName, self)); - return pool.GetAmmoCount(); + return pool.CurrentAmmoCount; } [Desc("Returns the maximum count of ammo the actor can load.")] diff --git a/OpenRA.Mods.Common/Traits/Air/Aircraft.cs b/OpenRA.Mods.Common/Traits/Air/Aircraft.cs index 8a5a9344a8..c48ef71192 100644 --- a/OpenRA.Mods.Common/Traits/Air/Aircraft.cs +++ b/OpenRA.Mods.Common/Traits/Air/Aircraft.cs @@ -540,7 +540,7 @@ namespace OpenRA.Mods.Common.Traits var canRearmAtActor = rearmable != null && rearmable.Info.RearmActors.Contains(a.Info.Name); var canRepairAtActor = repairable != null && repairable.Info.RepairActors.Contains(a.Info.Name); - var allowedToEnterRearmer = canRearmAtActor && (allowedToForceEnter || rearmable.RearmableAmmoPools.Any(p => !p.FullAmmo())); + var allowedToEnterRearmer = canRearmAtActor && (allowedToForceEnter || rearmable.RearmableAmmoPools.Any(p => !p.HasFullAmmo)); var allowedToEnterRepairer = canRepairAtActor && (allowedToForceEnter || self.GetDamageState() != DamageState.Undamaged); return allowedToEnterRearmer || allowedToEnterRepairer; @@ -654,7 +654,7 @@ namespace OpenRA.Mods.Common.Traits public bool CanRearmAt(Actor host) { - return rearmable != null && rearmable.Info.RearmActors.Contains(host.Info.Name) && rearmable.RearmableAmmoPools.Any(p => !p.FullAmmo()); + return rearmable != null && rearmable.Info.RearmActors.Contains(host.Info.Name) && rearmable.RearmableAmmoPools.Any(p => !p.HasFullAmmo); } public bool CanRepairAt(Actor host) diff --git a/OpenRA.Mods.Common/Traits/AmmoPool.cs b/OpenRA.Mods.Common/Traits/AmmoPool.cs index 2074c2910e..6bb1183345 100644 --- a/OpenRA.Mods.Common/Traits/AmmoPool.cs +++ b/OpenRA.Mods.Common/Traits/AmmoPool.cs @@ -67,34 +67,33 @@ namespace OpenRA.Mods.Common.Traits public int RemainingTicks; [Sync] - int currentAmmo; + public int CurrentAmmoCount { get; private set; } + + public bool HasAmmo { get { return CurrentAmmoCount > 0; } } + public bool HasFullAmmo { get { return CurrentAmmoCount == Info.Ammo; } } public AmmoPool(Actor self, AmmoPoolInfo info) { Info = info; - currentAmmo = Info.InitialAmmo < Info.Ammo && Info.InitialAmmo >= 0 ? Info.InitialAmmo : Info.Ammo; + CurrentAmmoCount = Info.InitialAmmo < Info.Ammo && Info.InitialAmmo >= 0 ? Info.InitialAmmo : Info.Ammo; } - public int GetAmmoCount() { return currentAmmo; } - public bool FullAmmo() { return currentAmmo == Info.Ammo; } - public bool HasAmmo() { return currentAmmo > 0; } - public bool GiveAmmo(Actor self, int count) { - if (currentAmmo >= Info.Ammo || count < 0) + if (CurrentAmmoCount >= Info.Ammo || count < 0) return false; - currentAmmo = (currentAmmo + count).Clamp(0, Info.Ammo); + CurrentAmmoCount = (CurrentAmmoCount + count).Clamp(0, Info.Ammo); UpdateCondition(self); return true; } public bool TakeAmmo(Actor self, int count) { - if (currentAmmo <= 0 || count < 0) + if (CurrentAmmoCount <= 0 || count < 0) return false; - currentAmmo = (currentAmmo - count).Clamp(0, Info.Ammo); + CurrentAmmoCount = (CurrentAmmoCount - count).Clamp(0, Info.Ammo); UpdateCondition(self); return true; } @@ -121,10 +120,10 @@ namespace OpenRA.Mods.Common.Traits if (conditionManager == null || string.IsNullOrEmpty(Info.AmmoCondition)) return; - while (currentAmmo > tokens.Count && tokens.Count < Info.Ammo) + while (CurrentAmmoCount > tokens.Count && tokens.Count < Info.Ammo) tokens.Push(conditionManager.GrantCondition(self, Info.AmmoCondition)); - while (currentAmmo < tokens.Count && tokens.Count > 0) + while (CurrentAmmoCount < tokens.Count && tokens.Count > 0) conditionManager.RevokeCondition(self, tokens.Pop()); } @@ -133,7 +132,7 @@ namespace OpenRA.Mods.Common.Traits var pips = Info.PipCount >= 0 ? Info.PipCount : Info.Ammo; return Enumerable.Range(0, pips).Select(i => - (currentAmmo * pips) / Info.Ammo > i ? + (CurrentAmmoCount * pips) / Info.Ammo > i ? Info.PipType : Info.PipTypeEmpty); } } diff --git a/OpenRA.Mods.Common/Traits/BotModules/Squads/States/AirStates.cs b/OpenRA.Mods.Common/Traits/BotModules/Squads/States/AirStates.cs index 2180113467..6a6a860903 100644 --- a/OpenRA.Mods.Common/Traits/BotModules/Squads/States/AirStates.cs +++ b/OpenRA.Mods.Common/Traits/BotModules/Squads/States/AirStates.cs @@ -114,13 +114,13 @@ namespace OpenRA.Mods.Common.Traits.BotModules.Squads protected static bool FullAmmo(Actor a) { var ammoPools = a.TraitsImplementing(); - return ammoPools.All(x => x.FullAmmo()); + return ammoPools.All(x => x.HasFullAmmo); } protected static bool HasAmmo(Actor a) { var ammoPools = a.TraitsImplementing(); - return ammoPools.All(x => x.HasAmmo()); + return ammoPools.All(x => x.HasAmmo); } protected static bool ReloadsAutomatically(Actor a) diff --git a/OpenRA.Mods.Common/Traits/ReloadAmmoPool.cs b/OpenRA.Mods.Common/Traits/ReloadAmmoPool.cs index 211f5ca3bd..ea0fa9f9e5 100644 --- a/OpenRA.Mods.Common/Traits/ReloadAmmoPool.cs +++ b/OpenRA.Mods.Common/Traits/ReloadAmmoPool.cs @@ -80,7 +80,7 @@ namespace OpenRA.Mods.Common.Traits protected virtual void Reload(Actor self, int reloadDelay, int reloadCount, string sound) { - if (!ammoPool.FullAmmo() && --remainingTicks == 0) + if (!ammoPool.HasFullAmmo && --remainingTicks == 0) { remainingTicks = Util.ApplyPercentageModifiers(reloadDelay, modifiers.Select(m => m.GetReloadAmmoModifier())); if (!string.IsNullOrEmpty(sound)) diff --git a/OpenRA.Mods.Common/Traits/Repairable.cs b/OpenRA.Mods.Common/Traits/Repairable.cs index f99824abcf..86e52ac797 100644 --- a/OpenRA.Mods.Common/Traits/Repairable.cs +++ b/OpenRA.Mods.Common/Traits/Repairable.cs @@ -97,7 +97,7 @@ namespace OpenRA.Mods.Common.Traits bool CanRearm() { - return rearmable != null && rearmable.RearmableAmmoPools.Any(p => !p.FullAmmo()); + return rearmable != null && rearmable.RearmableAmmoPools.Any(p => !p.HasFullAmmo); } string IOrderVoice.VoicePhraseForOrder(Actor self, Order order)