Example #1
0
        public int GetAttackDamage2(AVehicle veh, double additionalRadius = 0)
        {
            var attackRange = Geom.Sqr(G.AttackRange[(int)Type, (int)veh.Type] + additionalRadius);

            if (GetDistanceTo2(veh) - Const.Eps > attackRange)
            {
                return(0);
            }
            return(G.AttackDamage[(int)Type, (int)veh.Type]);
        }
Example #2
0
        static double GetSumMaxAlmostAttacks(Sandbox env, IEnumerable <AVehicle> myVehicles)
        {
            Logger.CumulativeOperationStart("Danger0");
            var result = myVehicles.Sum(m =>
            {
                var additionalRadius   = m.ActualSpeed;
                const int radiusFactor = 5;
                return(env.GetOpponentNeighbours(m.X, m.Y, G.MaxAttackRange + additionalRadius * radiusFactor).DefaultIfEmpty(null)
                       .Max(opp =>
                {
                    if (opp == null)
                    {
                        return 0;
                    }
                    var dmg = opp.GetAttackDamage(m, additionalRadius * radiusFactor);
                    if (dmg == 0)
                    {
                        return
                        -(m.GetAttackDamage(opp, additionalRadius) +
                          m.GetAttackDamage(opp, additionalRadius * radiusFactor) / 2.0);
                    }
                    return (opp.GetAttackDamage(m, additionalRadius) +
                            opp.GetAttackDamage(m, additionalRadius * 2) / 2.0 +
                            opp.GetAttackDamage(m, additionalRadius * 3) / 4.0 +
                            opp.GetAttackDamage(m, additionalRadius * 4) / 8.0 +
                            dmg / 16.0
                            ) *
                    (G.AttackDamage[(int)m.Type, (int)opp.Type] > 0
                                   ? Geom.Sqr(1.0 * opp.Durability / G.MaxDurability)
                                   : 1) *
                    (m.Type == opp.Type ? 0.6 : 1) *
                    (G.IsAerialButerDetected && opp.Type == VehicleType.Fighter ? 3 : 1);
                }));
            });

            Logger.CumulativeOperationEnd("Danger0");
            return(result);
        }
Example #3
0
        public static double AreaCoeff(double area, int count)
        {
            var r = Geom.Sqr(2 * G.VehicleRadius) * count / area;

            return(Math.Pow(r, 0.25));
        }
