Streamline Land activity

Removed some redundant parameters, some redundant overloads
and made Land always consider LandAltitude relative to target.
This commit is contained in:
reaperrr
2019-04-26 01:30:49 +02:00
committed by Paul Chote
parent 14bd5ada1a
commit 0c2666b97e
11 changed files with 47 additions and 70 deletions

View File

@@ -72,7 +72,7 @@ namespace OpenRA.Mods.Cnc.Traits
}); });
actor.QueueActivity(new Fly(actor, Target.FromPos(self.CenterPosition + new WVec(landDistance, 0, 0)))); actor.QueueActivity(new Fly(actor, Target.FromPos(self.CenterPosition + new WVec(landDistance, 0, 0))));
actor.QueueActivity(new Land(actor, Target.FromActor(self), false)); actor.QueueActivity(new Land(actor, Target.FromActor(self)));
actor.QueueActivity(new CallFunc(() => actor.QueueActivity(new CallFunc(() =>
{ {
if (!self.IsInWorld || self.IsDead) if (!self.IsInWorld || self.IsDead)

View File

@@ -164,7 +164,7 @@ namespace OpenRA.Mods.Common.Activities
{ {
if (info.TurnToLand) if (info.TurnToLand)
self.QueueActivity(new Turn(self, info.InitialFacing)); self.QueueActivity(new Turn(self, info.InitialFacing));
self.QueueActivity(new Land(self, true)); self.QueueActivity(new Land(self));
activity = NextActivity; activity = NextActivity;
} }

View File

@@ -19,34 +19,23 @@ namespace OpenRA.Mods.Common.Activities
{ {
readonly Target target; readonly Target target;
readonly Aircraft aircraft; readonly Aircraft aircraft;
readonly bool requireSpace; readonly WVec offset;
readonly Actor ignoreActor;
readonly WDist landAltitude;
readonly bool ignoreTarget;
bool landingInitiated; bool landingInitiated;
bool soundPlayed; bool soundPlayed;
public Land(Actor self, Target t, bool requireSpace, WDist landAltitude, Actor ignoreActor = null) public Land(Actor self, Target t, WVec offset)
{ {
target = t; target = t;
aircraft = self.Trait<Aircraft>(); aircraft = self.Trait<Aircraft>();
this.requireSpace = requireSpace; this.offset = offset;
this.ignoreActor = ignoreActor;
this.landAltitude = landAltitude != WDist.Zero ? landAltitude : aircraft.Info.LandAltitude;
} }
public Land(Actor self, Target t, bool requireSpace, Actor ignoreActor = null) public Land(Actor self, Target t)
: this(self, t, requireSpace, WDist.Zero, ignoreActor) { } : this(self, t, WVec.Zero) { }
public Land(Actor self, bool requireSpace, WDist landAltitude, Actor ignoreActor = null) public Land(Actor self)
: this(self, Target.FromPos(self.CenterPosition), requireSpace, landAltitude, ignoreActor) : this(self, Target.FromPos(Aircraft.GroundPosition(self)), WVec.Zero) { }
{
ignoreTarget = true;
}
public Land(Actor self, bool requireSpace, Actor ignoreActor = null)
: this(self, requireSpace, WDist.Zero, ignoreActor) { }
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
@@ -57,22 +46,16 @@ namespace OpenRA.Mods.Common.Activities
return this; return this;
} }
if (!ignoreTarget && !target.IsValidFor(self)) if (IsCanceling || target.Type == TargetType.Invalid)
{
Cancel(self);
return NextActivity;
}
if (IsCanceling)
{ {
aircraft.RemoveInfluence(); aircraft.RemoveInfluence();
return NextActivity; return NextActivity;
} }
if (requireSpace && !landingInitiated) if (!landingInitiated)
{ {
var landingCell = !aircraft.Info.VTOL ? self.World.Map.CellContaining(target.CenterPosition) : self.Location; var landingCell = !aircraft.Info.VTOL ? self.World.Map.CellContaining(target.CenterPosition + offset) : self.Location;
if (!aircraft.CanLand(landingCell, ignoreActor)) if (!aircraft.CanLand(landingCell, target.Actor))
{ {
// Maintain holding pattern. // Maintain holding pattern.
if (!aircraft.Info.CanHover) if (!aircraft.Info.CanHover)
@@ -88,41 +71,38 @@ namespace OpenRA.Mods.Common.Activities
} }
var altitude = self.World.Map.DistanceAboveTerrain(self.CenterPosition); var altitude = self.World.Map.DistanceAboveTerrain(self.CenterPosition);
var landAltitude = self.World.Map.DistanceAboveTerrain(target.CenterPosition + offset) + aircraft.LandAltitude;
if (!soundPlayed && aircraft.Info.LandingSounds.Length > 0 && altitude != landAltitude)
{
Game.Sound.Play(SoundType.World, aircraft.Info.LandingSounds, self.World, aircraft.CenterPosition);
soundPlayed = true;
}
// For VTOLs we assume we've already arrived at the target location and just need to move downward
if (aircraft.Info.VTOL) if (aircraft.Info.VTOL)
{ {
var landAlt = !ignoreTarget ? self.World.Map.DistanceAboveTerrain(target.CenterPosition) : landAltitude; if (HeliFly.AdjustAltitude(self, aircraft, landAltitude))
if (!soundPlayed && aircraft.Info.LandingSounds.Length > 0 && altitude != landAlt)
PlayLandingSound(self);
if (HeliFly.AdjustAltitude(self, aircraft, landAlt))
return this; return this;
return NextActivity; return NextActivity;
} }
if (!soundPlayed && aircraft.Info.LandingSounds.Length > 0 && altitude != landAltitude) var d = (target.CenterPosition + offset) - self.CenterPosition;
PlayLandingSound(self);
var d = target.CenterPosition - self.CenterPosition;
// The next move would overshoot, so just set the final position // The next move would overshoot, so just set the final position
var move = aircraft.FlyStep(aircraft.Facing); var move = aircraft.FlyStep(aircraft.Facing);
if (d.HorizontalLengthSquared < move.HorizontalLengthSquared) if (d.HorizontalLengthSquared < move.HorizontalLengthSquared)
{ {
aircraft.SetPosition(self, target.CenterPosition); var landingAltVec = new WVec(WDist.Zero, WDist.Zero, aircraft.LandAltitude);
aircraft.SetPosition(self, target.CenterPosition + offset + landingAltVec);
return NextActivity; return NextActivity;
} }
var landingAlt = self.World.Map.DistanceAboveTerrain(target.CenterPosition); var landingAlt = self.World.Map.DistanceAboveTerrain(target.CenterPosition + offset) + aircraft.LandAltitude;
Fly.FlyToward(self, aircraft, d.Yaw.Facing, landingAlt); Fly.FlyToward(self, aircraft, d.Yaw.Facing, landingAlt);
return this; return this;
} }
void PlayLandingSound(Actor self)
{
Game.Sound.Play(SoundType.World, aircraft.Info.LandingSounds, self.World, aircraft.CenterPosition);
soundPlayed = true;
}
} }
} }

View File

@@ -192,7 +192,7 @@ namespace OpenRA.Mods.Common.Activities
if (aircraft.Info.TurnToLand) if (aircraft.Info.TurnToLand)
Queue(self, new Turn(self, aircraft.Info.InitialFacing)); Queue(self, new Turn(self, aircraft.Info.InitialFacing));
Queue(self, new Land(self, true)); Queue(self, new Land(self));
return NextActivity; return NextActivity;
} }
else else
@@ -225,16 +225,10 @@ namespace OpenRA.Mods.Common.Activities
{ {
aircraft.MakeReservation(dest); aircraft.MakeReservation(dest);
if (aircraft.Info.VTOL) if (aircraft.Info.VTOL && aircraft.Info.TurnToDock)
{
if (aircraft.Info.TurnToDock)
QueueChild(self, new Turn(self, aircraft.Info.InitialFacing), true); QueueChild(self, new Turn(self, aircraft.Info.InitialFacing), true);
QueueChild(self, new Land(self, true, dest), true); QueueChild(self, new Land(self, Target.FromActor(dest), offset), true);
}
else
QueueChild(self, new Land(self, Target.FromPos(dest.CenterPosition + offset), true, dest), true);
QueueChild(self, new Resupply(self, dest, WDist.Zero), true); QueueChild(self, new Resupply(self, dest, WDist.Zero), true);
resupplied = true; resupplied = true;
} }

