Skip to content

Commit

Permalink
Replace MoveIntoWorld with ReturnToCell/AssociateWithAirfield.
Browse files Browse the repository at this point in the history
  • Loading branch information
pchote committed Oct 15, 2019
1 parent 5dcbe01 commit 30e0d60
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 37 deletions.
2 changes: 1 addition & 1 deletion OpenRA.Mods.Cnc/Traits/TDGunboat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public Activity MoveWithinRange(Target target, WDist minRange, WDist maxRange,
WPos? initialTargetPosition = null, Color? targetLineColor = null) { return null; }
public Activity MoveFollow(Actor self, Target target, WDist minRange, WDist maxRange,
WPos? initialTargetPosition = null, Color? targetLineColor = null) { return null; }
public Activity MoveIntoWorld(Actor self, int delay = 0) { return null; }
public Activity ReturnToCell(Actor self) { return null; }
public Activity MoveToTarget(Actor self, Target target,
WPos? initialTargetPosition = null, Color? targetLineColor = null) { return null; }
public Activity MoveIntoTarget(Actor self, Target target) { return null; }
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Activities/Enter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public override bool Tick(Actor self)

case EnterState.Exiting:
{
QueueChild(move.MoveIntoWorld(self));
QueueChild(move.ReturnToCell(self));
lastState = EnterState.Finished;
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions OpenRA.Mods.Common/ActorInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ public FacingInit() { }
public int Value(World world) { return value; }
}

public class MoveIntoWorldDelayInit : IActorInit<int>
public class CreationActivityDelayInit : IActorInit<int>
{
[FieldFromYamlKey]
readonly int value = 0;

public MoveIntoWorldDelayInit() { }
public MoveIntoWorldDelayInit(int init) { value = init; }
public CreationActivityDelayInit() { }
public CreationActivityDelayInit(int init) { value = init; }
public int Value(World world) { return value; }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void MoveIntoWorld(CPos cell)
var pos = Self.CenterPosition;
mobile.SetPosition(Self, cell);
mobile.SetVisualPosition(Self, pos);
Self.QueueActivity(mobile.MoveIntoWorld(Self));
Self.QueueActivity(mobile.ReturnToCell(Self));
}

[ScriptActorPropertyActivity]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#endregion

using System.Linq;
using Eluant;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Traits;
using OpenRA.Scripting;
Expand All @@ -35,7 +36,13 @@ public TransportProperties(ScriptContext context, Actor self)
public int PassengerCount { get { return cargo.Passengers.Count(); } }

[Desc("Teleport an existing actor inside this transport.")]
public void LoadPassenger(Actor a) { cargo.Load(Self, a); }
public void LoadPassenger(Actor a)
{
if (!a.IsIdle)
throw new LuaException("LoadPassenger requires the passenger to be idle.");

cargo.Load(Self, a);
}

[Desc("Remove the first actor from the transport. This actor is not added to the world.")]
public Actor UnloadPassenger() { return cargo.Unload(Self); }
Expand Down
17 changes: 8 additions & 9 deletions OpenRA.Mods.Common/Traits/Air/Aircraft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public class Aircraft : ITick, ISync, IFacing, IPositionable, IMove, IIssueOrder

IEnumerable<CPos> landingCells = Enumerable.Empty<CPos>();
bool requireForceMove;
int moveIntoWorldDelay;
int creationActivityDelay;

public static WPos GroundPosition(Actor self)
{
Expand Down Expand Up @@ -251,7 +251,9 @@ public Aircraft(ActorInitializer init, AircraftInfo info)
SetPosition(self, init.Get<CenterPositionInit, WPos>());

Facing = init.Contains<FacingInit>() ? init.Get<FacingInit, int>() : Info.InitialFacing;
moveIntoWorldDelay = init.Contains<MoveIntoWorldDelayInit>() ? init.Get<MoveIntoWorldDelayInit, int>() : 0;

if (init.Contains<CreationActivityDelayInit>())
creationActivityDelay = init.Get<CreationActivityDelayInit, int>();
}

public WDist LandAltitude
Expand Down Expand Up @@ -841,18 +843,15 @@ public Activity MoveFollow(Actor self, Target target, WDist minRange, WDist maxR
initialTargetPosition, targetLineColor);
}

public Activity MoveIntoWorld(Actor self, int delay = 0)
{
return new MoveIntoWorldActivity(self, delay);
}
public Activity ReturnToCell(Actor self) { return null; }