Example #4
0
        Tuple <AMove[], MyGroup, DangerResult> MainLoopStrategy(bool opt)
        {
            VisualSegments.Clear();
            var visualVectorsStart = new List <Point>();
            var visualVectorsDir   = new List <Point>();
            var visualVectorsScore = new List <double>();

            _isSlowMode = _isSlowMode && Environment.OppVehicles.Count == 0;
            var baseTicksCount = Const.ActionsBruteforceDepth;

            if (Environment.Nuclears.Length > 0)
            {
                baseTicksCount *= 2;
            }

            var          selMoves  = new[] { new AMove() };
            MyGroup      selGroup  = null;
            DangerResult selDanger = null;

            var targetFacilities = GetDanger(Environment, Environment, GroupsManager.MyGroups, MyUngroupedClusters).TargetFacility;

            for (var s = 0; s < GroupsManager.MyGroups.Count + MyUngroupedClusters.Count; s++)
            {
                var availableActions = MoveObserver.AvailableActions;
                if (Opp.RemainingNuclearStrikeCooldownTicks < 60 && Environment.Nuclears.Length == 0)
                {
                    availableActions -= 2;
                }

                MyGroup         group;
                List <AVehicle> newGroupVehicles = null;
                if (s < GroupsManager.MyGroups.Count)
                {
                    group = GroupsManager.MyGroups[s];
                }
                else
                {
                    newGroupVehicles = MyUngroupedClusters[s - GroupsManager.MyGroups.Count];
                    if (newGroupVehicles.Count < NewGroupMinSize)
                    {
                        continue;
                    }
                    if (availableActions < 3) // required: clear, move, assign
                    {
                        continue;
                    }
                    group = new MyGroup(GroupsManager.NextGroupId, newGroupVehicles[0].Type);
                }

                if (opt && !(
                        Environment.Nuclears.Length > 0 ||
                        Environment.Facilities.Length <= 4 ||
                        World.TickIndex < 2500 ||
                        MoveObserver.MaxAvailableActions <= 12 ||
                        _doMainLastGroup != null && _doMainLastGroup.Group == group.Group ||                                                                   // ходил предыдущий раз
                        (newGroupVehicles == null && group.Group % 2 == _doMainsCount % 2 || newGroupVehicles != null && group.Group % 3 == _doMainsCount % 3) // через раз/два
                        ))
                {
                    continue;
                }

                if (_isSlowMode && World.TickIndex < 300 && group.VehicleType == VehicleType.Helicopter)
                {
                    continue;
                }

                Logger.CumulativeOperationStart("First env actions");

                var startEnv   = Environment.Clone();
                var ticksCount = baseTicksCount;

                if (newGroupVehicles != null)
                {
                    foreach (var newVeh in newGroupVehicles)
                    {
                        startEnv.AddVehicleGroup(startEnv.VehicleById[newVeh.Id], group.Group);
                    }
                }

                var selectedIds     = Utility.UnitsHash(startEnv.MyVehicles.Where(x => x.IsSelected));
                var needToSelectIds = Utility.UnitsHash(startEnv.GetVehicles(true, group));

                List <AMove> selectionMoves = new List <AMove>();

                if (selectedIds != needToSelectIds)
                {
                    if (availableActions < 2) // required: select, move
                    {
                        Logger.CumulativeOperationEnd("First env actions");
                        continue;
                    }

                    if (newGroupVehicles == null)
                    {
                        selectionMoves.Add(AMovePresets.ClearAndSelectGroup(group.Group));
                    }
                    else
                    {
                        var selectionMove = AMovePresets.ClearAndSelectType(group.VehicleType, Utility.BoundingRect(newGroupVehicles));
                        startEnv.ApplyMove(selectionMove); // ниже выполнится лишний раз, но не страшно
                        selectionMoves.Add(selectionMove);

                        foreach (var mg in GroupsManager.MyGroups)
                        {
                            if (mg.VehicleType == group.VehicleType && startEnv.MyVehicles.Where(x => x.IsSelected).Any(x => x.IsGroup(mg)))
                            {
                                selectionMoves.Add(new AMove {
                                    Action = ActionType.Deselect, Group = mg.Group
                                });
                            }
                        }
                    }

                    if (availableActions < selectionMoves.Count + 1) // required: select, move, deselect all
                    {
                        Logger.CumulativeOperationEnd("First env actions");
                        continue;
                    }

                    foreach (var mv in selectionMoves)
                    {
                        startEnv.ApplyMove(mv);
                        availableActions--;
                    }
                }
                availableActions--;

                Logger.CumulativeOperationEnd("First env actions");

                // availableActions теперь - это сколько действий останется после выполнения текущего тика
                // должно остаться хотябы 2, или 0, если обладатель ядерки selected
                var myNuclear = startEnv.Nuclears.FirstOrDefault(n => n.IsMy);
                if (myNuclear != null && availableActions < 2 - (startEnv.VehicleById.ContainsKey(myNuclear.VehicleId) && startEnv.VehicleById[myNuclear.VehicleId].IsSelected ? 2 : 0))
                {
                    continue;
                }

                var typeRect = Utility.BoundingRect(startEnv.GetVehicles(true, group));

                Sandbox partialEnv = null;
                var     sumMaxAlmostAttacksCache = -1.0;
                if (Environment.Nuclears.Length == 0)
                {
                    Logger.CumulativeOperationStart("Partial env actions");

                    partialEnv = new Sandbox(
                        startEnv.Vehicles.Where(x => !x.IsSelected),
                        new ANuclear[] { },
                        startEnv.Facilities,
                        clone: true
                        );
                    partialEnv.CheckCollisionsWithOpponent = false;

                    var grps = GroupsManager.MyGroups.Select(g => partialEnv.GetVehicles(true, g).ToArray()).ToArray();

                    try
                    {
                        for (var i = 0; i < ticksCount; i++)
                        {
                            for (var j = 0; j < grps.Length; j++)
                            {
                                var pgr = grps[j];
                                if (pgr.Length > 0)
                                {
                                    partialEnv.DoMoveApprox(pgr, pgr[0].Action != AVehicle.MoveType.Scale);
                                }
                            }
                            partialEnv.DoFacilities();
                            partialEnv.DoNuclears();
                        }
                        partialEnv.DoFight();

                        sumMaxAlmostAttacksCache = GetSumMaxAlmostAttacks(partialEnv, partialEnv.MyVehicles);
                    }
                    catch (QuadTree <AVehicle> .PointAlreadyExistsException e)
                    {
                        Logger.Log(e.Message);
                        continue;
                    }
                    finally
                    {
                        Logger.CumulativeOperationEnd("Partial env actions");
                    }
                }

                Logger.CumulativeOperationStart("Pre last env actions");

                Sandbox         preLastEnv             = null;
                AVehicle[]      nearInteractors        = null;
                List <AVehicle> currentVehicles        = null;
                AVehicle[]      preLastEnvVehiclesCopy = null;

                if (partialEnv != null)
                {
                    currentVehicles = startEnv.GetVehicles(true, group);
                    var currentVehiclesCenter = Utility.BoundingRect(currentVehicles).Center;
                    nearInteractors = partialEnv.Vehicles
                                      .Where(x =>
                                             !x.IsMy && x.GetDistanceTo2(currentVehiclesCenter) < Geom.Sqr(2 * G.TacticalNuclearStrikeRadius) ||
                                             x.IsMy && x.IsAerial == Utility.IsAerial(group.VehicleType) && x.GetDistanceTo2(currentVehiclesCenter) < Geom.Sqr(G.TacticalNuclearStrikeRadius))
                                      .ToArray();

                    preLastEnv = new Sandbox(
                        partialEnv.Vehicles.Concat(currentVehicles),
                        new ANuclear[] { },
                        new AFacility[] { },
                        clone: true
                        );
                    preLastEnvVehiclesCopy = preLastEnv.Vehicles.Select(x => new AVehicle(x)).ToArray();
                }

                Logger.CumulativeOperationEnd("Pre last env actions");

                Func <AMove, double> checkAction = move =>
                {
                    Logger.CumulativeOperationStart("Building nearby environment");
                    Sandbox env;

                    if (partialEnv == null)
                    {
                        env = startEnv.Clone();
                    }
                    else
                    {
                        env = new Sandbox(nearInteractors.Concat(currentVehicles),
                                          startEnv.Nuclears, partialEnv.Facilities, clone: true);
                    }

                    env.CheckCollisionsWithOpponent = false;
                    env.UseFightOptimization        = false;

                    if (partialEnv != null)
                    {
                        foreach (var veh in env.Vehicles)
                        {
                            if (veh.IsGroup(group))
                            {
                                continue;
                            }

                            veh.ForgotTarget();            // чтобы не шли повторно
                            veh.CanChargeFacility = false; // чтобы не захватывали повторно
                            // TODO: лечение

                            if (veh.RemainingAttackCooldownTicks > 0) // у тех, кто стрелял давно, откатываем кд
                            {
                                veh.RemainingAttackCooldownTicks += ticksCount;
                            }
                            // TODO: если кд только восстановилось
                        }
                    }
                    Logger.CumulativeOperationEnd("Building nearby environment");

                    Logger.CumulativeOperationStart("End of simulation");
                    env.ApplyMove(move);
                    try
                    {
                        env.DoTicksApprox(ticksCount,
                                          moveApprox: move.Action == ActionType.Move || move.Action == ActionType.Rotate,
                                          fightApprox: true);
                    }
                    catch (QuadTree <AVehicle> .PointAlreadyExistsException e)
                    {
                        Logger.Log(e.Message);
                        return(double.PositiveInfinity);
                    }
                    finally
                    {
                        Logger.CumulativeOperationEnd("End of simulation");
                    }

                    Logger.CumulativeOperationStart("Building last environment");
                    var cache = sumMaxAlmostAttacksCache;
                    if (partialEnv != null)
                    {
                        var tmp = env;
                        env = preLastEnv;

                        for (var i = 0; i < env.Vehicles.Length; i++)
                        {
                            var veh = env.Vehicles[i];
                            if (tmp.VehicleById.ContainsKey(veh.Id))
                            {
                                env.UpdateVehicle(veh, tmp.VehicleById[veh.Id]);
                            }
                            else
                            {
                                env.UpdateVehicle(veh, preLastEnvVehiclesCopy[i]);
                            }
                        }

                        env.Nuclears   = tmp.Nuclears;
                        env.Facilities = tmp.Facilities;
                        cache         += GetSumMaxAlmostAttacks(env, env.GetVehicles(true, group));
                    }

                    var myGroups   = GroupsManager.MyGroups.AsEnumerable();
                    var myUngroups = MyUngroupedClusters.AsEnumerable();
                    if (newGroupVehicles != null)
                    {
                        myGroups   = myGroups.ConcatSingle(group);
                        myUngroups = myUngroups.Where(cl => !cl.Equals(newGroupVehicles));
                    }

                    if (env.Vehicles.Length != Environment.Vehicles.Length)
                    {
                        throw new Exception("Final sandbox size mismatch");
                    }
                    Logger.CumulativeOperationEnd("Building last environment");


                    var danger = GetDanger(Environment, env, myGroups.ToList(), myUngroups.ToList(), cache);

                    if (move.Action == ActionType.Move)
                    {
                        visualVectorsStart.Add(typeRect.Center);
                        visualVectorsDir.Add(new Point(move.X, move.Y).Normalized());
                        visualVectorsScore.Add(danger.Score);
                    }

                    if (selDanger == null || danger.Score < selDanger.Score)
                    {
                        selDanger = danger;
                        selMoves  = selectionMoves.Concat(new[]
                        {
                            move,
                            newGroupVehicles == null
                                ? null
                                : AMovePresets.AssignGroup(group.Group)
                        })
                                    .Where(x => x != null)
                                    .ToArray();
                        selGroup = group;
                    }
                    return(danger.Score);
                };

                var typeRectCenter = typeRect.Center;
                var simpleMode     = Environment.Nuclears.Length == 0 && (
                    Environment.OppVehicles.Count == 0 ||
                    Environment.OppVehicles.Min(x => x.GetDistanceTo2(typeRectCenter)) > Geom.Sqr(120));

                var moveMaxSpeed = _isSlowMode && Utility.IsAerial(group.VehicleType) ? G.MaxSpeed[(int)group.VehicleType] * 0.6 : 0;

                Func <int, AMove> idxToMove = idx => AMovePresets.Move(
                    Point.ByAngle(2 * Math.PI / 12 * idx)
                    * startEnv.MyVehicles.Where(x => x.IsSelected).Max(x => x.ActualSpeed)
                    * ticksCount * 40,
                    group.Group == GroupsManager.StartingTanksGroupId && Const.MixArrvsWithGrounds
                        ? Math.Min(G.MaxSpeed[(int)VehicleType.Tank], G.MaxSpeed[(int)VehicleType.Arrv])
                        : group.Group == GroupsManager.StartingIfvsGroupId && Const.MixArrvsWithGrounds
                            ? Math.Min(G.MaxSpeed[(int)VehicleType.Ifv], G.MaxSpeed[(int)VehicleType.Arrv])
                            : moveMaxSpeed
                    );


                double[] dangers;
                if (simpleMode)
                {
                    // проверка на каждые 3 часа
                    dangers = Enumerable.Range(0, 4).Select(i => checkAction(idxToMove(i * 3))).ToArray();
                }
                else
                {
                    // проверка на каждые 2 часа
                    dangers = Enumerable.Range(0, 6).Select(i => checkAction(idxToMove(i * 2))).ToArray();

                    // проверка на середины лучших промежутков
                    var dangers2 = dangers.Select((x, i) => new Tuple <double, int>(x, i)).OrderBy(x => x.Item1).Select(x => x.Item2).Take(2).ToArray();
                    foreach (var i in dangers2.Select(i => i * 2 + 1).Concat(dangers2.Select(i => i * 2 - 1)).Distinct())
                    {
                        checkAction(idxToMove(i));
                    }
                }

                foreach (var move in
                         Environment.Nuclears
                         .Where(n =>
                                n.GetDistanceTo(Utility.Average(startEnv.MyVehicles.Where(x => x.IsSelected))) <
                                G.TacticalNuclearStrikeRadius * 2)
                         .SelectMany(nuclear => new[]
                {
                    AMovePresets.Scale(nuclear, 1.5),
                    AMovePresets.Scale(nuclear, 3.0),
                    Environment.VehicleById.ContainsKey(nuclear.VehicleId) ? AMovePresets.Scale((Environment.VehicleById[nuclear.VehicleId] + nuclear) / 2, 2.0) : null,
                }))
                {
                    if (move != null)
                    {
                        checkAction(move);
                    }
                }

                checkAction(AMovePresets.Scale(typeRect.Center, 0.1));
                if (_doMainLastUnscale.ContainsKey(group.Group))
                {
                    var prevScaleMove = _doMainLastUnscale[group.Group].Item2;
                    checkAction(AMovePresets.Scale(prevScaleMove.Point, 1 / prevScaleMove.Factor));
                }
                checkAction(AMovePresets.Rotate(typeRect.Center, Math.PI / 4));
                checkAction(AMovePresets.Rotate(typeRect.Center, -Math.PI / 4));

                List <Point> positiveInteractors = new List <Point>(), negativeInteractors = new List <Point>();
                foreach (var cluster in OppClusters)
                {
                    foreach (var oppType in Const.AllTypes)
                    {
                        if (cluster.CountByType[(int)oppType] > 0)
                        {
                            var avg = Utility.Average(cluster.VehicleType(oppType));
                            if (G.AttackDamage[(int)group.VehicleType, (int)oppType] > 0)
                            {
                                positiveInteractors.Add(avg);
                            }
                            else if (G.AttackDamage[(int)oppType, (int)group.VehicleType] > 0)
                            {
                                negativeInteractors.Add(avg);
                            }
                        }
                    }
                }

                foreach (var move in
                         positiveInteractors.OrderBy(cen => cen.GetDistanceTo2(typeRect.Center))
                         .Take(2)
                         .Select(cen => AMovePresets.MoveTo(typeRect.Center, cen, moveMaxSpeed)))
                {
                    checkAction(move);
                }

                foreach (var move in
                         negativeInteractors.OrderBy(cen => cen.GetDistanceTo2(typeRect.Center))
                         .Take(1)
                         .Select(cen => AMovePresets.Move((typeRect.Center - cen).Take(150), moveMaxSpeed)))
                {
                    checkAction(move);
                }

                if (targetFacilities.ContainsKey(group.Group))
                {
                    checkAction(AMovePresets.MoveTo(typeRect.Center, targetFacilities[group.Group], moveMaxSpeed));
                }

                // проверка действия "ничего не делать"
                if (selectionMoves.Count == 0)
                {
                    checkAction(new AMove());
                }

                // полет к ближайшему Arrv
                if (group.VehicleType == VehicleType.Helicopter || group.VehicleType == VehicleType.Fighter)
                {
                    var arrvGroupsCenters = GroupsManager.MyGroups
                                            .Where(g => g.VehicleType == VehicleType.Arrv)
                                            .Select(g => Utility.BoundingRect(Environment.GetVehicles(true, g)).Center)
                                            .ToArray();
                    if (arrvGroupsCenters.Length > 0)
                    {
                        checkAction(AMovePresets.MoveTo(typeRect.Center,
                                                        arrvGroupsCenters.ArgMin(p => p.GetDistanceTo2(typeRect.Center)), moveMaxSpeed));
                    }
                }
            }

#if DEBUG
            //if (selDanger != null)
            //{
            //    var maxScore = visualVectorsScore.Max();
            //    var minScore = visualVectorsScore.Min();
            //    if (maxScore > minScore)
            //    {
            //        for (var i = 0; i < visualVectorsStart.Count; i++)
            //        {
            //            VisualSegments.Add(new object[]
            //            {
            //                new List<Point>
            //                {
            //                    visualVectorsStart[i],
            //                    visualVectorsStart[i] + visualVectorsDir[i]*((visualVectorsScore[i]-minScore)/(minScore-maxScore)*40)
            //                },
            //                new System.Drawing.Pen(System.Drawing.Color.Green),
            //                5
            //            });
            //        }
            //    }
            //}
#endif

            return(new Tuple <AMove[], MyGroup, DangerResult>(selMoves, selGroup, selDanger));
        }
