StateBase.cs modified:

1. Optimize & move "ammo" related function from "AirStates.cs" to StateBase.cs

2. Optimize & move "IsRearm" function from "AirStates.cs" to StateBase.cs, name changed to "IsRearming"

(optimized by reaperrr)
This commit is contained in:
dnqbob
2020-04-26 22:58:40 +08:00
committed by abcdefg30
parent 3df43529a6
commit 2d7790f5e4
2 changed files with 56 additions and 48 deletions

View File

@@ -111,49 +111,6 @@ namespace OpenRA.Mods.Common.Traits.BotModules.Squads
return false; return false;
} }
protected static bool FullAmmo(Actor a)
{
var ammoPools = a.TraitsImplementing<AmmoPool>();
return ammoPools.All(x => x.HasFullAmmo);
}
protected static bool HasAmmo(Actor a)
{
var ammoPools = a.TraitsImplementing<AmmoPool>();
return ammoPools.All(x => x.HasAmmo);
}
protected static bool ReloadsAutomatically(Actor a)
{
var ammoPools = a.TraitsImplementing<AmmoPool>();
var rearmable = a.TraitOrDefault<Rearmable>();
if (rearmable == null)
return true;
return ammoPools.All(ap => !rearmable.Info.AmmoPools.Contains(ap.Info.Name));
}
protected static bool IsRearm(Actor a)
{
if (a.IsIdle)
return false;
var activity = a.CurrentActivity;
var type = activity.GetType();
if (type == typeof(Resupply))
return true;
var next = activity.NextActivity;
if (next == null)
return false;
var nextType = next.GetType();
if (nextType == typeof(Resupply))
return true;
return false;
}
// Checks the number of anti air enemies around units // Checks the number of anti air enemies around units
protected virtual bool ShouldFlee(Squad owner) protected virtual bool ShouldFlee(Squad owner)
{ {
@@ -220,12 +177,13 @@ namespace OpenRA.Mods.Common.Traits.BotModules.Squads
if (BusyAttack(a)) if (BusyAttack(a))
continue; continue;
if (!ReloadsAutomatically(a)) var ammoPools = a.TraitsImplementing<AmmoPool>().ToArray();
if (!ReloadsAutomatically(ammoPools, a.TraitOrDefault<Rearmable>()))
{ {
if (IsRearm(a)) if (IsRearming(a))
continue; continue;
if (!HasAmmo(a)) if (!HasAmmo(ammoPools))
{ {
owner.Bot.QueueOrder(new Order("ReturnToBase", a, false)); owner.Bot.QueueOrder(new Order("ReturnToBase", a, false));
continue; continue;
@@ -251,9 +209,10 @@ namespace OpenRA.Mods.Common.Traits.BotModules.Squads
foreach (var a in owner.Units) foreach (var a in owner.Units)
{ {
if (!ReloadsAutomatically(a) && !FullAmmo(a)) var ammoPools = a.TraitsImplementing<AmmoPool>().ToArray();
if (!ReloadsAutomatically(ammoPools, a.TraitOrDefault<Rearmable>()) && !FullAmmo(ammoPools))
{ {
if (IsRearm(a)) if (IsRearming(a))
continue; continue;
owner.Bot.QueueOrder(new Order("ReturnToBase", a, false)); owner.Bot.QueueOrder(new Order("ReturnToBase", a, false));

View File

@@ -101,5 +101,54 @@ namespace OpenRA.Mods.Common.Traits.BotModules.Squads
return flee(enemyAroundUnit); return flee(enemyAroundUnit);
} }
protected static bool IsRearming(Actor a)
{
if (a.IsIdle)
return false;
var activity = a.CurrentActivity;
if (activity.GetType() == typeof(Resupply))
return true;
var next = activity.NextActivity;
if (next == null)
return false;
if (next.GetType() == typeof(Resupply))
return true;
return false;
}
protected static bool FullAmmo(IEnumerable<AmmoPool> ammoPools)
{
foreach (var ap in ammoPools)
if (!ap.HasFullAmmo)
return false;
return true;
}
protected static bool HasAmmo(IEnumerable<AmmoPool> ammoPools)
{
foreach (var ap in ammoPools)
if (!ap.HasAmmo)
return false;
return true;
}
protected static bool ReloadsAutomatically(IEnumerable<AmmoPool> ammoPools, Rearmable rearmable)
{
if (rearmable == null)
return true;
foreach (var ap in ammoPools)
if (!rearmable.Info.AmmoPools.Contains(ap.Info.Name))
return false;
return true;
}
} }
} }