상속: Activity
예제 #1
0
파일: Land.cs 프로젝트: RAunplugged/OpenRA
		public override Activity Tick(Actor self)
		{
			if (!target.IsValidFor(self))
				Cancel(self);

			if (IsCanceled)
				return NextActivity;

			if (!soundPlayed && aircraft.Info.LandingSounds.Length > 0 && !self.IsAtGroundLevel())
			{
				Game.Sound.Play(SoundType.World, aircraft.Info.LandingSounds.Random(self.World.SharedRandom), aircraft.CenterPosition);
				soundPlayed = true;
			}

			var d = target.CenterPosition - self.CenterPosition;

			// The next move would overshoot, so just set the final position
			var move = aircraft.FlyStep(aircraft.Facing);
			if (d.HorizontalLengthSquared < move.HorizontalLengthSquared)
			{
				aircraft.SetPosition(self, target.CenterPosition);
				return NextActivity;
			}

			var landingAlt = self.World.Map.DistanceAboveTerrain(target.CenterPosition);
			Fly.FlyToward(self, aircraft, d.Yaw.Facing, landingAlt);

			return this;
		}
예제 #2
0
파일: FlyCircle.cs 프로젝트: jrb0001/OpenRA
        public override Activity Tick(Actor self)
        {
            // Refuse to take off if it would land immediately again.
            if (aircraft.ForceLanding)
            {
                Cancel(self);
                return(NextActivity);
            }

            if (IsCanceled)
            {
                return(NextActivity);
            }

            if (remainingTicks > 0)
            {
                remainingTicks--;
            }
            else if (remainingTicks == 0)
            {
                return(NextActivity);
            }

            // We can't possibly turn this fast
            var desiredFacing = aircraft.Facing + 64;

            Fly.FlyToward(self, aircraft, desiredFacing, cruiseAltitude);

            return(this);
        }
예제 #3
0
파일: TakeOff.cs 프로젝트: dnqbob/OpenRA
        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);
        }
예제 #4
0
파일: FlyFollow.cs 프로젝트: DoGyAUT/OpenRA
        public override Activity Tick(Actor self)
        {
            // Refuse to take off if it would land immediately again.
            if (plane.ForceLanding)
            {
                Cancel(self);
                return(NextActivity);
            }

            if (IsCanceled || !target.IsValidFor(self))
            {
                return(NextActivity);
            }

            if (target.IsInRange(self.CenterPosition, minRange))
            {
                var directVector = target.CenterPosition - self.CenterPosition;
                Fly.FlyToward(self, plane, (directVector.Yaw + WAngle.FromDegrees(180)).Facing, plane.Info.CruiseAltitude);
                return(this);
            }

            if (target.IsInRange(self.CenterPosition, maxRange))
            {
                Fly.FlyToward(self, plane, plane.Facing, plane.Info.CruiseAltitude);
                return(this);
            }

            return(ActivityUtils.SequenceActivities(new Fly(self, target, minRange, maxRange), this));
        }
예제 #5
0
파일: Land.cs 프로젝트: wenzeslaus/OpenRA
        public override Activity Tick(Actor self)
        {
            if (!target.IsValidFor(self))
            {
                Cancel(self);
            }

            if (IsCanceled)
            {
                return(NextActivity);
            }

            var d = target.CenterPosition - self.CenterPosition;

            // The next move would overshoot, so just set the final position
            var move = plane.FlyStep(plane.Facing);

            if (d.HorizontalLengthSquared < move.HorizontalLengthSquared)
            {
                plane.SetPosition(self, target.CenterPosition);
                return(NextActivity);
            }

            var desiredFacing = Util.GetFacing(d, plane.Facing);

            Fly.FlyToward(self, plane, desiredFacing, WRange.Zero);

            return(this);
        }
예제 #6
0
        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);
        }
예제 #7
0
        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);
        }