View File

@@ -109,7 +109,7 @@ namespace OpenRA.Mods.Common.Activities
// Make sure that the carried actor is on the ground before releasing it // Make sure that the carried actor is on the ground before releasing it
if (self.World.Map.DistanceAboveTerrain(carryablePosition) != WDist.Zero) if (self.World.Map.DistanceAboveTerrain(carryablePosition) != WDist.Zero)
QueueChild(self, new Land(self, true), true); QueueChild(self, new Land(self), true);
// Pause briefly before releasing for visual effect // Pause briefly before releasing for visual effect
if (carryall.Info.UnloadingDelay > 0) if (carryall.Info.UnloadingDelay > 0)

View File

@@ -126,7 +126,7 @@ namespace OpenRA.Mods.Common.Activities
if (targetPosition.Z != self.CenterPosition.Z) if (targetPosition.Z != self.CenterPosition.Z)
{ {
QueueChild(self, new Land(self, false, self.World.Map.DistanceAboveTerrain(targetPosition)), true); QueueChild(self, new Land(self, Target.FromActor(cargo), -carryableBody.LocalToWorld(localOffset)));
return this; return this;
} }

View File

@@ -39,7 +39,7 @@ namespace OpenRA.Mods.Common.Activities
QueueChild(self, new Turn(self, Facing)); QueueChild(self, new Turn(self, Facing));
if (self.Info.HasTraitInfo<AircraftInfo>()) if (self.Info.HasTraitInfo<AircraftInfo>())
QueueChild(self, new Land(self, true)); QueueChild(self, new Land(self));
} }
public override Activity Tick(Actor self) public override Activity Tick(Actor self)

