Skip to content

Commit

Permalink
Merge pull request OpenRA#7812 from pchote/modules
Browse files Browse the repository at this point in the history
Pluggable upgrade modules
  • Loading branch information
chrisforbes committed Apr 1, 2015
2 parents 28ea598 + 704e365 commit 14e9cfd
Show file tree
Hide file tree
Showing 18 changed files with 474 additions and 208 deletions.
2 changes: 2 additions & 0 deletions OpenRA.Mods.Common/OpenRA.Mods.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,8 @@
<Compile Include="UtilityCommands\CheckSequenceSprites.cs" />
<Compile Include="UtilityCommands\FixClassicTilesets.cs" />
<Compile Include="Graphics\TilesetSpecificSpriteSequence.cs" />
<Compile Include="Traits\Pluggable.cs" />
<Compile Include="Traits\Plug.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
Expand Down
55 changes: 46 additions & 9 deletions OpenRA.Mods.Common/Orders/PlaceBuildingOrderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class PlaceBuildingOrderGenerator : IOrderGenerator
readonly string building;
readonly BuildingInfo buildingInfo;
readonly PlaceBuildingInfo placeBuildingInfo;
readonly BuildingInfluence buildingInfluence;
readonly string race;
readonly Sprite buildOk;
readonly Sprite buildBlocked;
Expand Down Expand Up @@ -52,6 +53,8 @@ public PlaceBuildingOrderGenerator(ProductionQueue queue, string name)

buildOk = map.SequenceProvider.GetSequence("overlay", "build-valid-{0}".F(tileset)).GetSprite(0);
buildBlocked = map.SequenceProvider.GetSequence("overlay", "build-invalid").GetSprite(0);

buildingInfluence = producer.World.WorldActor.Trait<BuildingInfluence>();
}

public IEnumerable<Order> Order(World world, CPos xy, MouseInput mi)
Expand All @@ -73,16 +76,33 @@ IEnumerable<Order> InnerOrder(World world, CPos xy, MouseInput mi)

