Exemple #1
0
        internal static bool BuildRail(TileIndex previous, TileIndex from, TileIndex to, TileIndex next, HashSet <TileIndex> forbidden, int railCount = 1, Action <TileIndex, string> sign = null)
        {
            var path = RailBuilder.FindPath(from, to, previous, forbidden, sign);

            if (path == null)
            {
                return(false);
            }

            for (var i = 0; i < path.Count - 1; i++)
            {
                var p  = i == 0 ? next : path[i - 1].Tile;
                var c  = path[i].Tile;
                var n  = i == path.Count - 1 ? previous : path[i + 1].Tile;
                var nt = i == 0 ? BuildType.Basic : path[i - 1].Type;
                //sign(c, "[" + AIMap.GetTileX(c) + ", " + AIMap.GetTileY(c) + "] " + path[i].type);
                bool good = false;
                switch (path[i].Type)
                {
                case BuildType.Basic:
                    if (path[i].Length > 1)
                    {
                        throw new Exception("Should not be rail");
                    }

                    if (nt != BuildType.Basic)
                    {
                        continue;
                    }

                    //AILog.Info("Build a rail  from [" + AIMap.GetTileX(p) + ", " + AIMap.GetTileY(p) + "] via [" + AIMap.GetTileX(c) + ", " + AIMap.GetTileY(c) + "] to [" + AIMap.GetTileX(n) + ", " + AIMap.GetTileY(n) + "].");
                    good = AIRail.BuildRail(p, c, n);
                    break;

                case BuildType.Bridge:
                    var bridgeTypes = new AIBridgeList_Length(path[i].Length);
                    //AILog.Info("Build a bridge " + bridgeTypes.Begin() + " from [" + AIMap.GetTileX(c) + ", " + AIMap.GetTileY(c) + "] to [" + AIMap.GetTileX(n) + ", " + AIMap.GetTileY(n) + "].");
                    good = AIBridge.BuildBridge(AIVehicle.VT_RAIL, bridgeTypes.Begin(), c, n);
                    if (!good)
                    {
                        sign(p, "s");
                        sign(c, "e");
                    }

                    break;

                case BuildType.Tunnel:
                    throw new Exception("Tunnels not supported");
                }

                if (!good)
                {
                    AILog.Error("Failed to build on [" + AIMap.GetTileX(c) + ", " + AIMap.GetTileY(c) + "]. Reason: " + AIError.GetLastErrorString());
                }
            }

            return(true);
        }
Exemple #2
0
        public static RailStationBuildResult BuildStationNear(TileIndex tile, int platformLength, int platformCount = 2)
        {
            var stationTile = RailStationBuilder.FindPlaceForStation(tile, platformLength, platformCount);

            if (stationTile != null)
            {
                AILog.Info("Build " + stationTile.tile + ", " + stationTile.direction);
                var good = AIRail.BuildRailStation(stationTile.tile, stationTile.direction, platformCount, platformLength, AIStation.STATION_NEW);
                if (good)
                {
                    TileIndex entryTile;
                    int[]     matrix;
                    if (stationTile.direction == AIRail.RAILTRACK_NW_SE)
                    {
                        if (stationTile.entryNearTopCorner)
                        {
                            /*   \
                             \/\
                             \/   */
                            entryTile = stationTile.tile + AIMap.GetTileIndex(0, 0);
                            matrix    = new int[] { -1, 0, 0, -1 };
                        }
                        else
                        {
                            /*  /\
                            \/\
                            \   */
                            entryTile = stationTile.tile + AIMap.GetTileIndex(1, platformLength - 1);
                            matrix    = new int[] { 1, 0, 0, 1 };
                        }
                    }
                    else if (stationTile.direction == AIRail.RAILTRACK_NE_SW)
                    {
                        if (stationTile.entryNearTopCorner)
                        {
                            /*   /
                             *  /\/
                             \/   */
                            entryTile = stationTile.tile + AIMap.GetTileIndex(0, 1);
                            matrix    = new int[] { 0, -1, 1, 0 };
                        }
                        else
                        {
                            /*   /\
                            *   /\/
                            *    /    */
                            entryTile = stationTile.tile + AIMap.GetTileIndex(platformLength - 1, 0);
                            matrix    = new int[] { 0, 1, -1, 0 };
                        }
                    }
                    else
                    {
                        return(null);
                    }

                    /* |      |
                     +00-01-+
                     *  02 03
                     * 04 05 06 07
                     *  08 09
                     *  10 11
                     *
                     */

                    var tiles = new TileIndex[] {
                        entryTile + RailStationBuilder.GetTransformedTileIndex(0, 0, matrix),
                        entryTile + RailStationBuilder.GetTransformedTileIndex(-1, 0, matrix),

                        entryTile + RailStationBuilder.GetTransformedTileIndex(0, 1, matrix),
                        entryTile + RailStationBuilder.GetTransformedTileIndex(-1, 1, matrix),

                        entryTile + RailStationBuilder.GetTransformedTileIndex(1, 2, matrix),
                        entryTile + RailStationBuilder.GetTransformedTileIndex(0, 2, matrix),
                        entryTile + RailStationBuilder.GetTransformedTileIndex(-1, 2, matrix),
                        entryTile + RailStationBuilder.GetTransformedTileIndex(-2, 2, matrix),

                        entryTile + RailStationBuilder.GetTransformedTileIndex(0, 3, matrix),
                        entryTile + RailStationBuilder.GetTransformedTileIndex(-1, 3, matrix),

                        entryTile + RailStationBuilder.GetTransformedTileIndex(0, 4, matrix),
                        entryTile + RailStationBuilder.GetTransformedTileIndex(-1, 4, matrix),
                    };

                    // Exit rail
                    AIRail.BuildRail(
                        tiles[0],
                        tiles[2],
                        tiles[10]);

                    // Entry rail
                    AIRail.BuildRail(
                        tiles[1],
                        tiles[3],
                        tiles[11]);

                    // Signals
                    AIRail.BuildSignal(tiles[2], tiles[0], AIRail.SIGNALTYPE_PBS);
                    AIRail.BuildSignal(tiles[3], tiles[1], AIRail.SIGNALTYPE_PBS);
                    AIRail.BuildSignal(tiles[9], tiles[11], AIRail.SIGNALTYPE_PBS_ONEWAY);

                    //Depot
                    TileIndex depotTile = AIMap.TILE_INVALID;
                    if (AIRail.BuildRailDepot(tiles[4], tiles[5]))
                    {
                        depotTile = tiles[4];
                        RailStationBuilder.BuildIntersection(tiles[2], tiles[4], tiles[6], tiles[8]);
                        RailStationBuilder.BuildIntersection(tiles[3], tiles[5], tiles[9]);
                    }
                    else
                    {
                        if (AIRail.BuildRailDepot(tiles[7], tiles[6]))
                        {
                            depotTile = tiles[7];
                            RailStationBuilder.BuildIntersection(tiles[3], tiles[5], tiles[7], tiles[9]);
                            RailStationBuilder.BuildIntersection(tiles[2], tiles[6], tiles[8]);
                        }
                    }

                    return(new RailStationBuildResult()
                    {
                        StationID = AIStation.GetStationID(stationTile.tile),
                        ExitCloser = tiles[8],
                        ExitFarther = tiles[10],
                        EntryCloser = tiles[9],
                        EntryFarther = tiles[11],
                        DepotTile = depotTile
                    });
                }
            }

            return(null);
        }