예제 #8
0
        public override Activity Tick(Actor self)
        {
            if (!target.IsValidFor(self))
            {
                Cancel(self);
            }

            if (IsCanceled)
            {
                return(NextActivity);
            }

            var d = target.CenterPosition - self.CenterPosition;

            // The next move would overshoot, so just set the final position
            var move = aircraft.FlyStep(aircraft.Facing);

            if (d.HorizontalLengthSquared < move.HorizontalLengthSquared)
            {
                aircraft.SetPosition(self, target.CenterPosition);
                return(NextActivity);
            }

            var landingAlt = self.World.Map.DistanceAboveTerrain(target.CenterPosition);

            Fly.FlyToward(self, aircraft, d.Yaw.Facing, landingAlt);

            return(this);
        }
예제 #9
0
        // 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;
        }
예제 #10
0
        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);
        }
예제 #11
0
        public override Activity Tick(Actor self)
        {
            if (IsCanceled || !self.World.Map.Contains(self.Location))
            {
                return(NextActivity);
            }

            Fly.FlyToward(self, plane, plane.Facing, plane.Info.CruiseAltitude);
            return(this);
        }
예제 #12
0
        public override Activity Tick(Actor self)
        {
            if (IsCanceled || remainingTicks-- == 0)
            {
                return(NextActivity);
            }

            Fly.FlyToward(self, plane, plane.Facing, cruiseAltitude);

            return(this);
        }
예제 #13
0
        public override Activity Tick(Actor self)
        {
            if (IsCanceled)
            {
                return(NextActivity);
            }

            // We can't possibly turn this fast
            var desiredFacing = plane.Facing + 64;

            Fly.FlyToward(self, plane, desiredFacing, cruiseAltitude);

            return(this);
        }
예제 #14
0
        public override Activity Tick(Actor self)
        {
            if (IsCanceled || !target.IsValidFor(self))
            {
                return(NextActivity);
            }

            if (target.IsInRange(self.CenterPosition, maxRange) && !target.IsInRange(self.CenterPosition, minRange))
            {
                Fly.FlyToward(self, plane, plane.Facing, plane.Info.CruiseAltitude);
                return(this);
            }

            return(ActivityUtils.SequenceActivities(new Fly(self, target, minRange, maxRange), this));
        }
예제 #15
0
        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.FlyToward(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude);
            return(this);
        }
예제 #16
0
        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);
        }
예제 #17
0
        public override Activity Tick(Actor self)
        {
            // Refuse to take off if it would land immediately again.
            if (plane.ForceLanding)
            {
                Cancel(self);
                return(NextActivity);
            }

            if (IsCanceled || remainingTicks-- == 0)
            {
                return(NextActivity);
            }

            Fly.FlyToward(self, plane, plane.Facing, cruiseAltitude);

            return(this);
        }
예제 #18
0
        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);
        }
예제 #19
0
        public override Activity Tick(Actor self)
        {
            // Refuse to take off if it would land immediately again.
            if (aircraft.ForceLanding)
            {
                Cancel(self);
                return(NextActivity);
            }

            if (IsCanceled || !target.IsValidFor(self))
            {
                return(NextActivity);
            }

            if (target.IsInRange(self.CenterPosition, maxRange) && !target.IsInRange(self.CenterPosition, minRange))
            {
                Fly.FlyToward(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude);
                return(this);
            }

            return(ActivityUtils.SequenceActivities(new Fly(self, target, minRange, maxRange), this));
        }
예제 #20
0
파일: FlyIdle.cs 프로젝트: dnqbob/OpenRA
        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);
        }
예제 #21
0
		public override Activity Tick(Actor self)
		{
			if (!target.IsValidFor(self))
				Cancel(self);

			if (IsCanceled)
				return NextActivity;

			var d = target.CenterPosition - self.CenterPosition;

			// The next move would overshoot, so just set the final position
			var move = plane.FlyStep(plane.Facing);
			if (d.HorizontalLengthSquared < move.HorizontalLengthSquared)
			{
				plane.SetPosition(self, target.CenterPosition);
				return NextActivity;
			}

			Fly.FlyToward(self, plane, d.Yaw.Facing, new WDist(target.CenterPosition.Z));

			return this;
		}
예제 #22
0
        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);
        }
