# terraform_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: terraform_cmd.cpp 11828 2008-01-13 01:21:35Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "strings_type.h"
    00008 #include "command_func.h"
    00009 #include "tile_map.h"
    00010 #include "tunnel_map.h"
    00011 #include "bridge_map.h"
    00012 #include "variables.h"
    00013 #include "functions.h"
    00014 #include "economy_func.h"
    00015
    00016 #include "table/strings.h"
    00017
    00018 /*
    00019  * In one terraforming command all four corners of a initial tile can be raised/lowered (though this is not available to the player).
    00020  * The maximal amount of height modifications is archieved when raising a complete flat land from sea level to MAX_TILE_HEIGHT or vice versa.
    00021  * This affects all corners with a manhatten distance smaller than MAX_TILE_HEIGHT to one of the initial 4 corners.
    00022  * Their maximal amount is computed to 4 * \sum_{i=1}^{h_max} i  =  2 * h_max * (h_max + 1).
    00023  */
    00024 static const int TERRAFORMER_MODHEIGHT_SIZE = 2 * MAX_TILE_HEIGHT * (MAX_TILE_HEIGHT + 1);
    00025
    00026 /*
    00027  * The maximal amount of affected tiles (i.e. the tiles that incident with one of the corners above, is computed similiar to
    00028  * 1 + 4 * \sum_{i=1}^{h_max} (i+1)  =  1 + 2 * h_max + (h_max + 3).
    00029  */
    00030 static const int TERRAFORMER_TILE_TABLE_SIZE = 1 + 2 * MAX_TILE_HEIGHT * (MAX_TILE_HEIGHT + 3);
    00031
    00032 struct TerraformerHeightMod {
    00033   TileIndex tile;
    00034   byte height;
    00035 };
    00036
    00037 struct TerraformerState {
    00038   int modheight_count;
    00039   int tile_table_count;
    00040
    00048   TileIndex tile_table[TERRAFORMER_TILE_TABLE_SIZE];
    00049   TerraformerHeightMod modheight[TERRAFORMER_MODHEIGHT_SIZE];
    00050 };
    00051
    00052 TileIndex _terraform_err_tile;
    00053
    00061 static int TerraformGetHeightOfTile(TerraformerState *ts, TileIndex tile)
    00062 {
    00063   TerraformerHeightMod *mod = ts->modheight;
    00064   int count;
    00065
    00066   for (count = ts->modheight_count; count != 0; count--, mod++) {
    00067     if (mod->tile == tile) return mod->height;
    00068   }
    00069
    00070   /* TileHeight unchanged so far, read value from map. */
    00071   return TileHeight(tile);
    00072 }
    00073
    00081 static void TerraformSetHeightOfTile(TerraformerState *ts, TileIndex tile, int height)
    00082 {
    00083   /* Find tile in the "modheight" table.
    00084    * Note: In a normal user-terraform command the tile will not be found in the "modheight" table.
    00085    *       But during house- or industry-construction multiple corners can be terraformed at once. */
    00086   TerraformerHeightMod *mod = ts->modheight;
    00087   int count = ts->modheight_count;
    00088   while ((count > 0) && (mod->tile != tile)) {
    00089     mod++;
    00090     count--;
    00091   }
    00092
    00093   /* New entry? */
    00094   if (count == 0) {
    00095     assert(ts->modheight_count < TERRAFORMER_MODHEIGHT_SIZE);
    00096     ts->modheight_count++;
    00097   }
    00098
    00099   /* Finally store the new value */
    00100   mod->tile = tile;
    00101   mod->height = (byte)height;
    00102 }
    00103
    00111 static void TerraformAddDirtyTile(TerraformerState *ts, TileIndex tile)
    00112 {
    00113   int count;
    00114   TileIndex *t;
    00115
    00116   count = ts->tile_table_count;
    00117
    00118   for (t = ts->tile_table; count != 0; count--, t++) {
    00119     if (*t == tile) return;
    00120   }
    00121
    00122   assert(ts->tile_table_count < TERRAFORMER_TILE_TABLE_SIZE);
    00123
    00124   ts->tile_table[ts->tile_table_count++] = tile;
    00125 }
    00126
    00134 static void TerraformAddDirtyTileAround(TerraformerState *ts, TileIndex tile)
    00135 {
    00136   TerraformAddDirtyTile(ts, tile + TileDiffXY( 0, -1));
    00137   TerraformAddDirtyTile(ts, tile + TileDiffXY(-1, -1));
    00138   TerraformAddDirtyTile(ts, tile + TileDiffXY(-1,  0));
    00139   TerraformAddDirtyTile(ts, tile);
    00140 }
    00141
    00150 static CommandCost TerraformTileHeight(TerraformerState *ts, TileIndex tile, int height)
    00151 {
    00152   CommandCost total_cost(EXPENSES_CONSTRUCTION);
    00153
    00154   assert(tile < MapSize());
    00155
    00156   /* Check range of destination height */
    00157   if (height < 0) return_cmd_error(STR_1003_ALREADY_AT_SEA_LEVEL);
    00158   if (height > MAX_TILE_HEIGHT) return_cmd_error(STR_1004_TOO_HIGH);
    00159
    00160   /*
    00161    * Check if the terraforming has any effect.
    00162    * This can only be true, if multiple corners of the start-tile are terraformed (i.e. the terraforming is done by towns/industries etc.).
    00163    * In this case the terraforming should fail. (Don't know why.)
    00164    */
    00165   if (height == TerraformGetHeightOfTile(ts, tile)) return CMD_ERROR;
    00166
    00167   /* Check "too close to edge of map" */
    00168   uint x = TileX(tile);
    00169   uint y = TileY(tile);
    00170   if ((x <= 1) || (y <= 1) || (x >= MapMaxX() - 1) || (y >= MapMaxY() - 1)) {
    00171     /*
    00172      * Determine a sensible error tile
    00173      * Note: If x and y are both zero this will disable the error tile. (Tile 0 cannot be highlighted :( )
    00174      */
    00175     if ((x == 1) && (y != 0)) x = 0;
    00176     if ((y == 1) && (x != 0)) y = 0;
    00177     _terraform_err_tile = TileXY(x, y);
    00178     return_cmd_error(STR_0002_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   /* Increment cost */
    00188   total_cost.AddCost(_price.terraform);
    00189
    00190   /* Recurse to neighboured corners if height difference is larger than 1 */
    00191   {
    00192     const TileIndexDiffC *ttm;
    00193
    00194     static const TileIndexDiffC _terraform_tilepos[] = {
    00195       { 1,  0}, // move to tile in SE
    00196       {-2,  0}, // undo last move, and move to tile in NW
    00197       { 1,  1}, // undo last move, and move to tile in SW
    00198       { 0, -2}  // undo last move, and move to tile in NE
    00199     };
    00200
    00201     for (ttm = _terraform_tilepos; ttm != endof(_terraform_tilepos); ttm++) {
    00202       tile += ToTileIndexDiff(*ttm);
    00203
    00204       /* Get TileHeight of neighboured tile as of current terraform progress */
    00205       int r = TerraformGetHeightOfTile(ts, tile);
    00206       int height_diff = height - r;
    00207
    00208       /* Is the height difference to the neighboured corner greater than 1? */
    00209       if (abs(height_diff) > 1) {
    00210         /* Terraform the neighboured corner. The resulting height difference should be 1. */
    00211         height_diff += (height_diff < 0 ? 1 : -1);
    00212         CommandCost cost = TerraformTileHeight(ts, tile, r + height_diff);
    00213         if (CmdFailed(cost)) return cost;
    00214         total_cost.AddCost(cost);
    00215       }
    00216     }
    00217   }
    00218
    00219   return total_cost;
    00220 }
    00221
    00229 CommandCost CmdTerraformLand(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
    00230 {
    00231   TerraformerState ts;
    00232   CommandCost total_cost(EXPENSES_CONSTRUCTION);
    00233   int direction = (p2 != 0 ? 1 : -1);
    00234
    00235   _terraform_err_tile = 0;
    00236
    00237   ts.modheight_count = ts.tile_table_count = 0;
    00238
    00239   /* Make an extra check for map-bounds cause we add tiles to the originating tile */
    00240   if (tile + TileDiffXY(1, 1) >= MapSize()) return CMD_ERROR;
    00241
    00242   /* Compute the costs and the terraforming result in a model of the landscape */
    00243   if ((p1 & SLOPE_W) != 0) {
    00244     TileIndex t = tile + TileDiffXY(1, 0);
    00245     CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
    00246     if (CmdFailed(cost)) return cost;
    00247     total_cost.AddCost(cost);
    00248   }
    00249
    00250   if ((p1 & SLOPE_S) != 0) {
    00251     TileIndex t = tile + TileDiffXY(1, 1);
    00252     CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
    00253     if (CmdFailed(cost)) return cost;
    00254     total_cost.AddCost(cost);
    00255   }
    00256
    00257   if ((p1 & SLOPE_E) != 0) {
    00258     TileIndex t = tile + TileDiffXY(0, 1);
    00259     CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
    00260     if (CmdFailed(cost)) return cost;
    00261     total_cost.AddCost(cost);
    00262   }
    00263
    00264   if ((p1 & SLOPE_N) != 0) {
    00265     TileIndex t = tile + TileDiffXY(0, 0);
    00266     CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
    00267     if (CmdFailed(cost)) return cost;
    00268     total_cost.AddCost(cost);
    00269   }
    00270
    00271   /* Check if the terraforming is valid wrt. tunnels, bridges and objects on the surface */
    00272   {
    00273     int count;
    00274     TileIndex *ti = ts.tile_table;
    00275
    00276     for (count = ts.tile_table_count; count != 0; count--, ti++) {
    00277       TileIndex tile = *ti;
    00278
    00279       /* Find new heights of tile corners */
    00280       uint z_N = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 0));
    00281       uint z_W = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 0));
    00282       uint z_S = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 1));
    00283       uint z_E = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 1));
    00284
    00285       /* Find min and max height of tile */
    00286       uint z_min = min(min(z_N, z_W), min(z_S, z_E));
    00287       uint z_max = max(max(z_N, z_W), max(z_S, z_E));
    00288
    00289       /* Compute tile slope */
    00290       uint tileh = (z_max > z_min + 1 ? SLOPE_STEEP : SLOPE_FLAT);
    00291       if (z_W > z_min) tileh += SLOPE_W;
    00292       if (z_S > z_min) tileh += SLOPE_S;
    00293       if (z_E > z_min) tileh += SLOPE_E;
    00294       if (z_N > z_min) tileh += SLOPE_N;
    00295
    00296       /* Check if bridge would take damage */
    00297       if (direction == 1 && MayHaveBridgeAbove(tile) && IsBridgeAbove(tile) &&
    00298           GetBridgeHeight(GetSouthernBridgeEnd(tile)) <= z_max * TILE_HEIGHT) {
    00299         _terraform_err_tile = tile; // highlight the tile under the bridge
    00300         return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
    00301       }
    00302       /* Check if tunnel would take damage */
    00303       if (direction == -1 && IsTunnelInWay(tile, z_min * TILE_HEIGHT)) {
    00304         _terraform_err_tile = tile; // highlight the tile above the tunnel
    00305         return_cmd_error(STR_1002_EXCAVATION_WOULD_DAMAGE);
    00306       }
    00307       /* Check tiletype-specific things, and add extra-cost */
    00308       CommandCost cost = _tile_type_procs[GetTileType(tile)]->terraform_tile_proc(tile, flags | DC_AUTO, z_min * TILE_HEIGHT, (Slope) tileh);
    00309       if (CmdFailed(cost)) {
    00310         _terraform_err_tile = tile;
    00311         return cost;
    00312       }
    00313       total_cost.AddCost(cost);
    00314     }
    00315   }
    00316
    00317   if (flags & DC_EXEC) {
    00318     /* change the height */
    00319     {
    00320       int count;
    00321       TerraformerHeightMod *mod;
    00322
    00323       mod = ts.modheight;
    00324       for (count = ts.modheight_count; count != 0; count--, mod++) {
    00325         TileIndex til = mod->tile;
    00326
    00327         SetTileHeight(til, mod->height);
    00328       }
    00329     }
    00330
    00331     /* finally mark the dirty tiles dirty */
    00332     {
    00333       int count;
    00334       TileIndex *ti = ts.tile_table;
    00335       for (count = ts.tile_table_count; count != 0; count--, ti++) {
    00336         MarkTileDirtyByTile(*ti);
    00337       }
    00338     }
    00339   }
    00340   return total_cost;
    00341 }
    00342
    00343
    00351 CommandCost CmdLevelLand(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
    00352 {
    00353   int size_x, size_y;
    00354   int ex;
    00355   int ey;
    00356   int sx, sy;
    00357   uint h, oldh, curh;
    00358   CommandCost money;
    00359   CommandCost ret;
    00360   CommandCost cost(EXPENSES_CONSTRUCTION);
    00361
    00362   if (p1 >= MapSize()) return CMD_ERROR;
    00363
    00364   /* remember level height */
    00365   oldh = TileHeight(p1);
    00366
    00367   /* compute new height */
    00368   h = oldh + p2;
    00369
    00370   /* Check range of destination height */
    00371   if (h > MAX_TILE_HEIGHT) return_cmd_error((oldh == 0) ? STR_1003_ALREADY_AT_SEA_LEVEL : STR_1004_TOO_HIGH);
    00372
    00373   /* make sure sx,sy are smaller than ex,ey */
    00374   ex = TileX(tile);
    00375   ey = TileY(tile);
    00376   sx = TileX(p1);
    00377   sy = TileY(p1);
    00378   if (ex < sx) Swap(ex, sx);
    00379   if (ey < sy) Swap(ey, sy);
    00380   tile = TileXY(sx, sy);
    00381
    00382   size_x = ex - sx + 1;
    00383   size_y = ey - sy + 1;
    00384
    00385   money.AddCost(GetAvailableMoneyForCommand());
    00386
    00387   BEGIN_TILE_LOOP(tile2, size_x, size_y, tile) {
    00388     curh = TileHeight(tile2);
    00389     while (curh != h) {
    00390       ret = DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND);
    00391       if (CmdFailed(ret)) break;
    00392
    00393       if (flags & DC_EXEC) {
    00394         money.AddCost(-ret.GetCost());
    00395         if (money.GetCost() < 0) {
    00396           _additional_cash_required = ret.GetCost();
    00397           return cost;
    00398         }
    00399         DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
    00400       }
    00401
    00402       cost.AddCost(ret);
    00403       curh += (curh > h) ? -1 : 1;
    00404     }
    00405   } END_TILE_LOOP(tile2, size_x, size_y, tile)
    00406
    00407   return (cost.GetCost() == 0) ? CMD_ERROR : cost;
    00408 }