00001
00002
00005 #include "stdafx.h"
00006 #include "openttd.h"
00007 #include "road_type.h"
00008 #include "road_internal.h"
00009 #include "road_cmd.h"
00010 #include "landscape.h"
00011 #include "town_map.h"
00012 #include "viewport_func.h"
00013 #include "command_func.h"
00014 #include "industry.h"
00015 #include "station_base.h"
00016 #include "company_base.h"
00017 #include "news_func.h"
00018 #include "gui.h"
00019 #include "unmovable_map.h"
00020 #include "water_map.h"
00021 #include "variables.h"
00022 #include "slope_func.h"
00023 #include "genworld.h"
00024 #include "newgrf.h"
00025 #include "newgrf_house.h"
00026 #include "newgrf_commons.h"
00027 #include "newgrf_townname.h"
00028 #include "newgrf_text.h"
00029 #include "autoslope.h"
00030 #include "waypoint.h"
00031 #include "transparency.h"
00032 #include "tunnelbridge_map.h"
00033 #include "strings_func.h"
00034 #include "window_func.h"
00035 #include "string_func.h"
00036 #include "newgrf_cargo.h"
00037 #include "oldpool_func.h"
00038 #include "economy_func.h"
00039 #include "station_func.h"
00040 #include "station_map.h"
00041 #include "cheat_type.h"
00042 #include "functions.h"
00043 #include "animated_tile_func.h"
00044 #include "date_func.h"
00045 #include "core/smallmap_type.hpp"
00046
00047 #include "table/strings.h"
00048 #include "table/town_land.h"
00049
00050 uint _total_towns;
00051 HouseSpec _house_specs[HOUSE_MAX];
00052
00053 Town *_cleared_town;
00054 int _cleared_town_rating;
00055
00056 uint32 _cur_town_ctr;
00057 uint32 _cur_town_iter;
00058
00059
00060 DEFINE_OLD_POOL_GENERIC(Town, Town)
00061
00062 Town::Town(TileIndex tile)
00063 {
00064 if (tile != INVALID_TILE) _total_towns++;
00065 this->xy = tile;
00066 }
00067
00068 Town::~Town()
00069 {
00070 free(this->name);
00071
00072 if (CleaningPool()) return;
00073
00074 Industry *i;
00075
00076
00077
00078 DeleteWindowById(WC_TOWN_VIEW, this->index);
00079 InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 0);
00080 _total_towns--;
00081
00082
00083 FOR_ALL_INDUSTRIES(i) if (i->town == this) delete i;
00084
00085
00086 for (TileIndex tile = 0; tile < MapSize(); ++tile) {
00087 switch (GetTileType(tile)) {
00088 case MP_HOUSE:
00089 if (GetTownByTile(tile) == this) DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
00090 break;
00091
00092 case MP_ROAD:
00093
00094 if (HasTownOwnedRoad(tile) && GetTownIndex(tile) == this->index) {
00095 DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
00096 }
00097 break;
00098
00099 case MP_TUNNELBRIDGE:
00100 if (IsTileOwner(tile, OWNER_TOWN) &&
00101 ClosestTownFromTile(tile, UINT_MAX) == this)
00102 DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
00103 break;
00104
00105 default:
00106 break;
00107 }
00108 }
00109
00110 DeleteSubsidyWithTown(this->index);
00111
00112 MarkWholeScreenDirty();
00113
00114 this->xy = INVALID_TILE;
00115
00116 UpdateNearestTownForRoadTiles(false);
00117 }
00118
00122 void Town::InitializeLayout(TownLayout layout)
00123 {
00124 if (layout != TL_RANDOM) {
00125 this->layout = layout;
00126 return;
00127 }
00128
00129 this->layout = TileHash(TileX(this->xy), TileY(this->xy)) % (NUM_TLS - 1);
00130 }
00131
00132 Money HouseSpec::GetRemovalCost() const
00133 {
00134 return (_price.remove_house * this->removal_cost) >> 8;
00135 }
00136
00137
00138 static int _grow_town_result;
00139
00140
00141 enum TownGrowthResult {
00142 GROWTH_SUCCEED = -1,
00143 GROWTH_SEARCH_STOPPED = 0
00144
00145 };
00146
00147 static bool BuildTownHouse(Town *t, TileIndex tile);
00148
00149 static void TownDrawHouseLift(const TileInfo *ti)
00150 {
00151 AddChildSpriteScreen(SPR_LIFT, PAL_NONE, 14, 60 - GetLiftPosition(ti->tile));
00152 }
00153
00154 typedef void TownDrawTileProc(const TileInfo *ti);
00155 static TownDrawTileProc * const _town_draw_tile_procs[1] = {
00156 TownDrawHouseLift
00157 };
00158
00164 static inline DiagDirection RandomDiagDir()
00165 {
00166 return (DiagDirection)(3 & Random());
00167 }
00168
00174 static void DrawTile_Town(TileInfo *ti)
00175 {
00176 HouseID house_id = GetHouseType(ti->tile);
00177
00178 if (house_id >= NEW_HOUSE_OFFSET) {
00179
00180
00181
00182 if (GetHouseSpecs(house_id)->spritegroup != NULL) {
00183 DrawNewHouseTile(ti, house_id);
00184 return;
00185 } else {
00186 house_id = GetHouseSpecs(house_id)->substitute_id;
00187 }
00188 }
00189
00190
00191 const DrawBuildingsTileStruct *dcts = &_town_draw_tile_data[house_id << 4 | TileHash2Bit(ti->x, ti->y) << 2 | GetHouseBuildingStage(ti->tile)];
00192
00193 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
00194
00195 DrawGroundSprite(dcts->ground.sprite, dcts->ground.pal);
00196
00197
00198 if (IsInvisibilitySet(TO_HOUSES)) return;
00199
00200
00201 SpriteID image = dcts->building.sprite;
00202 if (image != 0) {
00203 AddSortableSpriteToDraw(image, dcts->building.pal,
00204 ti->x + dcts->subtile_x,
00205 ti->y + dcts->subtile_y,
00206 dcts->width,
00207 dcts->height,
00208 dcts->dz,
00209 ti->z,
00210 IsTransparencySet(TO_HOUSES)
00211 );
00212
00213 if (IsTransparencySet(TO_HOUSES)) return;
00214 }
00215
00216 {
00217 int proc = dcts->draw_proc - 1;
00218
00219 if (proc >= 0) _town_draw_tile_procs[proc](ti);
00220 }
00221 }
00222
00223 static uint GetSlopeZ_Town(TileIndex tile, uint x, uint y)
00224 {
00225 return GetTileMaxZ(tile);
00226 }
00227
00229 static Foundation GetFoundation_Town(TileIndex tile, Slope tileh)
00230 {
00231 return FlatteningFoundation(tileh);
00232 }
00233
00240 static void AnimateTile_Town(TileIndex tile)
00241 {
00242 if (GetHouseType(tile) >= NEW_HOUSE_OFFSET) {
00243 AnimateNewHouseTile(tile);
00244 return;
00245 }
00246
00247 if (_tick_counter & 3) return;
00248
00249
00250
00251
00252
00253 if (!(GetHouseSpecs(GetHouseType(tile))->building_flags & BUILDING_IS_ANIMATED)) {
00254 DeleteAnimatedTile(tile);
00255 return;
00256 }
00257
00258 if (!LiftHasDestination(tile)) {
00259 uint i;
00260
00261
00262
00263
00264
00265 do {
00266 i = RandomRange(7);
00267 } while (i == 1 || i * 6 == GetLiftPosition(tile));
00268
00269 SetLiftDestination(tile, i);
00270 }
00271
00272 int pos = GetLiftPosition(tile);
00273 int dest = GetLiftDestination(tile) * 6;
00274 pos += (pos < dest) ? 1 : -1;
00275 SetLiftPosition(tile, pos);
00276
00277 if (pos == dest) {
00278 HaltLift(tile);
00279 DeleteAnimatedTile(tile);
00280 }
00281
00282 MarkTileDirtyByTile(tile);
00283 }
00284
00291 static bool IsCloseToTown(TileIndex tile, uint dist)
00292 {
00293 const Town *t;
00294
00295 FOR_ALL_TOWNS(t) {
00296 if (DistanceManhattan(tile, t->xy) < dist) return true;
00297 }
00298 return false;
00299 }
00300
00309 static void MarkTownSignDirty(Town *t)
00310 {
00311 MarkAllViewportsDirty(
00312 t->sign.left - 6,
00313 t->sign.top - 3,
00314 t->sign.left + t->sign.width_1 * 4 + 12,
00315 t->sign.top + 45
00316 );
00317 }
00318
00324 void UpdateTownVirtCoord(Town *t)
00325 {
00326 MarkTownSignDirty(t);
00327 Point pt = RemapCoords2(TileX(t->xy) * TILE_SIZE, TileY(t->xy) * TILE_SIZE);
00328 SetDParam(0, t->index);
00329 SetDParam(1, t->population);
00330 UpdateViewportSignPos(&t->sign, pt.x, pt.y - 24,
00331 _settings_client.gui.population_in_label ? STR_TOWN_LABEL_POP : STR_TOWN_LABEL);
00332 MarkTownSignDirty(t);
00333 }
00334
00336 void UpdateAllTownVirtCoords()
00337 {
00338 Town *t;
00339 FOR_ALL_TOWNS(t) {
00340 UpdateTownVirtCoord(t);
00341 }
00342 }
00343
00349 static void ChangePopulation(Town *t, int mod)
00350 {
00351 t->population += mod;
00352 InvalidateWindow(WC_TOWN_VIEW, t->index);
00353 UpdateTownVirtCoord(t);
00354
00355 InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 1);
00356 }
00357
00363 uint32 GetWorldPopulation()
00364 {
00365 uint32 pop = 0;
00366 const Town *t;
00367
00368 FOR_ALL_TOWNS(t) pop += t->population;
00369 return pop;
00370 }
00371
00376 static void MakeSingleHouseBigger(TileIndex tile)
00377 {
00378 assert(IsTileType(tile, MP_HOUSE));
00379
00380
00381 if (LiftHasDestination(tile)) return;
00382
00383
00384 IncHouseConstructionTick(tile);
00385 if (GetHouseConstructionTick(tile) != 0) return;
00386
00387 const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
00388
00389
00390 if (HasBit(hs->callback_mask, CBM_HOUSE_CONSTRUCTION_STATE_CHANGE)) {
00391 uint16 callback_res = GetHouseCallback(CBID_HOUSE_CONSTRUCTION_STATE_CHANGE, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
00392 if (callback_res != CALLBACK_FAILED) ChangeHouseAnimationFrame(hs->grffile, tile, callback_res);
00393 }
00394
00395 if (IsHouseCompleted(tile)) {
00396
00397
00398 ChangePopulation(GetTownByTile(tile), hs->population);
00399 ResetHouseAge(tile);
00400 }
00401 MarkTileDirtyByTile(tile);
00402 }
00403
00407 static void MakeTownHouseBigger(TileIndex tile)
00408 {
00409 uint flags = GetHouseSpecs(GetHouseType(tile))->building_flags;
00410 if (flags & BUILDING_HAS_1_TILE) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 0));
00411 if (flags & BUILDING_2_TILES_Y) MakeSingleHouseBigger(TILE_ADDXY(tile, 0, 1));
00412 if (flags & BUILDING_2_TILES_X) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 0));
00413 if (flags & BUILDING_HAS_4_TILES) MakeSingleHouseBigger(TILE_ADDXY(tile, 1, 1));
00414 }
00415
00422 static void TileLoop_Town(TileIndex tile)
00423 {
00424 HouseID house_id = GetHouseType(tile);
00425
00426
00427
00428 if (house_id >= NEW_HOUSE_OFFSET && !NewHouseTileLoop(tile)) return;
00429
00430 if (!IsHouseCompleted(tile)) {
00431
00432 MakeTownHouseBigger(tile);
00433 return;
00434 }
00435
00436 const HouseSpec *hs = GetHouseSpecs(house_id);
00437
00438
00439 if ((hs->building_flags & BUILDING_IS_ANIMATED) &&
00440 house_id < NEW_HOUSE_OFFSET &&
00441 !LiftHasDestination(tile) &&
00442 Chance16(1, 2)) {
00443 AddAnimatedTile(tile);
00444 }
00445
00446 Town *t = GetTownByTile(tile);
00447 uint32 r = Random();
00448
00449 if (HasBit(hs->callback_mask, CBM_HOUSE_PRODUCE_CARGO)) {
00450 for (uint i = 0; i < 256; i++) {
00451 uint16 callback = GetHouseCallback(CBID_HOUSE_PRODUCE_CARGO, i, r, house_id, t, tile);
00452
00453 if (callback == CALLBACK_FAILED || callback == CALLBACK_HOUSEPRODCARGO_END) break;
00454
00455 CargoID cargo = GetCargoTranslation(GB(callback, 8, 7), hs->grffile);
00456 if (cargo == CT_INVALID) continue;
00457
00458 uint amt = GB(callback, 0, 8);
00459 if (amt == 0) continue;
00460
00461 uint moved = MoveGoodsToStation(tile, 1, 1, cargo, amt);
00462
00463 const CargoSpec *cs = GetCargo(cargo);
00464 switch (cs->town_effect) {
00465 case TE_PASSENGERS:
00466 t->new_max_pass += amt;
00467 t->new_act_pass += moved;
00468 break;
00469
00470 case TE_MAIL:
00471 t->new_max_mail += amt;
00472 t->new_act_mail += moved;
00473 break;
00474
00475 default:
00476 break;
00477 }
00478 }
00479 } else {
00480 if (GB(r, 0, 8) < hs->population) {
00481 uint amt = GB(r, 0, 8) / 8 + 1;
00482
00483 if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
00484 t->new_max_pass += amt;
00485 t->new_act_pass += MoveGoodsToStation(tile, 1, 1, CT_PASSENGERS, amt);
00486 }
00487
00488 if (GB(r, 8, 8) < hs->mail_generation) {
00489 uint amt = GB(r, 8, 8) / 8 + 1;
00490
00491 if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
00492 t->new_max_mail += amt;
00493 t->new_act_mail += MoveGoodsToStation(tile, 1, 1, CT_MAIL, amt);
00494 }
00495 }
00496
00497 _current_company = OWNER_TOWN;
00498
00499 if (hs->building_flags & BUILDING_HAS_1_TILE &&
00500 HasBit(t->flags12, TOWN_IS_FUNDED) &&
00501 CanDeleteHouse(tile) &&
00502 GetHouseAge(tile) >= hs->minimum_life &&
00503 --t->time_until_rebuild == 0) {
00504 t->time_until_rebuild = GB(r, 16, 8) + 192;
00505
00506 ClearTownHouse(t, tile);
00507
00508
00509 if (GB(r, 24, 8) >= 12) BuildTownHouse(t, tile);
00510 }
00511
00512 _current_company = OWNER_NONE;
00513 }
00514
00515 static CommandCost ClearTile_Town(TileIndex tile, DoCommandFlag flags)
00516 {
00517 if (flags & DC_AUTO) return_cmd_error(STR_2004_BUILDING_MUST_BE_DEMOLISHED);
00518 if (!CanDeleteHouse(tile)) return CMD_ERROR;
00519
00520 const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
00521
00522 CommandCost cost(EXPENSES_CONSTRUCTION);
00523 cost.AddCost(hs->GetRemovalCost());
00524
00525 int rating = hs->remove_rating_decrease;
00526 _cleared_town_rating += rating;
00527 Town *t = _cleared_town = GetTownByTile(tile);
00528
00529 if (IsValidCompanyID(_current_company)) {
00530 if (rating > t->ratings[_current_company] && !(flags & DC_NO_TEST_TOWN_RATING) && !_cheats.magic_bulldozer.value) {
00531 SetDParam(0, t->index);
00532 return_cmd_error(STR_2009_LOCAL_AUTHORITY_REFUSES);
00533 }
00534 }
00535
00536 ChangeTownRating(t, -rating, RATING_HOUSE_MINIMUM, flags);
00537 if (flags & DC_EXEC) {
00538 ClearTownHouse(t, tile);
00539 }
00540
00541 return cost;
00542 }
00543
00544 static void GetProducedCargo_Town(TileIndex tile, CargoID *b)
00545 {
00546 HouseID house_id = GetHouseType(tile);
00547 const HouseSpec *hs = GetHouseSpecs(house_id);
00548 Town *t = GetTownByTile(tile);
00549
00550 if (HasBit(hs->callback_mask, CBM_HOUSE_PRODUCE_CARGO)) {
00551 for (uint i = 0; i < 256; i++) {
00552 uint16 callback = GetHouseCallback(CBID_HOUSE_PRODUCE_CARGO, i, 0, house_id, t, tile);
00553
00554 if (callback == CALLBACK_FAILED || callback == CALLBACK_HOUSEPRODCARGO_END) break;
00555
00556 CargoID cargo = GetCargoTranslation(GB(callback, 8, 7), hs->grffile);
00557
00558 if (cargo == CT_INVALID) continue;
00559 *(b++) = cargo;
00560 }
00561 } else {
00562 if (hs->population > 0) {
00563 *(b++) = CT_PASSENGERS;
00564 }
00565 if (hs->mail_generation > 0) {
00566 *(b++) = CT_MAIL;
00567 }
00568 }
00569 }
00570
00571 static void GetAcceptedCargo_Town(TileIndex tile, AcceptedCargo ac)
00572 {
00573 const HouseSpec *hs = GetHouseSpecs(GetHouseType(tile));
00574 CargoID accepts[3];
00575
00576
00577 for (uint8 i = 0; i < lengthof(accepts); i++) {
00578 accepts[i] = hs->accepts_cargo[i];
00579 }
00580
00581
00582 if (HasBit(hs->callback_mask, CBM_HOUSE_ACCEPT_CARGO)) {
00583 uint16 callback = GetHouseCallback(CBID_HOUSE_ACCEPT_CARGO, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
00584 if (callback != CALLBACK_FAILED) {
00585
00586 accepts[0] = GetCargoTranslation(GB(callback, 0, 5), hs->grffile);
00587 accepts[1] = GetCargoTranslation(GB(callback, 5, 5), hs->grffile);
00588 accepts[2] = GetCargoTranslation(GB(callback, 10, 5), hs->grffile);
00589 }
00590 }
00591
00592
00593 if (HasBit(hs->callback_mask, CBM_HOUSE_CARGO_ACCEPTANCE)) {
00594 uint16 callback = GetHouseCallback(CBID_HOUSE_CARGO_ACCEPTANCE, 0, 0, GetHouseType(tile), GetTownByTile(tile), tile);
00595 if (callback != CALLBACK_FAILED) {
00596 if (accepts[0] != CT_INVALID) ac[accepts[0]] = GB(callback, 0, 4);
00597 if (accepts[1] != CT_INVALID) ac[accepts[1]] = GB(callback, 4, 4);
00598 if (_settings_game.game_creation.landscape != LT_TEMPERATE && HasBit(callback, 12)) {
00599
00600 ac[CT_FOOD] = GB(callback, 8, 4);
00601 } else {
00602 if (accepts[2] != CT_INVALID) ac[accepts[2]] = GB(callback, 8, 4);
00603 }
00604 return;
00605 }
00606 }
00607
00608
00609 for (uint8 i = 0; i < lengthof(accepts); i++) {
00610 if (accepts[i] != CT_INVALID) ac[accepts[i]] = hs->cargo_acceptance[i];
00611 }
00612 }
00613
00614 static void GetTileDesc_Town(TileIndex tile, TileDesc *td)
00615 {
00616 const HouseID house = GetHouseType(tile);
00617 const HouseSpec *hs = GetHouseSpecs(house);
00618 bool house_completed = IsHouseCompleted(tile);
00619
00620 td->str = hs->building_name;
00621
00622 uint16 callback_res = GetHouseCallback(CBID_HOUSE_CUSTOM_NAME, house_completed ? 1 : 0, 0, house, GetTownByTile(tile), tile);
00623 if (callback_res != CALLBACK_FAILED) {
00624 StringID new_name = GetGRFStringID(hs->grffile->grfid, 0xD000 + callback_res);
00625 if (new_name != STR_NULL && new_name != STR_UNDEFINED) {
00626 td->str = new_name;
00627 }
00628 }
00629
00630 if (!house_completed) {
00631 SetDParamX(td->dparam, 0, td->str);
00632 td->str = STR_2058_UNDER_CONSTRUCTION;
00633 }
00634
00635 if (hs->grffile != NULL) {
00636 const GRFConfig *gc = GetGRFConfig(hs->grffile->grfid);
00637 td->grf = gc->name;
00638 }
00639
00640 td->owner[0] = OWNER_TOWN;
00641 }
00642
00643 static TrackStatus GetTileTrackStatus_Town(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
00644 {
00645
00646 return 0;
00647 }
00648
00649 static void ChangeTileOwner_Town(TileIndex tile, Owner old_owner, Owner new_owner)
00650 {
00651
00652 }
00653
00654 static bool GrowTown(Town *t);
00655
00656 static void TownTickHandler(Town *t)
00657 {
00658 if (HasBit(t->flags12, TOWN_IS_FUNDED)) {
00659 int i = t->grow_counter - 1;
00660 if (i < 0) {
00661 if (GrowTown(t)) {
00662 i = t->growth_rate;
00663 } else {
00664 i = 0;
00665 }
00666 }
00667 t->grow_counter = i;
00668 }
00669
00670 UpdateTownRadius(t);
00671 }
00672
00673 void OnTick_Town()
00674 {
00675 if (_game_mode == GM_EDITOR) return;
00676
00677 Town *t;
00678 FOR_ALL_TOWNS(t) {
00679
00680 if ((_tick_counter + t->index) % TOWN_GROWTH_FREQUENCY == 0) {
00681 TownTickHandler(t);
00682 }
00683 }
00684 }
00685
00694 static RoadBits GetTownRoadBits(TileIndex tile)
00695 {
00696 if (IsRoadDepotTile(tile) || IsStandardRoadStopTile(tile)) return ROAD_NONE;
00697
00698 return GetAnyRoadBits(tile, ROADTYPE_ROAD, true);
00699 }
00700
00711 static bool IsNeighborRoadTile(TileIndex tile, const DiagDirection dir, uint dist_multi)
00712 {
00713 if (!IsValidTile(tile)) return false;
00714
00715
00716 const TileIndexDiff tid_lt[3] = {
00717 TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90RIGHT)),
00718 TileOffsByDiagDir(ChangeDiagDir(dir, DIAGDIRDIFF_90LEFT)),
00719 TileOffsByDiagDir(ReverseDiagDir(dir)),
00720 };
00721
00722 dist_multi = (dist_multi + 1) * 4;
00723 for (uint pos = 4; pos < dist_multi; pos++) {
00724
00725 TileIndexDiff cur = tid_lt[(pos & 1) ? 0 : 1] * (pos / 4);
00726
00727
00728 if (pos & 2) cur += tid_lt[2];
00729
00730
00731 if (IsValidTile(tile + cur) &&
00732 GetTownRoadBits(TILE_ADD(tile, cur)) & DiagDirToRoadBits((pos & 2) ? dir : ReverseDiagDir(dir))) return true;
00733 }
00734 return false;
00735 }
00736
00745 static bool IsRoadAllowedHere(Town *t, TileIndex tile, DiagDirection dir)
00746 {
00747 if (DistanceFromEdge(tile) == 0) return false;
00748
00749 Slope cur_slope, desired_slope;
00750
00751 for (;;) {
00752
00753 if (GetTownRoadBits(tile) == ROAD_NONE) {
00754
00755
00756
00757 if (CmdFailed(DoCommand(tile, ((dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? ROAD_X : ROAD_Y), 0, DC_AUTO, CMD_BUILD_ROAD)) &&
00758 CmdFailed(DoCommand(tile, 0, 0, DC_AUTO, CMD_LANDSCAPE_CLEAR)))
00759 return false;
00760 }
00761
00762 cur_slope = _settings_game.construction.build_on_slopes ? GetFoundationSlope(tile, NULL) : GetTileSlope(tile, NULL);
00763 bool ret = !IsNeighborRoadTile(tile, dir, t->layout == TL_ORIGINAL ? 1 : 2);
00764 if (cur_slope == SLOPE_FLAT) return ret;
00765
00766
00767
00768 desired_slope = (dir == DIAGDIR_NW || dir == DIAGDIR_SE) ? SLOPE_NW : SLOPE_NE;
00769 if (desired_slope != cur_slope && ComplementSlope(desired_slope) != cur_slope) {
00770 if (Chance16(1, 8)) {
00771 CommandCost res = CMD_ERROR;
00772 if (!_generating_world && Chance16(1, 10)) {
00773
00774 res = DoCommand(tile, Chance16(1, 16) ? cur_slope : cur_slope ^ SLOPE_ELEVATED, 0,
00775 DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
00776 }
00777 if (CmdFailed(res) && Chance16(1, 3)) {
00778
00779 return ret;
00780 }
00781 }
00782 return false;
00783 }
00784 return ret;
00785 }
00786 }
00787
00788 static bool TerraformTownTile(TileIndex tile, int edges, int dir)
00789 {
00790 assert(tile < MapSize());
00791
00792 CommandCost r = DoCommand(tile, edges, dir, DC_AUTO | DC_NO_WATER, CMD_TERRAFORM_LAND);
00793 if (CmdFailed(r) || r.GetCost() >= (_price.terraform + 2) * 8) return false;
00794 DoCommand(tile, edges, dir, DC_AUTO | DC_NO_WATER | DC_EXEC, CMD_TERRAFORM_LAND);
00795 return true;
00796 }
00797
00798 static void LevelTownLand(TileIndex tile)
00799 {
00800 assert(tile < MapSize());
00801
00802
00803 if (IsTileType(tile, MP_HOUSE)) return;
00804 Slope tileh = GetTileSlope(tile, NULL);
00805 if (tileh == SLOPE_FLAT) return;
00806
00807
00808 if (!TerraformTownTile(tile, ~tileh & SLOPE_ELEVATED, 1)) {
00809 TerraformTownTile(tile, tileh & SLOPE_ELEVATED, 0);
00810 }
00811 }
00812
00822 static RoadBits GetTownRoadGridElement(Town *t, TileIndex tile, DiagDirection dir)
00823 {
00824
00825 TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile);
00826 RoadBits rcmd = ROAD_NONE;
00827
00828 switch (t->layout) {
00829 default: NOT_REACHED();
00830
00831 case TL_2X2_GRID:
00832 if ((grid_pos.x % 3) == 0) rcmd |= ROAD_Y;
00833 if ((grid_pos.y % 3) == 0) rcmd |= ROAD_X;
00834 break;
00835
00836 case TL_3X3_GRID:
00837 if ((grid_pos.x % 4) == 0) rcmd |= ROAD_Y;
00838 if ((grid_pos.y % 4) == 0) rcmd |= ROAD_X;
00839 break;
00840 }
00841
00842
00843 if (rcmd != ROAD_ALL) return rcmd;
00844
00845 RoadBits rb_template;
00846
00847 switch (GetTileSlope(tile, NULL)) {
00848 default: rb_template = ROAD_ALL; break;
00849 case SLOPE_W: rb_template = ROAD_NW | ROAD_SW; break;
00850 case SLOPE_SW: rb_template = ROAD_Y | ROAD_SW; break;
00851 case SLOPE_S: rb_template = ROAD_SW | ROAD_SE; break;
00852 case SLOPE_SE: rb_template = ROAD_X | ROAD_SE; break;
00853 case SLOPE_E: rb_template = ROAD_SE | ROAD_NE; break;
00854 case SLOPE_NE: rb_template = ROAD_Y | ROAD_NE; break;
00855 case SLOPE_N: rb_template = ROAD_NE | ROAD_NW; break;
00856 case SLOPE_NW: rb_template = ROAD_X | ROAD_NW; break;
00857 case SLOPE_STEEP_W:
00858 case SLOPE_STEEP_S:
00859 case SLOPE_STEEP_E:
00860 case SLOPE_STEEP_N:
00861 rb_template = ROAD_NONE;
00862 break;
00863 }
00864
00865
00866 if (DiagDirToRoadBits(ReverseDiagDir(dir)) & rb_template) return rb_template;
00867
00868 return DiagDirToRoadBits(dir) | DiagDirToRoadBits(ReverseDiagDir(dir));
00869 }
00870
00881 static bool GrowTownWithExtraHouse(Town *t, TileIndex tile)
00882 {
00883
00884 if (DistanceFromEdge(tile) == 0) return false;
00885
00886 uint counter = 0;
00887
00888
00889 for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
00890
00891
00892
00893 switch (GetTileType(TileAddByDiagDir(tile, dir))) {
00894 case MP_HOUSE:
00895 case MP_VOID:
00896 counter++;
00897 break;
00898
00899 default:
00900 break;
00901 }
00902
00903
00904 if (counter >= 3) {
00905 if (BuildTownHouse(t, tile)) {
00906 _grow_town_result = GROWTH_SUCCEED;
00907 return true;
00908 }
00909 return false;
00910 }
00911 }
00912 return false;
00913 }
00914
00923 static bool GrowTownWithRoad(const Town *t, TileIndex tile, RoadBits rcmd)
00924 {
00925 if (CmdSucceeded(DoCommand(tile, rcmd, t->index, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_BUILD_ROAD))) {
00926 _grow_town_result = GROWTH_SUCCEED;
00927 return true;
00928 }
00929 return false;
00930 }
00931
00942 static bool GrowTownWithBridge(const Town *t, const TileIndex tile, const DiagDirection bridge_dir)
00943 {
00944 assert(bridge_dir < DIAGDIR_END);
00945
00946 const Slope slope = GetTileSlope(tile, NULL);
00947 if (slope == SLOPE_FLAT) return false;
00948
00949
00950
00951
00952 if (HASBITS(slope, InclinedSlope(bridge_dir))) return false;
00953
00954
00955 if (!(GetTownRoadBits(TileAddByDiagDir(tile, ReverseDiagDir(bridge_dir))) & DiagDirToRoadBits(bridge_dir))) return false;
00956
00957
00958 uint8 bridge_length = 0;
00959 TileIndex bridge_tile = tile;
00960
00961 const int delta = TileOffsByDiagDir(bridge_dir);
00962 do {
00963 if (bridge_length++ >= 11) {
00964
00965 return false;
00966 }
00967 bridge_tile += delta;
00968 } while (TileX(bridge_tile) != 0 && TileY(bridge_tile) != 0 && IsWaterTile(bridge_tile));
00969
00970
00971 if (bridge_length == 1) return false;
00972
00973 for (uint8 times = 0; times <= 22; times++) {
00974 byte bridge_type = RandomRange(MAX_BRIDGES - 1);
00975
00976
00977 if (CmdSucceeded(DoCommand(tile, bridge_tile, bridge_type | ROADTYPES_ROAD << 8 | TRANSPORT_ROAD << 15, DC_AUTO, CMD_BUILD_BRIDGE))) {
00978 DoCommand(tile, bridge_tile, bridge_type | ROADTYPES_ROAD << 8 | TRANSPORT_ROAD << 15, DC_EXEC | DC_AUTO, CMD_BUILD_BRIDGE);
00979 _grow_town_result = GROWTH_SUCCEED;
00980 return true;
00981 }
00982 }
00983
00984 return false;
00985 }
00986
01004 static void GrowTownInTile(TileIndex *tile_ptr, RoadBits cur_rb, DiagDirection target_dir, Town *t1)
01005 {
01006 RoadBits rcmd = ROAD_NONE;
01007 TileIndex tile = *tile_ptr;
01008
01009 assert(tile < MapSize());
01010
01011 if (cur_rb == ROAD_NONE) {
01012
01013
01014 _grow_town_result = GROWTH_SEARCH_STOPPED;
01015
01016 if (!_settings_game.economy.allow_town_roads && !_generating_world) return;
01017
01018
01019 if (!_settings_game.construction.build_on_slopes || Chance16(1, 6)) LevelTownLand(tile);
01020
01021
01022 switch (t1->layout) {
01023 default: NOT_REACHED();
01024
01025 case TL_3X3_GRID:
01026 case TL_2X2_GRID:
01027 rcmd = GetTownRoadGridElement(t1, tile, target_dir);
01028 if (rcmd == ROAD_NONE) return;
01029 break;
01030
01031 case TL_BETTER_ROADS:
01032 case TL_ORIGINAL:
01033 if (!IsRoadAllowedHere(t1, tile, target_dir)) return;
01034
01035 DiagDirection source_dir = ReverseDiagDir(target_dir);
01036
01037 if (Chance16(1, 4)) {
01038
01039 do target_dir = RandomDiagDir(); while (target_dir == source_dir);
01040 }
01041
01042 if (!IsRoadAllowedHere(t1, TileAddByDiagDir(tile, target_dir), target_dir)) {
01043
01044
01045 if (target_dir != ReverseDiagDir(source_dir)) return;
01046
01047
01048 if (!IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90RIGHT)), MP_HOUSE) &&
01049 !IsTileType(TileAddByDiagDir(tile, ChangeDiagDir(target_dir, DIAGDIRDIFF_90LEFT)), MP_HOUSE)) {
01050 return;
01051 }
01052
01053
01054
01055 }
01056
01057 rcmd = DiagDirToRoadBits(target_dir) | DiagDirToRoadBits(source_dir);
01058 break;
01059 }
01060
01061 } else if (target_dir < DIAGDIR_END && !(cur_rb & DiagDirToRoadBits(ReverseDiagDir(target_dir)))) {
01062
01063
01064
01065 _grow_town_result = GROWTH_SEARCH_STOPPED;
01066
01067 if (!_settings_game.economy.allow_town_roads && !_generating_world) return;
01068
01069 switch (t1->layout) {
01070 default: NOT_REACHED();
01071
01072 case TL_3X3_GRID:
01073 case TL_2X2_GRID:
01074 rcmd = GetTownRoadGridElement(t1, tile, target_dir);
01075 break;
01076
01077 case TL_BETTER_ROADS:
01078 case TL_ORIGINAL:
01079 rcmd = DiagDirToRoadBits(ReverseDiagDir(target_dir));
01080 break;
01081 }
01082 } else {
01083 bool allow_house = true;
01084
01085
01086 if (IsTileType(tile, MP_TUNNELBRIDGE)) {
01087 if (GetTunnelBridgeTransportType(tile) == TRANSPORT_ROAD) {
01088 *tile_ptr = GetOtherTunnelBridgeEnd(tile);
01089 }
01090 return;
01091 }
01092
01093
01094
01095 target_dir = RandomDiagDir();
01096 if (cur_rb & DiagDirToRoadBits(target_dir)) return;
01097
01098
01099 TileIndex house_tile = TileAddByDiagDir(tile, target_dir);
01100
01101
01102 if (IsWaterTile(house_tile)) return;
01103
01104 if (!IsValidTile(house_tile)) return;
01105
01106 if (_settings_game.economy.allow_town_roads || _generating_world) {
01107 switch (t1->layout) {
01108 default: NOT_REACHED();
01109
01110 case TL_3X3_GRID:
01111 GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir));
01112
01113
01114 case TL_2X2_GRID:
01115 rcmd = GetTownRoadGridElement(t1, house_tile, target_dir);
01116 allow_house = (rcmd == ROAD_NONE);
01117 break;
01118
01119 case TL_BETTER_ROADS:
01120 GrowTownWithExtraHouse(t1, TileAddByDiagDir(house_tile, target_dir));
01121
01122
01123 case TL_ORIGINAL:
01124
01125
01126 rcmd = DiagDirToRoadBits(target_dir);
01127 allow_house = (!IsRoadAllowedHere(t1, house_tile, target_dir) || Chance16(6, 10));
01128 break;
01129 }
01130 }
01131
01132 if (allow_house) {
01133
01134 if (!IsTileType(house_tile, MP_HOUSE)) {
01135
01136 if (Chance16(1, 6)) LevelTownLand(house_tile);
01137
01138
01139
01140 if (BuildTownHouse(t1, house_tile)) {
01141 _grow_town_result = GROWTH_SUCCEED;
01142 }
01143 }
01144 return;
01145 }
01146
01147 _grow_town_result = GROWTH_SEARCH_STOPPED;
01148 }
01149
01150
01151 if (IsWaterTile(tile)) return;
01152
01153
01154 rcmd = CleanUpRoadBits(tile, rcmd);
01155 if (rcmd == ROAD_NONE) return;
01156
01157
01158
01159
01160 if (GrowTownWithBridge(t1, tile, target_dir)) return;
01161
01162 GrowTownWithRoad(t1, tile, rcmd);
01163 }
01164
01170 static int GrowTownAtRoad(Town *t, TileIndex tile)
01171 {
01172
01173
01174
01175 DiagDirection target_dir = DIAGDIR_END;
01176
01177 assert(tile < MapSize());
01178
01179
01180
01181
01182 switch (t->layout) {
01183 case TL_BETTER_ROADS:
01184 _grow_town_result = 10 + t->num_houses * 2 / 9;
01185 break;
01186
01187 case TL_3X3_GRID:
01188 case TL_2X2_GRID:
01189 _grow_town_result = 10 + t->num_houses * 1 / 9;
01190 break;
01191
01192 default:
01193 _grow_town_result = 10 + t->num_houses * 4 / 9;
01194 break;
01195 }
01196
01197 do {
01198 RoadBits cur_rb = GetTownRoadBits(tile);
01199
01200
01201 GrowTownInTile(&tile, cur_rb, target_dir, t);
01202
01203
01204
01205 cur_rb &= ~DiagDirToRoadBits(ReverseDiagDir(target_dir));
01206 if (cur_rb == ROAD_NONE)
01207 return _grow_town_result;
01208
01209
01210
01211 do target_dir = RandomDiagDir(); while (!(cur_rb & DiagDirToRoadBits(target_dir)));
01212 tile = TileAddByDiagDir(tile, target_dir);
01213
01214 if (IsTileType(tile, MP_ROAD) && !IsRoadDepot(tile) && HasTileRoadType(tile, ROADTYPE_ROAD)) {
01215
01216 if (IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN) && GetTownByTile(tile) != t) {
01217 _grow_town_result = GROWTH_SUCCEED;
01218 } else if (IsRoadOwner(tile, ROADTYPE_ROAD, OWNER_NONE) && _game_mode == GM_EDITOR) {
01219
01220
01221 SetRoadOwner(tile, ROADTYPE_ROAD, OWNER_TOWN);
01222 SetTownIndex(tile, t->index);
01223 }
01224 }
01225
01226
01227 } while (--_grow_town_result >= 0);
01228
01229 return (_grow_town_result == -2);
01230 }
01231
01239 static RoadBits GenRandomRoadBits()
01240 {
01241 uint32 r = Random();
01242 uint a = GB(r, 0, 2);
01243 uint b = GB(r, 8, 2);
01244 if (a == b) b ^= 2;
01245 return (RoadBits)((ROAD_NW << a) + (ROAD_NW << b));
01246 }
01247
01252 static bool GrowTown(Town *t)
01253 {
01254 static const TileIndexDiffC _town_coord_mod[] = {
01255 {-1, 0},
01256 { 1, 1},
01257 { 1, -1},
01258 {-1, -1},
01259 {-1, 0},
01260 { 0, 2},
01261 { 2, 0},
01262 { 0, -2},
01263 {-1, -1},
01264 {-2, 2},
01265 { 2, 2},
01266 { 2, -2},
01267 { 0, 0}
01268 };
01269
01270
01271 CompanyID old_company = _current_company;
01272 _current_company = OWNER_TOWN;
01273
01274 TileIndex tile = t->xy;
01275
01276
01277 const TileIndexDiffC *ptr;
01278 for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) {
01279 if (GetTownRoadBits(tile) != ROAD_NONE) {
01280 int r = GrowTownAtRoad(t, tile);
01281 _current_company = old_company;
01282 return r != 0;
01283 }
01284 tile = TILE_ADD(tile, ToTileIndexDiff(*ptr));
01285 }
01286
01287
01288
01289 if (_settings_game.economy.allow_town_roads || _generating_world) {
01290 tile = t->xy;
01291 for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) {
01292
01293 if (!IsTileType(tile, MP_HOUSE) && GetTileSlope(tile, NULL) == SLOPE_FLAT) {
01294 if (CmdSucceeded(DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR))) {
01295 DoCommand(tile, GenRandomRoadBits(), t->index, DC_EXEC | DC_AUTO, CMD_BUILD_ROAD);
01296 _current_company = old_company;
01297 return true;
01298 }
01299 }
01300 tile = TILE_ADD(tile, ToTileIndexDiff(*ptr));
01301 }
01302 }
01303
01304 _current_company = old_company;
01305 return false;
01306 }
01307
01308 void UpdateTownRadius(Town *t)
01309 {
01310 static const uint32 _town_squared_town_zone_radius_data[23][5] = {
01311 { 4, 0, 0, 0, 0},
01312 { 16, 0, 0, 0, 0},
01313 { 25, 0, 0, 0, 0},
01314 { 36, 0, 0, 0, 0},
01315 { 49, 0, 4, 0, 0},
01316 { 64, 0, 4, 0, 0},
01317 { 64, 0, 9, 0, 1},
01318 { 64, 0, 9, 0, 4},
01319 { 64, 0, 16, 0, 4},
01320 { 81, 0, 16, 0, 4},
01321 { 81, 0, 16, 0, 4},
01322 { 81, 0, 25, 0, 9},
01323 { 81, 36, 25, 0, 9},
01324 { 81, 36, 25, 16, 9},
01325 { 81, 49, 0, 25, 9},
01326 { 81, 64, 0, 25, 9},
01327 { 81, 64, 0, 36, 9},
01328 { 81, 64, 0, 36, 16},
01329 {100, 81, 0, 49, 16},
01330 {100, 81, 0, 49, 25},
01331 {121, 81, 0, 49, 25},
01332 {121, 81, 0, 49, 25},
01333 {121, 81, 0, 49, 36},
01334 };
01335
01336 if (t->num_houses < 92) {
01337 memcpy(t->squared_town_zone_radius, _town_squared_town_zone_radius_data[t->num_houses / 4], sizeof(t->squared_town_zone_radius));
01338 } else {
01339 int mass = t->num_houses / 8;
01340
01341
01342
01343 t->squared_town_zone_radius[0] = mass * 15 - 40;
01344 t->squared_town_zone_radius[1] = mass * 9 - 15;
01345 t->squared_town_zone_radius[2] = 0;
01346 t->squared_town_zone_radius[3] = mass * 5 - 5;
01347 t->squared_town_zone_radius[4] = mass * 3 + 5;
01348 }
01349 }
01350
01351 extern int _nb_orig_names;
01352
01357 struct TownNameParams {
01358 uint32 grfid;
01359 uint16 townnametype;
01360 bool grf;
01361
01362 TownNameParams(byte town_name)
01363 {
01364 this->grf = town_name >= _nb_orig_names;
01365 this->grfid = this->grf ? GetGRFTownNameId(town_name - _nb_orig_names) : 0;
01366 this->townnametype = this->grf ? GetGRFTownNameType(town_name - _nb_orig_names) : SPECSTR_TOWNNAME_START + town_name;
01367 }
01368 };
01369
01376 static bool VerifyTownName(uint32 r, const TownNameParams *par)
01377 {
01378
01379
01380 char buf1[MAX_LENGTH_TOWN_NAME_BYTES + MAX_CHAR_LENGTH];
01381 char buf2[MAX_LENGTH_TOWN_NAME_BYTES + MAX_CHAR_LENGTH];
01382
01383 SetDParam(0, r);
01384 if (par->grf && par->grfid != 0) {
01385 GRFTownNameGenerate(buf1, par->grfid, par->townnametype, r, lastof(buf1));
01386 } else {
01387 GetString(buf1, par->townnametype, lastof(buf1));
01388 }
01389
01390
01391 if (strlen(buf1) >= MAX_LENGTH_TOWN_NAME_BYTES) return false;
01392
01393 const Town *t;
01394 FOR_ALL_TOWNS(t) {
01395
01396
01397 SetDParam(0, t->index);
01398 GetString(buf2, STR_TOWN, lastof(buf2));
01399 if (strcmp(buf1, buf2) == 0) return false;
01400 }
01401
01402 return true;
01403 }
01404
01410 bool GenerateTownName(uint32 *townnameparts)
01411 {
01412
01413
01414
01415
01416
01417 int tries = 1000;
01418 TownNameParams par(_settings_game.game_creation.town_name);
01419
01420 assert(townnameparts != NULL);
01421
01422 for (;;) {
01423 uint32 r = InteractiveRandom();
01424
01425 if (!VerifyTownName(r, &par)) {
01426 if (tries-- < 0) return false;
01427 continue;
01428 }
01429
01430 *townnameparts = r;
01431 return true;
01432 }
01433 }
01434
01435 void UpdateTownMaxPass(Town *t)
01436 {
01437 t->max_pass = t->population >> 3;
01438 t->max_mail = t->population >> 4;
01439 }
01440
01450 static void DoCreateTown(Town *t, TileIndex tile, uint32 townnameparts, TownSize size, bool city, TownLayout layout)
01451 {
01452 t->xy = tile;
01453 t->num_houses = 0;
01454 t->time_until_rebuild = 10;
01455 UpdateTownRadius(t);
01456 t->flags12 = 0;
01457 t->population = 0;
01458 t->grow_counter = 0;
01459 t->growth_rate = 250;
01460 t->new_max_pass = 0;
01461 t->new_max_mail = 0;
01462 t->new_act_pass = 0;
01463 t->new_act_mail = 0;
01464 t->max_pass = 0;
01465 t->max_mail = 0;
01466 t->act_pass = 0;
01467 t->act_mail = 0;
01468
01469 t->pct_pass_transported = 0;
01470 t->pct_mail_transported = 0;
01471 t->fund_buildings_months = 0;
01472 t->new_act_food = 0;
01473 t->new_act_water = 0;
01474 t->act_food = 0;
01475 t->act_water = 0;
01476
01477 for (uint i = 0; i != MAX_COMPANIES; i++) t->ratings[i] = RATING_INITIAL;
01478
01479 t->have_ratings = 0;
01480 t->exclusivity = INVALID_COMPANY;
01481 t->exclusive_counter = 0;
01482 t->statues = 0;
01483
01484 if (_settings_game.game_creation.town_name < _nb_orig_names) {
01485
01486 t->townnamegrfid = 0;
01487 t->townnametype = SPECSTR_TOWNNAME_START + _settings_game.game_creation.town_name;
01488 } else {
01489
01490 t->townnamegrfid = GetGRFTownNameId(_settings_game.game_creation.town_name - _nb_orig_names);
01491 t->townnametype = GetGRFTownNameType(_settings_game.game_creation.town_name - _nb_orig_names);
01492 }
01493 t->townnameparts = townnameparts;
01494
01495 UpdateTownVirtCoord(t);
01496 InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 0);
01497
01498 t->InitializeLayout(layout);
01499
01500 t->larger_town = city;
01501
01502 int x = (int)size * 16 + 3;
01503 if (size == TS_RANDOM) x = (Random() & 0xF) + 8;
01504 if (city) x *= _settings_game.economy.initial_city_size;
01505
01506 t->num_houses += x;
01507 UpdateTownRadius(t);
01508
01509 int i = x * 4;
01510 do {
01511 GrowTown(t);
01512 } while (--i);
01513
01514 t->num_houses -= x;
01515 UpdateTownRadius(t);
01516 UpdateTownMaxPass(t);
01517 UpdateAirportsNoise();
01518 }
01519
01530 CommandCost CmdBuildTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01531 {
01532
01533 if (_game_mode != GM_EDITOR) return CMD_ERROR;
01534
01535 TownSize size = (TownSize)GB(p1, 0, 2);
01536 bool city = HasBit(p1, 2);
01537 TownLayout layout = (TownLayout)GB(p1, 3, 3);
01538 TownNameParams par(_settings_game.game_creation.town_name);
01539 uint32 townnameparts = p2;
01540
01541 if (size > TS_RANDOM) return CMD_ERROR;
01542 if (layout > TL_RANDOM) return CMD_ERROR;
01543 if (!VerifyTownName(townnameparts, &par)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
01544
01545
01546 if (DistanceFromEdge(tile) < 12) {
01547 return_cmd_error(STR_0237_TOO_CLOSE_TO_EDGE_OF_MAP);
01548 }
01549
01550
01551 if (IsCloseToTown(tile, 20)) {
01552 return_cmd_error(STR_0238_TOO_CLOSE_TO_ANOTHER_TOWN);
01553 }
01554
01555
01556 if ((!IsTileType(tile, MP_CLEAR) && !IsTileType(tile, MP_TREES)) || GetTileSlope(tile, NULL) != SLOPE_FLAT) {
01557 return_cmd_error(STR_0239_SITE_UNSUITABLE);
01558 }
01559
01560
01561 if (!Town::CanAllocateItem()) return_cmd_error(STR_023A_TOO_MANY_TOWNS);
01562
01563
01564 if (flags & DC_EXEC) {
01565 Town *t = new Town(tile);
01566 _generating_world = true;
01567 UpdateNearestTownForRoadTiles(true);
01568 DoCreateTown(t, tile, townnameparts, size, city, layout);
01569 UpdateNearestTownForRoadTiles(false);
01570 _generating_world = false;
01571 }
01572 return CommandCost();
01573 }
01574
01575 Town *CreateRandomTown(uint attempts, TownSize size, bool city, TownLayout layout)
01576 {
01577 if (!Town::CanAllocateItem()) return NULL;
01578
01579 do {
01580
01581 TileIndex tile = RandomTile();
01582 switch (layout) {
01583 case TL_2X2_GRID:
01584 tile = TileXY(TileX(tile) - TileX(tile) % 3, TileY(tile) - TileY(tile) % 3);
01585 break;
01586 case TL_3X3_GRID:
01587 tile = TileXY(TileX(tile) & ~3, TileY(tile) & ~3);
01588 break;
01589 default: break;
01590 }
01591 if (DistanceFromEdge(tile) < 20) continue;
01592
01593
01594 if (!IsTileType(tile, MP_CLEAR) || GetTileSlope(tile, NULL) != SLOPE_FLAT) continue;
01595
01596
01597 if (IsCloseToTown(tile, 20)) continue;
01598
01599 uint32 townnameparts;
01600
01601
01602 if (!GenerateTownName(&townnameparts)) break;
01603
01604
01605 Town *t = new Town(tile);
01606
01607 DoCreateTown(t, tile, townnameparts, size, city, layout);
01608 return t;
01609 } while (--attempts != 0);
01610
01611 return NULL;
01612 }
01613
01614 static const byte _num_initial_towns[4] = {5, 11, 23, 46};
01615
01622 bool GenerateTowns(TownLayout layout)
01623 {
01624 uint num = 0;
01625 uint difficulty = _settings_game.difficulty.number_towns;
01626 uint n = (difficulty == (uint)CUSTOM_TOWN_NUMBER_DIFFICULTY) ? _settings_game.game_creation.custom_town_number : ScaleByMapSize(_num_initial_towns[difficulty] + (Random() & 7));
01627
01628 SetGeneratingWorldProgress(GWP_TOWN, n);
01629
01630
01631
01632
01633 do {
01634 bool city = (_settings_game.economy.larger_towns != 0 && Chance16(1, _settings_game.economy.larger_towns));
01635 IncreaseGeneratingWorldProgress(GWP_TOWN);
01636
01637 if (CreateRandomTown(20, TS_RANDOM, city, layout) != NULL) num++;
01638 } while (--n);
01639
01640
01641
01642 if (num == 0 && CreateRandomTown(10000, TS_RANDOM, _settings_game.economy.larger_towns != 0, layout) == NULL) {
01643 if (GetNumTowns() == 0) {
01644 if (_game_mode != GM_EDITOR) {
01645 extern StringID _switch_mode_errorstr;
01646 _switch_mode_errorstr = STR_COULD_NOT_CREATE_TOWN;
01647 }
01648 }
01649 return false;
01650 }
01651 return true;
01652 }
01653
01654
01660 HouseZonesBits GetTownRadiusGroup(const Town *t, TileIndex tile)
01661 {
01662 uint dist = DistanceSquare(tile, t->xy);
01663
01664 if (t->fund_buildings_months && dist <= 25) return HZB_TOWN_CENTRE;
01665
01666 HouseZonesBits smallest = HZB_TOWN_EDGE;
01667 for (HouseZonesBits i = HZB_BEGIN; i < HZB_END; i++) {
01668 if (dist < t->squared_town_zone_radius[i]) smallest = i;
01669 }
01670
01671 return smallest;
01672 }
01673
01684 static inline void ClearMakeHouseTile(TileIndex tile, Town *t, byte counter, byte stage, HouseID type, byte random_bits)
01685 {
01686 CommandCost cc = DoCommand(tile, 0, 0, DC_EXEC | DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR);
01687
01688 assert(CmdSucceeded(cc));
01689
01690 IncreaseBuildingCount(t, type);
01691 MakeHouseTile(tile, t->index, counter, stage, type, random_bits);
01692 if (GetHouseSpecs(type)->building_flags & BUILDING_IS_ANIMATED) AddAnimatedTile(tile);
01693
01694 MarkTileDirtyByTile(tile);
01695 }
01696
01697
01708 static void MakeTownHouse(TileIndex t, Town *town, byte counter, byte stage, HouseID type, byte random_bits)
01709 {
01710 BuildingFlags size = GetHouseSpecs(type)->building_flags;
01711
01712 ClearMakeHouseTile(t, town, counter, stage, type, random_bits);
01713 if (size & BUILDING_2_TILES_Y) ClearMakeHouseTile(t + TileDiffXY(0, 1), town, counter, stage, ++type, random_bits);
01714 if (size & BUILDING_2_TILES_X) ClearMakeHouseTile(t + TileDiffXY(1, 0), town, counter, stage, ++type, random_bits);
01715 if (size & BUILDING_HAS_4_TILES) ClearMakeHouseTile(t + TileDiffXY(1, 1), town, counter, stage, ++type, random_bits);
01716 }
01717
01718
01727 static inline bool CanBuildHouseHere(TileIndex tile, TownID town, bool noslope)
01728 {
01729
01730 Slope slope = GetTileSlope(tile, NULL);
01731 if ((noslope && slope != SLOPE_FLAT) || IsSteepSlope(slope)) return false;
01732
01733
01734 if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return false;
01735
01736
01737 if (IsTileType(tile, MP_HOUSE) && GetTownIndex(tile) != town) return false;
01738
01739
01740 return CmdSucceeded(DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR));
01741 }
01742
01743
01753 static inline bool CheckBuildHouseSameZ(TileIndex tile, TownID town, uint z, bool noslope)
01754 {
01755 if (!CanBuildHouseHere(tile, town, noslope)) return false;
01756
01757
01758 if (GetTileMaxZ(tile) != z) return false;
01759
01760 return true;
01761 }
01762
01763
01773 static bool CheckFree2x2Area(TileIndex tile, TownID town, uint z, bool noslope)
01774 {
01775
01776 if (!CheckBuildHouseSameZ(tile, town, z, noslope)) return false;
01777
01778 for (DiagDirection d = DIAGDIR_SE; d < DIAGDIR_END; d++) {
01779 tile += TileOffsByDiagDir(d);
01780 if (!CheckBuildHouseSameZ(tile, town, z, noslope)) return false;
01781 }
01782
01783 return true;
01784 }
01785
01786
01794 static inline bool TownLayoutAllowsHouseHere(Town *t, TileIndex tile)
01795 {
01796
01797 if (!_settings_game.economy.allow_town_roads && !_generating_world) return true;
01798
01799 TileIndexDiffC grid_pos = TileIndexToTileIndexDiffC(t->xy, tile);
01800
01801 switch (t->layout) {
01802 case TL_2X2_GRID:
01803 if ((grid_pos.x % 3) == 0 || (grid_pos.y % 3) == 0) return false;
01804 break;
01805
01806 case TL_3X3_GRID:
01807 if ((grid_pos.x % 4) == 0 || (grid_pos.y % 4) == 0) return false;
01808 break;
01809
01810 default:
01811 break;
01812 }
01813
01814 return true;
01815 }
01816
01817
01825 static inline bool TownLayoutAllows2x2HouseHere(Town *t, TileIndex tile)
01826 {
01827
01828 if (!_settings_game.economy.allow_town_roads && !_generating_world) return true;
01829
01830
01831
01832 uint dx = MapSize() + TileX(t->xy) - TileX(tile);
01833 uint dy = MapSize() + TileY(t->xy) - TileY(tile);
01834
01835 switch (t->layout) {
01836 case TL_2X2_GRID:
01837 if ((dx % 3) != 0 || (dy % 3) != 0) return false;
01838 break;
01839
01840 case TL_3X3_GRID:
01841 if ((dx % 4) < 2 || (dy % 4) < 2) return false;
01842 break;
01843
01844 default:
01845 break;
01846 }
01847
01848 return true;
01849 }
01850
01851
01861 static bool CheckTownBuild2House(TileIndex *tile, Town *t, uint maxz, bool noslope, DiagDirection second)
01862 {
01863
01864
01865 TileIndex tile2 = *tile + TileOffsByDiagDir(second);
01866 if (TownLayoutAllowsHouseHere(t, tile2) && CheckBuildHouseSameZ(tile2, t->index, maxz, noslope)) return true;
01867
01868 tile2 = *tile + TileOffsByDiagDir(ReverseDiagDir(second));
01869 if (TownLayoutAllowsHouseHere(t, tile2) && CheckBuildHouseSameZ(tile2, t->index, maxz, noslope)) {
01870 *tile = tile2;
01871 return true;
01872 }
01873
01874 return false;
01875 }
01876
01877
01886 static bool CheckTownBuild2x2House(TileIndex *tile, Town *t, uint maxz, bool noslope)
01887 {
01888 TileIndex tile2 = *tile;
01889
01890 for (DiagDirection d = DIAGDIR_SE;;d++) {
01891 if (TownLayoutAllows2x2HouseHere(t, tile2) && CheckFree2x2Area(tile2, t->index, maxz, noslope)) {
01892 *tile = tile2;
01893 return true;
01894 }
01895 if (d == DIAGDIR_END) break;
01896 tile2 += TileOffsByDiagDir(ReverseDiagDir(d));
01897 }
01898
01899 return false;
01900 }
01901
01902
01909 static bool BuildTownHouse(Town *t, TileIndex tile)
01910 {
01911
01912 if (!TownLayoutAllowsHouseHere(t, tile)) return false;
01913
01914
01915 if (!CanBuildHouseHere(tile, t->index, false)) return false;
01916
01917 uint z;
01918 Slope slope = GetTileSlope(tile, &z);
01919
01920
01921
01922 HouseZonesBits rad = GetTownRadiusGroup(t, tile);
01923
01924
01925 int land = _settings_game.game_creation.landscape;
01926 if (land == LT_ARCTIC && z >= _settings_game.game_creation.snow_line) land = -1;
01927
01928 uint bitmask = (1 << rad) + (1 << (land + 12));
01929
01930
01931
01932
01933 HouseID houses[HOUSE_MAX];
01934 uint num = 0;
01935 uint probs[HOUSE_MAX];
01936 uint probability_max = 0;
01937
01938
01939 for (uint i = 0; i < HOUSE_MAX; i++) {
01940 const HouseSpec *hs = GetHouseSpecs(i);
01941
01942
01943 if ((~hs->building_availability & bitmask) != 0 || !hs->enabled) continue;
01944
01945
01946 if (hs->class_id != HOUSE_NO_CLASS) {
01947
01948 if (t->building_counts.class_count[hs->class_id] == UINT16_MAX) continue;
01949 } else {
01950
01951 if (t->building_counts.id_count[i] == UINT16_MAX) continue;
01952 }
01953
01954
01955 uint cur_prob = (_loaded_newgrf_features.has_newhouses ? hs->probability : 1);
01956 probability_max += cur_prob;
01957 probs[num] = cur_prob;
01958 houses[num++] = (HouseID)i;
01959 }
01960
01961 uint maxz = GetTileMaxZ(tile);
01962
01963 while (probability_max > 0) {
01964 uint r = RandomRange(probability_max);
01965 uint i;
01966 for (i = 0; i < num; i++) {
01967 if (probs[i] > r) break;
01968 r -= probs[i];
01969 }
01970
01971 HouseID house = houses[i];
01972 probability_max -= probs[i];
01973
01974
01975 num--;
01976 houses[i] = houses[num];
01977 probs[i] = probs[num];
01978
01979 const HouseSpec *hs = GetHouseSpecs(house);
01980
01981 if (_loaded_newgrf_features.has_newhouses) {
01982 if (hs->override != 0) {
01983 house = hs->override;
01984 hs = GetHouseSpecs(house);
01985 }
01986
01987 if ((hs->extra_flags & BUILDING_IS_HISTORICAL) && !_generating_world && _game_mode != GM_EDITOR) continue;
01988 }
01989
01990 if (_cur_year < hs->min_year || _cur_year > hs->max_year) continue;
01991
01992
01993 uint oneof = 0;
01994
01995 if (hs->building_flags & BUILDING_IS_CHURCH) {
01996 SetBit(oneof, TOWN_HAS_CHURCH);
01997 } else if (hs->building_flags & BUILDING_IS_STADIUM) {
01998 SetBit(oneof, TOWN_HAS_STADIUM);
01999 }
02000
02001 if (HASBITS(t->flags12, oneof)) continue;
02002
02003
02004 bool noslope = (hs->building_flags & TILE_NOT_SLOPED) != 0;
02005 if (noslope && slope != SLOPE_FLAT) continue;
02006
02007 if (hs->building_flags & TILE_SIZE_2x2) {
02008 if (!CheckTownBuild2x2House(&tile, t, maxz, noslope)) continue;
02009 } else if (hs->building_flags & TILE_SIZE_2x1) {
02010 if (!CheckTownBuild2House(&tile, t, maxz, noslope, DIAGDIR_SW)) continue;
02011 } else if (hs->building_flags & TILE_SIZE_1x2) {
02012 if (!CheckTownBuild2House(&tile, t, maxz, noslope, DIAGDIR_SE)) continue;
02013 } else {
02014
02015 }
02016
02017 if (HasBit(hs->callback_mask, CBM_HOUSE_ALLOW_CONSTRUCTION)) {
02018 uint16 callback_res = GetHouseCallback(CBID_HOUSE_ALLOW_CONSTRUCTION, 0, 0, house, t, tile);
02019 if (callback_res != CALLBACK_FAILED && GB(callback_res, 0, 8) == 0) continue;
02020 }
02021
02022
02023 t->num_houses++;
02024
02025
02026 t->flags12 |= oneof;
02027
02028 byte construction_counter = 0;
02029 byte construction_stage = 0;
02030
02031 if (_generating_world || _game_mode == GM_EDITOR) {
02032 uint32 r = Random();
02033
02034 construction_stage = TOWN_HOUSE_COMPLETED;
02035 if (Chance16(1, 7)) construction_stage = GB(r, 0, 2);
02036
02037 if (construction_stage == TOWN_HOUSE_COMPLETED) {
02038 ChangePopulation(t, hs->population);
02039 } else {
02040 construction_counter = GB(r, 2, 2);
02041 }
02042 }
02043
02044 MakeTownHouse(tile, t, construction_counter, construction_stage, house, Random());
02045
02046 return true;
02047 }
02048
02049 return false;
02050 }
02051
02058 static void DoClearTownHouseHelper(TileIndex tile, Town *t, HouseID house)
02059 {
02060 assert(IsTileType(tile, MP_HOUSE));
02061 DecreaseBuildingCount(t, house);
02062 DoClearSquare(tile);
02063 DeleteAnimatedTile(tile);
02064 }
02065
02073 TileIndexDiff GetHouseNorthPart(HouseID &house)
02074 {
02075 if (house >= 3) {
02076 if (GetHouseSpecs(house - 1)->building_flags & TILE_SIZE_2x1) {
02077 house--;
02078 return TileDiffXY(-1, 0);
02079 } else if (GetHouseSpecs(house - 1)->building_flags & BUILDING_2_TILES_Y) {
02080 house--;
02081 return TileDiffXY(0, -1);
02082 } else if (GetHouseSpecs(house - 2)->building_flags & BUILDING_HAS_4_TILES) {
02083 house -= 2;
02084 return TileDiffXY(-1, 0);
02085 } else if (GetHouseSpecs(house - 3)->building_flags & BUILDING_HAS_4_TILES) {
02086 house -= 3;
02087 return TileDiffXY(-1, -1);
02088 }
02089 }
02090 return 0;
02091 }
02092
02093 void ClearTownHouse(Town *t, TileIndex tile)
02094 {
02095 assert(IsTileType(tile, MP_HOUSE));
02096
02097 HouseID house = GetHouseType(tile);
02098
02099
02100 tile += GetHouseNorthPart(house);
02101
02102 const HouseSpec *hs = GetHouseSpecs(house);
02103
02104
02105 if (IsHouseCompleted(tile)) {
02106 ChangePopulation(t, -hs->population);
02107 }
02108
02109 t->num_houses--;
02110
02111
02112 if (hs->building_flags & BUILDING_IS_CHURCH) {
02113 ClrBit(t->flags12, TOWN_HAS_CHURCH);
02114 } else if (hs->building_flags & BUILDING_IS_STADIUM) {
02115 ClrBit(t->flags12, TOWN_HAS_STADIUM);
02116 }
02117
02118
02119 uint eflags = hs->building_flags;
02120 DoClearTownHouseHelper(tile, t, house);
02121 if (eflags & BUILDING_2_TILES_Y) DoClearTownHouseHelper(tile + TileDiffXY(0, 1), t, ++house);
02122 if (eflags & BUILDING_2_TILES_X) DoClearTownHouseHelper(tile + TileDiffXY(1, 0), t, ++house);
02123 if (eflags & BUILDING_HAS_4_TILES) DoClearTownHouseHelper(tile + TileDiffXY(1, 1), t, ++house);
02124 }
02125
02126 static bool IsUniqueTownName(const char *name)
02127 {
02128 const Town *t;
02129
02130 FOR_ALL_TOWNS(t) {
02131 if (t->name != NULL && strcmp(t->name, name) == 0) return false;
02132 }
02133
02134 return true;
02135 }
02136
02143 CommandCost CmdRenameTown(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
02144 {
02145 if (!IsValidTownID(p1)) return CMD_ERROR;
02146
02147 bool reset = StrEmpty(text);
02148
02149 if (!reset) {
02150 if (strlen(text) >= MAX_LENGTH_TOWN_NAME_BYTES) return CMD_ERROR;
02151 if (!IsUniqueTownName(text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
02152 }
02153
02154 if (flags & DC_EXEC) {
02155 Town *t = GetTown(p1);
02156
02157 free(t->name);
02158 t->name = reset ? NULL : strdup(text);
02159
02160 UpdateTownVirtCoord(t);
02161 InvalidateWindowData(WC_TOWN_DIRECTORY, 0, 1);
02162 UpdateAllStationVirtCoord();
02163 UpdateAllWaypointSigns();
02164 MarkWholeScreenDirty();
02165 }
02166 return CommandCost();
02167 }
02168
02170 void ExpandTown(Town *t)
02171 {
02172
02173
02174 static bool warned_no_roads = false;
02175 if (!_settings_game.economy.allow_town_roads && !warned_no_roads) {
02176 ShowErrorMessage(INVALID_STRING_ID, STR_TOWN_EXPAND_WARN_NO_ROADS, 0, 0);
02177 warned_no_roads = true;
02178 }
02179
02180
02181 uint amount = RandomRange(ClampToU16(t->num_houses / 10)) + 3;
02182 t->num_houses += amount;
02183 UpdateTownRadius(t);
02184
02185 uint n = amount * 10;
02186 do GrowTown(t); while (--n);
02187
02188 t->num_houses -= amount;
02189 UpdateTownRadius(t);
02190
02191 UpdateTownMaxPass(t);
02192 }
02193
02194 extern const byte _town_action_costs[8] = {
02195 2, 4, 9, 35, 48, 53, 117, 175
02196 };
02197
02198 static void TownActionAdvertiseSmall(Town *t)
02199 {
02200 ModifyStationRatingAround(t->xy, _current_company, 0x40, 10);
02201 }
02202
02203 static void TownActionAdvertiseMedium(Town *t)
02204 {
02205 ModifyStationRatingAround(t->xy, _current_company, 0x70, 15);
02206 }
02207
02208 static void TownActionAdvertiseLarge(Town *t)
02209 {
02210 ModifyStationRatingAround(t->xy, _current_company, 0xA0, 20);
02211 }
02212
02213 static void TownActionRoadRebuild(Town *t)
02214 {
02215 t->road_build_months = 6;
02216
02217 char company_name[MAX_LENGTH_COMPANY_NAME_BYTES];
02218 SetDParam(0, _current_company);
02219 GetString(company_name, STR_COMPANY_NAME, lastof(company_name));
02220
02221 char *cn = strdup(company_name);
02222 SetDParam(0, t->index);
02223 SetDParamStr(1, cn);
02224
02225 AddNewsItem(STR_2055_TRAFFIC_CHAOS_IN_ROAD_REBUILDING, NS_GENERAL, t->xy, 0, cn);
02226 }
02227
02228 static bool DoBuildStatueOfCompany(TileIndex tile, TownID town_id)
02229 {
02230
02231 if (IsSteepSlope(GetTileSlope(tile, NULL))) return false;
02232
02233 if (MayHaveBridgeAbove(tile) && IsBridgeAbove(tile)) return false;
02234
02235 if (!IsTileType(tile, MP_HOUSE) &&
02236 !IsTileType(tile, MP_CLEAR) &&
02237 !IsTileType(tile, MP_TREES)) {
02238 return false;
02239 }
02240
02241 CompanyID old = _current_company;
02242 _current_company = OWNER_NONE;
02243 CommandCost r = DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
02244 _current_company = old;
02245
02246 if (CmdFailed(r)) return false;
02247
02248 MakeStatue(tile, _current_company, town_id);
02249 MarkTileDirtyByTile(tile);
02250
02251 return true;
02252 }
02253
02260 static bool SearchTileForStatue(TileIndex tile, void *user_data)
02261 {
02262 TownID *town_id = (TownID *)user_data;
02263 return DoBuildStatueOfCompany(tile, *town_id);
02264 }
02265
02271 static void TownActionBuildStatue(Town *t)
02272 {
02273 TileIndex tile = t->xy;
02274
02275 if (CircularTileSearch(&tile, 9, SearchTileForStatue, &t->index)) {
02276 SetBit(t->statues, _current_company);
02277 }
02278 }
02279
02280 static void TownActionFundBuildings(Town *t)
02281 {
02282
02283 t->grow_counter = 1;
02284
02285 SetBit(t->flags12, TOWN_IS_FUNDED);
02286
02287 t->fund_buildings_months = 3;
02288 }
02289
02290 static void TownActionBuyRights(Town *t)
02291 {
02292
02293 if (!_settings_game.economy.exclusive_rights) return;
02294
02295 t->exclusive_counter = 12;
02296 t->exclusivity = _current_company;
02297
02298 ModifyStationRatingAround(t->xy, _current_company, 130, 17);
02299 }
02300
02301 static void TownActionBribe(Town *t)
02302 {
02303 if (Chance16(1, 14)) {
02304
02305 t->unwanted[_current_company] = 6;
02306
02307
02308 Station *st;
02309 FOR_ALL_STATIONS(st) {
02310 if (st->town == t && st->owner == _current_company) {
02311 for (CargoID i = 0; i < NUM_CARGO; i++) st->goods[i].rating = 0;
02312 }
02313 }
02314
02315
02316
02317 if (IsLocalCompany()) ShowErrorMessage(STR_BRIBE_FAILED_2, STR_BRIBE_FAILED, 0, 0);
02318
02319
02320
02321
02322
02323 if (t->ratings[_current_company] > RATING_BRIBE_DOWN_TO) {
02324 t->ratings[_current_company] = RATING_BRIBE_DOWN_TO;
02325 InvalidateWindow(WC_TOWN_AUTHORITY, t->index);
02326 }
02327 } else {
02328 ChangeTownRating(t, RATING_BRIBE_UP_STEP, RATING_BRIBE_MAXIMUM, DC_EXEC);
02329 }
02330 }
02331
02332 typedef void TownActionProc(Town *t);
02333 static TownActionProc * const _town_action_proc[] = {
02334 TownActionAdvertiseSmall,
02335 TownActionAdvertiseMedium,
02336 TownActionAdvertiseLarge,
02337 TownActionRoadRebuild,
02338 TownActionBuildStatue,
02339 TownActionFundBuildings,
02340 TownActionBuyRights,
02341 TownActionBribe
02342 };
02343
02344 enum TownActions {
02345 TACT_NONE = 0x00,
02346
02347 TACT_ADVERTISE_SMALL = 0x01,
02348 TACT_ADVERTISE_MEDIUM = 0x02,
02349 TACT_ADVERTISE_LARGE = 0x04,
02350 TACT_ROAD_REBUILD = 0x08,
02351 TACT_BUILD_STATUE = 0x10,
02352 TACT_FOUND_BUILDINGS = 0x20,
02353 TACT_BUY_RIGHTS = 0x40,
02354 TACT_BRIBE = 0x80,
02355
02356 TACT_ADVERTISE = TACT_ADVERTISE_SMALL | TACT_ADVERTISE_MEDIUM | TACT_ADVERTISE_LARGE,
02357 TACT_CONSTRUCTION = TACT_ROAD_REBUILD | TACT_BUILD_STATUE | TACT_FOUND_BUILDINGS,
02358 TACT_FUNDS = TACT_BUY_RIGHTS | TACT_BRIBE,
02359 TACT_ALL = TACT_ADVERTISE | TACT_CONSTRUCTION | TACT_FUNDS,
02360 };
02361
02362 DECLARE_ENUM_AS_BIT_SET(TownActions);
02363
02370 uint GetMaskOfTownActions(int *nump, CompanyID cid, const Town *t)
02371 {
02372 int num = 0;
02373 TownActions buttons = TACT_NONE;
02374
02375
02376 if (cid != COMPANY_SPECTATOR && !(_settings_game.economy.bribe && t->unwanted[cid])) {
02377
02378
02379 Money avail = GetCompany(cid)->money + _price.station_value * 200;
02380 Money ref = _price.build_industry >> 8;
02381
02382
02383
02384 for (uint i = 0; i != lengthof(_town_action_costs); i++) {
02385 const TownActions cur = (TownActions)(1 << i);
02386
02387
02388 if (cur == TACT_BRIBE && (!_settings_game.economy.bribe || t->ratings[cid] >= RATING_BRIBE_MAXIMUM))
02389 continue;
02390
02391
02392 if (cur == TACT_BUY_RIGHTS && !_settings_game.economy.exclusive_rights)
02393 continue;
02394
02395
02396 if (cur == TACT_BUILD_STATUE && HasBit(t->statues, cid))
02397 continue;
02398
02399 if (avail >= _town_action_costs[i] * ref) {
02400 buttons |= cur;
02401 num++;
02402 }
02403 }
02404 }
02405
02406 if (nump != NULL) *nump = num;
02407 return buttons;
02408 }
02409
02418 CommandCost CmdDoTownAction(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
02419 {
02420 if (!IsValidTownID(p1) || p2 >= lengthof(_town_action_proc)) return CMD_ERROR;
02421
02422 Town *t = GetTown(p1);
02423
02424 if (!HasBit(GetMaskOfTownActions(NULL, _current_company, t), p2)) return CMD_ERROR;
02425
02426 CommandCost cost(EXPENSES_OTHER, (_price.build_industry >> 8) * _town_action_costs[p2]);
02427
02428 if (flags & DC_EXEC) {
02429 _town_action_proc[p2](t);
02430 InvalidateWindow(WC_TOWN_AUTHORITY, p1);
02431 }
02432
02433 return cost;
02434 }
02435
02436 static void UpdateTownGrowRate(Town *t)
02437 {
02438
02439 const Company *c;
02440 FOR_ALL_COMPANIES(c) {
02441 if (t->ratings[c->index] < RATING_GROWTH_MAXIMUM) {
02442 t->ratings[c->index] = min((int)RATING_GROWTH_MAXIMUM, t->ratings[c->index] + RATING_GROWTH_UP_STEP);
02443 }
02444 }
02445
02446 int n = 0;
02447
02448 const Station *st;
02449 FOR_ALL_STATIONS(st) {
02450 if (DistanceSquare(st->xy, t->xy) <= t->squared_town_zone_radius[0]) {
02451 if (st->time_since_load <= 20 || st->time_since_unload <= 20) {
02452 n++;
02453 if (IsValidCompanyID(st->owner)) {
02454 int new_rating = t->ratings[st->owner] + RATING_STATION_UP_STEP;
02455 t->ratings[st->owner] = min(new_rating, INT16_MAX);
02456 }
02457 } else {
02458 if (IsValidCompanyID(st->owner)) {
02459 int new_rating = t->ratings[st->owner] + RATING_STATION_DOWN_STEP;
02460 t->ratings[st->owner] = max(new_rating, INT16_MIN);
02461 }
02462 }
02463 }
02464 }
02465
02466
02467 for (uint i = 0; i < MAX_COMPANIES; i++) {
02468 t->ratings[i] = Clamp(t->ratings[i], RATING_MINIMUM, RATING_MAXIMUM);
02469 }
02470
02471 InvalidateWindow(WC_TOWN_AUTHORITY, t->index);
02472
02473 ClrBit(t->flags12, TOWN_IS_FUNDED);
02474 if (_settings_game.economy.town_growth_rate == 0 && t->fund_buildings_months == 0) return;
02475
02478 static const uint16 _grow_count_values[2][6] = {
02479 { 120, 120, 120, 100, 80, 60 },
02480 { 320, 420, 300, 220, 160, 100 }
02481 };
02482
02483 uint16 m;
02484
02485 if (t->fund_buildings_months != 0) {
02486 m = _grow_count_values[0][min(n, 5)];
02487 t->fund_buildings_months--;
02488 } else {
02489 m = _grow_count_values[1][min(n, 5)];
02490 if (n == 0 && !Chance16(1, 12)) return;
02491 }
02492
02493 if (_settings_game.game_creation.landscape == LT_ARCTIC) {
02494 if (TilePixelHeight(t->xy) >= GetSnowLine() && t->act_food == 0 && t->population > 90)
02495 return;
02496 } else if (_settings_game.game_creation.landscape == LT_TROPIC) {
02497 if (GetTropicZone(t->xy) == TROPICZONE_DESERT && (t->act_food == 0 || t->act_water == 0) && t->population > 60)
02498 return;
02499 }
02500
02501
02502
02503 uint growth_multiplier = _settings_game.economy.town_growth_rate != 0 ? _settings_game.economy.town_growth_rate - 1 : 1;
02504
02505 m >>= growth_multiplier;
02506 if (t->larger_town) m /= 2;
02507
02508 t->growth_rate = m / (t->num_houses / 50 + 1);
02509 if (m <= t->grow_counter)
02510 t->grow_counter = m;
02511
02512 SetBit(t->flags12, TOWN_IS_FUNDED);
02513 }
02514
02515 static void UpdateTownAmounts(Town *t)
02516 {
02517
02518 t->pct_pass_transported = t->new_act_pass * 256 / (t->new_max_pass + 1);
02519
02520 t->max_pass = t->new_max_pass; t->new_max_pass = 0;
02521 t->act_pass = t->new_act_pass; t->new_act_pass = 0;
02522 t->act_food = t->new_act_food; t->new_act_food = 0;
02523 t->act_water = t->new_act_water; t->new_act_water = 0;
02524
02525
02526 t->pct_mail_transported = t->new_act_mail * 256 / (t->new_max_mail + 1);
02527 t->max_mail = t->new_max_mail; t->new_max_mail = 0;
02528 t->act_mail = t->new_act_mail; t->new_act_mail = 0;
02529
02530 InvalidateWindow(WC_TOWN_VIEW, t->index);
02531 }
02532
02533 static void UpdateTownUnwanted(Town *t)
02534 {
02535 const Company *c;
02536
02537 FOR_ALL_COMPANIES(c) {
02538 if (t->unwanted[c->index] > 0) t->unwanted[c->index]--;
02539 }
02540 }
02541
02547 bool CheckIfAuthorityAllowsNewStation(TileIndex tile, DoCommandFlag flags)
02548 {
02549 if (!IsValidCompanyID(_current_company) || (flags & DC_NO_TEST_TOWN_RATING)) return true;
02550
02551 Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
02552 if (t == NULL) return true;
02553
02554 if (t->ratings[_current_company] > RATING_VERYPOOR) return true;
02555
02556 _error_message = STR_2009_LOCAL_AUTHORITY_REFUSES;
02557 SetDParam(0, t->index);
02558
02559 return false;
02560 }
02561
02562
02563 Town *CalcClosestTownFromTile(TileIndex tile, uint threshold)
02564 {
02565 Town *t;
02566 uint best = threshold;
02567 Town *best_town = NULL;
02568
02569 FOR_ALL_TOWNS(t) {
02570 uint dist = DistanceManhattan(tile, t->xy);
02571 if (dist < best) {
02572 best = dist;
02573 best_town = t;
02574 }
02575 }
02576
02577 return best_town;
02578 }
02579
02580
02581 Town *ClosestTownFromTile(TileIndex tile, uint threshold)
02582 {
02583 switch (GetTileType(tile)) {
02584 case MP_ROAD:
02585 if (!HasTownOwnedRoad(tile)) {
02586 TownID tid = GetTownIndex(tile);
02587 if (tid == (TownID)INVALID_TOWN) {
02588
02589 if (_generating_world) return CalcClosestTownFromTile(tile, threshold);
02590 assert(GetNumTowns() == 0);
02591 return NULL;
02592 }
02593
02594 Town *town = GetTown(tid);
02595 assert(town->IsValid());
02596
02597 if (DistanceManhattan(tile, town->xy) >= threshold) town = NULL;
02598
02599 return town;
02600 }
02601
02602
02603 case MP_HOUSE:
02604 return GetTownByTile(tile);
02605
02606 default:
02607 return CalcClosestTownFromTile(tile, threshold);
02608 }
02609 }
02610
02611 static bool _town_rating_test = false;
02612 SmallMap<const Town *, int, 4> _town_test_ratings;
02613
02614 void SetTownRatingTestMode(bool mode)
02615 {
02616 static int ref_count = 0;
02617 if (mode) {
02618 if (ref_count == 0) {
02619 _town_test_ratings.Clear();
02620 }
02621 ref_count++;
02622 } else {
02623 assert(ref_count > 0);
02624 ref_count--;
02625 }
02626 _town_rating_test = !(ref_count == 0);
02627 }
02628
02629 static int GetRating(const Town *t)
02630 {
02631 if (_town_rating_test) {
02632 SmallMap<const Town *, int>::iterator it = _town_test_ratings.Find(t);
02633 if (it != _town_test_ratings.End()) {
02634 return it->second;
02635 }
02636 }
02637 return t->ratings[_current_company];
02638 }
02639
02647 void ChangeTownRating(Town *t, int add, int max, DoCommandFlag flags)
02648 {
02649
02650 if (t == NULL || (flags & DC_NO_MODIFY_TOWN_RATING) ||
02651 !IsValidCompanyID(_current_company) ||
02652 (_cheats.magic_bulldozer.value && add < 0)) {
02653 return;
02654 }
02655
02656 int rating = GetRating(t);
02657 if (add < 0) {
02658 if (rating > max) {
02659 rating += add;
02660 if (rating < max) rating = max;
02661 }
02662 } else {
02663 if (rating < max) {
02664 rating += add;
02665 if (rating > max) rating = max;
02666 }
02667 }
02668 if (_town_rating_test) {
02669 _town_test_ratings[t] = rating;
02670 } else {
02671 SetBit(t->have_ratings, _current_company);
02672 t->ratings[_current_company] = rating;
02673 InvalidateWindow(WC_TOWN_AUTHORITY, t->index);
02674 }
02675 }
02676
02677
02678 static const int _default_rating_settings [3][3] = {
02679
02680 { 0, 128, 384},
02681 { 48, 192, 480},
02682 { 96, 384, 768},
02683 };
02684
02685 bool CheckforTownRating(DoCommandFlag flags, Town *t, byte type)
02686 {
02687
02688 if (t == NULL || !IsValidCompanyID(_current_company) || _cheats.magic_bulldozer.value)
02689 return true;
02690
02691
02692
02693
02694
02695 int modemod = _default_rating_settings[_settings_game.difficulty.town_council_tolerance][type];
02696
02697 if (GetRating(t) < 16 + modemod && !(flags & DC_NO_TEST_TOWN_RATING)) {
02698 SetDParam(0, t->index);
02699 _error_message = STR_2009_LOCAL_AUTHORITY_REFUSES;
02700 return false;
02701 }
02702
02703 return true;
02704 }
02705
02706 void TownsMonthlyLoop()
02707 {
02708 Town *t;
02709
02710 FOR_ALL_TOWNS(t) {
02711 if (t->road_build_months != 0) t->road_build_months--;
02712
02713 if (t->exclusive_counter != 0)
02714 if (--t->exclusive_counter == 0) t->exclusivity = INVALID_COMPANY;
02715
02716 UpdateTownGrowRate(t);
02717 UpdateTownAmounts(t);
02718 UpdateTownUnwanted(t);
02719 }
02720 }
02721
02722 void TownsYearlyLoop()
02723 {
02724
02725 for (TileIndex t = 0; t < MapSize(); t++) {
02726 if (!IsTileType(t, MP_HOUSE)) continue;
02727 IncrementHouseAge(t);
02728 }
02729 }
02730
02731 void InitializeTowns()
02732 {
02733
02734 _Town_pool.CleanPool();
02735 _Town_pool.AddBlockToPool();
02736
02737 memset(_subsidies, 0, sizeof(_subsidies));
02738 for (Subsidy *s = _subsidies; s != endof(_subsidies); s++) {
02739 s->cargo_type = CT_INVALID;
02740 }
02741
02742 _total_towns = 0;
02743 }
02744
02745 static CommandCost TerraformTile_Town(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
02746 {
02747 if (AutoslopeEnabled()) {
02748 HouseID house = GetHouseType(tile);
02749 GetHouseNorthPart(house);
02750 const HouseSpec *hs = GetHouseSpecs(house);
02751
02752
02753 if (((hs->building_flags & TILE_NOT_SLOPED) == 0) && !IsSteepSlope(tileh_new) &&
02754 (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
02755 }
02756
02757 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
02758 }
02759
02761 extern const TileTypeProcs _tile_type_town_procs = {
02762 DrawTile_Town,
02763 GetSlopeZ_Town,
02764 ClearTile_Town,
02765 GetAcceptedCargo_Town,
02766 GetTileDesc_Town,
02767 GetTileTrackStatus_Town,
02768 NULL,
02769 AnimateTile_Town,
02770 TileLoop_Town,
02771 ChangeTileOwner_Town,
02772 GetProducedCargo_Town,
02773 NULL,
02774 GetFoundation_Town,
02775 TerraformTile_Town,
02776 };
02777
02778 void ResetHouses()
02779 {
02780 memset(&_house_specs, 0, sizeof(_house_specs));
02781 memcpy(&_house_specs, &_original_house_specs, sizeof(_original_house_specs));
02782
02783
02784 _house_mngr.ResetOverride();
02785 }