terraform_cmd.cpp

Go to the documentation of this file.
00001 /* $Id: terraform_cmd.cpp 14754 2008-12-28 14:37:19Z 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(const TerraformerState *ts, TileIndex tile)
00062 {
00063   const TerraformerHeightMod *mod = ts->modheight;
00064 
00065   for (int count = ts->modheight_count; count != 0; count--, mod++) {
00066     if (mod->tile == tile) return mod->height;
00067   }
00068 
00069   /* TileHeight unchanged so far, read value from map. */
00070   return TileHeight(tile);
00071 }
00072 
00080 static void TerraformSetHeightOfTile(TerraformerState *ts, TileIndex tile, int height)
00081 {
00082   /* Find tile in the "modheight" table.
00083    * Note: In a normal user-terraform command the tile will not be found in the "modheight" table.
00084    *       But during house- or industry-construction multiple corners can be terraformed at once. */
00085   TerraformerHeightMod *mod = ts->modheight;
00086   int count = ts->modheight_count;
00087 
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 = ts->tile_table_count;
00114 
00115   for (TileIndex *t = ts->tile_table; count != 0; count--, t++) {
00116     if (*t == tile) return;
00117   }
00118 
00119   assert(ts->tile_table_count < TERRAFORMER_TILE_TABLE_SIZE);
00120 
00121   ts->tile_table[ts->tile_table_count++] = tile;
00122 }
00123 
00131 static void TerraformAddDirtyTileAround(TerraformerState *ts, TileIndex tile)
00132 {
00133   TerraformAddDirtyTile(ts, tile + TileDiffXY( 0, -1));
00134   TerraformAddDirtyTile(ts, tile + TileDiffXY(-1, -1));
00135   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" */
00163   uint x = TileX(tile);
00164   uint y = TileY(tile);
00165   if ((x <= 1) || (y <= 1) || (x >= MapMaxX() - 1) || (y >= MapMaxY() - 1)) {
00166     /*
00167      * Determine a sensible error tile
00168      * Note: If x and y are both zero this will disable the error tile. (Tile 0 cannot be highlighted :( )
00169      */
00170     if ((x == 1) && (y != 0)) x = 0;
00171     if ((y == 1) && (x != 0)) y = 0;
00172     _terraform_err_tile = TileXY(x, y);
00173     return_cmd_error(STR_0002_TOO_CLOSE_TO_EDGE_OF_MAP);
00174   }
00175 
00176   /* Mark incident tiles, that are involved in the terraforming */
00177   TerraformAddDirtyTileAround(ts, tile);
00178 
00179   /* Store the height modification */
00180   TerraformSetHeightOfTile(ts, tile, height);
00181 
00182   CommandCost total_cost(EXPENSES_CONSTRUCTION);
00183 
00184   /* Increment cost */
00185   total_cost.AddCost(_price.terraform);
00186 
00187   /* Recurse to neighboured corners if height difference is larger than 1 */
00188   {
00189     const TileIndexDiffC *ttm;
00190 
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       /* Get TileHeight of neighboured tile as of current terraform progress */
00202       int r = TerraformGetHeightOfTile(ts, tile);
00203       int height_diff = height - r;
00204 
00205       /* Is the height difference to the neighboured corner greater than 1? */
00206       if (abs(height_diff) > 1) {
00207         /* Terraform the neighboured corner. The resulting height difference should be 1. */
00208         height_diff += (height_diff < 0 ? 1 : -1);
00209         CommandCost cost = TerraformTileHeight(ts, tile, r + height_diff);
00210         if (CmdFailed(cost)) return cost;
00211         total_cost.AddCost(cost);
00212       }
00213     }
00214   }
00215 
00216   return total_cost;
00217 }
00218 
00226 CommandCost CmdTerraformLand(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, const char *text)
00227 {
00228   /* Make an extra check for map-bounds cause we add tiles to the originating tile */
00229   if (tile + TileDiffXY(1, 1) >= MapSize()) return CMD_ERROR;
00230 
00231   _terraform_err_tile = INVALID_TILE;
00232 
00233   CommandCost total_cost(EXPENSES_CONSTRUCTION);
00234   int direction = (p2 != 0 ? 1 : -1);
00235   TerraformerState ts;
00236 
00237   ts.modheight_count = ts.tile_table_count = 0;
00238 
00239   /* Compute the costs and the terraforming result in a model of the landscape */
00240   if ((p1 & SLOPE_W) != 0) {
00241     TileIndex t = tile + TileDiffXY(1, 0);
00242     CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
00243     if (CmdFailed(cost)) return cost;
00244     total_cost.AddCost(cost);
00245   }
00246 
00247   if ((p1 & SLOPE_S) != 0) {
00248     TileIndex t = tile + TileDiffXY(1, 1);
00249     CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
00250     if (CmdFailed(cost)) return cost;
00251     total_cost.AddCost(cost);
00252   }
00253 
00254   if ((p1 & SLOPE_E) != 0) {
00255     TileIndex t = tile + TileDiffXY(0, 1);
00256     CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
00257     if (CmdFailed(cost)) return cost;
00258     total_cost.AddCost(cost);
00259   }
00260 
00261   if ((p1 & SLOPE_N) != 0) {
00262     TileIndex t = tile + TileDiffXY(0, 0);
00263     CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
00264     if (CmdFailed(cost)) return cost;
00265     total_cost.AddCost(cost);
00266   }
00267 
00268   /* Check if the terraforming is valid wrt. tunnels, bridges and objects on the surface */
00269   {
00270     TileIndex *ti = ts.tile_table;
00271 
00272     for (int count = ts.tile_table_count; count != 0; count--, ti++) {
00273       TileIndex tile = *ti;
00274 
00275       /* Find new heights of tile corners */
00276       uint z_N = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 0));
00277       uint z_W = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 0));
00278       uint z_S = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 1));
00279       uint z_E = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 1));
00280 
00281       /* Find min and max height of tile */
00282       uint z_min = min(min(z_N, z_W), min(z_S, z_E));
00283       uint z_max = max(max(z_N, z_W), max(z_S, z_E));
00284 
00285       /* Compute tile slope */
00286       Slope tileh = (z_max > z_min + 1 ? SLOPE_STEEP : SLOPE_FLAT);
00287       if (z_W > z_min) tileh |= SLOPE_W;
00288       if (z_S > z_min) tileh |= SLOPE_S;
00289       if (z_E > z_min) tileh |= SLOPE_E;
00290       if (z_N > z_min) tileh |= SLOPE_N;
00291 
00292       /* Check if bridge would take damage */
00293       if (direction == 1 && MayHaveBridgeAbove(tile) && IsBridgeAbove(tile) &&
00294           GetBridgeHeight(GetSouthernBridgeEnd(tile)) <= z_max * TILE_HEIGHT) {
00295         _terraform_err_tile = tile; // highlight the tile under the bridge
00296         return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
00297       }
00298       /* Check if tunnel would take damage */
00299       if (direction == -1 && IsTunnelInWay(tile, z_min * TILE_HEIGHT)) {
00300         _terraform_err_tile = tile; // highlight the tile above the tunnel
00301         return_cmd_error(STR_1002_EXCAVATION_WOULD_DAMAGE);
00302       }
00303       /* Check tiletype-specific things, and add extra-cost */
00304       const bool curr_gen = _generating_world;
00305       if (_game_mode == GM_EDITOR) _generating_world = true; // used to create green terraformed land
00306       CommandCost cost = _tile_type_procs[GetTileType(tile)]->terraform_tile_proc(tile, flags | DC_AUTO, z_min * TILE_HEIGHT, tileh);
00307       _generating_world = curr_gen;
00308       if (CmdFailed(cost)) {
00309         _terraform_err_tile = tile;
00310         return cost;
00311       }
00312       total_cost.AddCost(cost);
00313     }
00314   }
00315 
00316   if (flags & DC_EXEC) {
00317     /* change the height */
00318     {
00319       int count;
00320       TerraformerHeightMod *mod;
00321 
00322       mod = ts.modheight;
00323       for (count = ts.modheight_count; count != 0; count--, mod++) {
00324         TileIndex til = mod->tile;
00325 
00326         SetTileHeight(til, mod->height);
00327       }
00328     }
00329 
00330     /* finally mark the dirty tiles dirty */
00331     {
00332       int count;
00333       TileIndex *ti = ts.tile_table;
00334       for (count = ts.tile_table_count; count != 0; count--, ti++) {
00335         MarkTileDirtyByTile(*ti);
00336       }
00337     }
00338   }
00339   return total_cost;
00340 }
00341 
00342 
00350 CommandCost CmdLevelLand(TileIndex tile, uint32 flags, uint32 p1, uint32 p2, const char *text)
00351 {
00352   if (p1 >= MapSize()) return CMD_ERROR;
00353 
00354   /* remember level height */
00355   uint oldh = TileHeight(p1);
00356 
00357   /* compute new height */
00358   uint h = oldh + p2;
00359 
00360   /* Check range of destination height */
00361   if (h > MAX_TILE_HEIGHT) return_cmd_error((oldh == 0) ? STR_1003_ALREADY_AT_SEA_LEVEL : STR_1004_TOO_HIGH);
00362   if (p2 == 0) _error_message = STR_ALREADY_LEVELLED;
00363 
00364   /* make sure sx,sy are smaller than ex,ey */
00365   int ex = TileX(tile);
00366   int ey = TileY(tile);
00367   int sx = TileX(p1);
00368   int sy = TileY(p1);
00369   if (ex < sx) Swap(ex, sx);
00370   if (ey < sy) Swap(ey, sy);
00371   tile = TileXY(sx, sy);
00372 
00373   int size_x = ex - sx + 1;
00374   int size_y = ey - sy + 1;
00375 
00376   Money money = GetAvailableMoneyForCommand();
00377   CommandCost cost(EXPENSES_CONSTRUCTION);
00378 
00379   BEGIN_TILE_LOOP(tile2, size_x, size_y, tile) {
00380     uint curh = TileHeight(tile2);
00381     while (curh != h) {
00382       CommandCost ret = DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND);
00383       if (CmdFailed(ret)) break;
00384 
00385       if (flags & DC_EXEC) {
00386         money -= ret.GetCost();
00387         if (money < 0) {
00388           _additional_cash_required = ret.GetCost();
00389           return cost;
00390         }
00391         DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
00392       }
00393 
00394       cost.AddCost(ret);
00395       curh += (curh > h) ? -1 : 1;
00396     }
00397   } END_TILE_LOOP(tile2, size_x, size_y, tile)
00398 
00399   return (cost.GetCost() == 0) ? CMD_ERROR : cost;
00400 }

Generated on Fri Jan 9 19:01:52 2009 for openttd by  doxygen 1.5.6