Example #5
0
        private Tuple <double, AMove> _nuclearFindMove(Sandbox env, double selTotalDamage, bool checkOnly)
        {
            AMove selMove = null;

            for (var s = 0; s < GroupsManager.MyGroups.Count + MyUngroupedClusters.Count; s++)
            {
                var vehicles = s < GroupsManager.MyGroups.Count
                    ? env.GetVehicles(true, GroupsManager.MyGroups[s])
                    : MyUngroupedClusters[s - GroupsManager.MyGroups.Count];
                var myAvg = Utility.Average(vehicles);

                var vrg = G.VisionRange[(int)VehicleType.Fighter] + G.MaxTacticalNuclearStrikeDamage;

                var oppGroups = OppClusters
                                .Where(cl =>
                                       cl.Avg.GetDistanceTo(myAvg) < vrg &&
                                       cl.Any(x => env.VehicleById.ContainsKey(x.Id)))// пропускать полностью фантомные группы
                                .OrderBy(cl => cl.Avg.GetDistanceTo(myAvg))
                                .Take(3)
                                .ToArray();

                foreach (var veh in vehicles)
                {
                    if (!veh.IsSelected && MoveObserver.AvailableActions < 3)
                    {
                        continue;
                    }
                    if (veh.IsSelected && MoveObserver.AvailableActions < 2)
                    {
                        continue;
                    }

                    var vr = veh.ActualVisionRange * 0.9;

                    foreach (
                        var oppGroup in
                        new[] { env.GetOpponentNeighbours(veh.X, veh.Y, vr + G.TacticalNuclearStrikeRadius) }.Concat(oppGroups))
                    {
                        ANuclear nuclear;
                        var      totalDamage = _nuclearGetDamage(veh, env, selTotalDamage, oppGroup, out nuclear);

                        if (totalDamage <= selTotalDamage)
                        {
                            continue;
                        }

                        var vehNextMove = new AVehicle(veh);
                        for (var t = 0; t < 3; t++)
                        {
                            vehNextMove.Move();
                        }
                        if (vehNextMove.GetDistanceTo2(nuclear) + Const.Eps >= Geom.Sqr(vehNextMove.ActualVisionRange))
                        {
                            continue;
                        }

                        const int n = 10;
                        if (vehicles.Count > n)
                        {
                            var myDist2        = veh.GetDistanceTo2(nuclear);
                            var myNearestCount = vehicles.Count(x => x.GetDistanceTo2(nuclear) <= myDist2);
                            if (myNearestCount < n)
                            {
                                continue;
                            }
                        }

                        selTotalDamage = totalDamage;
                        selMove        = new AMove
                        {
                            Action    = ActionType.TacticalNuclearStrike,
                            VehicleId = veh.Id,
                            Point     = nuclear,
                        };

                        if (checkOnly)
                        {
                            return(new Tuple <double, AMove>(selTotalDamage, selMove));
                        }
                    }
                }
            }

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

            return(new Tuple <double, AMove>(selTotalDamage, selMove));
        }
