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 "functions.h"
00018 #include "economy_func.h"
00019 #include "genworld.h"
00020 #include "object_base.h"
00021
00022 #include "table/strings.h"
00023
00024
00025
00026
00027
00028
00029
00030 static const int TERRAFORMER_MODHEIGHT_SIZE = 2 * MAX_TILE_HEIGHT * (MAX_TILE_HEIGHT + 1);
00031
00032
00033
00034
00035
00036 static const int TERRAFORMER_TILE_TABLE_SIZE = 1 + 2 * MAX_TILE_HEIGHT * (MAX_TILE_HEIGHT + 3);
00037
00038 struct TerraformerHeightMod {
00039 TileIndex tile;
00040 byte height;
00041 };
00042
00043 struct TerraformerState {
00044 int modheight_count;
00045 int tile_table_count;
00046
00054 TileIndex tile_table[TERRAFORMER_TILE_TABLE_SIZE];
00055 TerraformerHeightMod modheight[TERRAFORMER_MODHEIGHT_SIZE];
00056 };
00057
00058 TileIndex _terraform_err_tile;
00059
00067 static int TerraformGetHeightOfTile(const TerraformerState *ts, TileIndex tile)
00068 {
00069 const TerraformerHeightMod *mod = ts->modheight;
00070
00071 for (int count = ts->modheight_count; count != 0; count--, mod++) {
00072 if (mod->tile == tile) return mod->height;
00073 }
00074
00075
00076 return TileHeight(tile);
00077 }
00078
00086 static void TerraformSetHeightOfTile(TerraformerState *ts, TileIndex tile, int height)
00087 {
00088
00089
00090
00091 TerraformerHeightMod *mod = ts->modheight;
00092 int count = ts->modheight_count;
00093
00094 while ((count > 0) && (mod->tile != tile)) {
00095 mod++;
00096 count--;
00097 }
00098
00099
00100 if (count == 0) {
00101 assert(ts->modheight_count < TERRAFORMER_MODHEIGHT_SIZE);
00102 ts->modheight_count++;
00103 }
00104
00105
00106 mod->tile = tile;
00107 mod->height = (byte)height;
00108 }
00109
00117 static void TerraformAddDirtyTile(TerraformerState *ts, TileIndex tile)
00118 {
00119 int count = ts->tile_table_count;
00120
00121 for (TileIndex *t = ts->tile_table; count != 0; count--, t++) {
00122 if (*t == tile) return;
00123 }
00124
00125 assert(ts->tile_table_count < TERRAFORMER_TILE_TABLE_SIZE);
00126
00127 ts->tile_table[ts->tile_table_count++] = tile;
00128 }
00129
00137 static void TerraformAddDirtyTileAround(TerraformerState *ts, TileIndex tile)
00138 {
00139
00140 if (TileY(tile) >= 1) TerraformAddDirtyTile(ts, tile + TileDiffXY( 0, -1));
00141 if (TileY(tile) >= 1 && TileX(tile) >= 1) TerraformAddDirtyTile(ts, tile + TileDiffXY(-1, -1));
00142 if (TileX(tile) >= 1) TerraformAddDirtyTile(ts, tile + TileDiffXY(-1, 0));
00143 TerraformAddDirtyTile(ts, tile);
00144 }
00145
00154 static CommandCost TerraformTileHeight(TerraformerState *ts, TileIndex tile, int height)
00155 {
00156 assert(tile < MapSize());
00157
00158
00159 if (height < 0) return_cmd_error(STR_ERROR_ALREADY_AT_SEA_LEVEL);
00160 if (height > (int)MAX_TILE_HEIGHT) return_cmd_error(STR_ERROR_TOO_HIGH);
00161
00162
00163
00164
00165
00166
00167 if (height == TerraformGetHeightOfTile(ts, tile)) return CMD_ERROR;
00168
00169
00170 uint x = TileX(tile);
00171 uint y = TileY(tile);
00172 if (!_settings_game.construction.freeform_edges && ((x <= 1) || (y <= 1) || (x >= MapMaxX() - 1) || (y >= MapMaxY() - 1))) {
00173
00174
00175
00176 if (x == 1) x = 0;
00177 if (y == 1) y = 0;
00178 _terraform_err_tile = TileXY(x, y);
00179 return_cmd_error(STR_ERROR_TOO_CLOSE_TO_EDGE_OF_MAP);
00180 }
00181
00182
00183 TerraformAddDirtyTileAround(ts, tile);
00184
00185
00186 TerraformSetHeightOfTile(ts, tile, height);
00187
00188 CommandCost total_cost(EXPENSES_CONSTRUCTION);
00189
00190
00191 total_cost.AddCost(_price[PR_TERRAFORM]);
00192
00193
00194 {
00195 const TileIndexDiffC *ttm;
00196
00197 TileIndex orig_tile = tile;
00198 static const TileIndexDiffC _terraform_tilepos[] = {
00199 { 1, 0},
00200 {-2, 0},
00201 { 1, 1},
00202 { 0, -2}
00203 };
00204
00205 for (ttm = _terraform_tilepos; ttm != endof(_terraform_tilepos); ttm++) {
00206 tile += ToTileIndexDiff(*ttm);
00207
00208 if (tile >= MapSize()) continue;
00209
00210 if (Delta(TileX(orig_tile), TileX(tile)) == MapSizeX() - 1) continue;
00211 if (Delta(TileY(orig_tile), TileY(tile)) == MapSizeY() - 1) continue;
00212
00213
00214 int r = TerraformGetHeightOfTile(ts, tile);
00215 int height_diff = height - r;
00216
00217
00218 if (abs(height_diff) > 1) {
00219
00220 height_diff += (height_diff < 0 ? 1 : -1);
00221 CommandCost cost = TerraformTileHeight(ts, tile, r + height_diff);
00222 if (cost.Failed()) return cost;
00223 total_cost.AddCost(cost);
00224 }
00225 }
00226 }
00227
00228 return total_cost;
00229 }
00230
00240 CommandCost CmdTerraformLand(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00241 {
00242 _terraform_err_tile = INVALID_TILE;
00243
00244 CommandCost total_cost(EXPENSES_CONSTRUCTION);
00245 int direction = (p2 != 0 ? 1 : -1);
00246 TerraformerState ts;
00247
00248 ts.modheight_count = ts.tile_table_count = 0;
00249
00250
00251 if ((p1 & SLOPE_W) != 0 && tile + TileDiffXY(1, 0) < MapSize()) {
00252 TileIndex t = tile + TileDiffXY(1, 0);
00253 CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
00254 if (cost.Failed()) return cost;
00255 total_cost.AddCost(cost);
00256 }
00257
00258 if ((p1 & SLOPE_S) != 0 && tile + TileDiffXY(1, 1) < MapSize()) {
00259 TileIndex t = tile + TileDiffXY(1, 1);
00260 CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
00261 if (cost.Failed()) return cost;
00262 total_cost.AddCost(cost);
00263 }
00264
00265 if ((p1 & SLOPE_E) != 0 && tile + TileDiffXY(0, 1) < MapSize()) {
00266 TileIndex t = tile + TileDiffXY(0, 1);
00267 CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
00268 if (cost.Failed()) return cost;
00269 total_cost.AddCost(cost);
00270 }
00271
00272 if ((p1 & SLOPE_N) != 0) {
00273 TileIndex t = tile + TileDiffXY(0, 0);
00274 CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
00275 if (cost.Failed()) return cost;
00276 total_cost.AddCost(cost);
00277 }
00278
00279
00280
00281
00282 for (int pass = 0; pass < 2; pass++) {
00283 TileIndex *ti = ts.tile_table;
00284
00285 for (int count = ts.tile_table_count; count != 0; count--, ti++) {
00286 TileIndex tile = *ti;
00287
00288 assert(tile < MapSize());
00289
00290
00291 if (IsTileType(tile, MP_VOID)) continue;
00292
00293
00294 uint z_N = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 0));
00295 uint z_W = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 0));
00296 uint z_S = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 1));
00297 uint z_E = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 1));
00298
00299
00300 uint z_min = min(min(z_N, z_W), min(z_S, z_E));
00301 uint z_max = max(max(z_N, z_W), max(z_S, z_E));
00302
00303
00304 Slope tileh = (z_max > z_min + 1 ? SLOPE_STEEP : SLOPE_FLAT);
00305 if (z_W > z_min) tileh |= SLOPE_W;
00306 if (z_S > z_min) tileh |= SLOPE_S;
00307 if (z_E > z_min) tileh |= SLOPE_E;
00308 if (z_N > z_min) tileh |= SLOPE_N;
00309
00310
00311 if (direction == 1 && MayHaveBridgeAbove(tile) && IsBridgeAbove(tile) &&
00312 GetBridgeHeight(GetSouthernBridgeEnd(tile)) <= z_max * TILE_HEIGHT) {
00313 _terraform_err_tile = tile;
00314 return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
00315 }
00316
00317 if (direction == -1 && IsTunnelInWay(tile, z_min * TILE_HEIGHT)) {
00318 _terraform_err_tile = tile;
00319 return_cmd_error(STR_ERROR_EXCAVATION_WOULD_DAMAGE);
00320 }
00321
00322
00323 const ClearedObjectArea *coa = FindClearedObject(tile);
00324 bool indirectly_cleared = coa != NULL && coa->first_tile != tile;
00325
00326
00327 const bool curr_gen = _generating_world;
00328 if (_game_mode == GM_EDITOR) _generating_world = true;
00329 DoCommandFlag tile_flags = flags | DC_AUTO | DC_FORCE_CLEAR_TILE;
00330 if (pass == 0) {
00331 tile_flags &= ~DC_EXEC;
00332 tile_flags |= DC_NO_MODIFY_TOWN_RATING;
00333 }
00334 CommandCost cost;
00335 if (indirectly_cleared) {
00336 cost = DoCommand(tile, 0, 0, tile_flags, CMD_LANDSCAPE_CLEAR);
00337 } else {
00338 cost = _tile_type_procs[GetTileType(tile)]->terraform_tile_proc(tile, tile_flags, z_min * TILE_HEIGHT, tileh);
00339 }
00340 _generating_world = curr_gen;
00341 if (cost.Failed()) {
00342 _terraform_err_tile = tile;
00343 return cost;
00344 }
00345 if (pass == 1) total_cost.AddCost(cost);
00346 }
00347 }
00348
00349 if (flags & DC_EXEC) {
00350
00351 {
00352 int count;
00353 TerraformerHeightMod *mod;
00354
00355 mod = ts.modheight;
00356 for (count = ts.modheight_count; count != 0; count--, mod++) {
00357 TileIndex til = mod->tile;
00358
00359 SetTileHeight(til, mod->height);
00360 }
00361 }
00362
00363
00364 {
00365 int count;
00366 TileIndex *ti = ts.tile_table;
00367 for (count = ts.tile_table_count; count != 0; count--, ti++) {
00368 MarkTileDirtyByTile(*ti);
00369 }
00370 }
00371 }
00372 return total_cost;
00373 }
00374
00375
00387 CommandCost CmdLevelLand(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
00388 {
00389 if (p1 >= MapSize()) return CMD_ERROR;
00390
00391 _terraform_err_tile = INVALID_TILE;
00392
00393
00394 uint oldh = TileHeight(p1);
00395
00396
00397 uint h = oldh;
00398 LevelMode lm = (LevelMode)GB(p2, 1, 2);
00399 switch (lm) {
00400 case LM_LEVEL: break;
00401 case LM_RAISE: h++; break;
00402 case LM_LOWER: h--; break;
00403 default: return CMD_ERROR;
00404 }
00405
00406
00407 if (h > MAX_TILE_HEIGHT) return_cmd_error((oldh == 0) ? STR_ERROR_ALREADY_AT_SEA_LEVEL : STR_ERROR_TOO_HIGH);
00408
00409 Money money = GetAvailableMoneyForCommand();
00410 CommandCost cost(EXPENSES_CONSTRUCTION);
00411 CommandCost last_error(lm == LM_LEVEL ? STR_ERROR_ALREADY_LEVELLED : INVALID_STRING_ID);
00412 bool had_success = false;
00413
00414 TileArea ta(tile, p1);
00415 TileIterator *iter = HasBit(p2, 0) ? (TileIterator *)new DiagonalTileIterator(tile, p1) : new OrthogonalTileIterator(ta);
00416 for (; *iter != INVALID_TILE; ++(*iter)) {
00417 TileIndex t = *iter;
00418 uint curh = TileHeight(t);
00419 while (curh != h) {
00420 CommandCost ret = DoCommand(t, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND);
00421 if (ret.Failed()) {
00422 last_error = ret;
00423 break;
00424 }
00425
00426 if (flags & DC_EXEC) {
00427 money -= ret.GetCost();
00428 if (money < 0) {
00429 _additional_cash_required = ret.GetCost();
00430 delete iter;
00431 return cost;
00432 }
00433 DoCommand(t, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
00434 }
00435
00436 cost.AddCost(ret);
00437 curh += (curh > h) ? -1 : 1;
00438 had_success = true;
00439 }
00440 }
00441
00442 delete iter;
00443 return had_success ? cost : last_error;
00444 }