private CollisionType GetCollisionType(int my, ShipMoveCommand myc, int other, ShipMoveCommand oc)
        {
            uint myMovement;
            uint otherMovement;
            var  collisionType = CollisionChecker.Move(my, myc, other, oc, out myMovement, out otherMovement);

            Console.Out.WriteLine(FastShipPosition.ToShipPosition(FastShipPosition.GetMovedPosition(myMovement)) + " - " + FastShipPosition.ToShipPosition(FastShipPosition.GetFinalPosition(myMovement)));
            Console.Out.WriteLine(FastShipPosition.ToShipPosition(FastShipPosition.GetMovedPosition(otherMovement)) + " - " + FastShipPosition.ToShipPosition(FastShipPosition.GetFinalPosition(otherMovement)));
            return(collisionType);
        }
Example #2
0
        public void Move_ReturnsValidPosition_Valid()
        {
            int i = 0;

            for (int x = 0; x < Constants.MAP_WIDTH; x++)
            {
                for (int y = 0; y < Constants.MAP_HEIGHT; y++)
                {
                    for (int speed = 0; speed <= Constants.MAX_SHIP_SPEED; speed++)
                    {
                        for (int orientation = 0; orientation < 6; orientation++)
                        {
                            foreach (var moveCommand in ShipMoveCommands.all)
                            {
                                var shipPosition  = new ShipPosition(new Coord(x, y), orientation, speed);
                                var nextPositions = shipPosition.Apply(moveCommand);
                                nextPositions.Count.Should().Be(2);
                                var fastShipPosition = FastShipPosition.Create(shipPosition);

                                for (int phase = 0; phase < nextPositions.Count; phase++)
                                {
                                    if ((i++) % 99 == 0)
                                    {
                                        Console.Out.WriteLine($"shipPosition: {shipPosition}; moveCommand: {moveCommand}; phase: {phase}; nextPosition: {nextPositions[phase]}");
                                    }
                                    var actual = FastShipPosition.ToShipPosition(FastShipPosition.GetPositionAtPhase(FastShipPosition.Move(fastShipPosition, moveCommand), phase));
                                    actual.Should().Be(nextPositions[phase], $"shipPosition: {shipPosition}; moveCommand: {moveCommand}; phase: {phase}; nextPosition: {nextPositions[phase]}");
                                }

                                var  otherPosition = FastShipPosition.Create((x + Constants.MAP_WIDTH / 2) % Constants.MAP_WIDTH, y, 0, 0);
                                uint myMovement;
                                uint otherMovement;
                                CollisionChecker.Move(fastShipPosition, moveCommand, otherPosition, ShipMoveCommand.Wait, out myMovement, out otherMovement);
                                for (int phase = 0; phase < nextPositions.Count; phase++)
                                {
                                    if ((i++) % 99 == 0)
                                    {
                                        Console.Out.WriteLine($"shipPosition: {shipPosition}; moveCommand: {moveCommand}; phase: {phase}; nextPosition: {nextPositions[phase]}");
                                    }
                                    var actual = FastShipPosition.ToShipPosition(FastShipPosition.GetPositionAtPhase(myMovement, phase));
                                    actual.Should().Be(nextPositions[phase], $"shipPosition: {shipPosition}; moveCommand: {moveCommand}; phase: {phase}; nextPosition: {nextPositions[phase]}");
                                }
                            }
                        }
                    }
                }
            }
        }
        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);
        }
        public List <PathItem> FindPath(TurnState turnState, int ftarget, NavigationMethod navigationMethod)
        {
            var ship = turnState.FindMyShip(shipId);

            //if (FastShipPosition.Collides(ship.fposition, ftarget))
            //	return new List<PathItem>();

            var queue = new Queue <ShipPathChainItem>();

            queue.Enqueue(ShipPathChainItem.Start(ship.fposition, ftarget));

            var used = new Dictionary <ShipMovementState, ShipPathChainItem>();

            while (queue.Any())
            {
                var current = queue.Dequeue();
                if (current.depth != Settings.NAVIGATION_PATH_DEPTH)
                {
                    var turnForecast = gameState.forecaster.GetTurnForecast(current.depth);
                    foreach (var moveCommand in ShipMoveCommands.all)
                    {
                        var newShipMovement  = FastShipPosition.Move(current.fposition, moveCommand);
                        var newMovedPos      = FastShipPosition.GetMovedPosition(newShipMovement);
                        var newPos           = FastShipPosition.GetFinalPosition(newShipMovement);
                        var newMovementState = new ShipMovementState(newPos, current.depth + 1);
                        if (!used.ContainsKey(newMovementState))
                        {
                            var onMyShip = false;
                            foreach (var otherShip in turnState.myShips)
                            {
                                if (otherShip == ship)
                                {
                                    continue;
                                }
                                var  otherPosition = turnForecast.myShipsSourcePositions[otherShip.index];
                                uint myMovement;
                                uint otherMovement;
                                var  collisionType = CollisionChecker.Move(current.fposition, moveCommand, otherPosition, turnForecast.myShipsMoveCommands[otherShip.index], out myMovement, out otherMovement);
                                if ((collisionType & (CollisionType.MyMove | CollisionType.MyRotation)) != CollisionType.None)
                                {
                                    newShipMovement  = myMovement;
                                    newMovedPos      = FastShipPosition.GetMovedPosition(newShipMovement);
                                    newPos           = FastShipPosition.GetFinalPosition(newShipMovement);
                                    newMovementState = new ShipMovementState(newPos, current.depth + 1);
                                    onMyShip         = used.ContainsKey(newMovementState);
                                    break;
                                }
                            }

                            if (onMyShip)
                            {
                                continue;
                            }

                            var onEnemyShip = false;
                            if (current.depth == 0)
                            {
                                foreach (var enemyShip in turnState.enemyShips)
                                {
                                    var enemyPosition = enemyShip.fposition;
                                    foreach (var enemyMoveCommand in ShipMoveCommands.all)
                                    {
                                        uint myMovement;
                                        uint enemyMovement;
                                        var  collisionType = CollisionChecker.Move(current.fposition, moveCommand, enemyPosition, enemyMoveCommand, out myMovement, out enemyMovement);
                                        if ((collisionType & (CollisionType.MyMove | CollisionType.MyRotation)) != CollisionType.None)
                                        {
                                            newShipMovement  = myMovement;
                                            newMovedPos      = FastShipPosition.GetMovedPosition(newShipMovement);
                                            newPos           = FastShipPosition.GetFinalPosition(newShipMovement);
                                            newMovementState = new ShipMovementState(newPos, current.depth + 1);
                                            onEnemyShip      = used.ContainsKey(newMovementState);
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                var prevEnemyFinalPositions = gameState.forecaster.GetTurnForecast(Math.Min(current.depth - 1, Settings.NAVIGATOR_ENEMY_POSITION_DEPTH)).enemyShipsFinalPositions;
                                foreach (var enemyPosition in prevEnemyFinalPositions)
                                {
                                    uint myMovement;
                                    uint enemyMovement;
                                    var  collisionType = CollisionChecker.Move(current.fposition, moveCommand, enemyPosition, ShipMoveCommand.Wait, out myMovement, out enemyMovement);
                                    if ((collisionType & (CollisionType.MyMove | CollisionType.MyRotation)) != CollisionType.None)
                                    {
                                        newShipMovement  = myMovement;
                                        newMovedPos      = FastShipPosition.GetMovedPosition(newShipMovement);
                                        newPos           = FastShipPosition.GetFinalPosition(newShipMovement);
                                        newMovementState = new ShipMovementState(newPos, current.depth + 1);
                                        onEnemyShip      = used.ContainsKey(newMovementState);
                                        break;
                                    }
                                }

                                //onEnemyShip = gameState.forecaster.GetTurnForecast(Math.Min(current.depth, Settings.NAVIGATOR_ENEMY_POSITION_DEPTH)).enemyShipsFinalPositions
                                //	.Any(m => FastShipPosition.CollidesShip(newPos, m) || FastShipPosition.CollidesShip(newMovedPos, m));
                            }

                            if (onEnemyShip)
                            {
                                //used.Add(newMovementState, null);
                                continue;
                            }

                            var damage = turnForecast.mineDamageCoordMap[FastShipPosition.Coord(newPos)]
                                         + turnForecast.mineDamageCoordMap[FastShipPosition.Bow(newPos)]
                                         + turnForecast.mineDamageCoordMap[FastShipPosition.Stern(newPos)]
                                         + turnForecast.nearMineDamageCoordMap[FastShipPosition.Bow(newPos)]
                                         + turnForecast.nearMineDamageCoordMap[FastShipPosition.Stern(newPos)];

                            if (newMovedPos != newPos)
                            {
                                damage += turnForecast.mineDamageCoordMap[FastShipPosition.Bow(newMovedPos)]
                                          + turnForecast.mineDamageCoordMap[FastShipPosition.Stern(newMovedPos)]
                                          + turnForecast.nearMineDamageCoordMap[FastShipPosition.Bow(newMovedPos)]
                                          + turnForecast.nearMineDamageCoordMap[FastShipPosition.Stern(newMovedPos)];
                            }

                            var cannonedBowOrStern = turnForecast.cannonballCoordsMap[FastShipPosition.Bow(newPos)] || turnForecast.cannonballCoordsMap[FastShipPosition.Stern(newPos)];
                            if (cannonedBowOrStern)
                            {
                                damage += Constants.LOW_DAMAGE;
                            }

                            var cannonedCenter = turnForecast.cannonballCoordsMap[FastShipPosition.Coord(newPos)];
                            if (cannonedCenter)
                            {
                                damage += Constants.HIGH_DAMAGE;
                            }

                            if (Settings.NEAR_ENEMYSHIP_VIRTUAL_DAMAGE > 0)
                            {
                                var nearEnemyShip = turnState.enemyShips.Any(m => FastShipPosition.DistanceTo(newPos, m.fcoord) < Settings.NEAR_ENEMY_SHIP_MIN_DIST);
                                if (nearEnemyShip)
                                {
                                    damage += Settings.NEAR_ENEMYSHIP_VIRTUAL_DAMAGE;
                                }
                            }

                            var next = current.Next(newPos, moveCommand, ftarget, damage);
                            queue.Enqueue(next);
                            used.Add(newMovementState, next);
                        }
                    }
                }
            }

            ShipPathChainItem bestChainItem = null;

            foreach (var chainItem in used.Values.Where(v => v != null))
            {
                if (chainItem.prev != null)
                {
                    if (bestChainItem == null)
                    {
                        bestChainItem = chainItem;
                    }
                    else
                    {
                        switch (navigationMethod)
                        {
                        case NavigationMethod.Approach:
                            bestChainItem = SelectBestPath_Approach(chainItem, bestChainItem, ship);
                            break;

                        case NavigationMethod.Collect:
                            bestChainItem = SelectBestPath_Collect(chainItem, bestChainItem, ship);
                            break;

                        default:
                            bestChainItem = SelectBestPath(chainItem, bestChainItem, ship);
                            break;
                        }
                    }
                }
            }

            if (bestChainItem == null)
            {
                return(new List <PathItem>());
            }

            var chainDump = new List <ShipPathChainItem>();
            var chain     = new List <PathItem>();

            while (bestChainItem.prev != null)
            {
                chain.Add(new PathItem {
                    command = bestChainItem.command, targetPosition = bestChainItem.fposition, sourcePosition = bestChainItem.prev.fposition
                });
                chainDump.Add(bestChainItem);
                bestChainItem = bestChainItem.prev;
            }
            chainDump.Reverse();
            if (Settings.DUMP_BEST_PATH)
            {
                Console.Error.WriteLine($"Best path for ship {shipId}");
                foreach (var item in chainDump)
                {
                    Console.Error.WriteLine($"{item.command} - {FastShipPosition.ToShipPosition(item.fposition)} - dmg:{item.damage}");
                }
            }
            chain.Reverse();
            return(chain);
        }