Example #6
0
        private void _findAllNearby(Node node, double left, double top, double right, double bottom)
        {
            if (node.Value != null)
            {
                if (_getSquaredDistanceTo(node.Value) <= _squaredDistanceE)
                {
                    _values.Add(node.Value);
                }
                return;
            }

            if (!node.HasValueBelow)
            {
                return;
            }

            var centerX = (left + right) / 2.0D;
            var centerY = (top + bottom) / 2.0D;

            if (_x < centerX)
            {
                if (_y < centerY)
                {
                    _findAllNearby(node.LeftTop, left, top, centerX, centerY);

                    if (_squaredDistanceE >= Geom.Sqr(centerX - _x))
                    {
                        _findAllNearby(node.RightTop, centerX, top, right, centerY);
                    }

                    if (_squaredDistanceE >= Geom.Sqr(centerY - _y))
                    {
                        _findAllNearby(node.LeftBottom, left, centerY, centerX, bottom);
                    }

                    if (_squaredDistanceE >= Geom.SumSqr(centerX - _x, centerY - _y))
                    {
                        _findAllNearby(node.RightBottom, centerX, centerY, right, bottom);
                    }
                }
                else
                {
                    _findAllNearby(node.LeftBottom, left, centerY, centerX, bottom);

                    if (_squaredDistanceE >= Geom.Sqr(centerX - _x))
                    {
                        _findAllNearby(node.RightBottom, centerX, centerY, right, bottom);
                    }

                    if (_squaredDistanceE > Geom.Sqr(_y - centerY))
                    {
                        _findAllNearby(node.LeftTop, left, top, centerX, centerY);
                    }

                    if (_squaredDistanceE >= Geom.SumSqr(centerX - _x, _y - centerY))
                    {
                        _findAllNearby(node.RightTop, centerX, top, right, centerY);
                    }
                }
            }
            else
            {
                if (_y < centerY)
                {
                    _findAllNearby(node.RightTop, centerX, top, right, centerY);

                    if (_squaredDistanceE > Geom.Sqr(_x - centerX))
                    {
                        _findAllNearby(node.LeftTop, left, top, centerX, centerY);
                    }

                    if (_squaredDistanceE >= Geom.Sqr(centerY - _y))
                    {
                        _findAllNearby(node.RightBottom, centerX, centerY, right, bottom);
                    }

                    if (_squaredDistanceE >= Geom.SumSqr(_x - centerX, centerY - _y))
                    {
                        _findAllNearby(node.LeftBottom, left, centerY, centerX, bottom);
                    }
                }
                else
                {
                    _findAllNearby(node.RightBottom, centerX, centerY, right, bottom);

                    if (_squaredDistanceE > Geom.Sqr(_x - centerX))
                    {
                        _findAllNearby(node.LeftBottom, left, centerY, centerX, bottom);
                    }

                    if (_squaredDistanceE > Geom.Sqr(_y - centerY))
                    {
                        _findAllNearby(node.RightTop, centerX, top, right, centerY);
                    }

                    if (_squaredDistanceE > Geom.SumSqr(_x - centerX, _y - centerY))
                    {
                        _findAllNearby(node.LeftTop, left, top, centerX, centerY);
                    }
                }
            }
        }
