fix MoveAdjacentTo so it doesnt suck so much
This commit is contained in:
@@ -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 =
|
static int2[] fvecs =
|
||||||
{
|
{
|
||||||
new int2( 0, -1331 ),
|
new int2( 0, -1331 ),
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ namespace OpenRA.Mods.RA.Activities
|
|||||||
|
|
||||||
// Todo: Queue a move order to the transport? need to be
|
// Todo: Queue a move order to the transport? need to be
|
||||||
// careful about units that can't path to the transport
|
// 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;
|
return NextActivity;
|
||||||
|
|
||||||
cargo.Load(transport, self);
|
cargo.Load(transport, self);
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
*/
|
*/
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using OpenRA.Mods.RA.Move;
|
using OpenRA.Mods.RA.Move;
|
||||||
using OpenRA.Traits;
|
using OpenRA.Traits;
|
||||||
@@ -20,28 +21,14 @@ namespace OpenRA.Mods.RA.Activities
|
|||||||
{
|
{
|
||||||
readonly Target target;
|
readonly Target target;
|
||||||
|
|
||||||
public MoveAdjacentTo( Actor target )
|
public MoveAdjacentTo( Actor target ) { this.target = Target.FromActor(target); }
|
||||||
{
|
public MoveAdjacentTo( Target target ) { this.target = target; }
|
||||||
this.target = Target.FromActor(target);
|
|
||||||
}
|
|
||||||
|
|
||||||
public MoveAdjacentTo( Target target )
|
|
||||||
{
|
|
||||||
this.target = target;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override Activity Tick( Actor self )
|
public override Activity Tick( Actor self )
|
||||||
{
|
{
|
||||||
if( IsCanceled || !target.IsValid) return NextActivity;
|
if( IsCanceled || !target.IsValid) return NextActivity;
|
||||||
|
|
||||||
var mobile = self.Trait<Mobile>();
|
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 )
|
var ps1 = new PathSearch( self.World, mobile.Info, self.Owner )
|
||||||
{
|
{
|
||||||
@@ -50,18 +37,17 @@ namespace OpenRA.Mods.RA.Activities
|
|||||||
inReverse = true
|
inReverse = true
|
||||||
};
|
};
|
||||||
|
|
||||||
foreach( var cell in cells )
|
foreach( var cell in Util.AdjacentCells(target) )
|
||||||
{
|
if (cell == self.Location)
|
||||||
ps1.AddInitialCell( cell.First );
|
|
||||||
if( ( mobile.toCell - cell.First ).LengthSquared <= 2 )
|
|
||||||
return NextActivity;
|
return NextActivity;
|
||||||
}
|
else
|
||||||
|
ps1.AddInitialCell( cell );
|
||||||
|
|
||||||
ps1.heuristic = PathSearch.DefaultEstimator( mobile.toCell );
|
ps1.heuristic = PathSearch.DefaultEstimator( mobile.toCell );
|
||||||
|
|
||||||
var ps2 = PathSearch.FromPoint( self.World, mobile.Info, self.Owner, mobile.toCell, Util.CellContaining(target.CenterLocation), true );
|
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 );
|
var ret = self.World.WorldActor.Trait<PathFinder>().FindBidiPath( ps1, ps2 );
|
||||||
if( ret.Count > 0 )
|
|
||||||
ret.RemoveAt( 0 );
|
|
||||||
return Util.SequenceActivities( mobile.MoveTo( () => ret ), this );
|
return Util.SequenceActivities( mobile.MoveTo( () => ret ), this );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,16 +63,23 @@ namespace OpenRA.Mods.RA.Activities
|
|||||||
return this;
|
return this;
|
||||||
|
|
||||||
var actor = cargo.Unload(self);
|
var actor = cargo.Unload(self);
|
||||||
|
var exitPx = Util.CenterOfCell(exitTile.Value);
|
||||||
|
var currentPx = Util.CenterOfCell(self.Location);
|
||||||
|
|
||||||
self.World.AddFrameEndTask(w =>
|
self.World.AddFrameEndTask(w =>
|
||||||
{
|
{
|
||||||
if (actor.Destroyed) return;
|
if (actor.Destroyed) return;
|
||||||
|
|
||||||
var mobile = actor.Trait<Mobile>();
|
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);
|
w.Add(actor);
|
||||||
actor.CancelActivity();
|
actor.CancelActivity();
|
||||||
|
actor.QueueActivity(new Drag(currentPx, exitPx, length));
|
||||||
actor.QueueActivity(mobile.MoveTo(exitTile.Value, 0));
|
actor.QueueActivity(mobile.MoveTo(exitTile.Value, 0));
|
||||||
actor.SetTargetLine(Target.FromCell(exitTile.Value), Color.Green, false);
|
actor.SetTargetLine(Target.FromCell(exitTile.Value), Color.Green, false);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -149,24 +149,6 @@ namespace OpenRA.Mods.RA
|
|||||||
return null;
|
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)
|
bool NoBuildingsUnder(IEnumerable<int2> cells)
|
||||||
{
|
{
|
||||||
var bi = world.WorldActor.Trait<BuildingInfluence>();
|
var bi = world.WorldActor.Trait<BuildingInfluence>();
|
||||||
@@ -181,8 +163,8 @@ namespace OpenRA.Mods.RA
|
|||||||
foreach (var t in world.FindTilesInCircle(baseCenter, k))
|
foreach (var t in world.FindTilesInCircle(baseCenter, k))
|
||||||
if (world.CanPlaceBuilding(item.Item, bi, t, null))
|
if (world.CanPlaceBuilding(item.Item, bi, t, null))
|
||||||
if (bi.IsCloseEnoughToBase(world, p, item.Item, t))
|
if (bi.IsCloseEnoughToBase(world, p, item.Item, t))
|
||||||
if (NoBuildingsUnder(ExpandFootprint(
|
if (NoBuildingsUnder(Util.ExpandFootprint(
|
||||||
FootprintUtils.Tiles( item.Item, bi, t ))))
|
FootprintUtils.Tiles( item.Item, bi, t ), false)))
|
||||||
return t;
|
return t;
|
||||||
|
|
||||||
return null; // i don't know where to put it.
|
return null; // i don't know where to put it.
|
||||||
|
|||||||
Reference in New Issue
Block a user