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); }
public void Begin(GameMaster master) { this.master = master; for (int X = x - 1; X <= x + 1 && X < master.x; X++) { if (X < 0) { continue; } for (int Y = y - 1; Y <= y + 1 && Y < master.y; Y++) { if (Y < 0) { continue; } if (X == x && Y == y) { continue; } AITile tile = master.aiTiles[X, Y]; neighbours.Add(tile); } } }
public void Initialize(AITile tile) { if (initialized) { Debug.Log("Already init"); return; } source.clip = initializeSound; source.Play(); initialized = true; List <Vector2> possiblePoints = new List <Vector2>(); foreach (AITile t in aiTiles) { bool canUse = t != tile; if (canUse) { foreach (AITile t1 in tile.neighbours) { if (t1 == t) { canUse = false; break; } } } if (canUse) { possiblePoints.Add(new Vector2(t.x, t.y)); } } for (int i = 0; i < bombs; i++) { int index = Random.Range(0, possiblePoints.Count); Vector2 point = possiblePoints[index]; possiblePoints.RemoveAt(index); aiTiles[(int)point.x, (int)point.y].hasBomb = true; } foreach (AITile t in aiTiles) { t.Apply(); } }
void GenerateTiles() { int mode = PlayerPrefs.GetInt("Mode"); for (int Y = 0; Y < y; Y++) { for (int X = 0; X < x; X++) { if (mode == 4) { AITile aiTile = Instantiate(aiTilePrefab, transform.position, Quaternion.identity, transform).GetComponent <AITile>(); aiTile.x = X; aiTile.y = Y; aiTiles[X, Y] = aiTile; continue; } GameTile tile = Instantiate(tilePrefab, transform.position, Quaternion.identity, transform).GetComponent <GameTile>(); tile.x = X; tile.y = Y; tiles[X, Y] = tile; } } if (mode == 4) { foreach (AITile tile in aiTiles) { tile.Begin(this); } } else { foreach (GameTile tile in tiles) { tile.Begin(this); } } if (mode == 4) { StartCoroutine(StartAI()); } }
public List <AITile> GetMutualNeighbors(AITile tile) { List <AITile> mutuals = new List <AITile>(); foreach (AITile t1 in tile.neighbours) { foreach (AITile t2 in neighbours) { if (t1 == t2) { mutuals.Add(t1); break; } } } return(mutuals); }
private static RoadStationInfo TestPlaceForStation(TileIndex tile) { if (AITile.IsBuildable(tile) && (AITile.GetSlope(tile) == AITile.SLOPE_FLAT)) { var other = tile + AIMap.GetTileIndex(0, 1); if (AIRoad.IsRoadTile(other) && (AITile.GetSlope(other) == AITile.SLOPE_FLAT)) { return(new RoadStationInfo() { tile = tile, entryPoint = other }); } other = tile + AIMap.GetTileIndex(1, 0); if (AIRoad.IsRoadTile(other) && (AITile.GetSlope(other) == AITile.SLOPE_FLAT)) { return(new RoadStationInfo() { tile = tile, entryPoint = other }); } other = tile + AIMap.GetTileIndex(0, -1); if (AIRoad.IsRoadTile(other) && (AITile.GetSlope(other) == AITile.SLOPE_FLAT)) { return(new RoadStationInfo() { tile = tile, entryPoint = other }); } other = tile + AIMap.GetTileIndex(-1, 0); if (AIRoad.IsRoadTile(other) && (AITile.GetSlope(other) == AITile.SLOPE_FLAT)) { return(new RoadStationInfo() { tile = tile, entryPoint = other }); } } return(null); }
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); }
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); }