Move Upgrades and Prerequisites
This commit is contained in:
@@ -12,6 +12,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Mods.Common;
|
||||
using OpenRA.Mods.Common.Power;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Mods.RA.Buildings;
|
||||
using OpenRA.Mods.RA.Traits;
|
||||
using OpenRA.Traits;
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
#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;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.RA
|
||||
{
|
||||
[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) { }
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
using System.Linq;
|
||||
using OpenRA.Effects;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Mods.RA.Buildings;
|
||||
using OpenRA.Mods.RA.Traits;
|
||||
using OpenRA.Primitives;
|
||||
|
||||
@@ -13,6 +13,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Mods.Common;
|
||||
using OpenRA.Mods.Common.Power;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Mods.RA.Buildings;
|
||||
using OpenRA.Traits;
|
||||
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
#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.RA
|
||||
{
|
||||
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; }
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
#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.RA
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,172 +0,0 @@
|
||||
#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.RA
|
||||
{
|
||||
[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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user