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 DrawFoundation(ti, GetFoundation_Unmovable(ti->tile, ti->tileh));
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 DrawClearLandTile(ti, 2);
00198
00199 if (IsInvisibilitySet(TO_STRUCTURES)) break;
00200
00201 AddSortableSpriteToDraw(
00202 dtu->image.sprite, PAL_NONE, ti->x | dtu->delta_x, ti->y | dtu->delta_y,
00203 dtu->size_x, dtu->size_y, dtu->size_z, ti->z,
00204 IsTransparencySet(TO_STRUCTURES)
00205 );
00206 break;
00207 }
00208
00209 case UNMOVABLE_STATUE:
00210 DrawGroundSprite(SPR_CONCRETE_GROUND, PAL_NONE);
00211
00212 if (IsInvisibilitySet(TO_STRUCTURES)) break;
00213
00214 AddSortableSpriteToDraw(SPR_STATUE_COMPANY, COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile)), ti->x, ti->y, 16, 16, 25, ti->z, IsTransparencySet(TO_STRUCTURES));
00215 break;
00216
00217 case UNMOVABLE_OWNED_LAND:
00218 DrawClearLandTile(ti, 0);
00219
00220 AddSortableSpriteToDraw(
00221 SPR_BOUGHT_LAND, COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile)),
00222 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)
00223 );
00224 DrawBridgeMiddle(ti);
00225 break;
00226
00227 case UNMOVABLE_HQ:{
00228 assert(IsCompanyHQ(ti->tile));
00229
00230 PaletteID palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
00231
00232 const DrawTileSprites *t = &_unmovable_display_datas[GetCompanyHQSize(ti->tile) << 2 | GetCompanyHQSection(ti->tile)];
00233 DrawGroundSprite(t->ground.sprite, palette);
00234
00235 if (IsInvisibilitySet(TO_STRUCTURES)) break;
00236
00237 const DrawTileSeqStruct *dtss;
00238 foreach_draw_tile_seq(dtss, t->seq) {
00239 AddSortableSpriteToDraw(
00240 dtss->image.sprite, palette,
00241 ti->x + dtss->delta_x, ti->y + dtss->delta_y,
00242 dtss->size_x, dtss->size_y,
00243 dtss->size_z, ti->z + dtss->delta_z,
00244 IsTransparencySet(TO_STRUCTURES)
00245 );
00246 }
00247 break;
00248 }
00249 }
00250 }
00251
00252 static uint GetSlopeZ_Unmovable(TileIndex tile, uint x, uint y)
00253 {
00254 if (IsOwnedLand(tile)) {
00255 uint z;
00256 Slope tileh = GetTileSlope(tile, &z);
00257
00258 return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
00259 } else {
00260 return GetTileMaxZ(tile);
00261 }
00262 }
00263
00264 static Foundation GetFoundation_Unmovable(TileIndex tile, Slope tileh)
00265 {
00266 return IsOwnedLand(tile) ? FOUNDATION_NONE : FlatteningFoundation(tileh);
00267 }
00268
00269 static CommandCost ClearTile_Unmovable(TileIndex tile, DoCommandFlag flags)
00270 {
00271 if (IsCompanyHQ(tile)) {
00272 if (_current_company == OWNER_WATER) {
00273 return DestroyCompanyHQ(GetTileOwner(tile), DC_EXEC);
00274 } else {
00275 return_cmd_error(flags & DC_AUTO ? STR_ERROR_COMPANY_HEADQUARTERS_IN : INVALID_STRING_ID);
00276 }
00277 }
00278
00279 if (IsOwnedLand(tile)) {
00280 return DoCommand(tile, 0, 0, flags, CMD_SELL_LAND_AREA);
00281 }
00282
00283
00284 if (_game_mode != GM_EDITOR && _current_company != OWNER_WATER && ((flags & DC_AUTO) || !_cheats.magic_bulldozer.value) )
00285 return_cmd_error(flags & DC_AUTO ? STR_ERROR_OBJECT_IN_THE_WAY : INVALID_STRING_ID);
00286
00287 if (IsStatue(tile)) {
00288 if (flags & DC_AUTO) return_cmd_error(STR_ERROR_OBJECT_IN_THE_WAY);
00289
00290 if (flags & DC_EXEC) {
00291 TownID town = GetStatueTownID(tile);
00292 ClrBit(Town::Get(town)->statues, GetTileOwner(tile));
00293 SetWindowDirty(WC_TOWN_AUTHORITY, town);
00294 }
00295 }
00296
00297 if (flags & DC_EXEC) {
00298 DoClearSquare(tile);
00299 }
00300
00301 return CommandCost();
00302 }
00303
00304 static void AddAcceptedCargo_Unmovable(TileIndex tile, CargoArray &acceptance, uint32 *always_accepted)
00305 {
00306 if (!IsCompanyHQ(tile)) return;
00307
00308
00309
00310
00311
00312 uint level = GetCompanyHQSize(tile) + 1;
00313
00314
00315
00316 acceptance[CT_PASSENGERS] += max(1U, level);
00317 SetBit(*always_accepted, CT_PASSENGERS);
00318
00319
00320
00321
00322
00323 acceptance[CT_MAIL] += max(1U, level / 2);
00324 SetBit(*always_accepted, CT_MAIL);
00325 }
00326
00327
00328 static void GetTileDesc_Unmovable(TileIndex tile, TileDesc *td)
00329 {
00330 td->str = GetUnmovableSpec(GetUnmovableType(tile))->name;
00331 td->owner[0] = GetTileOwner(tile);
00332 }
00333
00334 static void TileLoop_Unmovable(TileIndex tile)
00335 {
00336 if (!IsCompanyHQ(tile)) return;
00337
00338
00339
00340
00341
00342 uint level = GetCompanyHQSize(tile) + 1;
00343 assert(level < 6);
00344
00345 StationFinder stations(TileArea(tile, 2, 2));
00346
00347 uint r = Random();
00348
00349 if (GB(r, 0, 8) < (256 / 4 / (6 - level))) {
00350 uint amt = GB(r, 0, 8) / 8 / 4 + 1;
00351 if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
00352 MoveGoodsToStation(CT_PASSENGERS, amt, ST_HEADQUARTERS, GetTileOwner(tile), stations.GetStations());
00353 }
00354
00355
00356
00357
00358 if (GB(r, 8, 8) < (196 / 4 / (6 - level))) {
00359 uint amt = GB(r, 8, 8) / 8 / 4 + 1;
00360 if (_economy.fluct <= 0) amt = (amt + 1) >> 1;
00361 MoveGoodsToStation(CT_MAIL, amt, ST_HEADQUARTERS, GetTileOwner(tile), stations.GetStations());
00362 }
00363 }
00364
00365
00366 static TrackStatus GetTileTrackStatus_Unmovable(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
00367 {
00368 return 0;
00369 }
00370
00371 static bool ClickTile_Unmovable(TileIndex tile)
00372 {
00373 if (!IsCompanyHQ(tile)) return false;
00374
00375 ShowCompany(GetTileOwner(tile));
00376 return true;
00377 }
00378
00379
00380
00381 static bool IsRadioTowerNearby(TileIndex tile)
00382 {
00383 TileIndex tile_s = tile - TileDiffXY(min(TileX(tile), 4U), min(TileY(tile), 4U));
00384 uint w = min(TileX(tile), 4U) + 1 + min(MapMaxX() - TileX(tile), 4U);
00385 uint h = min(TileY(tile), 4U) + 1 + min(MapMaxY() - TileY(tile), 4U);
00386
00387 TILE_LOOP(tile, w, h, tile_s) {
00388 if (IsTransmitterTile(tile)) return true;
00389 }
00390
00391 return false;
00392 }
00393
00394 void GenerateUnmovables()
00395 {
00396 if (_settings_game.game_creation.landscape == LT_TOYLAND) return;
00397
00398
00399 int radiotower_to_build = ScaleByMapSize(15);
00400 int lighthouses_to_build = _settings_game.game_creation.landscape == LT_TROPIC ? 0 : ScaleByMapSize1D((Random() & 3) + 7);
00401
00402
00403 if (_settings_game.construction.freeform_edges && lighthouses_to_build != 0) {
00404 uint num_water_tiles = 0;
00405 for (uint x = 0; x < MapMaxX(); x++) {
00406 if (IsTileType(TileXY(x, 1), MP_WATER)) num_water_tiles++;
00407 if (IsTileType(TileXY(x, MapMaxY() - 1), MP_WATER)) num_water_tiles++;
00408 }
00409 for (uint y = 1; y < MapMaxY() - 1; y++) {
00410 if (IsTileType(TileXY(1, y), MP_WATER)) num_water_tiles++;
00411 if (IsTileType(TileXY(MapMaxX() - 1, y), MP_WATER)) num_water_tiles++;
00412 }
00413
00414
00415 lighthouses_to_build = lighthouses_to_build * num_water_tiles / (2 * MapMaxY() + 2 * MapMaxX() - 6);
00416 }
00417
00418 SetGeneratingWorldProgress(GWP_UNMOVABLE, radiotower_to_build + lighthouses_to_build);
00419
00420 for (uint i = ScaleByMapSize(1000); i != 0; i--) {
00421 TileIndex tile = RandomTile();
00422
00423 uint h;
00424 if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h >= TILE_HEIGHT * 4 && !IsBridgeAbove(tile)) {
00425 if (IsRadioTowerNearby(tile)) continue;
00426
00427 MakeTransmitter(tile);
00428 IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
00429 if (--radiotower_to_build == 0) break;
00430 }
00431 }
00432
00433
00434 uint maxx = MapMaxX();
00435 uint maxy = MapMaxY();
00436 for (int loop_count = 0; loop_count < 1000 && lighthouses_to_build != 0; loop_count++) {
00437 uint r = Random();
00438
00439
00440 int perimeter = (GB(r, 16, 16) % (2 * (maxx + maxy))) - maxy;
00441 DiagDirection dir;
00442 for (dir = DIAGDIR_NE; perimeter > 0; dir++) {
00443 perimeter -= (DiagDirToAxis(dir) == AXIS_X) ? maxx : maxy;
00444 }
00445
00446 TileIndex tile;
00447 switch (dir) {
00448 default:
00449 case DIAGDIR_NE: tile = TileXY(maxx - 1, r % maxy); break;
00450 case DIAGDIR_SE: tile = TileXY(r % maxx, 1); break;
00451 case DIAGDIR_SW: tile = TileXY(1, r % maxy); break;
00452 case DIAGDIR_NW: tile = TileXY(r % maxx, maxy - 1); break;
00453 }
00454
00455
00456 if (!IsTileType(tile, MP_WATER)) continue;
00457
00458 for (int j = 0; j < 19; j++) {
00459 uint h;
00460 if (IsTileType(tile, MP_CLEAR) && GetTileSlope(tile, &h) == SLOPE_FLAT && h <= TILE_HEIGHT * 2 && !IsBridgeAbove(tile)) {
00461 MakeLighthouse(tile);
00462 IncreaseGeneratingWorldProgress(GWP_UNMOVABLE);
00463 lighthouses_to_build--;
00464 assert(tile < MapSize());
00465 break;
00466 }
00467 tile = AddTileIndexDiffCWrap(tile, TileIndexDiffCByDiagDir(dir));
00468 if (tile == INVALID_TILE) break;
00469 }
00470 }
00471 }
00472
00473 static void ChangeTileOwner_Unmovable(TileIndex tile, Owner old_owner, Owner new_owner)
00474 {
00475 if (!IsTileOwner(tile, old_owner)) return;
00476
00477 if (IsOwnedLand(tile) && new_owner != INVALID_OWNER) {
00478 SetTileOwner(tile, new_owner);
00479 } else if (IsStatueTile(tile)) {
00480 TownID town = GetStatueTownID(tile);
00481 Town *t = Town::Get(town);
00482 ClrBit(t->statues, old_owner);
00483 if (new_owner != INVALID_OWNER && !HasBit(t->statues, new_owner)) {
00484
00485 SetBit(t->statues, new_owner);
00486 SetTileOwner(tile, new_owner);
00487 } else {
00488 DoClearSquare(tile);
00489 }
00490
00491 SetWindowDirty(WC_TOWN_AUTHORITY, town);
00492 } else {
00493 DoClearSquare(tile);
00494 }
00495 }
00496
00497 static CommandCost TerraformTile_Unmovable(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
00498 {
00499
00500 if (IsOwnedLand(tile) && CheckTileOwnership(tile)) return CommandCost();
00501
00502 if (AutoslopeEnabled() && (IsStatue(tile) || IsCompanyHQ(tile))) {
00503 if (!IsSteepSlope(tileh_new) && (z_new + GetSlopeMaxZ(tileh_new) == GetTileMaxZ(tile))) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
00504 }
00505
00506 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
00507 }
00508
00509 extern const TileTypeProcs _tile_type_unmovable_procs = {
00510 DrawTile_Unmovable,
00511 GetSlopeZ_Unmovable,
00512 ClearTile_Unmovable,
00513 AddAcceptedCargo_Unmovable,
00514 GetTileDesc_Unmovable,
00515 GetTileTrackStatus_Unmovable,
00516 ClickTile_Unmovable,
00517 NULL,
00518 TileLoop_Unmovable,
00519 ChangeTileOwner_Unmovable,
00520 NULL,
00521 NULL,
00522 GetFoundation_Unmovable,
00523 TerraformTile_Unmovable,
00524 };