1308 if (GetTownRoadBits(tile) != ROAD_NONE) { |
1308 if (GetTownRoadBits(tile) != ROAD_NONE) { |
1309 int r = GrowTownAtRoad(t, tile); |
1309 int r = GrowTownAtRoad(t, tile); |
1310 _current_company = old_company; |
1310 _current_company = old_company; |
1311 return r != 0; |
1311 return r != 0; |
1312 } |
1312 } |
1313 tile = TILE_ADD(tile, ToTileIndexDiff(*ptr)); |
1313 |
|
1314 /* Next (valid) tile... */ |
|
1315 TileIndexDiff dt = ToTileIndexDiff(*ptr); |
|
1316 |
|
1317 if (IsValidTile(tile + dt)) |
|
1318 tile = TILE_ADD(tile, ToTileIndexDiff(*ptr)); |
1314 } |
1319 } |
1315 |
1320 |
1316 /* No road available, try to build a random road block by |
1321 /* No road available, try to build a random road block by |
1317 * clearing some land and then building a road there. */ |
1322 * clearing some land and then building a road there. */ |
1318 tile = t->xy; |
1323 tile = t->xy; |
1539 /* Only in the scenario editor */ |
1544 /* Only in the scenario editor */ |
1540 if (_game_mode != GM_EDITOR) return CMD_ERROR; |
1545 if (_game_mode != GM_EDITOR) return CMD_ERROR; |
1541 if (p2 > TSM_CITY) return CMD_ERROR; |
1546 if (p2 > TSM_CITY) return CMD_ERROR; |
1542 |
1547 |
1543 /* Check if too close to the edge of map */ |
1548 /* Check if too close to the edge of map */ |
1544 if (DistanceFromEdge(tile) < 12) |
1549 if (DistanceFromEdge(tile) < 1) |
1545 return_cmd_error(STR_0237_TOO_CLOSE_TO_EDGE_OF_MAP); |
1550 return_cmd_error(STR_0237_TOO_CLOSE_TO_EDGE_OF_MAP); |
1546 |
1551 |
1547 /* Can only build on clear flat areas, possibly with trees. */ |
1552 /* Can only build on clear flat areas, possibly with trees. */ |
1548 if ((!IsTileType(tile, MP_CLEAR) && !IsTileType(tile, MP_TREES)) || GetTileSlope(tile, NULL) != SLOPE_FLAT) { |
1553 if ((!IsTileType(tile, MP_CLEAR) && !IsTileType(tile, MP_TREES)) || GetTileSlope(tile, NULL) != SLOPE_FLAT) { |
1549 return_cmd_error(STR_0239_SITE_UNSUITABLE); |
1554 return_cmd_error(STR_0239_SITE_UNSUITABLE); |
1577 Town *CreateRandomTown(uint attempts, TownSizeMode mode, uint size) |
1582 Town *CreateRandomTown(uint attempts, TownSizeMode mode, uint size) |
1578 { |
1583 { |
1579 do { |
1584 do { |
1580 /* Generate a tile index not too close from the edge */ |
1585 /* Generate a tile index not too close from the edge */ |
1581 TileIndex tile = RandomTile(); |
1586 TileIndex tile = RandomTile(); |
1582 if (DistanceFromEdge(tile) < 20) continue; |
1587 if (DistanceFromEdge(tile) < 1) continue; |
1583 |
1588 |
1584 /* Make sure the tile is plain */ |
1589 /* Make sure the tile is plain */ |
1585 if (!IsTileType(tile, MP_CLEAR) || GetTileSlope(tile, NULL) != SLOPE_FLAT) continue; |
1590 if (!IsTileType(tile, MP_CLEAR) || GetTileSlope(tile, NULL) != SLOPE_FLAT) continue; |
1586 |
1591 |
1587 /* Check not too close to a town */ |
1592 /* Check not too close to a town */ |
1606 static const byte _num_initial_towns[4] = {5, 11, 23, 46}; // very low, low, normal, high |
1611 static const byte _num_initial_towns[4] = {5, 11, 23, 46}; // very low, low, normal, high |
1607 |
1612 |
1608 bool GenerateTowns() |
1613 bool GenerateTowns() |
1609 { |
1614 { |
1610 uint num = 0; |
1615 uint num = 0; |
1611 uint n = ScaleByMapSize(_num_initial_towns[_settings_game.difficulty.number_towns] + (Random() & 7)); |
1616 uint n = ScaleByMapSize(_num_initial_towns[_settings_game.difficulty.number_towns]) + (Random() & 7); |
1612 uint num_cities = _settings_game.economy.larger_towns == 0 ? 0 : n / _settings_game.economy.larger_towns; |
1617 uint num_cities = _settings_game.economy.larger_towns == 0 ? 0 : n / _settings_game.economy.larger_towns; |
1613 |
1618 |
1614 SetGeneratingWorldProgress(GWP_TOWN, n); |
1619 SetGeneratingWorldProgress(GWP_TOWN, n); |
1615 |
1620 |
1616 do { |
1621 do { |
1617 IncreaseGeneratingWorldProgress(GWP_TOWN); |
1622 IncreaseGeneratingWorldProgress(GWP_TOWN); |
1618 /* try 20 times to create a random-sized town for the first loop. */ |
1623 /* try 20 times to create a random-sized town for the first loop. */ |
1619 TownSizeMode mode = num_cities > 0 ? TSM_CITY : TSM_RANDOM; |
1624 TownSizeMode mode = num_cities > 0 ? TSM_CITY : TSM_RANDOM; |
1620 if (CreateRandomTown(20, mode, _settings_game.economy.initial_city_size) != NULL) num++; |
1625 if (CreateRandomTown(20, mode, _settings_game.economy.initial_city_size) != NULL) num++; |
1621 if (num_cities > 0) num_cities--; |
1626 if (num_cities > 0) num_cities--; |
1622 } while (--n); |
1627 } while (n && --n); |
1623 |
1628 |
1624 /* give it a last try, but now more aggressive */ |
1629 /* give it a last try, but now more aggressive */ |
1625 if (num == 0 && CreateRandomTown(10000, TSM_RANDOM, 0) == NULL) { |
1630 if (num == 0 && CreateRandomTown(10000, TSM_RANDOM, 0) == NULL) { |
1626 if (GetNumTowns() == 0) { |
1631 if (GetNumTowns() == 0) { |
1627 /* XXX - can we handle that more gracefully? */ |
1632 /* XXX - can we handle that more gracefully? */ |
1868 if (TownLayoutAllows2x2HouseHere(t, tile2) && CheckFree2x2Area(tile2, t->index, maxz, noslope)) { |
1873 if (TownLayoutAllows2x2HouseHere(t, tile2) && CheckFree2x2Area(tile2, t->index, maxz, noslope)) { |
1869 *tile = tile2; |
1874 *tile = tile2; |
1870 return true; |
1875 return true; |
1871 } |
1876 } |
1872 if (d == DIAGDIR_END) break; |
1877 if (d == DIAGDIR_END) break; |
1873 tile2 += TileOffsByDiagDir(ReverseDiagDir(d)); // go clockwise |
1878 |
|
1879 // go clockwise |
|
1880 TileIndexDiff dt = TileOffsByDiagDir(ReverseDiagDir(d)); |
|
1881 |
|
1882 if (IsValidTile(tile2 + dt)) |
|
1883 tile2 += dt; |
1874 } |
1884 } |
1875 |
1885 |
1876 return false; |
1886 return false; |
1877 } |
1887 } |
1878 |
1888 |