# terraform_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: terraform_cmd.cpp 18723 2010-01-04 18:49:27Z rubidium $ */
    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 "openttd.h"
    00014 #include "command_func.h"
    00015 #include "tunnel_map.h"
    00016 #include "bridge_map.h"
    00017 #include "variables.h"
    00018 #include "functions.h"
    00019 #include "economy_func.h"
    00020
    00021 #include "table/strings.h"
    00022
    00023 /*
    00024  * In one terraforming command all four corners of a initial tile can be raised/lowered (though this is not available to the player).
    00025  * The maximal amount of height modifications is archieved when raising a complete flat land from sea level to MAX_TILE_HEIGHT or vice versa.
    00026  * This affects all corners with a manhatten distance smaller than MAX_TILE_HEIGHT to one of the initial 4 corners.
    00027  * Their maximal amount is computed to 4 * \sum_{i=1}^{h_max} i  =  2 * h_max * (h_max + 1).
    00028  */
    00029 static const int TERRAFORMER_MODHEIGHT_SIZE = 2 * MAX_TILE_HEIGHT * (MAX_TILE_HEIGHT + 1);
    00030
    00031 /*
    00032  * The maximal amount of affected tiles (i.e. the tiles that incident with one of the corners above, is computed similiar to
    00033  * 1 + 4 * \sum_{i=1}^{h_max} (i+1)  =  1 + 2 * h_max + (h_max + 3).
    00034  */
    00035 static const int TERRAFORMER_TILE_TABLE_SIZE = 1 + 2 * MAX_TILE_HEIGHT * (MAX_TILE_HEIGHT + 3);
    00036
    00037 struct TerraformerHeightMod {
    00038   TileIndex tile;
    00039   byte height;
    00040 };
    00041
    00042 struct TerraformerState {
    00043   int modheight_count;
    00044   int tile_table_count;
    00045
    00053   TileIndex tile_table[TERRAFORMER_TILE_TABLE_SIZE];
    00054   TerraformerHeightMod modheight[TERRAFORMER_MODHEIGHT_SIZE];
    00055 };
    00056
    00057 TileIndex _terraform_err_tile;
    00058
    00066 static int TerraformGetHeightOfTile(const TerraformerState *ts, TileIndex tile)
    00067 {
    00068   const TerraformerHeightMod *mod = ts->modheight;
    00069
    00070   for (int count = ts->modheight_count; count != 0; count--, mod++) {
    00071     if (mod->tile == tile) return mod->height;
    00072   }
    00073
    00074   /* TileHeight unchanged so far, read value from map. */
    00075   return TileHeight(tile);
    00076 }
    00077
    00085 static void TerraformSetHeightOfTile(TerraformerState *ts, TileIndex tile, int height)
    00086 {
    00087   /* Find tile in the "modheight" table.
    00088    * Note: In a normal user-terraform command the tile will not be found in the "modheight" table.
    00089    *       But during house- or industry-construction multiple corners can be terraformed at once. */
    00090   TerraformerHeightMod *mod = ts->modheight;
    00091   int count = ts->modheight_count;
    00092
    00093   while ((count > 0) && (mod->tile != tile)) {
    00094     mod++;
    00095     count--;
    00096   }
    00097
    00098   /* New entry? */
    00099   if (count == 0) {
    00100     assert(ts->modheight_count < TERRAFORMER_MODHEIGHT_SIZE);
    00101     ts->modheight_count++;
    00102   }
    00103
    00104   /* Finally store the new value */
    00105   mod->tile = tile;
    00106   mod->height = (byte)height;
    00107 }
    00108
    00116 static void TerraformAddDirtyTile(TerraformerState *ts, TileIndex tile)
    00117 {
    00118   int count = ts->tile_table_count;
    00119
    00120   for (TileIndex *t = ts->tile_table; count != 0; count--, t++) {
    00121     if (*t == tile) return;
    00122   }
    00123
    00124   assert(ts->tile_table_count < TERRAFORMER_TILE_TABLE_SIZE);
    00125
    00126   ts->tile_table[ts->tile_table_count++] = tile;
    00127 }
    00128
    00136 static void TerraformAddDirtyTileAround(TerraformerState *ts, TileIndex tile)
    00137 {
    00138   /* Make sure all tiles passed to TerraformAddDirtyTile are within [0, MapSize()] */
    00139   if (TileY(tile) >= 1) TerraformAddDirtyTile(ts, tile + TileDiffXY( 0, -1));
    00140   if (TileY(tile) >= 1 && TileX(tile) >= 1) TerraformAddDirtyTile(ts, tile + TileDiffXY(-1, -1));
    00141   if (TileX(tile) >= 1) TerraformAddDirtyTile(ts, tile + TileDiffXY(-1,  0));
    00142   TerraformAddDirtyTile(ts, tile);
    00143 }
    00144
    00153 static CommandCost TerraformTileHeight(TerraformerState *ts, TileIndex tile, int height)
    00154 {
    00155   assert(tile < MapSize());
    00156
    00157   /* Check range of destination height */
    00158   if (height < 0) return_cmd_error(STR_ERROR_ALREADY_AT_SEA_LEVEL);
    00159   if (height > MAX_TILE_HEIGHT) return_cmd_error(STR_ERROR_TOO_HIGH);
    00160
    00161   /*
    00162    * Check if the terraforming has any effect.
    00163    * This can only be true, if multiple corners of the start-tile are terraformed (i.e. the terraforming is done by towns/industries etc.).
    00164    * In this case the terraforming should fail. (Don't know why.)
    00165    */
    00166   if (height == TerraformGetHeightOfTile(ts, tile)) return CMD_ERROR;
    00167
    00168   /* Check "too close to edge of map". Only possible when freeform-edges is off. */
    00169   uint x = TileX(tile);
    00170   uint y = TileY(tile);
    00171   if (!_settings_game.construction.freeform_edges && ((x <= 1) || (y <= 1) || (x >= MapMaxX() - 1) || (y >= MapMaxY() - 1))) {
    00172     /*
    00173      * Determine a sensible error tile
    00174      */
    00175     if (x == 1) x = 0;
    00176     if (y == 1) y = 0;
    00177     _terraform_err_tile = TileXY(x, y);
    00178     return_cmd_error(STR_ERROR_TOO_CLOSE_TO_EDGE_OF_MAP);
    00179   }
    00180
    00181   /* Mark incident tiles, that are involved in the terraforming */
    00182   TerraformAddDirtyTileAround(ts, tile);
    00183
    00184   /* Store the height modification */
    00185   TerraformSetHeightOfTile(ts, tile, height);
    00186
    00187   CommandCost total_cost(EXPENSES_CONSTRUCTION);
    00188
    00189   /* Increment cost */
    00190   total_cost.AddCost(_price[PR_TERRAFORM]);
    00191
    00192   /* Recurse to neighboured corners if height difference is larger than 1 */
    00193   {
    00194     const TileIndexDiffC *ttm;
    00195
    00196     TileIndex orig_tile = tile;
    00197     static const TileIndexDiffC _terraform_tilepos[] = {
    00198       { 1,  0}, // move to tile in SE
    00199       {-2,  0}, // undo last move, and move to tile in NW
    00200       { 1,  1}, // undo last move, and move to tile in SW
    00201       { 0, -2}  // undo last move, and move to tile in NE
    00202     };
    00203
    00204     for (ttm = _terraform_tilepos; ttm != endof(_terraform_tilepos); ttm++) {
    00205       tile += ToTileIndexDiff(*ttm);
    00206
    00207       if (tile >= MapSize()) continue;
    00208       /* Make sure we don't wrap around the map */
    00209       if (Delta(TileX(orig_tile), TileX(tile)) == MapSizeX() - 1) continue;
    00210       if (Delta(TileY(orig_tile), TileY(tile)) == MapSizeY() - 1) continue;
    00211
    00212       /* Get TileHeight of neighboured tile as of current terraform progress */
    00213       int r = TerraformGetHeightOfTile(ts, tile);
    00214       int height_diff = height - r;
    00215
    00216       /* Is the height difference to the neighboured corner greater than 1? */
    00217       if (abs(height_diff) > 1) {
    00218         /* Terraform the neighboured corner. The resulting height difference should be 1. */
    00219         height_diff += (height_diff < 0 ? 1 : -1);
    00220         CommandCost cost = TerraformTileHeight(ts, tile, r + height_diff);
    00221         if (CmdFailed(cost)) return cost;
    00222         total_cost.AddCost(cost);
    00223       }
    00224     }
    00225   }
    00226
    00227   return total_cost;
    00228 }
    00229
    00238 CommandCost CmdTerraformLand(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00239 {
    00240   _terraform_err_tile = INVALID_TILE;
    00241
    00242   CommandCost total_cost(EXPENSES_CONSTRUCTION);
    00243   int direction = (p2 != 0 ? 1 : -1);
    00244   TerraformerState ts;
    00245
    00246   ts.modheight_count = ts.tile_table_count = 0;
    00247
    00248   /* Compute the costs and the terraforming result in a model of the landscape */
    00249   if ((p1 & SLOPE_W) != 0 && tile + TileDiffXY(1, 0) < MapSize()) {
    00250     TileIndex t = tile + TileDiffXY(1, 0);
    00251     CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
    00252     if (CmdFailed(cost)) return cost;
    00253     total_cost.AddCost(cost);
    00254   }
    00255
    00256   if ((p1 & SLOPE_S) != 0 && tile + TileDiffXY(1, 1) < MapSize()) {
    00257     TileIndex t = tile + TileDiffXY(1, 1);
    00258     CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
    00259     if (CmdFailed(cost)) return cost;
    00260     total_cost.AddCost(cost);
    00261   }
    00262
    00263   if ((p1 & SLOPE_E) != 0 && tile + TileDiffXY(0, 1) < MapSize()) {
    00264     TileIndex t = tile + TileDiffXY(0, 1);
    00265     CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
    00266     if (CmdFailed(cost)) return cost;
    00267     total_cost.AddCost(cost);
    00268   }
    00269
    00270   if ((p1 & SLOPE_N) != 0) {
    00271     TileIndex t = tile + TileDiffXY(0, 0);
    00272     CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
    00273     if (CmdFailed(cost)) return cost;
    00274     total_cost.AddCost(cost);
    00275   }
    00276
    00277   /* Check if the terraforming is valid wrt. tunnels, bridges and objects on the surface */
    00278   {
    00279     TileIndex *ti = ts.tile_table;
    00280
    00281     for (int count = ts.tile_table_count; count != 0; count--, ti++) {
    00282       TileIndex tile = *ti;
    00283
    00284       assert(tile < MapSize());
    00285       /* MP_VOID tiles can be terraformed but as tunnels and bridges
    00286        * cannot go under / over these tiles they don't need checking. */
    00287       if (IsTileType(tile, MP_VOID)) continue;
    00288
    00289       /* Find new heights of tile corners */
    00290       uint z_N = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 0));
    00291       uint z_W = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 0));
    00292       uint z_S = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 1));
    00293       uint z_E = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 1));
    00294
    00295       /* Find min and max height of tile */
    00296       uint z_min = min(min(z_N, z_W), min(z_S, z_E));
    00297       uint z_max = max(max(z_N, z_W), max(z_S, z_E));
    00298
    00299       /* Compute tile slope */
    00300       Slope tileh = (z_max > z_min + 1 ? SLOPE_STEEP : SLOPE_FLAT);
    00301       if (z_W > z_min) tileh |= SLOPE_W;
    00302       if (z_S > z_min) tileh |= SLOPE_S;
    00303       if (z_E > z_min) tileh |= SLOPE_E;
    00304       if (z_N > z_min) tileh |= SLOPE_N;
    00305
    00306       /* Check if bridge would take damage */
    00307       if (direction == 1 && MayHaveBridgeAbove(tile) && IsBridgeAbove(tile) &&
    00308           GetBridgeHeight(GetSouthernBridgeEnd(tile)) <= z_max * TILE_HEIGHT) {
    00309         _terraform_err_tile = tile; // highlight the tile under the bridge
    00310         return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
    00311       }
    00312       /* Check if tunnel would take damage */
    00313       if (direction == -1 && IsTunnelInWay(tile, z_min * TILE_HEIGHT)) {
    00314         _terraform_err_tile = tile; // highlight the tile above the tunnel
    00315         return_cmd_error(STR_ERROR_EXCAVATION_WOULD_DAMAGE);
    00316       }
    00317       /* Check tiletype-specific things, and add extra-cost */
    00318       const bool curr_gen = _generating_world;
    00319       if (_game_mode == GM_EDITOR) _generating_world = true; // used to create green terraformed land
    00320       CommandCost cost = _tile_type_procs[GetTileType(tile)]->terraform_tile_proc(tile, flags | DC_AUTO, z_min * TILE_HEIGHT, tileh);
    00321       _generating_world = curr_gen;
    00322       if (CmdFailed(cost)) {
    00323         _terraform_err_tile = tile;
    00324         return cost;
    00325       }
    00326       total_cost.AddCost(cost);
    00327     }
    00328   }
    00329
    00330   if (flags & DC_EXEC) {
    00331     /* change the height */
    00332     {
    00333       int count;
    00334       TerraformerHeightMod *mod;
    00335
    00336       mod = ts.modheight;
    00337       for (count = ts.modheight_count; count != 0; count--, mod++) {
    00338         TileIndex til = mod->tile;
    00339
    00340         SetTileHeight(til, mod->height);
    00341       }
    00342     }
    00343
    00344     /* finally mark the dirty tiles dirty */
    00345     {
    00346       int count;
    00347       TileIndex *ti = ts.tile_table;
    00348       for (count = ts.tile_table_count; count != 0; count--, ti++) {
    00349         MarkTileDirtyByTile(*ti);
    00350       }
    00351     }
    00352   }
    00353   return total_cost;
    00354 }
    00355
    00356
    00365 CommandCost CmdLevelLand(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    00366 {
    00367   if (p1 >= MapSize()) return CMD_ERROR;
    00368
    00369   _terraform_err_tile = INVALID_TILE;
    00370
    00371   /* remember level height */
    00372   uint oldh = TileHeight(p1);
    00373
    00374   /* compute new height */
    00375   uint h = oldh + p2;
    00376
    00377   /* Check range of destination height */
    00378   if (h > MAX_TILE_HEIGHT) return_cmd_error((oldh == 0) ? STR_ERROR_ALREADY_AT_SEA_LEVEL : STR_ERROR_TOO_HIGH);
    00379   if (p2 == 0) _error_message = STR_ERROR_ALREADY_LEVELLED;
    00380
    00381   Money money = GetAvailableMoneyForCommand();
    00382   CommandCost cost(EXPENSES_CONSTRUCTION);
    00383
    00384   TileArea ta(tile, p1);
    00385   TILE_AREA_LOOP(tile, ta) {
    00386     uint curh = TileHeight(tile);
    00387     while (curh != h) {
    00388       CommandCost ret = DoCommand(tile, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND);
    00389       if (CmdFailed(ret)) break;
    00390
    00391       if (flags & DC_EXEC) {
    00392         money -= ret.GetCost();
    00393         if (money < 0) {
    00394           _additional_cash_required = ret.GetCost();
    00395           return cost;
    00396         }
    00397         DoCommand(tile, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
    00398       }
    00399
    00400       cost.AddCost(ret);
    00401       curh += (curh > h) ? -1 : 1;
    00402     }
    00403   }
    00404
    00405   return (cost.GetCost() == 0) ? CMD_ERROR : cost;
    00406 }