internal override void ActInPlanningPhase()
        {
            // first check whether you're hurt and go scared if you are
            if (!Scared && Aircraft.Health < Aircraft.MaxHealth)
            {
                Scared = true;
            }
            // go through all aircrafts and find the closest enemy
            // (for now an enemy is everyone who isn't on your team)
            var myTeam = Aircraft.Team;
            IEnumerable <Aircraft> aircrafts;

            if (myTeam == Team.PlayerTeam)
            {
                aircrafts = Aircraft.ActiveAircraftsInLevel();
            }
            else
            {
                aircrafts = Aircraft.PlayerAircraftsInLevel();
            }
            Aircraft closestAircraft = null;

            foreach (var aircraft in aircrafts)
            {
                if (aircraft.MyState != Aircraft.State.SHOT_DOWN && aircraft != Aircraft && aircraft.Team.IsEnemy(myTeam) && aircraft.IsActive())
                {
                    // check the distance (closer is better)
                    if (closestAircraft == null ||
                        CCPoint.Distance(Aircraft.Position, aircraft.Position) < CCPoint.Distance(Aircraft.Position, closestAircraft.Position))
                    {
                        closestAircraft = aircraft;
                    }
                }
            }
            // if it is too close run away
            if (closestAircraft != null && (Scared || CCPoint.Distance(Aircraft.Position, closestAircraft.Position) < SCARE_DISTANCE))
            {
                //Console.WriteLine("FLEEING");
                Aircraft.TryToSetFlightPathHeadTo(Aircraft.Position + (Aircraft.Position - closestAircraft.Position) * 16);
            }
            // else move randomly
            else
            {
                //Console.WriteLine("Moving randomly");
                Aircraft.TryToSetFlightPathHeadTo(Constants.RandomPointBoxnear(Aircraft.Position, 1000f));
            }
        }
Esempio n. 2
0
        private protected void InitiateChunk(CCPointI chunkPoint)
        {
            // add some squadrons randomly
            var       rng        = new Random();
            const int squadCount = 5;
            // choose random positions inside of this chunk
            CCPoint chunkMiddle = ChunkToWorldPos(chunkPoint);

            for (int i = 0; i < squadCount; i++)
            {
                var randomPos = Constants.RandomPointBoxnear(chunkMiddle, CHUNK_SIZE / 2, rng);
                int zone      = RadiusToZoneNum(randomPos.Length);
                // place more squads in zone 1 and less in zone 3 (statistically)
                switch (zone)
                {
                case 1:
                    if (rng.Next(3) == 0)
                    {
                        i--;
                    }
                    break;

                case 3:
                    if (rng.Next(3) == 0)
                    {
                        i++;
                    }
                    break;

                default:
                    break;
                }
                var newSquadron = GenerateSquadron(zone, rng);
                if (newSquadron != null)
                {
                    // choose a random orientation
                    var CCdirection = (float)rng.NextDouble() * 360f;
                    AddSquadron(newSquadron, randomPos, CCdirection);
                }
            }

            KnownChunks.Add(chunkPoint);
        }
