Skip to content

Commit

Permalink
add GiveUnitCrateAction
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisforbes committed Jul 31, 2010
1 parent 53b9b63 commit 4c24547
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
63 changes: 63 additions & 0 deletions OpenRA.Mods.RA/Crates/GiveUnitCrateAction.cs
@@ -0,0 +1,63 @@
#region Copyright & License Information
/*
* Copyright 2007-2010 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 LICENSE.
*/
#endregion

using System.Linq;
using OpenRA.Traits;

namespace OpenRA.Mods.RA.Crates
{
class GiveUnitCrateActionInfo : CrateActionInfo
{
[ActorReference]
public readonly string Unit = null;

public override object Create(ActorInitializer init) { return new GiveUnitCrateAction(init.self, this); }
}

class GiveUnitCrateAction : CrateAction
{
GiveUnitCrateActionInfo Info;
public GiveUnitCrateAction(Actor self, GiveUnitCrateActionInfo info)
: base(self, info) { Info = info; }

public override int GetSelectionShares(Actor collector)
{
var valuedInfo = Rules.Info[Info.Unit].Traits.Get<ValuedInfo>();
return valuedInfo.Owner.Contains(collector.Owner.Country.Race)
? base.GetSelectionShares(collector)
: 0; // this unit is not buildable by the collector's country, so
// don't give them free ones either.
}

public override void Activate(Actor collector)
{
var location = ChooseEmptyCellNear(collector);
if (location != null)
collector.World.AddFrameEndTask(
w => w.Add(new Actor(w, Info.Unit, location.Value, collector.Owner)));

base.Activate(collector);
}

int2? ChooseEmptyCellNear(Actor a)
{
// hack: use `a`'s movement capability.
var move = a.traits.Get<ITeleportable>();
var loc = a.Location;

for (var i = -1; i < 2; i++)
for (var j = -1; j < 2; j++)
if (move.CanEnterCell(loc + new int2(i, j)))
return loc + new int2(i, j);

return null; // nowhere we can place this -- so the crate will do nothing.
}
}
}
3 changes: 2 additions & 1 deletion OpenRA.Mods.RA/OpenRA.Mods.RA.csproj
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -78,6 +78,7 @@
<Compile Include="Activities\Wait.cs" />
<Compile Include="AttackBase.cs" />
<Compile Include="Combat.cs" />
<Compile Include="Crates\GiveUnitCrateAction.cs" />
<Compile Include="DetectCloaked.cs" />
<Compile Include="Effects\Bullet.cs" />
<Compile Include="Effects\Explosion.cs" />
Expand Down

0 comments on commit 4c24547

Please sign in to comment.