Added in code for Tech Levels using prereqs
and fixed up some kinks of the old system
This commit is contained in:
@@ -123,11 +123,12 @@ namespace OpenRA.Mods.RA
|
||||
foreach (var a in AllBuildables(Info.Type))
|
||||
{
|
||||
var bi = a.Traits.Get<BuildableInfo>();
|
||||
// Can our race build this by satisfying normal prereqs?
|
||||
// Can our race build this by satisfying normal prerequisites?
|
||||
var buildable = bi.Owner.Contains(Race.Race);
|
||||
tech.Add(a, new ProductionState { Visible = buildable && !bi.Hidden });
|
||||
// Checks if Prerequisites want to hide the Actor from buildQueue if they are false
|
||||
tech.Add(a, new ProductionState { Visible = buildable });
|
||||
if (buildable)
|
||||
ttc.Add(a.Name, bi, this);
|
||||
ttc.Add(a.Name, bi.Prerequisites, bi.BuildLimit, this);
|
||||
}
|
||||
|
||||
return tech;
|
||||
@@ -161,6 +162,18 @@ namespace OpenRA.Mods.RA
|
||||
ps.Buildable = false;
|
||||
}
|
||||
|
||||
public void PrerequisitesItemHidden(string key)
|
||||
{
|
||||
var ps = Produceable[self.World.Map.Rules.Actors[key]];
|
||||
ps.Visible = false;
|
||||
}
|
||||
|
||||
public void PrerequisitesItemVisable(string key)
|
||||
{
|
||||
var ps = Produceable[self.World.Map.Rules.Actors[key]];
|
||||
ps.Visible = true;
|
||||
}
|
||||
|
||||
public ProductionItem CurrentItem()
|
||||
{
|
||||
return Queue.ElementAtOrDefault(0);
|
||||
|
||||
51
OpenRA.Mods.RA/Player/ProvidesTechPrerequisite.cs
Normal file
51
OpenRA.Mods.RA/Player/ProvidesTechPrerequisite.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
#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 System.Text;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
using OpenRA.FileSystem;
|
||||
|
||||
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;
|
||||
this.enabled = info.Name == init.world.LobbyInfo.GlobalSettings.TechLevel;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,14 +41,14 @@ namespace OpenRA.Mods.RA
|
||||
|
||||
public void Update()
|
||||
{
|
||||
var buildables = GatherBuildables(player);
|
||||
var ownedPrerequisites = GatherOwnedPrerequisites(player);
|
||||
foreach (var w in watchers)
|
||||
w.Update(buildables);
|
||||
w.Update(ownedPrerequisites);
|
||||
}
|
||||
|
||||
public void Add(string key, BuildableInfo info, ITechTreeElement tte)
|
||||
public void Add(string key, string[] prerequisites, int limit, ITechTreeElement tte)
|
||||
{
|
||||
watchers.Add(new Watcher(key, info, tte));
|
||||
watchers.Add(new Watcher(key, prerequisites, limit, tte));
|
||||
}
|
||||
|
||||
public void Remove(string key)
|
||||
@@ -56,17 +56,17 @@ namespace OpenRA.Mods.RA
|
||||
watchers.RemoveAll(x => x.Key == key);
|
||||
}
|
||||
|
||||
static Cache<string, List<Actor>> GatherBuildables(Player player)
|
||||
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 buildables that provide prerequisites
|
||||
var prereqs = player.World.ActorsWithTrait<ITechTreePrerequisite>()
|
||||
// Add all actors that provide prerequisites
|
||||
var prerequisites = player.World.ActorsWithTrait<ITechTreePrerequisite>()
|
||||
.Where(a => a.Actor.Owner == player && !a.Actor.IsDead() && a.Actor.IsInWorld);
|
||||
|
||||
foreach (var b in prereqs)
|
||||
foreach (var b in prerequisites)
|
||||
{
|
||||
foreach (var p in b.Trait.ProvidesPrerequisites)
|
||||
{
|
||||
@@ -91,30 +91,54 @@ namespace OpenRA.Mods.RA
|
||||
{
|
||||
public readonly string Key;
|
||||
|
||||
// strings may be either actor type, or "alternate name" key
|
||||
// Strings may be either actor type, or "alternate name" key
|
||||
readonly string[] prerequisites;
|
||||
readonly ITechTreeElement watcher;
|
||||
bool hasPrerequisites;
|
||||
int buildLimit;
|
||||
int limit;
|
||||
bool hidden;
|
||||
bool initialized = false;
|
||||
|
||||
public Watcher(string key, BuildableInfo info, ITechTreeElement watcher)
|
||||
public Watcher(string key, string[] prerequisites, int limit, ITechTreeElement watcher)
|
||||
{
|
||||
this.Key = key;
|
||||
this.prerequisites = info.Prerequisites;
|
||||
this.prerequisites = prerequisites;
|
||||
this.watcher = watcher;
|
||||
this.hasPrerequisites = false;
|
||||
this.buildLimit = info.BuildLimit;
|
||||
this.limit = limit;
|
||||
this.hidden = false;
|
||||
}
|
||||
|
||||
bool HasPrerequisites(Cache<string, List<Actor>> buildables)
|
||||
bool HasPrerequisites(Cache<string, List<Actor>> ownedPrerequisites)
|
||||
{
|
||||
return prerequisites.All(p => !(p.StartsWith("!") ^ !buildables.Keys.Contains(p.Replace("!", ""))));
|
||||
return prerequisites.All(p => !(p.Replace("~", "").StartsWith("!") ^ !ownedPrerequisites.Keys.Contains(p.Replace("!", "").Replace("~", ""))));
|
||||
}
|
||||
|
||||
public void Update(Cache<string, List<Actor>> buildables)
|
||||
bool IsHidden(Cache<string, List<Actor>> ownedPrerequisites)
|
||||
{
|
||||
var hasReachedBuildLimit = buildLimit > 0 && buildables.Keys.Contains(Key) && buildables[Key].Count >= buildLimit;
|
||||
var nowHasPrerequisites = HasPrerequisites(buildables) && !hasReachedBuildLimit;
|
||||
return prerequisites.Any(prereq => prereq.StartsWith("~") && (prereq.Replace("~", "").StartsWith("!") ^ !ownedPrerequisites.Keys.Contains(prereq.Replace("~", "").Replace("!", ""))));
|
||||
}
|
||||
|
||||
public void Update(Cache<string, List<Actor>> ownedPrerequisites)
|
||||
{
|
||||
var hasReachedLimit = limit > 0 && ownedPrerequisites.Keys.Contains(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.PrerequisitesItemVisable(Key);
|
||||
|
||||
if (nowHasPrerequisites && !hasPrerequisites)
|
||||
watcher.PrerequisitesAvailable(Key);
|
||||
@@ -122,6 +146,7 @@ namespace OpenRA.Mods.RA
|
||||
if (!nowHasPrerequisites && hasPrerequisites)
|
||||
watcher.PrerequisitesUnavailable(Key);
|
||||
|
||||
hidden = nowHidden;
|
||||
hasPrerequisites = nowHasPrerequisites;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user