예제 #23
0
        public override Activity Tick(Actor self)
        {
            if (ChildActivity != null)
            {
                ChildActivity = ActivityUtils.RunActivityTick(self, ChildActivity);
                if (ChildActivity != null)
                {
                    return(this);
                }
            }

            if (IsCanceling || target.Type == TargetType.Invalid)
            {
                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 (HeliFly.AdjustAltitude(self, aircraft, 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.FlyToward(self, aircraft, d.Yaw.Facing, landingAlt);

            return(this);
        }
예제 #24
0
        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);
        }
예제 #25
0
        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);
            }

            var dat = self.World.Map.DistanceAboveTerrain(aircraft.CenterPosition);

            if (IsCanceling)
            {
                // We must return the actor to a sensible height before continuing.
                // If the aircraft is on the ground we queue TakeOff to manage the influence reservation and takeoff sounds etc.
                // TODO: It would be better to not take off at all, but we lack the plumbing to detect current airborne/landed state.
                // If the aircraft lands when idle and is idle, we let the default idle handler manage this.
                // TODO: Remove this after fixing all activities to work properly with arbitrary starting altitudes.
                var skipHeightAdjustment = aircraft.Info.LandWhenIdle && self.CurrentActivity.IsCanceling && self.CurrentActivity.NextActivity == null;
                if (aircraft.Info.CanHover && !skipHeightAdjustment && dat != aircraft.Info.CruiseAltitude)
                {
                    if (dat <= aircraft.LandAltitude)
                    {
                        QueueChild(self, new TakeOff(self, target), true);
                    }
                    else
                    {
                        VerticalTakeOffOrLandTick(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude);
                    }

                    return(this);
                }

                return(NextActivity);
            }
            else if (dat <= aircraft.LandAltitude)
            {
                QueueChild(self, new TakeOff(self, target), true);
                return(this);
            }

            bool targetIsHiddenActor;

            target = target.Recalculate(self.Owner, out targetIsHiddenActor);
            if (!targetIsHiddenActor && target.Type == TargetType.Actor)
            {
                lastVisibleTarget = Target.FromTargetPositions(target);
            }

            var oldUseLastVisibleTarget = useLastVisibleTarget;

            useLastVisibleTarget = targetIsHiddenActor || !target.IsValidFor(self);

            // Update target lines if required
            if (useLastVisibleTarget != oldUseLastVisibleTarget && targetLineColor.HasValue)
            {
                self.SetTargetLine(useLastVisibleTarget ? lastVisibleTarget : target, targetLineColor.Value, false);
            }

            // Target is hidden or dead, and we don't have a fallback position to move towards
            if (useLastVisibleTarget && !lastVisibleTarget.IsValidFor(self))
            {
                return(NextActivity);
            }

            var checkTarget   = useLastVisibleTarget ? lastVisibleTarget : target;
            var delta         = checkTarget.CenterPosition - self.CenterPosition;
            var desiredFacing = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : aircraft.Facing;

            // Inside the target annulus, so we're done
            var insideMaxRange = maxRange.Length > 0 && checkTarget.IsInRange(aircraft.CenterPosition, maxRange);
            var insideMinRange = minRange.Length > 0 && checkTarget.IsInRange(aircraft.CenterPosition, minRange);

            if (insideMaxRange && !insideMinRange)
            {
                return(NextActivity);
            }

            var move = aircraft.Info.CanHover ? aircraft.FlyStep(desiredFacing) : aircraft.FlyStep(aircraft.Facing);

            // Inside the minimum range, so reverse if CanHover
            if (aircraft.Info.CanHover && insideMinRange)
            {
                FlyTick(self, aircraft, desiredFacing, aircraft.Info.CruiseAltitude, -move);
                return(this);
            }

            // The next move would overshoot, so consider it close enough or set final position if CanHover
            if (delta.HorizontalLengthSquared < move.HorizontalLengthSquared)
            {
                if (aircraft.Info.CanHover)
                {
                    // Set final (horizontal) position
                    if (delta.HorizontalLengthSquared != 0)
                    {
                        // Ensure we don't include a non-zero vertical component here that would move us away from CruiseAltitude
                        var deltaMove = new WVec(delta.X, delta.Y, 0);
                        FlyTick(self, aircraft, desiredFacing, dat, deltaMove);
                    }

                    // Move to CruiseAltitude, if not already there
                    if (dat != aircraft.Info.CruiseAltitude)
                    {
                        Fly.VerticalTakeOffOrLandTick(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude);
                        return(this);
                    }
                }

                return(NextActivity);
            }

            if (!aircraft.Info.CanHover)
            {
                // Using the turn rate, compute a hypothetical circle traced by a continuous turn.
                // If it contains the destination point, it's unreachable without more complex manuvering.
                var turnRadius = CalculateTurnRadius(aircraft.MovementSpeed, aircraft.TurnSpeed);

                // The current facing is a tangent of the minimal turn circle.
                // Make a perpendicular vector, and use it to locate the turn's center.
                var turnCenterFacing = aircraft.Facing;
                turnCenterFacing += Util.GetNearestFacing(aircraft.Facing, desiredFacing) > 0 ? 64 : -64;

                var turnCenterDir = new WVec(0, -1024, 0).Rotate(WRot.FromFacing(turnCenterFacing));
                turnCenterDir *= turnRadius;
                turnCenterDir /= 1024;

                // Compare with the target point, and keep flying away if it's inside the circle.
                var turnCenter = aircraft.CenterPosition + turnCenterDir;
                if ((checkTarget.CenterPosition - turnCenter).HorizontalLengthSquared < turnRadius * turnRadius)
                {
                    desiredFacing = aircraft.Facing;
                }
            }

            FlyTick(self, aircraft, desiredFacing, aircraft.Info.CruiseAltitude);

            return(this);
        }
