truelight@0: #include "stdafx.h" truelight@0: #include "ttd.h" tron@507: #include "table/strings.h" tron@679: #include "map.h" truelight@0: #include "viewport.h" truelight@0: #include "town.h" truelight@0: #include "command.h" truelight@0: #include "pathfind.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" truelight@543: #include "network.h" truelight@0: tron@483: enum { tron@483: TOWN_HAS_CHURCH = 0x02, tron@483: TOWN_HAS_STADIUM = 0x04 tron@483: }; tron@483: truelight@0: // Local truelight@0: static int _grow_town_result; truelight@0: truelight@0: static bool BuildTownHouse(Town *t, uint tile); truelight@0: static void ClearTownHouse(Town *t, uint tile); truelight@0: static void DoBuildTownHouse(Town *t, uint tile); truelight@0: truelight@0: typedef struct DrawTownTileStruct { truelight@0: uint32 sprite_1; truelight@0: uint32 sprite_2; truelight@0: tron@482: byte subtile_x:4; tron@482: byte subtile_y:4; tron@482: byte width:4; tron@482: byte height:4; truelight@0: byte dz; truelight@0: byte proc; truelight@0: } DrawTownTileStruct; truelight@0: truelight@0: #include "table/town_land.h" truelight@0: truelight@0: truelight@0: static void TownDrawTileProc1(TileInfo *ti) truelight@0: { truelight@0: AddChildSpriteScreen(0x5A3, 0xE, 0x3C - (_map_owner[ti->tile]&0x7F)); truelight@0: } truelight@0: truelight@0: typedef void TownDrawTileProc(TileInfo *ti); truelight@0: static TownDrawTileProc * const _town_draw_tile_procs[1] = { truelight@0: TownDrawTileProc1 truelight@0: }; truelight@0: truelight@0: truelight@0: static void DrawTile_Town(TileInfo *ti) truelight@0: { truelight@0: const DrawTownTileStruct *dcts; truelight@0: byte z; 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 */ truelight@817: byte gfx = (byte)_map2[ti->tile]; tron@480: byte stage = _map3_lo[ti->tile] >> 6; 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; tron@480: dcts = &_town_draw_tile_data[gfx << 4 | variant << 2 | stage]; truelight@0: } truelight@0: truelight@0: z = ti->z; truelight@0: truelight@0: /* Add bricks below the house? */ truelight@0: if (ti->tileh) { truelight@0: AddSortableSpriteToDraw(0x3DD + ti->tileh, ti->x, ti->y, 16, 16, 7, z); truelight@0: AddChildSpriteScreen(dcts->sprite_1, 0x1F, 1); truelight@0: z += 8; truelight@0: } else { truelight@0: /* Else draw regular ground */ truelight@0: DrawGroundSprite(dcts->sprite_1); truelight@0: } truelight@0: truelight@0: /* Add a house on top of the ground? */ truelight@0: if ((image = dcts->sprite_2) != 0) { tron@497: if (_display_opt & DO_TRANS_BUILDINGS) truelight@0: image = (image & 0x3FFF) | 0x3224000; 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, truelight@0: z); truelight@0: tron@497: if (_display_opt & DO_TRANS_BUILDINGS) truelight@0: return; truelight@0: } truelight@0: truelight@0: { truelight@0: int proc; truelight@0: if ((proc=dcts->proc-1) >= 0 ) truelight@0: _town_draw_tile_procs[proc](ti); truelight@0: } truelight@0: } truelight@0: truelight@0: static uint GetSlopeZ_Town(TileInfo *ti) truelight@0: { truelight@0: uint z = GetPartialZ(ti->x&0xF, ti->y&0xF, ti->tileh) + ti->z; truelight@0: if (ti->tileh != 0) z = (z & ~7) + 4; truelight@193: return (uint16) z; truelight@0: } truelight@0: dominik@39: static uint GetSlopeTileh_Town(TileInfo *ti) dominik@39: { dominik@39: return ti->tileh; dominik@39: } dominik@39: truelight@0: static void AnimateTile_Town(uint tile) truelight@0: { truelight@0: int old; truelight@0: int i; truelight@0: int a,b; truelight@0: truelight@0: if (_tick_counter & 3) truelight@0: return; truelight@0: truelight@0: if (_map2[tile] != 4 && _map2[tile] != 5) truelight@0: return; truelight@0: truelight@0: if (!((old=_map_owner[tile])&0x80)) { truelight@0: _map_owner[tile] |= 0x80; truelight@0: truelight@0: do { truelight@0: i = (Random()&7) - 1; truelight@0: } while (i < 0 || i == 1 || i*6==old); truelight@0: truelight@0: _map5[tile] = (_map5[tile] & ~0x3F) | i; truelight@0: } truelight@0: truelight@0: a = _map_owner[tile]&0x7F; truelight@0: b = (_map5[tile]&0x3F) * 6; truelight@0: a += (a < b) ? 1 : -1; truelight@0: _map_owner[tile] = (_map_owner[tile]&0x80)|a; truelight@0: truelight@0: if (a == b) { truelight@0: _map5[tile] &= 0x40; truelight@0: _map_owner[tile] &= 0x7F; truelight@0: DeleteAnimatedTile(tile); truelight@0: } truelight@193: truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: truelight@0: static void UpdateTownRadius(Town *t); truelight@0: truelight@0: static bool IsCloseToTown(uint tile, uint dist) truelight@0: { truelight@0: Town *t; truelight@0: truelight@0: FOR_ALL_TOWNS(t) { truelight@0: if (t->xy != 0 && GetTileDist(tile, t->xy) < dist) truelight@0: 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); tron@926: pt = RemapCoords2(TileX(t->xy) * 16, TileY(t->xy) * 16); truelight@835: SetDParam(0, t->townnametype); truelight@835: SetDParam(1, t->townnameparts); truelight@835: SetDParam(2, t->population); truelight@835: UpdateViewportSignPos(&t->sign, pt.x, pt.y - 24, _patches.population_in_label ? STR_TOWN_LABEL_POP : STR_2001); 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; celestar@1080: Town *t; celestar@1080: pop = 0; celestar@1080: FOR_ALL_TOWNS(t) { celestar@1080: pop += t->population; celestar@1080: } celestar@1080: return pop; celestar@1080: } celestar@1080: truelight@0: static void MakeSingleHouseBigger(uint tile) truelight@0: { truelight@0: byte b; truelight@193: tron@1035: assert(IsTileType(tile, MP_HOUSE)); truelight@193: truelight@0: b = _map5[tile]; truelight@0: if (b & 0x80) truelight@0: return; truelight@0: truelight@0: _map5[tile] = (b & 0xC0) | ((b+1)&7); truelight@0: truelight@0: if ((_map5[tile]&7) != 0) truelight@0: return; truelight@0: truelight@0: _map3_lo[tile] = _map3_lo[tile] + 0x40; truelight@0: truelight@0: if ( (_map3_lo[tile] & 0xC0) == 0xC0) { truelight@0: Town *t = ClosestTownFromTile(tile, (uint)-1); truelight@193: ChangePopulation(t, _housetype_population[_map2[tile]]); truelight@0: } truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: truelight@0: static void MakeTownHouseBigger(uint tile) truelight@0: { truelight@193: uint flags = _house_more_flags[_map2[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: truelight@0: static void TileLoop_Town(uint tile) truelight@0: { truelight@0: int house; truelight@0: Town *t; truelight@0: uint32 r; truelight@0: truelight@0: if ((_map3_lo[tile] & 0xC0) != 0xC0) { truelight@0: MakeTownHouseBigger(tile); truelight@0: return; truelight@0: } truelight@0: truelight@0: house = _map2[tile]; truelight@0: if (_housetype_extra_flags[house] & 0x20 && truelight@0: !(_map5[tile] & 0x80) && truelight@0: CHANCE16(1,2) && truelight@0: AddAnimatedTile(tile)) { truelight@0: _map5[tile] = (_map5[tile] & 0x40)|0x80; truelight@0: } truelight@0: truelight@0: t = ClosestTownFromTile(tile, (uint)-1); truelight@0: truelight@0: r = Random(); truelight@0: truelight@0: if ( (byte)r < _housetype_population[house] ) { truelight@0: uint amt = ((byte)r >> 3) + 1, moved; 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: truelight@0: if ( (byte)(r>>8) < _housetype_mailamount[house] ) { truelight@0: uint amt = ((byte)(r>>8) >> 3) + 1, moved; 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: truelight@0: if (_house_more_flags[house]&8 && (t->flags12&1) && --t->time_until_rebuild == 0) { truelight@0: r>>=16; truelight@0: t->time_until_rebuild = (r & 63) + 130; truelight@0: truelight@0: _current_player = OWNER_TOWN; truelight@0: truelight@0: ClearTownHouse(t, tile); truelight@193: truelight@0: // rebuild with another house? truelight@0: if ( (byte) (r >> 8) >= 12) { truelight@0: DoBuildTownHouse(t, tile); truelight@0: } truelight@314: truelight@314: _current_player = OWNER_NONE; truelight@0: } truelight@0: } truelight@0: truelight@0: static void ClickTile_Town(uint tile) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: truelight@0: static int32 ClearTile_Town(uint 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: truelight@0: house = _map2[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; truelight@0: _cleared_town = t = ClosestTownFromTile(tile, (uint)-1); truelight@193: truelight@0: if (_current_player < MAX_PLAYERS) { 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@473: static void GetAcceptedCargo_Town(uint tile, AcceptedCargo ac) truelight@0: { truelight@0: int type = _map2[tile]; truelight@193: tron@473: ac[CT_PASSENGERS] = _housetype_cargo_passengers[type]; tron@473: ac[CT_MAIL] = _housetype_cargo_mail[type]; tron@473: ac[CT_GOODS] = _housetype_cargo_goods[type]; tron@473: ac[CT_FOOD] = _housetype_cargo_food[type]; truelight@0: } truelight@0: truelight@0: static void GetTileDesc_Town(uint tile, TileDesc *td) truelight@0: { truelight@0: td->str = _town_tile_names[_map2[tile]]; truelight@0: if ((_map3_lo[tile] & 0xC0) != 0xC0) { 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: truelight@159: static uint32 GetTileTrackStatus_Town(uint tile, TransportType mode) truelight@0: { truelight@0: /* not used */ truelight@0: return 0; truelight@0: } truelight@0: truelight@0: static void ChangeTileOwner_Town(uint tile, byte old_player, byte 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: truelight@0: static void TownTickHandler(Town *t) truelight@0: { truelight@0: if (t->flags12&1) { 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: { truelight@0: uint i; truelight@0: Town *t; truelight@0: truelight@0: if (_game_mode == GM_EDITOR) truelight@0: return; truelight@0: truelight@0: i = _cur_town_ctr; truelight@919: t = GetTown(i); truelight@919: if (++i == _towns_size) i = 0; truelight@0: _cur_town_ctr = i; truelight@0: truelight@0: if (t->xy != 0) truelight@0: TownTickHandler(t); truelight@0: truelight@0: } truelight@0: truelight@0: static byte GetTownRoadMask(TileIndex tile) truelight@0: { pasky@516: byte b = GetRoadBitsByTile(tile); truelight@0: byte r=0; truelight@0: if (b&1) r|=10; truelight@0: if (b&2) r|=5; truelight@0: if (b&4) r|=9; truelight@0: if (b&8) r|=6; truelight@0: if (b&16) r|=3; truelight@0: if (b&32) r|=12; truelight@0: return r; truelight@0: } truelight@0: truelight@0: static bool IsRoadAllowedHere(uint tile, int dir) truelight@0: { truelight@0: uint k; truelight@0: uint 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: truelight@0: for(;;) { truelight@0: // Check if there already is a road at this point? pasky@516: if (GetRoadBitsByTile(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. truelight@0: if (DoCommandByTile(tile, (dir&1)?0xA:0x5, 0, DC_AUTO, CMD_BUILD_ROAD) == CMD_ERROR && truelight@0: DoCommandByTile(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR) == CMD_ERROR) truelight@0: return false; truelight@0: } truelight@0: truelight@0: slope = GetTileSlope(tile, NULL); truelight@0: if (slope == 0) { 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. truelight@0: if ((k = (dir&1)?0xC:0x9) != slope && (k^0xF) != slope) { truelight@0: uint32 r = Random(); truelight@0: truelight@0: if (CHANCE16I(1,8, r) && !_generating_world) { pasky@465: int32 res; pasky@465: truelight@0: if (CHANCE16I(1,16,r)) pasky@465: res = DoCommandByTile(tile, slope, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, pasky@465: CMD_TERRAFORM_LAND); truelight@0: else pasky@465: res = DoCommandByTile(tile, slope^0xF, 1, DC_EXEC | DC_AUTO | DC_NO_WATER, pasky@465: CMD_TERRAFORM_LAND); pasky@465: if (res == CMD_ERROR && CHANCE16I(1,3,r)) pasky@465: // We can consider building on the slope, though. pasky@465: goto no_slope; truelight@0: } truelight@0: return false; truelight@0: } truelight@0: truelight@835: /* Can somebody explain for what this is needed? :s */ truelight@835: // tile = TILE_ADD(tile, _roadblock_tileadd[dir]); truelight@835: return true; truelight@0: } truelight@0: } truelight@0: truelight@0: static bool TerraformTownTile(uint tile, int edges, int dir) truelight@0: { truelight@0: int32 r; truelight@193: truelight@0: TILE_ASSERT(tile); truelight@0: truelight@0: r = DoCommandByTile(tile, edges, dir, DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND); truelight@0: if (r == CMD_ERROR || r >= 126*16) truelight@0: return false; truelight@0: DoCommandByTile(tile, edges, dir, DC_AUTO | DC_NO_WATER | DC_EXEC, CMD_TERRAFORM_LAND); truelight@0: return true; truelight@0: } truelight@0: truelight@0: static void LevelTownLand(uint tile) truelight@0: { truelight@0: TileInfo ti; 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. truelight@0: FindLandscapeHeightByTile(&ti, tile); truelight@0: if (ti.tileh == 0 || ti.type == MP_HOUSE) truelight@0: return; truelight@0: truelight@0: // First try up, then down truelight@0: if (!TerraformTownTile(tile, ~ti.tileh & 0xF, 1)) { truelight@0: TerraformTownTile(tile, ti.tileh & 0xF, 0); truelight@0: } truelight@0: } truelight@0: tron@1035: #define IS_WATER_TILE(t) (IsTileType((t), MP_WATER) && _map5[(t)] == 0) truelight@0: truelight@0: static void GrowTownInTile(uint *tile_ptr, uint mask, int block, Town *t1) truelight@0: { truelight@0: Town *t2; truelight@0: uint16 r; truelight@0: int a,b,rcmd; truelight@0: uint tmptile; truelight@0: TileInfo ti; truelight@0: int i; truelight@0: int j; truelight@0: uint tile = *tile_ptr; truelight@0: truelight@0: TILE_ASSERT(tile); truelight@0: truelight@0: if (mask == 0) { 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: // Then check if the tile we are at belongs to the town, truelight@0: // if not, bail out. truelight@0: t2 = ClosestTownFromTile(tile, (uint)-1); truelight@0: if (t2 != t1) truelight@0: return; truelight@0: truelight@0: // Remove hills etc truelight@0: LevelTownLand(tile); truelight@0: truelight@0: // Is a road allowed here? truelight@0: if (!IsRoadAllowedHere(tile, block)) truelight@0: return; truelight@0: truelight@0: // Randomize new road block numbers truelight@0: a = block; truelight@0: b = block ^ 2; truelight@0: r = (uint16)Random(); truelight@0: if (r <= 0x4000) do { truelight@0: a = (int)Random() & 3; truelight@0: } while(a == b); 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. truelight@0: if ( a != (b^2)) truelight@0: 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; truelight@0: rcmd = 1 << (block^2); truelight@0: } else { truelight@0: truelight@0: // Reached a tunnel? Then continue at the other side of it. tron@1035: if (IsTileType(tile, MP_TUNNELBRIDGE) && (_map5[tile]& ~3) == 4) { truelight@159: FindLengthOfTunnelResult flotr = FindLengthOfTunnel(tile, _map5[tile]&3); truelight@0: *tile_ptr = flotr.tile; truelight@0: return; truelight@0: } truelight@193: truelight@0: // For any other kind of tunnel/bridge, bail out. tron@1035: if (IsTileType(tile, MP_TUNNELBRIDGE)) truelight@0: 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. truelight@0: i = (int)Random() & 3; truelight@0: if (HASBIT(mask, i)) truelight@0: 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. truelight@0: if (IS_WATER_TILE(tmptile)) truelight@0: return; truelight@0: truelight@0: // If the new tile belongs to another town, truelight@0: // then stop the search altogether. truelight@0: if (ClosestTownFromTile(tmptile, (uint)-1) != t1) { truelight@0: _grow_town_result = 0; truelight@0: return; truelight@0: } truelight@0: truelight@193: // Build a house at the edge. 60% chance or truelight@0: // always ok if no road allowed. truelight@0: 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. truelight@0: if (BuildTownHouse(t1, tmptile)) truelight@0: _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: FindLandscapeHeightByTile(&ti, tile); truelight@0: truelight@0: // Return if a water tile truelight@0: if (ti.type == MP_WATER && ti.map5==0) truelight@0: return; truelight@0: truelight@0: // Determine direction of slope, truelight@0: // and build a road if not a special slope. truelight@0: if ((i=0,ti.tileh != 3) && truelight@0: (i++,ti.tileh != 9) && truelight@0: (i++,ti.tileh != 12) && truelight@0: (i++,ti.tileh != 6)) { truelight@0: build_road_and_exit: truelight@0: if (DoCommandByTile(tile, rcmd, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD) != CMD_ERROR) truelight@0: _grow_town_result = -1; truelight@0: return; truelight@0: } truelight@0: truelight@0: tmptile = tile; truelight@0: truelight@0: // Now it contains the direction of the slope truelight@0: j = -11; // max 11 tile long bridges truelight@0: do { truelight@0: if (++j == 0) truelight@0: goto build_road_and_exit; tron@900: tmptile = TILE_MASK(tmptile + TileOffsByDir(i)); truelight@0: } while (IS_WATER_TILE(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)) { truelight@0: if (DoCommandByTile(tile, tmptile, 0x8000 + bridge_type, DC_EXEC | DC_AUTO, CMD_BUILD_BRIDGE) != CMD_ERROR) 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: } truelight@0: } while(--j != 0); truelight@0: } truelight@0: } truelight@0: #undef IS_WATER_TILE truelight@0: truelight@0: truelight@0: // Returns true if a house was built, or no if the build failed. truelight@0: static int GrowTownAtRoad(Town *t, uint tile) truelight@0: { truelight@0: uint mask; 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 truelight@0: 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@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@1093: static int GenRandomRoadBits(void) truelight@0: { truelight@0: uint32 r = Random(); truelight@0: int a = r&3, b = (r >> 8) & 3; truelight@0: if (a == b) b ^= 2; truelight@0: return (1<xy; tron@909: for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) { pasky@516: if (GetRoadBitsByTile(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@0: FindLandscapeHeightByTile(&ti, tile); truelight@193: truelight@0: // Only work with plain land that not already has a house with map5=0 truelight@0: if (ti.tileh == 0 && !(ti.type==MP_HOUSE && ti.map5==0)) { truelight@0: if (DoCommandByTile(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR) != CMD_ERROR) { truelight@0: DoCommandByTile(tile, GenRandomRoadBits(), 0, 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] = { pasky@470: { 4, 0, 0, 0, 0}, // 0 truelight@0: {16, 0, 0, 0, 0}, truelight@0: {25, 0, 0, 0, 0}, truelight@0: {36, 0, 0, 0, 0}, truelight@0: {49, 0, 4, 0, 0}, pasky@470: {64, 0, 4, 0, 0}, // 20 truelight@0: {64, 0, 9, 0, 1}, truelight@0: {64, 0, 9, 0, 4}, truelight@0: {64, 0, 16, 0, 4}, truelight@0: {81, 0, 16, 0, 4}, pasky@470: {81, 0, 16, 0, 4}, // 40 truelight@0: {81, 0, 25, 0, 9}, truelight@0: {81, 36, 25, 0, 9}, truelight@0: {81, 36, 25, 16, 9}, truelight@0: {81, 49, 0, 25, 9}, pasky@470: {81, 64, 0, 25, 9}, // 60 truelight@0: {81, 64, 0, 36, 9}, truelight@0: {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: truelight@0: static void CreateTownName(Town *t1) truelight@0: { truelight@0: Town *t2; truelight@0: char buf1[64]; truelight@0: char buf2[64]; truelight@0: uint32 r; truelight@0: truelight@0: t1->townnametype = SPECSTR_TOWNNAME_START + _opt.town_name; truelight@0: truelight@0: for(;;) { truelight@0: restart: truelight@0: r = Random(); truelight@0: tron@534: SetDParam(0, r); truelight@0: GetString(buf1, t1->townnametype); truelight@193: truelight@0: // Check size and width truelight@0: if (strlen(buf1) >= 31 || GetStringWidth(buf1) > 130) truelight@0: continue; truelight@0: truelight@0: FOR_ALL_TOWNS(t2) { truelight@0: if (t2->xy != 0) { tron@534: SetDParam(0, t2->townnameparts); truelight@0: GetString(buf2, t2->townnametype); truelight@0: if (str_eq(buf1, buf2)) truelight@0: goto restart; truelight@0: } truelight@0: } truelight@0: t1->townnameparts = r; truelight@193: truelight@0: return; truelight@0: } truelight@0: } truelight@0: truelight@0: static 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: truelight@0: static void DoCreateTown(Town *t, TileIndex tile) 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; 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: truelight@0: 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: truelight@0: CreateTownName(t); truelight@193: truelight@0: UpdateTownVirtCoord(t); truelight@0: _town_sort_dirty = true; truelight@0: truelight@0: x = (Random() & 0xF) + 8; truelight@0: if (_game_mode == GM_EDITOR) truelight@0: x = _new_town_size * 16 + 3; 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@0: FOR_ALL_TOWNS(t) { truelight@0: if (t->xy == 0) { truelight@543: if (t->index > _total_towns) _total_towns = t->index; truelight@0: return t; truelight@0: } truelight@0: } truelight@0: return NULL; truelight@0: } truelight@0: truelight@0: int32 CmdBuildTown(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: uint tile = TILE_FROM_XY(x,y); truelight@0: TileInfo ti; truelight@0: Town *t; truelight@193: truelight@0: SET_EXPENSES_TYPE(EXPENSES_OTHER); truelight@0: truelight@0: // Check if too close to the edge of map truelight@0: if (!CheckDistanceFromEdge(tile, 12)) truelight@0: return_cmd_error(STR_0237_TOO_CLOSE_TO_EDGE_OF_MAP); truelight@0: truelight@0: // Can only build on clear flat areas. truelight@0: FindLandscapeHeightByTile(&ti, tile); truelight@0: if (ti.type != MP_CLEAR || ti.tileh != 0) truelight@0: return_cmd_error(STR_0239_SITE_UNSUITABLE); 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: truelight@0: // Allocate town struct truelight@0: t = AllocateTown(); truelight@0: if (t == NULL) truelight@193: 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; truelight@0: DoCreateTown(t, tile); truelight@0: _generating_world = false; truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: tron@1093: Town *CreateRandomTown(void) truelight@0: { truelight@0: uint tile; truelight@0: TileInfo ti; truelight@0: Town *t; truelight@0: int n; truelight@0: truelight@0: // Try 20 times. truelight@0: n = 20; truelight@0: do { truelight@0: // Generate a tile index not too close from the edge truelight@0: tile = TILE_MASK(Random()); truelight@0: if (!CheckDistanceFromEdge(tile, 20)) truelight@0: continue; truelight@0: truelight@0: // Make sure the tile is plain truelight@0: FindLandscapeHeightByTile(&ti, tile); truelight@0: if (ti.type != MP_CLEAR || ti.tileh != 0) truelight@0: continue; truelight@0: truelight@0: // Check not too close to a town truelight@0: if (IsCloseToTown(tile, 20)) truelight@0: continue; truelight@193: truelight@0: // Allocate a town struct truelight@0: t = AllocateTown(); truelight@0: if (t == NULL) truelight@0: break; truelight@0: truelight@0: DoCreateTown(t, tile); truelight@0: return t; truelight@0: } while (--n); truelight@0: return NULL; truelight@0: } truelight@0: truelight@0: static const byte _num_initial_towns[3] = { truelight@0: 11, 23, 46 truelight@0: }; truelight@0: tron@1093: void GenerateTowns(void) truelight@0: { truelight@0: uint n; truelight@0: n = _num_initial_towns[_opt.diff.number_towns] + (Random()&7); truelight@0: do CreateRandomTown(); while (--n); truelight@0: } truelight@0: truelight@0: static bool CheckBuildHouseMode(Town *t1, uint tile, uint tileh, int mode) { truelight@0: Town *t2 = ClosestTownFromTile(tile, (uint)-1); truelight@0: int b; truelight@0: uint 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: if (t2 != t1) truelight@0: return false; truelight@0: truelight@0: slope = GetTileSlope(tile, NULL); truelight@0: if (slope & 0x10) truelight@0: return false; truelight@0: truelight@0: b = 0; truelight@0: if ((slope & 0xF && ~slope & _masks[mode])) b = ~b; truelight@0: if ((tileh & 0xF && ~tileh & _masks[mode+4])) b = ~b; truelight@0: if (b) truelight@0: return false; truelight@0: truelight@0: return DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR) != CMD_ERROR; truelight@0: } truelight@0: truelight@0: int GetTownRadiusGroup(Town *t, uint tile) truelight@0: { truelight@0: uint dist; truelight@0: int i,smallest; truelight@0: truelight@0: dist = GetTileDistAdv(tile, t->xy); truelight@0: if (t->fund_buildings_months && dist <= 25) truelight@0: return 4; truelight@0: truelight@0: smallest = 0; truelight@0: for(i=0; i!=lengthof(t->radius); i++) { truelight@0: if (dist < t->radius[i]) truelight@0: smallest = i; truelight@0: } truelight@0: truelight@0: return smallest; truelight@0: } truelight@0: truelight@0: static bool CheckFree2x2Area(Town *t1, uint tile) truelight@0: { truelight@0: Town *t; 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: truelight@0: for(i=0; i!=4; i++) { tron@909: tile += ToTileIndexDiff(_tile_add[i]); truelight@0: truelight@0: t = ClosestTownFromTile(tile, (uint)-1); truelight@193: if (t1 != t) truelight@0: return false; truelight@0: truelight@0: if (GetTileSlope(tile, NULL)) truelight@0: return false; truelight@0: truelight@0: if (DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER | DC_FORCETEST, CMD_LANDSCAPE_CLEAR) == CMD_ERROR) truelight@0: return false; truelight@0: } truelight@0: truelight@0: return true; truelight@0: } truelight@0: truelight@0: static void DoBuildTownHouse(Town *t, uint tile) truelight@0: { truelight@0: int i; truelight@0: uint bitmask; truelight@0: int house; truelight@0: uint slope; truelight@0: int z; truelight@0: uint oneof; 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; truelight@0: if (land == LT_HILLY && z >= _opt.snow_line) truelight@0: 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. truelight@0: 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: truelight@0: 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: tron@483: oneof = TOWN_HAS_CHURCH; tron@483: break; tron@483: case HOUSE_STADIUM: tron@483: case HOUSE_MODERN_STADIUM: tron@483: oneof = TOWN_HAS_STADIUM; tron@483: break; tron@483: default: tron@483: oneof = 0; tron@483: break; tron@483: } truelight@0: truelight@0: if (t->flags12 & oneof) truelight@0: continue; truelight@0: truelight@0: // Make sure there is no slope? truelight@0: if (_housetype_extra_flags[house]&0x12 && slope) truelight@0: continue; truelight@193: truelight@0: if (_housetype_extra_flags[house]&0x10) { truelight@0: if (CheckFree2x2Area(t,tile) || truelight@0: CheckFree2x2Area(t,(tile+=TILE_XY(-1,0))) || truelight@0: CheckFree2x2Area(t,(tile+=TILE_XY(0,-1))) || truelight@0: CheckFree2x2Area(t,(tile+=TILE_XY(1,0)))) truelight@0: break; truelight@0: tile += TILE_XY(0,1); truelight@0: } else if (_housetype_extra_flags[house]&4) { truelight@0: if (CheckBuildHouseMode(t, tile+TILE_XY(1,0), slope, 0)) truelight@0: break; truelight@193: truelight@0: if (CheckBuildHouseMode(t, tile+TILE_XY(-1,0), slope, 1)) { truelight@0: tile += TILE_XY(-1,0); truelight@0: break; truelight@0: } truelight@0: } else if (_housetype_extra_flags[house]&8) { truelight@0: if (CheckBuildHouseMode(t, tile+TILE_XY(0,1), slope, 2)) truelight@0: break; truelight@0: truelight@0: if (CheckBuildHouseMode(t, tile+TILE_XY(0,-1), slope, 3)) { truelight@0: tile += TILE_XY(0,-1); truelight@0: break; truelight@0: } truelight@0: } else truelight@0: break; 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: { truelight@0: int m3lo,m5,eflags; truelight@0: truelight@0: // ENDING_2 truelight@0: m3lo = 0; truelight@0: m5 = 0; truelight@0: if (_generating_world) { truelight@0: uint32 r = Random(); truelight@193: truelight@0: // Value for map3lo truelight@0: m3lo = 0xC0; truelight@0: if ((byte)r >= 220) m3lo &= (r>>8); truelight@0: truelight@0: if (m3lo == 0xC0) truelight@0: ChangePopulation(t, _housetype_population[house]); truelight@193: truelight@0: // Initial value for map5. truelight@0: m5 = (r >> 16) & 0x3F; truelight@0: } truelight@193: tron@1035: assert(IsTileType(tile, MP_CLEAR)); truelight@0: truelight@193: ModifyTile(tile, truelight@0: MP_SETTYPE(MP_HOUSE) | MP_MAP2 | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5 | MP_MAPOWNER, truelight@0: house, /* map2 */ truelight@0: m3lo, /* map3_lo */ truelight@0: 0, /* map_owner */ truelight@0: m5 /* map5 */ truelight@0: ); truelight@0: truelight@0: eflags = _housetype_extra_flags[house]; truelight@0: truelight@0: if (eflags&0x18) { tron@1035: assert(IsTileType(tile + TILE_XY(0,1), MP_CLEAR)); truelight@0: ModifyTile(tile + TILE_XY(0,1), truelight@0: MP_SETTYPE(MP_HOUSE) | MP_MAP2 | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5 | MP_MAPOWNER, truelight@0: ++house, /* map2 */ truelight@0: m3lo, /* map3_lo */ truelight@0: 0, /* map_owner */ truelight@0: m5 /* map5 */ truelight@0: ); truelight@0: } truelight@0: truelight@0: if (eflags&0x14) { tron@1035: assert(IsTileType(tile + TILE_XY(1,0), MP_CLEAR)); truelight@0: ModifyTile(tile + TILE_XY(1,0), truelight@0: MP_SETTYPE(MP_HOUSE) | MP_MAP2 | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5 | MP_MAPOWNER, truelight@0: ++house, /* map2 */ truelight@0: m3lo, /* map3_lo */ truelight@0: 0, /* map_owner */ truelight@0: m5 /* map5 */ truelight@0: ); truelight@0: } truelight@0: truelight@0: if (eflags&0x10) { tron@1035: assert(IsTileType(tile + TILE_XY(1,1), MP_CLEAR)); truelight@0: ModifyTile(tile + TILE_XY(1,1), truelight@0: MP_SETTYPE(MP_HOUSE) | MP_MAP2 | MP_MAP3LO | MP_MAP3HI_CLEAR | MP_MAP5 | MP_MAPOWNER, truelight@0: ++house, /* map2 */ truelight@0: m3lo, /* map3_lo */ truelight@0: 0, /* map_owner */ truelight@0: m5 /* map5 */ truelight@0: ); truelight@0: } truelight@0: } truelight@0: truelight@0: // ENDING truelight@0: } truelight@0: truelight@0: static bool BuildTownHouse(Town *t, uint tile) truelight@0: { truelight@0: int32 r; truelight@193: truelight@0: // make sure it's possible truelight@0: if (!EnsureNoVehicle(tile)) return false; truelight@0: if (GetTileSlope(tile, NULL) & 0x10) return false; truelight@0: truelight@0: r = DoCommandByTile(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR); truelight@0: if (r == CMD_ERROR) return false; truelight@0: truelight@0: DoBuildTownHouse(t, tile); truelight@0: return true; truelight@0: } truelight@0: truelight@0: truelight@0: static void DoClearTownHouseHelper(uint tile) truelight@0: { tron@1035: assert(IsTileType(tile, MP_HOUSE)); truelight@0: DoClearSquare(tile); truelight@0: DeleteAnimatedTile(tile); truelight@0: } truelight@0: truelight@0: static void ClearTownHouse(Town *t, uint tile) { truelight@0: uint house = _map2[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--; truelight@0: tile += TILE_XY(-1,0); truelight@0: } else if (_housetype_extra_flags[house-1] & 0x18) { truelight@0: house--; truelight@0: tile += TILE_XY(0,-1); truelight@0: } else if (_housetype_extra_flags[house-2] & 0x10) { truelight@0: house-=2; truelight@0: tile += TILE_XY(-1,0); truelight@0: } else if (_housetype_extra_flags[house-3] & 0x10) { truelight@0: house-=3; truelight@0: tile += TILE_XY(-1,-1); truelight@0: } truelight@0: } truelight@193: truelight@0: // Remove population from the town if the truelight@0: // house is finished. truelight@0: if ((~_map3_lo[tile] & 0xC0) == 0) { 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: tron@483: t->flags12 &= ~TOWN_HAS_CHURCH; tron@483: break; tron@483: case HOUSE_STADIUM: tron@483: case HOUSE_MODERN_STADIUM: tron@483: 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); truelight@0: if (eflags & 0x14) DoClearTownHouseHelper(tile + TILE_XY(1,0)); truelight@0: if (eflags & 0x18) DoClearTownHouseHelper(tile + TILE_XY(0,1)); truelight@0: if (eflags & 0x10) DoClearTownHouseHelper(tile + TILE_XY(1,1)); truelight@0: } truelight@0: truelight@0: int32 CmdRenameTown(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: StringID str; truelight@919: Town *t = GetTown(p1); truelight@193: truelight@543: str = AllocateNameUnique((byte*)_decode_parameters, 4); truelight@0: if (str == 0) truelight@0: return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { truelight@0: StringID old_str = t->townnametype; truelight@0: t->townnametype = str; truelight@0: DeleteName(old_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 DeleteTown(Town *t) truelight@0: { truelight@0: Industry *i; truelight@0: uint tile; truelight@0: truelight@0: // Delete town authority window truelight@0: // and remove from list of sorted towns truelight@0: DeleteWindowById(WC_TOWN_VIEW, t->index); truelight@0: _town_sort_dirty = true; truelight@0: truelight@0: // Delete all industries belonging to the town truelight@830: FOR_ALL_INDUSTRIES(i) { truelight@0: if (i->xy && i->town == t) truelight@0: DeleteIndustry(i); truelight@0: } truelight@0: truelight@0: // Go through all tiles and delete those belonging to the town truelight@0: tile = 0; truelight@0: do { tron@1035: if (IsTileType(tile, MP_HOUSE)) { truelight@0: if (ClosestTownFromTile(tile, (uint)-1) == t) { truelight@0: DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@0: } tron@1035: } else if (IsTileType(tile, MP_TUNNELBRIDGE) || IsTileType(tile, MP_STREET)) { truelight@0: if (_map_owner[tile] == OWNER_TOWN && ClosestTownFromTile(tile, (uint)-1) == t) { truelight@0: DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@0: } truelight@0: } tron@863: } while (++tile != MapSize()); truelight@0: truelight@0: t->xy = 0; truelight@0: DeleteName(t->townnametype); truelight@0: truelight@0: MarkWholeScreenDirty(); 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: truelight@0: typedef void TownActionProc(Town *t, int action); truelight@0: truelight@0: static void TownActionAdvertise(Town *t, int action) truelight@0: { truelight@0: static const byte _advertising_amount[3] = {0x40, 0x70, 0xA0}; truelight@0: static const byte _advertising_radius[3] = {10,15,20}; truelight@193: ModifyStationRatingAround(t->xy, _current_player, truelight@0: _advertising_amount[action], truelight@0: _advertising_radius[action]); truelight@0: } truelight@0: truelight@0: static void TownActionRoadRebuild(Town *t, int action) truelight@0: { truelight@0: Player *p; truelight@0: truelight@0: t->road_build_months = 6; truelight@193: tron@534: SetDParam(0, t->index); truelight@0: truelight@0: p = DEREF_PLAYER(_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: truelight@0: static bool DoBuildStatueOfCompany(uint tile) truelight@0: { truelight@0: TileInfo ti; truelight@0: byte old; truelight@0: int32 r; truelight@0: truelight@0: FindLandscapeHeightByTile(&ti, tile); truelight@0: if (ti.tileh != 0) truelight@0: return false; truelight@0: truelight@0: if (ti.type != MP_HOUSE && ti.type != MP_CLEAR && ti.type != MP_TREES) truelight@0: return false; truelight@0: truelight@0: truelight@0: old = _current_player; truelight@0: _current_player = OWNER_NONE; truelight@0: r = DoCommandByTile(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@0: _current_player = old; truelight@0: truelight@0: if (r == CMD_ERROR) truelight@0: return false; truelight@0: truelight@0: ModifyTile(tile, MP_SETTYPE(MP_UNMOVABLE) | MP_MAPOWNER_CURRENT | MP_MAP5, truelight@0: 2 /* map5 */ truelight@0: ); truelight@0: truelight@0: return true; truelight@0: } truelight@0: truelight@0: static void TownActionBuildStatue(Town *t, int action) truelight@0: { truelight@0: // Layouted as an outward spiral tron@909: static const TileIndexDiffC _statue_tiles[] = { tron@909: {-1, 0}, tron@909: { 0, 1}, tron@909: { 1, 0}, { 1, 0}, tron@909: { 0,-1}, { 0,-1}, tron@909: {-1, 0}, {-1, 0}, {-1, 0}, tron@909: { 0, 1}, { 0, 1}, { 0, 1}, tron@909: { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, tron@909: { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, tron@909: {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, tron@909: { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, tron@909: { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, tron@909: { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, tron@909: {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, tron@909: { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, { 0, 1}, tron@909: { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, { 1, 0}, tron@909: { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, { 0,-1}, tron@909: {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0}, tron@909: { 0, 0} tron@909: }; truelight@0: uint tile = t->xy; tron@909: const TileIndexDiffC *p; truelight@0: truelight@0: SETBIT(t->statues, _current_player); truelight@0: tron@909: for (p = _statue_tiles; p != endof(_statue_tiles); ++p) { truelight@0: if (DoBuildStatueOfCompany(tile)) truelight@0: return; tron@909: tile = TILE_ADD(tile, ToTileIndexDiff(*p)); tron@909: } truelight@0: } truelight@0: truelight@0: static void TownActionFundBuildings(Town *t, int action) truelight@0: { truelight@0: t->grow_counter = 1; truelight@0: t->flags12 |= 1; truelight@0: t->fund_buildings_months = 3; truelight@0: } truelight@0: truelight@0: static void TownActionBuyRights(Town *t, int action) 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: truelight@0: static void TownActionBribe(Town *t, int action) truelight@0: { truelight@0: if (!RandomRange(15)) { truelight@0: GoodsEntry *ge; truelight@0: Station *st; truelight@0: int i, rating; 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) { truelight@0: for (i=0, ge = st->goods; i != NUM_CARGO; i++, ge++) truelight@0: ge->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 truelight@0: if (!_networking || (_current_player == _local_player)) truelight@0: ShowErrorMessage(STR_BRIBE_FAILED_2, STR_BRIBE_FAILED, 0, 0); truelight@0: truelight@0: /* decrease by a lot! truelight@0: * ChangeTownRating is only for stuff in demolishing. Bribe failure should truelight@0: * be independent of any cheat settings truelight@0: */ truelight@0: rating = t->ratings[_current_player]; truelight@0: if (rating > -50) celestar@1005: t->ratings[_current_player] = RATING_BRIBE_DOWN_TO; truelight@0: } else { celestar@1005: ChangeTownRating(t, RATING_BRIBE_UP_STEP, RATING_BRIBE_MAXIMUM); truelight@0: } truelight@0: } truelight@0: truelight@0: static TownActionProc * const _town_action_proc[] = { truelight@0: TownActionAdvertise, truelight@0: TownActionAdvertise, truelight@0: TownActionAdvertise, truelight@0: TownActionRoadRebuild, truelight@0: TownActionBuildStatue, truelight@0: TownActionFundBuildings, truelight@0: TownActionBuyRights, truelight@0: TownActionBribe truelight@0: }; truelight@0: truelight@0: // p1 = town truelight@0: // p2 = action truelight@0: int32 CmdDoTownAction(int x, int y, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: int32 cost; 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) { truelight@919: _town_action_proc[p2](GetTown(p1), p2); 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) { truelight@0: if (GetTileDistAdv(st->xy, t->xy) <= t->radius[0]) { truelight@0: if (st->time_since_load <= 20 || st->time_since_unload <= 20) { truelight@0: n++; truelight@0: if (st->owner < MAX_PLAYERS && t->ratings[st->owner] <= 1000-12) truelight@0: t->ratings[st->owner] += 12; truelight@0: } else { truelight@0: if (st->owner < MAX_PLAYERS && t->ratings[st->owner] >= -1000+15) truelight@0: t->ratings[st->owner] -= 15; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: t->flags12 &= ~1; 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) { tron@1041: 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) { pasky@468: if (GetMapExtraBits(t->xy) == 1 && (t->act_food==0 || t->act_water==0) && t->population > 60) truelight@0: return; truelight@0: } truelight@0: pasky@470: t->growth_rate = m / (t->num_houses / 50 + 1); truelight@0: if (m <= t->grow_counter) truelight@0: t->grow_counter = m; truelight@0: truelight@0: t->flags12 |= 1; 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: { truelight@0: Player *p; truelight@0: truelight@0: FOR_ALL_PLAYERS(p) { truelight@0: if (t->unwanted[p->index] > 0) truelight@0: t->unwanted[p->index]--; truelight@0: } truelight@0: } truelight@0: truelight@0: bool CheckIfAuthorityAllows(uint tile) truelight@0: { truelight@0: Town *t; truelight@0: truelight@0: if (_current_player >= MAX_PLAYERS) truelight@0: return true; truelight@0: truelight@0: t = ClosestTownFromTile(tile, _patches.dist_local_authority); truelight@0: if (t == NULL) truelight@0: return true; truelight@0: truelight@0: if (t->ratings[_current_player] > -200) truelight@0: 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: truelight@0: Town *ClosestTownFromTile(uint 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@0: if (t->xy != 0) { truelight@0: dist = GetTileDist(tile, t->xy); truelight@0: if (dist < best) { truelight@0: best = dist; truelight@0: best_town = t; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: return best_town; truelight@0: } truelight@0: truelight@0: void ChangeTownRating(Town *t, int add, int max) truelight@0: { truelight@0: int rating; truelight@0: truelight@0: // if magic_bulldozer cheat is active, town doesn't penaltize for removing stuff truelight@0: if (t == NULL || _current_player >= MAX_PLAYERS || (_cheats.magic_bulldozer.value && add < 0) ) truelight@0: return; 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: truelight@0: /* penalty for removing town-owned stuff */ truelight@0: static const int _default_rating_settings [3][3] = { truelight@0: // ROAD_REMOVE, TUNNELBRIDGE_REMOVE, INDUSTRY_REMOVE truelight@0: { 0, 128, 384}, // Permissive truelight@0: { 48, 192, 480}, // Neutral truelight@0: { 96, 384, 768}, // Hostile truelight@0: }; truelight@0: truelight@0: bool CheckforTownRating(uint tile, uint32 flags, Town *t, byte type) truelight@0: { truelight@0: int modemod; truelight@0: truelight@0: // if magic_bulldozer cheat is active, town doesn't restrict your destructive actions truelight@0: if (t == NULL || _current_player >= MAX_PLAYERS || _cheats.magic_bulldozer.value) truelight@0: return true; truelight@0: truelight@0: /* check if you're allowed to remove the street/bridge/tunnel/industry truelight@193: * owned by a town no removal if rating is lower than ... depends now on truelight@0: * difficulty setting. Minimum town rating selected by difficulty level truelight@0: */ truelight@0: modemod = _default_rating_settings[_opt_mod_ptr->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@0: FOR_ALL_TOWNS(t) if (t->xy != 0) { truelight@0: if (t->road_build_months != 0) truelight@0: t->road_build_months--; truelight@0: dominik@121: if (t->exclusive_counter != 0) dominik@121: if(--t->exclusive_counter==0) dominik@121: 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@919: Town *t; truelight@0: int i; truelight@0: truelight@919: memset(_towns, 0, sizeof(_towns[0]) * _towns_size); truelight@919: truelight@919: i = 0; truelight@919: FOR_ALL_TOWNS(t) truelight@919: t->index = i++; truelight@0: truelight@0: memset(_subsidies, 0, sizeof(_subsidies)); truelight@0: for (s=_subsidies; s != endof(_subsidies); s++) truelight@0: s->cargo_type = 0xFF; truelight@0: truelight@0: _cur_town_ctr = 0; truelight@0: _town_sort_dirty = true; truelight@0: _total_towns = 0; truelight@0: } truelight@0: truelight@0: const TileTypeProcs _tile_type_town_procs = { truelight@0: DrawTile_Town, /* draw_tile_proc */ truelight@0: GetSlopeZ_Town, /* get_slope_z_proc */ truelight@0: ClearTile_Town, /* clear_tile_proc */ truelight@0: GetAcceptedCargo_Town, /* get_accepted_cargo_proc */ truelight@0: GetTileDesc_Town, /* get_tile_desc_proc */ truelight@0: GetTileTrackStatus_Town, /* get_tile_track_status_proc */ truelight@0: ClickTile_Town, /* click_tile_proc */ truelight@0: AnimateTile_Town, /* animate_tile_proc */ truelight@0: TileLoop_Town, /* tile_loop_clear */ truelight@0: ChangeTileOwner_Town, /* change_tile_owner_clear */ truelight@0: NULL, /* get_produced_cargo_proc */ truelight@0: NULL, /* vehicle_enter_tile_proc */ truelight@0: NULL, /* vehicle_leave_tile_proc */ dominik@39: GetSlopeTileh_Town, /* get_slope_tileh_proc */ truelight@0: }; truelight@0: truelight@0: truelight@0: // Save and load of towns. truelight@0: static const byte _town_desc[] = { tron@1174: SLE_CONDVAR(Town, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), tron@1174: SLE_CONDVAR(Town, xy, SLE_UINT32, 6, 255), truelight@193: truelight@0: SLE_CONDVAR(Town,population, SLE_FILE_U16 | SLE_VAR_U32, 0, 2), truelight@0: SLE_CONDVAR(Town,population, SLE_UINT32, 3, 255), truelight@0: truelight@0: truelight@0: SLE_VAR(Town,num_houses, SLE_UINT16), truelight@0: SLE_VAR(Town,townnametype,SLE_UINT16), truelight@0: SLE_VAR(Town,townnameparts,SLE_UINT32), truelight@0: truelight@0: SLE_VAR(Town,flags12, SLE_UINT8), truelight@0: SLE_VAR(Town,statues, SLE_UINT8), truelight@0: truelight@0: // sort_index_obsolete was stored here in savegame format 0 - 1 truelight@0: SLE_CONDARR(NullStruct,null,SLE_FILE_U8 | SLE_VAR_NULL, 1, 0, 1), truelight@0: truelight@0: SLE_VAR(Town,have_ratings,SLE_UINT8), truelight@0: SLE_ARR(Town,ratings, SLE_INT16, 8), truelight@0: // failed bribe attempts are stored since savegame format 4 truelight@0: SLE_CONDARR(Town,unwanted, SLE_INT8, 8, 4,255), truelight@0: truelight@0: SLE_VAR(Town,max_pass, SLE_UINT16), truelight@0: SLE_VAR(Town,max_mail, SLE_UINT16), truelight@0: SLE_VAR(Town,new_max_pass,SLE_UINT16), truelight@0: SLE_VAR(Town,new_max_mail,SLE_UINT16), truelight@0: SLE_VAR(Town,act_pass, SLE_UINT16), truelight@0: SLE_VAR(Town,act_mail, SLE_UINT16), truelight@0: SLE_VAR(Town,new_act_pass,SLE_UINT16), truelight@0: SLE_VAR(Town,new_act_mail,SLE_UINT16), truelight@0: truelight@0: SLE_VAR(Town,pct_pass_transported,SLE_UINT8), truelight@0: SLE_VAR(Town,pct_mail_transported,SLE_UINT8), truelight@0: truelight@0: SLE_VAR(Town,act_food, SLE_UINT16), darkvater@4: SLE_VAR(Town,act_water, SLE_UINT16), truelight@0: SLE_VAR(Town,new_act_food,SLE_UINT16), darkvater@4: SLE_VAR(Town,new_act_water,SLE_UINT16), truelight@0: truelight@0: SLE_VAR(Town,time_until_rebuild, SLE_UINT8), truelight@0: SLE_VAR(Town,grow_counter, SLE_UINT8), truelight@0: SLE_VAR(Town,growth_rate, SLE_UINT8), truelight@0: SLE_VAR(Town,fund_buildings_months, SLE_UINT8), truelight@0: SLE_VAR(Town,road_build_months, SLE_UINT8), truelight@0: dominik@121: SLE_VAR(Town,exclusivity, SLE_UINT8), dominik@121: SLE_VAR(Town,exclusive_counter, SLE_UINT8), dominik@121: // reserve extra space in savegame here. (currently 30 bytes) dominik@121: SLE_CONDARR(NullStruct,null,SLE_FILE_U8 | SLE_VAR_NULL, 30, 2, 255), 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@919: if (t->xy != 0) { truelight@919: SlSetArrayIndex(t->index); truelight@919: SlObject(t, _town_desc); truelight@919: } truelight@0: } truelight@0: } truelight@0: tron@1093: static void Load_TOWN(void) truelight@0: { truelight@0: int index; truelight@0: while ((index = SlIterateArray()) != -1) { truelight@919: Town *t = GetTown(index); truelight@919: truelight@0: SlObject(t, _town_desc); truelight@543: if (index > _total_towns) _total_towns = index; truelight@0: } truelight@0: } truelight@0: tron@1093: void AfterLoadTown(void) truelight@0: { truelight@0: Town *t; truelight@0: FOR_ALL_TOWNS(t) { truelight@0: if (t->xy != 0) { truelight@0: UpdateTownRadius(t); truelight@0: UpdateTownVirtCoord(t); truelight@0: } 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: }; truelight@0: truelight@0: