Adds the RequiresPrerequisites field into ProvidesCustomPrerequisite and all the logic behind it.

This commit is contained in:
WolfGaming
2014-07-16 05:34:47 +00:00
parent 62820430de
commit 7b10da6bde
3 changed files with 36 additions and 13 deletions

View File

@@ -20,31 +20,30 @@ namespace OpenRA.Mods.RA
[Desc("The prerequisite type that this provides")] [Desc("The prerequisite type that this provides")]
public readonly string Prerequisite = null; 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")] [Desc("Only grant this prerequisite for certain factions")]
public readonly string[] Race = { }; public readonly string[] Race = { };
[Desc("Should the prerequisite remain enabled if the owner changes?")] [Desc("Should it recheck everything when it is captured?")]
public readonly bool Sticky = true; public readonly bool ResetOnOwnerChange = false;
public object Create(ActorInitializer init) { return new ProvidesCustomPrerequisite(init, this); } public object Create(ActorInitializer init) { return new ProvidesCustomPrerequisite(init, this); }
} }
public class ProvidesCustomPrerequisite : ITechTreePrerequisite, INotifyOwnerChanged public class ProvidesCustomPrerequisite : ITechTreePrerequisite, INotifyOwnerChanged
{ {
ProvidesCustomPrerequisiteInfo info; readonly ProvidesCustomPrerequisiteInfo info;
bool enabled = true; bool enabled = true;
public ProvidesCustomPrerequisite(ActorInitializer init, ProvidesCustomPrerequisiteInfo info) public ProvidesCustomPrerequisite(ActorInitializer init, ProvidesCustomPrerequisiteInfo info)
{ {
this.info = info; this.info = info;
if (info.Race.Any()) var race = init.Contains<RaceInit>() ? init.Get<RaceInit, string>() : init.self.Owner.Country.Race;
{
var race = init.self.Owner.Country.Race;
if (init.Contains<RaceInit>())
race = init.Get<RaceInit, string>();
enabled = info.Race.Contains(race); Update(init.self.Owner, race);
}
} }
public IEnumerable<string> ProvidesPrerequisites public IEnumerable<string> ProvidesPrerequisites
@@ -60,8 +59,19 @@ namespace OpenRA.Mods.RA
public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner) public void OnOwnerChanged(Actor self, Player oldOwner, Player newOwner)
{ {
if (!info.Sticky && info.Race.Any()) if (info.ResetOnOwnerChange)
enabled = info.Race.Contains(self.Owner.Country.Race); 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);
} }
} }

View File

@@ -61,6 +61,13 @@ namespace OpenRA.Mods.RA
watchers.RemoveAll(x => x.RegisteredBy == 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) static Cache<string, List<Actor>> GatherOwnedPrerequisites(Player player)
{ {
var ret = new Cache<string, List<Actor>>(x => new List<Actor>()); var ret = new Cache<string, List<Actor>>(x => new List<Actor>());
@@ -69,7 +76,7 @@ namespace OpenRA.Mods.RA
// Add all actors that provide prerequisites // Add all actors that provide prerequisites
var prerequisites = player.World.ActorsWithTrait<ITechTreePrerequisite>() var prerequisites = player.World.ActorsWithTrait<ITechTreePrerequisite>()
.Where(a => a.Actor.Owner == player && !a.Actor.IsDead() && a.Actor.IsInWorld); .Where(a => a.Actor.Owner == player && a.Actor.IsInWorld && !a.Actor.IsDead());
foreach (var b in prerequisites) foreach (var b in prerequisites)
{ {

View File

@@ -695,6 +695,12 @@ WEAP:
ProvidesCustomPrerequisite@soviet: ProvidesCustomPrerequisite@soviet:
Race: soviet Race: soviet
Prerequisite: vehicles.soviet Prerequisite: vehicles.soviet
ProvidesCustomPrerequisite@alliedstructure:
RequiresPrerequisites: structures.allies
Prerequisite: vehicles.allies
ProvidesCustomPrerequisite@sovietstructure:
RequiresPrerequisites: structures.soviet
Prerequisite: vehicles.soviet
PrimaryBuilding: PrimaryBuilding:
IronCurtainable: IronCurtainable:
ProductionBar: ProductionBar: