00001
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
00019
00020
00021
00022
00023 static const int TERRAFORMER_MODHEIGHT_SIZE = 2 * MAX_TILE_HEIGHT * (MAX_TILE_HEIGHT + 1);
00024
00025
00026
00027
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
00069 return TileHeight(tile);
00070 }
00071
00079 static void TerraformSetHeightOfTile(TerraformerState *ts, TileIndex tile, int height)
00080 {
00081
00082
00083
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
00093 if (count == 0) {
00094 assert(ts->modheight_count < TERRAFORMER_MODHEIGHT_SIZE);
00095 ts->modheight_count++;
00096 }
00097
00098
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
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
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
00157
00158
00159
00160 if (height == TerraformGetHeightOfTile(ts, tile)) return CMD_ERROR;
00161
00162
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
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
00176 TerraformAddDirtyTileAround(ts, tile);
00177
00178
00179 TerraformSetHeightOfTile(ts, tile, height);
00180
00181 CommandCost total_cost(EXPENSES_CONSTRUCTION);
00182
00183
00184 total_cost.AddCost(_price.terraform);
00185
00186
00187 {
00188 const TileIndexDiffC *ttm;
00189
00190 TileIndex orig_tile = tile;
00191 static const TileIndexDiffC _terraform_tilepos[] = {
00192 { 1, 0},
00193 {-2, 0},
00194 { 1, 1},
00195 { 0, -2}
00196 };
00197
00198 for (ttm = _terraform_tilepos; ttm != endof(_terraform_tilepos); ttm++) {
00199 tile += ToTileIndexDiff(*ttm);
00200
00201 if (tile >= MapSize()) continue;
00202
00203 if (Delta(TileX(orig_tile), TileX(tile)) == MapSizeX() - 1) continue;
00204 if (Delta(TileY(orig_tile), TileY(tile)) == MapSizeY() - 1) continue;
00205
00206
00207 int r = TerraformGetHeightOfTile(ts, tile);
00208 int height_diff = height - r;
00209
00210
00211 if (abs(height_diff) > 1) {
00212
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
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
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
00279
00280 if (IsTileType(tile, MP_VOID)) continue;
00281
00282
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
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
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
00300 if (direction == 1 && MayHaveBridgeAbove(tile) && IsBridgeAbove(tile) &&
00301 GetBridgeHeight(GetSouthernBridgeEnd(tile)) <= z_max * TILE_HEIGHT) {
00302 _terraform_err_tile = tile;
00303 return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
00304 }
00305
00306 if (direction == -1 && IsTunnelInWay(tile, z_min * TILE_HEIGHT)) {
00307 _terraform_err_tile = tile;
00308 return_cmd_error(STR_1002_EXCAVATION_WOULD_DAMAGE);
00309 }
00310
00311 const bool curr_gen = _generating_world;
00312 if (_game_mode == GM_EDITOR) _generating_world = true;
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
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
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
00364 uint oldh = TileHeight(p1);
00365
00366
00367 uint h = oldh + p2;
00368
00369
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
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 }