Convert GlobalUpgradable to conditions.

This commit is contained in:
Paul Chote
2016-12-04 11:58:17 +00:00
parent b5ee30345b
commit 1ef3e246d1
13 changed files with 121 additions and 106 deletions

View File

@@ -331,7 +331,7 @@
<Compile Include="Traits\GainsExperience.cs" /> <Compile Include="Traits\GainsExperience.cs" />
<Compile Include="Traits\GivesBounty.cs" /> <Compile Include="Traits\GivesBounty.cs" />
<Compile Include="Traits\GivesExperience.cs" /> <Compile Include="Traits\GivesExperience.cs" />
<Compile Include="Traits\GlobalUpgradable.cs" /> <Compile Include="Traits\GrantConditionOnPrerequisite.cs" />
<Compile Include="Traits\Guard.cs" /> <Compile Include="Traits\Guard.cs" />
<Compile Include="Traits\Guardable.cs" /> <Compile Include="Traits\Guardable.cs" />
<Compile Include="Traits\Harvester.cs" /> <Compile Include="Traits\Harvester.cs" />
@@ -373,7 +373,7 @@
<Compile Include="Traits\Player\ClassicProductionQueue.cs" /> <Compile Include="Traits\Player\ClassicProductionQueue.cs" />
<Compile Include="Traits\Player\ConquestVictoryConditions.cs" /> <Compile Include="Traits\Player\ConquestVictoryConditions.cs" />
<Compile Include="Traits\Player\EnemyWatcher.cs" /> <Compile Include="Traits\Player\EnemyWatcher.cs" />
<Compile Include="Traits\Player\GlobalUpgradeManager.cs" /> <Compile Include="Traits\Player\GrantConditionOnPrerequisiteManager.cs" />
<Compile Include="Traits\Player\HarvesterAttackNotifier.cs" /> <Compile Include="Traits\Player\HarvesterAttackNotifier.cs" />
<Compile Include="Traits\Player\MissionObjectives.cs" /> <Compile Include="Traits\Player\MissionObjectives.cs" />
<Compile Include="Traits\Player\PlaceBeacon.cs" /> <Compile Include="Traits\Player\PlaceBeacon.cs" />

View File

