Fix cargo loading/unloading.

- Fix the unloading subcell bug, letting us have units move to directly adjacent cells.
- Have the cursor change to a deploy-blocked cursor if the transport can't unload due to terrain type.
- Add RenderTransport for transport door opening.
- Remove turning/opening in general.
This commit is contained in:
ScottNZ
2014-02-10 22:49:41 +13:00
parent ea1d1320be
commit 025de83d3a
11 changed files with 182 additions and 97 deletions

View File

@@ -8,79 +8,64 @@
*/ */
#endregion #endregion
using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using OpenRA.Mods.RA.Move; using OpenRA.Mods.RA.Move;
using OpenRA.Mods.RA.Render;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities namespace OpenRA.Mods.RA.Activities
{ {
public class UnloadCargo : Activity public class UnloadCargo : Activity
{ {
bool unloadAll; readonly Actor self;
readonly Cargo cargo;
readonly bool unloadAll;
public UnloadCargo(bool unloadAll) { this.unloadAll = unloadAll; } public UnloadCargo(Actor self, bool unloadAll)
CPos? ChooseExitTile(Actor self, Actor cargo)
{ {
// is anyone still hogging this tile? this.self = self;
if (self.World.ActorMap.GetUnitsAt(self.Location).Count() > 1) cargo = self.Trait<Cargo>();
return null; this.unloadAll = unloadAll;
var mobile = cargo.Trait<Mobile>();
for (var i = -1; i < 2; i++)
for (var j = -1; j < 2; j++)
if ((i != 0 || j != 0) &&
mobile.CanEnterCell(self.Location + new CVec(i, j)))
return self.Location + new CVec(i, j);
return null;
} }
CPos? ChooseRallyPoint(Actor self) public CPos? ChooseExitCell(Actor passenger)
{ {
var mobile = self.Trait<Mobile>(); var mobile = passenger.Trait<Mobile>();
for (var i = -1; i < 2; i++) return cargo.CurrentAdjacentCells
for (var j = -1; j < 2; j++) .Shuffle(self.World.SharedRandom)
if ((i != 0 || j != 0) && .Cast<CPos?>()
mobile.CanEnterCell(self.Location + new CVec(i, j))) .FirstOrDefault(c => mobile.CanEnterCell(c.Value));
return self.Location + new CVec(i, j); }
return self.Location; IEnumerable<CPos> BlockedExitCells(Actor passenger)
{
var mobile = passenger.Trait<Mobile>();
return cargo.CurrentAdjacentCells
.Where(c => mobile.MovementSpeedForCell(passenger, c) != int.MaxValue && !mobile.CanEnterCell(c));
} }
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
if (IsCanceled) return NextActivity; if (IsCanceled || cargo.IsEmpty(self))
// if we're a thing that can turn, turn to the
// right facing for the unload animation
var facing = self.TraitOrDefault<IFacing>();
var unloadFacing = self.Info.Traits.Get<CargoInfo>().UnloadFacing;
if (facing != null && facing.Facing != unloadFacing)
return Util.SequenceActivities( new Turn(unloadFacing), this );
// TODO: handle the BS of open/close sequences, which are inconsistent,
// for reasons that probably make good sense to the westwood guys.
var cargo = self.Trait<Cargo>();
if (cargo.IsEmpty(self))
return NextActivity; return NextActivity;
var ru = self.TraitOrDefault<RenderUnit>(); var actor = cargo.Peek(self);
if (ru != null)
ru.PlayCustomAnimation(self, "unload", null);
var exitTile = ChooseExitTile(self, cargo.Peek(self)); var exitCell = ChooseExitCell(actor);
if (exitTile == null) if (exitCell == null)
return this; {
foreach (var blocker in BlockedExitCells(actor).SelectMany(self.World.ActorMap.GetUnitsAt))
{
foreach (var nbm in blocker.TraitsImplementing<INotifyBlockingMove>())
nbm.OnNotifyBlockingMove(blocker, self);
}
return Util.SequenceActivities(new Wait(10), this);
}
var actor = cargo.Unload(self); cargo.Unload(self);
var exit = exitTile.Value.CenterPosition;
var current = self.Location.CenterPosition;
self.World.AddFrameEndTask(w => self.World.AddFrameEndTask(w =>
{ {
@@ -88,23 +73,33 @@ namespace OpenRA.Mods.RA.Activities
return; return;
var mobile = actor.Trait<Mobile>(); var mobile = actor.Trait<Mobile>();
mobile.Facing = Util.GetFacing(exit - current, mobile.Facing );
mobile.SetPosition(actor, exitTile.Value); var exitSubcell = mobile.GetDesiredSubcell(exitCell.Value, null);
mobile.fromSubCell = exitSubcell; // these settings make sure that the below Set* calls
mobile.toSubCell = exitSubcell; // and the above GetDesiredSubcell call pick a good free subcell for later units being unloaded
var exit = exitCell.Value.CenterPosition + MobileInfo.SubCellOffsets[exitSubcell];
var current = self.Location.CenterPosition + MobileInfo.SubCellOffsets[exitSubcell];
mobile.Facing = Util.GetFacing(exit - current, mobile.Facing);
mobile.SetPosition(actor, exitCell.Value);
mobile.SetVisualPosition(actor, current); mobile.SetVisualPosition(actor, current);
var speed = mobile.MovementSpeedForCell(actor, exitTile.Value); var speed = mobile.MovementSpeedForCell(actor, exitCell.Value);
var length = speed > 0 ? (exit - current).Length / speed : 0; var length = speed > 0 ? (exit - current).Length / speed : 0;
w.Add(actor); w.Add(actor);
actor.CancelActivity(); actor.CancelActivity();
actor.QueueActivity(new Drag(current, exit, length)); actor.QueueActivity(new Drag(current, exit, length));
actor.QueueActivity(mobile.MoveTo(exitTile.Value, 0)); actor.QueueActivity(mobile.MoveTo(exitCell.Value, 0));
var rallyPoint = ChooseRallyPoint(actor).Value; actor.SetTargetLine(Target.FromCell(exitCell.Value), Color.Green, false);
actor.QueueActivity(mobile.MoveTo(rallyPoint, 0));
actor.SetTargetLine(Target.FromCell(rallyPoint), Color.Green, false);
}); });
return unloadAll ? this : NextActivity; if (!unloadAll || cargo.IsEmpty(self))
return NextActivity;
return this;
} }
} }
} }

