Sanitize ProductionQueue and fix bugs in TechTreeCache.
Gives perf win on every tick, and fixes bugs where you lose prereqs when buildings are low power.
This commit is contained in:
@@ -227,6 +227,7 @@
|
|||||||
<Compile Include="Traits\ActorStance.cs" />
|
<Compile Include="Traits\ActorStance.cs" />
|
||||||
<Compile Include="Traits\Armor.cs" />
|
<Compile Include="Traits\Armor.cs" />
|
||||||
<Compile Include="Graphics\CursorProvider.cs" />
|
<Compile Include="Graphics\CursorProvider.cs" />
|
||||||
|
<Compile Include="Traits\Player\PlayerProductionQueue.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">
|
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">
|
||||||
|
|||||||
@@ -46,8 +46,6 @@ namespace OpenRA
|
|||||||
{
|
{
|
||||||
World = world;
|
World = world;
|
||||||
Shroud = new ShroudRenderer(this, world.Map);
|
Shroud = new ShroudRenderer(this, world.Map);
|
||||||
|
|
||||||
PlayerActor = world.CreateActor("Player", new TypeDictionary{ new OwnerInit( this ) });
|
|
||||||
|
|
||||||
Index = index;
|
Index = index;
|
||||||
Palette = "player"+index;
|
Palette = "player"+index;
|
||||||
@@ -63,15 +61,14 @@ namespace OpenRA
|
|||||||
|
|
||||||
PlayerRef = pr;
|
PlayerRef = pr;
|
||||||
|
|
||||||
RegisterPlayerColor(world, Palette);
|
RegisterPlayerColor(world, Palette);
|
||||||
|
PlayerActor = world.CreateActor("Player", new TypeDictionary{ new OwnerInit( this ) });
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player( World world, Session.Client client, PlayerReference pr, int index )
|
public Player( World world, Session.Client client, PlayerReference pr, int index )
|
||||||
{
|
{
|
||||||
World = world;
|
World = world;
|
||||||
Shroud = new ShroudRenderer(this, world.Map);
|
Shroud = new ShroudRenderer(this, world.Map);
|
||||||
|
|
||||||
PlayerActor = world.CreateActor("Player", new TypeDictionary{ new OwnerInit( this ) });
|
|
||||||
|
|
||||||
Index = index;
|
Index = index;
|
||||||
Palette = "player"+index;
|
Palette = "player"+index;
|
||||||
@@ -86,7 +83,8 @@ namespace OpenRA
|
|||||||
ClientIndex = client.Index;
|
ClientIndex = client.Index;
|
||||||
PlayerRef = pr;
|
PlayerRef = pr;
|
||||||
|
|
||||||
RegisterPlayerColor(world, Palette);
|
RegisterPlayerColor(world, Palette);
|
||||||
|
PlayerActor = world.CreateActor("Player", new TypeDictionary{ new OwnerInit( this ) });
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RegisterPlayerColor(World world, string palette)
|
public void RegisterPlayerColor(World world, string palette)
|
||||||
|
|||||||
77
OpenRA.Game/Traits/Player/PlayerProductionQueue.cs
Normal file
77
OpenRA.Game/Traits/Player/PlayerProductionQueue.cs
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
#region Copyright & License Information
|
||||||
|
/*
|
||||||
|
* Copyright 2007-2010 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 LICENSE.
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using OpenRA.FileFormats;
|
||||||
|
|
||||||
|
namespace OpenRA.Traits
|
||||||
|
{
|
||||||
|
public class PlayerProductionQueueInfo : ProductionQueueInfo, ITraitPrerequisite<TechTreeCacheInfo>
|
||||||
|
{
|
||||||
|
public override object Create(ActorInitializer init) { return new PlayerProductionQueue(init.self, this); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PlayerProductionQueue : ProductionQueue
|
||||||
|
{
|
||||||
|
public PlayerProductionQueue( Actor self, PlayerProductionQueueInfo info )
|
||||||
|
: base(self, self, info as ProductionQueueInfo) {}
|
||||||
|
|
||||||
|
[Sync] bool QueueActive = true;
|
||||||
|
public override void Tick( Actor self )
|
||||||
|
{
|
||||||
|
if (self == self.Owner.PlayerActor)
|
||||||
|
QueueActive = self.World.Queries.OwnedBy[self.Owner].WithTrait<Production>()
|
||||||
|
.Where(x => x.Trait.Info.Produces.Contains(Info.Type))
|
||||||
|
.Any();
|
||||||
|
|
||||||
|
base.Tick(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
ActorInfo[] None = new ActorInfo[]{};
|
||||||
|
public override IEnumerable<ActorInfo> AllItems()
|
||||||
|
{
|
||||||
|
return QueueActive ? base.AllItems() : None;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override IEnumerable<ActorInfo> BuildableItems()
|
||||||
|
{
|
||||||
|
return QueueActive ? base.BuildableItems() : None;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void BuildUnit( string name )
|
||||||
|
{
|
||||||
|
// original ra behavior; queue lives on PlayerActor, need to find a production structure
|
||||||
|
var producers = self.World.Queries.OwnedBy[self.Owner]
|
||||||
|
.WithTrait<Production>()
|
||||||
|
.Where(x => x.Trait.Info.Produces.Contains(Info.Type))
|
||||||
|
.OrderByDescending(x => x.Actor.IsPrimaryBuilding() ? 1 : 0 ) // prioritize the primary.
|
||||||
|
.ToArray();
|
||||||
|
|
||||||
|
if (producers.Length == 0)
|
||||||
|
{
|
||||||
|
CancelProduction(name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var p in producers)
|
||||||
|
{
|
||||||
|
if (IsDisabledBuilding(p.Actor)) continue;
|
||||||
|
|
||||||
|
if (p.Trait.Produce(p.Actor, Rules.Info[ name ]))
|
||||||
|
{
|
||||||
|
FinishProduction();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,7 +20,7 @@ namespace OpenRA.Traits
|
|||||||
public readonly string Type = null;
|
public readonly string Type = null;
|
||||||
public float BuildSpeed = 0.4f;
|
public float BuildSpeed = 0.4f;
|
||||||
public readonly int LowPowerSlowdown = 3;
|
public readonly int LowPowerSlowdown = 3;
|
||||||
public object Create(ActorInitializer init) { return new ProductionQueue(init.self, this); }
|
public virtual object Create(ActorInitializer init) { return new ProductionQueue(init.self, init.self.Owner.PlayerActor, this); }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class ProductionQueue : IResolveOrder, ITick, ITechTreeElement
|
public class ProductionQueue : IResolveOrder, ITick, ITechTreeElement
|
||||||
@@ -35,18 +35,12 @@ namespace OpenRA.Traits
|
|||||||
// A list of things we could possibly build, even if our race doesn't normally get it
|
// A list of things we could possibly build, even if our race doesn't normally get it
|
||||||
Dictionary<ActorInfo, ProductionState> Produceable = new Dictionary<ActorInfo, ProductionState>();
|
Dictionary<ActorInfo, ProductionState> Produceable = new Dictionary<ActorInfo, ProductionState>();
|
||||||
|
|
||||||
public ProductionQueue( Actor self, ProductionQueueInfo info )
|
public ProductionQueue( Actor self, Actor playerActor, ProductionQueueInfo info )
|
||||||
{
|
{
|
||||||
this.self = self;
|
this.self = self;
|
||||||
this.Info = info;
|
this.Info = info;
|
||||||
}
|
|
||||||
|
var ttc = playerActor.Trait<TechTreeCache>();
|
||||||
// Trait initialization bites us when queue lives on PlayerActor; delay init until first tick
|
|
||||||
bool initialized = false;
|
|
||||||
void Initialize()
|
|
||||||
{
|
|
||||||
initialized = true;
|
|
||||||
var ttc = self.Owner.PlayerActor.Trait<TechTreeCache>();
|
|
||||||
foreach (var a in Rules.TechTree.AllBuildables(Info.Type))
|
foreach (var a in Rules.TechTree.AllBuildables(Info.Type))
|
||||||
{
|
{
|
||||||
var bi = a.Traits.Get<BuildableInfo>();
|
var bi = a.Traits.Get<BuildableInfo>();
|
||||||
@@ -88,23 +82,16 @@ namespace OpenRA.Traits
|
|||||||
return Queue;
|
return Queue;
|
||||||
}
|
}
|
||||||
|
|
||||||
ActorInfo[] None = new ActorInfo[]{};
|
public virtual IEnumerable<ActorInfo> AllItems()
|
||||||
public IEnumerable<ActorInfo> AllItems()
|
{
|
||||||
{
|
|
||||||
if (!QueueActive)
|
|
||||||
return None;
|
|
||||||
|
|
||||||
if (Game.LobbyInfo.GlobalSettings.AllowCheats && self.Owner.PlayerActor.Trait<DeveloperMode>().AllTech)
|
if (Game.LobbyInfo.GlobalSettings.AllowCheats && self.Owner.PlayerActor.Trait<DeveloperMode>().AllTech)
|
||||||
return Produceable.Select(a => a.Key);
|
return Produceable.Select(a => a.Key);
|
||||||
|
|
||||||
return Produceable.Where(a => a.Value.Buildable || a.Value.Visible).Select(a => a.Key);
|
return Produceable.Where(a => a.Value.Buildable || a.Value.Visible).Select(a => a.Key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<ActorInfo> BuildableItems()
|
public virtual IEnumerable<ActorInfo> BuildableItems()
|
||||||
{
|
{
|
||||||
if (!QueueActive)
|
|
||||||
return None;
|
|
||||||
|
|
||||||
if (Game.LobbyInfo.GlobalSettings.AllowCheats && self.Owner.PlayerActor.Trait<DeveloperMode>().AllTech)
|
if (Game.LobbyInfo.GlobalSettings.AllowCheats && self.Owner.PlayerActor.Trait<DeveloperMode>().AllTech)
|
||||||
return Produceable.Select(a => a.Key);
|
return Produceable.Select(a => a.Key);
|
||||||
|
|
||||||
@@ -117,17 +104,8 @@ namespace OpenRA.Traits
|
|||||||
return Rules.TechTree.CanBuild(actor, self.Owner, buildings);
|
return Rules.TechTree.CanBuild(actor, self.Owner, buildings);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Sync] bool QueueActive = true;
|
public virtual void Tick( Actor self )
|
||||||
public void Tick( Actor self )
|
{
|
||||||
{
|
|
||||||
if (!initialized)
|
|
||||||
Initialize();
|
|
||||||
|
|
||||||
if (self == self.Owner.PlayerActor)
|
|
||||||
QueueActive = self.World.Queries.OwnedBy[self.Owner].WithTrait<Production>()
|
|
||||||
.Where(x => x.Trait.Info.Produces.Contains(Info.Type))
|
|
||||||
.Any();
|
|
||||||
|
|
||||||
while( Queue.Count > 0 && !BuildableItems().Any(b => b.Name == Queue[ 0 ].Item) )
|
while( Queue.Count > 0 && !BuildableItems().Any(b => b.Name == Queue[ 0 ].Item) )
|
||||||
{
|
{
|
||||||
self.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(Queue[0].TotalCost - Queue[0].RemainingCost); // refund what's been paid so far.
|
self.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(Queue[0].TotalCost - Queue[0].RemainingCost); // refund what's been paid so far.
|
||||||
@@ -204,7 +182,7 @@ namespace OpenRA.Traits
|
|||||||
return (int) time;
|
return (int) time;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CancelProduction( string itemName )
|
protected void CancelProduction( string itemName )
|
||||||
{
|
{
|
||||||
if (Queue.Count == 0)
|
if (Queue.Count == 0)
|
||||||
return; // Nothing to do here
|
return; // Nothing to do here
|
||||||
@@ -226,52 +204,23 @@ namespace OpenRA.Traits
|
|||||||
Queue.RemoveAt(0);
|
Queue.RemoveAt(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void BeginProduction( ProductionItem item )
|
protected void BeginProduction( ProductionItem item )
|
||||||
{
|
{
|
||||||
Queue.Add(item);
|
Queue.Add(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool IsDisabledBuilding(Actor a)
|
protected static bool IsDisabledBuilding(Actor a)
|
||||||
{
|
{
|
||||||
var building = a.TraitOrDefault<Building>();
|
var building = a.TraitOrDefault<Building>();
|
||||||
return building != null && building.Disabled;
|
return building != null && building.Disabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BuildUnit( string name )
|
protected virtual void BuildUnit( string name )
|
||||||
{
|
{
|
||||||
if (self == self.Owner.PlayerActor)
|
// queue lives on actor; is produced at same actor
|
||||||
{
|
var sp = self.TraitsImplementing<Production>().Where(p => p.Info.Produces.Contains(Info.Type)).FirstOrDefault();
|
||||||
// original ra behavior; queue lives on PlayerActor, need to find a production structure
|
if (sp != null && !IsDisabledBuilding(self) && sp.Produce(self, Rules.Info[ name ]))
|
||||||
var producers = self.World.Queries.OwnedBy[self.Owner]
|
FinishProduction();
|
||||||
.WithTrait<Production>()
|
|
||||||
.Where(x => x.Trait.Info.Produces.Contains(Info.Type))
|
|
||||||
.OrderByDescending(x => x.Actor.IsPrimaryBuilding() ? 1 : 0 ) // prioritize the primary.
|
|
||||||
.ToArray();
|
|
||||||
|
|
||||||
if (producers.Length == 0)
|
|
||||||
{
|
|
||||||
CancelProduction(name);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (var p in producers)
|
|
||||||
{
|
|
||||||
if (IsDisabledBuilding(p.Actor)) continue;
|
|
||||||
|
|
||||||
if (p.Trait.Produce(p.Actor, Rules.Info[ name ]))
|
|
||||||
{
|
|
||||||
FinishProduction();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// queue lives on actor; is produced at same actor
|
|
||||||
var sp = self.TraitsImplementing<Production>().Where(p => p.Info.Produces.Contains(Info.Type)).FirstOrDefault();
|
|
||||||
if (sp != null && !IsDisabledBuilding(self) && sp.Produce(self, Rules.Info[ name ]))
|
|
||||||
FinishProduction();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,10 +15,45 @@ using OpenRA.GameRules;
|
|||||||
|
|
||||||
namespace OpenRA.Traits
|
namespace OpenRA.Traits
|
||||||
{
|
{
|
||||||
class TechTreeCacheInfo : TraitInfo<TechTreeCache> { }
|
class TechTreeCacheInfo : ITraitInfo
|
||||||
|
|
||||||
class TechTreeCache : ITick
|
|
||||||
{
|
{
|
||||||
|
public object Create(ActorInitializer init) { return new TechTreeCache(init);}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TechTreeCache
|
||||||
|
{
|
||||||
|
readonly List<Watcher> watchers = new List<Watcher>();
|
||||||
|
readonly Player player;
|
||||||
|
public TechTreeCache(ActorInitializer init)
|
||||||
|
{
|
||||||
|
player = init.self.Owner;
|
||||||
|
init.world.ActorAdded += ActorChanged;
|
||||||
|
init.world.ActorRemoved += ActorChanged;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ActorChanged(Actor a)
|
||||||
|
{
|
||||||
|
if (a.Owner == player && a.HasTrait<Building>())
|
||||||
|
Update();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update()
|
||||||
|
{
|
||||||
|
var buildings = Rules.TechTree.GatherBuildings(player);
|
||||||
|
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 ));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Remove(string key)
|
||||||
|
{
|
||||||
|
watchers.RemoveAll(x => x.key == key);
|
||||||
|
}
|
||||||
|
|
||||||
class Watcher
|
class Watcher
|
||||||
{
|
{
|
||||||
public readonly string key;
|
public readonly string key;
|
||||||
@@ -35,10 +70,16 @@ namespace OpenRA.Traits
|
|||||||
this.hasPrerequisites = false;
|
this.hasPrerequisites = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Tick( Player owner, Cache<string, List<Actor>> buildings )
|
public void Update(Cache<string, List<Actor>> buildings)
|
||||||
{
|
{
|
||||||
var nowHasPrerequisites = prerequisites.All( a => buildings[ a ].Any( b => !b.Trait<Building>().Disabled ) );
|
var nowHasPrerequisites = true;
|
||||||
|
foreach (var p in prerequisites)
|
||||||
|
if (!buildings.Keys.Contains(p))
|
||||||
|
{
|
||||||
|
nowHasPrerequisites = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if( nowHasPrerequisites && !hasPrerequisites )
|
if( nowHasPrerequisites && !hasPrerequisites )
|
||||||
watcher.PrerequisitesAvailable(key);
|
watcher.PrerequisitesAvailable(key);
|
||||||
|
|
||||||
@@ -48,26 +89,6 @@ namespace OpenRA.Traits
|
|||||||
hasPrerequisites = nowHasPrerequisites;
|
hasPrerequisites = nowHasPrerequisites;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
readonly List<Watcher> watchers = new List<Watcher>();
|
|
||||||
|
|
||||||
public void Tick( Actor self )
|
|
||||||
{
|
|
||||||
var buildings = Rules.TechTree.GatherBuildings( self.Owner );
|
|
||||||
|
|
||||||
foreach( var w in watchers )
|
|
||||||
w.Tick( self.Owner, buildings );
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Add( string key, List<string> prerequisites, ITechTreeElement tte )
|
|
||||||
{
|
|
||||||
watchers.Add( new Watcher( key, prerequisites, tte ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Remove( string key )
|
|
||||||
{
|
|
||||||
watchers.RemoveAll( x => x.key == key );
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ITechTreeElement
|
interface ITechTreeElement
|
||||||
|
|||||||
@@ -1,26 +1,26 @@
|
|||||||
Player:
|
Player:
|
||||||
TechTreeCache:
|
TechTreeCache:
|
||||||
ProductionQueue@Building:
|
PlayerProductionQueue@Building:
|
||||||
Type: Building
|
Type: Building
|
||||||
BuildSpeed: .4
|
BuildSpeed: .4
|
||||||
LowPowerSlowdown: 3
|
LowPowerSlowdown: 3
|
||||||
ProductionQueue@Defense:
|
PlayerProductionQueue@Defense:
|
||||||
Type: Defense
|
Type: Defense
|
||||||
BuildSpeed: .4
|
BuildSpeed: .4
|
||||||
LowPowerSlowdown: 3
|
LowPowerSlowdown: 3
|
||||||
ProductionQueue@Vehicle:
|
PlayerProductionQueue@Vehicle:
|
||||||
Type: Vehicle
|
Type: Vehicle
|
||||||
BuildSpeed: .4
|
BuildSpeed: .4
|
||||||
LowPowerSlowdown: 3
|
LowPowerSlowdown: 3
|
||||||
ProductionQueue@Infantry:
|
PlayerProductionQueue@Infantry:
|
||||||
Type: Infantry
|
Type: Infantry
|
||||||
BuildSpeed: .4
|
BuildSpeed: .4
|
||||||
LowPowerSlowdown: 3
|
LowPowerSlowdown: 3
|
||||||
ProductionQueue@Ship:
|
PlayerProductionQueue@Ship:
|
||||||
Type: Ship
|
Type: Ship
|
||||||
BuildSpeed: .4
|
BuildSpeed: .4
|
||||||
LowPowerSlowdown: 3
|
LowPowerSlowdown: 3
|
||||||
ProductionQueue@Plane:
|
PlayerProductionQueue@Plane:
|
||||||
Type: Plane
|
Type: Plane
|
||||||
BuildSpeed: .4
|
BuildSpeed: .4
|
||||||
LowPowerSlowdown: 3
|
LowPowerSlowdown: 3
|
||||||
|
|||||||
Reference in New Issue
Block a user