@@ -1,72 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2016 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Grants upgrades to the actor this is attached to when prerequisites are available.")]
public class GlobalUpgradableInfo : ITraitInfo, Requires<UpgradeManagerInfo>
{
[UpgradeGrantedReference, FieldLoader.Require]
[Desc("List of upgrades to apply.")]
public readonly string[] Upgrades = { };
[FieldLoader.Require]
[Desc("List of required prerequisites.")]
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,78 @@
#region Copyright & License Information
/*
* Copyright 2007-2016 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Grants a condition to the actor this is attached to when prerequisites are available.")]
public class GrantConditionOnPrerequisiteInfo : ITraitInfo
{
[FieldLoader.Require]
[UpgradeGrantedReference]
[Desc("The condition to grant.")]
public readonly string Condition = null;
[FieldLoader.Require]
[Desc("List of required prerequisites.")]
public readonly string[] Prerequisites = { };
public object Create(ActorInitializer init) { return new GrantConditionOnPrerequisite(init.Self, this); }
}
public class GrantConditionOnPrerequisite : INotifyCreated, INotifyAddedToWorld, INotifyRemovedFromWorld
{
readonly GrantConditionOnPrerequisiteInfo info;
readonly GrantConditionOnPrerequisiteManager globalManager;
UpgradeManager manager;
int conditionToken = UpgradeManager.InvalidConditionToken;
bool wasAvailable;
public GrantConditionOnPrerequisite(Actor self, GrantConditionOnPrerequisiteInfo info)
{
this.info = info;
globalManager = self.Owner.PlayerActor.Trait<GrantConditionOnPrerequisiteManager>();
}
void INotifyCreated.Created(Actor self)
{
manager = self.TraitOrDefault<UpgradeManager>();
}
void INotifyAddedToWorld.AddedToWorld(Actor self)
{
if (info.Prerequisites.Any())
globalManager.Register(self, this, info.Prerequisites);
}
void INotifyRemovedFromWorld.RemovedFromWorld(Actor self)
{
if (info.Prerequisites.Any())
globalManager.Unregister(self, this, info.Prerequisites);
}
public void PrerequisitesUpdated(Actor self, bool available)
{
if (available == wasAvailable || manager == null)
return;
if (available && conditionToken == UpgradeManager.InvalidConditionToken)
conditionToken = manager.GrantCondition(self, info.Condition);
else if (!available && conditionToken != UpgradeManager.InvalidConditionToken)
conditionToken = manager.RevokeCondition(self, conditionToken);
wasAvailable = available;
}
}
}

View File

@@ -17,18 +17,18 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
[Desc("Attach this to the player actor.")] [Desc("Attach this to the player actor.")]
public class GlobalUpgradeManagerInfo : ITraitInfo, Requires<TechTreeInfo> public class GrantConditionOnPrerequisiteManagerInfo : ITraitInfo, Requires<TechTreeInfo>
{ {
public object Create(ActorInitializer init) { return new GlobalUpgradeManager(init); } public object Create(ActorInitializer init) { return new GrantConditionOnPrerequisiteManager(init); }
} }
public class GlobalUpgradeManager : ITechTreeElement public class GrantConditionOnPrerequisiteManager : ITechTreeElement
{ {
readonly Actor self; readonly Actor self;
readonly Dictionary<string, List<Pair<Actor, GlobalUpgradable>>> upgradables = new Dictionary<string, List<Pair<Actor, GlobalUpgradable>>>(); readonly Dictionary<string, List<Pair<Actor, GrantConditionOnPrerequisite>>> upgradables = new Dictionary<string, List<Pair<Actor, GrantConditionOnPrerequisite>>>();
readonly TechTree techTree; readonly TechTree techTree;
public GlobalUpgradeManager(ActorInitializer init) public GrantConditionOnPrerequisiteManager(ActorInitializer init)
{ {
self = init.Self; self = init.Self;
techTree = self.Trait<TechTree>(); techTree = self.Trait<TechTree>();
@@ -39,12 +39,12 @@ namespace OpenRA.Mods.Common.Traits
return "upgrade_" + string.Join("_", prerequisites.OrderBy(a => a)); return "upgrade_" + string.Join("_", prerequisites.OrderBy(a => a));
} }
public void Register(Actor actor, GlobalUpgradable u, string[] prerequisites) public void Register(Actor actor, GrantConditionOnPrerequisite u, string[] prerequisites)
{ {
var key = MakeKey(prerequisites); var key = MakeKey(prerequisites);
if (!upgradables.ContainsKey(key)) if (!upgradables.ContainsKey(key))
{ {
upgradables.Add(key, new List<Pair<Actor, GlobalUpgradable>>()); upgradables.Add(key, new List<Pair<Actor, GrantConditionOnPrerequisite>>());
techTree.Add(key, prerequisites, 0, this); techTree.Add(key, prerequisites, 0, this);
} }
@@ -54,7 +54,7 @@ namespace OpenRA.Mods.Common.Traits
u.PrerequisitesUpdated(actor, techTree.HasPrerequisites(prerequisites)); u.PrerequisitesUpdated(actor, techTree.HasPrerequisites(prerequisites));
} }
public void Unregister(Actor actor, GlobalUpgradable u, string[] prerequisites) public void Unregister(Actor actor, GrantConditionOnPrerequisite u, string[] prerequisites)
{ {
var key = MakeKey(prerequisites); var key = MakeKey(prerequisites);
var list = upgradables[key]; var list = upgradables[key];
@@ -69,7 +69,7 @@ namespace OpenRA.Mods.Common.Traits
public void PrerequisitesAvailable(string key) public void PrerequisitesAvailable(string key)
{ {
List<Pair<Actor, GlobalUpgradable>> list; List<Pair<Actor, GrantConditionOnPrerequisite>> list;
if (!upgradables.TryGetValue(key, out list)) if (!upgradables.TryGetValue(key, out list))
return; return;
@@ -79,7 +79,7 @@ namespace OpenRA.Mods.Common.Traits
public void PrerequisitesUnavailable(string key) public void PrerequisitesUnavailable(string key)
{ {
List<Pair<Actor, GlobalUpgradable>> list; List<Pair<Actor, GrantConditionOnPrerequisite>> list;
if (!upgradables.TryGetValue(key, out list)) if (!upgradables.TryGetValue(key, out list))
return; return;

View File

@@ -659,6 +659,15 @@ namespace OpenRA.Mods.Common.UtilityCommands
} }
} }
} }
if (node.Key.StartsWith("GlobalUpgradable", StringComparison.Ordinal))
{
RenameNodeKey(node, "GrantConditionOnPrerequisite");
ConvertUpgradesToCondition(parent, node, "Upgrades", "Condition");
}
if (node.Key.StartsWith("GlobalUpgradeManager", StringComparison.Ordinal))
RenameNodeKey(node, "GrantConditionOnPrerequisiteManager");
} }
UpgradeActorRules(modData, engineVersion, ref node.Value.Nodes, node, depth + 1); UpgradeActorRules(modData, engineVersion, ref node.Value.Nodes, node, depth + 1);

View File

@@ -283,8 +283,8 @@
DamageInterval: 16 DamageInterval: 16
DamageTypes: TiberiumDeath DamageTypes: TiberiumDeath
RequiresCondition: !hazmatsuits RequiresCondition: !hazmatsuits
GlobalUpgradable@BIO: GrantConditionOnPrerequisite@BIO:
Upgrades: hazmatsuits Condition: hazmatsuits
Prerequisites: bio Prerequisites: bio
WithDecoration@HAZMAT: WithDecoration@HAZMAT:
Image: pips Image: pips
@@ -309,8 +309,8 @@
HealIfBelow: 100 HealIfBelow: 100
DamageCooldown: 125 DamageCooldown: 125
RequiresCondition: hospitalheal RequiresCondition: hospitalheal
GlobalUpgradable@HOSPITAL: GrantConditionOnPrerequisite@HOSPITAL:
Upgrades: hospitalheal Condition: hospitalheal
Prerequisites: hosp Prerequisites: hosp
WithDecoration@REDCROSS: WithDecoration@REDCROSS:
Image: pips Image: pips

View File

@@ -32,6 +32,6 @@ Player:
Name: Unrestricted Name: Unrestricted
Prerequisites: techlevel.low, techlevel.medium, techlevel.high, techlevel.superweapons Prerequisites: techlevel.low, techlevel.medium, techlevel.high, techlevel.superweapons
Id: unrestricted Id: unrestricted
GlobalUpgradeManager: GrantConditionOnPrerequisiteManager:
ResourceStorageWarning: ResourceStorageWarning:
PlayerExperience: PlayerExperience:

View File

@@ -88,7 +88,7 @@ Player:
Id: unrestricted Id: unrestricted
EnemyWatcher: EnemyWatcher:
HarvesterInsurance: HarvesterInsurance:
GlobalUpgradeManager: GrantConditionOnPrerequisiteManager:
ResourceStorageWarning: ResourceStorageWarning:
AdviceInterval: 26 AdviceInterval: 26
PlayerExperience: PlayerExperience:

View File

@@ -90,9 +90,9 @@ construction_yard:
PrimaryBuilding: PrimaryBuilding:
PrimaryCondition: primary PrimaryCondition: primary
ProvidesPrerequisite@buildingname: ProvidesPrerequisite@buildingname:
GlobalUpgradable: GrantConditionOnPrerequisite:
Prerequisites: upgrade.conyard Prerequisites: upgrade.conyard
Upgrades: stardecoration Condition: stardecoration
WithDecoration@upgraded: WithDecoration@upgraded:
RequiresSelection: true RequiresSelection: true
Image: pips Image: pips
@@ -203,9 +203,9 @@ barracks:
smuggler: barracks.ordos smuggler: barracks.ordos
mercenary: barracks.ordos mercenary: barracks.ordos
ProvidesPrerequisite@buildingname: ProvidesPrerequisite@buildingname:
GlobalUpgradable: GrantConditionOnPrerequisite:
Prerequisites: upgrade.barracks Prerequisites: upgrade.barracks
Upgrades: stardecoration Condition: stardecoration
WithDecoration@upgraded: WithDecoration@upgraded:
RequiresSelection: true RequiresSelection: true
Image: pips Image: pips
@@ -383,9 +383,9 @@ light_factory:
Sequence: idle-top Sequence: idle-top
Power: Power:
Amount: -125 Amount: -125
GlobalUpgradable: GrantConditionOnPrerequisite:
Prerequisites: upgrade.light Prerequisites: upgrade.light
Upgrades: stardecoration Condition: stardecoration
WithDecoration@upgraded: WithDecoration@upgraded:
RequiresSelection: true RequiresSelection: true
Image: pips Image: pips
@@ -463,9 +463,9 @@ heavy_factory:
ProvidesPrerequisite@buildingname: ProvidesPrerequisite@buildingname:
SelectionDecorations: SelectionDecorations:
VisualBounds: 96,96 VisualBounds: 96,96
GlobalUpgradable: GrantConditionOnPrerequisite:
Prerequisites: upgrade.heavy Prerequisites: upgrade.heavy
Upgrades: stardecoration Condition: stardecoration
WithDecoration@upgraded: WithDecoration@upgraded:
RequiresSelection: true RequiresSelection: true
Image: pips Image: pips
@@ -841,9 +841,9 @@ high_tech_factory:
Amount: -75 Amount: -75
SelectionDecorations: SelectionDecorations:
VisualBounds: 96,96 VisualBounds: 96,96
GlobalUpgradable: GrantConditionOnPrerequisite:
Prerequisites: upgrade.hightech Prerequisites: upgrade.hightech
Upgrades: stardecoration Condition: stardecoration
WithDecoration@upgraded: WithDecoration@upgraded:
RequiresSelection: true RequiresSelection: true
Image: pips Image: pips

View File

@@ -291,8 +291,8 @@
HealIfBelow: 100 HealIfBelow: 100
DamageCooldown: 125 DamageCooldown: 125
RequiresCondition: hospitalheal RequiresCondition: hospitalheal
GlobalUpgradable: GrantConditionOnPrerequisite:
Upgrades: hospitalheal Condition: hospitalheal
Prerequisites: hosp Prerequisites: hosp
DeathSounds@NORMAL: DeathSounds@NORMAL:
DeathTypes: DefaultDeath, BulletDeath, SmallExplosionDeath, ExplosionDeath DeathTypes: DefaultDeath, BulletDeath, SmallExplosionDeath, ExplosionDeath

View File

@@ -69,7 +69,7 @@ Player:
Name: Unrestricted Name: Unrestricted
Prerequisites: techlevel.infonly, techlevel.low, techlevel.medium, techlevel.high, techlevel.unrestricted Prerequisites: techlevel.infonly, techlevel.low, techlevel.medium, techlevel.high, techlevel.unrestricted
Id: unrestricted Id: unrestricted
GlobalUpgradeManager: GrantConditionOnPrerequisiteManager:
EnemyWatcher: EnemyWatcher:
VeteranProductionIconOverlay: VeteranProductionIconOverlay:
Image: iconchevrons Image: iconchevrons

View File

@@ -326,8 +326,8 @@
HealIfBelow: 100 HealIfBelow: 100
DamageCooldown: 125 DamageCooldown: 125
RequiresCondition: hospitalheal RequiresCondition: hospitalheal
GlobalUpgradable@HOSPITAL: GrantConditionOnPrerequisite@HOSPITAL:
Upgrades: hospitalheal Condition: hospitalheal
Prerequisites: cahosp Prerequisites: cahosp
WithDecoration@REDCROSS: WithDecoration@REDCROSS:
Image: pips Image: pips

View File

@@ -1,7 +1,7 @@
Player: Player:
AlwaysVisible: AlwaysVisible:
TechTree: TechTree:
GlobalUpgradeManager: GrantConditionOnPrerequisiteManager:
ClassicProductionQueue@Building: ClassicProductionQueue@Building:
Type: Building Type: Building
BuildDurationModifier: 120 BuildDurationModifier: 120