예제 #26
0
파일: Fly.cs 프로젝트: Mete0/anki-OpenRA
        public override bool Tick(Actor self)
        {
            // Refuse to take off if it would land immediately again.
            if (aircraft.ForceLanding)
            {
                Cancel(self);
            }

            var dat      = self.World.Map.DistanceAboveTerrain(aircraft.CenterPosition);
            var isLanded = dat <= aircraft.LandAltitude;

            // HACK: Prevent paused (for example, EMP'd) aircraft from taking off.
            // This is necessary until the TODOs in the IsCanceling block below are adressed.
            if (isLanded && aircraft.IsTraitPaused)
            {
                return(false);
            }

            if (IsCanceling)
            {
                // We must return the actor to a sensible height before continuing.
                // If the aircraft is on the ground we queue TakeOff to manage the influence reservation and takeoff sounds etc.
                // TODO: It would be better to not take off at all, but we lack the plumbing to detect current airborne/landed state.
                // If the aircraft lands when idle and is idle, we let the default idle handler manage this.
                // TODO: Remove this after fixing all activities to work properly with arbitrary starting altitudes.
                var landWhenIdle         = aircraft.Info.IdleBehavior == IdleBehaviorType.Land;
                var skipHeightAdjustment = landWhenIdle && self.CurrentActivity.IsCanceling && self.CurrentActivity.NextActivity == null;
                if (aircraft.Info.CanHover && !skipHeightAdjustment && dat != aircraft.Info.CruiseAltitude)
                {
                    if (isLanded)
                    {
                        QueueChild(new TakeOff(self));
                    }
                    else
                    {
                        VerticalTakeOffOrLandTick(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude);
                    }

                    return(false);
                }

                return(true);
            }
            else if (isLanded)
            {
                QueueChild(new TakeOff(self));
                return(false);
            }

            bool targetIsHiddenActor;

            target = target.Recalculate(self.Owner, out targetIsHiddenActor);
            if (!targetIsHiddenActor && target.Type == TargetType.Actor)
            {
                lastVisibleTarget = Target.FromTargetPositions(target);
            }

            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);
            }

            var checkTarget = useLastVisibleTarget ? lastVisibleTarget : target;
            var pos         = aircraft.GetPosition();
            var delta       = checkTarget.CenterPosition - pos;

            // Inside the target annulus, so we're done
            var insideMaxRange = maxRange.Length > 0 && checkTarget.IsInRange(pos, maxRange);
            var insideMinRange = minRange.Length > 0 && checkTarget.IsInRange(pos, minRange);

            if (insideMaxRange && !insideMinRange)
            {
                return(true);
            }

            var isSlider      = aircraft.Info.CanSlide;
            var desiredFacing = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : aircraft.Facing;
            var move          = isSlider ? aircraft.FlyStep(desiredFacing) : aircraft.FlyStep(aircraft.Facing);

            // Inside the minimum range, so reverse if we CanSlide, otherwise face away from the target.
            if (insideMinRange)
            {
                if (isSlider)
                {
                    FlyTick(self, aircraft, desiredFacing, aircraft.Info.CruiseAltitude, -move);
                }
                else
                {
                    desiredFacing = Util.NormalizeFacing(desiredFacing + 128);
                    FlyTick(self, aircraft, desiredFacing, aircraft.Info.CruiseAltitude, move);
                }

                return(false);
            }

            // HACK: Consider ourselves blocked if we have moved by less than 64 WDist in the last five ticks
            // Stop if we are blocked and close enough
            if (positionBuffer.Count >= 5 && (positionBuffer.Last() - positionBuffer[0]).LengthSquared < 4096 &&
                delta.HorizontalLengthSquared <= nearEnough.LengthSquared)
            {
                return(true);
            }

            // The next move would overshoot, so consider it close enough or set final position if we CanSlide
            if (delta.HorizontalLengthSquared < move.HorizontalLengthSquared)
            {
                // For VTOL landing to succeed, it must reach the exact target position,
                // so for the final move it needs to behave as if it had CanSlide.
                if (isSlider || aircraft.Info.VTOL)
                {
                    // Set final (horizontal) position
                    if (delta.HorizontalLengthSquared != 0)
                    {
                        // Ensure we don't include a non-zero vertical component here that would move us away from CruiseAltitude
                        var deltaMove = new WVec(delta.X, delta.Y, 0);
                        FlyTick(self, aircraft, desiredFacing, dat, deltaMove);
                    }

                    // Move to CruiseAltitude, if not already there
                    if (dat != aircraft.Info.CruiseAltitude)
                    {
                        Fly.VerticalTakeOffOrLandTick(self, aircraft, aircraft.Facing, aircraft.Info.CruiseAltitude);
                        return(false);
                    }
                }

                return(true);
            }

            if (!isSlider)
            {
                // Using the turn rate, compute a hypothetical circle traced by a continuous turn.
                // If it contains the destination point, it's unreachable without more complex manuvering.
                var turnRadius = CalculateTurnRadius(aircraft.MovementSpeed, aircraft.TurnSpeed);

                // The current facing is a tangent of the minimal turn circle.
                // Make a perpendicular vector, and use it to locate the turn's center.
                var turnCenterFacing = aircraft.Facing;
                turnCenterFacing += Util.GetNearestFacing(aircraft.Facing, desiredFacing) > 0 ? 64 : -64;

                var turnCenterDir = new WVec(0, -1024, 0).Rotate(WRot.FromFacing(turnCenterFacing));
                turnCenterDir *= turnRadius;
                turnCenterDir /= 1024;

                // Compare with the target point, and keep flying away if it's inside the circle.
                var turnCenter = aircraft.CenterPosition + turnCenterDir;
                if ((checkTarget.CenterPosition - turnCenter).HorizontalLengthSquared < turnRadius * turnRadius)
                {
                    desiredFacing = aircraft.Facing;
                }
            }

            positionBuffer.Add(self.CenterPosition);
            if (positionBuffer.Count > 5)
            {
                positionBuffer.RemoveAt(0);
            }

            FlyTick(self, aircraft, desiredFacing, aircraft.Info.CruiseAltitude);

            return(false);
        }
