Implementation of the automated D2k Carryall

Closes #2246
This commit is contained in:
Matthijs Benschop
2014-11-20 16:14:29 +01:00
parent 71a8815ffb
commit 2d380c64bd
22 changed files with 681 additions and 21 deletions

View File

@@ -0,0 +1,175 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits.Render;
using OpenRA.Mods.RA;
using OpenRA.Mods.RA.Activities;
using OpenRA.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k
{
[Desc("Automatically transports harvesters with the Carryable trait between resource fields and refineries")]
public class AutoCarryallInfo : ITraitInfo, Requires<IBodyOrientationInfo>
{
public object Create(ActorInitializer init) { return new AutoCarryall(init.self, this); }
}
public class AutoCarryall : INotifyBecomingIdle, INotifyKilled, ISync, IRender
{
readonly Actor self;
readonly WRange carryHeight;
// The actor we are currently carrying.
[Sync] Actor carrying;
// TODO: Use ActorPreviews so that this can support actors with multiple sprites
Animation anim;
public bool Busy { get; internal set; }
public AutoCarryall(Actor self, AutoCarryallInfo info)
{
this.self = self;
carryHeight = self.Trait<Helicopter>().Info.LandAltitude;
}
public void OnBecomingIdle(Actor self)
{
FindCarryableForTransport();
if (!Busy)
self.QueueActivity(new HeliFlyCircle(self));
}
// A carryable notifying us that he'd like to be carried
public bool RequestTransportNotify(Actor carryable)
{
if (Busy)
return false;
if (ReserveCarryable(carryable))
{
self.QueueActivity(false, new CarryUnit(self, carryable));
return true;
}
return false;
}
void FindCarryableForTransport()
{
// get all carryables who want transport
var carryables = self.World.ActorsWithTrait<Carryable>()
.Where(p =>
{
var actor = p.Actor;
if (actor == null)
return false;
if (actor.Owner != self.Owner)
return false;
if (actor.IsDead)
return false;
var trait = p.Trait;
if (trait.Reserved)
return false;
if (!trait.WantsTransport)
return false;
return true;
})
.OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);
foreach (var p in carryables)
{
// Check if its actually me who's the best candidate
if (p.Trait.GetClosestIdleCarrier() == self && ReserveCarryable(p.Actor))
{
self.QueueActivity(false, new CarryUnit(self, p.Actor));
break;
}
}
}
// Reserve the carryable so its ours exclusively
public bool ReserveCarryable(Actor carryable)
{
if (carryable.Trait<Carryable>().Reserve(self))
{
carrying = carryable;
Busy = true;
return true;
}
return false;
}
// Unreserve the carryable
public void UnreserveCarryable()
{
if (carrying != null)
{
if (carrying.IsInWorld && !carrying.IsDead)
carrying.Trait<Carryable>().UnReserve(self);
carrying = null;
}
Busy = false;
}
// INotifyKilled
public void Killed(Actor self, AttackInfo e)
{
if (carrying != null)
{
carrying.Kill(e.Attacker);
carrying = null;
}
UnreserveCarryable();
}
// Called when carryable is inside.
public void AttachCarryable(Actor carryable)
{
// Create a new animation for our carryable unit
anim = new Animation(self.World, RenderSprites.GetImage(carryable.Info), RenderSprites.MakeFacingFunc(self));
anim.PlayRepeating("idle");
}
// Called when released
public void CarryableReleased()
{
anim = null;
}
public IEnumerable<IRenderable> Render(Actor self, WorldRenderer wr)
{
// Render the carryable below us TODO: Implement RenderSprites trait
if (anim != null && !self.World.FogObscures(self))
{
anim.Tick();
var renderables = anim.Render(self.CenterPosition + new WVec(0, 0, -carryHeight.Range), wr.Palette("player" + carrying.Owner.InternalName));
foreach (var rr in renderables)
yield return rr;
}
}
}
}

View File

@@ -0,0 +1,196 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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;
using System.Drawing;
using System.Linq;
using OpenRA.Mods.RA;
using OpenRA.Mods.RA.Activities;
using OpenRA.Mods.RA.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k
{
public class CarryUnit : Activity
{
readonly Actor self;
readonly Actor carryable;
readonly IMove movement;
readonly Carryable c;
readonly AutoCarryall aca;
readonly Helicopter helicopter;
readonly IPositionable positionable;
readonly IFacing cFacing; // Carryable facing
readonly IFacing sFacing; // Self facing
enum State { Intercept, LockCarryable, MoveToCarryable, Turn, Pickup, Transport, Land, Release, Takeoff, Done }
State state;
public CarryUnit(Actor self, Actor carryable)
{
this.self = self;
this.carryable = carryable;
movement = self.Trait<IMove>();
c = carryable.Trait<Carryable>();
aca = self.Trait<AutoCarryall>();
helicopter = self.Trait<Helicopter>();
positionable = carryable.Trait<IPositionable>();
cFacing = carryable.Trait<IFacing>();
sFacing = self.Trait<IFacing>();
state = State.Intercept;
}
// 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 (carryable.IsDead)
{
aca.UnreserveCarryable();
return NextActivity;
}
switch (state)
{
case State.Intercept: // Move towards our carryable
state = State.LockCarryable;
return Util.SequenceActivities(movement.MoveWithinRange(Target.FromActor(carryable), WRange.FromCells(4)), this);
case State.LockCarryable:
// Last check
if (c.StandbyForPickup(self))
{
state = State.MoveToCarryable;
return this;
}
else
{
// We got cancelled
aca.UnreserveCarryable();
return NextActivity;
}
case State.MoveToCarryable: // We arrived, move on top
if (self.Location == carryable.Location)
{
state = State.Turn;
return this;
}
else
return Util.SequenceActivities(movement.MoveTo(carryable.Location, 0), this);
case State.Turn: // Align facing and Land
if (sFacing.Facing != cFacing.Facing)
return Util.SequenceActivities(new Turn(self, cFacing.Facing), this);
else
{
state = State.Pickup;
return Util.SequenceActivities(new HeliLand(false), new Wait(10), this);
}
case State.Pickup:
// Remove our carryable from world
self.World.AddFrameEndTask(w => carryable.World.Remove(carryable));
aca.AttachCarryable(carryable);
state = State.Transport;
return this;
case State.Transport:
// Move self to destination
var targetl = GetLocationToDrop(c.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;
else
{
state = State.Release;
return Util.SequenceActivities(new Wait(15), this);
}
case State.Release:
if (!CanDropHere())
{
state = State.Transport;
return this;
}
positionable.SetPosition(carryable, self.Location, SubCell.FullCell);
cFacing.Facing = sFacing.Facing;
// Put back into world
self.World.AddFrameEndTask(w => carryable.World.Add(carryable));
// Unlock carryable
aca.CarryableReleased();
c.Dropped();
state = State.Done;
return Util.SequenceActivities(new Wait(10), this);
case State.Done:
self.Trait<AutoCarryall>().UnreserveCarryable();
return NextActivity;
}
return NextActivity;
}
public override void Cancel(Actor self)
{
// TODO: Drop the unit at the nearest available cell
}
}
}