class MoveIntoWorldActivity : Activity
class AssociateWithAirfieldActivity : Activity
{
readonly Actor self;
readonly Aircraft aircraft;
readonly int delay;

public MoveIntoWorldActivity(Actor self, int delay = 0)
public AssociateWithAirfieldActivity(Actor self, int delay = 0)
{
this.self = self;
aircraft = self.Trait<Aircraft>();
Expand Down Expand Up @@ -1164,7 +1163,7 @@ void IActorPreviewInitModifier.ModifyActorPreviewInit(Actor self, TypeDictionary

Activity ICreationActivity.GetCreationActivity()
{
return MoveIntoWorld(self, moveIntoWorldDelay);
return new AssociateWithAirfieldActivity(self, creationActivityDelay);
}

public class AircraftMoveOrderTargeter : IOrderTargeter
Expand Down
12 changes: 6 additions & 6 deletions OpenRA.Mods.Common/Traits/Cargo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,11 @@ public void Load(Actor self, Actor a)
// If not initialized then this will be notified in the first tick
if (initialized)
{
foreach (var npe in self.TraitsImplementing<INotifyPassengerEntered>())
npe.OnPassengerEntered(self, a);

foreach (var nec in a.TraitsImplementing<INotifyEnteredCargo>())
nec.OnEnteredCargo(a, self);

foreach (var npe in self.TraitsImplementing<INotifyPassengerEntered>())
npe.OnPassengerEntered(self, a);
}

var p = a.Trait<Passenger>();
Expand Down Expand Up @@ -507,11 +507,11 @@ void ITick.Tick(Actor self)
{
c.Trait<Passenger>().Transport = self;

foreach (var npe in self.TraitsImplementing<INotifyPassengerEntered>())
npe.OnPassengerEntered(self, c);

foreach (var nec in c.TraitsImplementing<INotifyEnteredCargo>())
nec.OnEnteredCargo(c, self);

foreach (var npe in self.TraitsImplementing<INotifyPassengerEntered>())
npe.OnPassengerEntered(self, c);
}

initialized = true;
Expand Down
34 changes: 22 additions & 12 deletions OpenRA.Mods.Common/Traits/Mobile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ public class Mobile : PausableConditionalTrait<MobileInfo>, IIssueOrder, IResolv
{
readonly Actor self;
readonly Lazy<IEnumerable<int>> speedModifiers;
readonly int moveIntoWorldDelay;

readonly bool returnToCellOnCreation;
readonly bool returnToCellOnCreationRecalculateSubCell = true;
readonly int creationActivityDelay;

#region IMove CurrentMovementTypes
MovementType movementTypes;
Expand Down Expand Up @@ -228,7 +231,10 @@ public Mobile(ActorInitializer init, MobileInfo info)

ToSubCell = FromSubCell = info.LocomotorInfo.SharesCell ? init.World.Map.Grid.DefaultSubCell : SubCell.FullCell;
if (init.Contains<SubCellInit>())
{
FromSubCell = ToSubCell = init.Get<SubCellInit, SubCell>();
returnToCellOnCreationRecalculateSubCell = false;
}

if (init.Contains<LocationInit>())
{
Expand All @@ -238,12 +244,16 @@ public Mobile(ActorInitializer init, MobileInfo info)

Facing = init.Contains<FacingInit>() ? init.Get<FacingInit, int>() : info.InitialFacing;

// Sets the visual position to WPos accuracy
// Use LocationInit if you want to insert the actor into the ActorMap!
// Sets the initial visual position
// Unit will move into the cell grid (defined by LocationInit) as its initial activity
if (init.Contains<CenterPositionInit>())
{
SetVisualPosition(self, init.Get<CenterPositionInit, WPos>());
returnToCellOnCreation = true;
}

moveIntoWorldDelay = init.Contains<MoveIntoWorldDelayInit>() ? init.Get<MoveIntoWorldDelayInit, int>() : 0;
if (init.Contains<CreationActivityDelayInit>())
creationActivityDelay = init.Get<CreationActivityDelayInit, int>();
}

protected override void Created(Actor self)
Expand Down Expand Up @@ -568,27 +578,27 @@ public Activity MoveFollow(Actor self, Target target, WDist minRange, WDist maxR
return WrapMove(new Follow(self, target, minRange, maxRange, initialTargetPosition, targetLineColor));
}

public Activity MoveIntoWorld(Actor self, int delay = 0)
public Activity ReturnToCell(Actor self)
{
return new MoveIntoWorldActivity(self, delay);
return new ReturnToCellActivity(self);
}

class MoveIntoWorldActivity : Activity
class ReturnToCellActivity : Activity
{
readonly Actor self;
readonly Mobile mobile;
readonly bool recalculateSubCell;

CPos cell;
SubCell subCell;
WPos pos;
int delay;

public MoveIntoWorldActivity(Actor self, int delay = 0)
public ReturnToCellActivity(Actor self, int delay = 0, bool recalculateSubCell = false)
{
this.self = self;
mobile = self.Trait<Mobile>();
IsInterruptible = false;
this.delay = delay;
this.recalculateSubCell = recalculateSubCell;
}

protected override void OnFirstRun(Actor self)
Expand All @@ -604,7 +614,7 @@ public override bool Tick(Actor self)
cell = mobile.ToCell;
subCell = mobile.ToSubCell;

if (subCell == SubCell.Any)
if (recalculateSubCell)
subCell = mobile.Info.LocomotorInfo.SharesCell ? self.World.ActorMap.FreeSubCell(cell, subCell) : SubCell.FullCell;

// TODO: solve/reduce cell is full problem
Expand Down Expand Up @@ -898,7 +908,7 @@ string IOrderVoice.VoicePhraseForOrder(Actor self, Order order)

Activity ICreationActivity.GetCreationActivity()
{
return MoveIntoWorld(self, moveIntoWorldDelay);
return returnToCellOnCreation ? new ReturnToCellActivity(self, creationActivityDelay, returnToCellOnCreationRecalculateSubCell) : null;
}

class MoveOrderTargeter : IOrderTargeter
Expand Down
10 changes: 10 additions & 0 deletions OpenRA.Mods.Common/Traits/Passenger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ void INotifyEnteredCargo.OnEnteredCargo(Actor self, Actor cargo)
if (specificCargoToken == ConditionManager.InvalidConditionToken && Info.CargoConditions.TryGetValue(cargo.Info.Name, out specificCargoCondition))
specificCargoToken = conditionManager.GrantCondition(self, specificCargoCondition);
}

// Allow scripted / initial actors to move from the unload point back into the cell grid on unload
// This is handled by the RideTransport activity for player-loaded cargo
if (self.IsIdle)
{
// IMove is not used anywhere else in this trait, there is no benefit to caching it from Created.
var move = self.TraitOrDefault<IMove>();
if (move != null)
self.QueueActivity(move.ReturnToCell(self));
}
}

void INotifyExitedCargo.OnExitedCargo(Actor self, Actor cargo)
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Traits/Production.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public virtual void DoProduction(Actor self, ActorInfo producee, ExitInfo exitin
td.Add(new CenterPositionInit(spawn));
td.Add(new FacingInit(initialFacing));
if (exitinfo != null)
td.Add(new MoveIntoWorldDelayInit(exitinfo.ExitDelay));
td.Add(new CreationActivityDelayInit(exitinfo.ExitDelay));
}

self.World.AddFrameEndTask(w =>
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/Traits/ProductionParadrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public override void DoProduction(Actor self, ActorInfo producee, ExitInfo exiti
td.Add(new LocationInit(exit));
td.Add(new CenterPositionInit(spawn));
td.Add(new FacingInit(initialFacing));
td.Add(new MoveIntoWorldDelayInit(exitinfo.ExitDelay));
td.Add(new CreationActivityDelayInit(exitinfo.ExitDelay));
}

self.World.AddFrameEndTask(w =>
Expand Down
2 changes: 1 addition & 1 deletion OpenRA.Mods.Common/TraitsInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ Activity MoveFollow(Actor self, Target target, WDist minRange, WDist maxRange,
WPos? initialTargetPosition = null, Color? targetLineColor = null);
Activity MoveToTarget(Actor self, Target target,
WPos? initialTargetPosition = null, Color? targetLineColor = null);
Activity MoveIntoWorld(Actor self, int delay = 0);
Activity ReturnToCell(Actor self);
Activity MoveIntoTarget(Actor self, Target target);
Activity VisualMove(Actor self, WPos fromPos, WPos toPos);
int EstimatedMoveDuration(Actor self, WPos fromPos, WPos toPos);
Expand Down

0 comments on commit 30e0d60

Please sign in to comment.