예제 #27
0
        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);
        }
예제 #28
0
        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);
            }

            if (IsCanceling)
            {
                // Cancel the requested target, but keep firing on it while in range
                if (attackAircraft.Info.PersistentTargeting)
                {
                    attackAircraft.OpportunityTarget      = attackAircraft.RequestedTarget;
                    attackAircraft.OpportunityForceAttack = attackAircraft.RequestedForceAttack;
                    attackAircraft.OpportunityTargetIsPersistentTarget = true;
                }

                attackAircraft.RequestedTarget = Target.Invalid;
                return(NextActivity);
            }

            // 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(NextActivity);
            }

            if (attackAircraft.IsTraitPaused)
            {
                return(this);
            }

            bool targetIsHiddenActor;

            attackAircraft.RequestedTarget         = target = target.Recalculate(self.Owner, out targetIsHiddenActor);
            attackAircraft.RequestedTargetLastTick = self.World.WorldTick;
            hasTicked = true;

            if (!targetIsHiddenActor && target.Type == TargetType.Actor)
            {
                lastVisibleTarget       = Target.FromTargetPositions(target);
                lastVisibleMaximumRange = attackAircraft.GetMaximumRangeVersusTarget(target);
                lastVisibleOwner        = target.Actor.Owner;
                lastVisibleTargetTypes  = target.Actor.GetEnabledTargetTypes();
            }

            var oldUseLastVisibleTarget = useLastVisibleTarget;

            useLastVisibleTarget = targetIsHiddenActor || !target.IsValidFor(self);

            // Update target lines if required
            if (useLastVisibleTarget != oldUseLastVisibleTarget)
            {
                self.SetTargetLine(useLastVisibleTarget ? lastVisibleTarget : target, Color.Red, false);
            }

            // Target is hidden or dead, and we don't have a fallback position to move towards
            if (useLastVisibleTarget && !lastVisibleTarget.IsValidFor(self))
            {
                attackAircraft.RequestedTarget = Target.Invalid;
                return(NextActivity);
            }

            // If all valid weapons have depleted their ammo and Rearmable trait exists, return to RearmActor to reload and then resume the activity
            if (rearmable != null && !useLastVisibleTarget && attackAircraft.Armaments.All(x => x.IsTraitPaused || !x.Weapon.IsValidAgainst(target, self.World, self)))
            {
                QueueChild(self, new ReturnToBase(self, aircraft.Info.AbortOnResupply), true);
                return(this);
            }

            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))
                {
                    attackAircraft.RequestedTarget = Target.Invalid;
                    return(NextActivity);
                }

                // Fly towards the last known position
                QueueChild(self, new Fly(self, target, WDist.Zero, lastVisibleMaximumRange, checkTarget.CenterPosition, Color.Red), true);
                return(this);
            }

            var delta         = attackAircraft.GetTargetPosition(pos, target) - pos;
            var desiredFacing = delta.HorizontalLengthSquared != 0 ? delta.Yaw.Facing : aircraft.Facing;
            var isAirborne    = self.World.Map.DistanceAboveTerrain(pos).Length >= aircraft.Info.MinAirborneAltitude;

            if (!isAirborne)
            {
                QueueChild(self, new TakeOff(self), true);
            }

            if (attackAircraft.Info.AttackType == AirAttackType.Strafe)
            {
                if (target.IsInRange(pos, attackAircraft.GetMinimumRange()))
                {
                    QueueChild(self, new FlyTimed(ticksUntilTurn, self), true);
                }

                QueueChild(self, new Fly(self, target, target.CenterPosition, Color.Red), true);
                QueueChild(self, new FlyTimed(ticksUntilTurn, self));
            }
            else
            {
                var minimumRange = attackAircraft.GetMinimumRangeVersusTarget(target);
                if (!target.IsInRange(pos, lastVisibleMaximumRange) || target.IsInRange(pos, minimumRange))
                {
                    QueueChild(self, new Fly(self, target, minimumRange, lastVisibleMaximumRange, target.CenterPosition, Color.Red), true);
                }
                else if (isAirborne)                 // Don't use 'else' to avoid conflict with TakeOff
                {
                    Fly.VerticalTakeOffOrLandTick(self, aircraft, desiredFacing, aircraft.Info.CruiseAltitude);
                }
            }

            return(this);
        }
예제 #29
0
        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);
        }
예제 #30
0
        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);
        }