Exemple #1
0
        /// <summary>
        /// Gets a direction of tile2 related to tile1.
        /// </summary>
        /// <param name="tile1"></param>
        /// <param name="tile2"></param>
        /// <returns></returns>
        public static Direction GetDirection(TileIndex tile1, TileIndex tile2)
        {
            if ((tile1 == tile2) || !(AIMap.IsValidTile(tile1) && AIMap.IsValidTile(tile2)))
            {
                return(Direction.None);
            }

            int x1 = AIMap.GetTileX(tile1);
            int x2 = AIMap.GetTileX(tile2);
            int y1 = AIMap.GetTileY(tile1);
            int y2 = AIMap.GetTileY(tile2);

            if (y1 == y2)
            {
                // /
                return(x1 < x2 ? Direction.SouthWest : Direction.NorthEast);
            }
            else if (x1 == x2)
            {
                // \
                return(y1 < y2 ? Direction.SouthEast : Direction.NorthWest);
            }

            return(Direction.None);
        }
Exemple #2
0
        private static bool IsClear(TileIndex topCorner, int width, int height)
        {
            if (!AITile.IsBuildableRectangle(topCorner, width, height))
            {
                return(false);
            }

            var xx = AIMap.GetTileX(topCorner);
            var yy = AIMap.GetTileY(topCorner);

            for (var x = 0; x < width; x++)
            {
                for (var y = 0; y < height; y++)
                {
                    var testTile = topCorner + AIMap.GetTileIndex(x, y);
                    if (!AIMap.IsValidTile(testTile))
                    {
                        return(false);
                    }

                    var slope = AITile.GetSlope(testTile);
                    if (slope != AITile.SLOPE_FLAT)
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Exemple #3
0
        internal static List <PathInfo> GetNeighbors(TileIndex tile, PathInfo cameFrom, Action <TileIndex, string> sign = null)
        {
            var oldcost = cameFrom.Cost;
            var result  = new List <PathInfo>();
            var isFlat  = AITile.GetSlope(tile) == AITile.SLOPE_FLAT;
            var dirs    = new int[][] { new int[] { 0, 1 }, new int[] { 0, -1 }, new int[] { 1, 0 }, new int[] { -1, 0 } };
            var oldDir  = (cameFrom.Previous != null) ? Helper.GetDirection(cameFrom.Previous.Tile, tile) : Direction.None;

            foreach (var dir in dirs)
            {
                var x = dir[0];
                var y = dir[1];
                if (cameFrom.Tile == tile + AIMap.GetTileIndex(x, y))
                {
                    continue;
                }

                var newDir = Helper.GetDirection(tile, tile + AIMap.GetTileIndex(x, y));

                var isCoast   = AITile.IsCoastTile(tile);
                var straight  = newDir == oldDir;
                var maxLength = 20;

                bool lastWasExistingBridge = false;

                TileIndex neighbor;
                for (var length = 1; AIMap.IsValidTile(neighbor = tile + AIMap.GetTileIndex(x * length, y * length)) && length <= maxLength; length++)
                {
                    if (AIBridge.IsBridgeTile(neighbor))
                    {
                        var otherEnd  = AIBridge.GetOtherBridgeEnd(neighbor);
                        var bridgeDir = Helper.GetDirection(neighbor, otherEnd);

                        if (newDir == bridgeDir)
                        {
                            length += AIMap.DistanceManhattan(neighbor, otherEnd);
                            lastWasExistingBridge = true;
                            continue;
                        }
                    }
                    else if (AIRoad.IsRoadTile(neighbor) && (lastWasExistingBridge || (length == 1)))
                    {
                        result.Add(new PathInfo(
                                       neighbor,
                                       length,
                                       oldcost + (length * 0.5),
                                       length > 1 ? BuildType.Bridge : BuildType.Basic,
                                       cameFrom
                                       ));
                        break;
                    }

                    lastWasExistingBridge = false;
                    if (isCoast)
                    {
                        if (!straight)
                        {
                            break;
                        }

                        if (AITile.IsWaterTile(neighbor))
                        {
                            continue;
                        }
                    }

                    double multiplier = 1;
                    if (AITile.IsFarmTile(neighbor) || AITile.IsRockTile(neighbor) || AITile.IsRoughTile(tile))
                    {
                        // Make farms, rocks, etc more expensive.
                        multiplier *= 1.1;
                    }

                    if (AITile.IsBuildable(neighbor))
                    {
                        if (isCoast)
                        {
                            result.Add(new PathInfo(
                                           neighbor,
                                           length,
                                           oldcost + ((length * multiplier * 2)),
                                           BuildType.Bridge,
                                           cameFrom
                                           ));

                            break;
                        }
                        else if ((isFlat && cameFrom.Length == 1) || ((length == 1) && straight))
                        {
                            result.Add(new PathInfo(
                                           neighbor,
                                           length,
                                           oldcost + ((length * multiplier)),
                                           length == 1 ? BuildType.Basic : BuildType.Bridge,
                                           cameFrom
                                           ));

                            break;
                        }
                    }
                    else if (!(
                                 (AIRoad.IsRoadTile(neighbor) || AIRoad.IsRoadTile(neighbor) || AITile.IsWaterTile(neighbor)) &&
                                 (AITile.GetSlope(neighbor) == AITile.SLOPE_FLAT) &&
                                 !AIStation.IsValidStation(AIStation.GetStationID(neighbor))))
                    {
                        // Can't built over
                        break;
                    }
                }
            }

            return(result);
        }
Exemple #4
0
        internal static List <PathInfo> GetNeighbors(TileIndex tile, PathInfo cameFrom, Action <TileIndex, string> sign = null)
        {
            var oldcost = cameFrom.Cost;
            var result  = new List <PathInfo>();
            var isFlat  = AITile.GetSlope(tile) == AITile.SLOPE_FLAT;
            var dirs    = new int[][] { new int[] { 0, 1 }, new int[] { 0, -1 }, new int[] { 1, 0 }, new int[] { -1, 0 } };
            var oldDir  = Helper.GetDirection(tile, cameFrom.Previous.Tile);

            foreach (var dir in dirs)
            {
                var x = dir[0];
                var y = dir[1];
                if (cameFrom.Tile == tile + AIMap.GetTileIndex(x, y))
                {
                    continue;
                }

                var newDir = Helper.GetDirection(tile + AIMap.GetTileIndex(x, y), tile);

                var isCoast   = AITile.IsCoastTile(tile);
                var straight  = newDir == oldDir;
                var maxLength = isCoast ? 20 : (straight ? 5 : 1);

                TileIndex neighbor;
                for (var length = 1; AIMap.IsValidTile(neighbor = tile + AIMap.GetTileIndex(x * length, y * length)) && length <= maxLength; length++)
                {
                    if (isCoast)
                    {
                        if (!straight)
                        {
                            break;
                        }

                        if (AITile.IsWaterTile(neighbor))
                        {
                            continue;
                        }
                    }

                    double multiplier = 1;
                    if (AITile.IsFarmTile(neighbor) || AITile.IsRockTile(neighbor) || AITile.IsRoughTile(tile))
                    {
                        // Make farms, rocks, etc more expensive.
                        multiplier *= 1.1;
                    }

                    if (AITile.IsBuildable(neighbor))
                    {
                        double angleFactor = RailBuilder.CalculateAngle(neighbor, tile, cameFrom.Previous);
                        if (isCoast)
                        {
                            result.Add(new PathInfo(
                                           neighbor,
                                           length,
                                           oldcost + ((length * multiplier * 2) + angleFactor),
                                           BuildType.Bridge,
                                           cameFrom
                                           ));

                            break;
                        }
                        else if ((isFlat && cameFrom.Length == 1) || ((length == 1) && straight))
                        {
                            result.Add(new PathInfo(
                                           neighbor,
                                           length,
                                           oldcost + ((length * multiplier) + angleFactor),
                                           length == 1 ? BuildType.Basic : BuildType.Bridge,
                                           cameFrom
                                           ));

                            break;
                        }
                    }
                    else if (!(
                                 (AIRail.IsRailTile(neighbor) || AIRoad.IsRoadTile(neighbor) || AITile.IsWaterTile(neighbor)) &&
                                 (AITile.GetSlope(neighbor) == AITile.SLOPE_FLAT) &&
                                 !AIStation.IsValidStation(AIStation.GetStationID(neighbor))))
                    {
                        // Can't built over
                        break;
                    }
                }
            }

            return(result);
        }