Route the power check for support powers via the tech tree

This commit is contained in:
Paul Chote
2010-09-18 20:42:32 +12:00
parent 2a10af2007
commit 652f06f604
10 changed files with 42 additions and 45 deletions

View File

@@ -102,11 +102,13 @@ namespace OpenRA.Traits
}
}
public PowerState GetPowerState()
public PowerState PowerState
{
if (PowerProvided >= PowerDrained) return PowerState.Normal;
if (PowerProvided > PowerDrained / 2) return PowerState.Low;
return PowerState.Critical;
get {
if (PowerProvided >= PowerDrained) return PowerState.Normal;
if (PowerProvided > PowerDrained / 2) return PowerState.Low;
return PowerState.Critical;
}
}
}
}

View File

@@ -276,7 +276,7 @@ namespace OpenRA.Traits
if (Paused) return;
if (pm.GetPowerState() != PowerState.Normal)
if (pm.PowerState != PowerState.Normal)
{
if (--slowdown <= 0)
slowdown = Queue.Info.LowPowerSlowdown;

View File

@@ -14,12 +14,12 @@ using OpenRA.FileFormats;
namespace OpenRA.Traits
{
class TechTreeInfo : ITraitInfo
public class TechTreeInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new TechTree(init);}
}
class TechTree
public class TechTree
{
readonly List<Watcher> watchers = new List<Watcher>();
readonly Player player;
@@ -42,10 +42,16 @@ namespace OpenRA.Traits
foreach(var w in watchers)
w.Update(buildings);
}
public void Add(string key, List<string> prerequisites, ITechTreeElement tte)
{
watchers.Add(new Watcher( key, prerequisites, tte ));
Add(key, prerequisites, false, tte);
}
// set requiresPowered = true to discard buildings that have an IDisabled active (eg manually powered down)
public void Add(string key, List<string> prerequisites, bool requiresPowered, ITechTreeElement tte)
{
watchers.Add(new Watcher( key, prerequisites, requiresPowered, tte ));
}
public void Remove(string key)
@@ -53,7 +59,7 @@ namespace OpenRA.Traits
watchers.RemoveAll(x => x.key == key);
}
public static Cache<string, List<Actor>> GatherBuildings( Player player )
static Cache<string, List<Actor>> GatherBuildings( Player player )
{
var ret = new Cache<string, List<Actor>>( x => new List<Actor>() );
if (player == null)
@@ -77,25 +83,28 @@ namespace OpenRA.Traits
public readonly List<string> prerequisites;
public readonly ITechTreeElement watcher;
bool hasPrerequisites;
public Watcher(string key, List<string> prerequisites, ITechTreeElement watcher)
bool requiresPowered;
public Watcher(string key, List<string> prerequisites, bool requiresPowered, ITechTreeElement watcher)
{
this.key = key;
this.prerequisites = prerequisites;
this.watcher = watcher;
this.hasPrerequisites = false;
this.requiresPowered = requiresPowered;
}
public void Update(Cache<string, List<Actor>> buildings)
{
{
var nowHasPrerequisites = true;
foreach (var p in prerequisites)
if (!buildings.Keys.Contains(p))
if (!buildings.Keys.Contains(p) ||
(requiresPowered && buildings[p].All(b => b.TraitsImplementing<IDisable>().Any(d => d.Disabled))))
{
nowHasPrerequisites = false;
break;
}
if( nowHasPrerequisites && !hasPrerequisites )
watcher.PrerequisitesAvailable(key);
@@ -107,7 +116,7 @@ namespace OpenRA.Traits
}
}
interface ITechTreeElement
public interface ITechTreeElement
{
void PrerequisitesAvailable(string key);
void PrerequisitesUnavailable(string key);

View File

@@ -47,7 +47,6 @@ namespace OpenRA.Traits
protected readonly Actor Self;
protected readonly Player Owner;
string[] effectivePrereq = {};
bool notifiedCharging;
bool notifiedReady;
@@ -60,9 +59,6 @@ namespace OpenRA.Traits
Self = self;
Owner = self.Owner;
PlayerPower = self.Trait<PowerManager>();
effectivePrereq = Info.Prerequisites
.Select(a => a.ToLowerInvariant()).ToArray();
self.Trait<TechTree>().Add( Info.OrderName, Info.Prerequisites.Select( a => a.ToLowerInvariant() ).ToList(), this );
}
@@ -74,8 +70,8 @@ namespace OpenRA.Traits
if (Info.GivenAuto)
IsAvailable = hasPrerequisites;
if (IsAvailable && (!Info.RequiresPower || IsPowered()))
if (IsAvailable && (!Info.RequiresPower || PlayerPower.PowerState == PowerState.Normal))
{
if (Game.LobbyInfo.GlobalSettings.AllowCheats && self.Trait<DeveloperMode>().FastCharge) RemainingTime = 0;
@@ -97,16 +93,6 @@ namespace OpenRA.Traits
}
}
bool IsPowered()
{
if (effectivePrereq.Count() == 0)
return PlayerPower.GetPowerState() == PowerState.Normal;
// Takes 0.3ms on pchote's machine -- calling it every tick for every active special power is retarded
var buildings = TechTree.GatherBuildings(Owner);
return effectivePrereq.All(a => buildings[a].Any(b => !b.TraitsImplementing<IDisable>().All(d => d.Disabled)));
}
public void FinishActivate()
{
if (Info.OneShot)
@@ -135,7 +121,7 @@ namespace OpenRA.Traits
if (!IsAvailable || !IsReady)
return;
if (Info.RequiresPower && !IsPowered())
if (Info.RequiresPower && PlayerPower.PowerState != PowerState.Normal)
{
var eva = Owner.World.WorldActor.Info.Traits.Get<EvaAlertsInfo>();
Sound.Play(eva.AbilityInsufficientPower);