View File

@@ -22,26 +22,27 @@ namespace OpenRA.Mods.RA
public readonly int MaxWeight = 0; public readonly int MaxWeight = 0;
public readonly int PipCount = 0; public readonly int PipCount = 0;
public readonly string[] Types = { }; public readonly string[] Types = { };
public readonly int UnloadFacing = 0;
public readonly string[] InitialUnits = { }; public readonly string[] InitialUnits = { };
public readonly WRange MaximumUnloadAltitude = WRange.Zero;
public object Create( ActorInitializer init ) { return new Cargo( init, this ); } public object Create(ActorInitializer init) { return new Cargo(init, this); }
} }
public class Cargo : IPips, IIssueOrder, IResolveOrder, IOrderVoice, INotifyKilled, INotifyCapture public class Cargo : IPips, IIssueOrder, IResolveOrder, IOrderVoice, INotifyKilled, INotifyCapture, ITick
{ {
readonly Actor self; readonly Actor self;
readonly CargoInfo info; public readonly CargoInfo Info;
int totalWeight = 0; int totalWeight = 0;
List<Actor> cargo = new List<Actor>(); List<Actor> cargo = new List<Actor>();
public IEnumerable<Actor> Passengers { get { return cargo; } } public IEnumerable<Actor> Passengers { get { return cargo; } }
CPos currentCell;
public IEnumerable<CPos> CurrentAdjacentCells { get; private set; }
public Cargo(ActorInitializer init, CargoInfo info) public Cargo(ActorInitializer init, CargoInfo info)
{ {
this.self = init.self; self = init.self;
this.info = info; Info = info;
if (init.Contains<CargoInit>()) if (init.Contains<CargoInit>())
{ {
@@ -59,11 +60,14 @@ namespace OpenRA.Mods.RA
Load(self, unit); Load(self, unit);
} }
} }
currentCell = self.CenterPosition.ToCPos();
CurrentAdjacentCells = GetAdjacentCells();
} }
public IEnumerable<IOrderTargeter> Orders public IEnumerable<IOrderTargeter> Orders
{ {
get { yield return new DeployOrderTargeter("Unload", 10, () => CanUnload(self)); } get { yield return new DeployOrderTargeter("Unload", 10, CanUnload); }
} }
public Order IssueOrder(Actor self, IOrderTargeter order, Target target, bool queued) public Order IssueOrder(Actor self, IOrderTargeter order, Target target, bool queued)
@@ -78,41 +82,34 @@ namespace OpenRA.Mods.RA
{ {
if (order.OrderString == "Unload") if (order.OrderString == "Unload")
{ {
if (!CanUnload(self)) if (!CanUnload())
return; return;
self.CancelActivity(); self.CancelActivity();
self.QueueActivity(new UnloadCargo(true)); self.QueueActivity(new UnloadCargo(self, true));
} }
} }
bool CanUnload(Actor self) IEnumerable<CPos> GetAdjacentCells()
{ {
if (IsEmpty(self)) return Util.AdjacentCells(Target.FromActor(self)).Where(c => self.Location != c);
return false; }
// Cannot unload mid-air bool CanUnload()
var ios = self.TraitOrDefault<IOccupySpace>(); {
if (ios != null && ios.CenterPosition.Z > info.MaximumUnloadAltitude.Range) return !IsEmpty(self) && self.CenterPosition.Z == 0
return false; && CurrentAdjacentCells.Any(c => Passengers.Any(p => p.Trait<IPositionable>().CanEnterCell(c)));
// TODO: Check if there is a free tile to unload to
return true;
} }
public bool CanLoad(Actor self, Actor a) public bool CanLoad(Actor self, Actor a)
{ {
if (!HasSpace(GetWeight(a))) return HasSpace(GetWeight(a)) && self.CenterPosition.Z == 0;
return false;
// Cannot load mid-air
return self.CenterPosition.Z <= info.MaximumUnloadAltitude.Range;
} }
public string CursorForOrder(Actor self, Order order) public string CursorForOrder(Actor self, Order order)
{ {
if (order.OrderString != "Unload") return null; if (order.OrderString != "Unload") return null;
return CanUnload(self) ? "deploy" : "deploy-blocked"; return CanUnload() ? "deploy" : "deploy-blocked";
} }
public string VoicePhraseForOrder(Actor self, Order order) public string VoicePhraseForOrder(Actor self, Order order)
@@ -121,10 +118,10 @@ namespace OpenRA.Mods.RA
return self.HasVoice("Unload") ? "Unload" : "Move"; return self.HasVoice("Unload") ? "Unload" : "Move";
} }
public bool HasSpace(int weight) { return totalWeight + weight <= info.MaxWeight; } public bool HasSpace(int weight) { return totalWeight + weight <= Info.MaxWeight; }
public bool IsEmpty(Actor self) { return cargo.Count == 0; } public bool IsEmpty(Actor self) { return cargo.Count == 0; }
public Actor Peek(Actor self) { return cargo[0]; } public Actor Peek(Actor self) { return cargo[0]; }
static int GetWeight(Actor a) { return a.Info.Traits.Get<PassengerInfo>().Weight; } static int GetWeight(Actor a) { return a.Info.Traits.Get<PassengerInfo>().Weight; }
@@ -142,7 +139,7 @@ namespace OpenRA.Mods.RA
public IEnumerable<PipType> GetPips(Actor self) public IEnumerable<PipType> GetPips(Actor self)
{ {
int numPips = info.PipCount; var numPips = Info.PipCount;
for (int i = 0; i < numPips; i++) for (int i = 0; i < numPips; i++)
yield return GetPipAt(i); yield return GetPipAt(i);
@@ -150,7 +147,7 @@ namespace OpenRA.Mods.RA
PipType GetPipAt(int i) PipType GetPipAt(int i)
{ {
var n = i * info.MaxWeight / info.PipCount; var n = i * Info.MaxWeight / Info.PipCount;
foreach (var c in cargo) foreach (var c in cargo)
{ {
@@ -191,6 +188,16 @@ namespace OpenRA.Mods.RA
p.Owner = newOwner; p.Owner = newOwner;
}); });
} }
public void Tick(Actor self)
{
var cell = self.CenterPosition.ToCPos();
if (currentCell != cell)
{
currentCell = cell;
CurrentAdjacentCells = GetAdjacentCells();
}
}
} }
public interface INotifyPassengerEntered { void PassengerEntered(Actor self, Actor passenger); } public interface INotifyPassengerEntered { void PassengerEntered(Actor self, Actor passenger); }
@@ -198,7 +205,8 @@ namespace OpenRA.Mods.RA
public class CargoInit : IActorInit<Actor[]> public class CargoInit : IActorInit<Actor[]>
{ {
[FieldFromYamlKey] public readonly Actor[] value = {}; [FieldFromYamlKey]
public readonly Actor[] value = { };
public CargoInit() { } public CargoInit() { }
public CargoInit(Actor[] init) { value = init; } public CargoInit(Actor[] init) { value = init; }
public Actor[] Value(World world) { return value; } public Actor[] Value(World world) { return value; }

View File

@@ -285,7 +285,7 @@ namespace OpenRA.Mods.RA.Missions
self.QueueActivity(new Move.Move(reinforcementsEntryPoint.Location)); self.QueueActivity(new Move.Move(reinforcementsEntryPoint.Location));
self.QueueActivity(new RemoveSelf()); self.QueueActivity(new RemoveSelf());
})); }));
lst.QueueActivity(new UnloadCargo(true)); lst.QueueActivity(new UnloadCargo(lst, true));
lst.QueueActivity(new Transform(lst, "lst.unselectable.nocargo") { SkipMakeAnims = true }); lst.QueueActivity(new Transform(lst, "lst.unselectable.nocargo") { SkipMakeAnims = true });
} }
@@ -342,7 +342,7 @@ namespace OpenRA.Mods.RA.Missions
})); }));
lst.QueueActivity(new Move.Move(spyReinforcementsUnloadPoint.Location)); lst.QueueActivity(new Move.Move(spyReinforcementsUnloadPoint.Location));
lst.QueueActivity(new Wait(10)); lst.QueueActivity(new Wait(10));
lst.QueueActivity(new UnloadCargo(true)); lst.QueueActivity(new UnloadCargo(lst, true));
lst.QueueActivity(new Transform(lst, "lst.unselectable.nocargo") { SkipMakeAnims = true }); lst.QueueActivity(new Transform(lst, "lst.unselectable.nocargo") { SkipMakeAnims = true });
} }

