00001
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
00020
00021
00022
00023
00024 static const int TERRAFORMER_MODHEIGHT_SIZE = 2 * MAX_TILE_HEIGHT * (MAX_TILE_HEIGHT + 1);
00025
00026
00027
00028
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
00070 return TileHeight(tile);
00071 }
00072
00080 static void TerraformSetHeightOfTile(TerraformerState *ts, TileIndex tile, int height)
00081 {
00082
00083
00084
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
00094 if (count == 0) {
00095 assert(ts->modheight_count < TERRAFORMER_MODHEIGHT_SIZE);
00096 ts->modheight_count++;
00097 }
00098
00099
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
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 ((x <= 1) || (y <= 1) || (x >= MapMaxX() - 1) || (y >= MapMaxY() - 1)) {
00166
00167
00168
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
00177 TerraformAddDirtyTileAround(ts, tile);
00178
00179
00180 TerraformSetHeightOfTile(ts, tile, height);
00181
00182 CommandCost total_cost(EXPENSES_CONSTRUCTION);
00183
00184
00185 total_cost.AddCost(_price.terraform);
00186
00187
00188 {
00189 const TileIndexDiffC *ttm;
00190
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
00202 int r = TerraformGetHeightOfTile(ts, tile);
00203 int height_diff = height - r;
00204
00205
00206 if (abs(height_diff) > 1) {
00207
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
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
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
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
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
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
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
00293 if (direction == 1 && MayHaveBridgeAbove(tile) && IsBridgeAbove(tile) &&
00294 GetBridgeHeight(GetSouthernBridgeEnd(tile)) <= z_max * TILE_HEIGHT) {
00295 _terraform_err_tile = tile;
00296 return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
00297 }
00298
00299 if (direction == -1 && IsTunnelInWay(tile, z_min * TILE_HEIGHT)) {
00300 _terraform_err_tile = tile;
00301 return_cmd_error(STR_1002_EXCAVATION_WOULD_DAMAGE);
00302 }
00303
00304 const bool curr_gen = _generating_world;
00305 if (_game_mode == GM_EDITOR) _generating_world = true;
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
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
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
00355 uint oldh = TileHeight(p1);
00356
00357
00358 uint h = oldh + p2;
00359
00360
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
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 }