View File

@@ -183,11 +183,11 @@ namespace OpenRA.Mods.Common.Scripting
Move(transport, destination); Move(transport, destination);
transport.QueueActivity(new Turn(transport, aircraft.Info.InitialFacing)); transport.QueueActivity(new Turn(transport, aircraft.Info.InitialFacing));
transport.QueueActivity(new Land(transport, true)); transport.QueueActivity(new Land(transport));
} }
else else
{ {
transport.QueueActivity(new Land(transport, Target.FromCell(transport.World, destination), true)); transport.QueueActivity(new Land(transport, Target.FromCell(transport.World, destination)));
} }
transport.QueueActivity(new Wait(15)); transport.QueueActivity(new Wait(15));

View File

@@ -48,7 +48,7 @@ namespace OpenRA.Mods.Common.Scripting
[Desc("Queues a landing activity on the specififed actor.")] [Desc("Queues a landing activity on the specififed actor.")]
public void Land(Actor landOn) public void Land(Actor landOn)
{ {
Self.QueueActivity(new Land(Self, Target.FromActor(landOn), true, landOn)); Self.QueueActivity(new Land(Self, Target.FromActor(landOn)));
} }
[ScriptActorPropertyActivity] [ScriptActorPropertyActivity]

View File

@@ -195,6 +195,11 @@ namespace OpenRA.Mods.Common.Traits
public WDist LandAltitude { get; private set; } public WDist LandAltitude { get; private set; }
public static WPos GroundPosition(Actor self)
{
return self.CenterPosition - new WVec(WDist.Zero, WDist.Zero, self.World.Map.DistanceAboveTerrain(self.CenterPosition));
}
bool airborne; bool airborne;
bool cruising; bool cruising;
bool firstTick = true; bool firstTick = true;
@@ -321,7 +326,7 @@ namespace OpenRA.Mods.Common.Traits
if (Info.TurnToLand) if (Info.TurnToLand)
self.QueueActivity(new Turn(self, Info.InitialFacing)); self.QueueActivity(new Turn(self, Info.InitialFacing));
self.QueueActivity(new Land(self, true)); self.QueueActivity(new Land(self));
ForceLanding = true; ForceLanding = true;
} }
@@ -625,7 +630,7 @@ namespace OpenRA.Mods.Common.Traits
if (Info.TurnToLand) if (Info.TurnToLand)
self.QueueActivity(new Turn(self, Info.InitialFacing)); self.QueueActivity(new Turn(self, Info.InitialFacing));
self.QueueActivity(new Land(self, true)); self.QueueActivity(new Land(self));
} }
else if (!Info.CanHover && !atLandAltitude) else if (!Info.CanHover && !atLandAltitude)
self.QueueActivity(new FlyCircle(self, -1, Info.IdleTurnSpeed > -1 ? Info.IdleTurnSpeed : TurnSpeed)); self.QueueActivity(new FlyCircle(self, -1, Info.IdleTurnSpeed > -1 ? Info.IdleTurnSpeed : TurnSpeed));
@@ -812,10 +817,7 @@ namespace OpenRA.Mods.Common.Traits
public Activity MoveIntoTarget(Actor self, Target target) public Activity MoveIntoTarget(Actor self, Target target)
{ {
if (!Info.VTOL) return new Land(self, target);
return new Land(self, target, false);
return new Land(self, false);
} }
public Activity VisualMove(Actor self, WPos fromPos, WPos toPos) public Activity VisualMove(Actor self, WPos fromPos, WPos toPos)

View File

@@ -193,7 +193,8 @@ namespace OpenRA.Mods.Common.Traits
Unloading = true; Unloading = true;
if (aircraft != null) if (aircraft != null)
self.QueueActivity(new Land(self, true)); self.QueueActivity(new Land(self));
self.QueueActivity(new UnloadCargo(self, true)); self.QueueActivity(new UnloadCargo(self, true));
} }
} }