View File

@@ -224,7 +224,7 @@ namespace OpenRA.Mods.RA.Missions
chinook.QueueActivity(new HeliFly(chinook, Target.FromPos(lz.CenterPosition + offset))); // no reservation of hpad but it's not needed chinook.QueueActivity(new HeliFly(chinook, Target.FromPos(lz.CenterPosition + offset))); // no reservation of hpad but it's not needed
chinook.QueueActivity(new Turn(0)); chinook.QueueActivity(new Turn(0));
chinook.QueueActivity(new HeliLand(false)); chinook.QueueActivity(new HeliLand(false));
chinook.QueueActivity(new UnloadCargo(true)); chinook.QueueActivity(new UnloadCargo(chinook, true));
chinook.QueueActivity(new Wait(150)); chinook.QueueActivity(new Wait(150));
chinook.QueueActivity(new HeliFly(chinook, Target.FromCell(entry))); chinook.QueueActivity(new HeliFly(chinook, Target.FromCell(entry)));
chinook.QueueActivity(new RemoveSelf()); chinook.QueueActivity(new RemoveSelf());
@@ -313,7 +313,7 @@ namespace OpenRA.Mods.RA.Missions
{ {
var cargo = self.Trait<Cargo>(); var cargo = self.Trait<Cargo>();
if (!cargo.IsEmpty(self) && !(self.GetCurrentActivity() is UnloadCargo)) if (!cargo.IsEmpty(self) && !(self.GetCurrentActivity() is UnloadCargo))
self.QueueActivity(false, new UnloadCargo(true)); self.QueueActivity(false, new UnloadCargo(self, true));
} }
} }
} }

