fix MoveAdjacentTo so it doesnt suck so much

This commit is contained in:
Chris Forbes
2011-09-07 19:13:51 +12:00
parent cc18a6a515
commit a3818a6aa2
5 changed files with 57 additions and 45 deletions

View File

@@ -136,6 +136,43 @@ namespace OpenRA.Traits
}
}
static IEnumerable<int2> Neighbours(int2 c, bool allowDiagonal)
{
yield return c;
yield return new int2(c.X - 1, c.Y);
yield return new int2(c.X + 1, c.Y);
yield return new int2(c.X, c.Y - 1);
yield return new int2(c.X, c.Y + 1);
if (allowDiagonal)
{
yield return new int2(c.X - 1, c.Y - 1);
yield return new int2(c.X + 1, c.Y - 1);
yield return new int2(c.X - 1, c.Y + 1);
yield return new int2(c.X + 1, c.Y + 1);
}
}
public static IEnumerable<int2> ExpandFootprint(IEnumerable<int2> cells, bool allowDiagonal)
{
var result = new Dictionary<int2, bool>();
foreach (var c in cells.SelectMany(c => Neighbours(c, allowDiagonal)))
result[c] = true;
return result.Keys;
}
public static IEnumerable<int2> AdjacentCells( Target target )
{
var cells = target.IsActor
? target.Actor.OccupiesSpace.OccupiedCells().Select(c => c.First).ToArray()
: new int2[] {};
if (cells.Length == 0)
cells = new [] { Util.CellContaining(target.CenterLocation) };
return Util.ExpandFootprint(cells, true);
}
static int2[] fvecs =
{
new int2( 0, -1331 ),

View File

@@ -34,7 +34,7 @@ namespace OpenRA.Mods.RA.Activities
// Todo: Queue a move order to the transport? need to be
// careful about units that can't path to the transport
if ((transport.Location - self.Location).Length > 1)
if ((transport.Location - self.Location).LengthSquared > 2)
return NextActivity;
cargo.Load(transport, self);

View File

@@ -8,6 +8,7 @@
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.RA.Move;
using OpenRA.Traits;
@@ -20,28 +21,14 @@ namespace OpenRA.Mods.RA.Activities
{
readonly Target target;
public MoveAdjacentTo( Actor target )
{
this.target = Target.FromActor(target);
}
public MoveAdjacentTo( Target target )
{
this.target = target;
}
public MoveAdjacentTo( Actor target ) { this.target = Target.FromActor(target); }
public MoveAdjacentTo( Target target ) { this.target = target; }
public override Activity Tick( Actor self )
{
if( IsCanceled || !target.IsValid) return NextActivity;
var mobile = self.Trait<Mobile>();
var cells = new Pair<int2, SubCell>[] {};
if (target.IsActor)
cells = target.Actor.OccupiesSpace.OccupiedCells().ToArray();
if (cells.Length == 0)
cells = new [] {
Pair.New(Util.CellContaining(target.CenterLocation), SubCell.FullCell) };
var ps1 = new PathSearch( self.World, mobile.Info, self.Owner )
{
@@ -50,18 +37,17 @@ namespace OpenRA.Mods.RA.Activities
inReverse = true
};
foreach( var cell in cells )
{
ps1.AddInitialCell( cell.First );
if( ( mobile.toCell - cell.First ).LengthSquared <= 2 )
foreach( var cell in Util.AdjacentCells(target) )
if (cell == self.Location)
return NextActivity;
}
else
ps1.AddInitialCell( cell );
ps1.heuristic = PathSearch.DefaultEstimator( mobile.toCell );
var ps2 = PathSearch.FromPoint( self.World, mobile.Info, self.Owner, mobile.toCell, Util.CellContaining(target.CenterLocation), true );
var ret = self.World.WorldActor.Trait<PathFinder>().FindBidiPath( ps1, ps2 );
if( ret.Count > 0 )
ret.RemoveAt( 0 );
return Util.SequenceActivities( mobile.MoveTo( () => ret ), this );
}
}

View File

@@ -63,16 +63,23 @@ namespace OpenRA.Mods.RA.Activities
return this;
var actor = cargo.Unload(self);
var exitPx = Util.CenterOfCell(exitTile.Value);
var currentPx = Util.CenterOfCell(self.Location);
self.World.AddFrameEndTask(w =>
{
if (actor.Destroyed) return;
var mobile = actor.Trait<Mobile>();
mobile.SetPosition(actor, self.Location);
mobile.Facing = Util.GetFacing( exitPx - currentPx, mobile.Facing );
mobile.SetPosition(actor, exitTile.Value);
mobile.AdjustPxPosition(actor, currentPx);
var speed = mobile.MovementSpeedForCell(actor, exitTile.Value);
var length = speed > 0 ? ((int)(exitPx - currentPx).Length * 3 / speed) : 0;
w.Add(actor);
actor.CancelActivity();
actor.QueueActivity(new Drag(currentPx, exitPx, length));
actor.QueueActivity(mobile.MoveTo(exitTile.Value, 0));
actor.SetTargetLine(Target.FromCell(exitTile.Value), Color.Green, false);
});

View File

@@ -149,24 +149,6 @@ namespace OpenRA.Mods.RA
return null;
}
IEnumerable<int2> Neighbours(int2 c)
{
/* only return 4-neighbors for now, maybe add 8s later. */
yield return c;
yield return new int2(c.X - 1, c.Y);
yield return new int2(c.X + 1, c.Y);
yield return new int2(c.X, c.Y - 1);
yield return new int2(c.X, c.Y + 1);
}
IEnumerable<int2> ExpandFootprint(IEnumerable<int2> cells)
{
var result = new Dictionary<int2, bool>();
foreach (var c in cells.SelectMany(c => Neighbours(c)))
result[c] = true;
return result.Keys;
}
bool NoBuildingsUnder(IEnumerable<int2> cells)
{
var bi = world.WorldActor.Trait<BuildingInfluence>();
@@ -181,8 +163,8 @@ namespace OpenRA.Mods.RA
foreach (var t in world.FindTilesInCircle(baseCenter, k))
if (world.CanPlaceBuilding(item.Item, bi, t, null))
if (bi.IsCloseEnoughToBase(world, p, item.Item, t))
if (NoBuildingsUnder(ExpandFootprint(
FootprintUtils.Tiles( item.Item, bi, t ))))
if (NoBuildingsUnder(Util.ExpandFootprint(
FootprintUtils.Tiles( item.Item, bi, t ), false)))
return t;
return null; // i don't know where to put it.