Skip to content

Commit

Permalink
Allow units to move between subcells. Visually not quite right.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote committed Feb 4, 2011
1 parent 4aaafd1 commit 9c63292
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
10 changes: 8 additions & 2 deletions OpenRA.Game/Traits/World/UnitInfluence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,14 @@ public bool HasFreeSubCell(int2 a)
if (!AnyUnitsAt(a))
return true;

return new[]{SubCell.BottomLeft, SubCell.BottomRight, SubCell.Center,
SubCell.TopLeft, SubCell.TopRight}.Any(b => !AnyUnitsAt(a,b));
return new[]{SubCell.TopLeft, SubCell.TopRight, SubCell.Center,
SubCell.BottomLeft, SubCell.BottomRight}.Any(b => !AnyUnitsAt(a,b));
}

public SubCell GetFreeSubcell(int2 a, SubCell preferred)
{
return new[]{preferred, SubCell.TopLeft, SubCell.TopRight, SubCell.Center,
SubCell.BottomLeft, SubCell.BottomRight}.First(b => !AnyUnitsAt(a,b));
}

public bool AnyUnitsAt(int2 a)
Expand Down
10 changes: 6 additions & 4 deletions OpenRA.Mods.RA/Move/Mobile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public int MovementCostForCell(World world, int2 cell)
public readonly Dictionary<SubCell, int2> SubCellOffsets = new Dictionary<SubCell, int2>()
{
{SubCell.TopLeft, new int2(-6,-6)},
{SubCell.TopRight, new int2(6,6)},
{SubCell.TopRight, new int2(6,-6)},
{SubCell.Center, new int2(0,0)},
{SubCell.BottomLeft, new int2(-6,6)},
{SubCell.BottomRight, new int2(6,6)},
Expand Down Expand Up @@ -155,12 +155,14 @@ public int Altitude
[Sync]
public int PathHash; // written by Move.EvalPath, to temporarily debug this crap.

public void SetLocation(int2 from, int2 to)
public void SetLocation(int2 from, SubCell fromSub, int2 to, SubCell toSub)
{
if (fromCell == from && toCell == to) return;
RemoveInfluence();
__fromCell = from;
__toCell = to;
__fromSubCell = fromSub;
__toSubCell = toSub;
AddInfluence();
}

Expand Down Expand Up @@ -194,15 +196,15 @@ public Mobile(ActorInitializer init, MobileInfo info)

public void SetPosition(Actor self, int2 cell)
{
SetLocation(cell, cell);
SetLocation(cell,__fromSubCell, cell,__fromSubCell);
PxPosition = Util.CenterOfCell(fromCell) + Info.SubCellOffsets[__fromSubCell];
FinishedMoving(self);
}

public void SetPxPosition(Actor self, int2 px)
{
var cell = Util.CellContaining(px);
SetLocation(cell, cell);
SetLocation(cell,__fromSubCell, cell,__fromSubCell);
PxPosition = px;
FinishedMoving(self);
}
Expand Down
30 changes: 17 additions & 13 deletions OpenRA.Mods.RA/Move/Move.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using OpenRA.Mods.RA.Activities;
using OpenRA.Traits;
using OpenRA.Traits.Activities;
using OpenRA.FileFormats;

namespace OpenRA.Mods.RA.Move
{
Expand Down Expand Up @@ -132,16 +133,16 @@ public override IActivity Tick( Actor self )
if( nextCell == null )
return this;

int2 dir = nextCell.Value - mobile.fromCell;
int2 dir = nextCell.Value.First - mobile.fromCell;
var firstFacing = Util.GetFacing( dir, mobile.Facing );
if( firstFacing != mobile.Facing )
{
path.Add( nextCell.Value );
path.Add( nextCell.Value.First );
return Util.SequenceActivities( new Turn( firstFacing ), this );
}
else
{
mobile.SetLocation( mobile.fromCell, nextCell.Value );
mobile.SetLocation( mobile.fromCell, mobile.__fromSubCell, nextCell.Value.First, nextCell.Value.Second );
var move = new MoveFirstHalf(
this,
Util.CenterOfCell( mobile.fromCell ),
Expand Down Expand Up @@ -183,7 +184,7 @@ void NudgeBlocker(Actor self, int2 nextCell)
nudge.OnNudge(blocker, self);
}

int2? PopPath( Actor self, Mobile mobile )
Pair<int2, SubCell>? PopPath( Actor self, Mobile mobile )
{
if( path.Count == 0 ) return null;
var nextCell = path[ path.Count - 1 ];
Expand Down Expand Up @@ -229,7 +230,9 @@ void NudgeBlocker(Actor self, int2 nextCell)
hasNudged = false;
hasWaited = false;
path.RemoveAt( path.Count - 1 );
return nextCell;

var subCell = self.World.WorldActor.Trait<UnitInfluence>().GetFreeSubcell(nextCell, mobile.__fromSubCell);
return Pair.New(nextCell, subCell);
}

protected override bool OnCancel( Actor self )
Expand Down Expand Up @@ -335,22 +338,23 @@ protected override MovePart OnComplete( Actor self, Mobile mobile, Move parent )
var nextCell = parent.PopPath( self, mobile );
if( nextCell != null )
{
if( ( nextCell - mobile.toCell ) != ( mobile.toCell - mobile.fromCell ) )
if( ( nextCell.Value.First - mobile.toCell ) != ( mobile.toCell - mobile.fromCell ) )
{
var ret = new MoveFirstHalf(
move,
Util.BetweenCells( mobile.fromCell, mobile.toCell ),
Util.BetweenCells( mobile.toCell, nextCell.Value ),
Util.BetweenCells( mobile.toCell, nextCell.Value.First ),
mobile.__fromSubCell,
mobile.__toSubCell,
nextCell.Value.Second,
mobile.Facing,
Util.GetNearestFacing( mobile.Facing, Util.GetFacing( nextCell.Value - mobile.toCell, mobile.Facing ) ),
Util.GetNearestFacing( mobile.Facing, Util.GetFacing( nextCell.Value.First - mobile.toCell, mobile.Facing ) ),
moveFraction - moveFractionTotal );
mobile.SetLocation( mobile.toCell, nextCell.Value );

mobile.SetLocation( mobile.toCell, mobile.__toSubCell, nextCell.Value.First, nextCell.Value.Second);
return ret;
}
else
parent.path.Add( nextCell.Value );
parent.path.Add( nextCell.Value.First );
}
var ret2 = new MoveSecondHalf(
move,
Expand All @@ -361,7 +365,7 @@ protected override MovePart OnComplete( Actor self, Mobile mobile, Move parent )
mobile.Facing,
mobile.Facing,
moveFraction - moveFractionTotal );
mobile.SetLocation( mobile.toCell, mobile.toCell );
mobile.SetLocation( mobile.toCell, mobile.__toSubCell, mobile.toCell, mobile.__toSubCell );
return ret2;
}
}
Expand All @@ -376,7 +380,7 @@ public MoveSecondHalf( Move move, int2 from, int2 to, SubCell fromSubCell, SubCe
protected override MovePart OnComplete( Actor self, Mobile mobile, Move parent )
{
mobile.PxPosition = Util.CenterOfCell( mobile.toCell );
mobile.SetLocation( mobile.toCell, mobile.toCell );
mobile.SetLocation( mobile.toCell, mobile.__toSubCell, mobile.toCell, mobile.__toSubCell );
mobile.FinishedMoving(self);
return null;
}
Expand Down

0 comments on commit 9c63292

Please sign in to comment.