00001
00002
00003
00004
00005
00006
00007
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
00025
00026
00027
00028
00029 static const int TERRAFORMER_MODHEIGHT_SIZE = 2 * MAX_TILE_HEIGHT * (MAX_TILE_HEIGHT + 1);
00030
00031
00032
00033
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
00075 return TileHeight(tile);
00076 }
00077
00085 static void TerraformSetHeightOfTile(TerraformerState *ts, TileIndex tile, int height)
00086 {
00087
00088
00089
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
00099 if (count == 0) {
00100 assert(ts->modheight_count < TERRAFORMER_MODHEIGHT_SIZE);
00101 ts->modheight_count++;
00102 }
00103
00104
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
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
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
00163
00164
00165
00166 if (height == TerraformGetHeightOfTile(ts, tile)) return CMD_ERROR;
00167
00168
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
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
00182 TerraformAddDirtyTileAround(ts, tile);
00183
00184
00185 TerraformSetHeightOfTile(ts, tile, height);
00186
00187 CommandCost total_cost(EXPENSES_CONSTRUCTION);
00188
00189
00190 total_cost.AddCost(_price[PR_TERRAFORM]);
00191
00192
00193 {
00194 const TileIndexDiffC *ttm;
00195
00196 TileIndex orig_tile = tile;
00197 static const TileIndexDiffC _terraform_tilepos[] = {
00198 { 1, 0},
00199 {-2, 0},
00200 { 1, 1},
00201 { 0, -2}
00202 };
00203
00204 for (ttm = _terraform_tilepos; ttm != endof(_terraform_tilepos); ttm++) {
00205 tile += ToTileIndexDiff(*ttm);
00206
00207 if (tile >= MapSize()) continue;
00208
00209 if (Delta(TileX(orig_tile), TileX(tile)) == MapSizeX() - 1) continue;
00210 if (Delta(TileY(orig_tile), TileY(tile)) == MapSizeY() - 1) continue;
00211
00212
00213 int r = TerraformGetHeightOfTile(ts, tile);
00214 int height_diff = height - r;
00215
00216
00217 if (abs(height_diff) > 1) {
00218
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
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
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
00286
00287 if (IsTileType(tile, MP_VOID)) continue;
00288
00289
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
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
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
00307 if (direction == 1 && MayHaveBridgeAbove(tile) && IsBridgeAbove(tile) &&
00308 GetBridgeHeight(GetSouthernBridgeEnd(tile)) <= z_max * TILE_HEIGHT) {
00309 _terraform_err_tile = tile;
00310 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00311 }
00312
00313 if (direction == -1 && IsTunnelInWay(tile, z_min * TILE_HEIGHT)) {
00314 _terraform_err_tile = tile;
00315 return_cmd_error(STR_ERROR_EXCAVATION_WOULD_DAMAGE);
00316 }
00317
00318 const bool curr_gen = _generating_world;
00319 if (_game_mode == GM_EDITOR) _generating_world = true;
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
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
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
00372 uint oldh = TileHeight(p1);
00373
00374
00375 uint h = oldh + p2;
00376
00377
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
00382 int ex = TileX(tile);
00383 int ey = TileY(tile);
00384 int sx = TileX(p1);
00385 int sy = TileY(p1);
00386 if (ex < sx) Swap(ex, sx);
00387 if (ey < sy) Swap(ey, sy);
00388 tile = TileXY(sx, sy);
00389
00390 int size_x = ex - sx + 1;
00391 int size_y = ey - sy + 1;
00392
00393 Money money = GetAvailableMoneyForCommand();
00394 CommandCost cost(EXPENSES_CONSTRUCTION);
00395
00396 TILE_LOOP(tile2, size_x, size_y, tile) {
00397 uint curh = TileHeight(tile2);
00398 while (curh != h) {
00399 CommandCost ret = DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND);
00400 if (CmdFailed(ret)) break;
00401
00402 if (flags & DC_EXEC) {
00403 money -= ret.GetCost();
00404 if (money < 0) {
00405 _additional_cash_required = ret.GetCost();
00406 return cost;
00407 }
00408 DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
00409 }
00410
00411 cost.AddCost(ret);
00412 curh += (curh > h) ? -1 : 1;
00413 }
00414 }
00415
00416 return (cost.GetCost() == 0) ? CMD_ERROR : cost;
00417 }