if (mi.Button == MouseButton.Left)
{
var orderType = "PlaceBuilding";
var topLeft = xy - FootprintUtils.AdjustForBuildingSize(buildingInfo);
if (!world.CanPlaceBuilding(building, buildingInfo, topLeft, null)
|| !buildingInfo.IsCloseEnoughToBase(world, producer.Owner, building, topLeft))

var plugInfo = world.Map.Rules.Actors[building].Traits.GetOrDefault<PlugInfo>();
if (plugInfo != null)
{
orderType = "PlacePlug";
if (!AcceptsPlug(topLeft, plugInfo))
{
Sound.PlayNotification(world.Map.Rules, producer.Owner, "Speech", "BuildingCannotPlaceAudio", producer.Owner.Country.Race);
yield break;
}
}
else
{
Sound.PlayNotification(world.Map.Rules, producer.Owner, "Speech", "BuildingCannotPlaceAudio", producer.Owner.Country.Race);
yield break;
if (!world.CanPlaceBuilding(building, buildingInfo, topLeft, null)
|| !buildingInfo.IsCloseEnoughToBase(world, producer.Owner, building, topLeft))
{
Sound.PlayNotification(world.Map.Rules, producer.Owner, "Speech", "BuildingCannotPlaceAudio", producer.Owner.Country.Race);
yield break;
}

if (world.Map.Rules.Actors[building].Traits.Contains<LineBuildInfo>())
orderType = "LineBuild";
}

var isLineBuild = world.Map.Rules.Actors[building].Traits.Contains<LineBuildInfo>();
yield return new Order(isLineBuild ? "LineBuild" : "PlaceBuilding", producer.Owner.PlayerActor, false)
yield return new Order(orderType, producer.Owner.PlayerActor, false)
{
TargetLocation = topLeft,
TargetActor = producer,
Expand All @@ -101,6 +121,16 @@ public void Tick(World world)
p.Tick();
}

bool AcceptsPlug(CPos cell, PlugInfo plug)
{
var host = buildingInfluence.GetBuildingAt(cell);
if (host == null)
return false;

var location = host.Location;
return host.TraitsImplementing<Pluggable>().Any(p => location + p.Info.Offset == cell && p.AcceptsPlug(host, plug.Type));
}

public IEnumerable<IRenderable> Render(WorldRenderer wr, World world) { yield break; }
public IEnumerable<IRenderable> RenderAfterWorld(WorldRenderer wr, World world)
{
Expand All @@ -116,10 +146,17 @@ public IEnumerable<IRenderable> RenderAfterWorld(WorldRenderer wr, World world)

var cells = new Dictionary<CPos, bool>();

// Linebuild for walls.
// Requires a 1x1 footprint
if (rules.Actors[building].Traits.Contains<LineBuildInfo>())
var plugInfo = rules.Actors[building].Traits.GetOrDefault<PlugInfo>();
if (plugInfo != null)
{
if (buildingInfo.Dimensions.X != 1 || buildingInfo.Dimensions.Y != 1)
throw new InvalidOperationException("Plug requires a 1x1 sized Building");

cells.Add(topLeft, AcceptsPlug(topLeft, plugInfo));
}
else if (rules.Actors[building].Traits.Contains<LineBuildInfo>())
{
// Linebuild for walls.
if (buildingInfo.Dimensions.X != 1 || buildingInfo.Dimensions.Y != 1)
throw new InvalidOperationException("LineBuild requires a 1x1 sized Building");

Expand Down
23 changes: 15 additions & 8 deletions OpenRA.Mods.Common/Traits/Attack/AttackBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

namespace OpenRA.Mods.Common.Traits
{
public abstract class AttackBaseInfo : ITraitInfo
public abstract class AttackBaseInfo : UpgradableTraitInfo, ITraitInfo
{
[Desc("Armament names")]
public readonly string[] Armaments = { "primary", "secondary" };
Expand All @@ -35,11 +35,10 @@ public abstract class AttackBaseInfo : ITraitInfo
public abstract object Create(ActorInitializer init);
}

public abstract class AttackBase : IIssueOrder, IResolveOrder, IOrderVoice, ISync
public abstract class AttackBase : UpgradableTrait<AttackBaseInfo>, IIssueOrder, IResolveOrder, IOrderVoice, ISync
{
[Sync] public bool IsAttacking { get; internal set; }
public IEnumerable<Armament> Armaments { get { return getArmaments(); } }
public readonly AttackBaseInfo Info;

protected Lazy<IFacing> facing;
protected Lazy<Building> building;
Expand All @@ -49,9 +48,9 @@ public abstract class AttackBase : IIssueOrder, IResolveOrder, IOrderVoice, ISyn
readonly Actor self;

public AttackBase(Actor self, AttackBaseInfo info)
: base(info)
{
this.self = self;
Info = info;

var armaments = Exts.Lazy(() => self.TraitsImplementing<Armament>()
.Where(a => info.Armaments.Contains(a.Info.Name)));
Expand All @@ -65,7 +64,7 @@ public AttackBase(Actor self, AttackBaseInfo info)

protected virtual bool CanAttack(Actor self, Target target)
{
if (!self.IsInWorld)
if (!self.IsInWorld || IsTraitDisabled)
return false;

if (!HasAnyValidWeapons(target))
Expand Down Expand Up @@ -101,7 +100,7 @@ public IEnumerable<IOrderTargeter> Orders
get
{
var armament = Armaments.FirstOrDefault(a => a.Weapon.Warheads.Any(w => (w is DamageWarhead)));
if (armament == null)
if (armament == null || IsTraitDisabled)
yield break;

var negativeDamage = (armament.Weapon.Warheads.FirstOrDefault(w => (w is DamageWarhead)) as DamageWarhead).Damage < 0;
Expand Down Expand Up @@ -149,6 +148,9 @@ public string VoicePhraseForOrder(Actor self, Order order)

public bool HasAnyValidWeapons(Target t)
{
if (IsTraitDisabled)
return false;

if (Info.AttackRequiresEnteringCell && !positionable.Value.CanEnterCell(t.Actor.Location, null, false))
return false;

Expand All @@ -157,14 +159,19 @@ public bool HasAnyValidWeapons(Target t)

public WRange GetMaximumRange()
{
return Armaments.Select(a => a.Weapon.Range).Append(WRange.Zero).Max();
if (IsTraitDisabled)
return WRange.Zero;

return Armaments.Where(a => !a.IsTraitDisabled)
.Select(a => a.Weapon.Range)
.Append(WRange.Zero).Max();
}

public Armament ChooseArmamentForTarget(Target t) { return Armaments.FirstOrDefault(a => a.Weapon.IsValidAgainst(t, self.World, self)); }

public void AttackTarget(Target target, bool queued, bool allowMove)
{
if (self.IsDisabled())
if (self.IsDisabled() || IsTraitDisabled)
return;

if (!target.IsValidFor(self))
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Traits/Cloak.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public bool IsVisible(Actor self, Player viewer)
if (!Cloaked || self.Owner.IsAlliedWith(viewer))
return true;

return self.World.ActorsWithTrait<DetectCloaked>().Any(a => a.Actor.Owner.IsAlliedWith(viewer)
return self.World.ActorsWithTrait<DetectCloaked>().Any(a => !a.Trait.IsTraitDisabled && a.Actor.Owner.IsAlliedWith(viewer)
&& Info.CloakTypes.Intersect(a.Trait.Info.CloakTypes).Any()
&& (self.CenterPosition - a.Actor.CenterPosition).Length <= WRange.FromCells(a.Trait.Info.Range).Range);
}
Expand Down
11 changes: 3 additions & 8 deletions OpenRA.Mods.Common/Traits/DetectCloaked.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace OpenRA.Mods.Common.Traits
{
[Desc("Actor can reveal Cloak actors in a specified range.")]
public class DetectCloakedInfo : ITraitInfo
public class DetectCloakedInfo : UpgradableTraitInfo, ITraitInfo
{
[Desc("Specific cloak classifications I can reveal.")]
public readonly string[] CloakTypes = { "Cloak" };
Expand All @@ -24,13 +24,8 @@ public class DetectCloakedInfo : ITraitInfo
public object Create(ActorInitializer init) { return new DetectCloaked(this); }
}

public class DetectCloaked
public class DetectCloaked : UpgradableTrait<DetectCloakedInfo>
{
public readonly DetectCloakedInfo Info;

public DetectCloaked(DetectCloakedInfo info)
{
Info = info;
}
public DetectCloaked(DetectCloakedInfo info) : base(info) { }
}
}
23 changes: 22 additions & 1 deletion OpenRA.Mods.Common/Traits/Player/PlaceBuilding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PlaceBuilding : IResolveOrder
{
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString == "PlaceBuilding" || order.OrderString == "LineBuild")
if (order.OrderString == "PlaceBuilding" || order.OrderString == "LineBuild" || order.OrderString == "PlacePlug")
{
self.World.AddFrameEndTask(w =>
{
Expand Down Expand Up @@ -69,6 +69,27 @@ public void ResolveOrder(Actor self, Order order)
playSounds = false;
}
}
else if (order.OrderString == "PlacePlug")
{
var host = self.World.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(order.TargetLocation);
if (host == null)
return;

var plugInfo = unit.Traits.GetOrDefault<PlugInfo>();
if (plugInfo == null)
return;

var location = host.Location;
var pluggable = host.TraitsImplementing<Pluggable>()
.FirstOrDefault(p => location + p.Info.Offset == order.TargetLocation && p.AcceptsPlug(host, plugInfo.Type));

if (pluggable == null)
return;

pluggable.EnablePlug(host, plugInfo.Type);
foreach (var s in buildingInfo.BuildSounds)
Sound.PlayToPlayer(order.Player, s, host.CenterPosition);
}
else
{
if (!self.World.CanPlaceBuilding(order.TargetString, buildingInfo, order.TargetLocation, null)
Expand Down
24 changes: 24 additions & 0 deletions OpenRA.Mods.Common/Traits/Plug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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.Traits;

namespace OpenRA.Mods.Common.Traits
{
public class PlugInfo : TraitInfo<Plug>
{
[Desc("Plug type (matched against Upgrades in Pluggable)")]
public readonly string Type = null;
}

public class Plug { }
}
79 changes: 79 additions & 0 deletions OpenRA.Mods.Common/Traits/Pluggable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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.Traits;

namespace OpenRA.Mods.Common.Traits
{
public class PluggableInfo : ITraitInfo, Requires<UpgradeManagerInfo>
{
[Desc("Footprint cell offset where a plug can be placed.")]
public readonly CVec Offset = CVec.Zero;

[FieldLoader.LoadUsing("LoadUpgrades")]
[Desc("Upgrades to grant for each accepted plug type.")]
public readonly Dictionary<string, string[]> Upgrades = null;

static object LoadUpgrades(MiniYaml y)
{
MiniYaml upgrades;

if (!y.ToDictionary().TryGetValue("Upgrades", out upgrades))
return new Dictionary<string, string[]>();

return upgrades.Nodes.ToDictionary(
kv => kv.Key,
kv => FieldLoader.GetValue<string[]>("(value)", kv.Value.Value));
}

public object Create(ActorInitializer init) { return new Pluggable(init.Self, this); }
}

public class Pluggable
{
public readonly PluggableInfo Info;
readonly UpgradeManager upgradeManager;
string active;

public Pluggable(Actor self, PluggableInfo info)
{
Info = info;
upgradeManager = self.Trait<UpgradeManager>();
}

public bool AcceptsPlug(Actor self, string type)
{
return active == null && Info.Upgrades.ContainsKey(type);
}

public void EnablePlug(Actor self, string type)
{
string[] upgrades;
if (!Info.Upgrades.TryGetValue(type, out upgrades))
return;

foreach (var u in upgrades)
upgradeManager.GrantUpgrade(self, u, this);

active = type;
}

public void DisablePlug(Actor self, string type)
{
if (type != active)
return;

foreach (var u in Info.Upgrades[type])
upgradeManager.RevokeUpgrade(self, u, this);
}
}
}
11 changes: 10 additions & 1 deletion OpenRA.Mods.Common/Traits/Render/RenderDetectionCircle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Graphics;
using OpenRA.Traits;
Expand All @@ -32,9 +33,17 @@ public IEnumerable<IRenderable> RenderAfterWorld(WorldRenderer wr)
if (self.Owner != self.World.LocalPlayer)
yield break;

var range = self.TraitsImplementing<DetectCloaked>()
.Where(a => !a.IsTraitDisabled)
.Select(a => WRange.FromCells(a.Info.Range))
.Append(WRange.Zero).Max();

if (range == WRange.Zero)
yield break;

yield return new RangeCircleRenderable(
self.CenterPosition,
WRange.FromCells(self.Info.Traits.Get<DetectCloakedInfo>().Range),
range,
0,
Color.FromArgb(128, Color.LimeGreen),
Color.FromArgb(96, Color.Black));
Expand Down
Loading

0 comments on commit 14e9cfd

Please sign in to comment.