00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "landscape.h"
00014 #include "command_func.h"
00015 #include "viewport_func.h"
00016 #include "company_base.h"
00017 #include "town.h"
00018 #include "bridge_map.h"
00019 #include "genworld.h"
00020 #include "autoslope.h"
00021 #include "transparency.h"
00022 #include "functions.h"
00023 #include "window_func.h"
00024 #include "vehicle_func.h"
00025 #include "company_gui.h"
00026 #include "cheat_type.h"
00027 #include "landscape_type.h"
00028 #include "unmovable.h"
00029 #include "cargopacket.h"
00030 #include "sprite.h"
00031 #include "core/random_func.hpp"
00032 #include "unmovable_map.h"
00033
00034 #include "table/strings.h"
00035 #include "table/sprites.h"
00036 #include "table/unmovable_land.h"
00037
00046 static inline const UnmovableSpec *GetUnmovableSpec(UnmovableType type)
00047 {
00048 assert(type < UNMOVABLE_MAX);
00049 return &_original_unmovable[type];
00050 }
00051
00059 static CommandCost DestroyCompanyHQ(CompanyID cid, DoCommandFlag flags)
00060 {
00061 Company *c = Company::Get(cid);
00062
00063 if (flags & DC_EXEC) {
00064 TileIndex t = c->location_of_HQ;
00065
00066 DoClearSquare(t);
00067 DoClearSquare(t + TileDiffXY(0, 1));
00068 DoClearSquare(t + TileDiffXY(1, 0));
00069 DoClearSquare(t + TileDiffXY(1, 1));
00070 c->location_of_HQ = INVALID_TILE;
00071 SetWindowDirty(WC_COMPANY, cid);
00072
00073 CargoPacket::InvalidateAllFrom(ST_HEADQUARTERS, cid);
00074 }
00075
00076
00077 return CommandCost(EXPENSES_PROPERTY, CalculateCompanyValue(c) / 100);
00078 }
00079
00080 void UpdateCompanyHQ(Company *c, uint score)
00081 {
00082 byte val;
00083 TileIndex tile = c->location_of_HQ;
00084
00085 if (tile == INVALID_TILE) return;
00086
00087 (val = 0, score < 170) ||
00088 (val++, score < 350) ||
00089 (val++, score < 520) ||
00090 (val++, score < 720) ||
00091 (val++, true);
00092
00093 EnlargeCompanyHQ(tile, val);
00094
00095 MarkTileDirtyByTile(tile);
00096 MarkTileDirtyByTile(tile + TileDiffXY(0, 1));
00097 MarkTileDirtyByTile(tile + TileDiffXY(1, 0));
00098 MarkTileDirtyByTile(tile + TileDiffXY(1, 1));
00099 }
00100
00101 extern CommandCost CheckFlatLandBelow(TileIndex tile, uint w, uint h, DoCommandFlag flags, uint invalid_dirs, StationID *station, bool check_clear = true, RailType rt = INVALID_RAILTYPE);
00102
00111 CommandCost CmdBuildCompanyHQ(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00112 {
00113 Company *c = Company::Get(_current_company);
00114 CommandCost cost(EXPENSES_PROPERTY);
00115
00116 cost = CheckFlatLandBelow(tile, 2, 2, flags, 0, NULL);
00117 if (cost.Failed()) return cost;
00118
00119 if (c->location_of_HQ != INVALID_TILE) {
00120 cost.AddCost(DestroyCompanyHQ(_current_company, flags));
00121 }
00122
00123 if (flags & DC_EXEC) {
00124 int score = UpdateCompanyRatingAndValue(c, false);
00125
00126 c->location_of_HQ = tile;
00127
00128 MakeCompanyHQ(tile, _current_company);
00129
00130 UpdateCompanyHQ(c, score);
00131 SetWindowDirty(WC_COMPANY, c->index);
00132 }
00133
00134 return cost;
00135 }
00136
00146 CommandCost CmdPurchaseLandArea(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00147 {
00148 CommandCost cost(EXPENSES_CONSTRUCTION);
00149
00150 if (IsOwnedLandTile(tile) && IsTileOwner(tile, _current_company)) {
00151 return_cmd_error(STR_ERROR_YOU_ALREADY_OWN_IT);
00152 }
00153
00154 cost = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00155 if (cost.Failed()) return CMD_ERROR;
00156
00157 if (flags & DC_EXEC) {
00158 MakeOwnedLand(tile, _current_company);
00159 MarkTileDirtyByTile(tile);
00160 }
00161
00162 return cost.AddCost(GetUnmovableSpec(UNMOVABLE_OWNED_LAND)->GetBuildingCost());
00163 }
00164
00174 CommandCost CmdSellLandArea(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00175 {
00176 if (!IsOwnedLandTile(tile)) return CMD_ERROR;
00177 if (!CheckTileOwnership(tile) && _current_company != OWNER_WATER) return CMD_ERROR;
00178
00179 if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
00180
00181 if (flags & DC_EXEC) DoClearSquare(tile);
00182
00183 return CommandCost(EXPENSES_CONSTRUCTION, - GetUnmovableSpec(UNMOVABLE_OWNED_LAND)->GetRemovalCost());
00184 }
00185
00186 static Foundation GetFoundation_Unmovable(TileIndex tile, Slope tileh);
00187
00188 static void DrawTile_Unmovable(TileInfo *ti)
00189 {
00190
00191 switch (GetUnmovableType(ti->tile)) {
00192 default: NOT_REACHED();
00193 case UNMOVABLE_TRANSMITTER:
00194 case UNMOVABLE_LIGHTHOUSE: {
00195 const DrawTileSeqStruct *dtu = &_draw_tile_transmitterlighthouse_data[GetUnmovableType(ti->tile)];
00196
00197 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
00198 DrawClearLandTile(ti, 2);
00199
00200 if (IsInvisibilitySet(TO_STRUCTURES)) break;
00201
00202 AddSortableSpriteToDraw(
00203 dtu->image.sprite, PAL_NONE, ti->x | dtu->delta_x, ti->y | dtu->delta_y,
00204 dtu->size_x, dtu->size_y, dtu->size_z, ti->z,
00205 IsTransparencySet(TO_STRUCTURES)
00206 );
00207 break;
00208 }
00209
00210 case UNMOVABLE_STATUE:
00211
00212 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, GetFoundation_Unmovable(ti->tile, ti->tileh));
00213
00214 DrawGroundSprite(SPR_CONCRETE_GROUND, PAL_NONE);
00215
00216 if (IsInvisibilitySet(TO_STRUCTURES)) break;
00217
00218 AddSortableSpriteToDraw(SPR_STATUE_COMPANY, COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile)), ti->x, ti->y, 16, 16, 25, ti->z, IsTransparencySet(TO_STRUCTURES));
00219 break;
00220
00221 case UNMOVABLE_OWNED_LAND:
00222 DrawClearLandTile(ti, 0);
00223
00224 AddSortableSpriteToDraw(
00225 SPR_BOUGHT_LAND, COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile)),
00226 ti->x + TILE_SIZE / 2, ti->y + TILE_SIZE / 2, 1, 1, BB_HEIGHT_UNDER_BRIDGE, GetSlopeZ(ti->x + TILE_SIZE / 2, ti->y + TILE_SIZE / 2)
00227 );
00228 DrawBridgeMiddle(ti);
00229 break;
00230
00231 case UNMOVABLE_HQ:{
00232 assert(IsCompanyHQ(ti->tile));
00233 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
00234
00235 SpriteID palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
00236
00237 const DrawTileSprites *t = &_unmovable_display_datas[GetCompanyHQSize(ti->tile) << 2 | GetCompanyHQSection(ti->tile)];
00238 DrawGroundSprite(t->ground.sprite, palette);
00239
00240 if (IsInvisibilitySet(TO_STRUCTURES)) break;
00241
00242 const DrawTileSeqStruct *dtss;
00243 foreach_draw_tile_seq(dtss, t->seq) {
00244 AddSortableSpriteToDraw(
00245 dtss->image.sprite, palette,
00246 ti->x + dtss->delta_x, ti->y + dtss->delta_y,
00247 dtss->size_x, dtss->size_y,
00248 dtss->size_z, ti->z + dtss->delta_z,
00249 IsTransparencySet(TO_STRUCTURES)
00250 );
00251 }
00252 break;
00253 }
00254 }
00255 }
00256
00257 static uint GetSlopeZ_Unmovable(TileIndex tile, uint x, uint y)
00258 {
00259 if (IsOwnedLand(tile)) {
00260 uint z;
00261 Slope tileh = GetTileSlope(tile, &z);
00262
00263 return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
00264 } else {
00265 return GetTileMaxZ(tile);
00266 }
00267 }
00268
00269 static Foundation GetFoundation_Unmovable(TileIndex tile, Slope tileh)
00270 {
00271 return IsOwnedLand(tile) ? FOUNDATION_NONE : FlatteningFoundation(tileh);
00272 }
00273
00274 static CommandCost ClearTile_Unmovable(TileIndex tile, DoCommandFlag flags)
00275 {
00276 if (IsCompanyHQ(tile)) {
00277 if (_current_company == OWNER_WATER) {
00278 return DestroyCompanyHQ(GetTileOwner(tile), DC_EXEC);
00279 } else {
00280 return_cmd_error(flags & DC_AUTO ? STR_ERROR_COMPANY_HEADQUARTERS_IN : INVALID_STRING_ID);
00281 }
00282 }
00283
00284 if (IsOwnedLand(tile)) {
00285 return DoCommand(tile, 0, 0, flags, CMD_SELL_LAND_AREA);
00286 }
00287
00288
00289 if (_game_mode != GM_EDITOR && _current_company != OWNER_WATER && ((flags & DC_AUTO) || !_cheats.magic_bulldozer.value) )
00290 return_cmd_error(flags & DC_AUTO ? STR_ERROR_OBJECT_IN_THE_WAY : INVALID_STRING_ID);
00291
00292 if (IsStatue(tile)) {
00293 if (flags & DC_AUTO) return_cmd_error(STR_ERROR_OBJECT_IN_THE_WAY);
00294
00295 TownID town = GetStatueTownID(tile);
00296 ClrBit(Town::Get(town)->statues, GetTileOwner(tile));
00297 SetWindowDirty(WC_TOWN_AUTHORITY, town);
00298 }
00299
00300 if (flags & DC_EXEC) {
00301 DoClearSquare(tile);
00302 }
00303
00304 return CommandCost();
00305 }
00306
00307 static void AddAcceptedCargo_Unmovable(TileIndex tile, CargoArray &acceptance, uint32 *always_accepted)
00308 {
00309 if (!IsCompanyHQ(tile)) return;
00310
00311
00312
00313
00314
00315 uint level = GetCompanyHQSize(tile) + 1;
00316
00317
00318
00319 acceptance[CT_PASSENGERS] += max(1U, level);
00320 SetBit(*always_accepted, CT_PASSENGERS);
00321
00322
00323
00324
00325
00326 acceptance[CT_MAIL] += max(1U, level / 2);
00327 SetBit(*always_accepted, CT_MAIL);
00328 }
00329
00330
00331 static void GetTileDesc_Unmovable(TileIndex tile, TileDesc *td)
00332 {
00333 td->str = GetUnmovableSpec(GetUnmovableType(tile))->name;
00334 td->owner[0] = GetTileOwner(tile);
00335 }
00336
00337 static void TileLoop_Unmovable(TileIndex tile)
00338 {
00339 if (!IsCompanyHQ(tile)) return;
00340
00341
00342
00343
00344
00345 uint level = GetCompanyHQSize(tile) + 1;
00346 assert(level < 6);
00347
00348 StationFinder stations(TileArea(tile, 2, 2));
00349
00350 uint r = Random();
00351
00352 if (GB(r, 0, 8) < (256 / 4 / (6 - level))) {
00353 uint amt = GB(r, 0, 8) / 8 / 4 + 1;
00354 if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
00355 MoveGoodsToStation(CT_PASSENGERS, amt, ST_HEADQUARTERS, GetTileOwner(tile), stations.GetStations());
00356 }
00357
00358
00359
00360
00361 if (GB(r, 8, 8) < (196 / 4 / (6 - level))) {
00362 uint amt = GB(r, 8, 8) / 8 / 4 + 1;
00363 if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
00364 MoveGoodsToStation(CT_MAIL, amt, ST_HEADQUARTERS, GetTileOwner(tile), stations.GetStations());
00365 }
00366 }
00367
00368
00369 static TrackStatus GetTileTrackStatus_Unmovable(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
00370 {
00371 return 0;
00372 }
00373
00374 static bool ClickTile_Unmovable(TileIndex tile)
00375 {
00376 if (!IsCompanyHQ(tile)) return false;
00377
00378 ShowCompany(GetTileOwner(tile));
00379 return true;
00380 }
00381
00382
00383
00384 static bool IsRadioTowerNearby(TileIndex tile)
00385 {
00386 TileIndex tile_s = tile - TileDiffXY(min(TileX(tile), 4U), min(TileY(tile), 4U));
00387 uint w = min(TileX(tile), 4U) + 1 + min(MapMaxX() - TileX(tile), 4U);
00388 uint h = min(TileY(tile), 4U) + 1 + min(MapMaxY() - TileY(tile), 4U);
00389
00390 TILE_LOOP(tile, w, h, tile_s) {
00391 if (IsTransmitterTile(tile)) return true;
00392 }
00393
00394 return false;
00395 }
00396
00397 void GenerateUnmovables()
00398 {
00399 if (_settings_game.game_creation.landscape == LT_TOYLAND) return;
00400
00401
00402 int radiotower_to_build = ScaleByMapSize(15);
00403 int lighthouses_to_build = _settings_game.game_creation.landscape == LT_TROPIC ? 0 : ScaleByMapSize1D((Random() & 3) + 7);
00404
00405
00406 if (_settings_game.construction.freeform_edges && lighthouses_to_build != 0) {
00407 uint num_water_tiles = 0;
00408 for (uint x = 0; x < MapMaxX(); x++) {
00409 if (IsTileType(TileXY(x, 1), MP_WATER)) num_water_tiles++;
00410 if (IsTileType(TileXY(x, MapMaxY() - 1), MP_WATER)) num_water_tiles++;
00411 }
00412 for (uint y = 1; y < MapMaxY() - 1; y++) {
00413 if (IsTileType(TileXY(1, y), MP_WATER)) num_water_tiles++;
00414 if (IsTileType(TileXY(MapMaxX() - 1, y), MP_WATER)) num_water_tiles++;
00415 }
00416
00417
00418 lighthouses_to_build = lighthouses_to_build * num_water_tiles / (2 * MapMaxY() + 2 * MapMaxX() - 6);
00419 }
00420
00421 SetGeneratingWorldProgress(GWP_UNMOVABLE, radiotower_to_build + lighthouses_to_build);
00422
00423 for (uint i = ScaleByMapSize(1000); i != 0; i--) {
00424 TileIndex tile = RandomTile();
00425
00426 uint h;
00427 if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h >= TILE_HEIGHT * 4 && !IsBridgeAbove(tile)) {
00428 if (IsRadioTowerNearby(tile)) continue;
00429
00430 MakeTransmitter(tile);
00431 IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
00432 if (--radiotower_to_build == 0) break;
00433 }
00434 }
00435
00436
00437 uint maxx = MapMaxX();
00438 uint maxy = MapMaxY();
00439 for (int loop_count = 0; loop_count < 1000 && lighthouses_to_build != 0; loop_count++) {
00440 uint r = Random();
00441
00442
00443 int perimeter = (GB(r, 16, 16) % (2 * (maxx + maxy))) - maxy;
00444 DiagDirection dir;
00445 for (dir = DIAGDIR_NE; perimeter > 0; dir++) {
00446 perimeter -= (DiagDirToAxis(dir) == AXIS_X) ? maxx : maxy;
00447 }
00448
00449 TileIndex tile;
00450 switch (dir) {
00451 default:
00452 case DIAGDIR_NE: tile = TileXY(maxx - 1, r % maxy); break;
00453 case DIAGDIR_SE: tile = TileXY(r % maxx, 1); break;
00454 case DIAGDIR_SW: tile = TileXY(1, r % maxy); break;
00455 case DIAGDIR_NW: tile = TileXY(r % maxx, maxy - 1); break;
00456 }
00457
00458
00459 if (!IsTileType(tile, MP_WATER)) continue;
00460
00461 for (int j = 0; j < 19; j++) {
00462 uint h;
00463 if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h <= TILE_HEIGHT * 2 && !IsBridgeAbove(tile)) {
00464 MakeLighthouse(tile);
00465 IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
00466 lighthouses_to_build--;
00467 assert(tile < MapSize());
00468 break;
00469 }
00470 tile = AddTileIndexDiffCWrap(tile, TileIndexDiffCByDiagDir(dir));
00471 if (tile == INVALID_TILE) break;
00472 }
00473 }
00474 }
00475
00476 static void ChangeTileOwner_Unmovable(TileIndex tile, Owner old_owner, Owner new_owner)
00477 {
00478 if (!IsTileOwner(tile, old_owner)) return;
00479
00480 if (IsOwnedLand(tile) && new_owner != INVALID_OWNER) {
00481 SetTileOwner(tile, new_owner);
00482 } else if (IsStatueTile(tile)) {
00483 TownID town = GetStatueTownID(tile);
00484 Town *t = Town::Get(town);
00485 ClrBit(t->statues, old_owner);
00486 if (new_owner != INVALID_OWNER && !HasBit(t->statues, new_owner)) {
00487
00488 SetBit(t->statues, new_owner);
00489 SetTileOwner(tile, new_owner);
00490 } else {
00491 DoClearSquare(tile);
00492 }
00493
00494 SetWindowDirty(WC_TOWN_AUTHORITY, town);
00495 } else {
00496 DoClearSquare(tile);
00497 }
00498 }
00499
00500 static CommandCost TerraformTile_Unmovable(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
00501 {
00502
00503 if (IsOwnedLand(tile) && CheckTileOwnership(tile)) return CommandCost();
00504
00505 if (AutoslopeEnabled() && (IsStatue(tile) || IsCompanyHQ(tile))) {
00506 if (!IsSteepSlope(tileh_new) && (z_new + GetSlopeMaxZ(tileh_new) == GetTileMaxZ(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00507 }
00508
00509 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00510 }
00511
00512 extern const TileTypeProcs _tile_type_unmovable_procs = {
00513 DrawTile_Unmovable,
00514 GetSlopeZ_Unmovable,
00515 ClearTile_Unmovable,
00516 AddAcceptedCargo_Unmovable,
00517 GetTileDesc_Unmovable,
00518 GetTileTrackStatus_Unmovable,
00519 ClickTile_Unmovable,
00520 NULL,
00521 TileLoop_Unmovable,
00522 ChangeTileOwner_Unmovable,
00523 NULL,
00524 NULL,
00525 GetFoundation_Unmovable,
00526 TerraformTile_Unmovable,
00527 };