tron@2186: /* $Id$ */ tron@2186: rubidium@10429: /** @file town_cmd.cpp Handling of town tiles. */ belugas@6527: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" rubidium@8615: #include "tile_cmd.h" maedhros@6658: #include "debug.h" tron@3144: #include "road_map.h" rubidium@8598: #include "road_internal.h" /* Cleaning up road bits */ maedhros@6669: #include "landscape.h" tron@3319: #include "town_map.h" tron@3154: #include "tunnel_map.h" rubidium@8720: #include "viewport_func.h" truelight@0: #include "town.h" rubidium@8612: #include "command_func.h" truelight@0: #include "industry.h" rubidium@9281: #include "station_base.h" rubidium@8750: #include "player_base.h" rubidium@9259: #include "news_func.h" truelight@0: #include "saveload.h" truelight@0: #include "gui.h" tron@3310: #include "unmovable_map.h" celestar@3433: #include "water_map.h" tron@2159: #include "variables.h" celestar@3359: #include "bridge.h" celestar@5573: #include "bridge_map.h" truelight@4300: #include "genworld.h" maedhros@6658: #include "newgrf.h" maedhros@6658: #include "newgrf_callbacks.h" maedhros@6658: #include "newgrf_house.h" belugas@7125: #include "newgrf_commons.h" glx@7452: #include "newgrf_townname.h" rubidium@8078: #include "autoslope.h" glx@8205: #include "waypoint.h" belugas@8345: #include "transparency.h" smatz@8579: #include "tunnelbridge_map.h" rubidium@8610: #include "strings_func.h" rubidium@8627: #include "window_func.h" rubidium@8710: #include "string_func.h" rubidium@9283: #include "newgrf_cargo.h" smatz@9343: #include "oldpool_func.h" rubidium@10222: #include "sprite.h" rubidium@10222: #include "economy_func.h" rubidium@10222: #include "station_func.h" rubidium@10225: #include "cheat_func.h" rubidium@10269: #include "functions.h" rubidium@10269: #include "animated_tile_func.h" rubidium@10269: #include "date_func.h" smatz@11061: #include smatz@8579: rubidium@8760: #include "table/strings.h" rubidium@8760: #include "table/sprites.h" rubidium@8760: #include "table/town_land.h" truelight@1260: rubidium@8764: uint _total_towns; rubidium@8764: HouseSpec _house_specs[HOUSE_MAX]; rubidium@8764: rubidium@8764: Town *_cleared_town; rubidium@8764: int _cleared_town_rating; rubidium@8764: truelight@1260: /* Initialize the town-pool */ rubidium@7882: DEFINE_OLD_POOL_GENERIC(Town, Town) rubidium@7882: rubidium@7882: Town::Town(TileIndex tile) rubidium@7882: { glx@7906: if (tile != 0) _total_towns++; rubidium@7882: this->xy = tile; rubidium@7882: } rubidium@7882: rubidium@7882: Town::~Town() truelight@4396: { peter1138@8754: free(this->name); rubidium@7909: rubidium@7909: if (CleaningPool()) return; rubidium@7909: truelight@4396: Industry *i; truelight@4396: truelight@4396: /* Delete town authority window truelight@4396: * and remove from list of sorted towns */ rubidium@7882: DeleteWindowById(WC_TOWN_VIEW, this->index); peter1138@10747: InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 0); rubidium@5298: _total_towns--; truelight@4396: truelight@4396: /* Delete all industries belonging to the town */ rubidium@7886: FOR_ALL_INDUSTRIES(i) if (i->town == this) delete i; truelight@4396: truelight@4396: /* Go through all tiles and delete those belonging to the town */ rubidium@7882: for (TileIndex tile = 0; tile < MapSize(); ++tile) { truelight@4396: switch (GetTileType(tile)) { truelight@4396: case MP_HOUSE: rubidium@7882: if (GetTownByTile(tile) == this) DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@4396: break; truelight@4396: rubidium@7866: case MP_ROAD: truelight@4396: case MP_TUNNELBRIDGE: truelight@4396: if (IsTileOwner(tile, OWNER_TOWN) && rubidium@7882: ClosestTownFromTile(tile, (uint)-1) == this) truelight@4396: DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@4396: break; truelight@4396: truelight@4396: default: truelight@4396: break; truelight@4396: } truelight@4396: } truelight@4396: rubidium@7882: DeleteSubsidyWithTown(this->index); truelight@4396: truelight@4396: MarkWholeScreenDirty(); rubidium@7882: rubidium@7882: this->xy = 0; rubidium@7882: } rubidium@7882: skidd13@9292: /** skidd13@9292: * Generate a random town road layout. skidd13@9292: * skidd13@9292: * The layout is based on the TileHash. skidd13@9292: */ skidd13@9292: void Town::InitializeLayout() skidd13@9292: { skidd13@9292: this->layout = (TownLayout)(TileHash(TileX(this->xy), TileY(this->xy)) % NUM_TLS); skidd13@9292: skidd13@9292: /* Set invalid layouts to valid ones */ skidd13@9292: switch (this->layout) { skidd13@9292: default: break; skidd13@9292: case TL_RANDOM: this->layout = TL_ORIGINAL; break; skidd13@9292: case TL_NO_ROADS: this->layout = TL_BETTER_ROADS; break; skidd13@9292: } skidd13@9292: } skidd13@9292: belugas@10832: Money HouseSpec::GetRemovalCost() const belugas@10832: { belugas@10832: return (_price.remove_house * this->removal_cost) >> 8; belugas@10832: } belugas@10832: truelight@0: // Local truelight@0: static int _grow_town_result; truelight@0: rubidium@8180: /* Describe the possible states */ rubidium@8180: enum TownGrowthResult { rubidium@8180: GROWTH_SUCCEED = -1, rubidium@8180: GROWTH_SEARCH_STOPPED = 0 rubidium@8180: // GROWTH_SEARCH_RUNNING >= 1 rubidium@8180: }; rubidium@8180: tron@1977: static bool BuildTownHouse(Town *t, TileIndex tile); truelight@0: Darkvater@2436: static void TownDrawHouseLift(const TileInfo *ti) truelight@0: { peter1138@5919: AddChildSpriteScreen(SPR_LIFT, PAL_NONE, 14, 60 - GetLiftPosition(ti->tile)); truelight@0: } truelight@0: Darkvater@2436: typedef void TownDrawTileProc(const TileInfo *ti); smatz@9310: static TownDrawTileProc *const _town_draw_tile_procs[1] = { ludde@2065: TownDrawHouseLift truelight@0: }; truelight@0: skidd13@9287: /** belugas@8062: * Return a random direction belugas@8062: * belugas@8062: * @return a random direction belugas@8062: */ belugas@8062: static inline DiagDirection RandomDiagDir() belugas@8062: { belugas@8062: return (DiagDirection)(3 & Random()); belugas@8062: } belugas@8062: belugas@8062: /** 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: { 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 */ smatz@9310: const DrawBuildingsTileStruct *dcts = &_town_draw_tile_data[house_id << 4 | TileHash2Bit(ti->x, ti->y) << 2 | GetHouseBuildingStage(ti->tile)]; truelight@0: rubidium@7831: if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED); peter1138@5919: smatz@9310: DrawGroundSprite(dcts->ground.sprite, dcts->ground.pal); truelight@0: smatz@9302: /* If houses are invisible, do not draw the upper part */ smatz@9302: if (IsInvisibilitySet(TO_HOUSES)) return; smatz@9302: truelight@0: /* Add a house on top of the ground? */ smatz@9310: SpriteID image = dcts->building.sprite; tron@2639: if (image != 0) { rubidium@7829: AddSortableSpriteToDraw(image, dcts->building.pal, tron@482: ti->x + dcts->subtile_x, tron@482: ti->y + dcts->subtile_y, rubidium@8073: dcts->width, rubidium@8073: dcts->height, truelight@0: dcts->dz, rubidium@7829: ti->z, belugas@8345: IsTransparencySet(TO_HOUSES) tron@4053: ); truelight@0: belugas@8345: if (IsTransparencySet(TO_HOUSES)) return; truelight@0: } truelight@0: truelight@0: { belugas@3654: int proc = dcts->draw_proc - 1; tron@2639: tron@2639: if (proc >= 0) _town_draw_tile_procs[proc](ti); truelight@0: } truelight@0: } truelight@0: tron@4231: static uint GetSlopeZ_Town(TileIndex tile, uint x, uint y) truelight@0: { tron@4231: return GetTileMaxZ(tile); truelight@0: } truelight@0: rubidium@7831: static Foundation GetFoundation_Town(TileIndex tile, Slope tileh) dominik@39: { rubidium@7831: return FlatteningFoundation(tileh); 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: { 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)) { smatz@9310: uint i; smatz@9310: smatz@9310: /* Building has 6 floors, number 0 .. 6, where 1 is illegal. smatz@9310: * This is due to the fact that the first floor is, in the graphics, Darkvater@2891: * the height of 2 'normal' floors. smatz@9310: * Furthermore, there are 6 lift positions from floor N (incl) to floor N + 1 (excl) */ truelight@0: do { smatz@9310: i = RandomRange(7); smatz@9310: } while (i == 1 || i * 6 == GetLiftPosition(tile)); truelight@0: celestar@3426: SetLiftDestination(tile, i); truelight@0: } truelight@0: smatz@9310: int pos = GetLiftPosition(tile); smatz@9310: int dest = GetLiftDestination(tile) * 6; celestar@3426: pos += (pos < dest) ? 1 : -1; celestar@3426: SetLiftPosition(tile, pos); truelight@0: rubidium@10269: if (pos == dest) { rubidium@10269: HaltLift(tile); rubidium@10269: DeleteAnimatedTile(tile); rubidium@10269: } truelight@193: truelight@0: MarkTileDirtyByTile(tile); truelight@0: } 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: { smatz@9310: 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: /** rubidium@8041: * Marks the town sign as needing a repaint. rubidium@8041: * rubidium@8041: * This function marks the area of the sign of a town as dirty for repaint. rubidium@8041: * rubidium@8041: * @param t Town requesting town sign for repaint rubidium@8041: * @ingroup dirty 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@835: MarkTownSignDirty(t); smatz@9310: Point pt = RemapCoords2(TileX(t->xy) * TILE_SIZE, TileY(t->xy) * TILE_SIZE); ludde@2070: SetDParam(0, t->index); ludde@2070: SetDParam(1, t->population); ludde@2070: UpdateViewportSignPos(&t->sign, pt.x, pt.y - 24, rubidium@10775: _settings_client.gui.population_in_label ? STR_TOWN_LABEL_POP : STR_TOWN_LABEL); truelight@835: MarkTownSignDirty(t); truelight@835: } truelight@0: rubidium@8465: /** Update the virtual coords needed to draw the town sign for all towns. */ rubidium@8465: void UpdateAllTownVirtCoords() rubidium@8465: { rubidium@8465: Town *t; rubidium@8465: FOR_ALL_TOWNS(t) { rubidium@8465: UpdateTownVirtCoord(t); rubidium@8465: } rubidium@8465: } rubidium@8465: 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: peter1138@10747: InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 1); 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: { smatz@9310: uint32 pop = 0; smatz@9310: const Town *t; smatz@9310: 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: rubidium@10285: const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); rubidium@10285: belugas@7030: /* Check and/or */ rubidium@10285: if (HasBit(hs->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) { rubidium@7711: uint16 callback_res = GetHouseCallback(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile); rubidium@10285: if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(hs->grffile, 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. */ rubidium@10285: ChangePopulation(GetTownByTile(tile), hs->population); rubidium@10269: SetHouseConstructionYear(tile, _cur_year); 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: { maedhros@6658: HouseID house_id = GetHouseType(tile); truelight@0: maedhros@6658: /* NewHouseTileLoop returns false if Callback 21 succeeded, i.e. the house maedhros@6658: * doesn't exist any more, so don't continue here. */ maedhros@6658: if (house_id >= NEW_HOUSE_OFFSET && !NewHouseTileLoop(tile)) return; maedhros@6658: maedhros@6658: if (!IsHouseCompleted(tile)) { 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: smatz@9310: const HouseSpec *hs = GetHouseSpecs(house_id); smatz@9310: 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) && smatz@9310: Chance16(1, 2)) { belugas@7030: AddAnimatedTile(tile); smatz@9310: } smatz@9310: smatz@9310: Town *t = GetTownByTile(tile); smatz@9310: uint32 r = Random(); truelight@0: skidd13@8424: if (HasBit(hs->callback_mask, CBM_HOUSE_PRODUCE_CARGO)) { peter1138@7141: for (uint i = 0; i < 256; i++) { peter1138@7141: uint16 callback = GetHouseCallback(CBID_HOUSE_PRODUCE_CARGO, i, r, house_id, t, tile); peter1138@7141: belugas@8783: if (callback == CALLBACK_FAILED || callback == CALLBACK_HOUSEPRODCARGO_END) break; peter1138@7141: peter1138@7141: CargoID cargo = GetCargoTranslation(GB(callback, 8, 7), hs->grffile); peter1138@7141: if (cargo == CT_INVALID) continue; peter1138@7141: peter1138@7141: uint amt = GB(callback, 0, 8); peter1138@7141: uint moved = MoveGoodsToStation(tile, 1, 1, cargo, amt); peter1138@7141: peter1138@7141: const CargoSpec *cs = GetCargo(cargo); peter1138@7141: switch (cs->town_effect) { peter1138@7141: case TE_PASSENGERS: peter1138@7141: t->new_max_pass += amt; peter1138@7141: t->new_act_pass += moved; peter1138@7141: break; peter1138@7141: peter1138@7141: case TE_MAIL: peter1138@7141: t->new_max_mail += amt; peter1138@7141: t->new_act_mail += moved; peter1138@7141: break; peter1138@7141: peter1138@7141: default: peter1138@7141: break; peter1138@7141: } peter1138@7141: } peter1138@7141: } else { peter1138@7141: if (GB(r, 0, 8) < hs->population) { peter1138@7141: uint amt = GB(r, 0, 8) / 8 + 1; peter1138@7141: peter1138@7141: if (_economy.fluct <= 0) amt = (amt + 1) >> 1; peter1138@7141: t->new_max_pass += amt; smatz@9310: t->new_act_pass += MoveGoodsToStation(tile, 1, 1, CT_PASSENGERS, amt); peter1138@7141: } peter1138@7141: peter1138@7141: if (GB(r, 8, 8) < hs->mail_generation) { peter1138@7141: uint amt = GB(r, 8, 8) / 8 + 1; peter1138@7141: peter1138@7141: if (_economy.fluct <= 0) amt = (amt + 1) >> 1; peter1138@7141: t->new_max_mail += amt; smatz@9310: t->new_act_mail += MoveGoodsToStation(tile, 1, 1, CT_MAIL, amt); peter1138@7141: } truelight@0: } truelight@0: maedhros@6658: _current_player = OWNER_TOWN; maedhros@6658: belugas@7030: if (hs->building_flags & BUILDING_HAS_1_TILE && skidd13@8424: 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? */ smatz@8981: if (GB(r, 24, 8) >= 12) BuildTownHouse(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: rubidium@7439: static CommandCost ClearTile_Town(TileIndex tile, byte flags) truelight@0: { smatz@9310: if ((flags & DC_AUTO) && !(flags & DC_AI_BUILDING)) return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED); smatz@9310: if (!CanDeleteHouse(tile)) return CMD_ERROR; smatz@9310: smatz@9310: const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile)); smatz@9310: rubidium@8726: CommandCost cost(EXPENSES_CONSTRUCTION); belugas@10832: cost.AddCost(hs->GetRemovalCost()); truelight@0: smatz@9310: int rating = hs->remove_rating_decrease; truelight@0: _cleared_town_rating += rating; smatz@9310: Town *t = _cleared_town = GetTownByTile(tile); truelight@193: rubidium@11161: if (IsValidPlayerID(_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: glx@8728: ChangeTownRating(t, -rating, RATING_HOUSE_MINIMUM); truelight@0: if (flags & DC_EXEC) { truelight@0: ClearTownHouse(t, tile); truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: rubidium@9342: static void GetProducedCargo_Town(TileIndex tile, CargoID *b) rubidium@9342: { rubidium@9342: HouseID house_id = GetHouseType(tile); rubidium@9342: const HouseSpec *hs = GetHouseSpecs(house_id); rubidium@9342: Town *t = GetTownByTile(tile); rubidium@9342: rubidium@9342: if (HasBit(hs->callback_mask, CBM_HOUSE_PRODUCE_CARGO)) { rubidium@9342: for (uint i = 0; i < 256; i++) { rubidium@9342: uint16 callback = GetHouseCallback(CBID_HOUSE_PRODUCE_CARGO, i, 0, house_id, t, tile); rubidium@9342: rubidium@9342: if (callback == CALLBACK_FAILED || callback == CALLBACK_HOUSEPRODCARGO_END) break; rubidium@9342: rubidium@9342: CargoID cargo = GetCargoTranslation(GB(callback, 8, 7), hs->grffile); rubidium@9342: rubidium@9342: if (cargo == CT_INVALID) continue; rubidium@9342: *(b++) = cargo; rubidium@9342: } rubidium@9342: } else { rubidium@9342: if (hs->population > 0) { rubidium@9342: *(b++) = CT_PASSENGERS; rubidium@9342: } rubidium@9342: if (hs->mail_generation > 0) { rubidium@9342: *(b++) = CT_MAIL; rubidium@9342: } rubidium@9342: } rubidium@9342: } rubidium@9342: tron@1977: static void GetAcceptedCargo_Town(TileIndex tile, AcceptedCargo ac) truelight@0: { smatz@9310: const 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 */ skidd13@8424: if (HasBit(hs->callback_mask, CBM_HOUSE_ACCEPT_CARGO)) { peter1138@7138: uint16 callback = GetHouseCallback(CBID_HOUSE_ACCEPT_CARGO, 0, 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 */ skidd13@8424: if (HasBit(hs->callback_mask, CBM_HOUSE_CARGO_ACCEPTANCE)) { peter1138@7138: uint16 callback = GetHouseCallback(CBID_HOUSE_CARGO_ACCEPTANCE, 0, 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); rubidium@10775: if (_settings_game.game_creation.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: frosch@10662: td->owner[0] = OWNER_TOWN; truelight@0: } truelight@0: frosch@9112: static TrackStatus GetTileTrackStatus_Town(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side) truelight@0: { truelight@0: /* not used */ truelight@0: return 0; truelight@0: } truelight@0: Darkvater@2436: static void ChangeTileOwner_Town(TileIndex tile, PlayerID old_player, PlayerID new_player) truelight@0: { truelight@0: /* not used */ truelight@0: } truelight@0: tron@2817: static bool GrowTown(Town *t); tron@2817: truelight@0: static void TownTickHandler(Town *t) truelight@0: { skidd13@8424: 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: belugas@8062: /** belugas@8062: * Return the RoadBits of a tile belugas@8062: * belugas@8062: * @note There are many other functions doing things like that. belugas@8062: * @note Needs to be checked for needlessness. belugas@8062: * @param tile The tile we want to analyse belugas@8062: * @return The roadbits of the given tile belugas@8062: */ belugas@8062: static RoadBits GetTownRoadBits(TileIndex tile) truelight@0: { rubidium@7157: TrackBits b = GetAnyRoadTrackBits(tile, ROADTYPE_ROAD); rubidium@5838: RoadBits r = ROAD_NONE; tron@2639: belugas@7721: if (b == TRACK_BIT_NONE) return r; tron@3150: if (b & TRACK_BIT_X) r |= ROAD_X; tron@3150: if (b & TRACK_BIT_Y) r |= ROAD_Y; tron@3150: if (b & TRACK_BIT_UPPER) r |= ROAD_NE | ROAD_NW; tron@3150: if (b & TRACK_BIT_LOWER) r |= ROAD_SE | ROAD_SW; tron@3150: if (b & TRACK_BIT_LEFT) r |= ROAD_NW | ROAD_SW; tron@3150: if (b & TRACK_BIT_RIGHT) r |= ROAD_NE | ROAD_SE; truelight@0: return r; truelight@0: } truelight@0: 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@8062: * given distance is a road tile else false belugas@7067: */ skidd13@8947: static bool IsNeighborRoadTile(TileIndex tile, const DiagDirection dir, uint dist_multi) belugas@7067: { skidd13@8947: /* Lookup table for the used diff values */ skidd13@8947: const TileIndexDiff tid_lt[3] = { skidd13@8947: TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90RIGHT)), skidd13@8947: TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT)), skidd13@8947: TileOffsByDiagDir(ReverseDiagDir(dir)), skidd13@8947: }; belugas@8062: belugas@8062: /* We add 1 to the distance because we want to get 1 for belugas@8062: * the min distance multiplyer and not 0. belugas@8062: * Therefore we start at 4. The 4 is used because skidd13@8947: * there are 4 tiles per distance step to check. */ belugas@8062: dist_multi = (dist_multi + 1) * 4; belugas@8062: for (uint pos = 4; pos < dist_multi; pos++) { belugas@8062: TileIndexDiff cur = 0; belugas@8062: /* For each even value of pos add the right TileIndexDiff belugas@8062: * for each uneven value the left TileIndexDiff skidd13@8947: * for each with 2nd bit set (2,3,6,7,..) add the reversed TileIndexDiff */ belugas@8062: cur += tid_lt[(pos & 1) ? 0 : 1]; belugas@8062: if (pos & 2) cur += tid_lt[2]; belugas@8062: rubidium@8137: cur = (uint)(pos / 4) * cur; // Multiply for the fitting distance belugas@8062: if (GetTownRoadBits(TILE_ADD(tile, cur)) & DiagDirToRoadBits((pos & 2) ? dir : ReverseDiagDir(dir))) return true; belugas@8062: } belugas@8062: return false; belugas@7067: } belugas@7067: belugas@8062: /** belugas@8062: * Check if a Road is allowed on a given tile belugas@8062: * skidd13@9292: * @param t The current town belugas@8062: * @param tile The target tile belugas@8062: * @param dir The direction in which we want to extend the town belugas@8062: * @return true if it is allowed else false belugas@8062: */ skidd13@9292: static bool IsRoadAllowedHere(Town *t, TileIndex tile, DiagDirection dir) truelight@0: { skidd13@8947: if (TileX(tile) < 2 || TileX(tile) >= MapMaxX() || TileY(tile) < 2 || TileY(tile) >= MapMaxY()) return false; rubidium@7489: belugas@8062: Slope cur_slope, desired_slope; truelight@0: tron@2639: for (;;) { belugas@6918: /* Check if there already is a road at this point? */ belugas@8062: if (GetTownRoadBits(tile) == ROAD_NONE) { skidd13@8947: /* No, try if we are able to build a road piece there. skidd13@8947: * 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. */ belugas@8062: if (CmdFailed(DoCommand(tile, ((dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? ROAD_X : ROAD_Y), 0, DC_AUTO, CMD_BUILD_ROAD)) && tron@3491: CmdFailed(DoCommand(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR))) truelight@0: return false; truelight@0: } truelight@0: belugas@8062: cur_slope = GetTileSlope(tile, NULL); belugas@8062: if (cur_slope == SLOPE_FLAT) { pasky@465: no_slope: belugas@7067: /* Tile has no slope */ skidd13@9292: switch (t->GetActiveLayout()) { belugas@7067: default: NOT_REACHED(); belugas@7067: skidd13@8947: case TL_ORIGINAL: // Disallow the road if any neighboring tile has a road (distance: 1) belugas@8062: return !IsNeighborRoadTile(tile, dir, 1); belugas@7067: skidd13@8947: case TL_BETTER_ROADS: // Disallow the road if any neighboring tile has a road (distance: 1 and 2). belugas@8062: return !IsNeighborRoadTile(tile, dir, 2); 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. */ rubidium@8381: desired_slope = (dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? SLOPE_NW : SLOPE_NE; belugas@8062: if (desired_slope != cur_slope && ComplementSlope(desired_slope) != cur_slope) { skidd13@8463: if (Chance16(1, 8)) { rubidium@8262: CommandCost res = CMD_ERROR; skidd13@8463: if (!_generating_world && Chance16(1, 10)) { skidd13@8947: /* Note: Do not replace "^ SLOPE_ELEVATED" with ComplementSlope(). The slope might be steep. */ skidd13@8947: res = DoCommand(tile, Chance16(1, 16) ? cur_slope : cur_slope ^ SLOPE_ELEVATED, 0, rubidium@8262: DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND); tron@2639: } skidd13@8463: if (CmdFailed(res) && Chance16(1, 3)) { 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: TILE_ASSERT(tile); truelight@0: smatz@9310: CommandCost r = DoCommand(tile, edges, dir, DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND); smatz@9314: if (CmdFailed(r) || r.GetCost() >= (_price.terraform + 2) * 8) return false; tron@3491: DoCommand(tile, edges, dir, DC_AUTO | DC_NO_WATER | DC_EXEC, CMD_TERRAFORM_LAND); truelight@0: return true; truelight@0: } truelight@0: tron@1977: static void LevelTownLand(TileIndex tile) truelight@0: { truelight@0: TILE_ASSERT(tile); truelight@0: belugas@6918: /* Don't terraform if land is plain or if there's a house there. */ tron@3055: if (IsTileType(tile, MP_HOUSE)) return; smatz@9310: Slope tileh = GetTileSlope(tile, NULL); tron@3636: if (tileh == SLOPE_FLAT) return; truelight@0: belugas@6918: /* First try up, then down */ skidd13@8947: if (!TerraformTownTile(tile, ~tileh & SLOPE_ELEVATED, 1)) { skidd13@8947: TerraformTownTile(tile, tileh & SLOPE_ELEVATED, 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@8062: * @param dir The direction to which we are growing ATM belugas@7067: * @return the RoadBit of the current tile regarding belugas@7067: * the selected town layout belugas@7067: */ skidd13@9292: static RoadBits GetTownRoadGridElement(Town *t, TileIndex tile, DiagDirection dir) belugas@7067: { belugas@7067: /* align the grid to the downtown */ rubidium@8137: TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile); // Vector from downtown to the tile belugas@8062: RoadBits rcmd = ROAD_NONE; belugas@7067: skidd13@9292: switch (t->GetActiveLayout()) { belugas@7067: default: NOT_REACHED(); belugas@7067: belugas@7067: case TL_2X2_GRID: belugas@8062: if ((grid_pos.x % 3) == 0) rcmd |= ROAD_Y; belugas@8062: if ((grid_pos.y % 3) == 0) rcmd |= ROAD_X; belugas@7067: break; belugas@7067: belugas@7067: case TL_3X3_GRID: belugas@8062: if ((grid_pos.x % 4) == 0) rcmd |= ROAD_Y; belugas@8062: if ((grid_pos.y % 4) == 0) rcmd |= ROAD_X; belugas@7067: break; belugas@7067: } belugas@7067: rubidium@8099: /* Optimise only X-junctions */ rubidium@8262: if (rcmd != ROAD_ALL) return rcmd; rubidium@8262: rubidium@8262: RoadBits rb_template; rubidium@8262: rubidium@8262: switch (GetTileSlope(tile, NULL)) { rubidium@8262: default: rb_template = ROAD_ALL; break; rubidium@8262: case SLOPE_W: rb_template = ROAD_NW | ROAD_SW; break; rubidium@8262: case SLOPE_SW: rb_template = ROAD_Y | ROAD_SW; break; rubidium@8262: case SLOPE_S: rb_template = ROAD_SW | ROAD_SE; break; rubidium@8262: case SLOPE_SE: rb_template = ROAD_X | ROAD_SE; break; rubidium@8262: case SLOPE_E: rb_template = ROAD_SE | ROAD_NE; break; rubidium@8262: case SLOPE_NE: rb_template = ROAD_Y | ROAD_NE; break; rubidium@8262: case SLOPE_N: rb_template = ROAD_NE | ROAD_NW; break; rubidium@8262: case SLOPE_NW: rb_template = ROAD_X | ROAD_NW; break; rubidium@8262: case SLOPE_STEEP_W: rubidium@8262: case SLOPE_STEEP_S: rubidium@8262: case SLOPE_STEEP_E: rubidium@8262: case SLOPE_STEEP_N: rubidium@8262: rb_template = ROAD_NONE; rubidium@8262: break; belugas@7067: } belugas@8062: rubidium@8262: /* Stop if the template is compatible to the growth dir */ rubidium@8262: if (DiagDirToRoadBits(ReverseDiagDir(dir)) & rb_template) return rb_template; rubidium@8262: /* If not generate a straight road in the direction of the growth */ rubidium@8262: return DiagDirToRoadBits(dir) | DiagDirToRoadBits(ReverseDiagDir(dir)); belugas@7067: } belugas@7067: belugas@7067: /** rubidium@8137: * Grows the town with an extra house. rubidium@8137: * Check if there are enough neighbor house tiles rubidium@8137: * next to the current tile. If there are enough rubidium@8137: * add another house. belugas@7067: * rubidium@8137: * @param t The current town rubidium@8137: * @param tile The target tile for the extra house rubidium@8137: * @return true if an extra house has been added belugas@7067: */ rubidium@8137: static bool GrowTownWithExtraHouse(Town *t, TileIndex tile) belugas@7067: { belugas@7067: /* We can't look further than that. */ rubidium@8137: if (TileX(tile) < 2 || TileY(tile) < 2 || MapMaxX() <= TileX(tile) || MapMaxY() <= TileY(tile)) return false; rubidium@8137: rubidium@8137: uint counter = 0; // counts the house neighbor tiles rubidium@8137: rubidium@8137: /* Check the tiles E,N,W and S of the current tile for houses */ rubidium@8137: for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) { rubidium@8137: rubidium@8137: if (IsTileType(TileAddByDiagDir(tile, dir), MP_HOUSE)) counter++; rubidium@8137: rubidium@8137: /* If there are enough neighbors stop here */ rubidium@8137: if (counter >= 3) { rubidium@8137: if (BuildTownHouse(t, tile)) { rubidium@8180: _grow_town_result = GROWTH_SUCCEED; rubidium@8137: return true; rubidium@8137: } rubidium@8137: return false; rubidium@8137: } belugas@7067: } rubidium@8137: return false; rubidium@8137: } rubidium@8137: rubidium@8137: /** rubidium@8137: * Grows the town with a road piece. rubidium@8137: * rubidium@8137: * @param t The current town rubidium@8137: * @param tile The current tile rubidium@8137: * @param rcmd The RoadBits we want to build on the tile rubidium@8137: * @return true if the RoadBits have been added else false rubidium@8137: */ rubidium@8137: static bool GrowTownWithRoad(const Town *t, TileIndex tile, RoadBits rcmd) rubidium@8137: { rubidium@8137: if (CmdSucceeded(DoCommand(tile, rcmd, t->index, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD))) { rubidium@8180: _grow_town_result = GROWTH_SUCCEED; rubidium@8137: return true; rubidium@8137: } rubidium@8137: return false; rubidium@8137: } rubidium@8137: rubidium@8137: /** rubidium@8137: * Grows the town with a bridge. rubidium@8137: * At first we check if a bridge is reasonable. rubidium@8137: * If so we check if we are able to build it. rubidium@8137: * rubidium@8137: * @param t The current town rubidium@8137: * @param tile The current tile truelight@8341: * @param bridge_dir The valid direction in which to grow a bridge rubidium@8137: * @return true if a bridge has been build else false rubidium@8137: */ skidd13@8947: static bool GrowTownWithBridge(const Town *t, const TileIndex tile, const DiagDirection bridge_dir) rubidium@8137: { truelight@8341: assert(bridge_dir < DIAGDIR_END); truelight@8341: truelight@8341: const Slope slope = GetTileSlope(tile, NULL); truelight@8341: if (slope == SLOPE_FLAT) return false; // no slope, no bridge truelight@8341: truelight@8341: /* Make sure the direction is compatible with the slope. skidd13@8947: * Well we check if the slope has an up bit set in the skidd13@8947: * reverse direction. */ skidd13@8947: if (HASBITS(slope, InclinedSlope(bridge_dir))) return false; rubidium@8137: truelight@8343: /* Assure that the bridge is connectable to the start side */ truelight@8343: if (!(GetTownRoadBits(TileAddByDiagDir(tile, ReverseDiagDir(bridge_dir))) & DiagDirToRoadBits(bridge_dir))) return false; truelight@8343: rubidium@8137: /* We are in the right direction */ skidd13@8947: uint8 bridge_length = 0; // This value stores the length of the possible bridge rubidium@8137: TileIndex bridge_tile = tile; // Used to store the other waterside rubidium@8137: skidd13@8947: const int delta = TileOffsByDiagDir(bridge_dir); rubidium@8137: do { rubidium@8137: if (bridge_length++ >= 11) { rubidium@8137: /* Max 11 tile long bridges */ rubidium@8137: return false; belugas@7067: } rubidium@8367: bridge_tile += delta; skidd13@8947: } while (TileX(bridge_tile) != 0 && TileY(bridge_tile) != 0 && IsWaterTile(bridge_tile)); rubidium@8137: rubidium@8137: /* no water tiles in between? */ rubidium@8137: if (bridge_length == 1) return false; rubidium@8137: rubidium@8137: for (uint8 times = 0; times <= 22; times++) { rubidium@8137: byte bridge_type = RandomRange(MAX_BRIDGES - 1); rubidium@8137: rubidium@8137: /* Can we actually build the bridge? */ rubidium@8137: if (CmdSucceeded(DoCommand(tile, bridge_tile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_AUTO, CMD_BUILD_BRIDGE))) { rubidium@8137: DoCommand(tile, bridge_tile, bridge_type | ((0x80 | ROADTYPES_ROAD) << 8), DC_EXEC | DC_AUTO, CMD_BUILD_BRIDGE); rubidium@8180: _grow_town_result = GROWTH_SUCCEED; belugas@7067: return true; belugas@7067: } belugas@7067: } rubidium@8137: /* Quit if it selecting an appropiate bridge type fails a large number of times. */ 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: rubidium@8137: * @li Generate a random tile and check if there is a road allowed rubidium@8137: * @li TL_ORIGINAL rubidium@8137: * @li TL_BETTER_ROADS rubidium@8137: * @li Check if the town geometry allows a road and which one rubidium@8137: * @li TL_2X2_GRID rubidium@8137: * @li TL_3X3_GRID rubidium@8137: * @li Forbid roads, only build houses rubidium@8137: * @li TL_NO_ROADS belugas@7067: * belugas@8062: * @param tile_ptr The current tile belugas@8062: * @param cur_rb The current tiles RoadBits belugas@8062: * @param target_dir The target road dir belugas@8062: * @param t1 The current town belugas@7067: */ rubidium@8137: static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection target_dir, Town *t1) truelight@0: { rubidium@8137: RoadBits rcmd = ROAD_NONE; // RoadBits for the road construction command rubidium@8137: TileIndex tile = *tile_ptr; // The main tile on which we base our growth truelight@0: truelight@0: TILE_ASSERT(tile); truelight@0: belugas@8062: if (cur_rb == ROAD_NONE) { belugas@6918: /* Tile has no road. First reset the status counter belugas@6918: * to say that this is the last iteration. */ rubidium@8180: _grow_town_result = GROWTH_SEARCH_STOPPED; truelight@0: belugas@6918: /* Remove hills etc */ truelight@0: LevelTownLand(tile); truelight@0: belugas@6918: /* Is a road allowed here? */ skidd13@9292: switch (t1->GetActiveLayout()) { 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@8062: rcmd = GetTownRoadGridElement(t1, tile, target_dir); belugas@8062: if (rcmd == ROAD_NONE) return; belugas@7067: break; belugas@7067: belugas@7067: case TL_BETTER_ROADS: belugas@7067: case TL_ORIGINAL: skidd13@9292: if (!IsRoadAllowedHere(t1, tile, target_dir)) return; belugas@8062: belugas@8062: DiagDirection source_dir = ReverseDiagDir(target_dir); belugas@8062: skidd13@8463: if (Chance16(1, 4)) { belugas@8062: /* Randomize a new target dir */ belugas@8062: do target_dir = RandomDiagDir(); while (target_dir == source_dir); belugas@7067: } belugas@7067: skidd13@9292: if (!IsRoadAllowedHere(t1, TileAddByDiagDir(tile, target_dir), target_dir)) { belugas@7067: /* A road is not allowed to continue the randomized road, belugas@8062: * return if the road we're trying to build is curved. */ belugas@8062: if (target_dir != ReverseDiagDir(source_dir)) return; belugas@7067: belugas@7067: /* Return if neither side of the new road is a house */ rubidium@8137: if (!IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90RIGHT)), MP_HOUSE) && rubidium@8137: !IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90LEFT)), 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@8062: rcmd = DiagDirToRoadBits(target_dir) | DiagDirToRoadBits(source_dir); belugas@7067: break; tron@2637: } truelight@0: rubidium@8137: } else if (target_dir < DIAGDIR_END && !(cur_rb & DiagDirToRoadBits(ReverseDiagDir(target_dir)))) { belugas@6918: /* Continue building on a partial road. belugas@8062: * Should be allways OK, so we only generate belugas@8062: * the fitting RoadBits */ rubidium@8180: _grow_town_result = GROWTH_SEARCH_STOPPED; belugas@7067: skidd13@9292: switch (t1->GetActiveLayout()) { 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: rubidium@10230: rcmd = GetTownRoadGridElement(t1, tile, target_dir); belugas@7067: break; belugas@7067: belugas@7067: case TL_BETTER_ROADS: belugas@7067: case TL_ORIGINAL: belugas@8062: rcmd = DiagDirToRoadBits(ReverseDiagDir(target_dir)); belugas@7067: break; belugas@7067: } truelight@0: } else { rubidium@8137: bool allow_house = false; // Value which decides if we want to construct a house truelight@0: belugas@6918: /* Reached a tunnel/bridge? Then continue at the other side of it. */ rubidium@5696: if (IsTileType(tile, MP_TUNNELBRIDGE)) { smatz@8584: if (GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD) { smatz@8693: *tile_ptr = GetOtherTunnelBridgeEnd(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. */ belugas@8062: target_dir = RandomDiagDir(); belugas@8062: if (cur_rb & DiagDirToRoadBits(target_dir)) return; truelight@0: belugas@6918: /* This is the tile we will reach if we extend to this direction. */ rubidium@8137: TileIndex house_tile = TileAddByDiagDir(tile, target_dir); // position of a possible house belugas@8062: belugas@8062: /* Don't walk into water. */ rubidium@8235: if (IsWaterTile(house_tile)) return; truelight@0: skidd13@9292: switch (t1->GetActiveLayout()) { 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! */ rubidium@8137: GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir)); rubidium@8137: /* FALL THROUGH */ belugas@7067: belugas@7067: case TL_2X2_GRID: rubidium@8137: rcmd = GetTownRoadGridElement(t1, house_tile, target_dir); belugas@7067: allow_house = (rcmd == ROAD_NONE); belugas@7067: break; belugas@7067: belugas@7067: case TL_BETTER_ROADS: /* Use original afterwards! */ rubidium@8137: GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir)); rubidium@8137: /* FALL THROUGH */ 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@8062: rcmd = DiagDirToRoadBits(target_dir); skidd13@9292: allow_house = (!IsRoadAllowedHere(t1, house_tile, target_dir) || Chance16(6, 10)); belugas@7067: break; belugas@7067: } belugas@7067: belugas@7067: if (allow_house) { belugas@7067: /* Build a house, but not if there already is a house there. */ rubidium@8137: if (!IsTileType(house_tile, MP_HOUSE)) { belugas@6918: /* Level the land if possible */ skidd13@8463: if (Chance16(1, 6)) LevelTownLand(house_tile); truelight@0: belugas@6918: /* And build a house. belugas@6918: * Set result to -1 if we managed to build it. */ rubidium@8137: if (BuildTownHouse(t1, house_tile)) { rubidium@8180: _grow_town_result = GROWTH_SUCCEED; belugas@7067: } truelight@0: } truelight@0: return; truelight@0: } truelight@0: rubidium@8180: _grow_town_result = GROWTH_SEARCH_STOPPED; truelight@0: } truelight@0: belugas@6918: /* Return if a water tile */ rubidium@8235: if (IsWaterTile(tile)) return; truelight@0: rubidium@8137: /* Make the roads look nicer */ rubidium@8137: rcmd = CleanUpRoadBits(tile, rcmd); rubidium@8137: if (rcmd == ROAD_NONE) return; rubidium@8137: truelight@8341: /* Only use the target direction for bridges to ensure they're connected. truelight@8341: * The target_dir is as computed previously according to town layout, so truelight@8341: * it will match it perfectly. */ truelight@8341: if (GrowTownWithBridge(t1, tile, target_dir)) return; rubidium@8137: rubidium@8137: GrowTownWithRoad(t1, tile, rcmd); 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: { belugas@8062: /* Special case. belugas@8062: * @see GrowTownInTile Check the else if belugas@8062: */ rubidium@8137: DiagDirection target_dir = DIAGDIR_END; // The direction in which we want to extend the town 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. */ skidd13@9292: switch (t->GetActiveLayout()) { 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 { rubidium@8137: RoadBits cur_rb = GetTownRoadBits(tile); // The RoadBits of the current tile truelight@0: belugas@6918: /* Try to grow the town from this point */ belugas@8062: GrowTownInTile(&tile, cur_rb, target_dir, t); truelight@0: belugas@6918: /* Exclude the source position from the bitmask belugas@6918: * and return if no more road blocks available */ belugas@8062: cur_rb &= ~DiagDirToRoadBits(ReverseDiagDir(target_dir)); belugas@8062: if (cur_rb == ROAD_NONE) 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. */ belugas@8062: do target_dir = RandomDiagDir(); while (!(cur_rb & DiagDirToRoadBits(target_dir))); rubidium@8137: tile = TileAddByDiagDir(tile, target_dir); truelight@0: frosch@10689: if (IsTileType(tile, MP_ROAD) && !IsRoadDepot(tile) && HasTileRoadType(tile, ROADTYPE_ROAD)) { truelight@1327: /* Don't allow building over roads of other cities */ frosch@10689: if (IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN) && GetTownByTile(tile) != t) { rubidium@8180: _grow_town_result = GROWTH_SUCCEED; frosch@10689: } else if (IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_NONE) && _game_mode == GM_EDITOR) { truelight@1327: /* If we are in the SE, and this road-piece has no town owner yet, it just found an rubidium@4549: * owner :) (happy happy happy road now) */ frosch@10689: SetRoadOwner(tile, ROADTYPE_ROAD, 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@8062: /** belugas@8062: * Generate a random road block. belugas@6918: * The probability of a straight road belugas@8062: * is somewhat higher than a curved. belugas@8062: * belugas@8062: * @return A RoadBits value with 2 bits set belugas@8062: */ 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; belugas@8062: return (RoadBits)((ROAD_NW << a) + (ROAD_NW << b)); truelight@0: } truelight@0: belugas@6918: /** Grow the town smatz@9310: * @param t town to grow smatz@9310: * @return true iff a house was built smatz@9310: */ tron@2817: static bool GrowTown(Town *t) truelight@0: { belugas@8062: /* Let the town be a ghost town belugas@8062: * The player wanted it in such a way. Thus there he has it. ;) belugas@8062: * Never reached in editor mode. */ rubidium@10775: if (_settings_game.economy.town_layout == TL_NO_ROADS && _generating_world) { belugas@8062: return false; belugas@8062: } 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: }; belugas@7067: belugas@6918: /* Current player is a town */ belugas@8062: PlayerID old_player = _current_player; truelight@0: _current_player = OWNER_TOWN; truelight@0: rubidium@8137: TileIndex tile = t->xy; // The tile we are working with ATM belugas@8062: belugas@6918: /* Find a road that we can base the construction on. */ smatz@9310: const TileIndexDiffC *ptr; tron@909: for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) { belugas@8062: if (GetTownRoadBits(tile) != ROAD_NONE) { truelight@260: int r = GrowTownAtRoad(t, tile); truelight@260: _current_player = old_player; rubidium@5838: return r != 0; truelight@0: } tron@909: tile = TILE_ADD(tile, ToTileIndexDiff(*ptr)); tron@909: } truelight@0: 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) { rubidium@7442: if (CmdSucceeded(DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR))) { tron@3491: DoCommand(tile, GenRandomRoadBits(), t->index, DC_EXEC | DC_AUTO, CMD_BUILD_ROAD); truelight@260: _current_player = old_player; truelight@0: return true; truelight@0: } truelight@0: } tron@909: tile = TILE_ADD(tile, ToTileIndexDiff(*ptr)); tron@909: } truelight@0: truelight@260: _current_player = old_player; truelight@0: return false; truelight@0: } truelight@0: skidd13@9203: void UpdateTownRadius(Town *t) truelight@0: { smatz@10199: static const uint32 _town_squared_town_zone_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) { rubidium@10195: memcpy(t->squared_town_zone_radius, _town_squared_town_zone_radius_data[t->num_houses / 4], sizeof(t->squared_town_zone_radius)); pasky@470: } else { pasky@470: int mass = t->num_houses / 8; rubidium@10195: /* Actually we are proportional to sqrt() but that's right because we are covering an area. rubidium@10195: * The offsets are to make sure the radii do not decrease in size when going from the table rubidium@10195: * to the calculated value.*/ rubidium@10195: t->squared_town_zone_radius[0] = mass * 15 - 40; rubidium@10195: t->squared_town_zone_radius[1] = mass * 9 - 15; rubidium@10195: t->squared_town_zone_radius[2] = 0; rubidium@10195: t->squared_town_zone_radius[3] = mass * 5 - 5; rubidium@10195: t->squared_town_zone_radius[4] = mass * 3 + 5; pasky@470: } truelight@0: } truelight@0: pasky@1421: static bool CreateTownName(uint32 *townnameparts) truelight@0: { glx@7452: extern int _nb_orig_names; truelight@0: Town *t2; truelight@0: char buf1[64]; truelight@0: char buf2[64]; truelight@0: uint32 r; pasky@1421: /* Do not set too low tries, since when we run out of names, we loop pasky@1421: * for #tries only one time anyway - then we stop generating more pasky@1421: * towns. Do not show it too high neither, since looping through all pasky@1421: * the other towns may take considerable amount of time (10000 is pasky@1421: * too much). */ pasky@1421: int tries = 1000; rubidium@10775: bool grf = (_settings_game.game_creation.town_name >= _nb_orig_names); rubidium@10775: uint32 grfid = grf ? GetGRFTownNameId(_settings_game.game_creation.town_name - _nb_orig_names) : 0; rubidium@10775: uint16 townnametype = grf ? GetGRFTownNameType(_settings_game.game_creation.town_name - _nb_orig_names) : SPECSTR_TOWNNAME_START + _settings_game.game_creation.town_name; truelight@0: smatz@9310: assert(townnameparts != NULL); truelight@0: tron@2952: for (;;) { truelight@0: restart: truelight@0: r = Random(); truelight@0: tron@534: SetDParam(0, r); glx@7452: if (grf && grfid != 0) { glx@7452: GRFTownNameGenerate(buf1, grfid, townnametype, r, lastof(buf1)); glx@7452: } else { glx@7452: GetString(buf1, townnametype, lastof(buf1)); glx@7452: } 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: { glx@7452: extern int _nb_orig_names; truelight@0: truelight@0: t->xy = tile; truelight@0: t->num_houses = 0; truelight@0: t->time_until_rebuild = 10; truelight@0: UpdateTownRadius(t); truelight@0: t->flags12 = 0; truelight@0: t->population = 0; truelight@0: t->grow_counter = 0; truelight@0: t->growth_rate = 250; truelight@0: t->new_max_pass = 0; truelight@0: t->new_max_mail = 0; truelight@0: t->new_act_pass = 0; truelight@0: t->new_act_mail = 0; truelight@0: t->max_pass = 0; truelight@0: t->max_mail = 0; truelight@0: t->act_pass = 0; truelight@0: t->act_mail = 0; truelight@0: truelight@0: t->pct_pass_transported = 0; truelight@0: t->pct_mail_transported = 0; truelight@0: t->fund_buildings_months = 0; truelight@0: t->new_act_food = 0; darkvater@4: t->new_act_water = 0; truelight@0: t->act_food = 0; darkvater@4: t->act_water = 0; truelight@0: smatz@9310: for (uint i = 0; i != MAX_PLAYERS; i++) t->ratings[i] = RATING_INITIAL; truelight@0: truelight@0: t->have_ratings = 0; rubidium@5838: t->exclusivity = INVALID_PLAYER; dominik@121: t->exclusive_counter = 0; truelight@0: t->statues = 0; truelight@0: rubidium@10775: if (_settings_game.game_creation.town_name < _nb_orig_names) { glx@7452: /* Original town name */ glx@7452: t->townnamegrfid = 0; rubidium@10775: t->townnametype = SPECSTR_TOWNNAME_START + _settings_game.game_creation.town_name; glx@7452: } else { glx@7452: /* Newgrf town name */ rubidium@10775: t->townnamegrfid = GetGRFTownNameId(_settings_game.game_creation.town_name - _nb_orig_names); rubidium@10775: t->townnametype = GetGRFTownNameType(_settings_game.game_creation.town_name - _nb_orig_names); glx@7452: } pasky@1421: t->townnameparts = townnameparts; truelight@193: truelight@0: UpdateTownVirtCoord(t); peter1138@10747: InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 0); truelight@0: skidd13@9292: t->InitializeLayout(); skidd13@9292: maedhros@6982: /* Random town size. */ smatz@9310: int 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: rubidium@10775: x *= _settings_game.economy.initial_city_size; maedhros@6982: t->larger_town = true; maedhros@6982: break; celestar@3674: } truelight@0: belugas@10682: t->noise_reached = 0; belugas@10682: truelight@0: t->num_houses += x; truelight@0: UpdateTownRadius(t); truelight@0: smatz@9310: int i = x * 4; truelight@0: do { truelight@0: GrowTown(t); truelight@0: } while (--i); truelight@0: truelight@0: t->num_houses -= x; truelight@0: UpdateTownRadius(t); truelight@0: UpdateTownMaxPass(t); truelight@0: } truelight@0: Darkvater@1793: /** Create a new town. Darkvater@1793: * This obviously only works in the scenario editor. Function not removed Darkvater@1793: * as it might be possible in the future to fund your own town :) tron@3491: * @param tile coordinates where town is built 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: */ rubidium@7439: CommandCost CmdBuildTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { Darkvater@1793: /* Only in the scenario editor */ Darkvater@1793: if (_game_mode != GM_EDITOR) return CMD_ERROR; maedhros@6982: if (p2 > TSM_CITY) return CMD_ERROR; Darkvater@1793: 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: smatz@9310: uint32 townnameparts; smatz@9310: 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 */ rubidium@10314: if (!Town::CanAllocateItem()) return_cmd_error(STR_023A_TOO_MANY_TOWNS); truelight@0: belugas@6918: /* Create the town */ truelight@0: if (flags & DC_EXEC) { rubidium@10314: Town *t = new Town(tile); truelight@0: _generating_world = true; maedhros@6982: DoCreateTown(t, tile, townnameparts, (TownSizeMode)p2, p1); truelight@0: _generating_world = false; truelight@0: } rubidium@7446: return CommandCost(); truelight@0: } truelight@0: maedhros@6982: Town *CreateRandomTown(uint attempts, TownSizeMode mode, uint size) truelight@0: { truelight@0: do { belugas@6918: /* Generate a tile index not too close from the edge */ smatz@9310: TileIndex 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: smatz@9310: uint32 townnameparts; smatz@9310: belugas@6918: /* Get a unique name for the town. */ tron@2951: if (!CreateTownName(&townnameparts)) break; pasky@1421: belugas@6918: /* Allocate a town struct */ smatz@9310: Town *t = new Town(tile); tron@2951: if (t == NULL) break; truelight@0: maedhros@6982: DoCreateTown(t, tile, townnameparts, mode, size); truelight@0: return t; smatz@9310: } while (--attempts != 0); smatz@9310: truelight@0: return NULL; truelight@0: } truelight@0: belugas@10682: static const byte _num_initial_towns[4] = {5, 11, 23, 46}; // very low, low, normal, high truelight@0: rubidium@6573: bool GenerateTowns() truelight@0: { celestar@1362: uint num = 0; rubidium@10775: uint n = ScaleByMapSize(_num_initial_towns[_settings_game.difficulty.number_towns] + (Random() & 7)); rubidium@10775: uint num_cities = _settings_game.economy.larger_towns == 0 ? 0 : n / _settings_game.economy.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; rubidium@10775: if (CreateRandomTown(20, mode, _settings_game.economy.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? */ glx@10839: if (_game_mode != GM_EDITOR) usererror("Could not generate any town"); Darkvater@2430: truelight@4384: return false; tron@4000: } celestar@1362: } Darkvater@2430: Darkvater@2430: return true; truelight@0: } truelight@0: truelight@0: belugas@8804: /** Returns the bit corresponding to the town zone of the specified tile rubidium@10195: * @param t Town on which town zone is to be found rubidium@10195: * @param tile TileIndex where town zone needs to be found belugas@8804: * @return the bit position of the given zone, as defined in HouseZones belugas@8804: */ smatz@9310: HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile) truelight@0: { tron@4000: uint dist = DistanceSquare(tile, t->xy); truelight@0: belugas@8804: if (t->fund_buildings_months && dist <= 25) return HZB_TOWN_CENTRE; belugas@8804: smatz@9310: HouseZonesBits smallest = HZB_TOWN_EDGE; smatz@9310: for (HouseZonesBits i = HZB_BEGIN; i < HZB_END; i++) { rubidium@10195: if (dist < t->squared_town_zone_radius[i]) smallest = i; truelight@0: } truelight@0: truelight@0: return smallest; truelight@0: } truelight@0: smatz@8981: /** smatz@8981: * Clears tile and builds a house or house part. smatz@8981: * @param t tile index smatz@8981: * @param tid Town index smatz@8981: * @param counter of construction step smatz@8981: * @param stage of construction (used for drawing) smatz@8981: * @param type of house. Index into house specs array smatz@8981: * @param random_bits required for newgrf houses smatz@8981: * @pre house can be built here smatz@8981: */ frosch@10964: static inline void ClearMakeHouseTile(TileIndex tile, Town *t, byte counter, byte stage, HouseID type, byte random_bits) truelight@0: { smatz@10800: CommandCost cc = DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR); smatz@9114: smatz@8981: assert(CmdSucceeded(cc)); smatz@8981: frosch@10964: IncreaseBuildingCount(t, type); frosch@10964: MakeHouseTile(tile, t->index, counter, stage, type, random_bits); rubidium@10269: if (GetHouseSpecs(type)->building_flags & BUILDING_IS_ANIMATED) AddAnimatedTile(tile); rubidium@10269: rubidium@10269: MarkTileDirtyByTile(tile); smatz@8981: } smatz@8981: smatz@9076: smatz@8981: /** smatz@8981: * Write house information into the map. For houses > 1 tile, all tiles are marked. smatz@8981: * @param t tile index smatz@8981: * @param tid Town index smatz@8981: * @param counter of construction step smatz@8981: * @param stage of construction (used for drawing) smatz@8981: * @param type of house. Index into house specs array smatz@8981: * @param random_bits required for newgrf houses smatz@8981: * @pre house can be built here smatz@8981: */ frosch@10964: static void MakeTownHouse(TileIndex t, Town *town, byte counter, byte stage, HouseID type, byte random_bits) smatz@8981: { smatz@8981: BuildingFlags size = GetHouseSpecs(type)->building_flags; smatz@8981: frosch@10964: ClearMakeHouseTile(t, town, counter, stage, type, random_bits); frosch@10964: if (size & BUILDING_2_TILES_Y) ClearMakeHouseTile(t + TileDiffXY(0, 1), town, counter, stage, ++type, random_bits); frosch@10964: if (size & BUILDING_2_TILES_X) ClearMakeHouseTile(t + TileDiffXY(1, 0), town, counter, stage, ++type, random_bits); frosch@10964: if (size & BUILDING_HAS_4_TILES) ClearMakeHouseTile(t + TileDiffXY(1, 1), town, counter, stage, ++type, random_bits); smatz@8981: } smatz@8981: smatz@8981: smatz@8981: /** smatz@8981: * Checks if a house can be built here. Important is slope, bridge above smatz@8981: * and ability to clear the land. smatz@8981: * @param tile tile to check smatz@9077: * @param town town that is checking smatz@8981: * @param noslope are slopes (foundations) allowed? smatz@8981: * @return true iff house can be built here smatz@8981: */ smatz@9077: static inline bool CanBuildHouseHere(TileIndex tile, TownID town, bool noslope) smatz@8981: { smatz@8981: /* cannot build on these slopes... */ smatz@8981: Slope slope = GetTileSlope(tile, NULL); smatz@8981: if ((noslope && slope != SLOPE_FLAT) || IsSteepSlope(slope)) return false; smatz@8981: smatz@8981: /* building under a bridge? */ smatz@8981: if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return false; smatz@8981: smatz@9077: /* do not try to build over house owned by another town */ smatz@9077: if (IsTileType(tile, MP_HOUSE) && GetTownIndex(tile) != town) return false; smatz@9077: smatz@8981: /* can we clear the land? */ smatz@8981: return CmdSucceeded(DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR)); smatz@8981: } smatz@8981: smatz@8981: smatz@8981: /** smatz@8981: * Checks if a house can be built at this tile, must have the same max z as parameter. smatz@8981: * @param tile tile to check smatz@9077: * @param town town that is checking smatz@8981: * @param z max z of this tile so more parts of a house are at the same height (with foundation) smatz@8981: * @param noslope are slopes (foundations) allowed? smatz@8981: * @return true iff house can be built here smatz@8981: * @see CanBuildHouseHere() smatz@8981: */ smatz@9077: static inline bool CheckBuildHouseSameZ(TileIndex tile, TownID town, uint z, bool noslope) smatz@8981: { smatz@9077: if (!CanBuildHouseHere(tile, town, noslope)) return false; smatz@8981: smatz@8981: /* if building on slopes is allowed, there will be flattening foundation (to tile max z) */ smatz@8981: if (GetTileMaxZ(tile) != z) return false; smatz@8981: smatz@8981: return true; smatz@8981: } smatz@8981: smatz@8981: smatz@8981: /** smatz@8981: * Checks if a house of size 2x2 can be built at this tile smatz@8981: * @param tile tile, N corner smatz@9077: * @param town town that is checking smatz@8981: * @param z maximum tile z so all tile have the same max z smatz@8981: * @param noslope are slopes (foundations) allowed? smatz@8981: * @return true iff house can be built smatz@8981: * @see CheckBuildHouseSameZ() smatz@8981: */ smatz@9077: static bool CheckFree2x2Area(TileIndex tile, TownID town, uint z, bool noslope) smatz@8981: { smatz@8981: /* we need to check this tile too because we can be at different tile now */ smatz@9077: if (!CheckBuildHouseSameZ(tile, town, z, noslope)) return false; smatz@8981: smatz@8981: for (DiagDirection d = DIAGDIR_SE; d < DIAGDIR_END; d++) { smatz@8981: tile += TileOffsByDiagDir(d); smatz@9077: if (!CheckBuildHouseSameZ(tile, town, z, noslope)) return false; truelight@0: } truelight@0: truelight@0: return true; truelight@0: } truelight@0: smatz@8981: smatz@8981: /** smatz@9076: * Checks if current town layout allows building here smatz@9076: * @param t town smatz@9076: * @param tile tile to check smatz@9076: * @return true iff town layout allows building here smatz@9076: * @note see layouts smatz@9076: */ smatz@9076: static inline bool TownLayoutAllowsHouseHere(Town *t, TileIndex tile) smatz@9076: { smatz@9076: TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile); smatz@9076: skidd13@9292: switch (t->GetActiveLayout()) { smatz@9076: case TL_2X2_GRID: smatz@9076: if ((grid_pos.x % 3) == 0 || (grid_pos.y % 3) == 0) return false; smatz@9076: break; smatz@9076: smatz@9076: case TL_3X3_GRID: smatz@9076: if ((grid_pos.x % 4) == 0 || (grid_pos.y % 4) == 0) return false; smatz@9076: break; smatz@9076: smatz@9076: default: smatz@9076: break; smatz@9076: } smatz@9076: smatz@9076: return true; smatz@9076: } smatz@9076: smatz@9076: smatz@9076: /** smatz@9076: * Checks if current town layout allows 2x2 building here smatz@9076: * @param t town smatz@9076: * @param tile tile to check smatz@9076: * @return true iff town layout allows 2x2 building here smatz@9076: * @note see layouts smatz@9076: */ smatz@9076: static inline bool TownLayoutAllows2x2HouseHere(Town *t, TileIndex tile) smatz@9076: { smatz@9076: /* MapSize() is sure dividable by both MapSizeX() and MapSizeY(), smatz@9076: * so to do only one memory access, use MapSize() */ smatz@9076: uint dx = MapSize() + TileX(t->xy) - TileX(tile); smatz@9076: uint dy = MapSize() + TileY(t->xy) - TileY(tile); smatz@9076: skidd13@9292: switch (t->GetActiveLayout()) { smatz@9076: case TL_2X2_GRID: smatz@9076: if ((dx % 3) != 0 || (dy % 3) != 0) return false; smatz@9076: break; smatz@9076: smatz@9076: case TL_3X3_GRID: smatz@9076: if ((dx % 4) < 2 || (dy % 4) < 2) return false; smatz@9076: break; smatz@9076: smatz@9076: default: smatz@9076: break; smatz@9076: } smatz@9076: smatz@9076: return true; smatz@9076: } smatz@9076: smatz@9076: smatz@9076: /** smatz@9076: * Checks if 1x2 or 2x1 building is allowed here, also takes into account current town layout smatz@9076: * Also, tests both building positions that occupy this tile smatz@9076: * @param tile tile where the building should be built smatz@9076: * @param t town smatz@9076: * @param maxz all tiles should have the same height smatz@9076: * @param noslope are slopes forbidden? smatz@9076: * @param second diagdir from first tile to second tile smatz@9076: **/ smatz@9076: static bool CheckTownBuild2House(TileIndex *tile, Town *t, uint maxz, bool noslope, DiagDirection second) smatz@9076: { smatz@9076: /* 'tile' is already checked in BuildTownHouse() - CanBuildHouseHere() and slope test */ smatz@9076: smatz@9076: TileIndex tile2 = *tile + TileOffsByDiagDir(second); smatz@9077: if (TownLayoutAllowsHouseHere(t, tile2) && CheckBuildHouseSameZ(tile2, t->index, maxz, noslope)) return true; smatz@9076: smatz@9076: tile2 = *tile + TileOffsByDiagDir(ReverseDiagDir(second)); smatz@9077: if (TownLayoutAllowsHouseHere(t, tile2) && CheckBuildHouseSameZ(tile2, t->index, maxz, noslope)) { smatz@9076: *tile = tile2; smatz@9076: return true; smatz@9076: } smatz@9076: smatz@9076: return false; smatz@9076: } smatz@9076: smatz@9076: smatz@9076: /** smatz@9076: * Checks if 2x2 building is allowed here, also takes into account current town layout smatz@9076: * Also, tests all four building positions that occupy this tile smatz@9076: * @param tile tile where the building should be built smatz@9076: * @param t town smatz@9076: * @param maxz all tiles should have the same height smatz@9076: * @param noslope are slopes forbidden? smatz@9076: **/ smatz@9076: static bool CheckTownBuild2x2House(TileIndex *tile, Town *t, uint maxz, bool noslope) smatz@9076: { smatz@9076: TileIndex tile2 = *tile; smatz@9076: smatz@9076: for (DiagDirection d = DIAGDIR_SE;;d++) { // 'd' goes through DIAGDIR_SE, DIAGDIR_SW, DIAGDIR_NW, DIAGDIR_END smatz@9077: if (TownLayoutAllows2x2HouseHere(t, tile2) && CheckFree2x2Area(tile2, t->index, maxz, noslope)) { smatz@9076: *tile = tile2; smatz@9076: return true; smatz@9076: } smatz@9076: if (d == DIAGDIR_END) break; smatz@9076: tile2 += TileOffsByDiagDir(ReverseDiagDir(d)); // go clockwise smatz@9076: } smatz@9076: smatz@9076: return false; smatz@9076: } smatz@9076: smatz@9076: smatz@9076: /** smatz@8981: * Tries to build a house at this tile smatz@8981: * @param t town the house will belong to smatz@8981: * @param tile where the house will be built smatz@8981: * @return false iff no house can be built at this tile smatz@8981: */ smatz@8981: static bool BuildTownHouse(Town *t, TileIndex tile) truelight@0: { smatz@9076: /* forbidden building here by town layout */ smatz@9076: if (!TownLayoutAllowsHouseHere(t, tile)) return false; smatz@9076: smatz@8981: /* no house allowed at all, bail out */ smatz@9077: if (!CanBuildHouseHere(tile, t->index, false)) return false; smatz@8981: smatz@8984: uint z; smatz@8984: Slope slope = GetTileSlope(tile, &z); truelight@0: belugas@8804: /* Get the town zone type of the current tile, as well as the climate. belugas@8804: * This will allow to easily compare with the specs of the new house to build */ smatz@8984: HouseZonesBits rad = GetTownRadiusGroup(t, tile); smatz@8984: smatz@8984: /* Above snow? */ rubidium@10775: int land = _settings_game.game_creation.landscape; rubidium@10775: if (land == LT_ARCTIC && z >= _settings_game.game_creation.snow_line) land = -1; smatz@8984: smatz@8984: uint bitmask = (1 << rad) + (1 << (land + 12)); truelight@0: belugas@6918: /* bits 0-4 are used belugas@6918: * bits 11-15 are used belugas@6918: * bits 5-10 are not used. */ smatz@8984: HouseID houses[HOUSE_MAX]; smatz@8984: uint num = 0; smatz@8984: uint probs[HOUSE_MAX]; smatz@8984: uint probability_max = 0; smatz@8984: smatz@8984: /* Generate a list of all possible houses that can be built. */ smatz@8984: for (uint i = 0; i < HOUSE_MAX; i++) { smatz@9310: const HouseSpec *hs = GetHouseSpecs(i); smatz@8984: /* Verify that the candidate house spec matches the current tile status */ smatz@8984: if ((~hs->building_availability & bitmask) == 0 && hs->enabled) { smatz@8984: /* Without NewHouses, all houses have probability '1' */ smatz@8984: uint cur_prob = (_loaded_newgrf_features.has_newhouses ? hs->probability : 1); smatz@8984: probability_max += cur_prob; smatz@8984: probs[num] = cur_prob; smatz@8984: houses[num++] = (HouseID)i; smatz@8984: } smatz@8984: } smatz@8984: smatz@8984: uint maxz = GetTileMaxZ(tile); smatz@8984: smatz@8984: while (probability_max > 0) { smatz@8984: uint r = RandomRange(probability_max); smatz@8984: uint i; smatz@8984: for (i = 0; i < num; i++) { smatz@8984: if (probs[i] > r) break; smatz@8984: r -= probs[i]; smatz@8984: } smatz@8984: smatz@8984: HouseID house = houses[i]; smatz@8984: probability_max -= probs[i]; smatz@8984: smatz@8984: /* remove tested house from the set */ smatz@8984: num--; smatz@8984: houses[i] = houses[num]; smatz@8984: probs[i] = probs[num]; smatz@8984: smatz@9310: const HouseSpec *hs = GetHouseSpecs(house); smatz@8984: smatz@8984: if (_loaded_newgrf_features.has_newhouses) { smatz@8984: if (hs->override != 0) { smatz@8984: house = hs->override; smatz@8984: hs = GetHouseSpecs(house); smatz@8984: } smatz@8984: smatz@8984: if ((hs->extra_flags & BUILDING_IS_HISTORICAL) && !_generating_world) continue; truelight@0: } truelight@0: belugas@10817: if (_cur_year < hs->min_year || _cur_year > hs->max_year) continue; smatz@8984: smatz@8984: /* Special houses that there can be only one of. */ smatz@8984: uint oneof = 0; smatz@8984: smatz@8984: if (hs->building_flags & BUILDING_IS_CHURCH) { smatz@8984: SetBit(oneof, TOWN_HAS_CHURCH); smatz@8984: } else if (hs->building_flags & BUILDING_IS_STADIUM) { smatz@8984: SetBit(oneof, TOWN_HAS_STADIUM); smatz@8984: } smatz@8984: rubidium@10936: if (HASBITS(t->flags12, oneof)) continue; smatz@8984: smatz@8984: /* Make sure there is no slope? */ smatz@8984: bool noslope = (hs->building_flags & TILE_NOT_SLOPED) != 0; smatz@8984: if (noslope && slope != SLOPE_FLAT) continue; smatz@8984: smatz@8984: if (hs->building_flags & TILE_SIZE_2x2) { smatz@9076: if (!CheckTownBuild2x2House(&tile, t, maxz, noslope)) continue; smatz@8984: } else if (hs->building_flags & TILE_SIZE_2x1) { smatz@9076: if (!CheckTownBuild2House(&tile, t, maxz, noslope, DIAGDIR_SW)) continue; smatz@8984: } else if (hs->building_flags & TILE_SIZE_1x2) { smatz@9076: if (!CheckTownBuild2House(&tile, t, maxz, noslope, DIAGDIR_SE)) continue; smatz@9076: } else { smatz@9076: /* 1x1 house checks are already done */ truelight@0: } smatz@8984: rubidium@10936: if (HasBit(hs->callback_mask, CBM_HOUSE_ALLOW_CONSTRUCTION)) { rubidium@10936: uint16 callback_res = GetHouseCallback(CBID_HOUSE_ALLOW_CONSTRUCTION, 0, 0, house, t, tile); rubidium@10936: if (callback_res != CALLBACK_FAILED && GB(callback_res, 0, 8) == 0) continue; rubidium@10936: } rubidium@10936: smatz@8984: /* build the house */ smatz@8984: t->num_houses++; smatz@8984: smatz@8984: /* Special houses that there can be only one of. */ smatz@8984: t->flags12 |= oneof; smatz@8984: smatz@8984: byte construction_counter = 0; smatz@8984: byte construction_stage = 0; smatz@8984: smatz@8984: if (_generating_world) { smatz@8984: uint32 r = Random(); smatz@8984: smatz@8984: construction_stage = TOWN_HOUSE_COMPLETED; smatz@8984: if (Chance16(1, 7)) construction_stage = GB(r, 0, 2); smatz@8984: smatz@8984: if (construction_stage == TOWN_HOUSE_COMPLETED) { smatz@8984: ChangePopulation(t, hs->population); smatz@8984: } else { smatz@8984: construction_counter = GB(r, 2, 2); smatz@8984: } smatz@8984: } smatz@8984: frosch@10964: MakeTownHouse(tile, t, construction_counter, construction_stage, house, Random()); smatz@8984: smatz@8984: return true; truelight@0: } truelight@0: smatz@8983: return false; truelight@0: } truelight@0: truelight@0: frosch@10964: static void DoClearTownHouseHelper(TileIndex tile, Town *t, HouseID house) truelight@0: { tron@1035: assert(IsTileType(tile, MP_HOUSE)); frosch@10964: DecreaseBuildingCount(t, house); truelight@0: DoClearSquare(tile); truelight@0: DeleteAnimatedTile(tile); truelight@0: } truelight@0: frosch@10186: /** frosch@10186: * Determines if a given HouseID is part of a multitile house. frosch@10186: * The given ID is set to the ID of the north tile and the TileDiff to the north tile is returned. frosch@10186: * frosch@10186: * @param house Is changed to the HouseID of the north tile of the same house frosch@10186: * @return TileDiff from the tile of the given HouseID to the north tile frosch@10186: */ frosch@11077: TileIndex GetHouseNorthPart(HouseID &house) frosch@10186: { frosch@10186: if (house >= 3) { // house id 0,1,2 MUST be single tile houses, or this code breaks. frosch@10186: if (GetHouseSpecs(house - 1)->building_flags & TILE_SIZE_2x1) { frosch@10186: house--; frosch@10186: return TileDiffXY(-1, 0); frosch@10186: } else if (GetHouseSpecs(house - 1)->building_flags & BUILDING_2_TILES_Y) { frosch@10186: house--; frosch@10186: return TileDiffXY(0, -1); frosch@10186: } else if (GetHouseSpecs(house - 2)->building_flags & BUILDING_HAS_4_TILES) { frosch@10186: house -= 2; frosch@10186: return TileDiffXY(-1, 0); frosch@10186: } else if (GetHouseSpecs(house - 3)->building_flags & BUILDING_HAS_4_TILES) { frosch@10186: house -= 3; frosch@10186: return TileDiffXY(-1, -1); frosch@10186: } frosch@10186: } frosch@10186: return 0; frosch@10186: } frosch@10186: maedhros@6658: void ClearTownHouse(Town *t, TileIndex tile) tron@1977: { smatz@9310: assert(IsTileType(tile, MP_HOUSE)); smatz@9310: maedhros@6658: HouseID house = GetHouseType(tile); truelight@0: belugas@6918: /* need to align the tile to point to the upper left corner of the house */ frosch@10186: tile += GetHouseNorthPart(house); // modifies house to the ID of the north tile truelight@193: smatz@9310: const HouseSpec *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--; truelight@0: belugas@6918: /* Clear flags for houses that only may exist once/town. */ maedhros@6658: if (hs->building_flags & BUILDING_IS_CHURCH) { skidd13@8425: ClrBit(t->flags12, TOWN_HAS_CHURCH); maedhros@6658: } else if (hs->building_flags & BUILDING_IS_STADIUM) { skidd13@8425: ClrBit(t->flags12, TOWN_HAS_STADIUM); tron@483: } truelight@193: belugas@6918: /* Do the actual clearing of tiles */ smatz@9310: uint eflags = hs->building_flags; frosch@10964: DoClearTownHouseHelper(tile, t, house); frosch@10964: if (eflags & BUILDING_2_TILES_Y) DoClearTownHouseHelper(tile + TileDiffXY(0, 1), t, ++house); frosch@10964: if (eflags & BUILDING_2_TILES_X) DoClearTownHouseHelper(tile + TileDiffXY(1, 0), t, ++house); frosch@10964: if (eflags & BUILDING_HAS_4_TILES) DoClearTownHouseHelper(tile + TileDiffXY(1, 1), t, ++house); truelight@0: } truelight@0: peter1138@7593: static bool IsUniqueTownName(const char *name) peter1138@7593: { peter1138@7593: const Town *t; peter1138@7593: char buf[512]; peter1138@7593: peter1138@7593: FOR_ALL_TOWNS(t) { peter1138@7593: SetDParam(0, t->index); peter1138@7593: GetString(buf, STR_TOWN, lastof(buf)); peter1138@7593: if (strcmp(buf, name) == 0) return false; peter1138@7593: } peter1138@7593: peter1138@7593: return true; peter1138@7593: } peter1138@7593: 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: */ rubidium@7439: CommandCost CmdRenameTown(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { peter1138@7593: if (!IsValidTownID(p1) || StrEmpty(_cmd_text)) return CMD_ERROR; Darkvater@1793: peter1138@7593: if (!IsUniqueTownName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE); peter1138@7593: truelight@0: if (flags & DC_EXEC) { smatz@9310: Town *t = GetTown(p1); smatz@9310: peter1138@8754: free(t->name); peter1138@8754: t->name = strdup(_cmd_text); truelight@0: truelight@0: UpdateTownVirtCoord(t); peter1138@10747: InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 1); truelight@0: UpdateAllStationVirtCoord(); glx@8205: UpdateAllWaypointSigns(); truelight@0: MarkWholeScreenDirty(); truelight@0: } rubidium@7446: return CommandCost(); truelight@0: } truelight@0: belugas@6918: /** Called from GUI */ truelight@0: void ExpandTown(Town *t) truelight@0: { truelight@0: _generating_world = true; truelight@193: truelight@835: /* The more houses, the faster we grow */ smatz@9310: uint amount = RandomRange(ClampToU16(t->num_houses / 10)) + 3; truelight@0: t->num_houses += amount; truelight@0: UpdateTownRadius(t); truelight@0: smatz@9310: uint n = amount * 10; truelight@0: do GrowTown(t); while (--n); truelight@0: truelight@0: t->num_houses -= amount; truelight@0: UpdateTownRadius(t); truelight@0: truelight@0: UpdateTownMaxPass(t); truelight@0: _generating_world = false; truelight@0: } truelight@0: rubidium@5838: extern const byte _town_action_costs[8] = { truelight@0: 2, 4, 9, 35, 48, 53, 117, 175 truelight@0: }; truelight@0: smatz@9310: static void TownActionAdvertiseSmall(Town *t) truelight@0: { tron@3877: ModifyStationRatingAround(t->xy, _current_player, 0x40, 10); truelight@0: } truelight@0: smatz@9310: static void TownActionAdvertiseMedium(Town *t) tron@3877: { tron@3877: ModifyStationRatingAround(t->xy, _current_player, 0x70, 15); tron@3877: } tron@3877: smatz@9310: static void TownActionAdvertiseLarge(Town *t) tron@3877: { tron@3877: ModifyStationRatingAround(t->xy, _current_player, 0xA0, 20); tron@3877: } tron@3877: smatz@9310: static void TownActionRoadRebuild(Town *t) truelight@0: { truelight@0: t->road_build_months = 6; truelight@193: tron@534: SetDParam(0, t->index); peter1138@7554: SetDParam(1, _current_player); truelight@0: truelight@193: AddNewsItem(STR_2055_TRAFFIC_CHAOS_IN_ROAD_REBUILDING, rubidium@10556: NS_GENERAL, t->xy, 0); truelight@0: } truelight@0: truelight@6583: static bool DoBuildStatueOfCompany(TileIndex tile, TownID town_id) truelight@0: { rubidium@8045: /* Statues can be build on slopes, just like houses. Only the steep slopes is a no go. */ rubidium@8045: if (IsSteepSlope(GetTileSlope(tile, NULL))) return false; truelight@0: tron@2986: if (!IsTileType(tile, MP_HOUSE) && tron@2986: !IsTileType(tile, MP_CLEAR) && tron@2986: !IsTileType(tile, MP_TREES)) { truelight@0: return false; tron@2986: } truelight@0: smatz@9310: PlayerID old = _current_player; truelight@0: _current_player = OWNER_NONE; smatz@9310: CommandCost r = DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); truelight@0: _current_player = old; truelight@0: tron@2646: if (CmdFailed(r)) return false; truelight@0: truelight@6583: MakeStatue(tile, _current_player, town_id); tron@3310: MarkTileDirtyByTile(tile); truelight@0: truelight@0: return true; truelight@0: } truelight@0: belugas@5118: /** belugas@5118: * Search callback function for TownActionBuildStatue belugas@6980: * @param tile on which to perform the search frosch@11075: * @param user_data The town_id for which we want a statue belugas@6527: * @return the result of the test belugas@5118: */ frosch@11075: static bool SearchTileForStatue(TileIndex tile, void *user_data) belugas@5118: { frosch@11075: TownID *town_id = (TownID *)user_data; frosch@11075: 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: */ smatz@9310: static void TownActionBuildStatue(Town *t) truelight@0: { tron@1977: TileIndex tile = t->xy; truelight@0: frosch@11075: if (CircularTileSearch(&tile, 9, SearchTileForStatue, &t->index)) { skidd13@8427: SetBit(t->statues, _current_player); // Once found and built, "inform" the Town smatz@9310: } truelight@0: } truelight@0: smatz@9310: 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 */ skidd13@8427: SetBit(t->flags12, TOWN_IS_FUNDED); belugas@6918: /* And grow for 3 months */ truelight@0: t->fund_buildings_months = 3; truelight@0: } truelight@0: smatz@9310: static void TownActionBuyRights(Town *t) truelight@0: { rubidium@8153: /* Check if it's allowed to by the rights */ rubidium@10775: if (!_settings_game.economy.exclusive_rights) return; rubidium@8153: 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: smatz@9310: static void TownActionBribe(Town *t) truelight@0: { smatz@9310: if (Chance16(1, 14)) { 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 */ smatz@9310: Station *st; truelight@0: FOR_ALL_STATIONS(st) { truelight@0: if (st->town == t && st->owner == _current_player) { peter1138@6676: for (CargoID i = 0; i < NUM_CARGO; i++) st->goods[i].rating = 0; truelight@0: } truelight@0: } truelight@0: 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; smatz@10811: InvalidateWindow(WC_TOWN_AUTHORITY, t->index); tron@2549: } truelight@0: } else { celestar@1005: ChangeTownRating(t, RATING_BRIBE_UP_STEP, RATING_BRIBE_MAXIMUM); truelight@0: } truelight@0: } truelight@0: smatz@9310: typedef void TownActionProc(Town *t); smatz@9310: 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: */ rubidium@7439: CommandCost CmdDoTownAction(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) truelight@0: { truelight@4352: if (!IsValidTownID(p1) || p2 > lengthof(_town_action_proc)) return CMD_ERROR; Darkvater@1793: smatz@9310: Town *t = GetTown(p1); Darkvater@1793: skidd13@8424: if (!HasBit(GetMaskOfTownActions(NULL, _current_player, t), p2)) return CMD_ERROR; truelight@0: rubidium@8726: CommandCost cost(EXPENSES_OTHER, (_price.build_industry >> 8) * _town_action_costs[p2]); truelight@0: truelight@0: if (flags & DC_EXEC) { tron@3877: _town_action_proc[p2](t); truelight@0: InvalidateWindow(WC_TOWN_AUTHORITY, p1); truelight@0: } truelight@0: truelight@0: return cost; truelight@0: } truelight@0: truelight@0: static void UpdateTownGrowRate(Town *t) truelight@0: { smatz@9310: /* Increase player ratings if they're low */ smatz@9310: const Player *p; truelight@0: FOR_ALL_PLAYERS(p) { smatz@10387: if (p->is_active && t->ratings[p->index] < RATING_GROWTH_MAXIMUM) { smatz@9310: t->ratings[p->index] = min((int)RATING_GROWTH_MAXIMUM, t->ratings[p->index] + RATING_GROWTH_UP_STEP); truelight@0: } truelight@0: } truelight@0: smatz@9310: int n = 0; smatz@9310: smatz@9312: const Station *st; truelight@0: FOR_ALL_STATIONS(st) { rubidium@10195: if (DistanceSquare(st->xy, t->xy) <= t->squared_town_zone_radius[0]) { truelight@0: if (st->time_since_load <= 20 || st->time_since_unload <= 20) { truelight@0: n++; rubidium@11161: if (IsValidPlayerID(st->owner)) { smatz@9311: int new_rating = t->ratings[st->owner] + RATING_STATION_UP_STEP; smatz@9311: t->ratings[st->owner] = min(new_rating, INT16_MAX); // do not let it overflow smatz@9310: } truelight@0: } else { rubidium@11161: if (IsValidPlayerID(st->owner)) { smatz@9311: int new_rating = t->ratings[st->owner] + RATING_STATION_DOWN_STEP; smatz@9311: t->ratings[st->owner] = max(new_rating, INT16_MIN); smatz@9310: } truelight@0: } truelight@0: } truelight@0: } truelight@0: smatz@9311: /* clamp all ratings to valid values */ smatz@9311: for (uint i = 0; i < MAX_PLAYERS; i++) { smatz@9311: t->ratings[i] = Clamp(t->ratings[i], RATING_MINIMUM, RATING_MAXIMUM); smatz@9311: } smatz@9311: smatz@10811: InvalidateWindow(WC_TOWN_AUTHORITY, t->index); smatz@10811: skidd13@8425: ClrBit(t->flags12, TOWN_IS_FUNDED); rubidium@10775: if (_settings_game.economy.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] = { rubidium@8137: { 120, 120, 120, 100, 80, 60 }, // Fund new buildings has been activated rubidium@8137: { 320, 420, 300, 220, 160, 100 } // Normal values maedhros@6950: }; truelight@0: smatz@9310: uint16 m; smatz@9310: 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)]; skidd13@8463: if (n == 0 && !Chance16(1, 12)) return; truelight@0: } truelight@0: rubidium@10775: if (_settings_game.game_creation.landscape == LT_ARCTIC) { maedhros@6669: if (TilePixelHeight(t->xy) >= GetSnowLine() && t->act_food == 0 && t->population > 90) truelight@0: return; rubidium@10775: } else if (_settings_game.game_creation.landscape == LT_TROPIC) { rubidium@10229: 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. */ rubidium@10775: uint growth_multiplier = _settings_game.economy.town_growth_rate != 0 ? _settings_game.economy.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: skidd13@8427: 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: { smatz@9310: 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: { rubidium@11161: if (!IsValidPlayerID(_current_player)) return true; truelight@0: rubidium@10775: Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority); tron@4000: if (t == NULL) return true; truelight@0: smatz@9310: if (t->ratings[_current_player] > RATING_VERYPOOR) return true; truelight@0: truelight@0: _error_message = STR_2009_LOCAL_AUTHORITY_REFUSES; tron@534: SetDParam(0, t->index); truelight@0: truelight@0: return false; truelight@0: } truelight@0: truelight@0: smatz@9310: Town *CalcClosestTownFromTile(TileIndex tile, uint threshold) truelight@0: { truelight@0: Town *t; smatz@9310: uint best = threshold; truelight@0: Town *best_town = NULL; truelight@193: truelight@0: FOR_ALL_TOWNS(t) { smatz@9310: uint dist = DistanceManhattan(tile, t->xy); truelight@4346: if (dist < best) { truelight@4346: best = dist; truelight@4346: best_town = t; truelight@0: } truelight@0: } truelight@0: truelight@0: return best_town; truelight@0: } truelight@0: tron@3983: tron@3983: Town *ClosestTownFromTile(TileIndex tile, uint threshold) tron@3983: { tron@3983: if (IsTileType(tile, MP_HOUSE) || ( frosch@10689: IsTileType(tile, MP_ROAD) && HasTileRoadType(tile, ROADTYPE_ROAD) && frosch@10689: IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN) tron@3983: )) { tron@3983: return GetTownByTile(tile); tron@3983: } else { tron@3983: return CalcClosestTownFromTile(tile, threshold); tron@3983: } tron@3983: } tron@3983: glx@8728: static bool _town_rating_test = false; rubidium@10318: std::map _town_test_ratings; glx@8728: glx@8728: void SetTownRatingTestMode(bool mode) glx@8728: { glx@8728: static int ref_count = 0; glx@8728: if (mode) { glx@8728: if (ref_count == 0) { smatz@10379: _town_test_ratings.clear(); glx@8728: } glx@8728: ref_count++; glx@8728: } else { glx@8728: assert(ref_count > 0); glx@8728: ref_count--; glx@8728: } glx@8728: _town_rating_test = !(ref_count == 0); glx@8728: } tron@3983: rubidium@10318: static int GetRating(const Town *t) rubidium@10318: { rubidium@10318: if (_town_rating_test) { rubidium@10318: std::map::iterator it = _town_test_ratings.find(t); rubidium@10318: if (it != _town_test_ratings.end()) { rubidium@10318: return (*it).second; rubidium@10318: } rubidium@10318: } rubidium@10318: return t->ratings[_current_player]; rubidium@10318: } rubidium@10318: truelight@0: void ChangeTownRating(Town *t, int add, int max) truelight@0: { belugas@6918: /* if magic_bulldozer cheat is active, town doesn't penaltize for removing stuff */ tron@2639: if (t == NULL || rubidium@11161: !IsValidPlayerID(_current_player) || tron@2639: (_cheats.magic_bulldozer.value && add < 0)) { truelight@0: return; tron@2639: } truelight@0: skidd13@8427: SetBit(t->have_ratings, _current_player); truelight@193: rubidium@10318: int rating = GetRating(t); 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: } glx@8728: if (_town_rating_test) { rubidium@10318: _town_test_ratings[t] = rating; glx@8728: } else { glx@8728: t->ratings[_current_player] = rating; smatz@10811: InvalidateWindow(WC_TOWN_AUTHORITY, t->index); glx@8728: } 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 */ rubidium@8137: { 0, 128, 384}, // Permissive rubidium@8137: { 48, 192, 480}, // Neutral rubidium@8137: { 96, 384, 768}, // Hostile truelight@0: }; truelight@0: tron@2958: bool CheckforTownRating(uint32 flags, Town *t, byte type) truelight@0: { belugas@6918: /* if magic_bulldozer cheat is active, town doesn't restrict your destructive actions */ rubidium@11161: if (t == NULL || !IsValidPlayerID(_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: */ rubidium@10775: int modemod = _default_rating_settings[_settings_game.difficulty.town_council_tolerance][type]; truelight@0: rubidium@10318: if (GetRating(t) < 16 + modemod && !(flags & DC_NO_TOWN_RATING)) { tron@534: SetDParam(0, t->index); truelight@0: _error_message = STR_2009_LOCAL_AUTHORITY_REFUSES; truelight@0: return false; truelight@0: } truelight@0: truelight@0: return true; truelight@0: } truelight@0: rubidium@6573: void TownsMonthlyLoop() truelight@0: { truelight@0: Town *t; truelight@0: truelight@4346: FOR_ALL_TOWNS(t) { tron@4000: if (t->road_build_months != 0) t->road_build_months--; truelight@0: dominik@121: if (t->exclusive_counter != 0) rubidium@5838: if (--t->exclusive_counter == 0) t->exclusivity = INVALID_PLAYER; dominik@121: truelight@0: UpdateTownGrowRate(t); truelight@0: UpdateTownAmounts(t); truelight@0: UpdateTownUnwanted(t); truelight@0: } truelight@0: } truelight@0: rubidium@6573: void InitializeTowns() truelight@0: { truelight@1260: /* Clean the town pool and create 1 block in it */ rubidium@7897: _Town_pool.CleanPool(); rubidium@7897: _Town_pool.AddBlockToPool(); truelight@0: truelight@0: memset(_subsidies, 0, sizeof(_subsidies)); smatz@9310: for (Subsidy *s = _subsidies; s != endof(_subsidies); s++) { tron@2469: s->cargo_type = CT_INVALID; smatz@9310: } truelight@0: truelight@0: _cur_town_ctr = 0; pasky@1529: _cur_town_iter = 0; truelight@1260: _total_towns = 0; truelight@0: } truelight@0: rubidium@7990: static CommandCost TerraformTile_Town(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new) rubidium@7990: { rubidium@8078: if (AutoslopeEnabled()) { rubidium@8078: HouseID house = GetHouseType(tile); frosch@10186: GetHouseNorthPart(house); // modifies house to the ID of the north tile smatz@9310: const HouseSpec *hs = GetHouseSpecs(house); rubidium@8078: rubidium@8078: /* Here we differ from TTDP by checking TILE_NOT_SLOPED */ rubidium@8078: if (((hs->building_flags & TILE_NOT_SLOPED) == 0) && !IsSteepSlope(tileh_new) && rubidium@8726: (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); rubidium@8078: } rubidium@8078: rubidium@7990: return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR); rubidium@7990: } rubidium@7990: 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@9342: GetProducedCargo_Town, /* get_produced_cargo_proc */ rubidium@4344: NULL, /* vehicle_enter_tile_proc */ rubidium@7831: GetFoundation_Town, /* get_foundation_proc */ rubidium@7990: TerraformTile_Town, /* terraform_tile_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: skidd13@9203: SLE_CONDNULL(2, 0, 2), ///< population, no longer in use skidd13@9203: SLE_CONDNULL(4, 3, 84), ///< population, no longer in use skidd13@9203: SLE_CONDNULL(2, 0, 91), ///< num_houses, no longer in use skidd13@9203: glx@7452: SLE_CONDVAR(Town, townnamegrfid, SLE_UINT32, 66, SL_MAX_VERSION), rubidium@4344: SLE_VAR(Town, townnametype, SLE_UINT16), rubidium@4344: SLE_VAR(Town, townnameparts, SLE_UINT32), peter1138@8754: SLE_CONDSTR(Town, name, SLE_STR, 0, 84, SL_MAX_VERSION), truelight@0: rubidium@4344: SLE_VAR(Town, flags12, SLE_UINT8), rubidium@4344: SLE_VAR(Town, statues, SLE_UINT8), truelight@0: skidd13@9203: SLE_CONDNULL(1, 0, 1), ///< sort_index, no longer in use truelight@0: rubidium@4344: SLE_VAR(Town, have_ratings, SLE_UINT8), rubidium@4344: SLE_ARR(Town, ratings, SLE_INT16, 8), belugas@6918: /* failed bribe attempts are stored since savegame format 4 */ skidd13@9203: 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: rubidium@8855: SLE_CONDVAR(Town, time_until_rebuild, SLE_UINT16, 54, SL_MAX_VERSION), rubidium@8855: SLE_CONDVAR(Town, grow_counter, SLE_UINT16, 54, SL_MAX_VERSION), rubidium@8855: 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@8855: SLE_CONDVAR(Town, exclusivity, SLE_UINT8, 2, SL_MAX_VERSION), rubidium@8855: SLE_CONDVAR(Town, exclusive_counter, SLE_UINT8, 2, SL_MAX_VERSION), 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[] = { belugas@7125: SLE_VAR(EntityIDMapping, grfid, SLE_UINT32), belugas@7125: SLE_VAR(EntityIDMapping, entity_id, SLE_UINT8), belugas@7125: SLE_VAR(EntityIDMapping, substitute_id, SLE_UINT8), maedhros@6658: SLE_END() maedhros@6658: }; maedhros@6658: maedhros@6658: static void Save_HOUSEIDS() maedhros@6658: { belugas@7125: uint j = _house_mngr.GetMaxMapping(); belugas@7125: belugas@7125: for (uint i = 0; i < j; i++) { maedhros@6658: SlSetArrayIndex(i); belugas@7125: SlObject(&_house_mngr.mapping_ID[i], _house_id_mapping_desc); maedhros@6658: } maedhros@6658: } maedhros@6658: maedhros@6658: static void Load_HOUSEIDS() maedhros@6658: { maedhros@6658: int index; maedhros@6658: belugas@7125: _house_mngr.ResetMapping(); belugas@7125: uint max_id = _house_mngr.GetMaxMapping(); maedhros@6658: maedhros@6658: while ((index = SlIterateArray()) != -1) { belugas@7125: if ((uint)index >= max_id) break; belugas@7125: SlObject(&_house_mngr.mapping_ID[index], _house_id_mapping_desc); maedhros@6658: } maedhros@6658: } maedhros@6658: rubidium@6573: static void Save_TOWN() truelight@0: { truelight@0: Town *t; truelight@0: truelight@919: FOR_ALL_TOWNS(t) { truelight@4346: SlSetArrayIndex(t->index); truelight@4346: SlObject(t, _town_desc); truelight@0: } truelight@0: } truelight@0: rubidium@6573: static void Load_TOWN() truelight@0: { truelight@0: int index; truelight@919: truelight@1261: _total_towns = 0; truelight@1261: truelight@1260: while ((index = SlIterateArray()) != -1) { rubidium@7882: Town *t = new (index) Town(); truelight@0: SlObject(t, _town_desc); truelight@1260: rubidium@5298: _total_towns++; truelight@0: } truelight@1260: truelight@1260: /* This is to ensure all pointers are within the limits of truelight@1260: * the size of the TownPool */ matthijs@5247: if (_cur_town_ctr > GetMaxTownIndex()) truelight@1260: _cur_town_ctr = 0; truelight@0: } truelight@0: rubidium@6573: void AfterLoadTown() truelight@0: { skidd13@9292: Town *t; skidd13@9292: FOR_ALL_TOWNS(t) t->InitializeLayout(); truelight@0: } truelight@0: rubidium@5838: extern const ChunkHandler _town_chunk_handlers[] = { maedhros@6658: { 'HIDS', Save_HOUSEIDS, Load_HOUSEIDS, CH_ARRAY }, maedhros@6658: { 'CITY', Save_TOWN, Load_TOWN, CH_ARRAY | CH_LAST}, truelight@0: }; maedhros@6658: maedhros@6658: void ResetHouses() maedhros@6658: { maedhros@6658: memset(&_house_specs, 0, sizeof(_house_specs)); maedhros@6658: memcpy(&_house_specs, &_original_house_specs, sizeof(_original_house_specs)); belugas@8162: belugas@8162: /* Reset any overrides that have been set. */ belugas@8162: _house_mngr.ResetOverride(); maedhros@6658: }