# unmovable_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: unmovable_cmd.cpp 19128 2010-02-14 15:30:08Z alberth $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    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; // reset HQ position
    00071     SetWindowDirty(WC_COMPANY, cid);
    00072
    00073     CargoPacket::InvalidateAllFrom(ST_HEADQUARTERS, cid);
    00074   }
    00075
    00076   /* cost of relocating company is 1% of company value */
    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) { // Moving HQ
    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   cost.AddCost(GetUnmovableSpec(UNMOVABLE_OWNED_LAND)->GetBuildingCost());
    00163   return cost;
    00164 }
    00165
    00175 CommandCost CmdSellLandArea(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00176 {
    00177   if (!IsOwnedLandTile(tile)) return CMD_ERROR;
    00178   if (!CheckTileOwnership(tile) && _current_company != OWNER_WATER) return CMD_ERROR;
    00179
    00180   if (!EnsureNoVehicleOnGround(tile)) return CMD_ERROR;
    00181
    00182   if (flags & DC_EXEC) DoClearSquare(tile);
    00183
    00184   return CommandCost(EXPENSES_CONSTRUCTION, - GetUnmovableSpec(UNMOVABLE_OWNED_LAND)->GetRemovalCost());
    00185 }
    00186
    00187 static Foundation GetFoundation_Unmovable(TileIndex tile, Slope tileh);
    00188
    00189 static void DrawTile_Unmovable(TileInfo *ti)
    00190 {
    00191   DrawFoundation(ti, GetFoundation_Unmovable(ti->tile, ti->tileh));
    00192   switch (GetUnmovableType(ti->tile)) {
    00193     default: NOT_REACHED();
    00194     case UNMOVABLE_TRANSMITTER:
    00195     case UNMOVABLE_LIGHTHOUSE: {
    00196       const DrawTileSeqStruct *dtu = &_draw_tile_transmitterlighthouse_data[GetUnmovableType(ti->tile)];
    00197
    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       DrawGroundSprite(SPR_CONCRETE_GROUND, PAL_NONE);
    00212
    00213       if (IsInvisibilitySet(TO_STRUCTURES)) break;
    00214
    00215       AddSortableSpriteToDraw(SPR_STATUE_COMPANY, COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile)), ti->x, ti->y, 16, 16, 25, ti->z, IsTransparencySet(TO_STRUCTURES));
    00216       break;
    00217
    00218     case UNMOVABLE_OWNED_LAND:
    00219       DrawClearLandTile(ti, 0);
    00220
    00221       AddSortableSpriteToDraw(
    00222         SPR_BOUGHT_LAND, COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile)),
    00223         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)
    00224       );
    00225       DrawBridgeMiddle(ti);
    00226       break;
    00227
    00228     case UNMOVABLE_HQ: {
    00229       assert(IsCompanyHQ(ti->tile));
    00230
    00231       PaletteID palette = COMPANY_SPRITE_COLOUR(GetTileOwner(ti->tile));
    00232
    00233       const DrawTileSprites *t = &_unmovable_display_datas[GetCompanyHQSize(ti->tile) << 2 | GetCompanyHQSection(ti->tile)];
    00234       DrawGroundSprite(t->ground.sprite, palette);
    00235
    00236       if (IsInvisibilitySet(TO_STRUCTURES)) break;
    00237
    00238       const DrawTileSeqStruct *dtss;
    00239       foreach_draw_tile_seq(dtss, t->seq) {
    00240         AddSortableSpriteToDraw(
    00241           dtss->image.sprite, palette,
    00242           ti->x + dtss->delta_x, ti->y + dtss->delta_y,
    00243           dtss->size_x, dtss->size_y,
    00244           dtss->size_z, ti->z + dtss->delta_z,
    00245           IsTransparencySet(TO_STRUCTURES)
    00246         );
    00247       }
    00248       break;
    00249     }
    00250   }
    00251 }
    00252
    00253 static uint GetSlopeZ_Unmovable(TileIndex tile, uint x, uint y)
    00254 {
    00255   if (IsOwnedLand(tile)) {
    00256     uint z;
    00257     Slope tileh = GetTileSlope(tile, &z);
    00258
    00259     return z + GetPartialZ(x & 0xF, y & 0xF, tileh);
    00260   } else {
    00261     return GetTileMaxZ(tile);
    00262   }
    00263 }
    00264
    00265 static Foundation GetFoundation_Unmovable(TileIndex tile, Slope tileh)
    00266 {
    00267   return IsOwnedLand(tile) ? FOUNDATION_NONE : FlatteningFoundation(tileh);
    00268 }
    00269
    00270 static CommandCost ClearTile_Unmovable(TileIndex tile, DoCommandFlag flags)
    00271 {
    00272   if (IsCompanyHQ(tile)) {
    00273     if (_current_company == OWNER_WATER) {
    00274       return DestroyCompanyHQ(GetTileOwner(tile), DC_EXEC);
    00275     } else {
    00276       return_cmd_error(flags & DC_AUTO ? STR_ERROR_COMPANY_HEADQUARTERS_IN : INVALID_STRING_ID);
    00277     }
    00278   }
    00279
    00280   if (IsOwnedLand(tile)) {
    00281     return DoCommand(tile, 0, 0, flags, CMD_SELL_LAND_AREA);
    00282   }
    00283
    00284   /* checks if you're allowed to remove unmovable things */
    00285   if (_game_mode != GM_EDITOR && _current_company != OWNER_WATER && ((flags & DC_AUTO) || !_cheats.magic_bulldozer.value) )
    00286     return_cmd_error(flags & DC_AUTO ? STR_ERROR_OBJECT_IN_THE_WAY : INVALID_STRING_ID);
    00287
    00288   if (IsStatue(tile)) {
    00289     if (flags & DC_AUTO) return_cmd_error(STR_ERROR_OBJECT_IN_THE_WAY);
    00290
    00291     if (flags & DC_EXEC) {
    00292       TownID town = GetStatueTownID(tile);
    00293       ClrBit(Town::Get(town)->statues, GetTileOwner(tile));
    00294       SetWindowDirty(WC_TOWN_AUTHORITY, town);
    00295     }
    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   /* HQ accepts passenger and mail; but we have to divide the values
    00310    * between 4 tiles it occupies! */
    00311
    00312   /* HQ level (depends on company performance) in the range 1..5. */
    00313   uint level = GetCompanyHQSize(tile) + 1;
    00314
    00315   /* Top town building generates 10, so to make HQ interesting, the top
    00316    * type makes 20. */
    00317   acceptance[CT_PASSENGERS] += max(1U, level);
    00318   SetBit(*always_accepted, CT_PASSENGERS);
    00319
    00320   /* Top town building generates 4, HQ can make up to 8. The
    00321    * proportion passengers:mail is different because such a huge
    00322    * commercial building generates unusually high amount of mail
    00323    * correspondence per physical visitor. */
    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   /* HQ accepts passenger and mail; but we have to divide the values
    00340    * between 4 tiles it occupies! */
    00341
    00342   /* HQ level (depends on company performance) in the range 1..5. */
    00343   uint level = GetCompanyHQSize(tile) + 1;
    00344   assert(level < 6);
    00345
    00346   StationFinder stations(TileArea(tile, 2, 2));
    00347
    00348   uint r = Random();
    00349   /* Top town buildings generate 250, so the top HQ type makes 256. */
    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   /* Top town building generates 90, HQ can make up to 196. The
    00357    * proportion passengers:mail is about the same as in the acceptance
    00358    * equations. */
    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 /* checks, if a radio tower is within a 9x9 tile square around tile */
    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   /* add radio tower */
    00400   int radiotower_to_build = ScaleByMapSize(15); // maximum number of radio towers on the map
    00401   int lighthouses_to_build = _settings_game.game_creation.landscape == LT_TROPIC ? 0 : ScaleByMapSize1D((Random() & 3) + 7);
    00402
    00403   /* Scale the amount of lighthouses with the amount of land at the borders. */
    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     /* The -6 is because the top borders are MP_VOID (-2) and all corners
    00415      * are counted twice (-4). */
    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   /* add lighthouses */
    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     /* Scatter the lighthouses more evenly around the perimeter */
    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     /* Only build lighthouses at tiles where the border is sea. */
    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       /* Transfer ownership to the new company */
    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   /* Owned land remains unsold */
    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,             // draw_tile_proc
    00512   GetSlopeZ_Unmovable,            // get_slope_z_proc
    00513   ClearTile_Unmovable,            // clear_tile_proc
    00514   AddAcceptedCargo_Unmovable,     // add_accepted_cargo_proc
    00515   GetTileDesc_Unmovable,          // get_tile_desc_proc
    00516   GetTileTrackStatus_Unmovable,   // get_tile_track_status_proc
    00517   ClickTile_Unmovable,            // click_tile_proc
    00518   NULL,                           // animate_tile_proc
    00519   TileLoop_Unmovable,             // tile_loop_clear
    00520   ChangeTileOwner_Unmovable,      // change_tile_owner_clear
    00521   NULL,                           // add_produced_cargo_proc
    00522   NULL,                           // vehicle_enter_tile_proc
    00523   GetFoundation_Unmovable,        // get_foundation_proc
    00524   TerraformTile_Unmovable,        // terraform_tile_proc
    00525 };