terraform_cmd.cpp

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

Generated on Wed Dec 23 20:12:52 2009 for OpenTTD by  doxygen 1.5.6