Example #7
0
        private T _findNearest(Node node, double left, double top, double right, double bottom)
        {
            if (node.Value != null)
            {
                if (_getSquaredDistanceTo(node.Value, _squaredDistance) <= _squaredDistanceE)
                {
                    return(node.Value);
                }
                return(null);
            }

            if (!node.HasValueBelow)
            {
                return(null);
            }

            var centerX = (left + right) / 2.0D;
            var centerY = (top + bottom) / 2.0D;

            if (_x < centerX)
            {
                if (_y < centerY)
                {
                    var nearestValue           = _findNearest(node.LeftTop, left, top, centerX, centerY);
                    var nearestSquaredDistance = _getSquaredDistanceTo(nearestValue, _squaredDistance);

                    if (nearestSquaredDistance + _epsilon >= Geom.Sqr(centerX - _x))
                    {
                        var otherValue           = _findNearest(node.RightTop, centerX, top, right, centerY);
                        var otherSquaredDistance = _getSquaredDistanceTo(otherValue, _squaredDistance);

                        if (otherSquaredDistance < nearestSquaredDistance)
                        {
                            nearestValue           = otherValue;
                            nearestSquaredDistance = otherSquaredDistance;
                        }
                    }

                    if (nearestSquaredDistance + _epsilon >= Geom.Sqr(centerY - _y))
                    {
                        var otherValue           = _findNearest(node.LeftBottom, left, centerY, centerX, bottom);
                        var otherSquaredDistance = _getSquaredDistanceTo(otherValue, _squaredDistance);

                        if (otherSquaredDistance < nearestSquaredDistance)
                        {
                            nearestValue           = otherValue;
                            nearestSquaredDistance = otherSquaredDistance;
                        }
                    }

                    if (nearestSquaredDistance + _epsilon >= Geom.SumSqr(centerX - _x, centerY - _y))
                    {
                        var otherValue           = _findNearest(node.RightBottom, centerX, centerY, right, bottom);
                        var otherSquaredDistance = _getSquaredDistanceTo(otherValue, _squaredDistance);

                        if (otherSquaredDistance < nearestSquaredDistance)
                        {
                            nearestValue = otherValue;
                        }
                    }

                    return(nearestValue);
                }
                else
                {
                    var nearestValue           = _findNearest(node.LeftBottom, left, centerY, centerX, bottom);
                    var nearestSquaredDistance = _getSquaredDistanceTo(nearestValue, _squaredDistance);

                    if (nearestSquaredDistance + _epsilon >= Geom.Sqr(centerX - _x))
                    {
                        var otherValue           = _findNearest(node.RightBottom, centerX, centerY, right, bottom);
                        var otherSquaredDistance = _getSquaredDistanceTo(otherValue, _x, _y, _squaredDistance);

                        if (otherSquaredDistance < nearestSquaredDistance)
                        {
                            nearestValue           = otherValue;
                            nearestSquaredDistance = otherSquaredDistance;
                        }
                    }

                    if (nearestSquaredDistance + _epsilon > Geom.Sqr(_y - centerY))
                    {
                        var otherValue           = _findNearest(node.LeftTop, left, top, centerX, centerY);
                        var otherSquaredDistance = _getSquaredDistanceTo(otherValue, _squaredDistance);

                        if (otherSquaredDistance < nearestSquaredDistance)
                        {
                            nearestValue           = otherValue;
                            nearestSquaredDistance = otherSquaredDistance;
                        }
                    }

                    if (nearestSquaredDistance + _epsilon >= Geom.SumSqr(centerX - _x, _y - centerY))
                    {
                        var otherValue           = _findNearest(node.RightTop, centerX, top, right, centerY);
                        var otherSquaredDistance = _getSquaredDistanceTo(otherValue, _squaredDistance);

                        if (otherSquaredDistance < nearestSquaredDistance)
                        {
                            nearestValue = otherValue;
                        }
                    }

                    return(nearestValue);
                }
            }
            else
            {
                if (_y < centerY)
                {
                    var nearestValue           = _findNearest(node.RightTop, centerX, top, right, centerY);
                    var nearestSquaredDistance = _getSquaredDistanceTo(nearestValue, _squaredDistance);

                    if (nearestSquaredDistance + _epsilon > Geom.Sqr(_x - centerX))
                    {
                        var otherValue           = _findNearest(node.LeftTop, left, top, centerX, centerY);
                        var otherSquaredDistance = _getSquaredDistanceTo(otherValue, _squaredDistance);

                        if (otherSquaredDistance < nearestSquaredDistance)
                        {
                            nearestValue           = otherValue;
                            nearestSquaredDistance = otherSquaredDistance;
                        }
                    }

                    if (nearestSquaredDistance + _epsilon >= Geom.Sqr(centerY - _y))
                    {
                        var otherValue           = _findNearest(node.RightBottom, centerX, centerY, right, bottom);
                        var otherSquaredDistance = _getSquaredDistanceTo(otherValue, _squaredDistance);

                        if (otherSquaredDistance < nearestSquaredDistance)
                        {
                            nearestValue           = otherValue;
                            nearestSquaredDistance = otherSquaredDistance;
                        }
                    }

                    if (nearestSquaredDistance + _epsilon >= Geom.SumSqr(_x - centerX, centerY - _y))
                    {
                        var otherValue           = _findNearest(node.LeftBottom, left, centerY, centerX, bottom);
                        var otherSquaredDistance = _getSquaredDistanceTo(otherValue, _squaredDistance);

                        if (otherSquaredDistance < nearestSquaredDistance)
                        {
                            nearestValue = otherValue;
                        }
                    }

                    return(nearestValue);
                }
                else
                {
                    var nearestValue           = _findNearest(node.RightBottom, centerX, centerY, right, bottom);
                    var nearestSquaredDistance = _getSquaredDistanceTo(nearestValue, _squaredDistance);

                    if (nearestSquaredDistance + _epsilon > Geom.Sqr(_x - centerX))
                    {
                        var otherValue           = _findNearest(node.LeftBottom, left, centerY, centerX, bottom);
                        var otherSquaredDistance = _getSquaredDistanceTo(otherValue, _squaredDistance);

                        if (otherSquaredDistance < nearestSquaredDistance)
                        {
                            nearestValue           = otherValue;
                            nearestSquaredDistance = otherSquaredDistance;
                        }
                    }

                    if (nearestSquaredDistance + _epsilon > Geom.Sqr(_y - centerY))
                    {
                        var otherValue           = _findNearest(node.RightTop, centerX, top, right, centerY);
                        var otherSquaredDistance = _getSquaredDistanceTo(otherValue, _squaredDistance);

                        if (otherSquaredDistance < nearestSquaredDistance)
                        {
                            nearestValue           = otherValue;
                            nearestSquaredDistance = otherSquaredDistance;
                        }
                    }

                    if (nearestSquaredDistance + _epsilon > Geom.SumSqr(_x - centerX, _y - centerY))
                    {
                        var otherValue           = _findNearest(node.LeftTop, left, top, centerX, centerY);
                        var otherSquaredDistance = _getSquaredDistanceTo(otherValue, _squaredDistance);

                        if (otherSquaredDistance < nearestSquaredDistance)
                        {
                            nearestValue = otherValue;
                        }
                    }

                    return(nearestValue);
                }
            }
        }
