public void ReturnsValidValue()
 {
     for (int x = -10; x < Constants.MAP_WIDTH + 10; x++)
     {
         for (int y = -10; y < Constants.MAP_HEIGHT + 10; y++)
         {
             var coord     = new Coord(x, y);
             var fastCoord = FastCoord.Create(coord);
             FastCoord.IsInsideMap(fastCoord).Should().Be(coord.IsInsideMap(), coord.ToString());
         }
     }
 }
        private void BuildCannonballsForecast(TurnState turnState)
        {
            for (var i = 0; i < turnState.cannonballs.Count; i++)
            {
                if (turnState.cannonballs[i].turns - 1 >= 0 && turnState.cannonballs[i].turns - 1 < Settings.NAVIGATION_PATH_DEPTH)
                {
                    GetTurnForecast(turnState.cannonballs[i].turns - 1).cannonballCoordsMap[turnState.cannonballs[i].fcoord] = true;
                }
            }

            if (Settings.CANNONBALLS_FORECAST_TRAVEL_TIME_LIMIT > 0)
            {
                foreach (var enemyShip in turnState.enemyShips)
                {
                    if (!enemiesCanFire.Contains(enemyShip.id))
                    {
                        continue;
                    }
                    var targets = Enumerable.Range(0, 6).Select(o => enemyShip.fbow).ToArray();
                    for (var dist = 1; dist <= 10; dist++)
                    {
                        var travelTime = (int)(1 + Math.Round(dist / 3.0));
                        if (travelTime > Settings.CANNONBALLS_FORECAST_TRAVEL_TIME_LIMIT)
                        {
                            break;
                        }
                        if (travelTime >= Settings.NAVIGATION_PATH_DEPTH)
                        {
                            break;
                        }
                        for (var orientation = 0; orientation < 6; orientation++)
                        {
                            targets[orientation] = FastCoord.Neighbor(targets[orientation], orientation);
                        }
                        for (var orientation = 0; orientation < 6; orientation++)
                        {
                            if (FastCoord.IsInsideMap(targets[orientation]))
                            {
                                GetTurnForecast(travelTime).cannonballCoordsMap[targets[orientation]] = true;
                            }
                        }
                    }
                }
            }
        }
 private void BuildMineForecast(int mineCoord)
 {
     if (!FastCoord.IsInsideMap(mineCoord))
     {
         return;
     }
     for (var depth = 0; depth < Settings.NAVIGATION_PATH_DEPTH; depth++)
     {
         var turnForecast = GetTurnForecast(depth);
         turnForecast.mineDamageCoordMap[mineCoord] += Constants.MINE_DAMAGE;
         if (turnForecast.cannonballCoordsMap[mineCoord])
         {
             for (int orientation = 0; orientation < 6; orientation++)
             {
                 var neighbor = FastCoord.Neighbor(mineCoord, orientation);
                 turnForecast.nearMineDamageCoordMap[neighbor] += Constants.NEAR_MINE_DAMAGE;
             }
         }
     }
 }
        private static bool GoForward(int phaseSpeed, ref int coord, ref int bow, ref int stern, ref int orientation, ref int speed, ref int ncoord, ref int nbow, ref int nstern)
        {
            ncoord = coord;
            nbow   = bow;
            nstern = stern;

            if (phaseSpeed > speed)
            {
                return(true);
            }

            var newCoord = bow;

            if (FastCoord.IsInsideMap(newCoord))
            {
                ncoord = newCoord;
                nbow   = FastCoord.Neighbor(ncoord, orientation);
                nstern = FastCoord.Neighbor(ncoord, (orientation + 3) % 6);
                return(true);
            }

            speed = 0;
            return(false);
        }
        private List <FireTarget> GetFireTargets(Ship ship, Ship enemyShip)
        {
            var result      = new List <FireTarget>();
            var enemyIndex  = enemyShip.index;
            var cannonCoord = FastShipPosition.Bow(ship.fposition);

            for (var turn = 0; turn < Settings.CANNONS_TRAVEL_TIME_LIMIT + 1; turn++)
            {
                var forecast      = gameState.forecaster.GetTurnForecast(turn);
                var enemyPosition = forecast.enemyShipsFinalPositions[enemyIndex];

                var coords      = new[] { FastShipPosition.Coord(enemyPosition), FastShipPosition.Bow(enemyPosition), FastShipPosition.Stern(enemyPosition) };
                var targetTypes = new[] { FireTargetType.ShipCenter, FireTargetType.ShipBow, FireTargetType.ShipStern };
                for (var i = 0; i < coords.Length; i++)
                {
                    var target = coords[i];
                    if (FastCoord.IsInsideMap(target))
                    {
                        if (forecast.myShipsPositions.Any(m => FastShipPosition.Collides(m, target)))
                        {
                            continue;
                        }
                        var distanceTo = FastCoord.Distance(cannonCoord, target);
                        if (distanceTo <= 10)
                        {
                            var travelTime = (int)(1 + Math.Round(distanceTo / 3.0));
                            if (travelTime == turn)
                            {
                                result.Add(new FireTarget(target, turn, i == 0 ? Constants.HIGH_DAMAGE : Constants.LOW_DAMAGE, targetTypes[i]));
                            }
                        }
                    }
                }
            }
            return(result);
        }
        private static void Main22(string[] args)
        {
            FastCoord.Init();

            var coordsX = new List <Coord>();

            for (int x = -1; x < Constants.MAP_WIDTH + 1; x++)
            {
                for (int y = -1; y < Constants.MAP_HEIGHT + 1; y++)
                {
                    var coord = new Coord(x, y);
                    coordsX.Add(coord);
                }
            }

            var indexes = Enumerable.Range(0, coordsX.Count).ToArray();
            var seed    = new Random().Next();

            Console.Out.WriteLine($"Seed: {seed}");
            var random = new Random(seed);

            for (int i = 0; i < indexes.Length; i++)
            {
                var r   = random.Next(i, indexes.Length);
                var tmp = indexes[r];
                indexes[r] = indexes[i];
                indexes[i] = tmp;
            }

            var coords     = indexes.Select(i => coordsX[i]).ToArray();
            var fastCoords = indexes.Select(i => FastCoord.Create(coords[i])).ToArray();

            var ships     = coords.Select(c => new ShipPosition(c, random.Next(6), random.Next(3))).ToArray();
            var fastShips = ships.Select(FastShipPosition.Create).ToArray();

            var stopwatch = Stopwatch.StartNew();

            Console.Out.WriteLine("IsInsideMap");
            stopwatch.Restart();
            int ind = 0;

            for (int i = 0; i < 10000000; i++)
            {
                coords[ind++].IsInsideMap();
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                FastCoord.IsInsideMap(fastCoords[ind++]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            Console.Out.WriteLine("DistanceTo");
            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                coords[ind++].DistanceTo(coords[0]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                FastCoord.Distance(fastCoords[ind++], fastCoords[0]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            Console.Out.WriteLine("Neighbor");
            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                coords[ind].Neighbor(0);
                coords[ind].Neighbor(1);
                coords[ind].Neighbor(2);
                coords[ind].Neighbor(3);
                coords[ind].Neighbor(4);
                coords[ind].Neighbor(5);
                if (++ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                FastCoord.Neighbor(fastCoords[ind], 0);
                FastCoord.Neighbor(fastCoords[ind], 1);
                FastCoord.Neighbor(fastCoords[ind], 2);
                FastCoord.Neighbor(fastCoords[ind], 3);
                FastCoord.Neighbor(fastCoords[ind], 4);
                FastCoord.Neighbor(fastCoords[ind], 5);
                if (++ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            Console.Out.WriteLine("ShipDistanceTo");
            var shipPosition = new ShipPosition(coords[0], 0, 0);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                shipPosition.DistanceTo(coords[ind]);
                if (++ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            var fastShipPosition = FastShipPosition.Create(shipPosition);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                FastShipPosition.DistanceTo(fastShipPosition, fastCoords[ind++]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            Console.Out.WriteLine("Collides");
            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                shipPosition.Collides(coords[ind++]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                FastShipPosition.Collides(fastShipPosition, fastCoords[ind++]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            Console.Out.WriteLine("CollidesShip");
            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                shipPosition.CollidesShip(ships[ind++]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 10000000; i++)
            {
                FastShipPosition.CollidesShip(fastShipPosition, fastShips[ind++]);
                if (ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            Console.Out.WriteLine("Move");
            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 1_000_000; i++)
            {
                foreach (var moveCommand in ShipMoveCommands.all)
                {
                    ships[ind].Apply(moveCommand);
                }
                if (++ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 1_000_000; i++)
            {
                foreach (var moveCommand in ShipMoveCommands.all)
                {
                    var moved = FastShipPosition.Move(fastShips[ind], moveCommand);
                    FastShipPosition.GetMovedPosition(moved);
                    FastShipPosition.GetFinalPosition(moved);
                }
                if (++ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);

            stopwatch.Restart();
            ind = 0;
            for (int i = 0; i < 1_000_000; i++)
            {
                foreach (var moveCommand in ShipMoveCommands.all)
                {
                    uint myMovement;
                    uint otherMovement;
                    CollisionChecker.Move(fastShips[ind], moveCommand, fastShips[(ind + 1) % indexes.Length], moveCommand, out myMovement, out otherMovement);
                    FastShipPosition.GetMovedPosition(myMovement);
                    FastShipPosition.GetFinalPosition(myMovement);
                }
                if (++ind >= indexes.Length)
                {
                    ind = 0;
                }
            }
            stopwatch.Stop();
            Console.Out.WriteLine(stopwatch.ElapsedMilliseconds);
        }