Beispiel #1
0
        static Actor ChooseHelipad(Actor self)
        {
            var rearmBuildings = self.Info.Traits.Get <HelicopterInfo>().RearmBuildings;

            return(self.World.Actors.Where(a => a.Owner == self.Owner).FirstOrDefault(
                       a => rearmBuildings.Contains(a.Info.Name) && !Reservable.IsReserved(a)));
        }
        public Actor ChooseHelipad(Actor self)
        {
            var rearmBuildings = heli.Info.RearmBuildings;

            return(self.World.Actors.Where(a => a.Owner == self.Owner).FirstOrDefault(
                       a => rearmBuildings.Contains(a.Info.Name) && !Reservable.IsReserved(a)));
        }
Beispiel #3
0
        void Calculate(Actor self)
        {
            if (dest == null || dest.IsDead || Reservable.IsReserved(dest))
            {
                dest = ChooseResupplier(self, true);
            }

            if (dest == null)
            {
                return;
            }

            var landPos  = dest.CenterPosition;
            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 = CalculateTurnRadius(speed);

            // 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;
        }
Beispiel #4
0
        public override Activity Tick(Actor self)
        {
            if (IsCanceled)
            {
                return(NextActivity);
            }

            if (dest == null || dest.IsDead || Reservable.IsReserved(dest))
            {
                dest = ChooseHelipad(self, true);
            }

            var initialFacing = heli.Info.InitialFacing;

            if (dest == null || dest.IsDead)
            {
                var nearestHpad = ChooseHelipad(self, false);

                if (nearestHpad == null)
                {
                    return(ActivityUtils.SequenceActivities(new Turn(self, initialFacing), new HeliLand(self, true), NextActivity));
                }
                else
                {
                    var distanceFromHelipad = (nearestHpad.CenterPosition - self.CenterPosition).HorizontalLength;
                    var distanceLength      = heli.Info.WaitDistanceFromResupplyBase.Length;

                    // If no pad is available, move near one and wait
                    if (distanceFromHelipad > distanceLength)
                    {
                        var randomPosition = WVec.FromPDF(self.World.SharedRandom, 2) * distanceLength / 1024;

                        var target = Target.FromPos(nearestHpad.CenterPosition + randomPosition);

                        return(ActivityUtils.SequenceActivities(new HeliFly(self, target, WDist.Zero, heli.Info.WaitDistanceFromResupplyBase), this));
                    }

                    return(this);
                }
            }

            var exit   = dest.Info.TraitInfos <ExitInfo>().FirstOrDefault();
            var offset = (exit != null) ? exit.SpawnOffset : WVec.Zero;

            if (ShouldLandAtBuilding(self, dest))
            {
                heli.MakeReservation(dest);

                return(ActivityUtils.SequenceActivities(
                           new HeliFly(self, Target.FromPos(dest.CenterPosition + offset)),
                           new Turn(self, initialFacing),
                           new HeliLand(self, false),
                           new ResupplyAircraft(self),
                           !abortOnResupply ? NextActivity : null));
            }

            return(ActivityUtils.SequenceActivities(
                       new HeliFly(self, Target.FromPos(dest.CenterPosition + offset)),
                       NextActivity));
        }
Beispiel #5
0
        public void ResolveOrder(Actor self, Order order)
        {
            if (order.OrderString == "Move")
            {
                var cell     = self.World.Map.Clamp(order.TargetLocation);
                var explored = self.Owner.Shroud.IsExplored(cell);

                if (!explored && !Info.MoveIntoShroud)
                {
                    return;
                }

                UnReserve();

                var target = Target.FromCell(self.World, cell);
                self.SetTargetLine(target, Color.Green);
                self.CancelActivity();
                self.QueueActivity(new Fly(self, target));
                self.QueueActivity(new FlyCircle());
            }
            else if (order.OrderString == "Enter")
            {
                if (Reservable.IsReserved(order.TargetActor))
                {
                    return;
                }

                UnReserve();

                self.SetTargetLine(Target.FromOrder(self.World, order), Color.Green);

                self.CancelActivity();
                self.QueueActivity(new ReturnToBase(self, order.TargetActor));
                self.QueueActivity(new ResupplyAircraft());
            }
            else if (order.OrderString == "Stop")
            {
                UnReserve();
                self.CancelActivity();
            }
            else if (order.OrderString == "ReturnToBase")
            {
                var airfield = ReturnToBase.ChooseAirfield(self, true);
                if (airfield == null)
                {
                    return;
                }

                UnReserve();
                self.CancelActivity();
                self.SetTargetLine(Target.FromActor(airfield), Color.Green);
                self.QueueActivity(new ReturnToBase(self, airfield));
                self.QueueActivity(new ResupplyAircraft());
            }
            else
            {
                // Game.Debug("Unreserve due to unhandled order: {0}".F(order.OrderString));
                UnReserve();
            }
        }