Example #8
0
 public virtual bool IntersectsWith(ACircularUnit unit)
 {
     // если касаются, то false
     return(GetDistanceTo2(unit) < Geom.Sqr(Radius + unit.Radius));
 }
Example #9
0
        public void DoNuclears()
        {
            var needRemove = false;

            foreach (var nuclear in Nuclears)
            {
                if (VehicleById.ContainsKey(nuclear.VehicleId)) // если нет информации о владельце, то оставлять бомбу
                {
                    var owner = VehicleById[nuclear.VehicleId];
                    if (!owner.IsAlive || owner.GetDistanceTo2(nuclear) + Const.Eps > Geom.Sqr(owner.ActualVisionRange))
                    {
                        needRemove             = true;
                        nuclear.RemainingTicks = 0;
                        continue;
                    }
                }

                nuclear.RemainingTicks--;
                if (nuclear.RemainingTicks == 0)
                {
                    needRemove = true;

                    foreach (var target in GetAllNeighbours(nuclear.X, nuclear.Y, nuclear.Radius))
                    {
                        target.Durability -= target.GetNuclearDamage(nuclear);
                    }
                }
            }
            if (needRemove)
            {
                Nuclears = Nuclears.Where(x => x.RemainingTicks > 0).ToArray();
            }
        }
