示例#1
0
        public override bool Tick(Actor self)
        {
            // Refuse to take off if it would land immediately again.
            // Special case: Don't kill other deploy hotkey activities.
            if (aircraft.ForceLanding)
            {
                return(true);
            }

            if (IsCanceling || self.IsDead)
            {
                return(true);
            }

            if (dest == null || dest.IsDead || !Reservable.IsAvailableFor(dest, self))
            {
                dest = ChooseResupplier(self, true);
            }

            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(new Fly(self, target, WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green));
                        }

                        return(false);
                    }

                    QueueChild(new Fly(self, Target.FromActor(nearestResupplier), WDist.Zero, aircraft.Info.WaitDistanceFromResupplyBase, targetLineColor: Color.Green));
                    QueueChild(new FlyIdle(self, aircraft.Info.NumberOfTicksToVerifyAvailableAirport));
                    return(false);
                }

                // Prevent an infinite loop in case we'd return to the activity that called ReturnToBase in the first place. Go idle instead.
                self.CancelActivity();
                return(true);
            }

            if (ShouldLandAtBuilding(self, dest))
            {
                var exit   = dest.FirstExitOrDefault(null);
                var offset = exit != null ? exit.Info.SpawnOffset : WVec.Zero;
                if (aircraft.Info.TurnToDock || !aircraft.Info.VTOL)
                {
                    facing = aircraft.Info.InitialFacing;
                }

                aircraft.MakeReservation(dest);
                QueueChild(new Land(self, Target.FromActor(dest), offset, facing, Color.Green));
                QueueChild(new Resupply(self, dest, WDist.Zero, alwaysLand));
                return(true);
            }

            QueueChild(new Fly(self, Target.FromActor(dest), targetLineColor: Color.Green));
            return(true);
        }
示例#2
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.
            // 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)
            {
                self.CancelActivity();
            }

            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 Fly(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.VTOL || aircraft.Info.CanHover)
            {
                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);
        }
示例#3
0
        public void ResolveOrder(Actor self, Order order)
        {
            if (order.OrderString == "Move")
            {
                var cell = self.World.Map.Clamp(self.World.Map.CellContaining(order.Target.CenterPosition));
                if (!Info.MoveIntoShroud && !self.Owner.Shroud.IsExplored(cell))
                {
                    return;
                }

                if (!order.Queued)
                {
                    UnReserve();
                }

                var target = Target.FromCell(self.World, cell);

                self.SetTargetLine(target, Color.Green);

                if (!Info.CanHover)
                {
                    self.QueueActivity(order.Queued, new Fly(self, target));
                }
                //else
                //	self.QueueActivity(order.Queued, new HeliFlyAndLandWhenIdle(self, target, Info));
            }
            else if (order.OrderString == "Enter" || order.OrderString == "Repair")
            {
                // Enter and Repair orders are only valid for own/allied actors,
                // which are guaranteed to never be frozen.
                if (order.Target.Type != TargetType.Actor)
                {
                    return;
                }

                if (!order.Queued)
                {
                    UnReserve();
                }

                var targetActor = order.Target.Actor;

                // We only want to set a target line if the order will (most likely) succeed
                if (Reservable.IsAvailableFor(targetActor, self))
                {
                    self.SetTargetLine(Target.FromActor(targetActor), Color.Green);
                }

                self.QueueActivity(order.Queued, new ReturnToBase(self, Info.AbortOnResupply, targetActor));
            }
            else if (order.OrderString == "Stop")
            {
                self.CancelActivity();

                // HACK: If the player accidentally pressed 'Stop', we don't want this to cancel reservation.
                // If unreserving is actually desired despite an actor below, it should be triggered from OnBecomingIdle.

                UnReserve();
            }
            else if (order.OrderString == "ReturnToBase" && rearmable != null && rearmable.Info.RearmActors.Any())
            {
                // Don't restart activity every time deploy hotkey is triggered
                if (self.CurrentActivity is ReturnToBase)
                {
                    return;
                }

                if (!order.Queued)
                {
                    UnReserve();
                }

                // Aircraft with TakeOffOnResupply would immediately take off again, so there's no point in forcing them to land
                // on a resupplier. For aircraft without it, it makes more sense to land than to idle above a free resupplier, though.
                self.QueueActivity(order.Queued, new ReturnToBase(self, Info.AbortOnResupply, null, !Info.TakeOffOnResupply));
            }
            else if (order.OrderString == "Scatter")
            {
                Nudge(self);
            }
        }