View File

@@ -63,7 +63,7 @@ namespace OpenRA.Mods.RA.Missions
chinook.QueueActivity(new HeliFly(chinook, Target.FromCell(lz))); chinook.QueueActivity(new HeliFly(chinook, Target.FromCell(lz)));
chinook.QueueActivity(new Turn(0)); chinook.QueueActivity(new Turn(0));
chinook.QueueActivity(new HeliLand(true)); chinook.QueueActivity(new HeliLand(true));
chinook.QueueActivity(new UnloadCargo(true)); chinook.QueueActivity(new UnloadCargo(chinook, true));
chinook.QueueActivity(new CallFunc(() => afterUnload(unit))); chinook.QueueActivity(new CallFunc(() => afterUnload(unit)));
chinook.QueueActivity(new Wait(150)); chinook.QueueActivity(new Wait(150));
chinook.QueueActivity(new HeliFly(chinook, Target.FromCell(exit))); chinook.QueueActivity(new HeliFly(chinook, Target.FromCell(exit)));

View File

@@ -333,6 +333,7 @@
<Compile Include="Render\RenderInfantry.cs" /> <Compile Include="Render\RenderInfantry.cs" />
<Compile Include="Render\RenderInfantryPanic.cs" /> <Compile Include="Render\RenderInfantryPanic.cs" />
<Compile Include="Render\RenderSpy.cs" /> <Compile Include="Render\RenderSpy.cs" />
<Compile Include="Render\RenderLandingCraft.cs" />
<Compile Include="Render\RenderUnit.cs" /> <Compile Include="Render\RenderUnit.cs" />
<Compile Include="Render\RenderUnitReload.cs" /> <Compile Include="Render\RenderUnitReload.cs" />
<Compile Include="Render\WithBuildingExplosion.cs" /> <Compile Include="Render\WithBuildingExplosion.cs" />

