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); }
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); }
private static void BuildIntersection(TileIndex tile1, TileIndex tile2, TileIndex tile3, TileIndex tile4 = null) { var tile1x = AIMap.GetTileX(tile1); var tile1y = AIMap.GetTileY(tile1); var tile2x = AIMap.GetTileX(tile2); var tile2y = AIMap.GetTileY(tile2); var tile3x = AIMap.GetTileX(tile3); var tile3y = AIMap.GetTileY(tile3); var x = (int)((tile1x + tile2x + tile3x) / 3.0 + 0.5); var y = (int)((tile1y + tile2y + tile3y) / 3.0 + 0.5); var center = AIMap.GetTileIndex(x, y); var nwTile = AIMap.GetTileIndex(x, y - 1); var neTile = AIMap.GetTileIndex(x - 1, y); var swTile = AIMap.GetTileIndex(x + 1, y); var seTile = AIMap.GetTileIndex(x, y + 1); if (tile4 != null) { AIRail.BuildRailTrack(center, AIRail.RAILTRACK_NE_SW); // / AIRail.BuildRailTrack(center, AIRail.RAILTRACK_NW_SE); // \ AIRail.BuildRailTrack(center, AIRail.RAILTRACK_NW_NE); // - (upper) AIRail.BuildRailTrack(center, AIRail.RAILTRACK_SW_SE); // - (lower) AIRail.BuildRailTrack(center, AIRail.RAILTRACK_NW_SW); // | left AIRail.BuildRailTrack(center, AIRail.RAILTRACK_NE_SE); // | right return; } if ((tile1 != nwTile) && (tile2 != nwTile) && (tile3 != nwTile)) { AIRail.BuildRailTrack(center, AIRail.RAILTRACK_NE_SW); // / AIRail.BuildRailTrack(center, AIRail.RAILTRACK_SW_SE); // - (lower) AIRail.BuildRailTrack(center, AIRail.RAILTRACK_NE_SE); // | right return; } if ((tile1 != neTile) && (tile2 != neTile) && (tile3 != neTile)) { AIRail.BuildRailTrack(center, AIRail.RAILTRACK_NW_SE); // \ AIRail.BuildRailTrack(center, AIRail.RAILTRACK_SW_SE); // - (lower) AIRail.BuildRailTrack(center, AIRail.RAILTRACK_NW_SW); // | left return; } if ((tile1 != swTile) && (tile2 != swTile) && (tile3 != swTile)) { AIRail.BuildRailTrack(center, AIRail.RAILTRACK_NW_SE); // \ AIRail.BuildRailTrack(center, AIRail.RAILTRACK_NW_NE); // - (upper) AIRail.BuildRailTrack(center, AIRail.RAILTRACK_NE_SE); // | right return; } if ((tile1 != seTile) && (tile2 != seTile) && (tile3 != seTile)) { AIRail.BuildRailTrack(center, AIRail.RAILTRACK_NE_SW); // / AIRail.BuildRailTrack(center, AIRail.RAILTRACK_NW_NE); // - (upper) AIRail.BuildRailTrack(center, AIRail.RAILTRACK_NW_SW); // | left return; } }
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); } }
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); }