Move Upgrades and Prerequisites

This commit is contained in:
penev92
2014-12-10 00:07:07 +02:00
parent af0be2f592
commit ce2c536a1a
24 changed files with 40 additions and 32 deletions

View File

@@ -37,4 +37,17 @@ namespace OpenRA.Mods.Common
void MovementCancelled(Actor self);
void Harvested(Actor self, ResourceType resource);
}
public interface ITechTreePrerequisite
{
IEnumerable<string> ProvidesPrerequisites { get; }
}
public interface ITechTreeElement
{
void PrerequisitesAvailable (string key);
void PrerequisitesUnavailable (string key);
void PrerequisitesItemHidden (string key);
void PrerequisitesItemVisible (string key);
}
}

View File

@@ -108,11 +108,13 @@
<Compile Include="Scripting\Properties\ResourceProperties.cs" />
<Compile Include="Scripting\Properties\UpgradeProperties.cs" />
<Compile Include="Traits\BlocksBullets.cs" />
<Compile Include="Traits\Buildable.cs" />
<Compile Include="Traits\Buildings\DeadBuildingState.cs" />
<Compile Include="Traits\Burns.cs" />
<Compile Include="Traits\CustomBuildTimeValue.cs" />
<Compile Include="Traits\CustomSellValue.cs" />
<Compile Include="Traits\Demolishable.cs" />
<Compile Include="Traits\GlobalUpgradable.cs" />
<Compile Include="Traits\Immobile.cs" />
<Compile Include="Traits\JamsMissiles.cs" />
<Compile Include="Traits\Modifiers\DisabledOverlay.cs" />
@@ -123,6 +125,10 @@
<Compile Include="Traits\PaletteEffects\MenuPaletteEffect.cs" />
<Compile Include="Traits\PaletteEffects\NukePaletteEffect.cs" />
<Compile Include="Traits\PaletteEffects\WaterPaletteRotation.cs" />
<Compile Include="Traits\Player\GlobalUpgradeManager.cs" />
<Compile Include="Traits\Player\ProvidesCustomPrerequisite.cs" />
<Compile Include="Traits\Player\ProvidesTechPrerequisite.cs" />
<Compile Include="Traits\Player\TechTree.cs" />
<Compile Include="Traits\Power\AffectedByPowerOutage.cs" />
<Compile Include="Traits\Power\CanPowerDown.cs" />
<Compile Include="Traits\Power\Player\PowerManager.cs" />

View File

@@ -0,0 +1,44 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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 OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class BuildableInfo : TraitInfo<Buildable>
{
[Desc("The prerequisite names that must be available before this can be built.",
"This can be prefixed with ! to invert the prerequisite (disabling production if the prerequisite is available)",
"and/or ~ to hide the actor from the production palette if the prerequisite is not available.",
"Prerequisites are granted by actors with the Building trait (with a prerequisite string given by the lower case actor name)",
"and by the ProvidesCustomPrerequisite trait.")]
public readonly string[] Prerequisites = { };
[Desc("Restrict production to a specific race(s). **Deprecated**: Use race-specific prerequisites instead.")]
public readonly string[] Owner = { };
[Desc("Production queue(s) that can produce this.")]
public readonly string[] Queue = { };
[Desc("Override the production structure type (from the Production Produces list) that this unit should be built at.")]
public readonly string BuildAtProductionType = null;
[Desc("Disable production when there are more than this many of this actor on the battlefield. Set to 0 to disable.")]
public readonly int BuildLimit = 0;
[Desc("What the unit should start doing. Warning: If this is not a harvester", "it will break if you use FindResources.")]
public readonly string InitialActivity = null;
// TODO: UI fluff; doesn't belong here
public readonly int BuildPaletteOrder = 9999;
}
public class Buildable { }
}

View File