Beispiel #6
0
        void Calculate(Actor self)
        {
            if (dest == null || Reservable.IsReserved(dest))
            {
                dest = ChooseAirfield(self);
            }

            if (dest == null)
            {
                return;
            }

            var plane = self.Trait <Plane>();
            var res   = dest.TraitOrDefault <Reservable>();

            if (res != null)
            {
                plane.UnReserve();
                plane.reservation = res.Reserve(dest, self, plane);
            }

            var landPos  = dest.CenterLocation;
            var aircraft = self.Trait <Aircraft>();

            var speed = .2f * aircraft.MovementSpeed;

            var approachStart = landPos - new float2(aircraft.Altitude * speed, 0);
            var turnRadius    = (128f / self.Info.Traits.Get <AircraftInfo>().ROT) * speed / (float)Math.PI;

            /* work out the center points */
            var fwd            = -float2.FromAngle(aircraft.Facing / 128f * (float)Math.PI);
            var side           = new float2(-fwd.Y, fwd.X);             /* rotate */
            var sideTowardBase = new[] { side, -side }
            .OrderBy(a => float2.Dot(a, self.CenterLocation - approachStart))
            .First();

            var c1 = self.CenterLocation + turnRadius * sideTowardBase;
            var c2 = approachStart + new float2(0,
                                                turnRadius * Math.Sign(self.CenterLocation.Y - approachStart.Y)); // above or below start point

            /* work out tangent points */
            var d = c2 - c1;
            var e = (turnRadius / d.Length) * d;
            var f = new float2(-e.Y, e.X);                      /* rotate */

            /* todo: support internal tangents, too! */

            if (f.X > 0)
            {
                f = -f;
            }

            w1 = (c1 + f).ToInt2();
            w2 = (c2 + f).ToInt2();
            w3 = (approachStart).ToInt2();
            plane.RTBPathHash = w1 + w2 + w3;

            isCalculated = true;
        }
Beispiel #7
0
        public Actor ChooseHelipad(Actor self, bool unreservedOnly)
        {
            var rearmBuildings = heli.Info.RearmBuildings;

            return(self.World.Actors.Where(a => a.Owner == self.Owner &&
                                           rearmBuildings.Contains(a.Info.Name) &&
                                           (!unreservedOnly || !Reservable.IsReserved(self))).ClosestTo(self));
        }
Beispiel #8
0
        public static Actor ChooseAirfield(Actor self, bool unreservedOnly)
        {
            var rearmBuildings = self.Info.TraitInfo <AircraftInfo>().RearmBuildings;

            return(self.World.ActorsHavingTrait <Reservable>()
                   .Where(a => a.Owner == self.Owner &&
                          rearmBuildings.Contains(a.Info.Name) &&
                          (!unreservedOnly || !Reservable.IsReserved(a)))
                   .ClosestTo(self));
        }
Beispiel #9
0
        public static Actor ChooseAirfield(Actor self, bool unreservedOnly)
        {
            var rearmBuildings = self.Info.Traits.Get <PlaneInfo>().RearmBuildings;

            return(self.World.ActorsWithTrait <Reservable>()
                   .Where(a => a.Actor.Owner == self.Owner)
                   .Where(a => rearmBuildings.Contains(a.Actor.Info.Name) &&
                          (!unreservedOnly || !Reservable.IsReserved(a.Actor)))
                   .Select(a => a.Actor)
                   .ClosestTo(self));
        }
