Skip to content

Commit

Permalink
Add D2k harvester insurance on worm frenzy
Browse files Browse the repository at this point in the history
  • Loading branch information
penev92 committed Mar 20, 2015
1 parent 07b2240 commit 5153302
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
18 changes: 17 additions & 1 deletion OpenRA.Mods.D2k/Activities/SwallowActor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,23 @@ bool WormAttack(Actor worm)
sandworm.IsAttacking = true;

foreach (var actor in lunch)
actor.World.AddFrameEndTask(_ => actor.Destroy());
{
var actor1 = actor; // loop variable in closure hazard

actor.World.AddFrameEndTask(_ =>
{
actor1.Destroy();
// Harvester insurance
if (!actor1.HasTrait<Harvester>())
return;
var insurance = actor1.Owner.PlayerActor.TraitOrDefault<HarvesterInsurance>();
if (insurance != null)
actor1.World.AddFrameEndTask(__ => insurance.TryActivate());
});
}

positionable.SetPosition(worm, targetLocation);
foreach (var notify in worm.TraitsImplementing<INotifyAttack>())
Expand Down
1 change: 1 addition & 0 deletions OpenRA.Mods.D2k/OpenRA.Mods.D2k.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
<Compile Include="Traits\Buildings\DamagedWithoutFoundation.cs" />
<Compile Include="Traits\Buildings\LaysTerrain.cs" />
<Compile Include="Traits\Carryable.cs" />
<Compile Include="Traits\Player\HarvesterInsurance.cs" />
<Compile Include="Traits\Render\WithCrumbleOverlay.cs" />
<Compile Include="Traits\Render\WithDockingOverlay.cs" />
<Compile Include="Traits\Render\WithDeliveryOverlay.cs" />
Expand Down
48 changes: 48 additions & 0 deletions OpenRA.Mods.D2k/Traits/Player/HarvesterInsurance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#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.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;

namespace OpenRA.Mods.D2k.Traits
{
[Desc("A player with this trait will receive a free harvester when his last one gets eaten by a sandworm, provided he has at least one refinery.")]
public class HarvesterInsuranceInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new HarvesterInsurance(init.Self); }
}

public class HarvesterInsurance
{
readonly Actor self;

public HarvesterInsurance(Actor self)
{
this.self = self;
}

public void TryActivate()
{
var harvesters = self.World.ActorsWithTrait<Harvester>().Where(x => x.Actor.Owner == self.Owner);
if (harvesters.Any())
return;

var refineries = self.World.ActorsWithTrait<Refinery>().Where(x => x.Actor.Owner == self.Owner);
if (!refineries.Any())
return;

var refinery = refineries.First().Actor;
var delivery = refinery.Trait<FreeActorWithDelivery>();
delivery.DoDelivery(refinery.Location + delivery.Info.DeliveryOffset, delivery.Info.Actor,
delivery.Info.DeliveringActor, delivery.Info.InitialActivity);
}
}
}
1 change: 1 addition & 0 deletions mods/d2k/rules/player.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ Player:
Name: Unrestricted
Prerequisites: techlevel.low, techlevel.medium, techlevel.high, techlevel.superweapons
EnemyWatcher:
HarvesterInsurance:

0 comments on commit 5153302

Please sign in to comment.