@@ -0,0 +1,65 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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
{
public class GlobalUpgradableInfo : ITraitInfo, Requires<UpgradeManagerInfo>
{
public readonly string[] Upgrades = { };
public readonly string[] Prerequisites = { };
public object Create(ActorInitializer init) { return new GlobalUpgradable(init.self, this); }
}
public class GlobalUpgradable : INotifyAddedToWorld, INotifyRemovedFromWorld
{
readonly GlobalUpgradableInfo info;
readonly GlobalUpgradeManager globalManager;
readonly UpgradeManager manager;
bool wasAvailable;
public GlobalUpgradable(Actor self, GlobalUpgradableInfo info)
{
this.info = info;
globalManager = self.Owner.PlayerActor.Trait<GlobalUpgradeManager>();
manager = self.Trait<UpgradeManager>();
}
public void AddedToWorld(Actor self)
{
if (info.Prerequisites.Any())
globalManager.Register(self, this, info.Prerequisites);
}
public void RemovedFromWorld(Actor self)
{
if (info.Prerequisites.Any())
globalManager.Unregister(self, this, info.Prerequisites);
}
public void PrerequisitesUpdated(Actor self, bool available)
{
if (available == wasAvailable)
return;
if (available)
foreach (var u in info.Upgrades)
manager.GrantUpgrade(self, u, this);
else
foreach (var u in info.Upgrades)
manager.RevokeUpgrade(self, u, this);
wasAvailable = available;
}
}
}

View File

@@ -0,0 +1,92 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Collections.Generic;
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Attach this to the player actor.")]
public class GlobalUpgradeManagerInfo : ITraitInfo, Requires<TechTreeInfo>
{
public object Create(ActorInitializer init) { return new GlobalUpgradeManager(init); }
}
public class GlobalUpgradeManager : ITechTreeElement
{
readonly Actor self;
readonly Dictionary<string, List<Pair<Actor, GlobalUpgradable>>> upgradables = new Dictionary<string, List<Pair<Actor, GlobalUpgradable>>>();
readonly TechTree techTree;
public GlobalUpgradeManager(ActorInitializer init)
{
self = init.self;
techTree = self.Trait<TechTree>();
}
static string MakeKey(string[] prerequisites)
{
return "upgrade_" + string.Join("_", prerequisites.OrderBy(a => a));
}
public void Register(Actor actor, GlobalUpgradable u, string[] prerequisites)
{
var key = MakeKey(prerequisites);
if (!upgradables.ContainsKey(key))
{
upgradables.Add(key, new List<Pair<Actor, GlobalUpgradable>>());
techTree.Add(key, prerequisites, 0, this);
}
upgradables[key].Add(Pair.New(actor, u));
// Notify the current state
u.PrerequisitesUpdated(actor, techTree.HasPrerequisites(prerequisites));
}
public void Unregister(Actor actor, GlobalUpgradable u, string[] prerequisites)
{
var key = MakeKey(prerequisites);
var list = upgradables[key];
list.RemoveAll(x => x.First == actor && x.Second == u);
if (!list.Any())
{
upgradables.Remove(key);
techTree.Remove(key);
}
}
public void PrerequisitesAvailable(string key)
{
List<Pair<Actor, GlobalUpgradable>> list;
if (!upgradables.TryGetValue(key, out list))
return;
foreach (var u in list)
u.Second.PrerequisitesUpdated(u.First, true);
}
public void PrerequisitesUnavailable(string key)
{
List<Pair<Actor, GlobalUpgradable>> list;
if (!upgradables.TryGetValue(key, out list))
return;
foreach (var u in list)
u.Second.PrerequisitesUpdated(u.First, false);
}
public void PrerequisitesItemHidden(string key) { }
public void PrerequisitesItemVisible(string key) { }
}
}

View File