Esempio n. 3
0
        internal void Salvage()
        {
            State = WreckageState.SALVAGING;
            var mAircraft  = MiddleAircraft;
            var totalScale = mAircraft.GetTotalScale();

            mAircraft.Visible = false;
            List <Part> totalparts = mAircraft.TotalParts;

            // unmount and disassemble
            mAircraft.Body = null;
            foreach (var part in totalparts)
            {
                part.Disassemble();
                foreach (var singlePart in totalparts)
                {
                    if (singlePart.Flipped)
                    {
                        singlePart.Flip();
                    }
                }
                // repair the part fully
                part.Reinitialize();
            }
            // choose the parts that will be salvaged
            SalvagedParts = new List <Part>();
            // how many?
            var rng          = new Random();
            int salvageCount = (int)(GetWreckPercentile(mAircraft) * totalparts.Count());

            if (salvageCount != totalparts.Count() && rng.NextDouble() <= (GetWreckPercentile(mAircraft) * totalparts.Count()) % 1)
            {
                salvageCount++;
            }
            // choose random parts
            for (int i = 0; i < salvageCount; i++)
            {
                var index = rng.Next(totalparts.Count());
                SalvagedParts.Add(totalparts.ElementAt(index));
                totalparts.RemoveAt(index);
            }
            float delay    = SalvagedParts.Count * SalvagedParts.Count * 150 + 100;
            float delaySec = delay / 1000;

            // vibrate
            if (Constants.oS != Constants.OS.WINDOWS)
            {
                Vibration.Vibrate(delay * 0.015f);
            }
            // visualize
            var     boundsCenter = VisibleBoundsWorldspace.Center;
            CCPoint pointIn      = boundsCenter + new CCPoint(0, VisibleBoundsWorldspace.Size.Height * 0.6f);
            var     width        = VisibleBoundsWorldspace.Size.Width / 3;
            float   inMoveTime   = 1f;
            Dictionary <Part, CCPoint> destinations = new Dictionary <Part, CCPoint>();

            foreach (var part in SalvagedParts)
            {
                part.Visible = true;
                AddChild(part, -10);
                part.AnchorPoint = CCPoint.AnchorMiddle;
                var rotation = new CCRepeatForever(new CCRotateBy(1, rng.Next(20, 30) * 0.5f));
                rotation.Tag = RotationTag;
                part.AddAction(rotation);
                // find a free spot
                part.Scale = totalScale;
                const float BORDER   = 32f;
                bool        notFound = true;
                // first try to find a free space where the part can rotate without touching anything
                for (int tries = 0; tries < 40 && notFound; tries++)
                {
                    destinations[part] = Constants.RandomPointBoxnear(boundsCenter, width, rng);
                    // check whether the space is free
                    part.Position = destinations[part];
                    CCRect bbox = part.BoundingBoxTransformedToWorld;
                    // construct a bounding square
                    float size = bbox.Size.Height > bbox.Size.Width ? bbox.Size.Height : bbox.Size.Width;
                    bbox = new CCRect(bbox.Center.X - size / 2, bbox.Center.Y - size / 2, size, size);
                    // add a bit of padding
                    CCRect box = new CCRect(bbox.MinX - BORDER, bbox.MinY - BORDER, bbox.Size.Width + 2 * BORDER, bbox.Size.Height + 2 * BORDER);
                    notFound = false;
                    foreach (var otherPart in SalvagedParts)
                    {
                        if (otherPart == part)
                        {
                            continue;
                        }
                        if (box.IntersectsRect(otherPart.BoundingBoxTransformedToWorld))
                        {
                            notFound = true;
                            break;
                        }
                    }
                }
                // if this failed try to find a free space where the parts at least do not touch in starting configuration
                for (int tries = 0; tries < 40 && notFound; tries++)
                {
                    destinations[part] = Constants.RandomPointBoxnear(boundsCenter, width, rng);
                    // check whether the space is free
                    part.Position = destinations[part];
                    CCRect bbox = part.BoundingBoxTransformedToWorld;
                    // add a bit of padding
                    CCRect box = new CCRect(bbox.MinX - BORDER, bbox.MinY - BORDER, bbox.Size.Width + 2 * BORDER, bbox.Size.Height + 2 * BORDER);
                    notFound = false;
                    foreach (var otherPart in SalvagedParts)
                    {
                        if (otherPart == part)
                        {
                            continue;
                        }
                        if (box.IntersectsRect(otherPart.BoundingBoxTransformedToWorld))
                        {
                            notFound = true;
                            break;
                        }
                    }
                }
            }
            foreach (var part in SalvagedParts)
            {
                part.Position = pointIn;
                part.AddAction(new CCSequence(new CCDelayTime(delaySec), new CCEaseOut(new CCMoveTo(inMoveTime, destinations[part]), 2f)));
            }
            // count down the percentage
            float   startP       = GetWreckPercentile(mAircraft);
            CCLabel percentLabel = GetPercentLabel(mAircraft);

            AddAction(new CCSequence(new CCEaseIn(new CCCallFiniteTimeFunc(delaySec, (progress, duration) => { SetWreckPercentile(mAircraft, startP * (1 - progress)); }), 4f), new CCCallFunc(() => { percentLabel.Visible = false; })));
            // if no parts could be salvaged end the salvaged state immediately
            if (!SalvagedParts.Any())
            {
                AddAction(new CCSequence(new CCDelayTime(delaySec + inMoveTime), new CCCallFunc(() => { State = WreckageState.SALVAGED; EndSalvage(); })));
            }
            else
            {
                AddAction(new CCSequence(new CCDelayTime(delaySec + inMoveTime), new CCCallFunc(() => { State = WreckageState.SALVAGED; })));
            }
        }