View File

@@ -0,0 +1,81 @@
#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.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Render
{
public class RenderLandingCraftInfo : RenderUnitInfo
{
public readonly string[] OpenTerrainTypes = { "Clear" };
public readonly string OpenAnim = "open";
public readonly string UnloadAnim = "unload";
public override object Create(ActorInitializer init) { return new RenderLandingCraft(init.self, this); }
}
public class RenderLandingCraft : RenderUnit
{
readonly Actor self;
readonly Cargo cargo;
readonly RenderLandingCraftInfo info;
bool open;
public RenderLandingCraft(Actor self, RenderLandingCraftInfo info)
: base(self)
{
this.self = self;
cargo = self.Trait<Cargo>();
this.info = info;
}
public bool ShouldBeOpen()
{
if (self.CenterPosition.Z > 0)
return false;
return cargo.CurrentAdjacentCells
.Any(c => info.OpenTerrainTypes.Contains(self.World.GetTerrainType(c)));
}
void Open()
{
if (open || !anim.HasSequence(info.OpenAnim))
return;
open = true;
PlayCustomAnimation(self, info.OpenAnim, () =>
{
if (anim.HasSequence(info.UnloadAnim))
PlayCustomAnimRepeating(self, info.UnloadAnim);
});
}
void Close()
{
if (!open || !anim.HasSequence(info.OpenAnim))
return;
open = false;
PlayCustomAnimBackwards(self, info.OpenAnim, null);
}
public override void Tick(Actor self)
{
if (ShouldBeOpen())
Open();
else
Close();
base.Tick(self);
}
}
}

View File

@@ -80,7 +80,7 @@ Actor.Hunt = function(actor)
end end
Actor.UnloadCargo = function(actor, unloadAll) Actor.UnloadCargo = function(actor, unloadAll)
actor:QueueActivity(OpenRA.New("UnloadCargo", { unloadAll })) actor:QueueActivity(OpenRA.New("UnloadCargo", { actor, unloadAll }))
end end
Actor.Harvest = function(actor) Actor.Harvest = function(actor)

View File

@@ -66,8 +66,7 @@ tran:
Start: 32 Start: 32
Length: 4 Length: 4
unload: tran2 unload: tran2
Start: 32 Start: 35
Length: 4
icon: tranicon icon: tranicon
Start: 0 Start: 0

View File

@@ -47,6 +47,7 @@ lst:
open: open:
Start: 1 Start: 1
Length: 4 Length: 4
Tick: 150
unload: unload:
Start: 4 Start: 4
icon: lsticon icon: lsticon

View File

@@ -177,7 +177,7 @@ apc:
Start: 0 Start: 0
Length: 6 Length: 6
Facings: 8 Facings: 8
close: open:
Start: 32 Start: 32
Length: 3 Length: 3
unload: unload: