Skip to content

Commit

Permalink
Merge pull request #7664 from penev92/bleed_harvesterInsurance
Browse files Browse the repository at this point in the history
Implement Carryall edge spawn, harvester delivery by carryall and harvester insurance for D2k
  • Loading branch information
obrakmann committed Mar 25, 2015
2 parents 15af43d + efee359 commit 6fca67e
Show file tree
Hide file tree
Showing 17 changed files with 615 additions and 296 deletions.
13 changes: 13 additions & 0 deletions OpenRA.Game/Map/Map.cs
Expand Up @@ -804,6 +804,19 @@ public CPos ChooseRandomCell(MersenneTwister rand)
return new MPos(x, y).ToCPos(this);
}

public CPos ChooseClosestEdgeCell(CPos pos)
{
var mpos = pos.ToMPos(this);

var horizontalBound = ((mpos.U - Bounds.Left) < Bounds.Width / 2) ? Bounds.Left : Bounds.Right;
var verticalBound = ((mpos.V - Bounds.Top) < Bounds.Height / 2) ? Bounds.Top : Bounds.Bottom;

var distX = Math.Abs(horizontalBound - mpos.U);
var distY = Math.Abs(verticalBound - mpos.V);

return distX < distY ? new MPos(horizontalBound, mpos.V).ToCPos(this) : new MPos(mpos.U, verticalBound).ToCPos(this);
}

public CPos ChooseRandomEdgeCell(MersenneTwister rand)
{
var isX = rand.Next(2) == 0;
Expand Down
9 changes: 6 additions & 3 deletions OpenRA.Mods.Common/Traits/Buildings/FreeActor.cs
Expand Up @@ -19,16 +19,19 @@ namespace OpenRA.Mods.Common.Traits
public class FreeActorInfo : ITraitInfo
{
[ActorReference]
[Desc("Name of actor (use HARV if this trait is for refineries)")]
[Desc("Name of the actor.")]
public readonly string Actor = null;

[Desc("What the unit should start doing. Warning: If this is not a harvester", "it will break if you use FindResources.")]
public readonly string InitialActivity = null;
[Desc("Offset relative to structure-center in 2D (e.g. 1, 2)")]

[Desc("Offset relative to the top-left cell of the building.")]
public readonly CVec SpawnOffset = CVec.Zero;

[Desc("Which direction the unit should face.")]
public readonly int Facing = 0;

public object Create(ActorInitializer init) { return new FreeActor(init, this); }
public virtual object Create(ActorInitializer init) { return new FreeActor(init, this); }
}

public class FreeActor
Expand Down
196 changes: 0 additions & 196 deletions OpenRA.Mods.D2k/Activities/CarryUnit.cs

This file was deleted.

140 changes: 140 additions & 0 deletions OpenRA.Mods.D2k/Activities/DeliverUnit.cs
@@ -0,0 +1,140 @@
#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 OpenRA.Activities;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.D2k.Traits;
using OpenRA.Traits;

namespace OpenRA.Mods.D2k.Activities
{
public class DeliverUnit : Activity
{
readonly Actor self;
readonly Actor cargo;
readonly IMove movement;
readonly Carryable carryable;
readonly Carryall carryall;
readonly Helicopter helicopter;
readonly IPositionable positionable;
readonly IFacing cargoFacing;
readonly IFacing selfFacing;

enum State { Transport, Land, Release, Done }

State state;

public DeliverUnit(Actor self)
{
carryall = self.Trait<Carryall>();
this.self = self;
cargo = carryall.Carrying;
movement = self.Trait<IMove>();
carryable = cargo.Trait<Carryable>();
helicopter = self.Trait<Helicopter>();
positionable = cargo.Trait<IPositionable>();
cargoFacing = cargo.Trait<IFacing>();
selfFacing = self.Trait<IFacing>();
state = State.Transport;
}

// Find a suitable location to drop our carryable
CPos GetLocationToDrop(CPos requestedPosition)
{
if (positionable.CanEnterCell(requestedPosition))
return requestedPosition;

var candidateCells = Util.AdjacentCells(self.World, Target.FromCell(self.World, requestedPosition));

// TODO: This will behave badly if there is no suitable drop point nearby
do
{
foreach (var c in candidateCells)
if (positionable.CanEnterCell(c))
return c;

// Expanding dropable cells search area
// TODO: This also includes all of the cells we have just checked
candidateCells = Util.ExpandFootprint(candidateCells, true);
} while (true);
}

// Check if we can drop the unit at our current location.
bool CanDropHere()
{
return positionable.CanEnterCell(self.Location);
}

public override Activity Tick(Actor self)
{
if (cargo.IsDead || !carryall.IsBusy)
{
carryall.UnreserveCarryable();
return NextActivity;
}

switch (state)
{
case State.Transport:
var targetl = GetLocationToDrop(carryable.Destination);
state = State.Land;
return Util.SequenceActivities(movement.MoveTo(targetl, 0), this);

case State.Land:
if (!CanDropHere())
{
state = State.Transport;
return this;
}

if (HeliFly.AdjustAltitude(self, helicopter, helicopter.Info.LandAltitude))
return this;
state = State.Release;
return Util.SequenceActivities(new Wait(15), this);

case State.Release:
if (!CanDropHere())
{
state = State.Transport;
return this;
}

Release();
state = State.Done;
return Util.SequenceActivities(new Wait(10), this);

case State.Done:
self.Trait<Carryall>().UnreserveCarryable();
return NextActivity;
}

return NextActivity;
}

void Release()
{
positionable.SetPosition(cargo, self.Location, SubCell.FullCell);
cargoFacing.Facing = selfFacing.Facing;

// Put back into world
self.World.AddFrameEndTask(w => cargo.World.Add(cargo));

// Unlock carryable
carryall.CarryableReleased();
carryable.Dropped();
}

public override void Cancel(Actor self)
{
// TODO: Drop the unit at the nearest available cell
}
}
}

0 comments on commit 6fca67e

Please sign in to comment.