@@ -0,0 +1,87 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Collections.Generic;
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class ProvidesCustomPrerequisiteInfo : ITraitInfo
{
[Desc("The prerequisite type that this provides")]
public readonly string Prerequisite = null;
[Desc("Only grant this prerequisite when you have these prerequisites")]
public readonly string[] RequiresPrerequisites = { };
[Desc("Only grant this prerequisite for certain factions")]
public readonly string[] Race = { };
[Desc("Should it recheck everything when it is captured?")]
public readonly bool ResetOnOwnerChange = false;
public object Create(ActorInitializer init) { return new ProvidesCustomPrerequisite(init, this); }
}
public class ProvidesCustomPrerequisite : ITechTreePrerequisite, INotifyOwnerChanged
{
readonly ProvidesCustomPrerequisiteInfo info;
bool enabled = true;
public ProvidesCustomPrerequisite(ActorInitializer init, ProvidesCustomPrerequisiteInfo info)
{
this.info = info;
var race = init.Contains<RaceInit>() ? init.Get<RaceInit, string>() : init.self.Owner.Country.Race;
Update(init.self.Owner, race);
}
public IEnumerable<string> ProvidesPrerequisites
{
get
{
if (!enabled)
yield break;
yield return info.Prerequisite;
}
}
public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
{
if (info.ResetOnOwnerChange)
Update(newOwner, newOwner.Country.Race);
}
void Update(Player owner, string race)
{
enabled = true;
if (info.Race.Any())
enabled = info.Race.Contains(race);
if (info.RequiresPrerequisites.Any() && enabled)
enabled = owner.PlayerActor.Trait<TechTree>().HasPrerequisites(info.RequiresPrerequisites);
}
}
// Allows maps / transformations to specify the race variant of an actor.
public class RaceInit : IActorInit<string>
{
[FieldFromYamlKey] public readonly string Race;
public RaceInit() { }
public RaceInit(string race) { Race = race; }
public string Value(World world) { return Race; }
}
}

View File

@@ -0,0 +1,47 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Collections.Generic;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class ProvidesTechPrerequisiteInfo : ITraitInfo
{
public readonly string Name;
public readonly string[] Prerequisites = {};
public object Create(ActorInitializer init) { return new ProvidesTechPrerequisite(this, init); }
}
public class ProvidesTechPrerequisite : ITechTreePrerequisite
{
ProvidesTechPrerequisiteInfo info;
bool enabled;
static readonly string[] NoPrerequisites = new string[0];
public string Name { get { return info.Name; } }
public IEnumerable<string> ProvidesPrerequisites
{
get
{
return enabled ? info.Prerequisites : NoPrerequisites;
}
}
public ProvidesTechPrerequisite(ProvidesTechPrerequisiteInfo info, ActorInitializer init)
{
this.info = info;
var tech = init.world.Map.Options.TechLevel ?? init.world.LobbyInfo.GlobalSettings.TechLevel;
this.enabled = info.Name == tech;
}
}
}

View File