Example #10
0
        private void _doMove1(AVehicle[] vehicles)
        {
            var unblockedLength = 0;

            for (var i = 0; i < vehicles.Length; i++)
            {
                var veh = vehicles[i];
                if (veh.Action == AVehicle.MoveType.None)
                {
                    veh.Move();
                    _complete[i] = true;
                    continue;
                }
                if (_movedState[i] == null)
                {
                    _movedState[i] = new AVehicle(veh);
                }
                else
                {
                    _movedState[i].CopyFrom(veh);
                }

                if (!_movedState[i].Move())
                {
                    veh.CopyFrom(_movedState[i]);
                    _complete[i] = true;
                    continue;
                }

                _unblocked[unblockedLength++] = i;
                _complete[i] = false;
                veh.Index    = i;

                if (_deps[i] == null)
                {
                    _deps[i] = new int[MagicMaxDependenciesCount];
                }
                _depsLen[i] = 0;
            }

            while (unblockedLength > 0)
            {
                var unblockedNewLength = 0;

                for (var i = 0; i < unblockedLength; i++)
                {
                    var idx       = _unblocked[i];
                    var movedUnit = _movedState[idx];
                    var unitTree  = _tree(movedUnit.IsMy, movedUnit.IsAerial);
                    var oppTree   = _tree(!movedUnit.IsMy, movedUnit.IsAerial);

                    var intersectsWith = -1;

                    do
                    {
                        {
                            var nearest = unitTree.FindFirstNearby(movedUnit, Geom.Sqr(2 * movedUnit.Radius));

                            if (nearest != null)
                            {
                                if (nearest.IntersectsWith(movedUnit))
                                {
                                    intersectsWith = nearest.Index;
                                    break;
                                }
                            }
                        }

                        if (!movedUnit.IsAerial && CheckCollisionsWithOpponent)
                        {
                            var nearest = oppTree.FindFirstNearby(movedUnit, Geom.Sqr(2 * movedUnit.Radius));

                            if (nearest != null)
                            {
                                if (nearest.IntersectsWith(movedUnit))
                                {
                                    intersectsWith = nearest.Index;
                                    break;
                                }
                            }
                        }
                    } while (false);

                    if (intersectsWith != -1)
                    {
                        if (!_complete[intersectsWith])
                        {
                            _deps[intersectsWith][_depsLen[intersectsWith]++] = idx;
                        }
                    }
                    else
                    {
                        var unit  = vehicles[idx];
                        var prevX = unit.X;
                        var prevY = unit.Y;
                        unit.CopyFrom(movedUnit);

                        if (!Geom.PointsEquals(prevX, prevY, movedUnit.X, movedUnit.Y))
                        {
                            unit.X = prevX;
                            unit.Y = prevY;

                            if (!unitTree.ChangeXY(unit, movedUnit.X, movedUnit.Y))
                            {
                                throw new Exception("Can't change unit coordinates, id=" + unit.Id);
                            }


                            if (_nearestFightersCacheTick != null && _nearestFightersCacheTick[idx] != -1)
                            {
                                _nearestFightersCacheDist[idx] -= unit.GetDistanceTo(prevX, prevY);
                            }
                        }
                    }

                    if (intersectsWith == -1 || _complete[intersectsWith])
                    {
                        // resolve dependencies
                        _complete[idx] = true;
                        for (var k = 0; k < _depsLen[idx]; k++)
                        {
                            _unblocked2[unblockedNewLength++] = _deps[idx][k];
                        }
                    }
                }

                unblockedLength = unblockedNewLength;
                Utility.Swap(ref _unblocked, ref _unblocked2);
            }
        }
Example #11
0
 public AVehicle GetFirstIntersector(ACircularUnit circle, bool isAerial)
 {
     for (var isMy = 0; isMy < 2; isMy++)
     {
         var nearby = _tree(isMy == 1, isAerial).FindFirstNearby(circle.X, circle.Y, Geom.Sqr(circle.Radius + G.VehicleRadius), -1);
         if (nearby != null && nearby.IntersectsWith(circle))
         {
             return(nearby);
         }
     }
     return(null);
 }