// Calculates non-CanHover/non-VTOL approach vector and waypoints void Calculate(Actor self) { if (dest == null) { return; } var exit = dest.FirstExitOrDefault(null); var offset = exit != null ? exit.Info.SpawnOffset : WVec.Zero; var landPos = dest.CenterPosition + offset; var altitude = aircraft.Info.CruiseAltitude.Length; // Distance required for descent. var landDistance = altitude * 1024 / aircraft.Info.MaximumPitch.Tan(); // Land towards the east var approachStart = landPos + new WVec(-landDistance, 0, altitude); // 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; } w1 = posCenter + tangentOffset; w2 = approachCenter + tangentOffset; w3 = approachStart; isCalculated = true; }
public override Activity Tick(Actor self) { if (ChildActivity != null) { ChildActivity = ActivityUtils.RunActivityTick(self, ChildActivity); if (ChildActivity != null) { return(this); } } // Refuse to take off if it would land immediately again. // Special case: Don't kill other deploy hotkey activities. if (aircraft.ForceLanding) { return(NextActivity); } // If a Cancel was triggered at this point, it's unlikely that previously queued child activities finished, // so 'resupplied' needs to be set to false, else it + abortOnResupply might cause another Cancel // that would cancel any other activities that were queued after the first Cancel was triggered. // TODO: This is a mess, we need to somehow make the activity cancelling a bit less tricky. if (resupplied && IsCanceling) { resupplied = false; } if (resupplied && abortOnResupply) { Cancel(self); } if (resupplied || IsCanceling || self.IsDead) { return(NextActivity); } if (dest == null || dest.IsDead || !Reservable.IsAvailableFor(dest, self)) { dest = ReturnToBase.ChooseResupplier(self, true); } if (!isCalculated) { Calculate(self); } if (dest == null) { var nearestResupplier = ChooseResupplier(self, false); if (nearestResupplier != null) { if (aircraft.Info.CanHover) { var distanceFromResupplier = (nearestResupplier.CenterPosition - self.CenterPosition).HorizontalLength; var distanceLength = aircraft.Info.WaitDistanceFromResupplyBase.Length; // If no pad is available, move near one and wait if (distanceFromResupplier > distanceLength) { var randomPosition = WVec.FromPDF(self.World.SharedRandom, 2) * distanceLength / 1024; var target = Target.FromPos(nearestResupplier.CenterPosition + randomPosition); QueueChild(self, new HeliFly(self, target, WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green), true); } return(this); } else { QueueChild(self, new Fly(self, Target.FromActor(nearestResupplier), WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green), true); QueueChild(self, new FlyCircle(self, aircraft.Info.NumberOfTicksToVerifyAvailableAirport), true); return(this); } } else if (nearestResupplier == null && aircraft.Info.VTOL && aircraft.Info.LandWhenIdle) { // Using Queue instead of QueueChild here is intentional, as we want VTOLs with LandWhenIdle to land and stay there in this situation Cancel(self); if (aircraft.Info.TurnToLand) { Queue(self, new Turn(self, aircraft.Info.InitialFacing)); } Queue(self, new Land(self)); return(NextActivity); } else { // Prevent an infinite loop in case we'd return to the activity that called ReturnToBase in the first place. Go idle instead. Cancel(self); return(NextActivity); } } var exit = dest.FirstExitOrDefault(null); var offset = exit != null ? exit.Info.SpawnOffset : WVec.Zero; if (aircraft.Info.CanHover) { QueueChild(self, new HeliFly(self, Target.FromPos(dest.CenterPosition + offset)), true); } else if (aircraft.Info.VTOL) { QueueChild(self, new Fly(self, Target.FromPos(dest.CenterPosition + offset)), true); } else { var turnRadius = Fly.CalculateTurnRadius(aircraft.Info.Speed, aircraft.Info.TurnSpeed); QueueChild(self, new Fly(self, Target.FromPos(w1), WDist.Zero, new WDist(turnRadius * 3)), true); QueueChild(self, new Fly(self, Target.FromPos(w2)), true); // Fix a problem when the airplane is sent to resupply near the airport QueueChild(self, new Fly(self, Target.FromPos(w3), WDist.Zero, new WDist(turnRadius / 2)), true); } if (ShouldLandAtBuilding(self, dest)) { aircraft.MakeReservation(dest); if (aircraft.Info.VTOL && aircraft.Info.TurnToDock) { QueueChild(self, new Turn(self, aircraft.Info.InitialFacing), true); } QueueChild(self, new Land(self, Target.FromActor(dest), offset), true); QueueChild(self, new Resupply(self, dest, WDist.Zero), true); resupplied = true; } return(this); }
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); }