View File

@@ -0,0 +1,166 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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;
using System.Linq;
using OpenRA.Mods.Common;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k
{
[Desc("Can be carried by units with the trait `AutoCarryall`.")]
public class CarryableInfo : ITraitInfo
{
[Desc("Required distance away from destination before requesting a pickup.")]
public int MinDistance = 6;
public object Create(ActorInitializer init) { return new Carryable(init.self, this); }
}
public class Carryable : IDisableMove, INotifyHarvesterAction
{
readonly CarryableInfo info;
readonly Actor self;
public bool Reserved { get; private set; }
// If we're locked there isnt much we can do. We'll have to wait for the carrier to finish with us. We should not move or get new orders!
bool locked;
public bool WantsTransport { get; private set; }
public CPos Destination;
Activity afterLandActivity;
public Carryable(Actor self, CarryableInfo info)
{
this.info = info;
this.self = self;
locked = false;
Reserved = false;
WantsTransport = false;
}
public void MovingToResources(Actor self, CPos targetCell, Activity next) { RequestTransport(targetCell, next); }
public void MovingToRefinery(Actor self, CPos targetCell, Activity next) { RequestTransport(targetCell, next); }
void RequestTransport(CPos destination, Activity afterLandActivity)
{
if (locked || Reserved)
return;
if (destination == CPos.Zero)
return;
if ((self.Location - destination).Length < info.MinDistance)
return;
Destination = destination;
this.afterLandActivity = afterLandActivity;
WantsTransport = true;
// Inform all idle carriers
var carriers = self.World.ActorsWithTrait<AutoCarryall>()
.Where(c => !c.Trait.Busy && !c.Actor.IsDead && c.Actor.Owner == self.Owner)
.OrderBy(p => (self.Location - p.Actor.Location).LengthSquared);
foreach (var carrier in carriers)
{
// Notify the carrier and see if he's willing to transport us..
if (carrier.Trait.RequestTransportNotify(self))
break; // If true then we're done
}
}
// No longer want to be carried
public void MovementCancelled(Actor self)
{
if (locked)
return;
WantsTransport = false;
Reserved = false;
// TODO: We could implement something like a carrier.Trait<AutoCarryAll>().CancelTransportNotify(self) and call it here
}
// We do not handle Harvested notification
public void Harvested(Actor self, ResourceType resource) { }
public Actor GetClosestIdleCarrier()
{
// Find carriers
var carriers = self.World.ActorsWithTrait<AutoCarryall>()
.Where(p => p.Actor.Owner == self.Owner && !p.Trait.Busy)
.Select(h => h.Actor);
return WorldUtils.ClosestTo(carriers, self);
}
// This gets called by carrier after we touched down
public void Dropped()
{
WantsTransport = false;
locked = false;
if (afterLandActivity != null)
self.QueueActivity(false, afterLandActivity);
}
public bool Reserve(Actor carrier)
{
if ((self.Location - Destination).Length < info.MinDistance)
{
MovementCancelled(self);
return false;
}
Reserved = true;
return true;
}
public void UnReserve(Actor carrier)
{
Reserved = false;
locked = false;
}
// Prepare for transport pickup
public bool StandbyForPickup(Actor carrier)
{
if (Destination == CPos.Zero)
return false;
if (locked || !WantsTransport)
return false;
// Last change to change our mind...
if ((self.Location - Destination).Length < info.MinDistance)
{
MovementCancelled(self);
return false;
}
// Cancel our activities
self.CancelActivity();
locked = true;
return true;
}
// IMoveDisabled
public bool MoveDisabled(Actor self)
{
// We do not want to move while being locked. The carrier will try to pick us up.
return locked;
}
}
}

View File

@@ -86,6 +86,9 @@
<Compile Include="Widgets\BuildPaletteWidget.cs" />
<Compile Include="Widgets\SlidingContainerWidget.cs" />
<Compile Include="Widgets\Logic\IngameChromeLogic.cs" />
<Compile Include="AutoCarryall\AutoCarryall.cs" />
<Compile Include="AutoCarryall\Carryable.cs" />
<Compile Include="AutoCarryall\CarryUnit.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup>