tron@2186: /* $Id$ */ tron@2186: belugas@6527: /** @file town_cmd.cpp */ belugas@6527: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@2163: #include "functions.h" maedhros@6658: #include "debug.h" tron@1309: #include "strings.h" tron@3144: #include "road_map.h" tron@507: #include "table/strings.h" celestar@2148: #include "table/sprites.h" tron@679: #include "map.h" maedhros@6669: #include "landscape.h" tron@1209: #include "tile.h" tron@3319: #include "town_map.h" tron@3154: #include "tunnel_map.h" truelight@0: #include "viewport.h" truelight@0: #include "town.h" truelight@0: #include "command.h" truelight@0: #include "gfx.h" truelight@0: #include "industry.h" truelight@0: #include "station.h" maedhros@6658: #include "vehicle.h" truelight@0: #include "player.h" truelight@0: #include "news.h" truelight@0: #include "saveload.h" truelight@0: #include "economy.h" truelight@0: #include "gui.h" tron@3310: #include "unmovable_map.h" celestar@3433: #include "water_map.h" tron@2159: #include "variables.h" celestar@3359: #include "bridge.h" celestar@5573: #include "bridge_map.h" rubidium@4261: #include "date.h" belugas@3654: #include "table/town_land.h" truelight@4300: #include "genworld.h" maedhros@6658: #include "newgrf.h" maedhros@6658: #include "newgrf_callbacks.h" maedhros@6658: #include "newgrf_house.h" truelight@0: truelight@1260: /** truelight@1260: * Called if a new block is added to the town-pool truelight@1260: */ truelight@1260: static void TownPoolNewBlock(uint start_item) truelight@1260: { truelight@1260: Town *t; truelight@1260: truelight@4346: /* We don't use FOR_ALL here, because FOR_ALL skips invalid items. truelight@4346: * TODO - This is just a temporary stage, this will be removed. */ tron@4983: for (t = GetTown(start_item); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) t->index = start_item++; truelight@1260: } truelight@1260: truelight@1260: /* Initialize the town-pool */ matthijs@5216: DEFINE_OLD_POOL(Town, Town, TownPoolNewBlock, NULL) truelight@1260: belugas@7030: /** belugas@7030: * Removes a specific town as well as all industries belugas@7030: * under its "juridiction" belugas@7030: * @param t Town to remove belugas@7030: */ truelight@4396: void DestroyTown(Town *t) truelight@4396: { truelight@4396: Industry *i; truelight@4396: TileIndex tile; truelight@4396: truelight@4396: /* Delete town authority window truelight@4396: * and remove from list of sorted towns */ truelight@4396: DeleteWindowById(WC_TOWN_VIEW, t->index); truelight@4396: _town_sort_dirty = true; rubidium@5298: _total_towns--; truelight@4396: truelight@4396: /* Delete all industries belonging to the town */ truelight@4396: FOR_ALL_INDUSTRIES(i) if (i->town == t) DeleteIndustry(i); truelight@4396: truelight@4396: /* Go through all tiles and delete those belonging to the town */ truelight@4396: for (tile = 0; tile < MapSize(); ++tile) { truelight@4396: switch (GetTileType(tile)) { truelight@4396: case MP_HOUSE: truelight@4396: if (GetTownByTile(tile) == t) DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@4396: break; truelight@4396: truelight@4396: case MP_STREET: truelight@4396: case MP_TUNNELBRIDGE: truelight@4396: if (IsTileOwner(tile, OWNER_TOWN) && truelight@4396: ClosestTownFromTile(tile, (uint)-1) == t) truelight@4396: DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@4396: break; truelight@4396: truelight@4396: default: truelight@4396: break; truelight@4396: } truelight@4396: } truelight@4396: truelight@4396: DeleteName(t->townnametype); rubidium@5566: DeleteSubsidyWithTown(t->index); truelight@4396: truelight@4396: MarkWholeScreenDirty(); truelight@4396: } truelight@4396: truelight@0: // Local truelight@0: static int _grow_town_result; truelight@0: tron@1977: static bool BuildTownHouse(Town *t, TileIndex tile); tron@1977: static void DoBuildTownHouse(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); truelight@0: static TownDrawTileProc * const _town_draw_tile_procs[1] = { ludde@2065: TownDrawHouseLift truelight@0: }; truelight@0: maedhros@6658: uint OriginalTileRandomiser(uint x, uint y) maedhros@6658: { maedhros@6658: uint variant; maedhros@6658: variant = x >> 4; maedhros@6658: variant ^= x >> 6; maedhros@6658: variant ^= y >> 4; maedhros@6658: variant -= y >> 6; maedhros@6658: variant &= 3; maedhros@6658: return variant; maedhros@6658: } truelight@0: belugas@7030: /** belugas@7030: * House Tile drawing handler. belugas@7030: * Part of the tile loop process belugas@7030: * @param ti TileInfo of the tile to draw belugas@7030: */ truelight@0: static void DrawTile_Town(TileInfo *ti) truelight@0: { belugas@3654: const DrawBuildingsTileStruct *dcts; peter1138@5919: SpriteID image; peter1138@5919: SpriteID pal; 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 */ maedhros@6658: dcts = &_town_draw_tile_data[house_id << 4 | OriginalTileRandomiser(ti->x, ti->y) << 2 | GetHouseBuildingStage(ti->tile)]; truelight@0: tron@4053: if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, ti->tileh); peter1138@5919: peter1138@5919: image = dcts->ground.sprite; peter1138@5919: pal = dcts->ground.pal; peter1138@5919: DrawGroundSprite(image, pal); truelight@0: truelight@0: /* Add a house on top of the ground? */ peter1138@5919: image = dcts->building.sprite; tron@2639: if (image != 0) { peter1138@6923: if (HASBIT(_transparent_opt, TO_HOUSES)) { peter1138@5919: SETBIT(image, PALETTE_MODIFIER_TRANSPARENT); peter1138@5919: pal = PALETTE_TO_TRANSPARENT; peter1138@5919: } else { peter1138@5919: pal = dcts->building.pal; peter1138@5919: } truelight@193: peter1138@5919: AddSortableSpriteToDraw(image, pal, tron@482: ti->x + dcts->subtile_x, tron@482: ti->y + dcts->subtile_y, tron@482: dcts->width + 1, tron@482: dcts->height + 1, truelight@0: dcts->dz, tron@4053: ti->z tron@4053: ); truelight@0: peter1138@6923: if (HASBIT(_transparent_opt, 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: tron@3636: static Slope GetSlopeTileh_Town(TileIndex tile, Slope tileh) dominik@39: { tron@3636: return SLOPE_FLAT; dominik@39: } dominik@39: belugas@7030: /** belugas@7030: * Animate a tile for a town belugas@7030: * Only certain houses can be animated belugas@7030: * The newhouses animation superseeds regular ones belugas@7030: * @param tile TileIndex of the house to animate belugas@7030: */ tron@1977: static void AnimateTile_Town(TileIndex tile) truelight@0: { celestar@3426: int pos, dest; 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: belugas@6918: /* If the house is not one with a lift anymore, then stop this animating. belugas@6918: * Not exactly sure when this happens, but probably when a house changes. belugas@6918: * Before this was just a return...so it'd leak animated tiles.. belugas@6918: * 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)) { tron@2639: int i; tron@2639: belugas@6918: /* Building has 6 floors, number 0 .. 6, where 1 is illegal. Darkvater@2891: * This is due to the fact that the first floor is, in the graphics, Darkvater@2891: * the height of 2 'normal' floors. Darkvater@2891: * Furthermore, there are 6 lift positions from floor N (incl) to floor N + 1 (excl) */ truelight@0: do { tron@2639: i = (Random() & 7) - 1; celestar@3426: } while (i < 0 || i == 1 || i * 6 == GetLiftPosition(tile)); truelight@0: celestar@3426: SetLiftDestination(tile, i); truelight@0: } truelight@0: celestar@3426: pos = GetLiftPosition(tile); celestar@3426: dest = GetLiftDestination(tile) * 6; celestar@3426: pos += (pos < dest) ? 1 : -1; celestar@3426: SetLiftPosition(tile, pos); truelight@0: celestar@3426: if (pos == dest) HaltLift(tile); truelight@193: truelight@0: MarkTileDirtyByTile(tile); truelight@0: } truelight@0: truelight@0: static void UpdateTownRadius(Town *t); truelight@0: belugas@7030: /** belugas@7030: * Determines if a town is close to a tile belugas@7030: * @param tile TileIndex of the tile to query belugas@7030: * @param dist maximum distance to be accepted belugas@7030: * @returns true if the tile correspond to the distance criteria belugas@7030: */ tron@1977: static bool IsCloseToTown(TileIndex tile, uint dist) truelight@0: { tron@2630: const Town* t; truelight@0: truelight@0: FOR_ALL_TOWNS(t) { truelight@4346: if (DistanceManhattan(tile, t->xy) < dist) return true; truelight@0: } truelight@0: return false; truelight@0: } truelight@0: belugas@7030: /** belugas@7030: * Marks the town sign as needing a repaint belugas@7030: * @param t Town requesting repaint belugas@7030: */ truelight@835: static void MarkTownSignDirty(Town *t) truelight@835: { truelight@835: MarkAllViewportsDirty( rubidium@6987: t->sign.left - 6, rubidium@6987: t->sign.top - 3, rubidium@6987: t->sign.left + t->sign.width_1 * 4 + 12, truelight@835: t->sign.top + 45 truelight@835: ); truelight@835: } truelight@835: belugas@7030: /** belugas@7030: * Resize the sign(label) of the town after changes in belugas@7030: * population (creation or growth or else) belugas@7030: * @param t Town to update belugas@7030: */ truelight@835: void UpdateTownVirtCoord(Town *t) truelight@835: { truelight@836: Point pt; truelight@836: truelight@835: MarkTownSignDirty(t); celestar@3422: pt = RemapCoords2(TileX(t->xy) * TILE_SIZE, TileY(t->xy) * TILE_SIZE); ludde@2070: SetDParam(0, t->index); ludde@2070: SetDParam(1, t->population); ludde@2070: UpdateViewportSignPos(&t->sign, pt.x, pt.y - 24, ludde@2070: _patches.population_in_label ? STR_TOWN_LABEL_POP : STR_TOWN_LABEL); truelight@835: MarkTownSignDirty(t); truelight@835: } truelight@0: belugas@7030: /** belugas@7030: * Change the towns population belugas@7030: * @param t Town which polulation has changed belugas@7030: * @param mod polulation change (can be positive or negative) belugas@7030: */ 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: belugas@7030: /** belugas@7030: * Determines the world population belugas@7030: * Basically, count population of all towns, one by one belugas@7030: * @return uint32 the calculated population of the world belugas@7030: */ rubidium@6573: uint32 GetWorldPopulation() celestar@1080: { celestar@1080: uint32 pop; tron@2630: const Town* t; tron@2630: celestar@1080: pop = 0; tron@2639: FOR_ALL_TOWNS(t) pop += t->population; celestar@1080: return pop; celestar@1080: } celestar@1080: belugas@7030: /** belugas@7030: * Helper function for house completion stages progression belugas@7030: * @param tile TileIndex of the house (or parts of it) to "grow" belugas@7030: */ tron@1977: static void MakeSingleHouseBigger(TileIndex tile) truelight@0: { tron@1035: assert(IsTileType(tile, MP_HOUSE)); truelight@193: belugas@7030: /* means it is completed, get out. */ celestar@3426: if (LiftHasDestination(tile)) return; truelight@0: belugas@7030: /* progress in construction stages */ belugas@3432: IncHouseConstructionTick(tile); belugas@3432: if (GetHouseConstructionTick(tile) != 0) return; truelight@0: belugas@7030: /* Check and/or */ maedhros@6658: if (HASBIT(GetHouseSpecs(GetHouseType(tile))->callback_mask, CBM_CONSTRUCTION_STATE_CHANGE)) { maedhros@6658: uint16 callback_res = GetHouseCallback(CBID_CONSTRUCTION_STATE_CHANGE, 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: belugas@7030: /** Make the house advances in its construction stages until completion belugas@7030: * @param tile TileIndex of house belugas@7030: */ 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: belugas@7030: /** belugas@7030: * Periodic tic handler for houses and town belugas@7030: * @param tile been asked to do its stuff belugas@7030: */ tron@1977: static void TileLoop_Town(TileIndex tile) truelight@0: { truelight@0: Town *t; truelight@0: uint32 r; maedhros@6658: HouseID house_id = GetHouseType(tile); maedhros@6658: HouseSpec *hs = GetHouseSpecs(house_id); 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)) { belugas@6918: /* Construction is not completed. See if we can go further in construction*/ truelight@0: MakeTownHouseBigger(tile); truelight@0: return; truelight@0: } truelight@0: maedhros@6658: /* If the lift has a destination, it is already an animated tile. */ belugas@7030: if ((hs->building_flags & BUILDING_IS_ANIMATED) && belugas@7030: house_id < NEW_HOUSE_OFFSET && belugas@7030: !LiftHasDestination(tile) && belugas@7030: CHANCE16(1, 2)) belugas@7030: AddAnimatedTile(tile); truelight@0: tron@3319: t = GetTownByTile(tile); truelight@0: truelight@0: r = Random(); truelight@0: maedhros@6658: if (GB(r, 0, 8) < hs->population) { tron@2150: uint amt = GB(r, 0, 8) / 8 + 1; tron@2150: uint moved; tron@2150: truelight@0: if (_economy.fluct <= 0) amt = (amt + 1) >> 1; truelight@0: t->new_max_pass += amt; truelight@0: moved = MoveGoodsToStation(tile, 1, 1, CT_PASSENGERS, amt); truelight@0: t->new_act_pass += moved; truelight@0: } truelight@0: maedhros@6658: if (GB(r, 8, 8) < hs->mail_generation) { tron@2150: uint amt = GB(r, 8, 8) / 8 + 1; tron@2150: uint moved; tron@2150: truelight@0: if (_economy.fluct <= 0) amt = (amt + 1) >> 1; truelight@0: t->new_max_mail += amt; truelight@0: moved = MoveGoodsToStation(tile, 1, 1, CT_MAIL, amt); truelight@0: t->new_act_mail += moved; truelight@0: } truelight@0: maedhros@6658: _current_player = OWNER_TOWN; maedhros@6658: belugas@7030: if (hs->building_flags & BUILDING_HAS_1_TILE && belugas@7030: HASBIT(t->flags12, TOWN_IS_FUNDED) && belugas@7030: CanDeleteHouse(tile) && maedhros@7101: max(_cur_year - GetHouseConstructionYear(tile), 0) >= hs->minimum_life && belugas@7030: --t->time_until_rebuild == 0) { maedhros@6950: t->time_until_rebuild = GB(r, 16, 8) + 192; truelight@0: truelight@0: ClearTownHouse(t, tile); truelight@193: maedhros@6950: /* Rebuild with another house? */ tron@2150: if (GB(r, 24, 8) >= 12) DoBuildTownHouse(t, tile); maedhros@6658: } truelight@314: maedhros@6658: _current_player = OWNER_NONE; truelight@0: } truelight@0: belugas@7030: /** belugas@7030: * Unused handler belugas@7030: * @param tile unused belugas@7030: */ tron@1977: static void ClickTile_Town(TileIndex tile) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: tron@1977: static int32 ClearTile_Town(TileIndex tile, byte flags) truelight@0: { maedhros@6658: int rating; truelight@0: int32 cost; truelight@0: Town *t; maedhros@6658: HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); truelight@0: truelight@0: if (flags&DC_AUTO && !(flags&DC_AI_BUILDING)) return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED); maedhros@6658: if (!CanDeleteHouse(tile)) return CMD_ERROR; truelight@0: maedhros@6658: cost = _price.remove_house * hs->removal_cost >> 8; truelight@0: maedhros@6658: rating = hs->remove_rating_decrease; truelight@0: _cleared_town_rating += rating; tron@3319: _cleared_town = t = GetTownByTile(tile); truelight@193: Darkvater@4850: if (IsValidPlayer(_current_player)) { truelight@0: if (rating > t->ratings[_current_player] && !(flags & DC_NO_TOWN_RATING) && !_cheats.magic_bulldozer.value) { tron@534: SetDParam(0, t->index); truelight@0: return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES); truelight@193: } truelight@0: } truelight@0: truelight@0: if (flags & DC_EXEC) { celestar@1005: ChangeTownRating(t, -rating, RATING_HOUSE_MINIMUM); truelight@0: ClearTownHouse(t, tile); truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: tron@1977: static void GetAcceptedCargo_Town(TileIndex tile, AcceptedCargo ac) truelight@0: { maedhros@6658: HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); peter1138@7021: CargoID accepts[3]; truelight@193: peter1138@7021: /* Set the initial accepted cargo types */ peter1138@7021: for (uint8 i = 0; i < lengthof(accepts); i++) { peter1138@7021: accepts[i] = hs->accepts_cargo[i]; peter1138@7021: } peter1138@7021: peter1138@7021: /* Check for custom accepted cargo types */ peter1138@7021: if (HASBIT(hs->callback_mask, CBM_HOUSE_ACCEPT_CARGO)) { peter1138@7021: uint16 callback = GetHouseCallback(CBID_HOUSE_ACCEPT_CARGO, 0, GetHouseType(tile), GetTownByTile(tile), tile); peter1138@7021: if (callback != CALLBACK_FAILED) { peter1138@7021: /* Replace accepted cargo types with translated values from callback */ peter1138@7021: accepts[0] = GetCargoTranslation(GB(callback, 0, 5), hs->grffile); peter1138@7021: accepts[1] = GetCargoTranslation(GB(callback, 5, 5), hs->grffile); peter1138@7021: accepts[2] = GetCargoTranslation(GB(callback, 10, 5), hs->grffile); peter1138@7021: } peter1138@7021: } peter1138@7021: peter1138@7021: /* Check for custom cargo acceptance */ peter1138@7021: if (HASBIT(hs->callback_mask, CBM_CARGO_ACCEPTANCE)) { peter1138@7021: uint16 callback = GetHouseCallback(CBID_HOUSE_CARGO_ACCEPTANCE, 0, GetHouseType(tile), GetTownByTile(tile), tile); peter1138@7021: if (callback != CALLBACK_FAILED) { peter1138@7021: if (accepts[0] != CT_INVALID) ac[accepts[0]] = GB(callback, 0, 4); peter1138@7021: if (accepts[1] != CT_INVALID) ac[accepts[1]] = GB(callback, 4, 4); peter1138@7021: if (_opt.landscape != LT_TEMPERATE && HASBIT(callback, 12)) { peter1138@7021: /* The 'S' bit indicates food instead of goods */ peter1138@7021: ac[CT_FOOD] = GB(callback, 8, 4); peter1138@7021: } else { peter1138@7021: if (accepts[2] != CT_INVALID) ac[accepts[2]] = GB(callback, 8, 4); peter1138@7021: } peter1138@7021: return; peter1138@7021: } peter1138@7021: } peter1138@7021: peter1138@7021: /* No custom acceptance, so fill in with the default values */ peter1138@7021: for (uint8 i = 0; i < lengthof(accepts); i++) { peter1138@7021: if (accepts[i] != CT_INVALID) ac[accepts[i]] = hs->cargo_acceptance[i]; peter1138@7021: } 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: tron@1977: static uint32 GetTileTrackStatus_Town(TileIndex tile, TransportType mode) truelight@0: { truelight@0: /* not used */ truelight@0: return 0; truelight@0: } truelight@0: Darkvater@2436: static void ChangeTileOwner_Town(TileIndex tile, PlayerID old_player, PlayerID new_player) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: truelight@0: tron@909: static const TileIndexDiffC _roadblock_tileadd[] = { tron@909: { 0, -1}, tron@909: { 1, 0}, tron@909: { 0, 1}, tron@909: {-1, 0}, truelight@193: belugas@6918: /* Store the first 3 elements again. belugas@6918: * Lets us rotate without using &3. */ tron@909: { 0, -1}, tron@909: { 1, 0}, tron@909: { 0, 1} truelight@0: }; truelight@0: belugas@7067: /** belugas@7067: * Distance multiplyer belugas@7067: * Defines the possible distances between 2 road tiles belugas@7067: */ belugas@7067: enum RoadBlockTitleDistance { belugas@7067: RB_TILE_DIST1 = 1, ///< 1 tile between belugas@7067: RB_TILE_DIST2, ///< 2 tiles between belugas@7067: }; tron@2817: tron@2817: static bool GrowTown(Town *t); tron@2817: truelight@0: static void TownTickHandler(Town *t) truelight@0: { belugas@3432: if (HASBIT(t->flags12, TOWN_IS_FUNDED)) { truelight@0: int i = t->grow_counter - 1; truelight@0: if (i < 0) { truelight@0: if (GrowTown(t)) { truelight@0: i = t->growth_rate; truelight@0: } else { truelight@193: i = 0; truelight@0: } truelight@0: } truelight@0: t->grow_counter = i; truelight@0: } truelight@0: truelight@0: UpdateTownRadius(t); truelight@0: } truelight@0: 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: tron@3150: static RoadBits GetTownRoadMask(TileIndex tile) truelight@0: { tron@3150: TrackBits b = GetAnyRoadTrackBits(tile); rubidium@5838: RoadBits r = ROAD_NONE; tron@2639: tron@3150: if (b & TRACK_BIT_X) r |= ROAD_X; tron@3150: if (b & TRACK_BIT_Y) r |= ROAD_Y; tron@3150: if (b & TRACK_BIT_UPPER) r |= ROAD_NE | ROAD_NW; tron@3150: if (b & TRACK_BIT_LOWER) r |= ROAD_SE | ROAD_SW; tron@3150: if (b & TRACK_BIT_LEFT) r |= ROAD_NW | ROAD_SW; tron@3150: if (b & TRACK_BIT_RIGHT) r |= ROAD_NE | ROAD_SE; truelight@0: return r; truelight@0: } truelight@0: belugas@7067: /** belugas@7067: * Check if a neighboring tile has a road belugas@7067: * belugas@7067: * @param tile curent tile belugas@7067: * @param dir target direction belugas@7067: * @param dist_multi distance multiplyer belugas@7067: * @return true if one of the neighboring tiles at the belugas@7067: * given distance is a road tile else belugas@7067: */ belugas@7067: static bool NeighborIsRoadTile(TileIndex tile, int dir, RoadBlockTitleDistance dist_multi) belugas@7067: { belugas@7067: return (HASBIT(GetTownRoadMask(TILE_ADD(tile, dist_multi * ToTileIndexDiff(_roadblock_tileadd[dir + 1]))), dir ^ 2) || belugas@7067: HASBIT(GetTownRoadMask(TILE_ADD(tile, dist_multi * ToTileIndexDiff(_roadblock_tileadd[dir + 3]))), dir ^ 2) || belugas@7067: HASBIT(GetTownRoadMask(TILE_ADD(tile, dist_multi * (ToTileIndexDiff(_roadblock_tileadd[dir + 1]) + ToTileIndexDiff(_roadblock_tileadd[dir + 2])))), dir) || belugas@7067: HASBIT(GetTownRoadMask(TILE_ADD(tile, dist_multi * (ToTileIndexDiff(_roadblock_tileadd[dir + 3]) + ToTileIndexDiff(_roadblock_tileadd[dir + 2])))), dir)); belugas@7067: } belugas@7067: tron@1977: static bool IsRoadAllowedHere(TileIndex tile, int dir) truelight@0: { tron@3636: Slope k; tron@3636: Slope slope; truelight@0: belugas@6918: /* If this assertion fails, it might be because the world contains belugas@6918: * land at the edges. This is not ok. */ truelight@0: TILE_ASSERT(tile); truelight@193: tron@2639: for (;;) { belugas@6918: /* Check if there already is a road at this point? */ tron@3150: if (GetAnyRoadTrackBits(tile) == 0) { belugas@6918: /* No, try to build one in the direction. belugas@6918: * if that fails clear the land, and if that fails exit. belugas@6918: * This is to make sure that we can build a road here later. */ tron@3491: if (CmdFailed(DoCommand(tile, (dir & 1 ? ROAD_X : ROAD_Y), 0, DC_AUTO, CMD_BUILD_ROAD)) && tron@3491: CmdFailed(DoCommand(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR))) truelight@0: return false; truelight@0: } truelight@0: truelight@0: slope = GetTileSlope(tile, NULL); tron@3636: if (slope == SLOPE_FLAT) { pasky@465: no_slope: belugas@7067: /* Tile has no slope */ belugas@7067: switch (_patches.town_layout) { belugas@7067: default: NOT_REACHED(); belugas@7067: belugas@7067: case TL_ORIGINAL: /* Disallow the road if any neighboring tile has a road (distance: 1) */ belugas@7067: return !NeighborIsRoadTile(tile, dir, RB_TILE_DIST1); belugas@7067: belugas@7067: case TL_BETTER_ROADS: /* Disallow the road if any neighboring tile has a road (distance: 1 and 2). */ belugas@7067: return !(NeighborIsRoadTile(tile, dir, RB_TILE_DIST1) || belugas@7067: NeighborIsRoadTile(tile, dir, RB_TILE_DIST2)); belugas@7067: } truelight@0: } truelight@193: belugas@6918: /* If the tile is not a slope in the right direction, then belugas@6918: * maybe terraform some. */ tron@3636: k = (dir & 1) ? SLOPE_NE : SLOPE_NW; tron@3636: if (k != slope && ComplementSlope(k) != slope) { truelight@0: uint32 r = Random(); truelight@0: tron@2639: if (CHANCE16I(1, 8, r) && !_generating_world) { pasky@465: int32 res; pasky@465: tron@2639: if (CHANCE16I(1, 16, r)) { tron@3491: res = DoCommand(tile, slope, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, pasky@465: CMD_TERRAFORM_LAND); tron@2639: } else { tron@3639: res = DoCommand(tile, slope ^ 0xF, 1, DC_EXEC | DC_AUTO | DC_NO_WATER, pasky@465: CMD_TERRAFORM_LAND); tron@2639: } tron@2646: if (CmdFailed(res) && CHANCE16I(1, 3, r)) { belugas@6918: /* We can consider building on the slope, though. */ pasky@465: goto no_slope; tron@2646: } truelight@0: } truelight@0: return false; truelight@0: } truelight@835: return true; truelight@0: } truelight@0: } truelight@0: tron@1977: static bool TerraformTownTile(TileIndex tile, int edges, int dir) truelight@0: { truelight@0: int32 r; truelight@193: truelight@0: TILE_ASSERT(tile); truelight@0: tron@3491: r = DoCommand(tile, edges, dir, DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND); tron@2646: if (CmdFailed(r) || r >= 126 * 16) return false; tron@3491: DoCommand(tile, edges, dir, DC_AUTO | DC_NO_WATER | DC_EXEC, CMD_TERRAFORM_LAND); truelight@0: return true; truelight@0: } truelight@0: tron@1977: static void LevelTownLand(TileIndex tile) truelight@0: { tron@3636: Slope tileh; truelight@0: truelight@0: TILE_ASSERT(tile); truelight@0: belugas@6918: /* Don't terraform if land is plain or if there's a house there. */ tron@3055: if (IsTileType(tile, MP_HOUSE)) return; tron@3055: tileh = GetTileSlope(tile, NULL); tron@3636: if (tileh == SLOPE_FLAT) return; truelight@0: belugas@6918: /* First try up, then down */ tron@3055: if (!TerraformTownTile(tile, ~tileh & 0xF, 1)) { tron@3055: TerraformTownTile(tile, tileh & 0xF, 0); truelight@0: } truelight@0: } truelight@0: belugas@7067: /** belugas@7067: * Generate the RoadBits of a grid tile belugas@7067: * belugas@7067: * @param t current town belugas@7067: * @param tile tile in reference to the town belugas@7067: * @return the RoadBit of the current tile regarding belugas@7067: * the selected town layout belugas@7067: */ belugas@7067: static RoadBits GetTownRoadGridElement(Town* t, TileIndex tile) belugas@7067: { belugas@7067: /* align the grid to the downtown */ belugas@7067: TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile); ///< Vector from downtown to the tile belugas@7067: belugas@7067: /* lx, ly description: belugas@7067: * @li lx and ly are true if the tile is a crossing tile. belugas@7067: * @li lx xor ly are true if the tile is a straight road tile. belugas@7067: * @li lx and ly are false if the tile is a house tile. belugas@7067: */ belugas@7067: bool lx, ly; belugas@7067: belugas@7067: switch (_patches.town_layout) { belugas@7067: default: NOT_REACHED(); belugas@7067: belugas@7067: case TL_2X2_GRID: belugas@7067: lx = ((grid_pos.x % 3) == 0); belugas@7067: ly = ((grid_pos.y % 3) == 0); belugas@7067: break; belugas@7067: belugas@7067: case TL_3X3_GRID: belugas@7067: lx = ((grid_pos.x % 4) == 0); belugas@7067: ly = ((grid_pos.y % 4) == 0); belugas@7067: break; belugas@7067: } belugas@7067: belugas@7067: /* generate the basic grid structure */ belugas@7067: if (!lx && !ly) { ///< It is a house tile belugas@7067: return ROAD_NONE; belugas@7067: } else if (lx && !ly) { ///< It is a Y-dir road tile belugas@7067: return ROAD_Y; belugas@7067: } else if (!lx && ly) { ///< It is a X-dir road tile belugas@7067: return ROAD_X; belugas@7067: } else { ///< It is a crossing tile belugas@7067: /* Presets for junctions on slopes belugas@7067: * not nice :( */ belugas@7067: switch (GetTileSlope(tile, NULL)) { belugas@7067: case SLOPE_W: belugas@7067: return ROAD_NW | ROAD_SW; belugas@7067: case SLOPE_S: belugas@7067: return ROAD_SE | ROAD_SW; belugas@7067: case SLOPE_SW: belugas@7067: return ROAD_Y | ROAD_SW; belugas@7067: case SLOPE_E: belugas@7067: return ROAD_NE | ROAD_SE; belugas@7067: case SLOPE_SE: belugas@7067: return ROAD_X | ROAD_SE; belugas@7067: case SLOPE_N: belugas@7067: return ROAD_NW | ROAD_NE; belugas@7067: case SLOPE_NW: belugas@7067: return ROAD_X | ROAD_NW; belugas@7067: case SLOPE_NE: belugas@7067: return ROAD_Y | ROAD_NE; belugas@7067: case SLOPE_STEEP_W: belugas@7067: case SLOPE_STEEP_N: belugas@7067: return ROAD_X; belugas@7067: case SLOPE_STEEP_S: belugas@7067: case SLOPE_STEEP_E: belugas@7067: return ROAD_Y; belugas@7067: default: belugas@7067: return ROAD_ALL; belugas@7067: } belugas@7067: } belugas@7067: } belugas@7067: belugas@7067: /** belugas@7067: * Check there are enougth neighbor house tiles next to the current tile belugas@7067: * belugas@7067: * @param tile current tile belugas@7067: * @return true if there are more than 2 house tiles next belugas@7067: * to the current one belugas@7067: */ belugas@7067: static bool NeighborsAreHouseTiles(TileIndex tile) belugas@7067: { belugas@7067: uint counter = 0; ///< counts the house neighbor tiles belugas@7067: belugas@7067: /* We can't look further than that. */ belugas@7067: if (TileX(tile) < 1 || TileY(tile) < 1) { belugas@7067: return false; belugas@7067: } belugas@7067: belugas@7067: /* Check the tiles E,N,W and S of the current tile. */ belugas@7067: for (uint i = 0; i < 4; i++) { belugas@7067: if (IsTileType(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[i])), MP_HOUSE)) { belugas@7067: counter++; belugas@7067: } belugas@7067: belugas@7067: /* If there are enougth neighbor's stop it here */ belugas@7067: if (counter >= 3) { belugas@7067: return true; belugas@7067: } belugas@7067: } belugas@7067: return false; belugas@7067: } belugas@7067: belugas@7067: /** belugas@7067: * Grows the given town. belugas@7067: * There are at the moment 3 possible way's for belugas@7067: * the town expansion: belugas@7067: * @li Generate a random tile and check if there is a road allowed belugas@7067: * @li TL_ORIGINAL belugas@7067: * @li TL_BETTER_ROADS belugas@7067: * @li Check if the town geometry allows a road and which one belugas@7067: * @li TL_2X2_GRID belugas@7067: * @li TL_3X3_GRID belugas@7067: * @li Forbid roads, only build houses belugas@7067: * @li TL_NO_ROADS belugas@7067: * belugas@7067: * @param tile_ptr current tile belugas@7067: * @param mask current tiles RoadBits belugas@7067: * @param block road block belugas@7067: * @param t1 current town belugas@7067: */ tron@3150: static void GrowTownInTile(TileIndex* tile_ptr, RoadBits mask, int block, Town* t1) truelight@0: { tron@3150: RoadBits rcmd; tron@1977: TileIndex tmptile; tron@3150: DiagDirection i; truelight@0: int j; tron@1977: TileIndex tile = *tile_ptr; truelight@0: truelight@0: TILE_ASSERT(tile); truelight@0: truelight@0: if (mask == 0) { tron@3150: int a; tron@3150: int b; tron@3150: belugas@6918: /* Tile has no road. First reset the status counter belugas@6918: * to say that this is the last iteration. */ truelight@0: _grow_town_result = 0; truelight@0: belugas@6918: /* Remove hills etc */ truelight@0: LevelTownLand(tile); truelight@0: belugas@6918: /* Is a road allowed here? */ belugas@7067: switch (_patches.town_layout) { belugas@7067: default: NOT_REACHED(); belugas@7067: belugas@7067: case TL_NO_ROADS: /* Disallow Roads */ belugas@7067: return; belugas@7067: belugas@7067: case TL_3X3_GRID: belugas@7067: case TL_2X2_GRID: belugas@7067: rcmd = GetTownRoadGridElement(t1, tile); belugas@7067: if (rcmd == ROAD_NONE) { belugas@7067: return; belugas@7067: } belugas@7067: break; belugas@7067: belugas@7067: case TL_BETTER_ROADS: belugas@7067: case TL_ORIGINAL: belugas@7067: if (!IsRoadAllowedHere(tile, block)) { belugas@7067: return; belugas@7067: } belugas@7067: belugas@7067: /* Randomize new road block numbers */ belugas@7067: a = block; belugas@7067: b = block ^ 2; belugas@7067: if (CHANCE16(1, 4)) { belugas@7067: do { belugas@7067: a = GB(Random(), 0, 2); belugas@7067: } while (a == b); belugas@7067: } belugas@7067: belugas@7067: if (!IsRoadAllowedHere(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a])), a)) { belugas@7067: /* A road is not allowed to continue the randomized road, belugas@7067: * return if the road we're trying to build is curved. */ belugas@7067: if (a != (b ^ 2)) { belugas@7067: return; belugas@7067: } belugas@7067: belugas@7067: /* Return if neither side of the new road is a house */ belugas@7067: if (!IsTileType(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a + 1])), MP_HOUSE) && belugas@7067: !IsTileType(TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[a + 3])), MP_HOUSE)) { belugas@7067: return; belugas@7067: } belugas@7067: belugas@7067: /* That means that the road is only allowed if there is a house belugas@7067: * at any side of the new road. */ belugas@7067: } belugas@7067: belugas@7067: rcmd = (RoadBits)((1 << a) + (1 << b)); belugas@7067: break; tron@2637: } truelight@0: rubidium@6987: } else if (block < 5 && !HASBIT(mask, block ^ 2)) { belugas@6918: /* Continue building on a partial road. belugas@6918: * Always OK. */ truelight@0: _grow_town_result = 0; belugas@7067: belugas@7067: switch (_patches.town_layout) { belugas@7067: default: NOT_REACHED(); belugas@7067: belugas@7067: case TL_NO_ROADS: /* Disallow Roads */ belugas@7067: return; belugas@7067: belugas@7067: case TL_3X3_GRID: belugas@7067: case TL_2X2_GRID: belugas@7067: rcmd = GetTownRoadGridElement(t1, tile); belugas@7067: break; belugas@7067: belugas@7067: case TL_BETTER_ROADS: belugas@7067: case TL_ORIGINAL: belugas@7067: rcmd = (RoadBits)(1 << (block ^ 2)); belugas@7067: break; belugas@7067: } truelight@0: } else { tron@3150: int i; belugas@7067: bool allow_house = false; belugas@7067: TileIndex tmptile2; truelight@0: belugas@6918: /* Reached a tunnel/bridge? Then continue at the other side of it. */ rubidium@5696: if (IsTileType(tile, MP_TUNNELBRIDGE)) { rubidium@5696: if (IsTunnel(tile) && GetTunnelTransportType(tile) == TRANSPORT_ROAD) { rubidium@5696: *tile_ptr = GetOtherTunnelEnd(tile); rubidium@5696: } else if (IsBridge(tile) && GetBridgeTransportType(tile) == TRANSPORT_ROAD) { rubidium@5696: *tile_ptr = GetOtherBridgeEnd(tile); rubidium@5696: } truelight@0: return; truelight@0: } truelight@193: belugas@6918: /* Possibly extend the road in a direction. belugas@6918: * Randomize a direction and if it has a road, bail out. */ tron@2635: i = GB(Random(), 0, 2); tron@2639: if (HASBIT(mask, i)) return; truelight@0: belugas@6918: /* This is the tile we will reach if we extend to this direction. */ tron@909: tmptile = TILE_ADD(tile, ToTileIndexDiff(_roadblock_tileadd[i])); truelight@193: belugas@6918: /* Don't do it if it reaches to water. */ celestar@3433: if (IsClearWaterTile(tmptile)) return; truelight@0: belugas@7067: switch (_patches.town_layout) { belugas@7067: default: NOT_REACHED(); belugas@7067: belugas@7067: case TL_NO_ROADS: belugas@7067: allow_house = true; belugas@7067: break; belugas@7067: belugas@7067: case TL_3X3_GRID: /* Use 2x2 grid afterwards! */ belugas@7067: /* Fill gap if house has enougth neighbors */ belugas@7067: tmptile2 = TILE_ADD(tmptile, ToTileIndexDiff(_roadblock_tileadd[i])); belugas@7067: if (NeighborsAreHouseTiles(tmptile2) && BuildTownHouse(t1, tmptile2)) { belugas@7067: _grow_town_result = -1; belugas@7067: } belugas@7067: belugas@7067: case TL_2X2_GRID: belugas@7067: rcmd = GetTownRoadGridElement(t1, tmptile); belugas@7067: allow_house = (rcmd == ROAD_NONE); belugas@7067: break; belugas@7067: belugas@7067: case TL_BETTER_ROADS: /* Use original afterwards! */ belugas@7067: /* Fill gap if house has enougth neighbors */ belugas@7067: tmptile2 = TILE_ADD(tmptile, ToTileIndexDiff(_roadblock_tileadd[i])); belugas@7067: if (NeighborsAreHouseTiles(tmptile2) && BuildTownHouse(t1, tmptile2)) { belugas@7067: _grow_town_result = -1; belugas@7067: } belugas@7067: belugas@7067: case TL_ORIGINAL: belugas@7067: /* Allow a house at the edge. 60% chance or belugas@7067: * always ok if no road allowed. */ belugas@7067: allow_house = (!IsRoadAllowedHere(tmptile, i) || CHANCE16(6, 10)); belugas@7067: break; belugas@7067: } belugas@7067: belugas@7067: belugas@7067: if (allow_house) { belugas@7067: /* Build a house, but not if there already is a house there. */ tron@1035: if (!IsTileType(tmptile, MP_HOUSE)) { belugas@6918: /* Level the land if possible */ truelight@0: LevelTownLand(tmptile); truelight@0: belugas@6918: /* And build a house. belugas@6918: * Set result to -1 if we managed to build it. */ belugas@7067: if (BuildTownHouse(t1, tmptile)) { belugas@7067: _grow_town_result = -1; belugas@7067: } truelight@0: } truelight@0: return; truelight@0: } truelight@0: truelight@0: _grow_town_result = 0; rubidium@5838: rcmd = (RoadBits)(1 << i); truelight@0: } truelight@0: belugas@6918: /* Return if a water tile */ celestar@3433: if (IsClearWaterTile(tile)) return; truelight@0: belugas@6918: /* Determine direction of slope, belugas@6918: * and build a road if not a special slope. */ tron@3055: switch (GetTileSlope(tile, NULL)) { tron@3636: case SLOPE_SW: i = DIAGDIR_NE; break; tron@3636: case SLOPE_SE: i = DIAGDIR_NW; break; tron@3636: case SLOPE_NW: i = DIAGDIR_SE; break; tron@3636: case SLOPE_NE: i = DIAGDIR_SW; break; tron@3055: tron@3055: default: truelight@0: build_road_and_exit: tron@3491: if (!CmdFailed(DoCommand(tile, rcmd, t1->index, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD))) { tron@3055: _grow_town_result = -1; tron@3055: } tron@3055: return; truelight@0: } truelight@0: belugas@7067: /* Check if the bridge is in the right direction */ belugas@7067: if ((rcmd == ROAD_X && (i == DIAGDIR_NW || i == DIAGDIR_SE)) || belugas@7067: (rcmd == ROAD_Y && (i == DIAGDIR_NE || i == DIAGDIR_SW))) { belugas@7067: goto build_road_and_exit; belugas@7067: } belugas@7067: truelight@0: tmptile = tile; truelight@0: belugas@6918: /* Now it contains the direction of the slope */ rubidium@4434: j = -11; // max 11 tile long bridges truelight@0: do { truelight@0: if (++j == 0) truelight@0: goto build_road_and_exit; Darkvater@4559: tmptile = TILE_MASK(tmptile + TileOffsByDiagDir(i)); celestar@3433: } while (IsClearWaterTile(tmptile)); truelight@0: belugas@6918: /* no water tiles in between? */ truelight@0: if (j == -10) truelight@0: goto build_road_and_exit; truelight@0: belugas@6918: /* Quit if it selecting an appropiate bridge type fails a large number of times. */ truelight@0: j = 22; truelight@0: { truelight@0: int32 bridge_len = GetBridgeLength(tile, tmptile); truelight@0: do { truelight@0: byte bridge_type = RandomRange(MAX_BRIDGES - 1); truelight@0: if (CheckBridge_Stuff(bridge_type, bridge_len)) { tron@3491: if (!CmdFailed(DoCommand(tile, tmptile, 0x8000 + bridge_type, DC_EXEC | DC_AUTO, CMD_BUILD_BRIDGE))) truelight@0: _grow_town_result = -1; truelight@0: belugas@6918: /* obviously, if building any bridge would fail, there is no need to try other bridge-types */ truelight@0: return; truelight@0: } tron@2952: } while (--j != 0); truelight@0: } truelight@0: } truelight@0: belugas@6918: /** Returns "growth" if a house was built, or no if the build failed. belugas@6918: * @param t town to inquiry belugas@6918: * @param tile to inquiry belugas@6918: * @return something other than zero(0)if town expansion was possible belugas@6918: */ tron@1977: static int GrowTownAtRoad(Town *t, TileIndex tile) truelight@0: { truelight@0: int block = 5; // special case truelight@0: truelight@0: TILE_ASSERT(tile); truelight@0: belugas@7067: /* Number of times to search. belugas@7067: * Better roads, 2X2 and 3X3 grid grow quite fast so we give belugas@7067: * them a little handicap. */ belugas@7067: switch (_patches.town_layout) { belugas@7067: case TL_BETTER_ROADS: belugas@7067: _grow_town_result = 10 + t->num_houses * 2 / 9; belugas@7067: break; belugas@7067: belugas@7067: case TL_3X3_GRID: belugas@7067: case TL_2X2_GRID: belugas@7067: _grow_town_result = 10 + t->num_houses * 1 / 9; belugas@7067: break; belugas@7067: belugas@7067: default: belugas@7067: _grow_town_result = 10 + t->num_houses * 4 / 9; belugas@7067: break; belugas@7067: } truelight@0: truelight@0: do { belugas@6918: /* Get a bitmask of the road blocks on a tile */ tron@3150: RoadBits mask = GetTownRoadMask(tile); truelight@0: belugas@6918: /* Try to grow the town from this point */ belugas@6918: GrowTownInTile(&tile, mask, block, t); truelight@0: belugas@6918: /* Exclude the source position from the bitmask belugas@6918: * and return if no more road blocks available */ KUDr@6453: ClrBitT(mask, (block ^ 2)); truelight@0: if (mask == 0) truelight@0: return _grow_town_result; truelight@0: belugas@6918: /* Select a random bit from the blockmask, walk a step belugas@6918: * and continue the search from there. */ rubidium@6987: do block = Random() & 3; while (!HASBIT(mask, block)); tron@909: tile += ToTileIndexDiff(_roadblock_tileadd[block]); truelight@0: truelight@1327: if (IsTileType(tile, MP_STREET)) { truelight@1327: /* Don't allow building over roads of other cities */ tron@4077: if (IsTileOwner(tile, OWNER_TOWN) && GetTownByTile(tile) != t) { truelight@1327: _grow_town_result = -1; tron@4077: } else if (_game_mode == GM_EDITOR) { truelight@1327: /* If we are in the SE, and this road-piece has no town owner yet, it just found an rubidium@4549: * owner :) (happy happy happy road now) */ tron@1902: SetTileOwner(tile, OWNER_TOWN); belugas@3432: SetTownIndex(tile, t->index); truelight@1327: } truelight@1327: } truelight@1280: belugas@6918: /* 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: belugas@6918: /** Generate a random road block belugas@6918: * The probability of a straight road belugas@6918: * is somewhat higher than a curved. */ 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@5838: return (RoadBits)((1 << a) + (1 << b)); truelight@0: } truelight@0: belugas@6918: /** Grow the town belugas@6918: * @Return true if a house was built, or no if the build failed. */ tron@2817: static bool GrowTown(Town *t) truelight@0: { tron@1977: TileIndex tile; tron@909: const TileIndexDiffC *ptr; tron@2498: PlayerID old_player; truelight@0: tron@909: static const TileIndexDiffC _town_coord_mod[] = { tron@909: {-1, 0}, tron@909: { 1, 1}, tron@909: { 1, -1}, tron@909: {-1, -1}, tron@909: {-1, 0}, tron@909: { 0, 2}, tron@909: { 2, 0}, tron@909: { 0, -2}, tron@909: {-1, -1}, tron@909: {-2, 2}, tron@909: { 2, 2}, tron@909: { 2, -2}, tron@909: { 0, 0} truelight@0: }; truelight@0: belugas@7067: /* Let the town be a ghost town belugas@7067: * The player wanted it in such a way. Thus there he has it. ;) belugas@7067: * Never reached in editor mode. */ belugas@7106: if (_patches.town_layout == TL_NO_ROADS && _generating_world) { belugas@7067: return false; belugas@7067: } belugas@7067: belugas@6918: /* Current player is a town */ truelight@260: old_player = _current_player; truelight@0: _current_player = OWNER_TOWN; truelight@0: belugas@6918: /* Find a road that we can base the construction on. */ truelight@0: tile = t->xy; tron@909: for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) { tron@3150: if (GetAnyRoadTrackBits(tile) != 0) { truelight@260: int r = GrowTownAtRoad(t, tile); truelight@260: _current_player = old_player; rubidium@5838: return r != 0; truelight@0: } tron@909: tile = TILE_ADD(tile, ToTileIndexDiff(*ptr)); tron@909: } truelight@0: belugas@6918: /* No road available, try to build a random road block by belugas@6918: * 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) { maedhros@7043: if (!CmdFailed(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: truelight@0: static void UpdateTownRadius(Town *t) truelight@0: { truelight@0: static const uint16 _town_radius_data[23][5] = { rubidium@4344: { 4, 0, 0, 0, 0}, // 0 rubidium@4344: { 16, 0, 0, 0, 0}, rubidium@4344: { 25, 0, 0, 0, 0}, rubidium@4344: { 36, 0, 0, 0, 0}, rubidium@4344: { 49, 0, 4, 0, 0}, rubidium@4344: { 64, 0, 4, 0, 0}, // 20 rubidium@4344: { 64, 0, 9, 0, 1}, rubidium@4344: { 64, 0, 9, 0, 4}, rubidium@4344: { 64, 0, 16, 0, 4}, rubidium@4344: { 81, 0, 16, 0, 4}, rubidium@4344: { 81, 0, 16, 0, 4}, // 40 rubidium@4344: { 81, 0, 25, 0, 9}, rubidium@4344: { 81, 36, 25, 0, 9}, rubidium@4344: { 81, 36, 25, 16, 9}, rubidium@4344: { 81, 49, 0, 25, 9}, rubidium@4344: { 81, 64, 0, 25, 9}, // 60 rubidium@4344: { 81, 64, 0, 36, 9}, rubidium@4344: { 81, 64, 0, 36, 16}, pasky@470: {100, 81, 0, 49, 16}, pasky@470: {100, 81, 0, 49, 25}, pasky@470: {121, 81, 0, 49, 25}, // 80 pasky@470: {121, 81, 0, 49, 25}, pasky@470: {121, 81, 0, 49, 36}, // 88 truelight@0: }; pasky@470: pasky@470: if (t->num_houses < 92) { pasky@470: memcpy(t->radius, _town_radius_data[t->num_houses / 4], sizeof(t->radius)); pasky@470: } else { pasky@470: int mass = t->num_houses / 8; belugas@6918: /* At least very roughly extrapolate. Empirical numbers dancing between belugas@6918: * overwhelming by cottages and skyscrapers outskirts. */ pasky@470: t->radius[0] = mass * mass; belugas@6918: /* Actually we are proportional to sqrt() but that's right because belugas@6918: * we are covering an area. */ pasky@470: t->radius[1] = mass * 7; pasky@470: t->radius[2] = 0; pasky@470: t->radius[3] = mass * 4; pasky@470: t->radius[4] = mass * 3; pasky@470: //debug("%d (->%d): %d %d %d %d\n", t->num_houses, mass, t->radius[0], t->radius[1], t->radius[3], t->radius[4]); pasky@470: } truelight@0: } truelight@0: pasky@1421: static bool CreateTownName(uint32 *townnameparts) truelight@0: { truelight@0: Town *t2; truelight@0: char buf1[64]; truelight@0: char buf2[64]; truelight@0: uint32 r; pasky@1421: /* Do not set too low tries, since when we run out of names, we loop pasky@1421: * for #tries only one time anyway - then we stop generating more pasky@1421: * towns. Do not show it too high neither, since looping through all pasky@1421: * the other towns may take considerable amount of time (10000 is pasky@1421: * too much). */ pasky@1421: int tries = 1000; pasky@1421: uint16 townnametype = SPECSTR_TOWNNAME_START + _opt.town_name; truelight@0: pasky@1421: assert(townnameparts); truelight@0: tron@2952: for (;;) { truelight@0: restart: truelight@0: r = Random(); truelight@0: tron@534: SetDParam(0, r); Darkvater@4912: GetString(buf1, townnametype, lastof(buf1)); truelight@193: belugas@6918: /* Check size and width */ Darkvater@4609: if (strlen(buf1) >= 31 || GetStringBoundingBox(buf1).width > 130) continue; truelight@0: truelight@0: FOR_ALL_TOWNS(t2) { belugas@6918: /* We can't just compare the numbers since belugas@6918: * 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: maedhros@6982: /** maedhros@6982: * Does the actual town creation. maedhros@6982: * maedhros@6982: * @param t The town maedhros@6982: * @param tile Where to put it maedhros@6982: * @param townnameparts The town name maedhros@6982: * @param size_mode How the size should be determined maedhros@6982: * @param size Parameter for size determination maedhros@6982: */ maedhros@6982: static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSizeMode size_mode, uint size) truelight@0: { truelight@0: int x, i; truelight@0: belugas@6918: /* clear the town struct */ truelight@0: i = t->index; truelight@0: memset(t, 0, sizeof(Town)); truelight@0: t->index = i; rubidium@5319: _total_towns++; truelight@0: truelight@0: t->xy = tile; truelight@0: t->num_houses = 0; truelight@0: t->time_until_rebuild = 10; truelight@0: UpdateTownRadius(t); truelight@0: t->flags12 = 0; truelight@0: t->population = 0; truelight@0: t->grow_counter = 0; truelight@0: t->growth_rate = 250; truelight@0: t->new_max_pass = 0; truelight@0: t->new_max_mail = 0; truelight@0: t->new_act_pass = 0; truelight@0: t->new_act_mail = 0; truelight@0: t->max_pass = 0; truelight@0: t->max_mail = 0; truelight@0: t->act_pass = 0; truelight@0: t->act_mail = 0; truelight@0: truelight@0: t->pct_pass_transported = 0; truelight@0: t->pct_mail_transported = 0; truelight@0: t->fund_buildings_months = 0; truelight@0: t->new_act_food = 0; darkvater@4: t->new_act_water = 0; truelight@0: t->act_food = 0; darkvater@4: t->act_water = 0; truelight@0: tron@2952: for (i = 0; i != MAX_PLAYERS; i++) truelight@0: t->ratings[i] = 500; truelight@0: truelight@0: t->have_ratings = 0; rubidium@5838: t->exclusivity = INVALID_PLAYER; dominik@121: t->exclusive_counter = 0; truelight@0: t->statues = 0; truelight@0: pasky@1421: t->townnametype = SPECSTR_TOWNNAME_START + _opt.town_name; pasky@1421: t->townnameparts = townnameparts; truelight@193: truelight@0: UpdateTownVirtCoord(t); truelight@0: _town_sort_dirty = true; truelight@0: maedhros@6982: /* Random town size. */ maedhros@6982: x = (Random() & 0xF) + 8; maedhros@6982: maedhros@6982: switch (size_mode) { maedhros@6982: default: NOT_REACHED(); maedhros@6982: maedhros@6982: case TSM_RANDOM: maedhros@6982: t->larger_town = false; maedhros@6982: break; maedhros@6982: maedhros@6982: case TSM_FIXED: maedhros@6982: x = size * 16 + 3; maedhros@6982: t->larger_town = false; maedhros@6982: break; maedhros@6982: maedhros@6982: case TSM_CITY: maedhros@6982: x *= _patches.initial_city_size; maedhros@6982: t->larger_town = true; maedhros@6982: break; celestar@3674: } truelight@0: truelight@0: t->num_houses += x; truelight@0: UpdateTownRadius(t); truelight@0: truelight@0: i = x * 4; truelight@0: do { truelight@0: GrowTown(t); truelight@0: } while (--i); truelight@0: truelight@0: t->num_houses -= x; truelight@0: UpdateTownRadius(t); truelight@0: UpdateTownMaxPass(t); truelight@0: } truelight@0: rubidium@6573: static Town *AllocateTown() truelight@0: { truelight@0: Town *t; truelight@4346: truelight@4346: /* We don't use FOR_ALL here, because FOR_ALL skips invalid items. truelight@4346: * TODO - This is just a temporary stage, this will be removed. */ tron@4983: for (t = GetTown(0); t != NULL; t = (t->index + 1U < GetTownPoolSize()) ? GetTown(t->index + 1U) : NULL) { truelight@4346: if (!IsValidTown(t)) { rubidium@4330: TownID index = t->index; truelight@1260: truelight@1260: memset(t, 0, sizeof(Town)); truelight@1260: t->index = index; truelight@1260: truelight@0: return t; truelight@0: } truelight@0: } truelight@1260: truelight@1260: /* Check if we can add a block to the pool */ tron@4983: if (AddBlockToPool(&_Town_pool)) truelight@1260: return AllocateTown(); truelight@1260: truelight@0: return NULL; truelight@0: } truelight@0: Darkvater@1793: /** Create a new town. Darkvater@1793: * This obviously only works in the scenario editor. Function not removed Darkvater@1793: * as it might be possible in the future to fund your own town :) tron@3491: * @param tile coordinates where town is built belugas@6918: * @param flags type of operation maedhros@6982: * @param p1 size of the town (0 = small, 1 = medium, 2 = large) maedhros@6982: * @param p2 size mode (@see TownSizeMode) Darkvater@1793: */ tron@3491: int32 CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: Town *t; pasky@1421: uint32 townnameparts; truelight@193: Darkvater@1793: /* Only in the scenario editor */ Darkvater@1793: if (_game_mode != GM_EDITOR) return CMD_ERROR; maedhros@6982: if (p2 > TSM_CITY) return CMD_ERROR; Darkvater@1793: truelight@0: SET_EXPENSES_TYPE(EXPENSES_OTHER); truelight@0: belugas@6918: /* 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: belugas@6918: /* 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: belugas@6918: /* 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: belugas@6918: /* Get a unique name for the town. */ pasky@1421: if (!CreateTownName(&townnameparts)) pasky@1421: return_cmd_error(STR_023A_TOO_MANY_TOWNS); pasky@1421: belugas@6918: /* Allocate town struct */ truelight@0: t = AllocateTown(); Darkvater@1793: if (t == NULL) return_cmd_error(STR_023A_TOO_MANY_TOWNS); truelight@0: belugas@6918: /* Create the town */ truelight@0: if (flags & DC_EXEC) { truelight@0: _generating_world = true; maedhros@6982: DoCreateTown(t, tile, townnameparts, (TownSizeMode)p2, p1); truelight@0: _generating_world = false; truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: maedhros@6982: Town *CreateRandomTown(uint attempts, TownSizeMode mode, uint size) truelight@0: { tron@1977: TileIndex tile; truelight@0: Town *t; pasky@1421: uint32 townnameparts; truelight@0: truelight@0: do { belugas@6918: /* Generate a tile index not too close from the edge */ ludde@2051: tile = RandomTile(); tron@2951: if (DistanceFromEdge(tile) < 20) continue; truelight@0: belugas@6918: /* Make sure the tile is plain */ tron@3636: if (!IsTileType(tile, MP_CLEAR) || GetTileSlope(tile, NULL) != SLOPE_FLAT) continue; truelight@0: belugas@6918: /* Check not too close to a town */ tron@2951: if (IsCloseToTown(tile, 20)) continue; truelight@193: belugas@6918: /* Get a unique name for the town. */ tron@2951: if (!CreateTownName(&townnameparts)) break; pasky@1421: belugas@6918: /* Allocate a town struct */ truelight@0: t = AllocateTown(); tron@2951: if (t == NULL) break; truelight@0: maedhros@6982: DoCreateTown(t, tile, townnameparts, mode, size); truelight@0: return t; celestar@1362: } while (--attempts); truelight@0: return NULL; truelight@0: } truelight@0: belugas@7056: 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)); maedhros@6982: 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); belugas@6918: /* try 20 times to create a random-sized town for the first loop. */ maedhros@6982: TownSizeMode mode = num_cities > 0 ? TSM_CITY : TSM_RANDOM; maedhros@6982: if (CreateRandomTown(20, mode, _patches.initial_city_size) != NULL) num++; maedhros@6982: if (num_cities > 0) num_cities--; celestar@1362: } while (--n); celestar@1362: belugas@6918: /* give it a last try, but now more aggressive */ maedhros@6982: 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: tron@3636: static bool CheckBuildHouseMode(TileIndex tile, Slope tileh, int mode) tron@1977: { truelight@0: int b; tron@3636: Slope slope; truelight@0: truelight@0: static const byte _masks[8] = { truelight@0: 0xC,0x3,0x9,0x6, truelight@0: 0x3,0xC,0x6,0x9, truelight@0: }; truelight@0: truelight@0: slope = GetTileSlope(tile, NULL); tron@3636: if (IsSteepSlope(slope)) return false; truelight@0: celestar@5573: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return false; celestar@5573: truelight@0: b = 0; tron@3636: if ((slope != SLOPE_FLAT && ~slope & _masks[mode])) b = ~b; tron@3636: if ((tileh != SLOPE_FLAT && ~tileh & _masks[mode+4])) b = ~b; truelight@0: if (b) truelight@0: return false; truelight@0: tron@3491: return !CmdFailed(DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR)); truelight@0: } truelight@0: tron@4000: tron@4000: uint GetTownRadiusGroup(const Town* t, TileIndex tile) truelight@0: { tron@4000: uint dist = DistanceSquare(tile, t->xy); tron@4000: uint smallest; tron@4000: uint i; truelight@0: tron@4000: if (t->fund_buildings_months && dist <= 25) return 4; truelight@0: truelight@0: smallest = 0; Darkvater@2436: for (i = 0; i != lengthof(t->radius); i++) { tron@4000: if (dist < t->radius[i]) smallest = i; truelight@0: } truelight@0: truelight@0: return smallest; truelight@0: } truelight@0: tron@2958: static bool CheckFree2x2Area(TileIndex tile) truelight@0: { truelight@0: int i; truelight@0: tron@909: static const TileIndexDiffC _tile_add[] = { tron@909: {0 , 0 }, tron@909: {0 - 0, 1 - 0}, tron@909: {1 - 0, 0 - 1}, tron@909: {1 - 1, 1 - 0} truelight@0: }; truelight@0: tron@2952: for (i = 0; i != 4; i++) { tron@909: tile += ToTileIndexDiff(_tile_add[i]); truelight@0: tron@3636: if (GetTileSlope(tile, NULL) != SLOPE_FLAT) return false; truelight@0: celestar@5573: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return false; celestar@5573: tron@3491: if (CmdFailed(DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER | DC_FORCETEST, CMD_LANDSCAPE_CLEAR))) truelight@0: return false; truelight@0: } truelight@0: truelight@0: return true; truelight@0: } truelight@0: tron@1977: static void DoBuildTownHouse(Town *t, TileIndex tile) truelight@0: { truelight@0: int i; truelight@0: uint bitmask; maedhros@6658: HouseID house; tron@3636: Slope slope; tron@1335: uint z; belugas@3432: uint oneof = 0; maedhros@6658: HouseSpec *hs; truelight@193: belugas@6918: /* Above snow? */ truelight@0: slope = GetTileSlope(tile, &z); truelight@0: belugas@6918: /* Get the town zone type */ truelight@0: { truelight@0: uint rad = GetTownRadiusGroup(t, tile); truelight@0: truelight@0: int land = _opt.landscape; belugas@6683: if (land == LT_ARCTIC && z >= _opt.snow_line) land = -1; truelight@0: truelight@0: bitmask = (1 << rad) + (1 << (land + 12)); truelight@0: } truelight@0: belugas@6918: /* bits 0-4 are used belugas@6918: * bits 11-15 are used belugas@6918: * bits 5-10 are not used. */ truelight@0: { maedhros@6658: HouseID houses[HOUSE_MAX]; truelight@0: int num = 0; maedhros@6658: uint cumulative_probs[HOUSE_MAX]; maedhros@6658: uint probability_max = 0; truelight@0: belugas@6918: /* Generate a list of all possible houses that can be built. */ maedhros@6658: for (i = 0; i < HOUSE_MAX; i++) { maedhros@6658: hs = GetHouseSpecs(i); maedhros@6658: if ((~hs->building_availability & bitmask) == 0 && hs->enabled) { maedhros@6658: if (_have_newhouses) { maedhros@6658: probability_max += hs->probability; maedhros@6658: cumulative_probs[num] = probability_max; maedhros@6658: } maedhros@6658: houses[num++] = (HouseID)i; maedhros@6658: } truelight@0: } truelight@0: tron@2952: for (;;) { maedhros@6658: if (_have_newhouses) { maedhros@6658: uint r = RandomRange(probability_max); maedhros@6658: for (i = 0; i < num; i++) if (cumulative_probs[i] >= r) break; truelight@0: maedhros@6658: house = houses[i]; maedhros@6658: } else { maedhros@6658: house = houses[RandomRange(num)]; maedhros@6658: } maedhros@6658: maedhros@6658: hs = GetHouseSpecs(house); maedhros@6658: maedhros@6658: if (_have_newhouses) { maedhros@6658: if (hs->override != 0) hs = GetHouseSpecs(hs->override); maedhros@6658: maedhros@6658: if ((hs->extra_flags & BUILDING_IS_HISTORICAL) && !_generating_world) continue; maedhros@6658: maedhros@6658: if (HASBIT(hs->callback_mask, CBM_HOUSE_ALLOW_CONSTRUCTION)) { maedhros@6658: uint16 callback_res = GetHouseCallback(CBID_HOUSE_ALLOW_CONSTRUCTION, 0, house, t, tile); maedhros@6658: if (callback_res != CALLBACK_FAILED && callback_res == 0) continue; maedhros@6658: } maedhros@6658: } maedhros@6658: maedhros@6658: if (_cur_year < hs->min_date || _cur_year > hs->max_date) continue; truelight@0: belugas@6918: /* Special houses that there can be only one of. */ maedhros@6658: if (hs->building_flags & BUILDING_IS_CHURCH) { maedhros@6658: SETBIT(oneof, TOWN_HAS_CHURCH); maedhros@6658: } else if (hs->building_flags & BUILDING_IS_STADIUM) { maedhros@6658: SETBIT(oneof, TOWN_HAS_STADIUM); maedhros@6658: } else { maedhros@6658: oneof = 0; tron@483: } truelight@0: belugas@3432: if (HASBITS(t->flags12 , oneof)) continue; truelight@0: belugas@6918: /* Make sure there is no slope? */ maedhros@6658: if (hs->building_flags & TILE_NOT_SLOPED && slope != SLOPE_FLAT) continue; truelight@193: maedhros@6658: if (hs->building_flags & TILE_SIZE_2x2) { tron@2958: if (CheckFree2x2Area(tile) || tron@2958: CheckFree2x2Area(tile += TileDiffXY(-1, 0)) || tron@2958: CheckFree2x2Area(tile += TileDiffXY( 0, -1)) || tron@2958: CheckFree2x2Area(tile += TileDiffXY( 1, 0))) { tron@1981: break; tron@2951: } tron@2951: tile += TileDiffXY(0, 1); maedhros@6658: } else if (hs->building_flags & TILE_SIZE_2x1) { tron@2958: if (CheckBuildHouseMode(tile + TileDiffXY(1, 0), slope, 0)) break; truelight@193: tron@2958: if (CheckBuildHouseMode(tile + TileDiffXY(-1, 0), slope, 1)) { tron@1981: tile += TileDiffXY(-1, 0); truelight@0: break; truelight@0: } maedhros@6658: } else if (hs->building_flags & TILE_SIZE_1x2) { tron@2958: if (CheckBuildHouseMode(tile + TileDiffXY(0, 1), slope, 2)) break; truelight@0: tron@2958: if (CheckBuildHouseMode(tile + TileDiffXY(0, -1), slope, 3)) { tron@1981: tile += TileDiffXY(0, -1); truelight@0: break; truelight@0: } tron@2951: } else { truelight@0: break; tron@2951: } truelight@0: } truelight@0: } truelight@0: truelight@0: t->num_houses++; maedhros@6658: IncreaseBuildingCount(t, house); truelight@0: belugas@6918: /* Special houses that there can be only one of. */ truelight@0: t->flags12 |= oneof; truelight@193: truelight@0: { maedhros@6658: byte construction_counter = 0, construction_stage = 0; truelight@0: truelight@0: if (_generating_world) { truelight@0: uint32 r = Random(); truelight@193: belugas@3432: construction_stage = TOWN_HOUSE_COMPLETED; celestar@3382: if (CHANCE16(1, 7)) construction_stage = GB(r, 0, 2); truelight@0: belugas@3432: if (construction_stage == TOWN_HOUSE_COMPLETED) { maedhros@6658: ChangePopulation(t, hs->population); celestar@3382: } else { celestar@3382: construction_counter = GB(r, 2, 2); celestar@3382: } truelight@0: } maedhros@6658: MakeTownHouse(tile, t->index, construction_counter, construction_stage, house, VehicleRandomBits()); truelight@0: } truelight@0: } truelight@0: tron@1977: static bool BuildTownHouse(Town *t, TileIndex tile) truelight@0: { truelight@0: int32 r; truelight@193: tron@3636: if (IsSteepSlope(GetTileSlope(tile, NULL))) return false; celestar@5573: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return false; truelight@0: tron@3491: r = DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR); peter1138@2737: if (CmdFailed(r)) return false; truelight@0: truelight@0: DoBuildTownHouse(t, tile); truelight@0: return true; truelight@0: } truelight@0: truelight@0: tron@1977: static void DoClearTownHouseHelper(TileIndex tile) truelight@0: { tron@1035: assert(IsTileType(tile, MP_HOUSE)); truelight@0: DoClearSquare(tile); truelight@0: DeleteAnimatedTile(tile); truelight@0: } truelight@0: maedhros@6658: void ClearTownHouse(Town *t, TileIndex tile) tron@1977: { maedhros@6658: HouseID house = GetHouseType(tile); truelight@0: uint eflags; maedhros@6658: HouseSpec *hs; truelight@0: tron@1035: assert(IsTileType(tile, MP_HOUSE)); truelight@0: belugas@6918: /* 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: maedhros@6658: hs = GetHouseSpecs(house); maedhros@6658: belugas@6918: /* 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: belugas@6918: /* Clear flags for houses that only may exist once/town. */ maedhros@6658: if (hs->building_flags & BUILDING_IS_CHURCH) { maedhros@6658: CLRBIT(t->flags12, TOWN_HAS_CHURCH); maedhros@6658: } else if (hs->building_flags & BUILDING_IS_STADIUM) { maedhros@6658: CLRBIT(t->flags12, TOWN_HAS_STADIUM); tron@483: } truelight@193: belugas@6918: /* Do the actual clearing of tiles */ maedhros@6658: 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: Darkvater@1793: /** Rename a town (server-only). tron@3491: * @param tile unused belugas@6918: * @param flags type of operation Darkvater@1793: * @param p1 town ID to rename Darkvater@1793: * @param p2 unused Darkvater@1793: */ tron@3491: int32 CmdRenameTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: StringID str; Darkvater@1793: Town *t; Darkvater@1793: truelight@4352: if (!IsValidTownID(p1) || _cmd_text[0] == '\0') return CMD_ERROR; Darkvater@1793: Darkvater@1793: t = GetTown(p1); truelight@193: tron@1820: str = AllocateNameUnique(_cmd_text, 4); Darkvater@1793: if (str == 0) return CMD_ERROR; truelight@0: truelight@0: if (flags & DC_EXEC) { Darkvater@1793: DeleteName(t->townnametype); truelight@0: t->townnametype = str; truelight@0: truelight@0: UpdateTownVirtCoord(t); truelight@0: _town_sort_dirty = true; truelight@0: UpdateAllStationVirtCoord(); truelight@0: MarkWholeScreenDirty(); truelight@0: } else { truelight@0: DeleteName(str); truelight@0: } truelight@0: return 0; truelight@0: } truelight@0: belugas@6918: /** Called from GUI */ truelight@0: void ExpandTown(Town *t) truelight@0: { truelight@0: int amount, n; truelight@0: truelight@0: _generating_world = true; truelight@193: truelight@835: /* The more houses, the faster we grow */ truelight@835: amount = RandomRange(t->num_houses / 10) + 3; truelight@0: t->num_houses += amount; truelight@0: UpdateTownRadius(t); truelight@0: truelight@835: n = amount * 10; truelight@0: do GrowTown(t); while (--n); truelight@0: truelight@0: t->num_houses -= amount; truelight@0: UpdateTownRadius(t); truelight@0: truelight@0: UpdateTownMaxPass(t); truelight@0: _generating_world = false; truelight@0: } truelight@0: rubidium@5838: extern const byte _town_action_costs[8] = { truelight@0: 2, 4, 9, 35, 48, 53, 117, 175 truelight@0: }; truelight@0: tron@3877: static void TownActionAdvertiseSmall(Town* t) truelight@0: { tron@3877: ModifyStationRatingAround(t->xy, _current_player, 0x40, 10); truelight@0: } truelight@0: tron@3877: static void TownActionAdvertiseMedium(Town* t) tron@3877: { tron@3877: ModifyStationRatingAround(t->xy, _current_player, 0x70, 15); tron@3877: } tron@3877: tron@3877: static void TownActionAdvertiseLarge(Town* t) tron@3877: { tron@3877: ModifyStationRatingAround(t->xy, _current_player, 0xA0, 20); tron@3877: } tron@3877: tron@3877: static void TownActionRoadRebuild(Town* t) truelight@0: { tron@2548: const Player* p; truelight@0: truelight@0: t->road_build_months = 6; truelight@193: tron@534: SetDParam(0, t->index); truelight@0: celestar@1962: p = GetPlayer(_current_player); tron@534: SetDParam(1, p->name_1); tron@534: SetDParam(2, p->name_2); truelight@0: truelight@193: AddNewsItem(STR_2055_TRAFFIC_CHAOS_IN_ROAD_REBUILDING, truelight@0: NEWS_FLAGS(NM_NORMAL, NF_TILE, NT_GENERAL, 0), t->xy, 0); truelight@0: } truelight@0: truelight@6583: static bool DoBuildStatueOfCompany(TileIndex tile, TownID town_id) truelight@0: { tron@2498: PlayerID old; truelight@0: int32 r; truelight@0: tron@3636: if (GetTileSlope(tile, NULL) != SLOPE_FLAT) return false; truelight@0: tron@2986: if (!IsTileType(tile, MP_HOUSE) && tron@2986: !IsTileType(tile, MP_CLEAR) && tron@2986: !IsTileType(tile, MP_TREES)) { truelight@0: return false; tron@2986: } truelight@0: truelight@0: old = _current_player; truelight@0: _current_player = OWNER_NONE; tron@3491: r = DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@0: _current_player = old; truelight@0: tron@2646: if (CmdFailed(r)) return false; truelight@0: 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 belugas@6980: * @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: */ tron@3877: static void TownActionBuildStatue(Town* t) truelight@0: { tron@1977: TileIndex tile = t->xy; truelight@0: truelight@6583: if (CircularTileSearch(tile, 9, SearchTileForStatue, t->index)) belugas@6918: SETBIT(t->statues, _current_player); // Once found and built, "inform" the Town truelight@0: } truelight@0: tron@3877: static void TownActionFundBuildings(Town* t) truelight@0: { belugas@6918: /* Build next tick */ truelight@0: t->grow_counter = 1; belugas@6918: /* If we were not already growing */ belugas@3432: SETBIT(t->flags12, TOWN_IS_FUNDED); belugas@6918: /* And grow for 3 months */ truelight@0: t->fund_buildings_months = 3; truelight@0: } truelight@0: tron@3877: static void TownActionBuyRights(Town* t) truelight@0: { dominik@121: t->exclusive_counter = 12; dominik@121: t->exclusivity = _current_player; truelight@0: truelight@0: ModifyStationRatingAround(t->xy, _current_player, 130, 17); truelight@0: } truelight@0: tron@3877: static void TownActionBribe(Town* t) truelight@0: { truelight@0: if (!RandomRange(15)) { truelight@0: Station *st; truelight@0: belugas@6918: /* set as unwanted for 6 months */ truelight@0: t->unwanted[_current_player] = 6; truelight@0: belugas@6918: /* set all close by station ratings to 0 */ 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: belugas@6918: /* only show errormessage to the executing player. All errors are handled command.c belugas@6918: * but this is special, because it can only 'fail' on a DC_EXEC */ Darkvater@2425: if (IsLocalPlayer()) ShowErrorMessage(STR_BRIBE_FAILED_2, STR_BRIBE_FAILED, 0, 0); truelight@0: rubidium@4434: /* decrease by a lot! rubidium@4434: * ChangeTownRating is only for stuff in demolishing. Bribe failure should rubidium@4434: * be independent of any cheat settings truelight@0: */ tron@2549: if (t->ratings[_current_player] > RATING_BRIBE_DOWN_TO) { celestar@1005: t->ratings[_current_player] = RATING_BRIBE_DOWN_TO; tron@2549: } truelight@0: } else { celestar@1005: ChangeTownRating(t, RATING_BRIBE_UP_STEP, RATING_BRIBE_MAXIMUM); truelight@0: } truelight@0: } truelight@0: tron@3877: typedef void TownActionProc(Town* t); truelight@0: static TownActionProc * const _town_action_proc[] = { tron@3877: TownActionAdvertiseSmall, tron@3877: TownActionAdvertiseMedium, tron@3877: TownActionAdvertiseLarge, truelight@0: TownActionRoadRebuild, truelight@0: TownActionBuildStatue, truelight@0: TownActionFundBuildings, truelight@0: TownActionBuyRights, truelight@0: TownActionBribe truelight@0: }; truelight@0: Darkvater@1793: extern uint GetMaskOfTownActions(int *nump, PlayerID pid, const Town *t); Darkvater@1793: Darkvater@1793: /** Do a town action. Darkvater@1793: * This performs an action such as advertising, building a statue, funding buildings, Darkvater@1793: * but also bribing the town-council tron@3491: * @param tile unused belugas@6918: * @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: */ tron@3491: int32 CmdDoTownAction(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@0: int32 cost; Darkvater@1793: Town *t; Darkvater@1793: truelight@4352: if (!IsValidTownID(p1) || p2 > lengthof(_town_action_proc)) return CMD_ERROR; Darkvater@1793: Darkvater@1793: t = GetTown(p1); Darkvater@1793: Darkvater@1793: if (!HASBIT(GetMaskOfTownActions(NULL, _current_player, t), p2)) return CMD_ERROR; truelight@0: truelight@0: SET_EXPENSES_TYPE(EXPENSES_OTHER); truelight@0: truelight@0: cost = (_price.build_industry >> 8) * _town_action_costs[p2]; truelight@0: truelight@0: if (flags & DC_EXEC) { tron@3877: _town_action_proc[p2](t); truelight@0: InvalidateWindow(WC_TOWN_AUTHORITY, p1); truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: static void UpdateTownGrowRate(Town *t) truelight@0: { truelight@0: int n; truelight@0: Station *st; maedhros@6950: uint16 m; truelight@0: Player *p; truelight@0: belugas@6918: /* Reset player ratings if they're low */ truelight@0: FOR_ALL_PLAYERS(p) { truelight@0: if (p->is_active && t->ratings[p->index] <= 200) { truelight@0: t->ratings[p->index] += 5; truelight@0: } truelight@0: } truelight@0: truelight@0: n = 0; truelight@0: FOR_ALL_STATIONS(st) { tron@1245: if (DistanceSquare(st->xy, t->xy) <= t->radius[0]) { truelight@0: if (st->time_since_load <= 20 || st->time_since_unload <= 20) { truelight@0: n++; Darkvater@4850: if (IsValidPlayer(st->owner) && t->ratings[st->owner] <= 1000-12) truelight@0: t->ratings[st->owner] += 12; truelight@0: } else { Darkvater@4850: if (IsValidPlayer(st->owner) && t->ratings[st->owner] >= -1000+15) truelight@0: t->ratings[st->owner] -= 15; truelight@0: } truelight@0: } truelight@0: } truelight@0: belugas@3432: CLRBIT(t->flags12, TOWN_IS_FUNDED); maedhros@6951: if (_patches.town_growth_rate == 0 && t->fund_buildings_months == 0) return; maedhros@6950: maedhros@6950: /** Towns are processed every TOWN_GROWTH_FREQUENCY ticks, and this is the maedhros@6950: * number of times towns are processed before a new building is built. */ maedhros@6950: static const uint16 _grow_count_values[2][6] = { maedhros@6950: { 120, 120, 120, 100, 80, 60 }, ///< Fund new buildings has been activated maedhros@6950: { 320, 420, 300, 220, 160, 100 } ///< Normal values maedhros@6950: }; truelight@0: truelight@0: if (t->fund_buildings_months != 0) { maedhros@6950: m = _grow_count_values[0][min(n, 5)]; truelight@0: t->fund_buildings_months--; truelight@0: } else { maedhros@6950: m = _grow_count_values[1][min(n, 5)]; maedhros@6950: 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: maedhros@6951: /* Use the normal growth rate values if new buildings have been funded in maedhros@6951: * this town and the growth rate is set to none. */ maedhros@6951: uint growth_multiplier = _patches.town_growth_rate != 0 ? _patches.town_growth_rate - 1 : 1; maedhros@6951: maedhros@6951: m >>= growth_multiplier; maedhros@6982: if (t->larger_town) m /= 2; maedhros@6950: rubidium@4434: t->growth_rate = m / (t->num_houses / 50 + 1); truelight@0: if (m <= t->grow_counter) truelight@0: t->grow_counter = m; truelight@0: belugas@3432: SETBIT(t->flags12, TOWN_IS_FUNDED); truelight@0: } truelight@0: truelight@0: static void UpdateTownAmounts(Town *t) truelight@0: { belugas@6918: /* 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: belugas@6918: /* Using +1 here to prevent overflow and division by zero */ truelight@0: t->pct_mail_transported = t->new_act_mail * 256 / (t->new_max_mail + 1); truelight@0: t->max_mail = t->new_max_mail; t->new_max_mail = 0; truelight@0: t->act_mail = t->new_act_mail; t->new_act_mail = 0; truelight@0: truelight@0: InvalidateWindow(WC_TOWN_VIEW, t->index); truelight@0: } truelight@0: truelight@0: static void UpdateTownUnwanted(Town *t) truelight@0: { tron@4000: const Player* p; truelight@0: truelight@0: FOR_ALL_PLAYERS(p) { tron@4000: if (t->unwanted[p->index] > 0) t->unwanted[p->index]--; truelight@0: } truelight@0: } truelight@0: tron@1977: bool CheckIfAuthorityAllows(TileIndex tile) truelight@0: { truelight@0: Town *t; truelight@0: Darkvater@4850: if (!IsValidPlayer(_current_player)) return true; truelight@0: truelight@0: t = ClosestTownFromTile(tile, _patches.dist_local_authority); tron@4000: if (t == NULL) return true; truelight@0: tron@4000: if (t->ratings[_current_player] > -200) return true; truelight@0: truelight@0: _error_message = STR_2009_LOCAL_AUTHORITY_REFUSES; tron@534: SetDParam(0, t->index); truelight@0: truelight@0: return false; truelight@0: } truelight@0: truelight@0: tron@3983: Town* CalcClosestTownFromTile(TileIndex tile, uint threshold) truelight@0: { truelight@0: Town *t; truelight@0: uint dist, best = threshold; truelight@0: Town *best_town = NULL; truelight@193: truelight@0: FOR_ALL_TOWNS(t) { truelight@4346: dist = DistanceManhattan(tile, t->xy); truelight@4346: if (dist < best) { truelight@4346: best = dist; truelight@4346: best_town = t; truelight@0: } truelight@0: } truelight@0: truelight@0: return best_town; truelight@0: } truelight@0: tron@3983: tron@3983: Town *ClosestTownFromTile(TileIndex tile, uint threshold) tron@3983: { tron@3983: if (IsTileType(tile, MP_HOUSE) || ( tron@3983: IsTileType(tile, MP_STREET) && tron@3983: (IsLevelCrossing(tile) ? GetCrossingRoadOwner(tile) : GetTileOwner(tile)) == OWNER_TOWN tron@3983: )) { tron@3983: return GetTownByTile(tile); tron@3983: } else { tron@3983: return CalcClosestTownFromTile(tile, threshold); tron@3983: } tron@3983: } tron@3983: tron@3983: truelight@0: void ChangeTownRating(Town *t, int add, int max) truelight@0: { truelight@0: int rating; truelight@0: belugas@6918: /* if magic_bulldozer cheat is active, town doesn't penaltize for removing stuff */ tron@2639: if (t == NULL || Darkvater@4850: !IsValidPlayer(_current_player) || tron@2639: (_cheats.magic_bulldozer.value && add < 0)) { truelight@0: return; tron@2639: } truelight@0: truelight@0: SETBIT(t->have_ratings, _current_player); truelight@193: truelight@0: rating = t->ratings[_current_player]; truelight@193: truelight@0: if (add < 0) { truelight@0: if (rating > max) { truelight@0: rating += add; truelight@0: if (rating < max) rating = max; truelight@0: } truelight@0: } else { truelight@0: if (rating < max) { truelight@0: rating += add; truelight@0: if (rating > max) rating = max; truelight@0: } truelight@0: } truelight@0: t->ratings[_current_player] = rating; truelight@0: } truelight@0: rubidium@4434: /* penalty for removing town-owned stuff */ truelight@0: static const int _default_rating_settings [3][3] = { belugas@6918: /* ROAD_REMOVE, TUNNELBRIDGE_REMOVE, INDUSTRY_REMOVE */ belugas@6918: { 0, 128, 384}, ///< Permissive belugas@6918: { 48, 192, 480}, ///< Neutral belugas@6918: { 96, 384, 768}, ///< Hostile truelight@0: }; truelight@0: tron@2958: bool CheckforTownRating(uint32 flags, Town *t, byte type) truelight@0: { truelight@0: int modemod; truelight@0: belugas@6918: /* if magic_bulldozer cheat is active, town doesn't restrict your destructive actions */ Darkvater@4850: if (t == NULL || !IsValidPlayer(_current_player) || _cheats.magic_bulldozer.value) truelight@0: return true; truelight@0: rubidium@4434: /* check if you're allowed to remove the street/bridge/tunnel/industry rubidium@4434: * owned by a town no removal if rating is lower than ... depends now on rubidium@4434: * difficulty setting. Minimum town rating selected by difficulty level truelight@0: */ Darkvater@1500: modemod = _default_rating_settings[_opt.diff.town_council_tolerance][type]; truelight@0: truelight@0: if (t->ratings[_current_player] < 16 + modemod && !(flags & DC_NO_TOWN_RATING)) { tron@534: SetDParam(0, t->index); truelight@0: _error_message = STR_2009_LOCAL_AUTHORITY_REFUSES; truelight@0: return false; truelight@0: } truelight@0: truelight@0: return true; truelight@0: } truelight@0: 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@0: Subsidy *s; truelight@0: truelight@1260: /* Clean the town pool and create 1 block in it */ tron@4983: CleanPool(&_Town_pool); tron@4983: AddBlockToPool(&_Town_pool); truelight@0: truelight@0: memset(_subsidies, 0, sizeof(_subsidies)); truelight@0: for (s=_subsidies; s != endof(_subsidies); s++) tron@2469: s->cargo_type = CT_INVALID; truelight@0: truelight@0: _cur_town_ctr = 0; pasky@1529: _cur_town_iter = 0; truelight@1260: _total_towns = 0; truelight@0: _town_sort_dirty = true; truelight@0: } truelight@0: 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 */ rubidium@4344: NULL, /* get_produced_cargo_proc */ rubidium@4344: NULL, /* vehicle_enter_tile_proc */ rubidium@4344: GetSlopeTileh_Town, /* get_slope_tileh_proc */ truelight@0: }; truelight@0: truelight@0: belugas@6918: /** Save and load of towns. */ Darkvater@1881: static const SaveLoad _town_desc[] = { rubidium@4344: SLE_CONDVAR(Town, xy, SLE_FILE_U16 | SLE_VAR_U32, 0, 5), rubidium@4344: SLE_CONDVAR(Town, xy, SLE_UINT32, 6, SL_MAX_VERSION), truelight@193: rubidium@4344: SLE_CONDVAR(Town, population, SLE_FILE_U16 | SLE_VAR_U32, 0, 2), rubidium@4344: SLE_CONDVAR(Town, population, SLE_UINT32, 3, SL_MAX_VERSION), truelight@0: truelight@0: rubidium@4344: SLE_VAR(Town, num_houses, SLE_UINT16), rubidium@4344: SLE_VAR(Town, townnametype, SLE_UINT16), rubidium@4344: SLE_VAR(Town, townnameparts, SLE_UINT32), truelight@0: rubidium@4344: SLE_VAR(Town, flags12, SLE_UINT8), rubidium@4344: SLE_VAR(Town, statues, SLE_UINT8), truelight@0: belugas@6918: /* sort_index_obsolete was stored here in savegame format 0 - 1 */ Darkvater@3222: SLE_CONDNULL(1, 0, 1), truelight@0: rubidium@4344: SLE_VAR(Town, have_ratings, SLE_UINT8), rubidium@4344: SLE_ARR(Town, ratings, SLE_INT16, 8), belugas@6918: /* failed bribe attempts are stored since savegame format 4 */ rubidium@4344: SLE_CONDARR(Town, unwanted, SLE_INT8, 8, 4,SL_MAX_VERSION), celestar@1377: rubidium@4344: SLE_CONDVAR(Town, max_pass, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), rubidium@4344: SLE_CONDVAR(Town, max_mail, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), rubidium@4344: SLE_CONDVAR(Town, new_max_pass, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), rubidium@4344: SLE_CONDVAR(Town, new_max_mail, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), rubidium@4344: SLE_CONDVAR(Town, act_pass, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), rubidium@4344: SLE_CONDVAR(Town, act_mail, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), rubidium@4344: SLE_CONDVAR(Town, new_act_pass, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), rubidium@4344: SLE_CONDVAR(Town, new_act_mail, SLE_FILE_U16 | SLE_VAR_U32, 0, 8), truelight@0: rubidium@4344: SLE_CONDVAR(Town, max_pass, SLE_UINT32, 9, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Town, max_mail, SLE_UINT32, 9, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Town, new_max_pass, SLE_UINT32, 9, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Town, new_max_mail, SLE_UINT32, 9, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Town, act_pass, SLE_UINT32, 9, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Town, act_mail, SLE_UINT32, 9, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Town, new_act_pass, SLE_UINT32, 9, SL_MAX_VERSION), rubidium@4344: SLE_CONDVAR(Town, new_act_mail, SLE_UINT32, 9, SL_MAX_VERSION), truelight@0: rubidium@4344: SLE_VAR(Town, pct_pass_transported, SLE_UINT8), rubidium@4344: SLE_VAR(Town, pct_mail_transported, SLE_UINT8), truelight@0: rubidium@4344: SLE_VAR(Town, act_food, SLE_UINT16), rubidium@4344: SLE_VAR(Town, act_water, SLE_UINT16), rubidium@4344: SLE_VAR(Town, new_act_food, SLE_UINT16), rubidium@4344: SLE_VAR(Town, new_act_water, SLE_UINT16), rubidium@4344: maedhros@6950: SLE_CONDVAR(Town, time_until_rebuild, SLE_UINT8, 0, 53), maedhros@6950: SLE_CONDVAR(Town, grow_counter, SLE_UINT8, 0, 53), maedhros@6950: SLE_CONDVAR(Town, growth_rate, SLE_UINT8, 0, 53), maedhros@6950: maedhros@6950: SLE_CONDVAR(Town, time_until_rebuild, SLE_UINT16, 54, SL_MAX_VERSION), maedhros@6950: SLE_CONDVAR(Town, grow_counter, SLE_UINT16, 54, SL_MAX_VERSION), maedhros@6950: SLE_CONDVAR(Town, growth_rate, SLE_INT16, 54, SL_MAX_VERSION), maedhros@6950: rubidium@4344: SLE_VAR(Town, fund_buildings_months, SLE_UINT8), rubidium@4344: SLE_VAR(Town, road_build_months, SLE_UINT8), rubidium@4344: rubidium@4344: SLE_VAR(Town, exclusivity, SLE_UINT8), rubidium@4344: SLE_VAR(Town, exclusive_counter, SLE_UINT8), maedhros@6982: maedhros@6982: SLE_CONDVAR(Town, larger_town, SLE_BOOL, 56, SL_MAX_VERSION), maedhros@6982: belugas@6918: /* 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[] = { maedhros@6658: SLE_VAR(HouseIDMapping, grfid, SLE_UINT32), maedhros@6658: SLE_VAR(HouseIDMapping, house_id, SLE_UINT8), maedhros@6658: SLE_VAR(HouseIDMapping, substitute_id, SLE_UINT8), maedhros@6658: SLE_END() maedhros@6658: }; maedhros@6658: maedhros@6658: static void Save_HOUSEIDS() maedhros@6658: { maedhros@6658: uint i; maedhros@6658: maedhros@6658: for (i = 0; i != lengthof(_house_id_mapping); i++) { maedhros@6658: SlSetArrayIndex(i); maedhros@6658: SlObject(&_house_id_mapping[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: maedhros@6658: ResetHouseIDMapping(); maedhros@6658: maedhros@6658: while ((index = SlIterateArray()) != -1) { maedhros@6658: if ((uint)index >= lengthof(_house_id_mapping)) break; maedhros@6658: SlObject(&_house_id_mapping[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) { truelight@1260: Town *t; truelight@1260: tron@4983: if (!AddBlockIfNeeded(&_Town_pool, index)) truelight@1260: error("Towns: failed loading savegame: too many towns"); truelight@1260: truelight@1260: t = GetTown(index); truelight@0: SlObject(t, _town_desc); truelight@1260: rubidium@5298: _total_towns++; truelight@0: } truelight@1260: truelight@1260: /* This is to ensure all pointers are within the limits of truelight@1260: * the size of the TownPool */ matthijs@5247: if (_cur_town_ctr > GetMaxTownIndex()) truelight@1260: _cur_town_ctr = 0; truelight@0: } truelight@0: rubidium@6573: void AfterLoadTown() truelight@0: { truelight@0: Town *t; truelight@0: FOR_ALL_TOWNS(t) { truelight@4346: UpdateTownRadius(t); truelight@4346: UpdateTownVirtCoord(t); truelight@0: } truelight@0: _town_sort_dirty = true; truelight@0: } truelight@0: 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)); maedhros@6658: }