Beispiel #10
0
        public Actor ChooseResupplier(Actor self, bool unreservedOnly)
        {
            if (rearmable == null)
            {
                return(null);
            }

            return(self.World.Actors.Where(a => a.Owner == self.Owner &&
                                           rearmable.Info.RearmActors.Contains(a.Info.Name) &&
                                           (!unreservedOnly || !Reservable.IsReserved(a)))
                   .ClosestTo(self));
        }
Beispiel #11
0
        public void ResolveOrder(Actor self, Order order)
        {
            if (order.OrderString == "Move")
            {
                UnReserve();

                var target = self.World.ClampToWorld(order.TargetLocation);
                self.SetTargetLine(Target.FromCell(target), Color.Green);
                self.CancelActivity();
                self.QueueActivity(Fly.ToCell(target));
                self.QueueActivity(new FlyCircle());
            }

            else if (order.OrderString == "Enter")
            {
                if (Reservable.IsReserved(order.TargetActor))
                {
                    return;
                }

                UnReserve();

                self.SetTargetLine(Target.FromOrder(order), Color.Green);

                self.CancelActivity();
                self.QueueActivity(new ReturnToBase(self, order.TargetActor));
                self.QueueActivity(new ResupplyAircraft());
            }
            else if (order.OrderString == "Stop")
            {
                UnReserve();
                self.CancelActivity();
            }
            else if (order.OrderString == "ReturnToBase")
            {
                var airfield = ReturnToBase.ChooseAirfield(self);
                if (airfield == null)
                {
                    return;
                }

                UnReserve();
                self.CancelActivity();
                self.SetTargetLine(Target.FromActor(airfield), Color.Green);
                self.QueueActivity(new ReturnToBase(self, airfield));
                self.QueueActivity(new ResupplyAircraft());
            }
            else
            {
                // Game.Debug("Unreserve due to unhandled order: {0}".F(order.OrderString));
                UnReserve();
            }
        }
Beispiel #12
0
        public static Actor ChooseResupplier(Actor self, bool unreservedOnly)
        {
            var rearmInfo = self.Info.TraitInfoOrDefault <RearmableInfo>();

            if (rearmInfo == null)
            {
                return(null);
            }

            return(self.World.ActorsHavingTrait <Reservable>()
                   .Where(a => a.Owner == self.Owner &&
                          rearmInfo.RearmActors.Contains(a.Info.Name) &&
                          (!unreservedOnly || !Reservable.IsReserved(a)))
                   .ClosestTo(self));
        }
Beispiel #13
0
        public override Activity Tick(Actor self)
        {
            var host = aircraft.GetActorBelow();

            if (host == null)
            {
                return(NextActivity);
            }

            if (aircraft.IsPlane)
            {
                return(ActivityUtils.SequenceActivities(
                           aircraft.GetResupplyActivities(host)
                           .Append(new CallFunc(() => aircraft.UnReserve()))
                           .Append(new WaitFor(() => NextActivity != null || Reservable.IsReserved(host)))
                           .Append(new TakeOff(self))
                           .Append(NextActivity).ToArray()));
            }

            // If is helicopter move away as soon as the resupply ends
            return(ActivityUtils.SequenceActivities(
                       aircraft.GetResupplyActivities(host).Append(new TakeOff(self)).Append(NextActivity).ToArray()));
        }
