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