public override Activity Tick(Actor self) { if (remainingTicks == 0 || (NextActivity != null && remainingTicks < 0)) { return(NextActivity); } // Refuse to take off if it would land immediately again. if (aircraft.ForceLanding) { Cancel(self); return(NextActivity); } if (IsCanceling) { return(NextActivity); } if (remainingTicks > 0) { remainingTicks--; } // We can't possibly turn this fast var desiredFacing = aircraft.Facing + 64; // This override is necessary, otherwise CanHover aircraft would circle sideways var move = aircraft.FlyStep(aircraft.Facing); Fly.FlyTick(self, aircraft, desiredFacing, aircraft.Info.CruiseAltitude, move, turnSpeedOverride); return(this); }
public override bool Tick(Actor self) { // Refuse to take off if it would land immediately again. if (aircraft.ForceLanding) { Cancel(self); return(true); } var dat = self.World.Map.DistanceAboveTerrain(aircraft.CenterPosition); if (dat < aircraft.Info.CruiseAltitude) { // If we're a VTOL, rise before flying forward if (aircraft.Info.VTOL) { Fly.VerticalTakeOffOrLandTick(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude); return(false); } Fly.FlyTick(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude); return(false); } return(true); }
public override bool Tick(Actor self) { // Refuse to take off if it would land immediately again. if (aircraft.ForceLanding) { Cancel(self); return(true); } var dat = self.World.Map.DistanceAboveTerrain(aircraft.CenterPosition); if (dat < aircraft.Info.CruiseAltitude) { // If we're a VTOL, rise before flying forward if (aircraft.Info.VTOL) { Fly.VerticalTakeOffOrLandTick(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude); return(false); } Fly.FlyTick(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude); return(false); } // Only move to the fallback target if we don't have anything better to do if (NextActivity == null && fallbackTarget.IsValidFor(self) && !movedToTarget) { QueueChild(new AttackMoveActivity(self, () => move.MoveToTarget(self, fallbackTarget, targetLineColor: Color.OrangeRed))); movedToTarget = true; return(false); } return(true); }
public override bool Tick(Actor self) { // Refuse to take off if it would land immediately again. if (aircraft.ForceLanding) { Cancel(self); } if (IsCanceling) { return(true); } bool targetIsHiddenActor; target = target.Recalculate(self.Owner, out targetIsHiddenActor); if (!targetIsHiddenActor && target.Type == TargetType.Actor) { lastVisibleTarget = Target.FromTargetPositions(target); } useLastVisibleTarget = targetIsHiddenActor || !target.IsValidFor(self); // If we are ticking again after previously sequencing a MoveWithRange then that move must have completed // Either we are in range and can see the target, or we've lost track of it and should give up if (wasMovingWithinRange && targetIsHiddenActor) { return(true); } wasMovingWithinRange = false; // Target is hidden or dead, and we don't have a fallback position to move towards if (useLastVisibleTarget && !lastVisibleTarget.IsValidFor(self)) { return(true); } var pos = self.CenterPosition; var checkTarget = useLastVisibleTarget ? lastVisibleTarget : target; // We've reached the required range - if the target is visible and valid then we wait // otherwise if it is hidden or dead we give up if (checkTarget.IsInRange(pos, maxRange) && !checkTarget.IsInRange(pos, minRange)) { if (!aircraft.Info.CanHover) { Fly.FlyTick(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude); } return(useLastVisibleTarget); } wasMovingWithinRange = true; QueueChild(aircraft.MoveWithinRange(target, minRange, maxRange, checkTarget.CenterPosition, targetLineColor)); return(false); }
public override Activity Tick(Actor self) { // Refuse to take off if it would land immediately again. if (aircraft.ForceLanding) { Cancel(self); return(NextActivity); } if (IsCanceling || !self.World.Map.Contains(self.Location)) { return(NextActivity); } Fly.FlyTick(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude); return(this); }
public override Activity Tick(Actor self) { if (ChildActivity != null) { ChildActivity = ActivityUtils.RunActivity(self, ChildActivity); if (ChildActivity != null) { return(this); } } // Refuse to take off if it would land immediately again. if (aircraft.ForceLanding) { Cancel(self); return(NextActivity); } var dat = self.World.Map.DistanceAboveTerrain(aircraft.CenterPosition); if (dat < aircraft.Info.CruiseAltitude) { // If we're a VTOL, rise before flying forward if (aircraft.Info.VTOL) { Fly.VerticalTakeOffOrLandTick(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude); return(this); } else { Fly.FlyTick(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude); return(this); } } // Checking for NextActivity == null again in case another activity was queued while taking off if (moveToRallyPoint && NextActivity == null) { QueueChild(self, new AttackMoveActivity(self, () => move.MoveToTarget(self, target)), true); moveToRallyPoint = false; return(this); } return(NextActivity); }
public override bool Tick(Actor self) { // Refuse to take off if it would land immediately again. if (aircraft.ForceLanding) { Cancel(self); return(true); } if (IsCanceling || remainingTicks-- == 0) { return(true); } Fly.FlyTick(self, aircraft, aircraft.Facing, cruiseAltitude); return(false); }
public override bool Tick(Actor self) { if (remainingTicks == 0 || (NextActivity != null && remainingTicks < 0)) { return(true); } if (aircraft.ForceLanding || IsCanceling) { return(true); } if (remainingTicks > 0) { remainingTicks--; } if (tickIdles != null) { foreach (var tickIdle in tickIdles) { tickIdle.TickIdle(self); } } if (aircraft.Info.IdleSpeed > 0 || (!aircraft.Info.CanHover && aircraft.Info.IdleSpeed < 0)) { var speed = aircraft.Info.IdleSpeed < 0 ? aircraft.Info.Speed : aircraft.Info.IdleSpeed; // This override is necessary, otherwise aircraft with CanSlide would circle sideways var move = aircraft.FlyStep(speed, aircraft.Facing); // We can't possibly turn this fast var desiredFacing = aircraft.Facing + new WAngle(256); Fly.FlyTick(self, aircraft, desiredFacing, aircraft.Info.CruiseAltitude, move, idleTurn); } return(false); }
public override bool Tick(Actor self) { // Refuse to take off if it would land immediately again. if (aircraft.ForceLanding) { Cancel(self); return(true); } // Having flyTicks < 0 is valid and means the actor flies until this activity is canceled if (IsCanceling || (flyTicks > 0 && ticks++ >= flyTicks) || (flyTicks == 0 && remainingDistance <= 0)) { return(true); } // FlyTick moves the aircraft while FlyStep calculates how far we are moving if (remainingDistance != 0) { remainingDistance -= aircraft.FlyStep(aircraft.Facing).HorizontalLength; } Fly.FlyTick(self, aircraft, aircraft.Facing, cruiseAltitude); return(false); }
public override bool Tick(Actor self) { returnToBase = false; // Refuse to take off if it would land immediately again. if (aircraft.ForceLanding) { Cancel(self); } if (IsCanceling) { return(true); } // Check that AttackFollow hasn't cancelled the target by modifying attack.Target // Having both this and AttackFollow modify that field is a horrible hack. if (hasTicked && attackAircraft.RequestedTarget.Type == TargetType.Invalid) { return(true); } if (attackAircraft.IsTraitPaused) { return(false); } bool targetIsHiddenActor; target = target.Recalculate(self.Owner, out targetIsHiddenActor); attackAircraft.SetRequestedTarget(self, target, forceAttack); hasTicked = true; if (!targetIsHiddenActor && target.Type == TargetType.Actor) { lastVisibleTarget = Target.FromTargetPositions(target); lastVisibleMaximumRange = attackAircraft.GetMaximumRangeVersusTarget(target); lastVisibleOwner = target.Actor.Owner; lastVisibleTargetTypes = target.Actor.GetEnabledTargetTypes(); } // The target may become hidden in the same tick the FlyAttack constructor is called, // causing lastVisible* to remain uninitialized. // Fix the fallback values based on the frozen actor properties else if (target.Type == TargetType.FrozenActor && !lastVisibleTarget.IsValidFor(self)) { lastVisibleTarget = Target.FromTargetPositions(target); lastVisibleMaximumRange = attackAircraft.GetMaximumRangeVersusTarget(target); lastVisibleOwner = target.FrozenActor.Owner; lastVisibleTargetTypes = target.FrozenActor.TargetTypes; } useLastVisibleTarget = targetIsHiddenActor || !target.IsValidFor(self); // Target is hidden or dead, and we don't have a fallback position to move towards if (useLastVisibleTarget && !lastVisibleTarget.IsValidFor(self)) { return(true); } // If all valid weapons have depleted their ammo and Rearmable trait exists, return to RearmActor to reload // and resume the activity after reloading if AbortOnResupply is set to 'false' if (rearmable != null && !useLastVisibleTarget && attackAircraft.Armaments.All(x => x.IsTraitPaused || !x.Weapon.IsValidAgainst(target, self.World, self))) { QueueChild(new ReturnToBase(self)); returnToBase = true; return(attackAircraft.Info.AbortOnResupply); } var pos = self.CenterPosition; var checkTarget = useLastVisibleTarget ? lastVisibleTarget : target; // We don't know where the target actually is, so move to where we last saw it if (useLastVisibleTarget) { // We've reached the assumed position but it is not there - give up if (checkTarget.IsInRange(pos, lastVisibleMaximumRange)) { return(true); } // Fly towards the last known position QueueChild(new Fly(self, target, WDist.Zero, lastVisibleMaximumRange, checkTarget.CenterPosition, Color.Red)); return(false); } var delta = attackAircraft.GetTargetPosition(pos, target) - pos; var desiredFacing = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : aircraft.Facing; QueueChild(new TakeOff(self)); var minimumRange = attackAircraft.Info.AttackType == AirAttackType.Strafe ? WDist.Zero : attackAircraft.GetMinimumRangeVersusTarget(target); // When strafing we must move forward for a minimum number of ticks after passing the target. if (remainingTicksUntilTurn > 0) { Fly.FlyTick(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude); remainingTicksUntilTurn--; } // Move into range of the target. else if (!target.IsInRange(pos, lastVisibleMaximumRange) || target.IsInRange(pos, minimumRange)) { QueueChild(aircraft.MoveWithinRange(target, minimumRange, lastVisibleMaximumRange, target.CenterPosition, Color.Red)); } // The aircraft must keep moving forward even if it is already in an ideal position. else if (!aircraft.Info.CanHover || attackAircraft.Info.AttackType == AirAttackType.Strafe) { Fly.FlyTick(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude); remainingTicksUntilTurn = attackAircraft.Info.AttackTurnDelay; } // Turn to face the target if required. else if (!attackAircraft.TargetInFiringArc(self, target, attackAircraft.Info.FacingTolerance)) { aircraft.Facing = Util.TickFacing(aircraft.Facing, desiredFacing, aircraft.TurnSpeed); } return(false); }
public override bool Tick(Actor self) { if (IsCanceling || target.Type == TargetType.Invalid) { if (landingInitiated) { // We must return the actor to a sensible height before continuing. // If the aircraft lands when idle and is idle, continue landing, // otherwise climb back to CruiseAltitude. // TODO: Remove this after fixing all activities to work properly with arbitrary starting altitudes. var shouldLand = aircraft.Info.IdleBehavior == IdleBehaviorType.Land; var continueLanding = shouldLand && self.CurrentActivity.IsCanceling && self.CurrentActivity.NextActivity == null; if (!continueLanding) { var dat = self.World.Map.DistanceAboveTerrain(aircraft.CenterPosition); if (dat > aircraft.LandAltitude && dat < aircraft.Info.CruiseAltitude) { QueueChild(new TakeOff(self)); return(false); } aircraft.RemoveInfluence(); return(true); } } else { return(true); } } var pos = aircraft.GetPosition(); // Reevaluate target position in case the target has moved. targetPosition = target.CenterPosition + offset; landingCell = self.World.Map.CellContaining(targetPosition); // We are already at the landing location. if ((targetPosition - pos).LengthSquared == 0) { return(true); } // Look for free landing cell if (target.Type == TargetType.Terrain && !landingInitiated) { var newLocation = aircraft.FindLandingLocation(landingCell, landRange); // Cannot land so fly towards the last target location instead. if (!newLocation.HasValue) { QueueChild(aircraft.MoveTo(landingCell, 0)); return(true); } if (newLocation.Value != landingCell) { target = Target.FromCell(self.World, newLocation.Value); targetPosition = target.CenterPosition + offset; landingCell = self.World.Map.CellContaining(targetPosition); } } // Move towards landing location/facing if (aircraft.Info.VTOL) { if ((pos - targetPosition).HorizontalLengthSquared != 0) { QueueChild(new Fly(self, Target.FromPos(targetPosition))); return(false); } else if (desiredFacing != -1 && desiredFacing != aircraft.Facing) { QueueChild(new Turn(self, desiredFacing)); return(false); } } if (!aircraft.Info.VTOL && !finishedApproach) { // Calculate approach trajectory var altitude = aircraft.Info.CruiseAltitude.Length; // Distance required for descent. var landDistance = altitude * 1024 / aircraft.Info.MaximumPitch.Tan(); // Approach landing from the opposite direction of the desired facing // TODO: Calculate sensible trajectory without preferred facing. var rotation = WRot.Zero; if (desiredFacing != -1) { rotation = WRot.FromFacing(desiredFacing); } var approachStart = targetPosition + new WVec(0, landDistance, altitude).Rotate(rotation); // Add 10% to the turning radius to ensure we have enough room var speed = aircraft.MovementSpeed * 32 / 35; var turnRadius = Fly.CalculateTurnRadius(speed, aircraft.Info.TurnSpeed); // Find the center of the turning circles for clockwise and counterclockwise turns var angle = WAngle.FromFacing(aircraft.Facing); var fwd = -new WVec(angle.Sin(), angle.Cos(), 0); // Work out whether we should turn clockwise or counter-clockwise for approach var side = new WVec(-fwd.Y, fwd.X, fwd.Z); var approachDelta = self.CenterPosition - approachStart; var sideTowardBase = new[] { side, -side } .MinBy(a => WVec.Dot(a, approachDelta)); // Calculate the tangent line that joins the turning circles at the current and approach positions var cp = self.CenterPosition + turnRadius * sideTowardBase / 1024; var posCenter = new WPos(cp.X, cp.Y, altitude); var approachCenter = approachStart + new WVec(0, turnRadius * Math.Sign(self.CenterPosition.Y - approachStart.Y), 0); var tangentDirection = approachCenter - posCenter; var tangentLength = tangentDirection.Length; var tangentOffset = WVec.Zero; if (tangentLength != 0) { tangentOffset = new WVec(-tangentDirection.Y, tangentDirection.X, 0) * turnRadius / tangentLength; } // TODO: correctly handle CCW <-> CW turns if (tangentOffset.X > 0) { tangentOffset = -tangentOffset; } var w1 = posCenter + tangentOffset; var w2 = approachCenter + tangentOffset; var w3 = approachStart; turnRadius = Fly.CalculateTurnRadius(aircraft.Info.Speed, aircraft.Info.TurnSpeed); // Move along approach trajectory. QueueChild(new Fly(self, Target.FromPos(w1), WDist.Zero, new WDist(turnRadius * 3))); QueueChild(new Fly(self, Target.FromPos(w2))); // Fix a problem when the airplane is sent to land near the landing cell QueueChild(new Fly(self, Target.FromPos(w3), WDist.Zero, new WDist(turnRadius / 2))); finishedApproach = true; return(false); } if (!landingInitiated) { var blockingCells = clearCells.Append(landingCell); if (!aircraft.CanLand(blockingCells, target.Actor)) { // Maintain holding pattern. QueueChild(new FlyIdle(self, 25)); self.NotifyBlocker(blockingCells); finishedApproach = false; return(false); } if (aircraft.Info.LandingSounds.Length > 0) { Game.Sound.Play(SoundType.World, aircraft.Info.LandingSounds, self.World, aircraft.CenterPosition); } aircraft.AddInfluence(landingCell); aircraft.EnteringCell(self); landingInitiated = true; } // Final descent. if (aircraft.Info.VTOL) { var landAltitude = self.World.Map.DistanceAboveTerrain(targetPosition) + aircraft.LandAltitude; if (Fly.VerticalTakeOffOrLandTick(self, aircraft, aircraft.Facing, landAltitude)) { return(false); } return(true); } var d = targetPosition - pos; // The next move would overshoot, so just set the final position var move = aircraft.FlyStep(aircraft.Facing); if (d.HorizontalLengthSquared < move.HorizontalLengthSquared) { var landingAltVec = new WVec(WDist.Zero, WDist.Zero, aircraft.LandAltitude); aircraft.SetPosition(self, targetPosition + landingAltVec); return(true); } var landingAlt = self.World.Map.DistanceAboveTerrain(targetPosition) + aircraft.LandAltitude; Fly.FlyTick(self, aircraft, d.Yaw.Facing, landingAlt); return(false); }
public override Activity Tick(Actor self) { if (ChildActivity != null) { ChildActivity = ActivityUtils.RunActivity(self, ChildActivity); if (ChildActivity != null) { return(this); } } if (IsCanceling || target.Type == TargetType.Invalid) { // We must return the actor to a sensible height before continuing. // If the aircraft lands when idle and is idle, continue landing, // otherwise climb back to CruiseAltitude. // TODO: Remove this after fixing all activities to work properly with arbitrary starting altitudes. var continueLanding = aircraft.Info.LandWhenIdle && self.CurrentActivity.IsCanceling && self.CurrentActivity.NextActivity == null; if (!continueLanding) { var dat = self.World.Map.DistanceAboveTerrain(aircraft.CenterPosition); if (dat > aircraft.LandAltitude && dat < aircraft.Info.CruiseAltitude) { QueueChild(self, new TakeOff(self), true); return(this); } aircraft.RemoveInfluence(); return(NextActivity); } } if (!landingInitiated) { var landingCell = !aircraft.Info.VTOL ? self.World.Map.CellContaining(target.CenterPosition + offset) : self.Location; if (!aircraft.CanLand(landingCell, target.Actor)) { // Maintain holding pattern. if (!aircraft.Info.CanHover) { QueueChild(self, new FlyCircle(self, 25), true); } self.NotifyBlocker(landingCell); return(this); } aircraft.AddInfluence(landingCell); aircraft.EnteringCell(self); landingInitiated = true; } 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 (Fly.VerticalTakeOffOrLandTick(self, aircraft, aircraft.Facing, landAltitude)) { return(this); } return(NextActivity); } var d = (target.CenterPosition + offset) - self.CenterPosition; // The next move would overshoot, so just set the final position var move = aircraft.FlyStep(aircraft.Facing); if (d.HorizontalLengthSquared < move.HorizontalLengthSquared) { var landingAltVec = new WVec(WDist.Zero, WDist.Zero, aircraft.LandAltitude); aircraft.SetPosition(self, target.CenterPosition + offset + landingAltVec); return(NextActivity); } var landingAlt = self.World.Map.DistanceAboveTerrain(target.CenterPosition + offset) + aircraft.LandAltitude; Fly.FlyTick(self, aircraft, d.Yaw.Facing, landingAlt); return(this); }