tron@2186: /* $Id$ */ tron@2186: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@2163: #include "functions.h" tron@1309: #include "strings.h" tron@3144: #include "road_map.h" tron@507: #include "table/strings.h" celestar@2148: #include "table/sprites.h" tron@679: #include "map.h" tron@1209: #include "tile.h" tron@3319: #include "town_map.h" tron@3154: #include "tunnel_map.h" truelight@0: #include "viewport.h" truelight@0: #include "town.h" truelight@0: #include "command.h" truelight@0: #include "gfx.h" truelight@0: #include "industry.h" truelight@0: #include "station.h" truelight@0: #include "player.h" truelight@0: #include "news.h" truelight@0: #include "saveload.h" truelight@0: #include "economy.h" truelight@0: #include "gui.h" tron@3310: #include "unmovable_map.h" celestar@3433: #include "water_map.h" tron@2159: #include "variables.h" celestar@3359: #include "bridge.h" celestar@5573: #include "bridge_map.h" rubidium@4261: #include "date.h" belugas@3654: #include "table/town_land.h" truelight@4300: #include "genworld.h" truelight@0: truelight@1260: /** truelight@1260: * Called if a new block is added to the town-pool truelight@1260: */ truelight@1260: static void TownPoolNewBlock(uint start_item) truelight@1260: { truelight@1260: Town *t; truelight@1260: truelight@4346: /* We don't use FOR_ALL here, because FOR_ALL skips invalid items. truelight@4346: * TODO - This is just a temporary stage, this will be removed. */ tron@4983: for (t = GetTown(start_item); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) t->index = start_item++; truelight@1260: } truelight@1260: truelight@1260: /* Initialize the town-pool */ matthijs@5216: DEFINE_OLD_POOL(Town, Town, TownPoolNewBlock, NULL) truelight@1260: truelight@4396: void DestroyTown(Town *t) truelight@4396: { truelight@4396: Industry *i; truelight@4396: TileIndex tile; truelight@4396: truelight@4396: /* Delete town authority window truelight@4396: * and remove from list of sorted towns */ truelight@4396: DeleteWindowById(WC_TOWN_VIEW, t->index); truelight@4396: _town_sort_dirty = true; rubidium@5298: _total_towns--; truelight@4396: truelight@4396: /* Delete all industries belonging to the town */ truelight@4396: FOR_ALL_INDUSTRIES(i) if (i->town == t) DeleteIndustry(i); truelight@4396: truelight@4396: /* Go through all tiles and delete those belonging to the town */ truelight@4396: for (tile = 0; tile < MapSize(); ++tile) { truelight@4396: switch (GetTileType(tile)) { truelight@4396: case MP_HOUSE: truelight@4396: if (GetTownByTile(tile) == t) DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@4396: break; truelight@4396: truelight@4396: case MP_STREET: celestar@5590: case MP_TUNNEL: celestar@5590: case MP_STREET_BRIDGE: truelight@4396: if (IsTileOwner(tile, OWNER_TOWN) && truelight@4396: ClosestTownFromTile(tile, (uint)-1) == t) truelight@4396: DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@4396: break; truelight@4396: truelight@4396: default: truelight@4396: break; truelight@4396: } truelight@4396: } truelight@4396: truelight@4396: DeleteName(t->townnametype); rubidium@5566: DeleteSubsidyWithTown(t->index); truelight@4396: truelight@4396: MarkWholeScreenDirty(); truelight@4396: } truelight@4396: truelight@0: // Local truelight@0: static int _grow_town_result; truelight@0: tron@1977: static bool BuildTownHouse(Town *t, TileIndex tile); tron@1977: static void ClearTownHouse(Town *t, TileIndex tile); tron@1977: static void DoBuildTownHouse(Town *t, TileIndex tile); truelight@0: Darkvater@2436: static void TownDrawHouseLift(const TileInfo *ti) truelight@0: { celestar@3426: AddChildSpriteScreen(SPR_LIFT, 14, 60 - GetLiftPosition(ti->tile)); truelight@0: } truelight@0: Darkvater@2436: typedef void TownDrawTileProc(const TileInfo *ti); truelight@0: static TownDrawTileProc * const _town_draw_tile_procs[1] = { ludde@2065: TownDrawHouseLift truelight@0: }; truelight@0: truelight@0: truelight@0: static void DrawTile_Town(TileInfo *ti) truelight@0: { belugas@3654: const DrawBuildingsTileStruct *dcts; truelight@0: uint32 image; truelight@0: truelight@0: /* Retrieve pointer to the draw town tile struct */ truelight@0: { tron@480: /* this "randomizes" on the (up to) 4 variants of a building */ tron@480: uint variant; tron@480: variant = ti->x >> 4; tron@480: variant ^= ti->x >> 6; tron@480: variant ^= ti->y >> 4; tron@480: variant -= ti->y >> 6; tron@480: variant &= 3; belugas@3654: dcts = &_town_draw_tile_data[GetHouseType(ti->tile) << 4 | variant << 2 | GetHouseBuildingStage(ti->tile)]; truelight@0: } truelight@0: tron@4053: if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, ti->tileh); tron@4053: DrawGroundSprite(dcts->ground); truelight@0: truelight@0: /* Add a house on top of the ground? */ belugas@3654: image = dcts->building; tron@2639: if (image != 0) { tron@2639: if (_display_opt & DO_TRANS_BUILDINGS) MAKE_TRANSPARENT(image); truelight@193: truelight@193: AddSortableSpriteToDraw(image, tron@482: ti->x + dcts->subtile_x, tron@482: ti->y + dcts->subtile_y, tron@482: dcts->width + 1, tron@482: dcts->height + 1, truelight@0: dcts->dz, tron@4053: ti->z tron@4053: ); truelight@0: tron@2639: if (_display_opt & DO_TRANS_BUILDINGS) return; truelight@0: } truelight@0: truelight@0: { belugas@3654: int proc = dcts->draw_proc - 1; tron@2639: tron@2639: if (proc >= 0) _town_draw_tile_procs[proc](ti); truelight@0: } truelight@0: } truelight@0: tron@4231: static uint GetSlopeZ_Town(TileIndex tile, uint x, uint y) truelight@0: { tron@4231: return GetTileMaxZ(tile); truelight@0: } truelight@0: tron@3636: static Slope GetSlopeTileh_Town(TileIndex tile, Slope tileh) dominik@39: { tron@3636: return SLOPE_FLAT; dominik@39: } dominik@39: tron@1977: static void AnimateTile_Town(TileIndex tile) truelight@0: { celestar@3426: int pos, dest; truelight@0: tron@2639: if (_tick_counter & 3) return; truelight@0: ludde@2065: // If the house is not one with a lift anymore, then stop this animating. ludde@2065: // Not exactly sure when this happens, but probably when a house changes. ludde@2065: // Before this was just a return...so it'd leak animated tiles.. ludde@2065: // That bug seems to have been here since day 1?? celestar@3377: if (!(_housetype_extra_flags[GetHouseType(tile)] & 0x20)) { ludde@2065: DeleteAnimatedTile(tile); ludde@2065: return; ludde@2065: } truelight@0: celestar@3426: if (!IsLiftMoving(tile)) { tron@2639: int i; tron@2639: Darkvater@2891: /** Building has 6 floors, number 0 .. 6, where 1 is illegal. Darkvater@2891: * This is due to the fact that the first floor is, in the graphics, Darkvater@2891: * the height of 2 'normal' floors. Darkvater@2891: * Furthermore, there are 6 lift positions from floor N (incl) to floor N + 1 (excl) */ truelight@0: do { tron@2639: i = (Random() & 7) - 1; celestar@3426: } while (i < 0 || i == 1 || i * 6 == GetLiftPosition(tile)); truelight@0: celestar@3426: SetLiftDestination(tile, i); truelight@0: } truelight@0: celestar@3426: pos = GetLiftPosition(tile); celestar@3426: dest = GetLiftDestination(tile) * 6; celestar@3426: pos += (pos < dest) ? 1 : -1; celestar@3426: SetLiftPosition(tile, pos); truelight@0: celestar@3426: if (pos == dest) HaltLift(tile); truelight@193: truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: truelight@0: static void UpdateTownRadius(Town *t); truelight@0: tron@1977: static bool IsCloseToTown(TileIndex tile, uint dist) truelight@0: { tron@2630: const Town* t; truelight@0: truelight@0: FOR_ALL_TOWNS(t) { truelight@4346: if (DistanceManhattan(tile, t->xy) < dist) return true; truelight@0: } truelight@0: return false; truelight@0: } truelight@0: truelight@835: static void MarkTownSignDirty(Town *t) truelight@835: { truelight@835: MarkAllViewportsDirty( truelight@835: t->sign.left-6, truelight@835: t->sign.top-3, truelight@835: t->sign.left+t->sign.width_1*4+12, truelight@835: t->sign.top + 45 truelight@835: ); truelight@835: } truelight@835: truelight@835: void UpdateTownVirtCoord(Town *t) truelight@835: { truelight@836: Point pt; truelight@836: truelight@835: MarkTownSignDirty(t); celestar@3422: pt = RemapCoords2(TileX(t->xy) * TILE_SIZE, TileY(t->xy) * TILE_SIZE); ludde@2070: SetDParam(0, t->index); ludde@2070: SetDParam(1, t->population); ludde@2070: UpdateViewportSignPos(&t->sign, pt.x, pt.y - 24, ludde@2070: _patches.population_in_label ? STR_TOWN_LABEL_POP : STR_TOWN_LABEL); truelight@835: MarkTownSignDirty(t); truelight@835: } truelight@0: truelight@0: static void ChangePopulation(Town *t, int mod) truelight@0: { truelight@0: t->population += mod; truelight@0: InvalidateWindow(WC_TOWN_VIEW, t->index); truelight@835: UpdateTownVirtCoord(t); truelight@0: truelight@0: if (_town_sort_order & 2) _town_sort_dirty = true; truelight@0: } truelight@0: tron@1093: uint32 GetWorldPopulation(void) celestar@1080: { celestar@1080: uint32 pop; tron@2630: const Town* t; tron@2630: celestar@1080: pop = 0; tron@2639: FOR_ALL_TOWNS(t) pop += t->population; celestar@1080: return pop; celestar@1080: } celestar@1080: tron@1977: static void MakeSingleHouseBigger(TileIndex tile) truelight@0: { tron@1035: assert(IsTileType(tile, MP_HOUSE)); truelight@193: celestar@3426: if (LiftHasDestination(tile)) return; truelight@0: belugas@3432: IncHouseConstructionTick(tile); belugas@3432: if (GetHouseConstructionTick(tile) != 0) return; truelight@0: belugas@3432: IncHouseBuildingStage(tile); /*increase construction stage of one more step*/ truelight@0: belugas@3432: if (GetHouseBuildingStage(tile) == TOWN_HOUSE_COMPLETED){ belugas@3432: /*Now, construction is completed. Can add population of building to the town*/ celestar@3377: ChangePopulation(GetTownByTile(tile), _housetype_population[GetHouseType(tile)]); truelight@0: } truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: tron@1977: static void MakeTownHouseBigger(TileIndex tile) truelight@0: { celestar@3377: uint flags = _house_more_flags[GetHouseType(tile)]; truelight@0: if (flags & 8) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 0)); truelight@0: if (flags & 4) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 1)); truelight@0: if (flags & 2) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 0)); truelight@0: if (flags & 1) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 1)); truelight@0: } truelight@0: tron@1977: static void TileLoop_Town(TileIndex tile) truelight@0: { truelight@0: int house; truelight@0: Town *t; truelight@0: uint32 r; truelight@0: belugas@3432: if (GetHouseBuildingStage(tile) != TOWN_HOUSE_COMPLETED) { belugas@3432: /*Construction is not completed. See if we can go further in construction*/ truelight@0: MakeTownHouseBigger(tile); truelight@0: return; truelight@0: } truelight@0: celestar@3377: house = GetHouseType(tile); celestar@3426: if ((_housetype_extra_flags[house] & 0x20) && !LiftHasDestination(tile) && CHANCE16(1, 2) && AddAnimatedTile(tile)) BeginLiftMovement(tile); truelight@0: tron@3319: t = GetTownByTile(tile); truelight@0: truelight@0: r = Random(); truelight@0: tron@2150: if (GB(r, 0, 8) < _housetype_population[house]) { tron@2150: uint amt = GB(r, 0, 8) / 8 + 1; tron@2150: uint moved; tron@2150: truelight@0: if (_economy.fluct <= 0) amt = (amt + 1) >> 1; truelight@0: t->new_max_pass += amt; truelight@0: moved = MoveGoodsToStation(tile, 1, 1, CT_PASSENGERS, amt); truelight@0: t->new_act_pass += moved; truelight@0: } truelight@0: tron@2150: if (GB(r, 8, 8) < _housetype_mailamount[house] ) { tron@2150: uint amt = GB(r, 8, 8) / 8 + 1; tron@2150: uint moved; tron@2150: truelight@0: if (_economy.fluct <= 0) amt = (amt + 1) >> 1; truelight@0: t->new_max_mail += amt; truelight@0: moved = MoveGoodsToStation(tile, 1, 1, CT_MAIL, amt); truelight@0: t->new_act_mail += moved; truelight@0: } truelight@0: belugas@3432: if (_house_more_flags[house] & 8 && HASBIT(t->flags12, TOWN_IS_FUNDED) && --t->time_until_rebuild == 0) { tron@2150: t->time_until_rebuild = GB(r, 16, 6) + 130; truelight@0: truelight@0: _current_player = OWNER_TOWN; truelight@0: truelight@0: ClearTownHouse(t, tile); truelight@193: truelight@0: // rebuild with another house? tron@2150: if (GB(r, 24, 8) >= 12) DoBuildTownHouse(t, tile); truelight@314: truelight@314: _current_player = OWNER_NONE; truelight@0: } truelight@0: } truelight@0: tron@1977: static void ClickTile_Town(TileIndex tile) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: tron@1977: static int32 ClearTile_Town(TileIndex tile, byte flags) truelight@0: { truelight@0: int house, rating; truelight@0: int32 cost; truelight@0: Town *t; truelight@0: truelight@0: // safety checks truelight@0: if (!EnsureNoVehicle(tile)) return CMD_ERROR; truelight@0: if (flags&DC_AUTO && !(flags&DC_AI_BUILDING)) return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED); truelight@0: celestar@3377: house = GetHouseType(tile); truelight@0: cost = _price.remove_house * _housetype_remove_cost[house] >> 8; truelight@0: truelight@0: rating = _housetype_remove_ratingmod[house]; truelight@0: _cleared_town_rating += rating; tron@3319: _cleared_town = t = GetTownByTile(tile); truelight@193: Darkvater@4850: if (IsValidPlayer(_current_player)) { truelight@0: if (rating > t->ratings[_current_player] && !(flags & DC_NO_TOWN_RATING) && !_cheats.magic_bulldozer.value) { tron@534: SetDParam(0, t->index); truelight@0: return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES); truelight@193: } truelight@0: } truelight@0: truelight@0: if (flags & DC_EXEC) { celestar@1005: ChangeTownRating(t, -rating, RATING_HOUSE_MINIMUM); truelight@0: ClearTownHouse(t, tile); truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: tron@1977: static void GetAcceptedCargo_Town(TileIndex tile, AcceptedCargo ac) truelight@0: { celestar@3377: byte type = GetHouseType(tile); truelight@193: tron@473: ac[CT_PASSENGERS] = _housetype_cargo_passengers[type]; tron@2639: ac[CT_MAIL] = _housetype_cargo_mail[type]; tron@2639: ac[CT_GOODS] = _housetype_cargo_goods[type]; tron@2639: ac[CT_FOOD] = _housetype_cargo_food[type]; truelight@0: } truelight@0: tron@1977: static void GetTileDesc_Town(TileIndex tile, TileDesc *td) truelight@0: { celestar@3377: td->str = _town_tile_names[GetHouseType(tile)]; belugas@3432: if (GetHouseBuildingStage(tile) != TOWN_HOUSE_COMPLETED) { tron@534: SetDParamX(td->dparam, 0, td->str); truelight@0: td->str = STR_2058_UNDER_CONSTRUCTION; truelight@0: } truelight@0: truelight@0: td->owner = OWNER_TOWN; truelight@0: } truelight@0: tron@1977: static uint32 GetTileTrackStatus_Town(TileIndex tile, TransportType mode) truelight@0: { truelight@0: /* not used */ truelight@0: return 0; truelight@0: } truelight@0: Darkvater@2436: static void ChangeTileOwner_Town(TileIndex tile, PlayerID old_player, PlayerID new_player) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: truelight@0: tron@909: static const TileIndexDiffC _roadblock_tileadd[] = { tron@909: { 0, -1}, tron@909: { 1, 0}, tron@909: { 0, 1}, tron@909: {-1, 0}, truelight@193: truelight@0: // Store the first 3 elements again. truelight@0: // Lets us rotate without using &3. tron@909: { 0, -1}, tron@909: { 1, 0}, tron@909: { 0, 1} truelight@0: }; truelight@0: tron@2817: tron@2817: static bool GrowTown(Town *t); tron@2817: truelight@0: static void TownTickHandler(Town *t) truelight@0: { belugas@3432: if (HASBIT(t->flags12, TOWN_IS_FUNDED)) { truelight@0: int i = t->grow_counter - 1; truelight@0: if (i < 0) { truelight@0: if (GrowTown(t)) { truelight@0: i = t->growth_rate; truelight@0: } else { truelight@193: i = 0; truelight@0: } truelight@0: } truelight@0: t->grow_counter = i; truelight@0: } truelight@0: truelight@0: UpdateTownRadius(t); truelight@0: } truelight@0: tron@1093: void OnTick_Town(void) truelight@0: { tron@2639: if (_game_mode == GM_EDITOR) return; truelight@0: pasky@1451: /* Make sure each town's tickhandler invocation frequency is about the pasky@1451: * same - TOWN_GROWTH_FREQUENCY - independent on the number of towns. */ matthijs@5247: for (_cur_town_iter += GetMaxTownIndex() + 1; pasky@1529: _cur_town_iter >= TOWN_GROWTH_FREQUENCY; pasky@1529: _cur_town_iter -= TOWN_GROWTH_FREQUENCY) { pasky@1517: uint32 i = _cur_town_ctr; pasky@1451: matthijs@5247: if (++_cur_town_ctr > GetMaxTownIndex()) pasky@1448: _cur_town_ctr = 0; truelight@1260: truelight@4352: if (IsValidTownID(i)) TownTickHandler(GetTown(i)); pasky@1448: } truelight@0: } truelight@0: tron@3150: static RoadBits GetTownRoadMask(TileIndex tile) truelight@0: { tron@3150: TrackBits b = GetAnyRoadTrackBits(tile); tron@3150: RoadBits r = 0; tron@2639: tron@3150: if (b & TRACK_BIT_X) r |= ROAD_X; tron@3150: if (b & TRACK_BIT_Y) r |= ROAD_Y; tron@3150: if (b & TRACK_BIT_UPPER) r |= ROAD_NE | ROAD_NW; tron@3150: if (b & TRACK_BIT_LOWER) r |= ROAD_SE | ROAD_SW; tron@3150: if (b & TRACK_BIT_LEFT) r |= ROAD_NW | ROAD_SW; tron@3150: if (b & TRACK_BIT_RIGHT) r |= ROAD_NE | ROAD_SE; truelight@0: return r; truelight@0: } truelight@0: tron@1977: static bool IsRoadAllowedHere(TileIndex tile, int dir) truelight@0: { tron@3636: Slope k; tron@3636: Slope slope; truelight@0: truelight@0: // If this assertion fails, it might be because the world contains truelight@0: // land at the edges. This is not ok. truelight@0: TILE_ASSERT(tile); truelight@193: tron@2639: for (;;) { truelight@0: // Check if there already is a road at this point? tron@3150: if (GetAnyRoadTrackBits(tile) == 0) { truelight@0: // No, try to build one in the direction. truelight@0: // if that fails clear the land, and if that fails exit. truelight@0: // This is to make sure that we can build a road here later. tron@3491: if (CmdFailed(DoCommand(tile, (dir & 1 ? ROAD_X : ROAD_Y), 0, DC_AUTO, CMD_BUILD_ROAD)) && tron@3491: CmdFailed(DoCommand(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR))) truelight@0: return false; truelight@0: } truelight@0: truelight@0: slope = GetTileSlope(tile, NULL); tron@3636: if (slope == SLOPE_FLAT) { pasky@465: no_slope: truelight@0: // Tile has no slope truelight@0: // Disallow the road if any neighboring tile has a road. tron@909: if (HASBIT(GetTownRoadMask(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[dir+1]))), dir^2) || tron@909: HASBIT(GetTownRoadMask(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[dir+3]))), dir^2) || tron@909: HASBIT(GetTownRoadMask(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[dir+1]) + ToTileIndexDiff(_roadblock_tileadd[dir+2]))), dir) || tron@909: HASBIT(GetTownRoadMask(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[dir+3]) + ToTileIndexDiff(_roadblock_tileadd[dir+2]))), dir)) truelight@0: return false; truelight@193: truelight@0: // Otherwise allow truelight@0: return true; truelight@0: } truelight@193: truelight@0: // If the tile is not a slope in the right direction, then truelight@0: // maybe terraform some. tron@3636: k = (dir & 1) ? SLOPE_NE : SLOPE_NW; tron@3636: if (k != slope && ComplementSlope(k) != slope) { truelight@0: uint32 r = Random(); truelight@0: tron@2639: if (CHANCE16I(1, 8, r) && !_generating_world) { pasky@465: int32 res; pasky@465: tron@2639: if (CHANCE16I(1, 16, r)) { tron@3491: res = DoCommand(tile, slope, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, pasky@465: CMD_TERRAFORM_LAND); tron@2639: } else { tron@3639: res = DoCommand(tile, slope ^ 0xF, 1, DC_EXEC | DC_AUTO | DC_NO_WATER, pasky@465: CMD_TERRAFORM_LAND); tron@2639: } tron@2646: if (CmdFailed(res) && CHANCE16I(1, 3, r)) { pasky@465: // We can consider building on the slope, though. pasky@465: goto no_slope; tron@2646: } truelight@0: } truelight@0: return false; truelight@0: } truelight@835: return true; truelight@0: } truelight@0: } truelight@0: tron@1977: static bool TerraformTownTile(TileIndex tile, int edges, int dir) truelight@0: { truelight@0: int32 r; truelight@193: truelight@0: TILE_ASSERT(tile); truelight@0: tron@3491: r = DoCommand(tile, edges, dir, DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND); tron@2646: if (CmdFailed(r) || r >= 126 * 16) return false; tron@3491: DoCommand(tile, edges, dir, DC_AUTO | DC_NO_WATER | DC_EXEC, CMD_TERRAFORM_LAND); truelight@0: return true; truelight@0: } truelight@0: tron@1977: static void LevelTownLand(TileIndex tile) truelight@0: { tron@3636: Slope tileh; truelight@0: truelight@0: TILE_ASSERT(tile); truelight@0: truelight@0: // Don't terraform if land is plain or if there's a house there. tron@3055: if (IsTileType(tile, MP_HOUSE)) return; tron@3055: tileh = GetTileSlope(tile, NULL); tron@3636: if (tileh == SLOPE_FLAT) return; truelight@0: truelight@0: // First try up, then down tron@3055: if (!TerraformTownTile(tile, ~tileh & 0xF, 1)) { tron@3055: TerraformTownTile(tile, tileh & 0xF, 0); truelight@0: } truelight@0: } truelight@0: tron@3150: static void GrowTownInTile(TileIndex* tile_ptr, RoadBits mask, int block, Town* t1) truelight@0: { tron@3150: RoadBits rcmd; tron@1977: TileIndex tmptile; tron@3150: DiagDirection i; truelight@0: int j; tron@1977: TileIndex tile = *tile_ptr; truelight@0: truelight@0: TILE_ASSERT(tile); truelight@0: truelight@0: if (mask == 0) { tron@3150: int a; tron@3150: int b; tron@3150: truelight@0: // Tile has no road. First reset the status counter truelight@193: // to say that this is the last iteration. truelight@0: _grow_town_result = 0; truelight@0: truelight@0: // Remove hills etc truelight@0: LevelTownLand(tile); truelight@0: truelight@0: // Is a road allowed here? tron@2639: if (!IsRoadAllowedHere(tile, block)) return; truelight@0: truelight@0: // Randomize new road block numbers truelight@0: a = block; truelight@0: b = block ^ 2; tron@2637: if (CHANCE16(1, 4)) { tron@2637: do { tron@2637: a = GB(Random(), 0, 2); tron@2952: } while (a == b); tron@2637: } truelight@0: tron@909: if (!IsRoadAllowedHere(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a])), a)) { truelight@0: // A road is not allowed to continue the randomized road, truelight@0: // return if the road we're trying to build is curved. tron@2639: if (a != (b ^ 2)) return; truelight@193: truelight@0: // Return if neither side of the new road is a house tron@1035: if (!IsTileType(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a + 1])), MP_HOUSE) && tron@1035: !IsTileType(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a + 3])), MP_HOUSE)) truelight@0: return; truelight@0: truelight@0: // That means that the road is only allowed if there is a house truelight@0: // at any side of the new road. truelight@0: } truelight@0: rcmd = (1 << a) + (1 << b); truelight@0: truelight@0: } else if (block < 5 && !HASBIT(mask,block^2)) { truelight@0: // Continue building on a partial road. truelight@0: // Always OK. truelight@0: _grow_town_result = 0; tron@2639: rcmd = 1 << (block ^ 2); truelight@0: } else { tron@3150: int i; truelight@0: truelight@0: // Reached a tunnel? Then continue at the other side of it. tron@3184: if (IsTunnelTile(tile) && GetTunnelTransportType(tile) == TRANSPORT_ROAD) { tron@3154: *tile_ptr = GetOtherTunnelEnd(tile); truelight@0: return; truelight@0: } truelight@193: truelight@0: // For any other kind of tunnel/bridge, bail out. celestar@5590: if (IsTileType(tile, MP_TUNNEL)) return; celestar@5590: if (IsTileType(tile, MP_RAILWAY_BRIDGE)) return; truelight@0: truelight@0: // Possibly extend the road in a direction. truelight@0: // Randomize a direction and if it has a road, bail out. tron@2635: i = GB(Random(), 0, 2); tron@2639: if (HASBIT(mask, i)) return; truelight@0: truelight@0: // This is the tile we will reach if we extend to this direction. tron@909: tmptile = TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[i])); truelight@193: truelight@0: // Don't do it if it reaches to water. celestar@3433: if (IsClearWaterTile(tmptile)) return; truelight@0: truelight@193: // Build a house at the edge. 60% chance or truelight@0: // always ok if no road allowed. tron@2639: if (!IsRoadAllowedHere(tmptile, i) || CHANCE16(6, 10)) { truelight@0: // But not if there already is a house there. tron@1035: if (!IsTileType(tmptile, MP_HOUSE)) { truelight@0: // Level the land if possible truelight@0: LevelTownLand(tmptile); truelight@0: truelight@0: // And build a house. truelight@0: // Set result to -1 if we managed to build it. tron@2639: if (BuildTownHouse(t1, tmptile)) _grow_town_result = -1; truelight@0: } truelight@0: return; truelight@0: } truelight@0: truelight@0: _grow_town_result = 0; truelight@0: rcmd = 1 << i; truelight@0: } truelight@0: truelight@0: // Return if a water tile celestar@3433: if (IsClearWaterTile(tile)) return; truelight@0: truelight@0: // Determine direction of slope, truelight@0: // and build a road if not a special slope. tron@3055: switch (GetTileSlope(tile, NULL)) { tron@3636: case SLOPE_SW: i = DIAGDIR_NE; break; tron@3636: case SLOPE_SE: i = DIAGDIR_NW; break; tron@3636: case SLOPE_NW: i = DIAGDIR_SE; break; tron@3636: case SLOPE_NE: i = DIAGDIR_SW; break; tron@3055: tron@3055: default: truelight@0: build_road_and_exit: tron@3491: if (!CmdFailed(DoCommand(tile, rcmd, t1->index, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD))) { tron@3055: _grow_town_result = -1; tron@3055: } tron@3055: return; truelight@0: } truelight@0: truelight@0: tmptile = tile; truelight@0: truelight@0: // Now it contains the direction of the slope rubidium@4434: j = -11; // max 11 tile long bridges truelight@0: do { truelight@0: if (++j == 0) truelight@0: goto build_road_and_exit; Darkvater@4559: tmptile = TILE_MASK(tmptile + TileOffsByDiagDir(i)); celestar@3433: } while (IsClearWaterTile(tmptile)); truelight@0: truelight@0: // no water tiles in between? truelight@0: if (j == -10) truelight@0: goto build_road_and_exit; truelight@0: truelight@0: // Quit if it selecting an appropiate bridge type fails a large number of times. truelight@0: j = 22; truelight@0: { truelight@0: int32 bridge_len = GetBridgeLength(tile, tmptile); truelight@0: do { truelight@0: byte bridge_type = RandomRange(MAX_BRIDGES - 1); truelight@0: if (CheckBridge_Stuff(bridge_type, bridge_len)) { tron@3491: if (!CmdFailed(DoCommand(tile, tmptile, 0x8000 + bridge_type, DC_EXEC | DC_AUTO, CMD_BUILD_BRIDGE))) truelight@0: _grow_town_result = -1; truelight@0: truelight@0: // obviously, if building any bridge would fail, there is no need to try other bridge-types truelight@0: return; truelight@0: } tron@2952: } while (--j != 0); truelight@0: } truelight@0: } truelight@0: truelight@0: // Returns true if a house was built, or no if the build failed. tron@1977: static int GrowTownAtRoad(Town *t, TileIndex tile) truelight@0: { truelight@0: int block = 5; // special case truelight@0: truelight@0: TILE_ASSERT(tile); truelight@0: truelight@0: // Number of times to search. pasky@470: _grow_town_result = 10 + t->num_houses * 4 / 9; truelight@0: truelight@0: do { truelight@0: // Get a bitmask of the road blocks on a tile tron@3150: RoadBits mask = GetTownRoadMask(tile); truelight@0: truelight@0: // Try to grow the town from this point truelight@0: GrowTownInTile(&tile,mask,block,t); truelight@0: truelight@0: // Exclude the source position from the bitmask truelight@0: // and return if no more road blocks available truelight@0: CLRBIT(mask, (block ^ 2)); truelight@0: if (mask == 0) truelight@0: return _grow_town_result; truelight@0: truelight@0: // Select a random bit from the blockmask, walk a step truelight@0: // and continue the search from there. truelight@0: do block = Random() & 3; while (!HASBIT(mask,block)); tron@909: tile += ToTileIndexDiff(_roadblock_tileadd[block]); truelight@0: truelight@1327: if (IsTileType(tile, MP_STREET)) { truelight@1327: /* Don't allow building over roads of other cities */ tron@4077: if (IsTileOwner(tile, OWNER_TOWN) && GetTownByTile(tile) != t) { truelight@1327: _grow_town_result = -1; tron@4077: } else if (_game_mode == GM_EDITOR) { truelight@1327: /* If we are in the SE, and this road-piece has no town owner yet, it just found an rubidium@4549: * owner :) (happy happy happy road now) */ tron@1902: SetTileOwner(tile, OWNER_TOWN); belugas@3432: SetTownIndex(tile, t->index); truelight@1327: } truelight@1327: } truelight@1280: truelight@0: // Max number of times is checked. truelight@0: } while (--_grow_town_result >= 0); truelight@0: truelight@0: return (_grow_town_result == -2); truelight@0: } truelight@0: truelight@0: // Generate a random road block truelight@0: // The probability of a straight road truelight@0: // is somewhat higher than a curved. tron@3150: static RoadBits GenRandomRoadBits(void) truelight@0: { truelight@0: uint32 r = Random(); tron@2140: uint a = GB(r, 0, 2); tron@2140: uint b = GB(r, 8, 2); truelight@0: if (a == b) b ^= 2; tron@2140: return (1 << a) + (1 << b); truelight@0: } truelight@0: truelight@0: // Grow the town truelight@0: // Returns true if a house was built, or no if the build failed. tron@2817: static bool GrowTown(Town *t) truelight@0: { tron@1977: TileIndex tile; tron@909: const TileIndexDiffC *ptr; tron@2498: PlayerID old_player; truelight@0: tron@909: static const TileIndexDiffC _town_coord_mod[] = { tron@909: {-1, 0}, tron@909: { 1, 1}, tron@909: { 1, -1}, tron@909: {-1, -1}, tron@909: {-1, 0}, tron@909: { 0, 2}, tron@909: { 2, 0}, tron@909: { 0, -2}, tron@909: {-1, -1}, tron@909: {-2, 2}, tron@909: { 2, 2}, tron@909: { 2, -2}, tron@909: { 0, 0} truelight@0: }; truelight@0: truelight@0: // Current player is a town truelight@260: old_player = _current_player; truelight@0: _current_player = OWNER_TOWN; truelight@0: truelight@0: // Find a road that we can base the construction on. truelight@0: tile = t->xy; tron@909: for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) { tron@3150: if (GetAnyRoadTrackBits(tile) != 0) { truelight@260: int r = GrowTownAtRoad(t, tile); truelight@260: _current_player = old_player; truelight@260: return r; truelight@0: } tron@909: tile = TILE_ADD(tile, ToTileIndexDiff(*ptr)); tron@909: } truelight@0: truelight@0: // No road available, try to build a random road block by truelight@0: // clearing some land and then building a road there. truelight@0: tile = t->xy; tron@909: for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) { truelight@4317: /* Only work with plain land that not already has a house */ truelight@4317: if (!IsTileType(tile, MP_HOUSE) && GetTileSlope(tile, NULL) == SLOPE_FLAT) { tron@3491: if (!CmdFailed(DoCommand(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR))) { tron@3491: DoCommand(tile, GenRandomRoadBits(), t->index, DC_EXEC | DC_AUTO, CMD_BUILD_ROAD); truelight@260: _current_player = old_player; truelight@0: return true; truelight@0: } truelight@0: } tron@909: tile = TILE_ADD(tile, ToTileIndexDiff(*ptr)); tron@909: } truelight@0: truelight@260: _current_player = old_player; truelight@0: return false; truelight@0: } truelight@0: truelight@0: static void UpdateTownRadius(Town *t) truelight@0: { truelight@0: static const uint16 _town_radius_data[23][5] = { rubidium@4344: { 4, 0, 0, 0, 0}, // 0 rubidium@4344: { 16, 0, 0, 0, 0}, rubidium@4344: { 25, 0, 0, 0, 0}, rubidium@4344: { 36, 0, 0, 0, 0}, rubidium@4344: { 49, 0, 4, 0, 0}, rubidium@4344: { 64, 0, 4, 0, 0}, // 20 rubidium@4344: { 64, 0, 9, 0, 1}, rubidium@4344: { 64, 0, 9, 0, 4}, rubidium@4344: { 64, 0, 16, 0, 4}, rubidium@4344: { 81, 0, 16, 0, 4}, rubidium@4344: { 81, 0, 16, 0, 4}, // 40 rubidium@4344: { 81, 0, 25, 0, 9}, rubidium@4344: { 81, 36, 25, 0, 9}, rubidium@4344: { 81, 36, 25, 16, 9}, rubidium@4344: { 81, 49, 0, 25, 9}, rubidium@4344: { 81, 64, 0, 25, 9}, // 60 rubidium@4344: { 81, 64, 0, 36, 9}, rubidium@4344: { 81, 64, 0, 36, 16}, pasky@470: {100, 81, 0, 49, 16}, pasky@470: {100, 81, 0, 49, 25}, pasky@470: {121, 81, 0, 49, 25}, // 80 pasky@470: {121, 81, 0, 49, 25}, pasky@470: {121, 81, 0, 49, 36}, // 88 truelight@0: }; pasky@470: pasky@470: if (t->num_houses < 92) { pasky@470: memcpy(t->radius, _town_radius_data[t->num_houses / 4], sizeof(t->radius)); pasky@470: } else { pasky@470: int mass = t->num_houses / 8; pasky@470: // At least very roughly extrapolate. Empirical numbers dancing between pasky@470: // overwhelming by cottages and skyscrapers outskirts. pasky@470: t->radius[0] = mass * mass; pasky@470: // Actually we are proportional to sqrt() but that's right because pasky@470: // we are covering an area. pasky@470: t->radius[1] = mass * 7; pasky@470: t->radius[2] = 0; pasky@470: t->radius[3] = mass * 4; pasky@470: t->radius[4] = mass * 3; pasky@470: //debug("%d (->%d): %d %d %d %d\n", t->num_houses, mass, t->radius[0], t->radius[1], t->radius[3], t->radius[4]); pasky@470: } truelight@0: } truelight@0: pasky@1421: static bool CreateTownName(uint32 *townnameparts) truelight@0: { truelight@0: Town *t2; truelight@0: char buf1[64]; truelight@0: char buf2[64]; truelight@0: uint32 r; pasky@1421: /* Do not set too low tries, since when we run out of names, we loop pasky@1421: * for #tries only one time anyway - then we stop generating more pasky@1421: * towns. Do not show it too high neither, since looping through all pasky@1421: * the other towns may take considerable amount of time (10000 is pasky@1421: * too much). */ pasky@1421: int tries = 1000; pasky@1421: uint16 townnametype = SPECSTR_TOWNNAME_START + _opt.town_name; truelight@0: pasky@1421: assert(townnameparts); truelight@0: tron@2952: for (;;) { truelight@0: restart: truelight@0: r = Random(); truelight@0: tron@534: SetDParam(0, r); Darkvater@4912: GetString(buf1, townnametype, lastof(buf1)); truelight@193: truelight@0: // Check size and width Darkvater@4609: if (strlen(buf1) >= 31 || GetStringBoundingBox(buf1).width > 130) continue; truelight@0: truelight@0: FOR_ALL_TOWNS(t2) { truelight@4346: // We can't just compare the numbers since truelight@4346: // several numbers may map to a single name. truelight@4346: SetDParam(0, t2->index); Darkvater@4912: GetString(buf2, STR_TOWN, lastof(buf2)); truelight@4346: if (strcmp(buf1, buf2) == 0) { truelight@4346: if (tries-- < 0) return false; truelight@4346: goto restart; truelight@0: } truelight@0: } pasky@1421: *townnameparts = r; pasky@1421: return true; truelight@0: } truelight@0: } truelight@0: celestar@1377: void UpdateTownMaxPass(Town *t) truelight@0: { truelight@0: t->max_pass = t->population >> 3; truelight@0: t->max_mail = t->population >> 4; truelight@0: } truelight@0: celestar@3674: static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, uint size_mode) truelight@0: { truelight@0: int x, i; truelight@0: truelight@0: // clear the town struct truelight@0: i = t->index; truelight@0: memset(t, 0, sizeof(Town)); truelight@0: t->index = i; rubidium@5319: _total_towns++; truelight@0: truelight@0: t->xy = tile; truelight@0: t->num_houses = 0; truelight@0: t->time_until_rebuild = 10; truelight@0: UpdateTownRadius(t); truelight@0: t->flags12 = 0; truelight@0: t->population = 0; truelight@0: t->grow_counter = 0; truelight@0: t->growth_rate = 250; truelight@0: t->new_max_pass = 0; truelight@0: t->new_max_mail = 0; truelight@0: t->new_act_pass = 0; truelight@0: t->new_act_mail = 0; truelight@0: t->max_pass = 0; truelight@0: t->max_mail = 0; truelight@0: t->act_pass = 0; truelight@0: t->act_mail = 0; truelight@0: truelight@0: t->pct_pass_transported = 0; truelight@0: t->pct_mail_transported = 0; truelight@0: t->fund_buildings_months = 0; truelight@0: t->new_act_food = 0; darkvater@4: t->new_act_water = 0; truelight@0: t->act_food = 0; darkvater@4: t->act_water = 0; truelight@0: tron@2952: for (i = 0; i != MAX_PLAYERS; i++) truelight@0: t->ratings[i] = 500; truelight@0: truelight@0: t->have_ratings = 0; dominik@121: t->exclusivity = (byte)-1; dominik@121: t->exclusive_counter = 0; truelight@0: t->statues = 0; truelight@0: pasky@1421: t->townnametype = SPECSTR_TOWNNAME_START + _opt.town_name; pasky@1421: t->townnameparts = townnameparts; truelight@193: truelight@0: UpdateTownVirtCoord(t); truelight@0: _town_sort_dirty = true; truelight@0: celestar@3674: if (size_mode == 0) { celestar@3674: x = (Random() & 0xF) + 8; celestar@3674: } else { celestar@3674: x = (size_mode - 1) * 16 + 3; celestar@3674: } truelight@0: truelight@0: t->num_houses += x; truelight@0: UpdateTownRadius(t); truelight@0: truelight@0: i = x * 4; truelight@0: do { truelight@0: GrowTown(t); truelight@0: } while (--i); truelight@0: truelight@0: t->num_houses -= x; truelight@0: UpdateTownRadius(t); truelight@0: UpdateTownMaxPass(t); truelight@0: } truelight@0: tron@1093: static Town *AllocateTown(void) truelight@0: { truelight@0: Town *t; truelight@4346: truelight@4346: /* We don't use FOR_ALL here, because FOR_ALL skips invalid items. truelight@4346: * TODO - This is just a temporary stage, this will be removed. */ tron@4983: for (t = GetTown(0); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) { truelight@4346: if (!IsValidTown(t)) { rubidium@4330: TownID index = t->index; truelight@1260: truelight@1260: memset(t, 0, sizeof(Town)); truelight@1260: t->index = index; truelight@1260: truelight@0: return t; truelight@0: } truelight@0: } truelight@1260: truelight@1260: /* Check if we can add a block to the pool */ tron@4983: if (AddBlockToPool(&_Town_pool)) truelight@1260: return AllocateTown(); truelight@1260: truelight@0: return NULL; truelight@0: } truelight@0: Darkvater@1793: /** Create a new town. Darkvater@1793: * This obviously only works in the scenario editor. Function not removed Darkvater@1793: * as it might be possible in the future to fund your own town :) tron@3491: * @param tile coordinates where town is built Darkvater@5064: * @param p1 size of the town (0 = random, 1 = small, 2 = medium, 3 = large) Darkvater@1793: * @param p2 unused Darkvater@1793: */ tron@3491: int32 CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Town *t; pasky@1421: uint32 townnameparts; truelight@193: Darkvater@1793: /* Only in the scenario editor */ Darkvater@1793: if (_game_mode != GM_EDITOR) return CMD_ERROR; Darkvater@1793: truelight@0: SET_EXPENSES_TYPE(EXPENSES_OTHER); truelight@0: truelight@0: // Check if too close to the edge of map tron@1245: if (DistanceFromEdge(tile) < 12) truelight@0: return_cmd_error(STR_0237_TOO_CLOSE_TO_EDGE_OF_MAP); truelight@0: rubidium@5085: // Can only build on clear flat areas, possibly with trees. rubidium@5085: if ((!IsTileType(tile, MP_CLEAR) && !IsTileType(tile, MP_TREES)) || GetTileSlope(tile, NULL) != SLOPE_FLAT) { truelight@0: return_cmd_error(STR_0239_SITE_UNSUITABLE); tron@2986: } truelight@0: truelight@0: // Check distance to all other towns. truelight@0: if (IsCloseToTown(tile, 20)) truelight@0: return_cmd_error(STR_0238_TOO_CLOSE_TO_ANOTHER_TOWN); truelight@0: pasky@1421: // Get a unique name for the town. pasky@1421: if (!CreateTownName(&townnameparts)) pasky@1421: return_cmd_error(STR_023A_TOO_MANY_TOWNS); pasky@1421: truelight@0: // Allocate town struct truelight@0: t = AllocateTown(); Darkvater@1793: if (t == NULL) return_cmd_error(STR_023A_TOO_MANY_TOWNS); truelight@0: truelight@0: // Create the town truelight@0: if (flags & DC_EXEC) { truelight@0: _generating_world = true; celestar@3674: DoCreateTown(t, tile, townnameparts, p1); truelight@0: _generating_world = false; truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: celestar@3674: Town *CreateRandomTown(uint attempts, uint size_mode) truelight@0: { tron@1977: TileIndex tile; truelight@0: Town *t; pasky@1421: uint32 townnameparts; truelight@0: truelight@0: do { truelight@0: // Generate a tile index not too close from the edge ludde@2051: tile = RandomTile(); tron@2951: if (DistanceFromEdge(tile) < 20) continue; truelight@0: truelight@0: // Make sure the tile is plain tron@3636: if (!IsTileType(tile, MP_CLEAR) || GetTileSlope(tile, NULL) != SLOPE_FLAT) continue; truelight@0: truelight@0: // Check not too close to a town tron@2951: if (IsCloseToTown(tile, 20)) continue; truelight@193: pasky@1421: // Get a unique name for the town. tron@2951: if (!CreateTownName(&townnameparts)) break; pasky@1421: truelight@0: // Allocate a town struct truelight@0: t = AllocateTown(); tron@2951: if (t == NULL) break; truelight@0: celestar@3674: DoCreateTown(t, tile, townnameparts, size_mode); truelight@0: return t; celestar@1362: } while (--attempts); truelight@0: return NULL; truelight@0: } truelight@0: Darkvater@2430: static const byte _num_initial_towns[3] = {11, 23, 46}; truelight@0: Darkvater@2430: bool GenerateTowns(void) truelight@0: { celestar@1362: uint num = 0; Darkvater@2430: uint n = ScaleByMapSize(_num_initial_towns[_opt.diff.number_towns] + (Random() & 7)); tron@1202: truelight@4300: SetGeneratingWorldProgress(GWP_TOWN, n); truelight@4300: celestar@1362: do { truelight@4300: IncreaseGeneratingWorldProgress(GWP_TOWN); tron@4000: // try 20 times to create a random-sized town for the first loop. tron@4000: if (CreateRandomTown(20, 0) != NULL) num++; celestar@1362: } while (--n); celestar@1362: Darkvater@2430: // give it a last try, but now more aggressive celestar@3674: if (num == 0 && CreateRandomTown(10000, 0) == NULL) { matthijs@5247: if (GetNumTowns() == 0) { truelight@4384: /* XXX - can we handle that more gracefully? */ truelight@4384: if (_game_mode != GM_EDITOR) error("Could not generate any town"); Darkvater@2430: truelight@4384: return false; tron@4000: } celestar@1362: } Darkvater@2430: Darkvater@2430: return true; truelight@0: } truelight@0: tron@3636: static bool CheckBuildHouseMode(TileIndex tile, Slope tileh, int mode) tron@1977: { truelight@0: int b; tron@3636: Slope slope; truelight@0: truelight@0: static const byte _masks[8] = { truelight@0: 0xC,0x3,0x9,0x6, truelight@0: 0x3,0xC,0x6,0x9, truelight@0: }; truelight@0: truelight@0: slope = GetTileSlope(tile, NULL); tron@3636: if (IsSteepSlope(slope)) return false; truelight@0: celestar@5573: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return false; celestar@5573: truelight@0: b = 0; tron@3636: if ((slope != SLOPE_FLAT && ~slope & _masks[mode])) b = ~b; tron@3636: if ((tileh != SLOPE_FLAT && ~tileh & _masks[mode+4])) b = ~b; truelight@0: if (b) truelight@0: return false; truelight@0: tron@3491: return !CmdFailed(DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR)); truelight@0: } truelight@0: tron@4000: tron@4000: uint GetTownRadiusGroup(const Town* t, TileIndex tile) truelight@0: { tron@4000: uint dist = DistanceSquare(tile, t->xy); tron@4000: uint smallest; tron@4000: uint i; truelight@0: tron@4000: if (t->fund_buildings_months && dist <= 25) return 4; truelight@0: truelight@0: smallest = 0; Darkvater@2436: for (i = 0; i != lengthof(t->radius); i++) { tron@4000: if (dist < t->radius[i]) smallest = i; truelight@0: } truelight@0: truelight@0: return smallest; truelight@0: } truelight@0: tron@2958: static bool CheckFree2x2Area(TileIndex tile) truelight@0: { truelight@0: int i; truelight@0: tron@909: static const TileIndexDiffC _tile_add[] = { tron@909: {0 , 0 }, tron@909: {0 - 0, 1 - 0}, tron@909: {1 - 0, 0 - 1}, tron@909: {1 - 1, 1 - 0} truelight@0: }; truelight@0: tron@2952: for (i = 0; i != 4; i++) { tron@909: tile += ToTileIndexDiff(_tile_add[i]); truelight@0: tron@3636: if (GetTileSlope(tile, NULL) != SLOPE_FLAT) return false; truelight@0: celestar@5573: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return false; celestar@5573: tron@3491: if (CmdFailed(DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER | DC_FORCETEST, CMD_LANDSCAPE_CLEAR))) truelight@0: return false; truelight@0: } truelight@0: truelight@0: return true; truelight@0: } truelight@0: tron@1977: static void DoBuildTownHouse(Town *t, TileIndex tile) truelight@0: { truelight@0: int i; truelight@0: uint bitmask; truelight@0: int house; tron@3636: Slope slope; tron@1335: uint z; belugas@3432: uint oneof = 0; truelight@193: truelight@0: // Above snow? truelight@0: slope = GetTileSlope(tile, &z); truelight@0: truelight@0: // Get the town zone type truelight@0: { truelight@0: uint rad = GetTownRadiusGroup(t, tile); truelight@0: truelight@0: int land = _opt.landscape; tron@4000: if (land == LT_HILLY && z >= _opt.snow_line) land = -1; truelight@0: truelight@0: bitmask = (1 << rad) + (1 << (land + 12)); truelight@0: } truelight@0: truelight@0: // bits 0-4 are used truelight@0: // bits 11-15 are used truelight@0: // bits 5-10 are not used. truelight@0: { truelight@0: byte houses[lengthof(_housetype_flags)]; truelight@0: int num = 0; truelight@0: truelight@0: // Generate a list of all possible houses that can be built. tron@2952: for (i=0; i!=lengthof(_housetype_flags); i++) { truelight@0: if ((~_housetype_flags[i] & bitmask) == 0) truelight@0: houses[num++] = (byte)i; truelight@0: } truelight@0: tron@2952: for (;;) { truelight@0: house = houses[RandomRange(num)]; truelight@0: truelight@0: if (_cur_year < _housetype_years[house].min || _cur_year > _housetype_years[house].max) truelight@0: continue; truelight@0: truelight@0: // Special houses that there can be only one of. tron@483: switch (house) { tron@483: case HOUSE_TEMP_CHURCH: tron@483: case HOUSE_ARCT_CHURCH: tron@483: case HOUSE_SNOW_CHURCH: tron@483: case HOUSE_TROP_CHURCH: tron@483: case HOUSE_TOY_CHURCH: belugas@3432: SETBIT(oneof, TOWN_HAS_CHURCH); tron@483: break; tron@483: case HOUSE_STADIUM: tron@483: case HOUSE_MODERN_STADIUM: belugas@3432: SETBIT(oneof, TOWN_HAS_STADIUM); tron@483: break; tron@483: default: tron@483: oneof = 0; tron@483: break; tron@483: } truelight@0: belugas@3432: if (HASBITS(t->flags12 , oneof)) continue; truelight@0: truelight@0: // Make sure there is no slope? tron@3636: if (_housetype_extra_flags[house] & 0x12 && slope != SLOPE_FLAT) continue; truelight@193: tron@2951: if (_housetype_extra_flags[house] & 0x10) { tron@2958: if (CheckFree2x2Area(tile) || tron@2958: CheckFree2x2Area(tile += TileDiffXY(-1, 0)) || tron@2958: CheckFree2x2Area(tile += TileDiffXY( 0, -1)) || tron@2958: CheckFree2x2Area(tile += TileDiffXY( 1, 0))) { tron@1981: break; tron@2951: } tron@2951: tile += TileDiffXY(0, 1); tron@2951: } else if (_housetype_extra_flags[house] & 4) { tron@2958: if (CheckBuildHouseMode(tile + TileDiffXY(1, 0), slope, 0)) break; truelight@193: tron@2958: if (CheckBuildHouseMode(tile + TileDiffXY(-1, 0), slope, 1)) { tron@1981: tile += TileDiffXY(-1, 0); truelight@0: break; truelight@0: } tron@2951: } else if (_housetype_extra_flags[house] & 8) { tron@2958: if (CheckBuildHouseMode(tile + TileDiffXY(0, 1), slope, 2)) break; truelight@0: tron@2958: if (CheckBuildHouseMode(tile + TileDiffXY(0, -1), slope, 3)) { tron@1981: tile += TileDiffXY(0, -1); truelight@0: break; truelight@0: } tron@2951: } else { truelight@0: break; tron@2951: } truelight@0: } truelight@0: } truelight@0: truelight@0: t->num_houses++; truelight@0: truelight@0: // Special houses that there can be only one of. truelight@0: t->flags12 |= oneof; truelight@193: truelight@0: { celestar@3382: byte construction_counter = 0, construction_stage = 0, size_flags; truelight@0: truelight@0: if (_generating_world) { truelight@0: uint32 r = Random(); truelight@193: belugas@3432: construction_stage = TOWN_HOUSE_COMPLETED; celestar@3382: if (CHANCE16(1, 7)) construction_stage = GB(r, 0, 2); truelight@0: belugas@3432: if (construction_stage == TOWN_HOUSE_COMPLETED) { celestar@3382: ChangePopulation(t, _housetype_population[house]); celestar@3382: } else { celestar@3382: construction_counter = GB(r, 2, 2); celestar@3382: } truelight@0: } celestar@3382: size_flags = GB(_housetype_extra_flags[house], 2, 3); celestar@3382: MakeTownHouse(tile, t->index, construction_counter, construction_stage, size_flags, house); truelight@0: } truelight@0: } truelight@0: tron@1977: static bool BuildTownHouse(Town *t, TileIndex tile) truelight@0: { truelight@0: int32 r; truelight@193: truelight@0: // make sure it's possible truelight@0: if (!EnsureNoVehicle(tile)) return false; tron@3636: if (IsSteepSlope(GetTileSlope(tile, NULL))) return false; celestar@5573: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return false; truelight@0: tron@3491: r = DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR); peter1138@2737: if (CmdFailed(r)) return false; truelight@0: truelight@0: DoBuildTownHouse(t, tile); truelight@0: return true; truelight@0: } truelight@0: truelight@0: tron@1977: static void DoClearTownHouseHelper(TileIndex tile) truelight@0: { tron@1035: assert(IsTileType(tile, MP_HOUSE)); truelight@0: DoClearSquare(tile); truelight@0: DeleteAnimatedTile(tile); truelight@0: } truelight@0: tron@1977: static void ClearTownHouse(Town *t, TileIndex tile) tron@1977: { celestar@3377: uint house = GetHouseType(tile); truelight@0: uint eflags; truelight@0: tron@1035: assert(IsTileType(tile, MP_HOUSE)); truelight@0: truelight@0: // need to align the tile to point to the upper left corner of the house truelight@0: if (house >= 3) { // house id 0,1,2 MUST be single tile houses, or this code breaks. truelight@0: if (_housetype_extra_flags[house-1] & 0x04) { truelight@0: house--; tron@1981: tile += TileDiffXY(-1, 0); truelight@0: } else if (_housetype_extra_flags[house-1] & 0x18) { truelight@0: house--; tron@1981: tile += TileDiffXY(0, -1); truelight@0: } else if (_housetype_extra_flags[house-2] & 0x10) { truelight@0: house-=2; tron@1981: tile += TileDiffXY(-1, 0); truelight@0: } else if (_housetype_extra_flags[house-3] & 0x10) { truelight@0: house-=3; tron@1981: tile += TileDiffXY(-1, -1); truelight@0: } truelight@0: } truelight@193: belugas@3432: // Remove population from the town if the house is finished. belugas@3432: if (GetHouseBuildingStage(tile) == TOWN_HOUSE_COMPLETED) { truelight@0: ChangePopulation(t, -_housetype_population[house]); truelight@0: } truelight@0: truelight@0: t->num_houses--; truelight@0: truelight@0: // Clear flags for houses that only may exist once/town. tron@483: switch (house) { tron@483: case HOUSE_TEMP_CHURCH: tron@483: case HOUSE_ARCT_CHURCH: tron@483: case HOUSE_SNOW_CHURCH: tron@483: case HOUSE_TROP_CHURCH: tron@483: case HOUSE_TOY_CHURCH: belugas@3432: CLRBIT(t->flags12, TOWN_HAS_CHURCH); tron@483: break; tron@483: case HOUSE_STADIUM: tron@483: case HOUSE_MODERN_STADIUM: belugas@3432: CLRBIT(t->flags12, TOWN_HAS_STADIUM); tron@483: break; tron@483: default: tron@483: break; tron@483: } truelight@193: truelight@0: // Do the actual clearing of tiles truelight@0: eflags = _housetype_extra_flags[house]; truelight@0: DoClearTownHouseHelper(tile); tron@1981: if (eflags & 0x14) DoClearTownHouseHelper(tile + TileDiffXY(1, 0)); tron@1981: if (eflags & 0x18) DoClearTownHouseHelper(tile + TileDiffXY(0, 1)); tron@1981: if (eflags & 0x10) DoClearTownHouseHelper(tile + TileDiffXY(1, 1)); truelight@0: } truelight@0: Darkvater@1793: /** Rename a town (server-only). tron@3491: * @param tile unused Darkvater@1793: * @param p1 town ID to rename Darkvater@1793: * @param p2 unused Darkvater@1793: */ tron@3491: int32 CmdRenameTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: StringID str; Darkvater@1793: Town *t; Darkvater@1793: truelight@4352: if (!IsValidTownID(p1) || _cmd_text[0] == '\0') return CMD_ERROR; Darkvater@1793: Darkvater@1793: t = GetTown(p1); truelight@193: tron@1820: str = AllocateNameUnique(_cmd_text, 4); Darkvater@1793: if (str == 0) return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { Darkvater@1793: DeleteName(t->townnametype); truelight@0: t->townnametype = str; truelight@0: truelight@0: UpdateTownVirtCoord(t); truelight@0: _town_sort_dirty = true; truelight@0: UpdateAllStationVirtCoord(); truelight@0: MarkWholeScreenDirty(); truelight@0: } else { truelight@0: DeleteName(str); truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: truelight@0: // Called from GUI truelight@0: void ExpandTown(Town *t) truelight@0: { truelight@0: int amount, n; truelight@0: truelight@0: _generating_world = true; truelight@193: truelight@835: /* The more houses, the faster we grow */ truelight@835: amount = RandomRange(t->num_houses / 10) + 3; truelight@0: t->num_houses += amount; truelight@0: UpdateTownRadius(t); truelight@0: truelight@835: n = amount * 10; truelight@0: do GrowTown(t); while (--n); truelight@0: truelight@0: t->num_houses -= amount; truelight@0: UpdateTownRadius(t); truelight@0: truelight@0: UpdateTownMaxPass(t); truelight@0: _generating_world = false; truelight@0: } truelight@0: truelight@0: const byte _town_action_costs[8] = { truelight@0: 2, 4, 9, 35, 48, 53, 117, 175 truelight@0: }; truelight@0: tron@3877: static void TownActionAdvertiseSmall(Town* t) truelight@0: { tron@3877: ModifyStationRatingAround(t->xy, _current_player, 0x40, 10); truelight@0: } truelight@0: tron@3877: static void TownActionAdvertiseMedium(Town* t) tron@3877: { tron@3877: ModifyStationRatingAround(t->xy, _current_player, 0x70, 15); tron@3877: } tron@3877: tron@3877: static void TownActionAdvertiseLarge(Town* t) tron@3877: { tron@3877: ModifyStationRatingAround(t->xy, _current_player, 0xA0, 20); tron@3877: } tron@3877: tron@3877: static void TownActionRoadRebuild(Town* t) truelight@0: { tron@2548: const Player* p; truelight@0: truelight@0: t->road_build_months = 6; truelight@193: tron@534: SetDParam(0, t->index); truelight@0: celestar@1962: p = GetPlayer(_current_player); tron@534: SetDParam(1, p->name_1); tron@534: SetDParam(2, p->name_2); truelight@0: truelight@193: AddNewsItem(STR_2055_TRAFFIC_CHAOS_IN_ROAD_REBUILDING, truelight@0: NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_GENERAL, 0), t->xy, 0); truelight@0: } truelight@0: tron@1977: static bool DoBuildStatueOfCompany(TileIndex tile) truelight@0: { tron@2498: PlayerID old; truelight@0: int32 r; truelight@0: tron@3636: if (GetTileSlope(tile, NULL) != SLOPE_FLAT) return false; truelight@0: tron@2986: if (!IsTileType(tile, MP_HOUSE) && tron@2986: !IsTileType(tile, MP_CLEAR) && tron@2986: !IsTileType(tile, MP_TREES)) { truelight@0: return false; tron@2986: } truelight@0: truelight@0: old = _current_player; truelight@0: _current_player = OWNER_NONE; tron@3491: r = DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@0: _current_player = old; truelight@0: tron@2646: if (CmdFailed(r)) return false; truelight@0: tron@3310: MakeStatue(tile, _current_player); tron@3310: MarkTileDirtyByTile(tile); truelight@0: truelight@0: return true; truelight@0: } truelight@0: belugas@5118: /** belugas@5118: * Search callback function for TownActionBuildStatue belugas@5118: * @param data that is passed by the caller. In this case, nothing belugas@5118: * @result of the test belugas@5118: */ belugas@5118: static bool SearchTileForStatue(TileIndex tile, uint32 data) belugas@5118: { belugas@5118: return DoBuildStatueOfCompany(tile); belugas@5118: } belugas@5118: belugas@5118: /** belugas@5118: * Perform a 9x9 tiles circular search from the center of the town belugas@5118: * in order to find a free tile to place a statue belugas@5118: * @param t town to search in belugas@5118: */ tron@3877: static void TownActionBuildStatue(Town* t) truelight@0: { tron@1977: TileIndex tile = t->xy; truelight@0: belugas@5118: if (CircularTileSearch(tile, 9, SearchTileForStatue, 0)) belugas@5118: SETBIT(t->statues, _current_player); ///< Once found and built, "inform" the Town truelight@0: } truelight@0: tron@3877: static void TownActionFundBuildings(Town* t) truelight@0: { belugas@3432: // Build next tick truelight@0: t->grow_counter = 1; belugas@3432: // If we were not already growing belugas@3432: SETBIT(t->flags12, TOWN_IS_FUNDED); belugas@3432: // And grow for 3 months truelight@0: t->fund_buildings_months = 3; truelight@0: } truelight@0: tron@3877: static void TownActionBuyRights(Town* t) truelight@0: { dominik@121: t->exclusive_counter = 12; dominik@121: t->exclusivity = _current_player; truelight@0: truelight@0: ModifyStationRatingAround(t->xy, _current_player, 130, 17); truelight@0: } truelight@0: tron@3877: static void TownActionBribe(Town* t) truelight@0: { truelight@0: if (!RandomRange(15)) { truelight@0: Station *st; truelight@0: truelight@0: // set as unwanted for 6 months truelight@0: t->unwanted[_current_player] = 6; truelight@0: truelight@0: // set all close by station ratings to 0 truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->town == t && st->owner == _current_player) { tron@2549: uint i; tron@2549: tron@2549: for (i = 0; i != NUM_CARGO; i++) st->goods[i].rating = 0; truelight@0: } truelight@0: } truelight@0: truelight@0: // only show errormessage to the executing player. All errors are handled command.c truelight@0: // but this is special, because it can only 'fail' on a DC_EXEC Darkvater@2425: if (IsLocalPlayer()) ShowErrorMessage(STR_BRIBE_FAILED_2, STR_BRIBE_FAILED, 0, 0); truelight@0: rubidium@4434: /* decrease by a lot! rubidium@4434: * ChangeTownRating is only for stuff in demolishing. Bribe failure should rubidium@4434: * be independent of any cheat settings truelight@0: */ tron@2549: if (t->ratings[_current_player] > RATING_BRIBE_DOWN_TO) { celestar@1005: t->ratings[_current_player] = RATING_BRIBE_DOWN_TO; tron@2549: } truelight@0: } else { celestar@1005: ChangeTownRating(t, RATING_BRIBE_UP_STEP, RATING_BRIBE_MAXIMUM); truelight@0: } truelight@0: } truelight@0: tron@3877: typedef void TownActionProc(Town* t); truelight@0: static TownActionProc * const _town_action_proc[] = { tron@3877: TownActionAdvertiseSmall, tron@3877: TownActionAdvertiseMedium, tron@3877: TownActionAdvertiseLarge, truelight@0: TownActionRoadRebuild, truelight@0: TownActionBuildStatue, truelight@0: TownActionFundBuildings, truelight@0: TownActionBuyRights, truelight@0: TownActionBribe truelight@0: }; truelight@0: Darkvater@1793: extern uint GetMaskOfTownActions(int *nump, PlayerID pid, const Town *t); Darkvater@1793: Darkvater@1793: /** Do a town action. Darkvater@1793: * This performs an action such as advertising, building a statue, funding buildings, Darkvater@1793: * but also bribing the town-council tron@3491: * @param tile unused Darkvater@1793: * @param p1 town to do the action at Darkvater@1793: * @param p2 action to perform, @see _town_action_proc for the list of available actions Darkvater@1793: */ tron@3491: int32 CmdDoTownAction(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: int32 cost; Darkvater@1793: Town *t; Darkvater@1793: truelight@4352: if (!IsValidTownID(p1) || p2 > lengthof(_town_action_proc)) return CMD_ERROR; Darkvater@1793: Darkvater@1793: t = GetTown(p1); Darkvater@1793: Darkvater@1793: if (!HASBIT(GetMaskOfTownActions(NULL, _current_player, t), p2)) return CMD_ERROR; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_OTHER); truelight@0: truelight@0: cost = (_price.build_industry >> 8) * _town_action_costs[p2]; truelight@0: truelight@0: if (flags & DC_EXEC) { tron@3877: _town_action_proc[p2](t); truelight@0: InvalidateWindow(WC_TOWN_AUTHORITY, p1); truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: static void UpdateTownGrowRate(Town *t) truelight@0: { truelight@0: int n; truelight@0: Station *st; truelight@0: byte m; truelight@0: Player *p; truelight@0: truelight@0: // Reset player ratings if they're low truelight@0: FOR_ALL_PLAYERS(p) { truelight@0: if (p->is_active && t->ratings[p->index] <= 200) { truelight@0: t->ratings[p->index] += 5; truelight@0: } truelight@0: } truelight@0: truelight@0: n = 0; truelight@0: FOR_ALL_STATIONS(st) { tron@1245: if (DistanceSquare(st->xy, t->xy) <= t->radius[0]) { truelight@0: if (st->time_since_load <= 20 || st->time_since_unload <= 20) { truelight@0: n++; Darkvater@4850: if (IsValidPlayer(st->owner) && t->ratings[st->owner] <= 1000-12) truelight@0: t->ratings[st->owner] += 12; truelight@0: } else { Darkvater@4850: if (IsValidPlayer(st->owner) && t->ratings[st->owner] >= -1000+15) truelight@0: t->ratings[st->owner] -= 15; truelight@0: } truelight@0: } truelight@0: } truelight@0: belugas@3432: CLRBIT(t->flags12, TOWN_IS_FUNDED); truelight@0: truelight@0: if (t->fund_buildings_months != 0) { pasky@470: static const byte _grow_count_values[6] = { pasky@470: 60, 60, 60, 50, 40, 30 pasky@470: }; pasky@470: m = _grow_count_values[min(n, 5)]; truelight@0: t->fund_buildings_months--; truelight@0: } else if (n == 0) { truelight@0: m = 160; truelight@0: if (!CHANCE16(1, 12)) truelight@0: return; truelight@0: } else { truelight@0: static const byte _grow_count_values[5] = { truelight@0: 210, 150, 110, 80, 50 truelight@0: }; truelight@0: m = _grow_count_values[min(n, 5) - 1]; truelight@0: } truelight@0: truelight@0: if (_opt.landscape == LT_HILLY) { rubidium@4434: if (TilePixelHeight(t->xy) >= _opt.snow_line && t->act_food == 0 && t->population > 90) truelight@0: return; truelight@0: } else if (_opt.landscape == LT_DESERT) { rubidium@4434: if (GetTropicZone(t->xy) == TROPICZONE_DESERT && (t->act_food==0 || t->act_water==0) && t->population > 60) truelight@0: return; truelight@0: } truelight@0: rubidium@4434: t->growth_rate = m / (t->num_houses / 50 + 1); truelight@0: if (m <= t->grow_counter) truelight@0: t->grow_counter = m; truelight@0: belugas@3432: SETBIT(t->flags12, TOWN_IS_FUNDED); truelight@0: } truelight@0: truelight@0: static void UpdateTownAmounts(Town *t) truelight@0: { truelight@0: // Using +1 here to prevent overflow and division by zero truelight@0: t->pct_pass_transported = t->new_act_pass * 256 / (t->new_max_pass + 1); truelight@0: truelight@0: t->max_pass = t->new_max_pass; t->new_max_pass = 0; truelight@0: t->act_pass = t->new_act_pass; t->new_act_pass = 0; truelight@0: t->act_food = t->new_act_food; t->new_act_food = 0; darkvater@4: t->act_water = t->new_act_water; t->new_act_water = 0; truelight@0: truelight@0: // Using +1 here to prevent overflow and division by zero truelight@0: t->pct_mail_transported = t->new_act_mail * 256 / (t->new_max_mail + 1); truelight@0: t->max_mail = t->new_max_mail; t->new_max_mail = 0; truelight@0: t->act_mail = t->new_act_mail; t->new_act_mail = 0; truelight@0: truelight@0: InvalidateWindow(WC_TOWN_VIEW, t->index); truelight@0: } truelight@0: truelight@0: static void UpdateTownUnwanted(Town *t) truelight@0: { tron@4000: const Player* p; truelight@0: truelight@0: FOR_ALL_PLAYERS(p) { tron@4000: if (t->unwanted[p->index] > 0) t->unwanted[p->index]--; truelight@0: } truelight@0: } truelight@0: tron@1977: bool CheckIfAuthorityAllows(TileIndex tile) truelight@0: { truelight@0: Town *t; truelight@0: Darkvater@4850: if (!IsValidPlayer(_current_player)) return true; truelight@0: truelight@0: t = ClosestTownFromTile(tile, _patches.dist_local_authority); tron@4000: if (t == NULL) return true; truelight@0: tron@4000: if (t->ratings[_current_player] > -200) return true; truelight@0: truelight@0: _error_message = STR_2009_LOCAL_AUTHORITY_REFUSES; tron@534: SetDParam(0, t->index); truelight@0: truelight@0: return false; truelight@0: } truelight@0: truelight@0: tron@3983: Town* CalcClosestTownFromTile(TileIndex tile, uint threshold) truelight@0: { truelight@0: Town *t; truelight@0: uint dist, best = threshold; truelight@0: Town *best_town = NULL; truelight@193: truelight@0: FOR_ALL_TOWNS(t) { truelight@4346: dist = DistanceManhattan(tile, t->xy); truelight@4346: if (dist < best) { truelight@4346: best = dist; truelight@4346: best_town = t; truelight@0: } truelight@0: } truelight@0: truelight@0: return best_town; truelight@0: } truelight@0: tron@3983: tron@3983: Town *ClosestTownFromTile(TileIndex tile, uint threshold) tron@3983: { tron@3983: if (IsTileType(tile, MP_HOUSE) || ( tron@3983: IsTileType(tile, MP_STREET) && tron@3983: (IsLevelCrossing(tile) ? GetCrossingRoadOwner(tile) : GetTileOwner(tile)) == OWNER_TOWN tron@3983: )) { tron@3983: return GetTownByTile(tile); tron@3983: } else { tron@3983: return CalcClosestTownFromTile(tile, threshold); tron@3983: } tron@3983: } tron@3983: tron@3983: truelight@0: void ChangeTownRating(Town *t, int add, int max) truelight@0: { truelight@0: int rating; truelight@0: rubidium@4434: // if magic_bulldozer cheat is active, town doesn't penaltize for removing stuff tron@2639: if (t == NULL || Darkvater@4850: !IsValidPlayer(_current_player) || tron@2639: (_cheats.magic_bulldozer.value && add < 0)) { truelight@0: return; tron@2639: } truelight@0: truelight@0: SETBIT(t->have_ratings, _current_player); truelight@193: truelight@0: rating = t->ratings[_current_player]; truelight@193: truelight@0: if (add < 0) { truelight@0: if (rating > max) { truelight@0: rating += add; truelight@0: if (rating < max) rating = max; truelight@0: } truelight@0: } else { truelight@0: if (rating < max) { truelight@0: rating += add; truelight@0: if (rating > max) rating = max; truelight@0: } truelight@0: } truelight@0: t->ratings[_current_player] = rating; truelight@0: } truelight@0: rubidium@4434: /* penalty for removing town-owned stuff */ truelight@0: static const int _default_rating_settings [3][3] = { truelight@0: // ROAD_REMOVE, TUNNELBRIDGE_REMOVE, INDUSTRY_REMOVE rubidium@4344: { 0, 128, 384}, // Permissive rubidium@4344: { 48, 192, 480}, // Neutral rubidium@4344: { 96, 384, 768}, // Hostile truelight@0: }; truelight@0: tron@2958: bool CheckforTownRating(uint32 flags, Town *t, byte type) truelight@0: { truelight@0: int modemod; truelight@0: rubidium@4434: // if magic_bulldozer cheat is active, town doesn't restrict your destructive actions Darkvater@4850: if (t == NULL || !IsValidPlayer(_current_player) || _cheats.magic_bulldozer.value) truelight@0: return true; truelight@0: rubidium@4434: /* check if you're allowed to remove the street/bridge/tunnel/industry rubidium@4434: * owned by a town no removal if rating is lower than ... depends now on rubidium@4434: * difficulty setting. Minimum town rating selected by difficulty level truelight@0: */ Darkvater@1500: modemod = _default_rating_settings[_opt.diff.town_council_tolerance][type]; truelight@0: truelight@0: if (t->ratings[_current_player] < 16 + modemod && !(flags & DC_NO_TOWN_RATING)) { tron@534: SetDParam(0, t->index); truelight@0: _error_message = STR_2009_LOCAL_AUTHORITY_REFUSES; truelight@0: return false; truelight@0: } truelight@0: truelight@0: return true; truelight@0: } truelight@0: tron@1093: void TownsMonthlyLoop(void) truelight@0: { truelight@0: Town *t; truelight@0: truelight@4346: FOR_ALL_TOWNS(t) { tron@4000: if (t->road_build_months != 0) t->road_build_months--; truelight@0: dominik@121: if (t->exclusive_counter != 0) tron@4000: if (--t->exclusive_counter == 0) t->exclusivity = (byte)-1; dominik@121: truelight@0: UpdateTownGrowRate(t); truelight@0: UpdateTownAmounts(t); truelight@0: UpdateTownUnwanted(t); truelight@0: } truelight@0: } truelight@0: tron@1093: void InitializeTowns(void) truelight@0: { truelight@0: Subsidy *s; truelight@0: truelight@1260: /* Clean the town pool and create 1 block in it */ tron@4983: CleanPool(&_Town_pool); tron@4983: AddBlockToPool(&_Town_pool); truelight@0: truelight@0: memset(_subsidies, 0, sizeof(_subsidies)); truelight@0: for (s=_subsidies; s != endof(_subsidies); s++) tron@2469: s->cargo_type = CT_INVALID; truelight@0: truelight@0: _cur_town_ctr = 0; pasky@1529: _cur_town_iter = 0; truelight@1260: _total_towns = 0; truelight@0: _town_sort_dirty = true; truelight@0: } truelight@0: truelight@0: const TileTypeProcs _tile_type_town_procs = { rubidium@4344: DrawTile_Town, /* draw_tile_proc */ rubidium@4344: GetSlopeZ_Town, /* get_slope_z_proc */ rubidium@4344: ClearTile_Town, /* clear_tile_proc */ rubidium@4344: GetAcceptedCargo_Town, /* get_accepted_cargo_proc */ rubidium@4344: GetTileDesc_Town, /* get_tile_desc_proc */ rubidium@4344: GetTileTrackStatus_Town, /* get_tile_track_status_proc */ rubidium@4344: ClickTile_Town, /* click_tile_proc */ rubidium@4344: AnimateTile_Town, /* animate_tile_proc */ rubidium@4344: TileLoop_Town, /* tile_loop_clear */ rubidium@4344: ChangeTileOwner_Town, /* change_tile_owner_clear */ rubidium@4344: NULL, /* get_produced_cargo_proc */ rubidium@4344: NULL, /* vehicle_enter_tile_proc */ rubidium@4344: GetSlopeTileh_Town, /* get_slope_tileh_proc */ truelight@0: }; truelight@0: truelight@0: truelight@0: // Save and load of towns. Darkvater@1881: static const SaveLoad _town_desc[] = { rubidium@4344: SLE_CONDVAR(Town, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), rubidium@4344: SLE_CONDVAR(Town, xy, SLE_UINT32, 6, SL_MAX_VERSION), truelight@193: rubidium@4344: SLE_CONDVAR(Town, population, SLE_FILE_U16 | SLE_VAR_U32, 0, 2), rubidium@4344: SLE_CONDVAR(Town, population, SLE_UINT32, 3, SL_MAX_VERSION), truelight@0: truelight@0: rubidium@4344: SLE_VAR(Town, num_houses, SLE_UINT16), rubidium@4344: SLE_VAR(Town, townnametype, SLE_UINT16), rubidium@4344: SLE_VAR(Town, townnameparts, SLE_UINT32), truelight@0: rubidium@4344: SLE_VAR(Town, flags12, SLE_UINT8), rubidium@4344: SLE_VAR(Town, statues, SLE_UINT8), truelight@0: truelight@0: // sort_index_obsolete was stored here in savegame format 0 - 1 Darkvater@3222: SLE_CONDNULL(1, 0, 1), truelight@0: rubidium@4344: SLE_VAR(Town, have_ratings, SLE_UINT8), rubidium@4344: SLE_ARR(Town, ratings, SLE_INT16, 8), truelight@0: // failed bribe attempts are stored since savegame format 4 rubidium@4344: SLE_CONDARR(Town, unwanted, SLE_INT8, 8, 4,SL_MAX_VERSION), celestar@1377: rubidium@4344: SLE_CONDVAR(Town, max_pass, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), rubidium@4344: SLE_CONDVAR(Town, max_mail, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), rubidium@4344: SLE_CONDVAR(Town, new_max_pass, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), rubidium@4344: SLE_CONDVAR(Town, new_max_mail, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), rubidium@4344: SLE_CONDVAR(Town, act_pass, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), rubidium@4344: SLE_CONDVAR(Town, act_mail, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), rubidium@4344: SLE_CONDVAR(Town, new_act_pass, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), rubidium@4344: SLE_CONDVAR(Town, new_act_mail, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), truelight@0: rubidium@4344: SLE_CONDVAR(Town, max_pass, SLE_UINT32, 9, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Town, max_mail, SLE_UINT32, 9, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Town, new_max_pass, SLE_UINT32, 9, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Town, new_max_mail, SLE_UINT32, 9, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Town, act_pass, SLE_UINT32, 9, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Town, act_mail, SLE_UINT32, 9, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Town, new_act_pass, SLE_UINT32, 9, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Town, new_act_mail, SLE_UINT32, 9, SL_MAX_VERSION), truelight@0: rubidium@4344: SLE_VAR(Town, pct_pass_transported, SLE_UINT8), rubidium@4344: SLE_VAR(Town, pct_mail_transported, SLE_UINT8), truelight@0: rubidium@4344: SLE_VAR(Town, act_food, SLE_UINT16), rubidium@4344: SLE_VAR(Town, act_water, SLE_UINT16), rubidium@4344: SLE_VAR(Town, new_act_food, SLE_UINT16), rubidium@4344: SLE_VAR(Town, new_act_water, SLE_UINT16), rubidium@4344: rubidium@4344: SLE_VAR(Town, time_until_rebuild, SLE_UINT8), rubidium@4344: SLE_VAR(Town, grow_counter, SLE_UINT8), rubidium@4344: SLE_VAR(Town, growth_rate, SLE_UINT8), rubidium@4344: SLE_VAR(Town, fund_buildings_months, SLE_UINT8), rubidium@4344: SLE_VAR(Town, road_build_months, SLE_UINT8), rubidium@4344: rubidium@4344: SLE_VAR(Town, exclusivity, SLE_UINT8), rubidium@4344: SLE_VAR(Town, exclusive_counter, SLE_UINT8), dominik@121: // reserve extra space in savegame here. (currently 30 bytes) Darkvater@3222: SLE_CONDNULL(30, 2, SL_MAX_VERSION), truelight@0: truelight@0: SLE_END() truelight@0: }; truelight@0: tron@1093: static void Save_TOWN(void) truelight@0: { truelight@0: Town *t; truelight@0: truelight@919: FOR_ALL_TOWNS(t) { truelight@4346: SlSetArrayIndex(t->index); truelight@4346: SlObject(t, _town_desc); truelight@0: } truelight@0: } truelight@0: tron@1093: static void Load_TOWN(void) truelight@0: { truelight@0: int index; truelight@919: truelight@1261: _total_towns = 0; truelight@1261: truelight@1260: while ((index = SlIterateArray()) != -1) { truelight@1260: Town *t; truelight@1260: tron@4983: if (!AddBlockIfNeeded(&_Town_pool, index)) truelight@1260: error("Towns: failed loading savegame: too many towns"); truelight@1260: truelight@1260: t = GetTown(index); truelight@0: SlObject(t, _town_desc); truelight@1260: rubidium@5298: _total_towns++; truelight@0: } truelight@1260: truelight@1260: /* This is to ensure all pointers are within the limits of truelight@1260: * the size of the TownPool */ matthijs@5247: if (_cur_town_ctr > GetMaxTownIndex()) truelight@1260: _cur_town_ctr = 0; truelight@0: } truelight@0: tron@1093: void AfterLoadTown(void) truelight@0: { truelight@0: Town *t; truelight@0: FOR_ALL_TOWNS(t) { truelight@4346: UpdateTownRadius(t); truelight@4346: UpdateTownVirtCoord(t); truelight@0: } truelight@0: _town_sort_dirty = true; truelight@0: } truelight@0: truelight@0: truelight@0: const ChunkHandler _town_chunk_handlers[] = { truelight@0: { 'CITY', Save_TOWN, Load_TOWN, CH_ARRAY | CH_LAST}, truelight@0: };