@@ -0,0 +1,172 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.Collections.Generic;
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Manages build limits and pre-requisites.", " Attach this to the player actor.")]
public class TechTreeInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new TechTree(init); }
}
public class TechTree
{
readonly List<Watcher> watchers = new List<Watcher>();
readonly Player player;
public TechTree(ActorInitializer init)
{
player = init.self.Owner;
init.world.ActorAdded += ActorChanged;
init.world.ActorRemoved += ActorChanged;
}
public void ActorChanged(Actor a)
{
var bi = a.Info.Traits.GetOrDefault<BuildableInfo>();
if (a.Owner == player && (a.HasTrait<ITechTreePrerequisite>() || (bi != null && bi.BuildLimit > 0)))
Update();
}
public void Update()
{
var ownedPrerequisites = GatherOwnedPrerequisites(player);
foreach (var w in watchers)
w.Update(ownedPrerequisites);
}
public void Add(string key, string[] prerequisites, int limit, ITechTreeElement tte)
{
watchers.Add(new Watcher(key, prerequisites, limit, tte));
}
public void Remove(string key)
{
watchers.RemoveAll(x => x.Key == key);
}
public void Remove(ITechTreeElement tte)
{
watchers.RemoveAll(x => x.RegisteredBy == tte);
}
public bool HasPrerequisites(IEnumerable<string> prerequisites)
{
var ownedPrereqs = TechTree.GatherOwnedPrerequisites(player);
return prerequisites.All(p => !(p.Replace("~", "").StartsWith("!")
^ !ownedPrereqs.ContainsKey(p.Replace("!", "").Replace("~", ""))));
}
static Cache<string, List<Actor>> GatherOwnedPrerequisites(Player player)
{
var ret = new Cache<string, List<Actor>>(x => new List<Actor>());
if (player == null)
return ret;
// Add all actors that provide prerequisites
var prerequisites = player.World.ActorsWithTrait<ITechTreePrerequisite>()
.Where(a => a.Actor.Owner == player && a.Actor.IsInWorld && !a.Actor.IsDead);
foreach (var b in prerequisites)
{
foreach (var p in b.Trait.ProvidesPrerequisites)
{
// Ignore bogus prerequisites
if (p == null)
continue;
ret[p].Add(b.Actor);
}
}
// Add buildables that have a build limit set and are not already in the list
player.World.ActorsWithTrait<Buildable>()
.Where(a =>
a.Actor.Owner == player &&
a.Actor.IsInWorld &&
!a.Actor.IsDead &&
!ret.ContainsKey(a.Actor.Info.Name) &&
a.Actor.Info.Traits.Get<BuildableInfo>().BuildLimit > 0)
.Do(b => ret[b.Actor.Info.Name].Add(b.Actor));
return ret;
}
class Watcher
{
public readonly string Key;
public ITechTreeElement RegisteredBy { get { return watcher; } }
// Strings may be either actor type, or "alternate name" key
readonly string[] prerequisites;
readonly ITechTreeElement watcher;
bool hasPrerequisites;
int limit;
bool hidden;
bool initialized = false;
public Watcher(string key, string[] prerequisites, int limit, ITechTreeElement watcher)
{
this.Key = key;
this.prerequisites = prerequisites;
this.watcher = watcher;
this.hasPrerequisites = false;
this.limit = limit;
this.hidden = false;
}
bool HasPrerequisites(Cache<string, List<Actor>> ownedPrerequisites)
{
return prerequisites.All(p => !(p.Replace("~", "").StartsWith("!") ^ !ownedPrerequisites.ContainsKey(p.Replace("!", "").Replace("~", ""))));
}
bool IsHidden(Cache<string, List<Actor>> ownedPrerequisites)
{
return prerequisites.Any(prereq => prereq.StartsWith("~") && (prereq.Replace("~", "").StartsWith("!") ^ !ownedPrerequisites.ContainsKey(prereq.Replace("~", "").Replace("!", ""))));
}
public void Update(Cache<string, List<Actor>> ownedPrerequisites)
{
var hasReachedLimit = limit > 0 && ownedPrerequisites.ContainsKey(Key) && ownedPrerequisites[Key].Count >= limit;
// The '!' annotation inverts prerequisites: "I'm buildable if this prerequisite *isn't* met"
var nowHasPrerequisites = HasPrerequisites(ownedPrerequisites) && !hasReachedLimit;
var nowHidden = IsHidden(ownedPrerequisites);
if (initialized == false)
{
initialized = true;
hasPrerequisites = !nowHasPrerequisites;
hidden = !nowHidden;
}
// Hide the item from the UI if a prereq annotated with '~' is not met.
if (nowHidden && !hidden)
watcher.PrerequisitesItemHidden(Key);
if (!nowHidden && hidden)
watcher.PrerequisitesItemVisible(Key);
if (nowHasPrerequisites && !hasPrerequisites)
watcher.PrerequisitesAvailable(Key);
if (!nowHasPrerequisites && hasPrerequisites)
watcher.PrerequisitesUnavailable(Key);
hidden = nowHidden;
hasPrerequisites = nowHasPrerequisites;
}
}
}
}