Beispiel #14
0
        void Calculate(Actor self)
        {
            if (dest == null || Reservable.IsReserved(dest))
            {
                dest = ChooseAirfield(self, true);
            }

            if (dest == null)
            {
                return;
            }

            var plane     = self.Trait <Plane>();
            var planeInfo = self.Info.Traits.Get <PlaneInfo>();
            var res       = dest.TraitOrDefault <Reservable>();

            if (res != null)
            {
                plane.UnReserve();
                plane.Reservation = res.Reserve(dest, self, plane);
            }

            var landPos  = dest.CenterPosition;
            var altitude = planeInfo.CruiseAltitude.Range;

            // Distance required for descent.
            var landDistance = altitude * 1024 / plane.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      = plane.MovementSpeed * 32 / 35;
            var turnRadius = (int)(141 * speed / planeInfo.ROT / (float)Math.PI);

            // Find the center of the turning circles for clockwise and counterclockwise turns
            var angle = WAngle.FromFacing(plane.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 tangentOffset    = new WVec(-tangentDirection.Y, tangentDirection.X, 0) * turnRadius / tangentDirection.Length;

            // TODO: correctly handle CCW <-> CW turns
            if (tangentOffset.X > 0)
            {
                tangentOffset = -tangentOffset;
            }

            w1 = posCenter + tangentOffset;
            w2 = approachCenter + tangentOffset;
            w3 = approachStart;
            plane.RTBPathHash = w1 + (WVec)w2 + (WVec)w3;

            isCalculated = true;
        }
Beispiel #15
0
        public override Activity Tick(Actor self)
        {
            // Refuse to take off if it would land immediately again.
            // Special case: Don't kill other deploy hotkey activities.
            if (heli.ForceLanding)
            {
                return(NextActivity);
            }

            if (IsCanceled)
            {
                return(NextActivity);
            }

            if (dest == null || dest.IsDead || Reservable.IsReserved(dest))
            {
                dest = ChooseHelipad(self, true);
            }

            var initialFacing = heli.Info.InitialFacing;

            if (dest == null || dest.IsDead)
            {
                var nearestHpad = ChooseHelipad(self, false);

                // If a heli was told to return and there's no (available) RearmBuilding, going to the probable next queued activity (HeliAttack)
                // would be pointless (due to lack of ammo), and possibly even lead to an infinite loop due to HeliAttack.cs:L79.
                if (nearestHpad == null && heli.Info.LandWhenIdle)
                {
                    if (heli.Info.TurnToLand)
                    {
                        return(ActivityUtils.SequenceActivities(new Turn(self, initialFacing), new HeliLand(self, true)));
                    }

                    return(new HeliLand(self, true));
                }
                else if (nearestHpad == null && !heli.Info.LandWhenIdle)
                {
                    return(null);
                }
                else
                {
                    var distanceFromHelipad = (nearestHpad.CenterPosition - self.CenterPosition).HorizontalLength;
                    var distanceLength      = heli.Info.WaitDistanceFromResupplyBase.Length;

                    // If no pad is available, move near one and wait
                    if (distanceFromHelipad > distanceLength)
                    {
                        var randomPosition = WVec.FromPDF(self.World.SharedRandom, 2) * distanceLength / 1024;

                        var target = Target.FromPos(nearestHpad.CenterPosition + randomPosition);

                        return(ActivityUtils.SequenceActivities(new HeliFly(self, target, WDist.Zero, heli.Info.WaitDistanceFromResupplyBase), this));
                    }

                    return(this);
                }
            }

            var exit   = dest.Info.FirstExitOrDefault(null);
            var offset = (exit != null) ? exit.SpawnOffset : WVec.Zero;

            if (ShouldLandAtBuilding(self, dest))
            {
                heli.MakeReservation(dest);

                return(ActivityUtils.SequenceActivities(
                           new HeliFly(self, Target.FromPos(dest.CenterPosition + offset)),
                           new Turn(self, initialFacing),
                           new HeliLand(self, false),
                           new ResupplyAircraft(self),
                           !abortOnResupply ? NextActivity : null));
            }

            return(ActivityUtils.SequenceActivities(
                       new HeliFly(self, Target.FromPos(dest.CenterPosition + offset)),
                       NextActivity));
        }
Beispiel #16
0
        public void ResolveOrder(Actor self, Order order)
        {
            if (reservation != null)
            {
                reservation.Dispose();
                reservation = null;
            }

            if (order.OrderString == "Move")
            {
                var target = self.World.ClampToWorld(order.TargetLocation);

                self.SetTargetLine(Target.FromCell(target), Color.Green);
                self.CancelActivity();
                self.QueueActivity(new HeliFly(Util.CenterOfCell(target)));

                if (Info.LandWhenIdle)
                {
                    self.QueueActivity(new Turn(Info.InitialFacing));
                    self.QueueActivity(new HeliLand(true));
                }
            }

            if (order.OrderString == "Enter")
            {
                if (Reservable.IsReserved(order.TargetActor))
                {
                    self.CancelActivity();
                    self.QueueActivity(new HeliReturn());
                }
                else
                {
                    var res = order.TargetActor.TraitOrDefault <Reservable>();
                    if (res != null)
                    {
                        reservation = res.Reserve(order.TargetActor, self, this);
                    }

                    var exit   = order.TargetActor.Info.Traits.WithInterface <ExitInfo>().FirstOrDefault();
                    var offset = exit != null ? exit.SpawnOffset : int2.Zero;

                    self.SetTargetLine(Target.FromActor(order.TargetActor), Color.Green);

                    self.CancelActivity();
                    self.QueueActivity(new HeliFly(order.TargetActor.Trait <IHasLocation>().PxPosition + offset));
                    self.QueueActivity(new Turn(Info.InitialFacing));
                    self.QueueActivity(new HeliLand(false));
                    self.QueueActivity(new ResupplyAircraft());
                }
            }

            if (order.OrderString == "ReturnToBase")
            {
                self.CancelActivity();
                self.QueueActivity(new HeliReturn());
            }

            if (order.OrderString == "Stop")
            {
                self.CancelActivity();

                if (Info.LandWhenIdle)
                {
                    self.QueueActivity(new Turn(Info.InitialFacing));
                    self.QueueActivity(new HeliLand(true));
                }
            }
        }
Beispiel #17
0
        public void ResolveOrder(Actor self, Order order)
        {
            if (Reservation != null)
            {
                Reservation.Dispose();
                Reservation = null;
            }

            if (order.OrderString == "Move")
            {
                var cell     = self.World.Map.Clamp(order.TargetLocation);
                var explored = self.Owner.Shroud.IsExplored(cell);

                if (!explored && !Info.MoveIntoShroud)
                {
                    return;
                }

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

                self.SetTargetLine(target, Color.Green);
                self.CancelActivity();
                self.QueueActivity(new HeliFly(self, target));

                if (Info.LandWhenIdle)
                {
                    if (Info.TurnToLand)
                    {
                        self.QueueActivity(new Turn(self, Info.InitialFacing));
                    }

                    self.QueueActivity(new HeliLand(true));
                }
            }

            if (order.OrderString == "Enter")
            {
                if (Reservable.IsReserved(order.TargetActor))
                {
                    self.CancelActivity();
                    self.QueueActivity(new HeliReturn());
                }
                else
                {
                    var res = order.TargetActor.TraitOrDefault <Reservable>();
                    if (res != null)
                    {
                        Reservation = res.Reserve(order.TargetActor, self, this);
                    }

                    var exit   = order.TargetActor.Info.Traits.WithInterface <ExitInfo>().FirstOrDefault();
                    var offset = (exit != null) ? exit.SpawnOffset : WVec.Zero;

                    self.SetTargetLine(Target.FromActor(order.TargetActor), Color.Green);

                    self.CancelActivity();
                    self.QueueActivity(new HeliFly(self, Target.FromPos(order.TargetActor.CenterPosition + offset)));
                    self.QueueActivity(new Turn(self, Info.InitialFacing));
                    self.QueueActivity(new HeliLand(false));
                    self.QueueActivity(new ResupplyAircraft());
                    self.QueueActivity(new TakeOff());
                }
            }

            if (order.OrderString == "ReturnToBase")
            {
                self.CancelActivity();
                self.QueueActivity(new HeliReturn());
            }

            if (order.OrderString == "Stop")
            {
                self.CancelActivity();

                if (Info.LandWhenIdle)
                {
                    if (Info.TurnToLand)
                    {
                        self.QueueActivity(new Turn(self, Info.InitialFacing));
                    }

                    self.QueueActivity(new HeliLand(true));
                }
            }
        }