示例#1
0
        private static RailStationInfo FindPlaceForStation(TileIndex tile, int platformLength, int platformCount)
        {
            var xx = AIMap.GetTileX(tile);
            var yy = AIMap.GetTileY(tile);

            for (var step = 1; step < 15; step++)
            {
                for (var i = 0; i < step * 2; i++)
                {
                    var x     = xx - step;
                    var y     = yy + Helper.Alternate(i);
                    var tile2 = AIMap.GetTileIndex(x, y);
                    var test  = RailStationBuilder.TestPlaceForStation(tile2, platformLength, platformCount);
                    if (test != null)
                    {
                        return(test);
                    }

                    x     = xx - Helper.Alternate(i);
                    y     = yy + step;
                    tile2 = AIMap.GetTileIndex(x, y);
                    test  = RailStationBuilder.TestPlaceForStation(tile2, platformLength, platformCount);
                    if (test != null)
                    {
                        return(test);
                    }

                    x     = xx + step;
                    y     = yy + Helper.Alternate(i);
                    tile2 = AIMap.GetTileIndex(x, y);
                    test  = RailStationBuilder.TestPlaceForStation(tile2, platformLength, platformCount);
                    if (test != null)
                    {
                        return(test);
                    }

                    x     = xx + Helper.Alternate(i);
                    y     = yy - step;
                    tile2 = AIMap.GetTileIndex(x, y);
                    test  = RailStationBuilder.TestPlaceForStation(tile2, platformLength, platformCount);
                    if (test != null)
                    {
                        return(test);
                    }
                }
            }

            return(null);
        }
示例#2
0
        private static RailStationInfo TestPlaceForStation(TileIndex tile, int platformLength, int platformCount)
        {
            var buffer      = 4;
            var clearLength = platformLength;
            var clearWidth  = platformCount;

            var tile2 = tile - AIMap.GetTileIndex(clearWidth / 2, clearLength / 2);

            if (RailStationBuilder.IsClear(tile2, clearWidth, clearLength + buffer))
            {
                return(new RailStationInfo()
                {
                    tile = tile - AIMap.GetTileIndex(clearWidth / 2, clearLength / 2), direction = AIRail.RAILTRACK_NW_SE, entryNearTopCorner = false
                });
            }

            tile2 = tile - AIMap.GetTileIndex(clearLength / 2, clearWidth / 2);
            if (RailStationBuilder.IsClear(tile2, clearLength + buffer, clearWidth))
            {
                return(new RailStationInfo()
                {
                    tile = tile - AIMap.GetTileIndex(clearLength / 2, clearWidth / 2), direction = AIRail.RAILTRACK_NE_SW, entryNearTopCorner = false
                });
            }

            tile2 = tile - AIMap.GetTileIndex(clearWidth / 2, (clearLength / 2) + buffer);
            if (RailStationBuilder.IsClear(tile2, clearWidth, clearLength + buffer))
            {
                return(new RailStationInfo()
                {
                    tile = tile - AIMap.GetTileIndex(clearWidth / 2, clearLength / 2), direction = AIRail.RAILTRACK_NW_SE, entryNearTopCorner = true
                });
            }

            tile2 = tile - AIMap.GetTileIndex((clearLength / 2) + buffer, clearWidth / 2);
            if (RailStationBuilder.IsClear(tile2, clearLength + buffer, clearWidth))
            {
                return(new RailStationInfo()
                {
                    tile = tile - AIMap.GetTileIndex(clearLength / 2, clearWidth / 2), direction = AIRail.RAILTRACK_NE_SW, entryNearTopCorner = true
                });
            }

            return(null);
        }
示例#3
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);
        }
示例#4
0
        protected /*override*/ void Start2()
        {
            SignManager signManager = new SignManager();

            this.signs = new AISignList();
            var list = new AIRailTypeList();

            AIRail.SetCurrentRailType(list.Begin());

            AICompany.SetLoanAmount(AICompany.GetMaxLoanAmount());

            ////*
            //RailBuilder.BuildRail(
            //    AIMap.GetTileIndex(42, 121),
            //    AIMap.GetTileIndex(41, 121),
            //    AIMap.GetTileIndex(31, 121),
            //    AIMap.GetTileIndex(30, 121),
            //    new HashSet<TileIndex>(),
            //    1,
            //    this.BuildSign);
            ///*/

            //var i = 39;
            //var ns = RailBuilder.GetNeighbors(
            //    AIMap.GetTileIndex(i, 121),
            //    new RailBuilder.PathInfo(
            //        AIMap.GetTileIndex(i + 1, 121),
            //        1,
            //        1,
            //        RailBuilder.BuildType.Rail,
            //        new RailBuilder.PathInfo(
            //            AIMap.GetTileIndex(i + 2, 121),
            //            1,
            //            1,
            //            RailBuilder.BuildType.Rail,
            //            null)),
            //    this.BuildSign);

            //foreach (var n in ns)
            //{
            //    this.BuildSign(n.Tile, n.Cost.ToString());
            //}
            ///* */

            ///*
            try
            {
                if (this.workStatus == 0)
                {
                    var towns = new AITownList();
                    towns.Valuate(AITown.GetPopulation);
                    RailStationBuildResult s1 = null;
                    foreach (var(town, _) in towns)
                    {
                        if (s1 == null)
                        {
                            s1 = RailStationBuilder.BuildStationNear(AITown.GetLocation(town), this.platformSize, 2);
                        }
                        else
                        {
                            RailStationBuildResult s2        = RailStationBuilder.BuildStationNear(AITown.GetLocation(town), this.platformSize, 2);
                            HashSet <TileIndex>    forbidden = new HashSet <TileIndex>();
                            forbidden.Add(s1.ExitFarther);
                            forbidden.Add(s2.EntryFarther);
                            var good = RailBuilder.BuildRail(s1.EntryCloser, s1.EntryFarther, s2.ExitFarther, s2.ExitCloser, forbidden);
                            signManager.ClearSigns();
                            if (good)
                            {
                                forbidden = new HashSet <TileIndex>();
                                good      = RailBuilder.BuildRail(s1.ExitCloser, s1.ExitFarther, s2.EntryFarther, s2.EntryCloser, forbidden, 1);
                            }

                            if (good)
                            {
                                if (s1.DepotTile != AIMap.TILE_INVALID)
                                {
                                    TrainManager.BuildTrain(s1.DepotTile, new StationID[] { s1.StationID, s2.StationID }, this.BuildSign);
                                }
                                else
                                {
                                    TrainManager.BuildTrain(s2.DepotTile, new StationID[] { s2.StationID, s1.StationID }, this.BuildSign);
                                }
                            }
                            else
                            {
                                AILog.Info("No route found.");
                            }

                            s1 = null;

                            //if (AICompany.GetBankBalance(AICompany.COMPANY_SELF) < 50000)
                            {
                                // Better to stop before bankrupcy.
                                AILog.Info("Better to stop before bankrupcy.");
                                break;
                            }
                        }
                    }
                }

                this.workStatus = 1;
            }
            catch
            {
                AILog.Error("An error happened");
            }
            /* */

            signManager.ClearSigns();
            AILog.Info("Sign count: " + this.signs.Count());
            AIController.Sleep(150);

            AILog.Info("Remove all signs");
            foreach (var(signId, _) in this.signs)
            {
                AISign.RemoveSign(signId);
            }

            while (true)
            {
                // Main loop
                //this.setMinLoan();
                CsTestAi.SetMinimumLoanAmount();
                Sleep(100);
            }
        }