tron@2186: /* $Id$ */ tron@2186: belugas@6527: /** @file town_cmd.cpp */ belugas@6527: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" rubidium@6872: #include "tile_cmd.h" maedhros@6658: #include "debug.h" tron@3144: #include "road_map.h" rubidium@6872: #include "road_internal.h" /* Cleaning up road bits */ maedhros@6669: #include "landscape.h" tron@3319: #include "town_map.h" tron@3154: #include "tunnel_map.h" rubidium@6872: #include "viewport_func.h" truelight@0: #include "town.h" rubidium@6872: #include "command_func.h" truelight@0: #include "industry.h" richk@10184: #include "station_base.h" rubidium@6872: #include "player_base.h" richk@10184: #include "news_func.h" truelight@0: #include "saveload.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" truelight@4300: #include "genworld.h" maedhros@6658: #include "newgrf.h" maedhros@6658: #include "newgrf_callbacks.h" maedhros@6658: #include "newgrf_house.h" richk@6719: #include "newgrf_commons.h" richk@6720: #include "newgrf_townname.h" richk@6743: #include "misc/autoptr.hpp" rubidium@6870: #include "autoslope.h" rubidium@6871: #include "waypoint.h" rubidium@6871: #include "transparency.h" rubidium@6872: #include "tunnelbridge_map.h" rubidium@6872: #include "strings_func.h" rubidium@6872: #include "window_func.h" rubidium@6872: #include "string_func.h" richk@10184: #include "newgrf_cargo.h" richk@10184: #include "oldpool_func.h" rubidium@6872: rubidium@6872: #include "table/strings.h" rubidium@6872: #include "table/sprites.h" rubidium@6872: #include "table/town_land.h" rubidium@6872: rubidium@6872: uint _total_towns; rubidium@6872: HouseSpec _house_specs[HOUSE_MAX]; rubidium@6872: rubidium@6872: bool _town_sort_dirty; rubidium@6872: byte _town_sort_order; rubidium@6872: const Town **_town_sort; rubidium@6872: rubidium@6872: Town *_cleared_town; rubidium@6872: int _cleared_town_rating; truelight@1260: truelight@1260: /* Initialize the town-pool */ richk@6743: DEFINE_OLD_POOL_GENERIC(Town, Town) richk@6743: richk@6743: Town::Town(TileIndex tile) richk@6743: { rubidium@6800: if (tile != 0) _total_towns++; richk@6743: this->xy = tile; richk@6743: } richk@6743: richk@6743: Town::~Town() truelight@4396: { rubidium@6872: free(this->name); rubidium@6800: rubidium@6800: if (CleaningPool()) return; rubidium@6800: truelight@4396: Industry *i; truelight@4396: truelight@4396: /* Delete town authority window truelight@4396: * and remove from list of sorted towns */ richk@6743: DeleteWindowById(WC_TOWN_VIEW, this->index); truelight@4396: _town_sort_dirty = true; rubidium@5298: _total_towns--; truelight@4396: truelight@4396: /* Delete all industries belonging to the town */ richk@6743: FOR_ALL_INDUSTRIES(i) if (i->town == this) delete i; truelight@4396: truelight@4396: /* Go through all tiles and delete those belonging to the town */ richk@6743: for (TileIndex tile = 0; tile < MapSize(); ++tile) { truelight@4396: switch (GetTileType(tile)) { truelight@4396: case MP_HOUSE: richk@6743: if (GetTownByTile(tile) == this) DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@4396: break; truelight@4396: richk@6743: case MP_ROAD: truelight@4396: case MP_TUNNELBRIDGE: truelight@4396: if (IsTileOwner(tile, OWNER_TOWN) && richk@6743: ClosestTownFromTile(tile, (uint)-1) == this) 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: richk@6743: DeleteSubsidyWithTown(this->index); truelight@4396: truelight@4396: MarkWholeScreenDirty(); richk@6743: richk@6743: this->xy = 0; richk@6743: } richk@6743: richk@10184: /** richk@10184: * Generate a random town road layout. richk@10184: * richk@10184: * The layout is based on the TileHash. richk@10184: */ richk@10184: void Town::InitializeLayout() richk@10184: { richk@10184: this->layout = (TownLayout)(TileHash(TileX(this->xy), TileY(this->xy)) % NUM_TLS); richk@10184: richk@10184: /* Set invalid layouts to valid ones */ richk@10184: switch (this->layout) { richk@10184: default: break; richk@10184: case TL_RANDOM: this->layout = TL_ORIGINAL; break; richk@10184: case TL_NO_ROADS: this->layout = TL_BETTER_ROADS; break; richk@10184: } richk@10184: } richk@10184: truelight@0: // Local truelight@0: static int _grow_town_result; truelight@0: rubidium@6870: /* Describe the possible states */ rubidium@6870: enum TownGrowthResult { rubidium@6870: GROWTH_SUCCEED = -1, rubidium@6870: GROWTH_SEARCH_STOPPED = 0 rubidium@6870: // GROWTH_SEARCH_RUNNING >= 1 rubidium@6870: }; rubidium@6870: tron@1977: static bool BuildTownHouse(Town *t, TileIndex tile); truelight@0: Darkvater@2436: static void TownDrawHouseLift(const TileInfo *ti) truelight@0: { peter1138@5919: AddChildSpriteScreen(SPR_LIFT, PAL_NONE, 14, 60 - GetLiftPosition(ti->tile)); truelight@0: } truelight@0: Darkvater@2436: typedef void TownDrawTileProc(const TileInfo *ti); richk@10184: static TownDrawTileProc *const _town_draw_tile_procs[1] = { ludde@2065: TownDrawHouseLift truelight@0: }; truelight@0: richk@6719: /** rubidium@6870: * Return a random direction rubidium@6870: * rubidium@6870: * @return a random direction rubidium@6870: */ rubidium@6870: static inline DiagDirection RandomDiagDir() rubidium@6870: { rubidium@6870: return (DiagDirection)(3 & Random()); rubidium@6870: } rubidium@6870: rubidium@6870: /** richk@6719: * House Tile drawing handler. richk@6719: * Part of the tile loop process richk@6719: * @param ti TileInfo of the tile to draw richk@6719: */ truelight@0: static void DrawTile_Town(TileInfo *ti) truelight@0: { maedhros@6658: HouseID house_id = GetHouseType(ti->tile); maedhros@6658: maedhros@6658: if (house_id >= NEW_HOUSE_OFFSET) { maedhros@6668: /* Houses don't necessarily need new graphics. If they don't have a maedhros@6668: * spritegroup associated with them, then the sprite for the substitute maedhros@6668: * house id is drawn instead. */ maedhros@6668: if (GetHouseSpecs(house_id)->spritegroup != NULL) { maedhros@6668: DrawNewHouseTile(ti, house_id); maedhros@6668: return; maedhros@6668: } else { maedhros@6668: house_id = GetHouseSpecs(house_id)->substitute_id; maedhros@6668: } maedhros@6658: } truelight@0: truelight@0: /* Retrieve pointer to the draw town tile struct */ richk@10184: const DrawBuildingsTileStruct *dcts = &_town_draw_tile_data[house_id << 4 | TileHash2Bit(ti->x, ti->y) << 2 | GetHouseBuildingStage(ti->tile)]; truelight@0: richk@6743: if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED); peter1138@5919: richk@10184: DrawGroundSprite(dcts->ground.sprite, dcts->ground.pal); richk@10184: richk@10184: /* If houses are invisible, do not draw the upper part */ richk@10184: if (IsInvisibilitySet(TO_HOUSES)) return; truelight@0: truelight@0: /* Add a house on top of the ground? */ richk@10184: SpriteID image = dcts->building.sprite; tron@2639: if (image != 0) { richk@6743: AddSortableSpriteToDraw(image, dcts->building.pal, tron@482: ti->x + dcts->subtile_x, tron@482: ti->y + dcts->subtile_y, rubidium@6870: dcts->width, rubidium@6870: dcts->height, truelight@0: dcts->dz, richk@6743: ti->z, rubidium@6871: IsTransparencySet(TO_HOUSES) tron@4053: ); truelight@0: rubidium@6871: if (IsTransparencySet(TO_HOUSES)) 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: richk@6743: static Foundation GetFoundation_Town(TileIndex tile, Slope tileh) dominik@39: { richk@6743: return FlatteningFoundation(tileh); dominik@39: } dominik@39: richk@6719: /** richk@6719: * Animate a tile for a town richk@6719: * Only certain houses can be animated richk@6719: * The newhouses animation superseeds regular ones richk@6719: * @param tile TileIndex of the house to animate richk@6719: */ tron@1977: static void AnimateTile_Town(TileIndex tile) truelight@0: { maedhros@6658: if (GetHouseType(tile) >= NEW_HOUSE_OFFSET) { maedhros@6658: AnimateNewHouseTile(tile); maedhros@6658: return; maedhros@6658: } maedhros@6658: tron@2639: if (_tick_counter & 3) return; truelight@0: richk@6719: /* If the house is not one with a lift anymore, then stop this animating. richk@6719: * Not exactly sure when this happens, but probably when a house changes. richk@6719: * Before this was just a return...so it'd leak animated tiles.. richk@6719: * That bug seems to have been here since day 1?? */ maedhros@6658: if (!(GetHouseSpecs(GetHouseType(tile))->building_flags & BUILDING_IS_ANIMATED)) { ludde@2065: DeleteAnimatedTile(tile); ludde@2065: return; ludde@2065: } truelight@0: maedhros@6658: if (!LiftHasDestination(tile)) { richk@10184: uint i; richk@10184: richk@10184: /* Building has 6 floors, number 0 .. 6, where 1 is illegal. richk@10184: * This is due to the fact that the first floor is, in the graphics, Darkvater@2891: * the height of 2 'normal' floors. richk@10184: * Furthermore, there are 6 lift positions from floor N (incl) to floor N + 1 (excl) */ truelight@0: do { richk@10184: i = RandomRange(7); richk@10184: } while (i == 1 || i * 6 == GetLiftPosition(tile)); truelight@0: celestar@3426: SetLiftDestination(tile, i); truelight@0: } truelight@0: richk@10184: int pos = GetLiftPosition(tile); richk@10184: int 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: richk@6719: /** richk@6719: * Determines if a town is close to a tile richk@6719: * @param tile TileIndex of the tile to query richk@6719: * @param dist maximum distance to be accepted richk@6719: * @returns true if the tile correspond to the distance criteria richk@6719: */ tron@1977: static bool IsCloseToTown(TileIndex tile, uint dist) truelight@0: { richk@10184: 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: richk@6719: /** rubidium@6870: * Marks the town sign as needing a repaint. rubidium@6870: * rubidium@6870: * This function marks the area of the sign of a town as dirty for repaint. rubidium@6870: * rubidium@6870: * @param t Town requesting town sign for repaint rubidium@6870: * @ingroup dirty richk@6719: */ truelight@835: static void MarkTownSignDirty(Town *t) truelight@835: { truelight@835: MarkAllViewportsDirty( richk@6719: t->sign.left - 6, richk@6719: t->sign.top - 3, richk@6719: t->sign.left + t->sign.width_1 * 4 + 12, truelight@835: t->sign.top + 45 truelight@835: ); truelight@835: } truelight@835: richk@6719: /** richk@6719: * Resize the sign(label) of the town after changes in richk@6719: * population (creation or growth or else) richk@6719: * @param t Town to update richk@6719: */ truelight@835: void UpdateTownVirtCoord(Town *t) truelight@835: { truelight@835: MarkTownSignDirty(t); richk@10184: Point 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: rubidium@6871: /** Update the virtual coords needed to draw the town sign for all towns. */ rubidium@6871: void UpdateAllTownVirtCoords() rubidium@6871: { rubidium@6871: Town *t; rubidium@6871: FOR_ALL_TOWNS(t) { rubidium@6871: UpdateTownVirtCoord(t); rubidium@6871: } rubidium@6871: } rubidium@6871: richk@6719: /** richk@6719: * Change the towns population richk@6719: * @param t Town which polulation has changed richk@6719: * @param mod polulation change (can be positive or negative) richk@6719: */ 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: richk@6719: /** richk@6719: * Determines the world population richk@6719: * Basically, count population of all towns, one by one richk@6719: * @return uint32 the calculated population of the world richk@6719: */ rubidium@6573: uint32 GetWorldPopulation() celestar@1080: { richk@10184: uint32 pop = 0; richk@10184: const Town *t; richk@10184: tron@2639: FOR_ALL_TOWNS(t) pop += t->population; celestar@1080: return pop; celestar@1080: } celestar@1080: richk@6719: /** richk@6719: * Helper function for house completion stages progression richk@6719: * @param tile TileIndex of the house (or parts of it) to "grow" richk@6719: */ tron@1977: static void MakeSingleHouseBigger(TileIndex tile) truelight@0: { tron@1035: assert(IsTileType(tile, MP_HOUSE)); truelight@193: richk@6719: /* means it is completed, get out. */ celestar@3426: if (LiftHasDestination(tile)) return; truelight@0: richk@6719: /* progress in construction stages */ belugas@3432: IncHouseConstructionTick(tile); belugas@3432: if (GetHouseConstructionTick(tile) != 0) return; truelight@0: richk@6719: /* Check and/or */ rubidium@6871: if (HasBit(GetHouseSpecs(GetHouseType(tile))->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) { richk@6720: uint16 callback_res = GetHouseCallback(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile); maedhros@6658: if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(tile, callback_res); maedhros@6658: } truelight@0: maedhros@6658: if (IsHouseCompleted(tile)) { maedhros@6658: /* Now that construction is complete, we can add the population of the maedhros@6658: * building to the town. */ maedhros@6658: ChangePopulation(GetTownByTile(tile), GetHouseSpecs(GetHouseType(tile))->population); truelight@0: } truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: richk@6719: /** Make the house advances in its construction stages until completion richk@6719: * @param tile TileIndex of house richk@6719: */ tron@1977: static void MakeTownHouseBigger(TileIndex tile) truelight@0: { maedhros@6658: uint flags = GetHouseSpecs(GetHouseType(tile))->building_flags; maedhros@6658: if (flags & BUILDING_HAS_1_TILE) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 0)); maedhros@6658: if (flags & BUILDING_2_TILES_Y) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 1)); maedhros@6658: if (flags & BUILDING_2_TILES_X) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 0)); maedhros@6658: if (flags & BUILDING_HAS_4_TILES) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 1)); truelight@0: } truelight@0: richk@6719: /** richk@6719: * Periodic tic handler for houses and town richk@6719: * @param tile been asked to do its stuff richk@6719: */ tron@1977: static void TileLoop_Town(TileIndex tile) truelight@0: { maedhros@6658: HouseID house_id = GetHouseType(tile); truelight@0: maedhros@6658: /* NewHouseTileLoop returns false if Callback 21 succeeded, i.e. the house maedhros@6658: * doesn't exist any more, so don't continue here. */ maedhros@6658: if (house_id >= NEW_HOUSE_OFFSET && !NewHouseTileLoop(tile)) return; maedhros@6658: maedhros@6658: if (!IsHouseCompleted(tile)) { richk@6719: /* Construction is not completed. See if we can go further in construction*/ truelight@0: MakeTownHouseBigger(tile); truelight@0: return; truelight@0: } truelight@0: richk@10184: const HouseSpec *hs = GetHouseSpecs(house_id); richk@10184: maedhros@6658: /* If the lift has a destination, it is already an animated tile. */ richk@6719: if ((hs->building_flags & BUILDING_IS_ANIMATED) && richk@6719: house_id < NEW_HOUSE_OFFSET && richk@6719: !LiftHasDestination(tile) && richk@10184: Chance16(1, 2)) { richk@6719: AddAnimatedTile(tile); richk@10184: } richk@10184: richk@10184: Town *t = GetTownByTile(tile); richk@10184: uint32 r = Random(); truelight@0: rubidium@6871: if (HasBit(hs->callback_mask, CBM_HOUSE_PRODUCE_CARGO)) { richk@6719: for (uint i = 0; i < 256; i++) { richk@6719: uint16 callback = GetHouseCallback(CBID_HOUSE_PRODUCE_CARGO, i, r, house_id, t, tile); richk@6719: rubidium@6872: if (callback == CALLBACK_FAILED || callback == CALLBACK_HOUSEPRODCARGO_END) break; richk@6719: richk@6719: CargoID cargo = GetCargoTranslation(GB(callback, 8, 7), hs->grffile); richk@6719: if (cargo == CT_INVALID) continue; richk@6719: richk@6719: uint amt = GB(callback, 0, 8); richk@6719: uint moved = MoveGoodsToStation(tile, 1, 1, cargo, amt); richk@6719: richk@6719: const CargoSpec *cs = GetCargo(cargo); richk@6719: switch (cs->town_effect) { richk@6719: case TE_PASSENGERS: richk@6719: t->new_max_pass += amt; richk@6719: t->new_act_pass += moved; richk@6719: break; richk@6719: richk@6719: case TE_MAIL: richk@6719: t->new_max_mail += amt; richk@6719: t->new_act_mail += moved; richk@6719: break; richk@6719: richk@6719: default: richk@6719: break; richk@6719: } richk@6719: } richk@6719: } else { richk@6719: if (GB(r, 0, 8) < hs->population) { richk@6719: uint amt = GB(r, 0, 8) / 8 + 1; richk@6719: richk@6719: if (_economy.fluct <= 0) amt = (amt + 1) >> 1; richk@6719: t->new_max_pass += amt; richk@10184: t->new_act_pass += MoveGoodsToStation(tile, 1, 1, CT_PASSENGERS, amt); richk@6719: } richk@6719: richk@6719: if (GB(r, 8, 8) < hs->mail_generation) { richk@6719: uint amt = GB(r, 8, 8) / 8 + 1; richk@6719: richk@6719: if (_economy.fluct <= 0) amt = (amt + 1) >> 1; richk@6719: t->new_max_mail += amt; richk@10184: t->new_act_mail += MoveGoodsToStation(tile, 1, 1, CT_MAIL, amt); richk@6719: } truelight@0: } truelight@0: maedhros@6658: _current_player = OWNER_TOWN; maedhros@6658: richk@6719: if (hs->building_flags & BUILDING_HAS_1_TILE && rubidium@6871: HasBit(t->flags12, TOWN_IS_FUNDED) && richk@6719: CanDeleteHouse(tile) && richk@6719: max(_cur_year - GetHouseConstructionYear(tile), 0) >= hs->minimum_life && richk@6719: --t->time_until_rebuild == 0) { richk@6719: t->time_until_rebuild = GB(r, 16, 8) + 192; truelight@0: truelight@0: ClearTownHouse(t, tile); truelight@193: richk@6719: /* Rebuild with another house? */ richk@6878: if (GB(r, 24, 8) >= 12) BuildTownHouse(t, tile); maedhros@6658: } truelight@314: maedhros@6658: _current_player = OWNER_NONE; truelight@0: } truelight@0: richk@6719: /** richk@6719: * Unused handler richk@6719: * @param tile unused richk@6719: */ tron@1977: static void ClickTile_Town(TileIndex tile) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: richk@6720: static CommandCost ClearTile_Town(TileIndex tile, byte flags) truelight@0: { richk@10184: if ((flags & DC_AUTO) && !(flags & DC_AI_BUILDING)) return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED); richk@10184: if (!CanDeleteHouse(tile)) return CMD_ERROR; richk@10184: richk@10184: const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); richk@10184: rubidium@6872: CommandCost cost(EXPENSES_CONSTRUCTION); richk@6720: cost.AddCost(_price.remove_house * hs->removal_cost >> 8); truelight@0: richk@10184: int rating = hs->remove_rating_decrease; truelight@0: _cleared_town_rating += rating; richk@10184: Town *t = _cleared_town = 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: rubidium@6872: ChangeTownRating(t, -rating, RATING_HOUSE_MINIMUM); truelight@0: if (flags & DC_EXEC) { truelight@0: ClearTownHouse(t, tile); truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: richk@10184: static void GetProducedCargo_Town(TileIndex tile, CargoID *b) richk@10184: { richk@10184: HouseID house_id = GetHouseType(tile); richk@10184: const HouseSpec *hs = GetHouseSpecs(house_id); richk@10184: Town *t = GetTownByTile(tile); richk@10184: richk@10184: if (HasBit(hs->callback_mask, CBM_HOUSE_PRODUCE_CARGO)) { richk@10184: for (uint i = 0; i < 256; i++) { richk@10184: uint16 callback = GetHouseCallback(CBID_HOUSE_PRODUCE_CARGO, i, 0, house_id, t, tile); richk@10184: richk@10184: if (callback == CALLBACK_FAILED || callback == CALLBACK_HOUSEPRODCARGO_END) break; richk@10184: richk@10184: CargoID cargo = GetCargoTranslation(GB(callback, 8, 7), hs->grffile); richk@10184: richk@10184: if (cargo == CT_INVALID) continue; richk@10184: *(b++) = cargo; richk@10184: } richk@10184: } else { richk@10184: if (hs->population > 0) { richk@10184: *(b++) = CT_PASSENGERS; richk@10184: } richk@10184: if (hs->mail_generation > 0) { richk@10184: *(b++) = CT_MAIL; richk@10184: } richk@10184: } richk@10184: } richk@10184: tron@1977: static void GetAcceptedCargo_Town(TileIndex tile, AcceptedCargo ac) truelight@0: { richk@10184: const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); richk@6719: CargoID accepts[3]; richk@6719: richk@6719: /* Set the initial accepted cargo types */ richk@6719: for (uint8 i = 0; i < lengthof(accepts); i++) { richk@6719: accepts[i] = hs->accepts_cargo[i]; richk@6719: } richk@6719: richk@6719: /* Check for custom accepted cargo types */ rubidium@6871: if (HasBit(hs->callback_mask, CBM_HOUSE_ACCEPT_CARGO)) { richk@6719: uint16 callback = GetHouseCallback(CBID_HOUSE_ACCEPT_CARGO, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile); richk@6719: if (callback != CALLBACK_FAILED) { richk@6719: /* Replace accepted cargo types with translated values from callback */ richk@6719: accepts[0] = GetCargoTranslation(GB(callback, 0, 5), hs->grffile); richk@6719: accepts[1] = GetCargoTranslation(GB(callback, 5, 5), hs->grffile); richk@6719: accepts[2] = GetCargoTranslation(GB(callback, 10, 5), hs->grffile); richk@6719: } richk@6719: } richk@6719: richk@6719: /* Check for custom cargo acceptance */ rubidium@6871: if (HasBit(hs->callback_mask, CBM_HOUSE_CARGO_ACCEPTANCE)) { richk@6719: uint16 callback = GetHouseCallback(CBID_HOUSE_CARGO_ACCEPTANCE, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile); richk@6719: if (callback != CALLBACK_FAILED) { richk@6719: if (accepts[0] != CT_INVALID) ac[accepts[0]] = GB(callback, 0, 4); richk@6719: if (accepts[1] != CT_INVALID) ac[accepts[1]] = GB(callback, 4, 4); rubidium@6871: if (_opt.landscape != LT_TEMPERATE && HasBit(callback, 12)) { richk@6719: /* The 'S' bit indicates food instead of goods */ richk@6719: ac[CT_FOOD] = GB(callback, 8, 4); richk@6719: } else { richk@6719: if (accepts[2] != CT_INVALID) ac[accepts[2]] = GB(callback, 8, 4); richk@6719: } richk@6719: return; richk@6719: } richk@6719: } richk@6719: richk@6719: /* No custom acceptance, so fill in with the default values */ richk@6719: for (uint8 i = 0; i < lengthof(accepts); i++) { richk@6719: if (accepts[i] != CT_INVALID) ac[accepts[i]] = hs->cargo_acceptance[i]; richk@6719: } truelight@0: } truelight@0: tron@1977: static void GetTileDesc_Town(TileIndex tile, TileDesc *td) truelight@0: { maedhros@6658: td->str = GetHouseSpecs(GetHouseType(tile))->building_name; maedhros@6658: if (!IsHouseCompleted(tile)) { 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: richk@6878: static TrackStatus GetTileTrackStatus_Town(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side) 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: tron@2817: static bool GrowTown(Town *t); tron@2817: truelight@0: static void TownTickHandler(Town *t) truelight@0: { rubidium@6871: 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: rubidium@6573: void OnTick_Town() 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: rubidium@6870: /** rubidium@6870: * Return the RoadBits of a tile rubidium@6870: * rubidium@6870: * @note There are many other functions doing things like that. rubidium@6870: * @note Needs to be checked for needlessness. rubidium@6870: * @param tile The tile we want to analyse rubidium@6870: * @return The roadbits of the given tile rubidium@6870: */ rubidium@6870: static RoadBits GetTownRoadBits(TileIndex tile) truelight@0: { richk@6719: TrackBits b = GetAnyRoadTrackBits(tile, ROADTYPE_ROAD); rubidium@5838: RoadBits r = ROAD_NONE; tron@2639: richk@6720: if (b == TRACK_BIT_NONE) return r; 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: richk@6719: /** richk@6719: * Check if a neighboring tile has a road richk@6719: * richk@6719: * @param tile curent tile richk@6719: * @param dir target direction richk@6719: * @param dist_multi distance multiplyer richk@6719: * @return true if one of the neighboring tiles at the rubidium@6870: * given distance is a road tile else false richk@6719: */ rubidium@6877: static bool IsNeighborRoadTile(TileIndex tile, const DiagDirection dir, uint dist_multi) richk@6719: { rubidium@6877: /* Lookup table for the used diff values */ rubidium@6877: const TileIndexDiff tid_lt[3] = { rubidium@6877: TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90RIGHT)), rubidium@6877: TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT)), rubidium@6877: TileOffsByDiagDir(ReverseDiagDir(dir)), rubidium@6877: }; rubidium@6870: rubidium@6870: /* We add 1 to the distance because we want to get 1 for rubidium@6870: * the min distance multiplyer and not 0. rubidium@6870: * Therefore we start at 4. The 4 is used because rubidium@6877: * there are 4 tiles per distance step to check. */ rubidium@6870: dist_multi = (dist_multi + 1) * 4; rubidium@6870: for (uint pos = 4; pos < dist_multi; pos++) { rubidium@6870: TileIndexDiff cur = 0; rubidium@6870: /* For each even value of pos add the right TileIndexDiff rubidium@6870: * for each uneven value the left TileIndexDiff rubidium@6877: * for each with 2nd bit set (2,3,6,7,..) add the reversed TileIndexDiff */ rubidium@6870: cur += tid_lt[(pos & 1) ? 0 : 1]; rubidium@6870: if (pos & 2) cur += tid_lt[2]; rubidium@6870: rubidium@6870: cur = (uint)(pos / 4) * cur; // Multiply for the fitting distance rubidium@6870: if (GetTownRoadBits(TILE_ADD(tile, cur)) & DiagDirToRoadBits((pos & 2) ? dir : ReverseDiagDir(dir))) return true; rubidium@6870: } rubidium@6870: return false; richk@6719: } richk@6719: rubidium@6870: /** rubidium@6870: * Check if a Road is allowed on a given tile rubidium@6870: * richk@10184: * @param t The current town rubidium@6870: * @param tile The target tile rubidium@6870: * @param dir The direction in which we want to extend the town rubidium@6870: * @return true if it is allowed else false rubidium@6870: */ richk@10184: static bool IsRoadAllowedHere(Town *t, TileIndex tile, DiagDirection dir) truelight@0: { rubidium@6877: if (TileX(tile) < 2 || TileX(tile) >= MapMaxX() || TileY(tile) < 2 || TileY(tile) >= MapMaxY()) return false; richk@6720: rubidium@6870: Slope cur_slope, desired_slope; truelight@0: tron@2639: for (;;) { richk@6719: /* Check if there already is a road at this point? */ rubidium@6870: if (GetTownRoadBits(tile) == ROAD_NONE) { rubidium@6877: /* No, try if we are able to build a road piece there. rubidium@6877: * If that fails clear the land, and if that fails exit. richk@6719: * This is to make sure that we can build a road here later. */ rubidium@6870: if (CmdFailed(DoCommand(tile, ((dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? 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: rubidium@6870: cur_slope = GetTileSlope(tile, NULL); rubidium@6870: if (cur_slope == SLOPE_FLAT) { pasky@465: no_slope: richk@6719: /* Tile has no slope */ richk@10184: switch (t->GetActiveLayout()) { richk@6719: default: NOT_REACHED(); richk@6719: rubidium@6877: case TL_ORIGINAL: // Disallow the road if any neighboring tile has a road (distance: 1) rubidium@6870: return !IsNeighborRoadTile(tile, dir, 1); richk@6719: rubidium@6877: case TL_BETTER_ROADS: // Disallow the road if any neighboring tile has a road (distance: 1 and 2). rubidium@6870: return !IsNeighborRoadTile(tile, dir, 2); richk@6719: } truelight@0: } truelight@193: richk@6719: /* If the tile is not a slope in the right direction, then richk@6719: * maybe terraform some. */ rubidium@6871: desired_slope = (dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? SLOPE_NW : SLOPE_NE; rubidium@6870: if (desired_slope != cur_slope && ComplementSlope(desired_slope) != cur_slope) { rubidium@6871: if (Chance16(1, 8)) { rubidium@6871: CommandCost res = CMD_ERROR; rubidium@6871: if (!_generating_world && Chance16(1, 10)) { rubidium@6877: /* Note: Do not replace "^ SLOPE_ELEVATED" with ComplementSlope(). The slope might be steep. */ rubidium@6877: res = DoCommand(tile, Chance16(1, 16) ? cur_slope : cur_slope ^ SLOPE_ELEVATED, 0, rubidium@6871: DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND); tron@2639: } rubidium@6871: if (CmdFailed(res) && Chance16(1, 3)) { richk@6719: /* 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: TILE_ASSERT(tile); truelight@0: richk@10184: CommandCost r = DoCommand(tile, edges, dir, DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND); richk@10184: if (CmdFailed(r) || r.GetCost() >= (_price.terraform + 2) * 8) 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: { truelight@0: TILE_ASSERT(tile); truelight@0: richk@6719: /* Don't terraform if land is plain or if there's a house there. */ tron@3055: if (IsTileType(tile, MP_HOUSE)) return; richk@10184: Slope tileh = GetTileSlope(tile, NULL); tron@3636: if (tileh == SLOPE_FLAT) return; truelight@0: richk@6719: /* First try up, then down */ rubidium@6877: if (!TerraformTownTile(tile, ~tileh & SLOPE_ELEVATED, 1)) { rubidium@6877: TerraformTownTile(tile, tileh & SLOPE_ELEVATED, 0); truelight@0: } truelight@0: } truelight@0: richk@6719: /** richk@6719: * Generate the RoadBits of a grid tile richk@6719: * richk@6719: * @param t current town richk@6719: * @param tile tile in reference to the town rubidium@6870: * @param dir The direction to which we are growing ATM richk@6719: * @return the RoadBit of the current tile regarding richk@6719: * the selected town layout richk@6719: */ richk@10184: static RoadBits GetTownRoadGridElement(Town *t, TileIndex tile, DiagDirection dir) richk@6719: { richk@6719: /* align the grid to the downtown */ rubidium@6870: TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile); // Vector from downtown to the tile rubidium@6870: RoadBits rcmd = ROAD_NONE; richk@6719: richk@10184: switch (t->GetActiveLayout()) { richk@6719: default: NOT_REACHED(); richk@6719: richk@6719: case TL_2X2_GRID: rubidium@6870: if ((grid_pos.x % 3) == 0) rcmd |= ROAD_Y; rubidium@6870: if ((grid_pos.y % 3) == 0) rcmd |= ROAD_X; richk@6719: break; richk@6719: richk@6719: case TL_3X3_GRID: rubidium@6870: if ((grid_pos.x % 4) == 0) rcmd |= ROAD_Y; rubidium@6870: if ((grid_pos.y % 4) == 0) rcmd |= ROAD_X; richk@6719: break; richk@6719: } richk@6719: rubidium@6870: /* Optimise only X-junctions */ rubidium@6871: if (rcmd != ROAD_ALL) return rcmd; rubidium@6871: rubidium@6871: RoadBits rb_template; rubidium@6871: rubidium@6871: switch (GetTileSlope(tile, NULL)) { rubidium@6871: default: rb_template = ROAD_ALL; break; rubidium@6871: case SLOPE_W: rb_template = ROAD_NW | ROAD_SW; break; rubidium@6871: case SLOPE_SW: rb_template = ROAD_Y | ROAD_SW; break; rubidium@6871: case SLOPE_S: rb_template = ROAD_SW | ROAD_SE; break; rubidium@6871: case SLOPE_SE: rb_template = ROAD_X | ROAD_SE; break; rubidium@6871: case SLOPE_E: rb_template = ROAD_SE | ROAD_NE; break; rubidium@6871: case SLOPE_NE: rb_template = ROAD_Y | ROAD_NE; break; rubidium@6871: case SLOPE_N: rb_template = ROAD_NE | ROAD_NW; break; rubidium@6871: case SLOPE_NW: rb_template = ROAD_X | ROAD_NW; break; rubidium@6871: case SLOPE_STEEP_W: rubidium@6871: case SLOPE_STEEP_S: rubidium@6871: case SLOPE_STEEP_E: rubidium@6871: case SLOPE_STEEP_N: rubidium@6871: rb_template = ROAD_NONE; rubidium@6871: break; richk@6719: } rubidium@6870: rubidium@6871: /* Stop if the template is compatible to the growth dir */ rubidium@6871: if (DiagDirToRoadBits(ReverseDiagDir(dir)) & rb_template) return rb_template; rubidium@6871: /* If not generate a straight road in the direction of the growth */ rubidium@6871: return DiagDirToRoadBits(dir) | DiagDirToRoadBits(ReverseDiagDir(dir)); richk@6719: } richk@6719: richk@6719: /** rubidium@6870: * Grows the town with an extra house. rubidium@6870: * Check if there are enough neighbor house tiles rubidium@6870: * next to the current tile. If there are enough rubidium@6870: * add another house. richk@6719: * rubidium@6870: * @param t The current town rubidium@6870: * @param tile The target tile for the extra house rubidium@6870: * @return true if an extra house has been added richk@6719: */ rubidium@6870: static bool GrowTownWithExtraHouse(Town *t, TileIndex tile) richk@6719: { richk@6719: /* We can't look further than that. */ rubidium@6870: if (TileX(tile) < 2 || TileY(tile) < 2 || MapMaxX() <= TileX(tile) || MapMaxY() <= TileY(tile)) return false; rubidium@6870: rubidium@6870: uint counter = 0; // counts the house neighbor tiles rubidium@6870: rubidium@6870: /* Check the tiles E,N,W and S of the current tile for houses */ rubidium@6870: for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) { rubidium@6870: rubidium@6870: if (IsTileType(TileAddByDiagDir(tile, dir), MP_HOUSE)) counter++; rubidium@6870: rubidium@6870: /* If there are enough neighbors stop here */ rubidium@6870: if (counter >= 3) { rubidium@6870: if (BuildTownHouse(t, tile)) { rubidium@6870: _grow_town_result = GROWTH_SUCCEED; rubidium@6870: return true; rubidium@6870: } rubidium@6870: return false; rubidium@6870: } richk@6719: } rubidium@6870: return false; rubidium@6870: } rubidium@6870: rubidium@6870: /** rubidium@6870: * Grows the town with a road piece. rubidium@6870: * rubidium@6870: * @param t The current town rubidium@6870: * @param tile The current tile rubidium@6870: * @param rcmd The RoadBits we want to build on the tile rubidium@6870: * @return true if the RoadBits have been added else false rubidium@6870: */ rubidium@6870: static bool GrowTownWithRoad(const Town *t, TileIndex tile, RoadBits rcmd) rubidium@6870: { rubidium@6870: if (CmdSucceeded(DoCommand(tile, rcmd, t->index, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD))) { rubidium@6870: _grow_town_result = GROWTH_SUCCEED; rubidium@6870: return true; rubidium@6870: } rubidium@6870: return false; rubidium@6870: } rubidium@6870: rubidium@6870: /** rubidium@6870: * Grows the town with a bridge. rubidium@6870: * At first we check if a bridge is reasonable. rubidium@6870: * If so we check if we are able to build it. rubidium@6870: * rubidium@6870: * @param t The current town rubidium@6870: * @param tile The current tile rubidium@6871: * @param bridge_dir The valid direction in which to grow a bridge rubidium@6870: * @return true if a bridge has been build else false rubidium@6870: */ rubidium@6877: static bool GrowTownWithBridge(const Town *t, const TileIndex tile, const DiagDirection bridge_dir) rubidium@6870: { rubidium@6871: assert(bridge_dir < DIAGDIR_END); rubidium@6871: rubidium@6871: const Slope slope = GetTileSlope(tile, NULL); rubidium@6871: if (slope == SLOPE_FLAT) return false; // no slope, no bridge rubidium@6871: rubidium@6871: /* Make sure the direction is compatible with the slope. rubidium@6877: * Well we check if the slope has an up bit set in the rubidium@6877: * reverse direction. */ rubidium@6877: if (HASBITS(slope, InclinedSlope(bridge_dir))) return false; rubidium@6871: rubidium@6871: /* Assure that the bridge is connectable to the start side */ rubidium@6871: if (!(GetTownRoadBits(TileAddByDiagDir(tile, ReverseDiagDir(bridge_dir))) & DiagDirToRoadBits(bridge_dir))) return false; rubidium@6870: rubidium@6870: /* We are in the right direction */ rubidium@6877: uint8 bridge_length = 0; // This value stores the length of the possible bridge rubidium@6870: TileIndex bridge_tile = tile; // Used to store the other waterside rubidium@6870: rubidium@6877: const int delta = TileOffsByDiagDir(bridge_dir); rubidium@6870: do { rubidium@6870: if (bridge_length++ >= 11) { rubidium@6870: /* Max 11 tile long bridges */ rubidium@6870: return false; richk@6719: } rubidium@6871: bridge_tile += delta; rubidium@6877: } while (TileX(bridge_tile) != 0 && TileY(bridge_tile) != 0 && IsWaterTile(bridge_tile)); rubidium@6870: rubidium@6870: /* no water tiles in between? */ rubidium@6870: if (bridge_length == 1) return false; rubidium@6870: rubidium@6870: for (uint8 times = 0; times <= 22; times++) { rubidium@6870: byte bridge_type = RandomRange(MAX_BRIDGES - 1); rubidium@6870: rubidium@6870: /* Can we actually build the bridge? */ rubidium@6870: if (CmdSucceeded(DoCommand(tile, bridge_tile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE))) { rubidium@6870: DoCommand(tile, bridge_tile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_EXEC | DC_AUTO, CMD_BUILD_BRIDGE); rubidium@6870: _grow_town_result = GROWTH_SUCCEED; richk@6719: return true; richk@6719: } richk@6719: } rubidium@6870: /* Quit if it selecting an appropiate bridge type fails a large number of times. */ richk@6719: return false; richk@6719: } richk@6719: richk@6719: /** richk@6719: * Grows the given town. richk@6719: * There are at the moment 3 possible way's for richk@6719: * the town expansion: rubidium@6870: * @li Generate a random tile and check if there is a road allowed rubidium@6870: * @li TL_ORIGINAL rubidium@6870: * @li TL_BETTER_ROADS rubidium@6870: * @li Check if the town geometry allows a road and which one rubidium@6870: * @li TL_2X2_GRID rubidium@6870: * @li TL_3X3_GRID rubidium@6870: * @li Forbid roads, only build houses rubidium@6870: * @li TL_NO_ROADS richk@6719: * rubidium@6870: * @param tile_ptr The current tile rubidium@6870: * @param cur_rb The current tiles RoadBits rubidium@6870: * @param target_dir The target road dir rubidium@6870: * @param t1 The current town richk@6719: */ rubidium@6870: static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection target_dir, Town *t1) truelight@0: { rubidium@6870: RoadBits rcmd = ROAD_NONE; // RoadBits for the road construction command rubidium@6870: TileIndex tile = *tile_ptr; // The main tile on which we base our growth truelight@0: truelight@0: TILE_ASSERT(tile); truelight@0: rubidium@6870: if (cur_rb == ROAD_NONE) { richk@6719: /* Tile has no road. First reset the status counter richk@6719: * to say that this is the last iteration. */ rubidium@6870: _grow_town_result = GROWTH_SEARCH_STOPPED; truelight@0: richk@6719: /* Remove hills etc */ truelight@0: LevelTownLand(tile); truelight@0: richk@6719: /* Is a road allowed here? */ richk@10184: switch (t1->GetActiveLayout()) { richk@6719: default: NOT_REACHED(); richk@6719: richk@6719: case TL_NO_ROADS: /* Disallow Roads */ richk@6719: return; richk@6719: richk@6719: case TL_3X3_GRID: richk@6719: case TL_2X2_GRID: rubidium@6870: rcmd = GetTownRoadGridElement(t1, tile, target_dir); rubidium@6870: if (rcmd == ROAD_NONE) return; richk@6719: break; richk@6719: richk@6719: case TL_BETTER_ROADS: richk@6719: case TL_ORIGINAL: richk@10184: if (!IsRoadAllowedHere(t1, tile, target_dir)) return; rubidium@6870: rubidium@6870: DiagDirection source_dir = ReverseDiagDir(target_dir); rubidium@6870: rubidium@6871: if (Chance16(1, 4)) { rubidium@6870: /* Randomize a new target dir */ rubidium@6870: do target_dir = RandomDiagDir(); while (target_dir == source_dir); richk@6719: } richk@6719: richk@10184: if (!IsRoadAllowedHere(t1, TileAddByDiagDir(tile, target_dir), target_dir)) { richk@6719: /* A road is not allowed to continue the randomized road, rubidium@6870: * return if the road we're trying to build is curved. */ rubidium@6870: if (target_dir != ReverseDiagDir(source_dir)) return; richk@6719: richk@6719: /* Return if neither side of the new road is a house */ rubidium@6870: if (!IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90RIGHT)), MP_HOUSE) && rubidium@6870: !IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90LEFT)), MP_HOUSE)) { richk@6719: return; richk@6719: } richk@6719: richk@6719: /* That means that the road is only allowed if there is a house richk@6719: * at any side of the new road. */ richk@6719: } richk@6719: rubidium@6870: rcmd = DiagDirToRoadBits(target_dir) | DiagDirToRoadBits(source_dir); richk@6719: break; tron@2637: } truelight@0: rubidium@6870: } else if (target_dir < DIAGDIR_END && !(cur_rb & DiagDirToRoadBits(ReverseDiagDir(target_dir)))) { richk@6719: /* Continue building on a partial road. rubidium@6870: * Should be allways OK, so we only generate rubidium@6870: * the fitting RoadBits */ rubidium@6870: _grow_town_result = GROWTH_SEARCH_STOPPED; richk@6719: richk@10184: switch (t1->GetActiveLayout()) { richk@6719: default: NOT_REACHED(); richk@6719: richk@6719: case TL_NO_ROADS: /* Disallow Roads */ truelight@0: return; truelight@0: richk@6719: case TL_3X3_GRID: richk@6719: case TL_2X2_GRID: rubidium@6870: rcmd = GetTownRoadGridElement(t1, tile, target_dir); richk@6719: break; richk@6719: richk@6719: case TL_BETTER_ROADS: richk@6719: case TL_ORIGINAL: rubidium@6870: rcmd = DiagDirToRoadBits(ReverseDiagDir(target_dir)); richk@6719: break; truelight@0: } truelight@0: } else { rubidium@6870: bool allow_house = false; // Value which decides if we want to construct a house richk@6719: richk@6719: /* Reached a tunnel/bridge? Then continue at the other side of it. */ rubidium@5696: if (IsTileType(tile, MP_TUNNELBRIDGE)) { rubidium@6872: if (GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD) { rubidium@6872: *tile_ptr = GetOtherTunnelBridgeEnd(tile); rubidium@5696: } truelight@0: return; truelight@0: } truelight@193: richk@6719: /* Possibly extend the road in a direction. richk@6719: * Randomize a direction and if it has a road, bail out. */ rubidium@6870: target_dir = RandomDiagDir(); rubidium@6870: if (cur_rb & DiagDirToRoadBits(target_dir)) return; truelight@0: richk@6719: /* This is the tile we will reach if we extend to this direction. */ rubidium@6870: TileIndex house_tile = TileAddByDiagDir(tile, target_dir); // position of a possible house rubidium@6870: rubidium@6870: /* Don't walk into water. */ rubidium@6871: if (IsWaterTile(house_tile)) return; truelight@0: richk@10184: switch (t1->GetActiveLayout()) { richk@6719: default: NOT_REACHED(); richk@6719: richk@6719: case TL_NO_ROADS: richk@6719: allow_house = true; richk@6719: break; richk@6719: richk@6719: case TL_3X3_GRID: /* Use 2x2 grid afterwards! */ rubidium@6870: GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir)); rubidium@6870: /* FALL THROUGH */ richk@6719: richk@6719: case TL_2X2_GRID: rubidium@6870: rcmd = GetTownRoadGridElement(t1, house_tile, target_dir); richk@6719: allow_house = (rcmd == ROAD_NONE); richk@6719: break; richk@6719: richk@6719: case TL_BETTER_ROADS: /* Use original afterwards! */ rubidium@6870: GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir)); rubidium@6870: /* FALL THROUGH */ richk@6719: richk@6719: case TL_ORIGINAL: richk@6719: /* Allow a house at the edge. 60% chance or richk@6719: * always ok if no road allowed. */ rubidium@6870: rcmd = DiagDirToRoadBits(target_dir); richk@10184: allow_house = (!IsRoadAllowedHere(t1, house_tile, target_dir) || Chance16(6, 10)); richk@6719: break; richk@6719: } richk@6719: richk@6719: if (allow_house) { richk@6719: /* Build a house, but not if there already is a house there. */ rubidium@6870: if (!IsTileType(house_tile, MP_HOUSE)) { richk@6719: /* Level the land if possible */ rubidium@6871: if (Chance16(1, 6)) LevelTownLand(house_tile); truelight@0: richk@6719: /* And build a house. richk@6719: * Set result to -1 if we managed to build it. */ rubidium@6870: if (BuildTownHouse(t1, house_tile)) { rubidium@6870: _grow_town_result = GROWTH_SUCCEED; richk@6719: } truelight@0: } truelight@0: return; truelight@0: } truelight@0: rubidium@6870: _grow_town_result = GROWTH_SEARCH_STOPPED; truelight@0: } truelight@0: richk@6719: /* Return if a water tile */ rubidium@6871: if (IsWaterTile(tile)) return; truelight@0: rubidium@6870: /* Make the roads look nicer */ rubidium@6870: rcmd = CleanUpRoadBits(tile, rcmd); rubidium@6870: if (rcmd == ROAD_NONE) return; rubidium@6870: rubidium@6871: /* Only use the target direction for bridges to ensure they're connected. rubidium@6871: * The target_dir is as computed previously according to town layout, so rubidium@6871: * it will match it perfectly. */ rubidium@6871: if (GrowTownWithBridge(t1, tile, target_dir)) return; rubidium@6870: rubidium@6870: GrowTownWithRoad(t1, tile, rcmd); truelight@0: } truelight@0: richk@6719: /** Returns "growth" if a house was built, or no if the build failed. richk@6719: * @param t town to inquiry richk@6719: * @param tile to inquiry richk@6719: * @return something other than zero(0)if town expansion was possible richk@6719: */ tron@1977: static int GrowTownAtRoad(Town *t, TileIndex tile) truelight@0: { rubidium@6870: /* Special case. rubidium@6870: * @see GrowTownInTile Check the else if rubidium@6870: */ rubidium@6870: DiagDirection target_dir = DIAGDIR_END; // The direction in which we want to extend the town truelight@0: truelight@0: TILE_ASSERT(tile); truelight@0: richk@6719: /* Number of times to search. richk@6719: * Better roads, 2X2 and 3X3 grid grow quite fast so we give richk@6719: * them a little handicap. */ richk@10184: switch (t->GetActiveLayout()) { richk@6719: case TL_BETTER_ROADS: richk@6719: _grow_town_result = 10 + t->num_houses * 2 / 9; richk@6719: break; richk@6719: richk@6719: case TL_3X3_GRID: richk@6719: case TL_2X2_GRID: richk@6719: _grow_town_result = 10 + t->num_houses * 1 / 9; richk@6719: break; richk@6719: richk@6719: default: richk@6719: _grow_town_result = 10 + t->num_houses * 4 / 9; richk@6719: break; richk@6719: } truelight@0: truelight@0: do { rubidium@6870: RoadBits cur_rb = GetTownRoadBits(tile); // The RoadBits of the current tile truelight@0: richk@6719: /* Try to grow the town from this point */ rubidium@6870: GrowTownInTile(&tile, cur_rb, target_dir, t); richk@6719: richk@6719: /* Exclude the source position from the bitmask richk@6719: * and return if no more road blocks available */ rubidium@6870: cur_rb &= ~DiagDirToRoadBits(ReverseDiagDir(target_dir)); rubidium@6870: if (cur_rb == ROAD_NONE) truelight@0: return _grow_town_result; truelight@0: richk@6719: /* Select a random bit from the blockmask, walk a step richk@6719: * and continue the search from there. */ rubidium@6870: do target_dir = RandomDiagDir(); while (!(cur_rb & DiagDirToRoadBits(target_dir))); rubidium@6870: tile = TileAddByDiagDir(tile, target_dir); truelight@0: richk@6743: if (IsTileType(tile, MP_ROAD)) { truelight@1327: /* Don't allow building over roads of other cities */ tron@4077: if (IsTileOwner(tile, OWNER_TOWN) && GetTownByTile(tile) != t) { rubidium@6870: _grow_town_result = GROWTH_SUCCEED; rubidium@6877: } else if (IsTileOwner(tile, OWNER_NONE) && _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: richk@6719: /* 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: rubidium@6870: /** rubidium@6870: * Generate a random road block. richk@6719: * The probability of a straight road rubidium@6870: * is somewhat higher than a curved. rubidium@6870: * rubidium@6870: * @return A RoadBits value with 2 bits set rubidium@6870: */ rubidium@6573: static RoadBits GenRandomRoadBits() 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; rubidium@6870: return (RoadBits)((ROAD_NW << a) + (ROAD_NW << b)); truelight@0: } truelight@0: richk@6719: /** Grow the town richk@10184: * @param t town to grow richk@10184: * @return true iff a house was built richk@10184: */ tron@2817: static bool GrowTown(Town *t) truelight@0: { rubidium@6870: /* Let the town be a ghost town rubidium@6870: * The player wanted it in such a way. Thus there he has it. ;) rubidium@6870: * Never reached in editor mode. */ rubidium@6870: if (_patches.town_layout == TL_NO_ROADS && _generating_world) { rubidium@6870: return false; rubidium@6870: } 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: }; richk@6719: richk@6719: /* Current player is a town */ rubidium@6870: PlayerID old_player = _current_player; truelight@0: _current_player = OWNER_TOWN; truelight@0: rubidium@6870: TileIndex tile = t->xy; // The tile we are working with ATM rubidium@6870: richk@6719: /* Find a road that we can base the construction on. */ richk@10184: const TileIndexDiffC *ptr; tron@909: for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) { rubidium@6870: if (GetTownRoadBits(tile) != ROAD_NONE) { truelight@260: int r = GrowTownAtRoad(t, tile); truelight@260: _current_player = old_player; rubidium@5838: return r != 0; truelight@0: } tron@909: tile = TILE_ADD(tile, ToTileIndexDiff(*ptr)); tron@909: } truelight@0: richk@6719: /* No road available, try to build a random road block by richk@6719: * 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) { richk@6720: if (CmdSucceeded(DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, 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: richk@10184: 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; richk@6719: /* At least very roughly extrapolate. Empirical numbers dancing between richk@6719: * overwhelming by cottages and skyscrapers outskirts. */ pasky@470: t->radius[0] = mass * mass; richk@6719: /* Actually we are proportional to sqrt() but that's right because richk@6719: * 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: { richk@6720: extern int _nb_orig_names; 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; richk@6720: bool grf = (_opt.town_name >= _nb_orig_names); richk@6720: uint32 grfid = grf ? GetGRFTownNameId(_opt.town_name - _nb_orig_names) : 0; richk@6720: uint16 townnametype = grf ? GetGRFTownNameType(_opt.town_name - _nb_orig_names) : SPECSTR_TOWNNAME_START + _opt.town_name; truelight@0: richk@10184: assert(townnameparts != NULL); truelight@0: tron@2952: for (;;) { truelight@0: restart: truelight@0: r = Random(); truelight@0: tron@534: SetDParam(0, r); richk@6720: if (grf && grfid != 0) { richk@6720: GRFTownNameGenerate(buf1, grfid, townnametype, r, lastof(buf1)); richk@6720: } else { richk@6720: GetString(buf1, townnametype, lastof(buf1)); richk@6720: } truelight@193: richk@6719: /* Check size and width */ Darkvater@4609: if (strlen(buf1) >= 31 || GetStringBoundingBox(buf1).width > 130) continue; truelight@0: truelight@0: FOR_ALL_TOWNS(t2) { richk@6719: /* We can't just compare the numbers since richk@6719: * 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: richk@6719: /** richk@6719: * Does the actual town creation. richk@6719: * richk@6719: * @param t The town richk@6719: * @param tile Where to put it richk@6719: * @param townnameparts The town name richk@6719: * @param size_mode How the size should be determined richk@6719: * @param size Parameter for size determination richk@6719: */ richk@6719: static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSizeMode size_mode, uint size) truelight@0: { richk@6720: extern int _nb_orig_names; 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: richk@10184: for (uint i = 0; i != MAX_PLAYERS; i++) t->ratings[i] = RATING_INITIAL; truelight@0: truelight@0: t->have_ratings = 0; rubidium@5838: t->exclusivity = INVALID_PLAYER; dominik@121: t->exclusive_counter = 0; truelight@0: t->statues = 0; truelight@0: richk@6720: if (_opt.town_name < _nb_orig_names) { richk@6720: /* Original town name */ richk@6720: t->townnamegrfid = 0; richk@6720: t->townnametype = SPECSTR_TOWNNAME_START + _opt.town_name; richk@6720: } else { richk@6720: /* Newgrf town name */ richk@6720: t->townnamegrfid = GetGRFTownNameId(_opt.town_name - _nb_orig_names); richk@6720: t->townnametype = GetGRFTownNameType(_opt.town_name - _nb_orig_names); richk@6720: } pasky@1421: t->townnameparts = townnameparts; truelight@193: truelight@0: UpdateTownVirtCoord(t); truelight@0: _town_sort_dirty = true; truelight@0: richk@10184: t->InitializeLayout(); richk@10184: richk@6719: /* Random town size. */ richk@10184: int x = (Random() & 0xF) + 8; richk@6719: richk@6719: switch (size_mode) { richk@6719: default: NOT_REACHED(); richk@6719: richk@6719: case TSM_RANDOM: richk@6719: t->larger_town = false; richk@6719: break; richk@6719: richk@6719: case TSM_FIXED: richk@6719: x = size * 16 + 3; richk@6719: t->larger_town = false; richk@6719: break; richk@6719: richk@6719: case TSM_CITY: richk@6719: x *= _patches.initial_city_size; richk@6719: t->larger_town = true; richk@6719: break; celestar@3674: } truelight@0: truelight@0: t->num_houses += x; truelight@0: UpdateTownRadius(t); truelight@0: richk@10184: int 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: 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 richk@6719: * @param flags type of operation richk@6719: * @param p1 size of the town (0 = small, 1 = medium, 2 = large) richk@6719: * @param p2 size mode (@see TownSizeMode) Darkvater@1793: */ richk@6720: CommandCost CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { Darkvater@1793: /* Only in the scenario editor */ Darkvater@1793: if (_game_mode != GM_EDITOR) return CMD_ERROR; richk@6719: if (p2 > TSM_CITY) return CMD_ERROR; Darkvater@1793: richk@6719: /* 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: richk@6719: /* 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: richk@6719: /* 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: richk@10184: uint32 townnameparts; richk@10184: richk@6719: /* Get a unique name for the town. */ pasky@1421: if (!CreateTownName(&townnameparts)) pasky@1421: return_cmd_error(STR_023A_TOO_MANY_TOWNS); pasky@1421: richk@6719: /* Allocate town struct */ richk@6743: Town *t = new Town(tile); Darkvater@1793: if (t == NULL) return_cmd_error(STR_023A_TOO_MANY_TOWNS); richk@6743: AutoPtrT t_auto_delete = t; truelight@0: richk@6719: /* Create the town */ truelight@0: if (flags & DC_EXEC) { truelight@0: _generating_world = true; richk@6719: DoCreateTown(t, tile, townnameparts, (TownSizeMode)p2, p1); truelight@0: _generating_world = false; richk@6743: t_auto_delete.Detach(); truelight@0: } richk@6720: return CommandCost(); truelight@0: } truelight@0: richk@6719: Town *CreateRandomTown(uint attempts, TownSizeMode mode, uint size) truelight@0: { truelight@0: do { richk@6719: /* Generate a tile index not too close from the edge */ richk@10184: TileIndex tile = RandomTile(); tron@2951: if (DistanceFromEdge(tile) < 20) continue; truelight@0: richk@6719: /* Make sure the tile is plain */ tron@3636: if (!IsTileType(tile, MP_CLEAR) || GetTileSlope(tile, NULL) != SLOPE_FLAT) continue; truelight@0: richk@6719: /* Check not too close to a town */ tron@2951: if (IsCloseToTown(tile, 20)) continue; truelight@193: richk@10184: uint32 townnameparts; richk@10184: richk@6719: /* Get a unique name for the town. */ tron@2951: if (!CreateTownName(&townnameparts)) break; pasky@1421: richk@6719: /* Allocate a town struct */ richk@10184: Town *t = new Town(tile); tron@2951: if (t == NULL) break; truelight@0: richk@6719: DoCreateTown(t, tile, townnameparts, mode, size); truelight@0: return t; richk@10184: } while (--attempts != 0); richk@10184: truelight@0: return NULL; truelight@0: } truelight@0: richk@6719: static const byte _num_initial_towns[4] = {5, 11, 23, 46}; truelight@0: rubidium@6573: bool GenerateTowns() truelight@0: { celestar@1362: uint num = 0; miham@6387: uint n = ScaleByMapSize(_num_initial_towns[_opt.diff.number_towns] + (Random() & 7)); richk@6719: uint num_cities = _patches.larger_towns == 0 ? 0 : n / _patches.larger_towns; tron@1202: truelight@4300: SetGeneratingWorldProgress(GWP_TOWN, n); truelight@4300: celestar@1362: do { truelight@4300: IncreaseGeneratingWorldProgress(GWP_TOWN); richk@6719: /* try 20 times to create a random-sized town for the first loop. */ richk@6719: TownSizeMode mode = num_cities > 0 ? TSM_CITY : TSM_RANDOM; richk@6719: if (CreateRandomTown(20, mode, _patches.initial_city_size) != NULL) num++; richk@6719: if (num_cities > 0) num_cities--; celestar@1362: } while (--n); celestar@1362: richk@6719: /* give it a last try, but now more aggressive */ richk@6719: if (num == 0 && CreateRandomTown(10000, TSM_RANDOM, 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: truelight@0: rubidium@6872: /** Returns the bit corresponding to the town zone of the specified tile rubidium@6872: * @param t Town on which radius is to be found rubidium@6872: * @param tile TileIndex where radius needs to be found rubidium@6872: * @return the bit position of the given zone, as defined in HouseZones rubidium@6872: */ richk@10184: HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile) truelight@0: { tron@4000: uint dist = DistanceSquare(tile, t->xy); truelight@0: rubidium@6872: if (t->fund_buildings_months && dist <= 25) return HZB_TOWN_CENTRE; rubidium@6872: richk@10184: HouseZonesBits smallest = HZB_TOWN_EDGE; richk@10184: for (HouseZonesBits i = HZB_BEGIN; i < HZB_END; i++) { richk@10184: if (dist < t->radius[i]) smallest = i; truelight@0: } truelight@0: truelight@0: return smallest; truelight@0: } truelight@0: richk@6878: /** richk@6878: * Clears tile and builds a house or house part. richk@6878: * @param t tile index richk@6878: * @param tid Town index richk@6878: * @param counter of construction step richk@6878: * @param stage of construction (used for drawing) richk@6878: * @param type of house. Index into house specs array richk@6878: * @param random_bits required for newgrf houses richk@6878: * @pre house can be built here richk@6878: */ richk@6878: static inline void ClearMakeHouseTile(TileIndex tile, TownID tid, byte counter, byte stage, HouseID type, byte random_bits) truelight@0: { richk@6878: #if !defined(NDEBUG) || defined(WITH_ASSERT) richk@6878: CommandCost cc = richk@6878: #endif /* !defined(NDEBUG) || defined(WITH_ASSERT) */ richk@6878: richk@6878: DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR); richk@6878: richk@6878: assert(CmdSucceeded(cc)); richk@6878: richk@6878: MakeHouseTile(tile, tid, counter, stage, type, random_bits); richk@6878: } richk@6878: richk@6878: richk@6878: /** richk@6878: * Write house information into the map. For houses > 1 tile, all tiles are marked. richk@6878: * @param t tile index richk@6878: * @param tid Town index richk@6878: * @param counter of construction step richk@6878: * @param stage of construction (used for drawing) richk@6878: * @param type of house. Index into house specs array richk@6878: * @param random_bits required for newgrf houses richk@6878: * @pre house can be built here richk@6878: */ richk@6878: static void MakeTownHouse(TileIndex t, TownID tid, byte counter, byte stage, HouseID type, byte random_bits) richk@6878: { richk@6878: BuildingFlags size = GetHouseSpecs(type)->building_flags; richk@6878: richk@6878: ClearMakeHouseTile(t, tid, counter, stage, type, random_bits); richk@6878: if (size & BUILDING_2_TILES_Y) ClearMakeHouseTile(t + TileDiffXY(0, 1), tid, counter, stage, ++type, random_bits); richk@6878: if (size & BUILDING_2_TILES_X) ClearMakeHouseTile(t + TileDiffXY(1, 0), tid, counter, stage, ++type, random_bits); richk@6878: if (size & BUILDING_HAS_4_TILES) ClearMakeHouseTile(t + TileDiffXY(1, 1), tid, counter, stage, ++type, random_bits); richk@6878: } richk@6878: richk@6878: richk@6878: /** richk@6878: * Checks if a house can be built here. Important is slope, bridge above richk@6878: * and ability to clear the land. richk@6878: * @param tile tile to check richk@6878: * @param town town that is checking richk@6878: * @param noslope are slopes (foundations) allowed? richk@6878: * @return true iff house can be built here richk@6878: */ richk@6878: static inline bool CanBuildHouseHere(TileIndex tile, TownID town, bool noslope) richk@6878: { richk@6878: /* cannot build on these slopes... */ richk@6878: Slope slope = GetTileSlope(tile, NULL); richk@6878: if ((noslope && slope != SLOPE_FLAT) || IsSteepSlope(slope)) return false; richk@6878: richk@6878: /* building under a bridge? */ richk@6878: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return false; richk@6878: richk@6878: /* do not try to build over house owned by another town */ richk@6878: if (IsTileType(tile, MP_HOUSE) && GetTownIndex(tile) != town) return false; richk@6878: richk@6878: /* can we clear the land? */ richk@6878: return CmdSucceeded(DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR)); richk@6878: } richk@6878: richk@6878: richk@6878: /** richk@6878: * Checks if a house can be built at this tile, must have the same max z as parameter. richk@6878: * @param tile tile to check richk@6878: * @param town town that is checking richk@6878: * @param z max z of this tile so more parts of a house are at the same height (with foundation) richk@6878: * @param noslope are slopes (foundations) allowed? richk@6878: * @return true iff house can be built here richk@6878: * @see CanBuildHouseHere() richk@6878: */ richk@6878: static inline bool CheckBuildHouseSameZ(TileIndex tile, TownID town, uint z, bool noslope) richk@6878: { richk@6878: if (!CanBuildHouseHere(tile, town, noslope)) return false; richk@6878: richk@6878: /* if building on slopes is allowed, there will be flattening foundation (to tile max z) */ richk@6878: if (GetTileMaxZ(tile) != z) return false; richk@6878: richk@6878: return true; richk@6878: } richk@6878: richk@6878: richk@6878: /** richk@6878: * Checks if a house of size 2x2 can be built at this tile richk@6878: * @param tile tile, N corner richk@6878: * @param town town that is checking richk@6878: * @param z maximum tile z so all tile have the same max z richk@6878: * @param noslope are slopes (foundations) allowed? richk@6878: * @return true iff house can be built richk@6878: * @see CheckBuildHouseSameZ() richk@6878: */ richk@6878: static bool CheckFree2x2Area(TileIndex tile, TownID town, uint z, bool noslope) richk@6878: { richk@6878: /* we need to check this tile too because we can be at different tile now */ richk@6878: if (!CheckBuildHouseSameZ(tile, town, z, noslope)) return false; richk@6878: richk@6878: for (DiagDirection d = DIAGDIR_SE; d < DIAGDIR_END; d++) { richk@6878: tile += TileOffsByDiagDir(d); richk@6878: if (!CheckBuildHouseSameZ(tile, town, z, noslope)) return false; truelight@0: } truelight@0: truelight@0: return true; truelight@0: } truelight@0: richk@6878: richk@6878: /** richk@6878: * Checks if current town layout allows building here richk@6878: * @param t town richk@6878: * @param tile tile to check richk@6878: * @return true iff town layout allows building here richk@6878: * @note see layouts richk@6878: */ richk@6878: static inline bool TownLayoutAllowsHouseHere(Town *t, TileIndex tile) truelight@0: { richk@6878: TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile); richk@6878: richk@10184: switch (t->GetActiveLayout()) { richk@6878: case TL_2X2_GRID: richk@6878: if ((grid_pos.x % 3) == 0 || (grid_pos.y % 3) == 0) return false; richk@6878: break; richk@6878: richk@6878: case TL_3X3_GRID: richk@6878: if ((grid_pos.x % 4) == 0 || (grid_pos.y % 4) == 0) return false; richk@6878: break; richk@6878: richk@6878: default: richk@6878: break; richk@6878: } richk@6878: richk@6878: return true; richk@6878: } richk@6878: richk@6878: richk@6878: /** richk@6878: * Checks if current town layout allows 2x2 building here richk@6878: * @param t town richk@6878: * @param tile tile to check richk@6878: * @return true iff town layout allows 2x2 building here richk@6878: * @note see layouts richk@6878: */ richk@6878: static inline bool TownLayoutAllows2x2HouseHere(Town *t, TileIndex tile) richk@6878: { richk@6878: /* MapSize() is sure dividable by both MapSizeX() and MapSizeY(), richk@6878: * so to do only one memory access, use MapSize() */ richk@6878: uint dx = MapSize() + TileX(t->xy) - TileX(tile); richk@6878: uint dy = MapSize() + TileY(t->xy) - TileY(tile); richk@6878: richk@10184: switch (t->GetActiveLayout()) { richk@6878: case TL_2X2_GRID: richk@6878: if ((dx % 3) != 0 || (dy % 3) != 0) return false; richk@6878: break; richk@6878: richk@6878: case TL_3X3_GRID: richk@6878: if ((dx % 4) < 2 || (dy % 4) < 2) return false; richk@6878: break; richk@6878: richk@6878: default: richk@6878: break; richk@6878: } richk@6878: richk@6878: return true; richk@6878: } richk@6878: richk@6878: richk@6878: /** richk@6878: * Checks if 1x2 or 2x1 building is allowed here, also takes into account current town layout richk@6878: * Also, tests both building positions that occupy this tile richk@6878: * @param tile tile where the building should be built richk@6878: * @param t town richk@6878: * @param maxz all tiles should have the same height richk@6878: * @param noslope are slopes forbidden? richk@6878: * @param second diagdir from first tile to second tile richk@6878: **/ richk@6878: static bool CheckTownBuild2House(TileIndex *tile, Town *t, uint maxz, bool noslope, DiagDirection second) richk@6878: { richk@6878: /* 'tile' is already checked in BuildTownHouse() - CanBuildHouseHere() and slope test */ richk@6878: richk@6878: TileIndex tile2 = *tile + TileOffsByDiagDir(second); richk@6878: if (TownLayoutAllowsHouseHere(t, tile2) && CheckBuildHouseSameZ(tile2, t->index, maxz, noslope)) return true; richk@6878: richk@6878: tile2 = *tile + TileOffsByDiagDir(ReverseDiagDir(second)); richk@6878: if (TownLayoutAllowsHouseHere(t, tile2) && CheckBuildHouseSameZ(tile2, t->index, maxz, noslope)) { richk@6878: *tile = tile2; richk@6878: return true; richk@6878: } richk@6878: richk@6878: return false; richk@6878: } richk@6878: richk@6878: richk@6878: /** richk@6878: * Checks if 2x2 building is allowed here, also takes into account current town layout richk@6878: * Also, tests all four building positions that occupy this tile richk@6878: * @param tile tile where the building should be built richk@6878: * @param t town richk@6878: * @param maxz all tiles should have the same height richk@6878: * @param noslope are slopes forbidden? richk@6878: **/ richk@6878: static bool CheckTownBuild2x2House(TileIndex *tile, Town *t, uint maxz, bool noslope) richk@6878: { richk@6878: TileIndex tile2 = *tile; richk@6878: richk@6878: for (DiagDirection d = DIAGDIR_SE;;d++) { // 'd' goes through DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_END richk@6878: if (TownLayoutAllows2x2HouseHere(t, tile2) && CheckFree2x2Area(tile2, t->index, maxz, noslope)) { richk@6878: *tile = tile2; richk@6878: return true; richk@6878: } richk@6878: if (d == DIAGDIR_END) break; richk@6878: tile2 += TileOffsByDiagDir(ReverseDiagDir(d)); // go clockwise richk@6878: } richk@6878: richk@6878: return false; richk@6878: } richk@6878: richk@6878: richk@6878: /** richk@6878: * Tries to build a house at this tile richk@6878: * @param t town the house will belong to richk@6878: * @param tile where the house will be built richk@6878: * @return false iff no house can be built at this tile richk@6878: */ richk@6878: static bool BuildTownHouse(Town *t, TileIndex tile) richk@6878: { richk@6878: /* forbidden building here by town layout */ richk@6878: if (!TownLayoutAllowsHouseHere(t, tile)) return false; richk@6878: richk@6878: /* no house allowed at all, bail out */ richk@6878: if (!CanBuildHouseHere(tile, t->index, false)) return false; richk@6878: tron@1335: uint z; richk@6878: Slope slope = GetTileSlope(tile, &z); truelight@0: rubidium@6872: /* Get the town zone type of the current tile, as well as the climate. rubidium@6872: * This will allow to easily compare with the specs of the new house to build */ richk@6878: HouseZonesBits rad = GetTownRadiusGroup(t, tile); richk@6878: richk@6878: /* Above snow? */ richk@6878: int land = _opt.landscape; richk@6878: if (land == LT_ARCTIC && z >= _opt.snow_line) land = -1; richk@6878: richk@6878: uint bitmask = (1 << rad) + (1 << (land + 12)); truelight@0: richk@6719: /* bits 0-4 are used richk@6719: * bits 11-15 are used richk@6719: * bits 5-10 are not used. */ richk@6878: HouseID houses[HOUSE_MAX]; richk@6878: uint num = 0; richk@6878: uint probs[HOUSE_MAX]; richk@6878: uint probability_max = 0; richk@6878: richk@6878: /* Generate a list of all possible houses that can be built. */ richk@6878: for (uint i = 0; i < HOUSE_MAX; i++) { richk@10184: const HouseSpec *hs = GetHouseSpecs(i); richk@6878: /* Verify that the candidate house spec matches the current tile status */ richk@6878: if ((~hs->building_availability & bitmask) == 0 && hs->enabled) { richk@6878: /* Without NewHouses, all houses have probability '1' */ richk@6878: uint cur_prob = (_loaded_newgrf_features.has_newhouses ? hs->probability : 1); richk@6878: probability_max += cur_prob; richk@6878: probs[num] = cur_prob; richk@6878: houses[num++] = (HouseID)i; richk@6878: } richk@6878: } richk@6878: richk@6878: uint maxz = GetTileMaxZ(tile); richk@6878: richk@6878: while (probability_max > 0) { richk@6878: uint r = RandomRange(probability_max); richk@6878: uint i; richk@6878: for (i = 0; i < num; i++) { richk@6878: if (probs[i] > r) break; richk@6878: r -= probs[i]; richk@6878: } richk@6878: richk@6878: HouseID house = houses[i]; richk@6878: probability_max -= probs[i]; richk@6878: richk@6878: /* remove tested house from the set */ richk@6878: num--; richk@6878: houses[i] = houses[num]; richk@6878: probs[i] = probs[num]; richk@6878: richk@10184: const HouseSpec *hs = GetHouseSpecs(house); richk@6878: richk@6878: if (_loaded_newgrf_features.has_newhouses) { richk@6878: if (hs->override != 0) { richk@6878: house = hs->override; richk@6878: hs = GetHouseSpecs(house); richk@6878: } richk@6878: richk@6878: if ((hs->extra_flags & BUILDING_IS_HISTORICAL) && !_generating_world) continue; richk@6878: richk@6878: if (HasBit(hs->callback_mask, CBM_HOUSE_ALLOW_CONSTRUCTION)) { richk@6878: uint16 callback_res = GetHouseCallback(CBID_HOUSE_ALLOW_CONSTRUCTION, 0, 0, house, t, tile); richk@6878: if (callback_res != CALLBACK_FAILED && callback_res == 0) continue; maedhros@6658: } truelight@0: } truelight@0: richk@6878: if (_cur_year < hs->min_date || _cur_year > hs->max_date) continue; richk@6878: richk@6878: /* Special houses that there can be only one of. */ richk@6878: uint oneof = 0; richk@6878: richk@6878: if (hs->building_flags & BUILDING_IS_CHURCH) { richk@6878: SetBit(oneof, TOWN_HAS_CHURCH); richk@6878: } else if (hs->building_flags & BUILDING_IS_STADIUM) { richk@6878: SetBit(oneof, TOWN_HAS_STADIUM); truelight@0: } richk@6878: richk@6878: if (HASBITS(t->flags12 , oneof)) continue; richk@6878: richk@6878: /* Make sure there is no slope? */ richk@6878: bool noslope = (hs->building_flags & TILE_NOT_SLOPED) != 0; richk@6878: if (noslope && slope != SLOPE_FLAT) continue; richk@6878: richk@6878: if (hs->building_flags & TILE_SIZE_2x2) { richk@6878: if (!CheckTownBuild2x2House(&tile, t, maxz, noslope)) continue; richk@6878: } else if (hs->building_flags & TILE_SIZE_2x1) { richk@6878: if (!CheckTownBuild2House(&tile, t, maxz, noslope, DIAGDIR_SW)) continue; richk@6878: } else if (hs->building_flags & TILE_SIZE_1x2) { richk@6878: if (!CheckTownBuild2House(&tile, t, maxz, noslope, DIAGDIR_SE)) continue; richk@6878: } else { richk@6878: /* 1x1 house checks are already done */ richk@6878: } richk@6878: richk@6878: /* build the house */ richk@6878: t->num_houses++; richk@6878: IncreaseBuildingCount(t, house); richk@6878: richk@6878: /* Special houses that there can be only one of. */ richk@6878: t->flags12 |= oneof; richk@6878: richk@6878: byte construction_counter = 0; richk@6878: byte construction_stage = 0; truelight@0: truelight@0: if (_generating_world) { truelight@0: uint32 r = Random(); truelight@193: belugas@3432: construction_stage = TOWN_HOUSE_COMPLETED; rubidium@6871: if (Chance16(1, 7)) construction_stage = GB(r, 0, 2); truelight@0: belugas@3432: if (construction_stage == TOWN_HOUSE_COMPLETED) { maedhros@6658: ChangePopulation(t, hs->population); celestar@3382: } else { celestar@3382: construction_counter = GB(r, 2, 2); celestar@3382: } truelight@0: } richk@6878: rubidium@6872: MakeTownHouse(tile, t->index, construction_counter, construction_stage, house, Random()); richk@6878: richk@6878: return true; truelight@0: } richk@6878: richk@6878: return false; 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: maedhros@6658: void ClearTownHouse(Town *t, TileIndex tile) tron@1977: { richk@10184: assert(IsTileType(tile, MP_HOUSE)); richk@10184: maedhros@6658: HouseID house = GetHouseType(tile); truelight@0: richk@6719: /* 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. maedhros@6658: if (GetHouseSpecs(house-1)->building_flags & TILE_SIZE_2x1) { truelight@0: house--; tron@1981: tile += TileDiffXY(-1, 0); maedhros@6658: } else if (GetHouseSpecs(house-1)->building_flags & BUILDING_2_TILES_Y) { truelight@0: house--; tron@1981: tile += TileDiffXY(0, -1); maedhros@6658: } else if (GetHouseSpecs(house-2)->building_flags & BUILDING_HAS_4_TILES) { truelight@0: house-=2; tron@1981: tile += TileDiffXY(-1, 0); maedhros@6658: } else if (GetHouseSpecs(house-3)->building_flags & BUILDING_HAS_4_TILES) { truelight@0: house-=3; tron@1981: tile += TileDiffXY(-1, -1); truelight@0: } truelight@0: } truelight@193: richk@10184: const HouseSpec *hs = GetHouseSpecs(house); maedhros@6658: richk@6719: /* Remove population from the town if the house is finished. */ maedhros@6658: if (IsHouseCompleted(tile)) { maedhros@6658: ChangePopulation(t, -hs->population); truelight@0: } truelight@0: truelight@0: t->num_houses--; maedhros@6658: DecreaseBuildingCount(t, house); truelight@0: richk@6719: /* Clear flags for houses that only may exist once/town. */ maedhros@6658: if (hs->building_flags & BUILDING_IS_CHURCH) { rubidium@6871: ClrBit(t->flags12, TOWN_HAS_CHURCH); maedhros@6658: } else if (hs->building_flags & BUILDING_IS_STADIUM) { rubidium@6871: ClrBit(t->flags12, TOWN_HAS_STADIUM); tron@483: } truelight@193: richk@6719: /* Do the actual clearing of tiles */ richk@10184: uint eflags = hs->building_flags; truelight@0: DoClearTownHouseHelper(tile); maedhros@6658: if (eflags & BUILDING_2_TILES_X) DoClearTownHouseHelper(tile + TileDiffXY(1, 0)); maedhros@6658: if (eflags & BUILDING_2_TILES_Y) DoClearTownHouseHelper(tile + TileDiffXY(0, 1)); maedhros@6658: if (eflags & BUILDING_HAS_4_TILES) DoClearTownHouseHelper(tile + TileDiffXY(1, 1)); truelight@0: } truelight@0: richk@6720: static bool IsUniqueTownName(const char *name) richk@6720: { richk@6720: const Town *t; richk@6720: char buf[512]; richk@6720: richk@6720: FOR_ALL_TOWNS(t) { richk@6720: SetDParam(0, t->index); richk@6720: GetString(buf, STR_TOWN, lastof(buf)); richk@6720: if (strcmp(buf, name) == 0) return false; richk@6720: } richk@6720: richk@6720: return true; richk@6720: } richk@6720: Darkvater@1793: /** Rename a town (server-only). tron@3491: * @param tile unused richk@6719: * @param flags type of operation Darkvater@1793: * @param p1 town ID to rename Darkvater@1793: * @param p2 unused Darkvater@1793: */ richk@6720: CommandCost CmdRenameTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { richk@6720: if (!IsValidTownID(p1) || StrEmpty(_cmd_text)) return CMD_ERROR; Darkvater@1793: richk@6720: if (!IsUniqueTownName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE); richk@6720: truelight@0: if (flags & DC_EXEC) { richk@10184: Town *t = GetTown(p1); richk@10184: rubidium@6872: free(t->name); rubidium@6872: t->name = strdup(_cmd_text); truelight@0: truelight@0: UpdateTownVirtCoord(t); truelight@0: _town_sort_dirty = true; truelight@0: UpdateAllStationVirtCoord(); rubidium@6871: UpdateAllWaypointSigns(); truelight@0: MarkWholeScreenDirty(); truelight@0: } richk@6720: return CommandCost(); truelight@0: } truelight@0: richk@6719: /** Called from GUI */ truelight@0: void ExpandTown(Town *t) truelight@0: { truelight@0: _generating_world = true; truelight@193: truelight@835: /* The more houses, the faster we grow */ richk@10184: uint amount = RandomRange(ClampToU16(t->num_houses / 10)) + 3; truelight@0: t->num_houses += amount; truelight@0: UpdateTownRadius(t); truelight@0: richk@10184: uint 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: rubidium@5838: extern const byte _town_action_costs[8] = { truelight@0: 2, 4, 9, 35, 48, 53, 117, 175 truelight@0: }; truelight@0: richk@10184: static void TownActionAdvertiseSmall(Town *t) truelight@0: { tron@3877: ModifyStationRatingAround(t->xy, _current_player, 0x40, 10); truelight@0: } truelight@0: richk@10184: static void TownActionAdvertiseMedium(Town *t) tron@3877: { tron@3877: ModifyStationRatingAround(t->xy, _current_player, 0x70, 15); tron@3877: } tron@3877: richk@10184: static void TownActionAdvertiseLarge(Town *t) tron@3877: { tron@3877: ModifyStationRatingAround(t->xy, _current_player, 0xA0, 20); tron@3877: } tron@3877: richk@10184: static void TownActionRoadRebuild(Town *t) truelight@0: { truelight@0: t->road_build_months = 6; truelight@193: tron@534: SetDParam(0, t->index); richk@6720: SetDParam(1, _current_player); truelight@0: truelight@193: AddNewsItem(STR_2055_TRAFFIC_CHAOS_IN_ROAD_REBUILDING, richk@10184: NM_NORMAL, NF_TILE, NT_GENERAL, DNC_NONE, t->xy, 0); truelight@0: } truelight@0: truelight@6583: static bool DoBuildStatueOfCompany(TileIndex tile, TownID town_id) truelight@0: { rubidium@6870: /* Statues can be build on slopes, just like houses. Only the steep slopes is a no go. */ rubidium@6870: if (IsSteepSlope(GetTileSlope(tile, NULL))) 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: richk@10184: PlayerID old = _current_player; truelight@0: _current_player = OWNER_NONE; richk@10184: CommandCost 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: truelight@6583: MakeStatue(tile, _current_player, town_id); tron@3310: MarkTileDirtyByTile(tile); truelight@0: truelight@0: return true; truelight@0: } truelight@0: belugas@5118: /** belugas@5118: * Search callback function for TownActionBuildStatue richk@6719: * @param tile on which to perform the search truelight@6583: * @param town_id The town_id for which we want a statue belugas@6527: * @return the result of the test belugas@5118: */ truelight@6583: static bool SearchTileForStatue(TileIndex tile, uint32 town_id) belugas@5118: { truelight@6583: return DoBuildStatueOfCompany(tile, town_id); 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: */ richk@10184: static void TownActionBuildStatue(Town *t) truelight@0: { tron@1977: TileIndex tile = t->xy; truelight@0: richk@10184: if (CircularTileSearch(tile, 9, SearchTileForStatue, t->index)) { rubidium@6871: SetBit(t->statues, _current_player); // Once found and built, "inform" the Town richk@10184: } truelight@0: } truelight@0: richk@10184: static void TownActionFundBuildings(Town *t) truelight@0: { richk@6719: /* Build next tick */ truelight@0: t->grow_counter = 1; richk@6719: /* If we were not already growing */ rubidium@6871: SetBit(t->flags12, TOWN_IS_FUNDED); richk@6719: /* And grow for 3 months */ truelight@0: t->fund_buildings_months = 3; truelight@0: } truelight@0: richk@10184: static void TownActionBuyRights(Town *t) truelight@0: { rubidium@6870: /* Check if it's allowed to by the rights */ rubidium@6870: if (!_patches.exclusive_rights) return; rubidium@6870: 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: richk@10184: static void TownActionBribe(Town *t) truelight@0: { richk@10184: if (Chance16(1, 14)) { richk@6719: /* set as unwanted for 6 months */ truelight@0: t->unwanted[_current_player] = 6; truelight@0: richk@6719: /* set all close by station ratings to 0 */ richk@10184: Station *st; truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->town == t && st->owner == _current_player) { peter1138@6676: for (CargoID i = 0; i < NUM_CARGO; i++) st->goods[i].rating = 0; truelight@0: } truelight@0: } truelight@0: richk@6719: /* only show errormessage to the executing player. All errors are handled command.c richk@6719: * 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: richk@10184: typedef void TownActionProc(Town *t); richk@10184: 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 richk@6719: * @param flags type of operation 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: */ richk@6720: CommandCost CmdDoTownAction(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@4352: if (!IsValidTownID(p1) || p2 > lengthof(_town_action_proc)) return CMD_ERROR; Darkvater@1793: richk@10184: Town *t = GetTown(p1); Darkvater@1793: rubidium@6871: if (!HasBit(GetMaskOfTownActions(NULL, _current_player, t), p2)) return CMD_ERROR; truelight@0: rubidium@6872: CommandCost cost(EXPENSES_OTHER, (_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: { richk@10184: /* Increase player ratings if they're low */ richk@10184: const Player *p; truelight@0: FOR_ALL_PLAYERS(p) { richk@10184: if (p->is_active) { richk@10184: t->ratings[p->index] = min((int)RATING_GROWTH_MAXIMUM, t->ratings[p->index] + RATING_GROWTH_UP_STEP); truelight@0: } truelight@0: } truelight@0: richk@10184: int n = 0; richk@10184: richk@10184: const Station *st; 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++; richk@10184: if (IsValidPlayer(st->owner)) { richk@10184: int new_rating = t->ratings[st->owner] + RATING_STATION_UP_STEP; richk@10184: t->ratings[st->owner] = min(new_rating, INT16_MAX); // do not let it overflow richk@10184: } truelight@0: } else { richk@10184: if (IsValidPlayer(st->owner)) { richk@10184: int new_rating = t->ratings[st->owner] + RATING_STATION_DOWN_STEP; richk@10184: t->ratings[st->owner] = max(new_rating, INT16_MIN); richk@10184: } truelight@0: } truelight@0: } truelight@0: } truelight@0: richk@10184: /* clamp all ratings to valid values */ richk@10184: for (uint i = 0; i < MAX_PLAYERS; i++) { richk@10184: t->ratings[i] = Clamp(t->ratings[i], RATING_MINIMUM, RATING_MAXIMUM); richk@10184: } richk@10184: rubidium@6871: ClrBit(t->flags12, TOWN_IS_FUNDED); richk@6719: if (_patches.town_growth_rate == 0 && t->fund_buildings_months == 0) return; richk@6719: richk@6719: /** Towns are processed every TOWN_GROWTH_FREQUENCY ticks, and this is the richk@6719: * number of times towns are processed before a new building is built. */ richk@6719: static const uint16 _grow_count_values[2][6] = { rubidium@6870: { 120, 120, 120, 100, 80, 60 }, // Fund new buildings has been activated rubidium@6870: { 320, 420, 300, 220, 160, 100 } // Normal values richk@6719: }; truelight@0: richk@10184: uint16 m; richk@10184: truelight@0: if (t->fund_buildings_months != 0) { richk@6719: m = _grow_count_values[0][min(n, 5)]; truelight@0: t->fund_buildings_months--; truelight@0: } else { richk@6719: m = _grow_count_values[1][min(n, 5)]; rubidium@6871: if (n == 0 && !Chance16(1, 12)) return; truelight@0: } truelight@0: belugas@6683: if (_opt.landscape == LT_ARCTIC) { maedhros@6669: if (TilePixelHeight(t->xy) >= GetSnowLine() && t->act_food == 0 && t->population > 90) truelight@0: return; belugas@6683: } else if (_opt.landscape == LT_TROPIC) { 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: richk@6719: /* Use the normal growth rate values if new buildings have been funded in richk@6719: * this town and the growth rate is set to none. */ richk@6719: uint growth_multiplier = _patches.town_growth_rate != 0 ? _patches.town_growth_rate - 1 : 1; richk@6719: richk@6719: m >>= growth_multiplier; richk@6719: if (t->larger_town) m /= 2; richk@6719: 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: rubidium@6871: SetBit(t->flags12, TOWN_IS_FUNDED); truelight@0: } truelight@0: truelight@0: static void UpdateTownAmounts(Town *t) truelight@0: { richk@6719: /* 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: richk@6719: /* 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: { richk@10184: 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: { Darkvater@4850: if (!IsValidPlayer(_current_player)) return true; truelight@0: richk@10184: Town *t = ClosestTownFromTile(tile, _patches.dist_local_authority); tron@4000: if (t == NULL) return true; truelight@0: richk@10184: if (t->ratings[_current_player] > RATING_VERYPOOR) 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: richk@10184: Town *CalcClosestTownFromTile(TileIndex tile, uint threshold) truelight@0: { truelight@0: Town *t; richk@10184: uint best = threshold; truelight@0: Town *best_town = NULL; truelight@193: truelight@0: FOR_ALL_TOWNS(t) { richk@10184: uint 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) || ( richk@6743: IsTileType(tile, MP_ROAD) && richk@6719: GetRoadOwner(tile, ROADTYPE_ROAD) == OWNER_TOWN tron@3983: )) { tron@3983: return GetTownByTile(tile); tron@3983: } else { tron@3983: return CalcClosestTownFromTile(tile, threshold); tron@3983: } tron@3983: } tron@3983: rubidium@6872: static bool _town_rating_test = false; rubidium@6872: rubidium@6872: void SetTownRatingTestMode(bool mode) rubidium@6872: { rubidium@6872: static int ref_count = 0; rubidium@6872: if (mode) { rubidium@6872: if (ref_count == 0) { rubidium@6872: Town *t; rubidium@6872: FOR_ALL_TOWNS(t) t->test_rating = t->ratings[_current_player]; rubidium@6872: } rubidium@6872: ref_count++; rubidium@6872: } else { rubidium@6872: assert(ref_count > 0); rubidium@6872: ref_count--; rubidium@6872: } rubidium@6872: _town_rating_test = !(ref_count == 0); rubidium@6872: } tron@3983: truelight@0: void ChangeTownRating(Town *t, int add, int max) truelight@0: { richk@6719: /* 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: rubidium@6871: SetBit(t->have_ratings, _current_player); truelight@193: richk@10184: int rating = _town_rating_test ? t->test_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: } rubidium@6872: if (_town_rating_test) { rubidium@6872: t->test_rating = rating; rubidium@6872: } else { rubidium@6872: t->ratings[_current_player] = rating; rubidium@6872: } truelight@0: } truelight@0: rubidium@4434: /* penalty for removing town-owned stuff */ truelight@0: static const int _default_rating_settings [3][3] = { richk@6719: /* ROAD_REMOVE, TUNNELBRIDGE_REMOVE, INDUSTRY_REMOVE */ rubidium@6870: { 0, 128, 384}, // Permissive rubidium@6870: { 48, 192, 480}, // Neutral rubidium@6870: { 96, 384, 768}, // Hostile truelight@0: }; truelight@0: tron@2958: bool CheckforTownRating(uint32 flags, Town *t, byte type) truelight@0: { richk@6719: /* 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: */ richk@10184: int modemod = _default_rating_settings[_opt.diff.town_council_tolerance][type]; truelight@0: rubidium@6872: if ((_town_rating_test ? t->test_rating : 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: rubidium@6573: void TownsMonthlyLoop() 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) rubidium@5838: if (--t->exclusive_counter == 0) t->exclusivity = INVALID_PLAYER; dominik@121: truelight@0: UpdateTownGrowRate(t); truelight@0: UpdateTownAmounts(t); truelight@0: UpdateTownUnwanted(t); truelight@0: } truelight@0: } truelight@0: rubidium@6573: void InitializeTowns() truelight@0: { truelight@1260: /* Clean the town pool and create 1 block in it */ rubidium@6800: _Town_pool.CleanPool(); rubidium@6800: _Town_pool.AddBlockToPool(); truelight@0: truelight@0: memset(_subsidies, 0, sizeof(_subsidies)); richk@10184: for (Subsidy *s = _subsidies; s != endof(_subsidies); s++) { tron@2469: s->cargo_type = CT_INVALID; richk@10184: } 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: rubidium@6868: static CommandCost TerraformTile_Town(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new) rubidium@6868: { rubidium@6870: if (AutoslopeEnabled()) { rubidium@6870: HouseID house = GetHouseType(tile); richk@10184: const HouseSpec *hs = GetHouseSpecs(house); rubidium@6870: rubidium@6870: /* Here we differ from TTDP by checking TILE_NOT_SLOPED */ rubidium@6870: if (((hs->building_flags & TILE_NOT_SLOPED) == 0) && !IsSteepSlope(tileh_new) && rubidium@6872: (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); rubidium@6870: } rubidium@6870: rubidium@6868: return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); rubidium@6868: } rubidium@6868: rubidium@5838: extern 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 */ richk@10184: GetProducedCargo_Town, /* get_produced_cargo_proc */ rubidium@4344: NULL, /* vehicle_enter_tile_proc */ richk@6743: GetFoundation_Town, /* get_foundation_proc */ rubidium@6868: TerraformTile_Town, /* terraform_tile_proc */ truelight@0: }; truelight@0: truelight@0: richk@6719: /** 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: richk@10184: SLE_CONDNULL(2, 0, 2), ///< population, no longer in use richk@10184: SLE_CONDNULL(4, 3, 84), ///< population, no longer in use richk@10184: SLE_CONDNULL(2, 0, 91), ///< num_houses, no longer in use richk@10184: richk@6720: SLE_CONDVAR(Town, townnamegrfid, SLE_UINT32, 66, SL_MAX_VERSION), rubidium@4344: SLE_VAR(Town, townnametype, SLE_UINT16), rubidium@4344: SLE_VAR(Town, townnameparts, SLE_UINT32), rubidium@6872: SLE_CONDSTR(Town, name, SLE_STR, 0, 84, SL_MAX_VERSION), truelight@0: rubidium@4344: SLE_VAR(Town, flags12, SLE_UINT8), rubidium@4344: SLE_VAR(Town, statues, SLE_UINT8), truelight@0: richk@10184: SLE_CONDNULL(1, 0, 1), ///< sort_index, no longer in use truelight@0: rubidium@4344: SLE_VAR(Town, have_ratings, SLE_UINT8), rubidium@4344: SLE_ARR(Town, ratings, SLE_INT16, 8), richk@6719: /* failed bribe attempts are stored since savegame format 4 */ richk@10184: 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: richk@6719: SLE_CONDVAR(Town, time_until_rebuild, SLE_UINT8, 0, 53), richk@6719: SLE_CONDVAR(Town, grow_counter, SLE_UINT8, 0, 53), richk@6719: SLE_CONDVAR(Town, growth_rate, SLE_UINT8, 0, 53), richk@6719: rubidium@6872: SLE_CONDVAR(Town, time_until_rebuild, SLE_UINT16, 54, SL_MAX_VERSION), rubidium@6872: SLE_CONDVAR(Town, grow_counter, SLE_UINT16, 54, SL_MAX_VERSION), rubidium@6872: SLE_CONDVAR(Town, growth_rate, SLE_INT16, 54, SL_MAX_VERSION), richk@6719: rubidium@4344: SLE_VAR(Town, fund_buildings_months, SLE_UINT8), rubidium@4344: SLE_VAR(Town, road_build_months, SLE_UINT8), rubidium@4344: rubidium@6872: SLE_CONDVAR(Town, exclusivity, SLE_UINT8, 2, SL_MAX_VERSION), rubidium@6872: SLE_CONDVAR(Town, exclusive_counter, SLE_UINT8, 2, SL_MAX_VERSION), richk@6719: richk@6719: SLE_CONDVAR(Town, larger_town, SLE_BOOL, 56, SL_MAX_VERSION), richk@6719: richk@6719: /* 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: maedhros@6658: /* Save and load the mapping between the house id on the map, and the grf file maedhros@6658: * it came from. */ maedhros@6658: static const SaveLoad _house_id_mapping_desc[] = { richk@6719: SLE_VAR(EntityIDMapping, grfid, SLE_UINT32), richk@6719: SLE_VAR(EntityIDMapping, entity_id, SLE_UINT8), richk@6719: SLE_VAR(EntityIDMapping, substitute_id, SLE_UINT8), maedhros@6658: SLE_END() maedhros@6658: }; maedhros@6658: maedhros@6658: static void Save_HOUSEIDS() maedhros@6658: { richk@6719: uint j = _house_mngr.GetMaxMapping(); richk@6719: richk@6719: for (uint i = 0; i < j; i++) { maedhros@6658: SlSetArrayIndex(i); richk@6719: SlObject(&_house_mngr.mapping_ID[i], _house_id_mapping_desc); maedhros@6658: } maedhros@6658: } maedhros@6658: maedhros@6658: static void Load_HOUSEIDS() maedhros@6658: { maedhros@6658: int index; maedhros@6658: richk@6719: _house_mngr.ResetMapping(); richk@6719: uint max_id = _house_mngr.GetMaxMapping(); maedhros@6658: maedhros@6658: while ((index = SlIterateArray()) != -1) { richk@6719: if ((uint)index >= max_id) break; richk@6719: SlObject(&_house_mngr.mapping_ID[index], _house_id_mapping_desc); maedhros@6658: } maedhros@6658: } maedhros@6658: rubidium@6573: static void Save_TOWN() 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: rubidium@6573: static void Load_TOWN() truelight@0: { truelight@0: int index; truelight@919: truelight@1261: _total_towns = 0; truelight@1261: truelight@1260: while ((index = SlIterateArray()) != -1) { richk@6743: Town *t = new (index) Town(); 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: rubidium@6573: void AfterLoadTown() truelight@0: { richk@10184: _town_sort_dirty = true; richk@10184: truelight@0: Town *t; richk@10184: FOR_ALL_TOWNS(t) t->InitializeLayout(); truelight@0: } truelight@0: rubidium@5838: extern const ChunkHandler _town_chunk_handlers[] = { maedhros@6658: { 'HIDS', Save_HOUSEIDS, Load_HOUSEIDS, CH_ARRAY }, maedhros@6658: { 'CITY', Save_TOWN, Load_TOWN, CH_ARRAY | CH_LAST}, truelight@0: }; maedhros@6658: maedhros@6658: void ResetHouses() maedhros@6658: { maedhros@6658: memset(&_house_specs, 0, sizeof(_house_specs)); maedhros@6658: memcpy(&_house_specs, &_original_house_specs, sizeof(_original_house_specs)); rubidium@6870: rubidium@6870: /* Reset any overrides that have been set. */ rubidium@6870: _house_mngr.ResetOverride(); maedhros@6658: }