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(TerraformerState *ts, TileIndex tile)
00062 {
00063 TerraformerHeightMod *mod = ts->modheight;
00064 int count;
00065
00066 for (count = ts->modheight_count; count != 0; count--, mod++) {
00067 if (mod->tile == tile) return mod->height;
00068 }
00069
00070
00071 return TileHeight(tile);
00072 }
00073
00081 static void TerraformSetHeightOfTile(TerraformerState *ts, TileIndex tile, int height)
00082 {
00083
00084
00085
00086 TerraformerHeightMod *mod = ts->modheight;
00087 int count = ts->modheight_count;
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;
00114 TileIndex *t;
00115
00116 count = ts->tile_table_count;
00117
00118 for (t = ts->tile_table; count != 0; count--, t++) {
00119 if (*t == tile) return;
00120 }
00121
00122 assert(ts->tile_table_count < TERRAFORMER_TILE_TABLE_SIZE);
00123
00124 ts->tile_table[ts->tile_table_count++] = tile;
00125 }
00126
00134 static void TerraformAddDirtyTileAround(TerraformerState *ts, TileIndex tile)
00135 {
00136 TerraformAddDirtyTile(ts, tile + TileDiffXY( 0, -1));
00137 TerraformAddDirtyTile(ts, tile + TileDiffXY(-1, -1));
00138 TerraformAddDirtyTile(ts, tile + TileDiffXY(-1, 0));
00139 TerraformAddDirtyTile(ts, tile);
00140 }
00141
00150 static CommandCost TerraformTileHeight(TerraformerState *ts, TileIndex tile, int height)
00151 {
00152 CommandCost total_cost(EXPENSES_CONSTRUCTION);
00153
00154 assert(tile < MapSize());
00155
00156
00157 if (height < 0) return_cmd_error(STR_1003_ALREADY_AT_SEA_LEVEL);
00158 if (height > MAX_TILE_HEIGHT) return_cmd_error(STR_1004_TOO_HIGH);
00159
00160
00161
00162
00163
00164
00165 if (height == TerraformGetHeightOfTile(ts, tile)) return CMD_ERROR;
00166
00167
00168 uint x = TileX(tile);
00169 uint y = TileY(tile);
00170 if ((x <= 1) || (y <= 1) || (x >= MapMaxX() - 1) || (y >= MapMaxY() - 1)) {
00171
00172
00173
00174
00175 if ((x == 1) && (y != 0)) x = 0;
00176 if ((y == 1) && (x != 0)) y = 0;
00177 _terraform_err_tile = TileXY(x, y);
00178 return_cmd_error(STR_0002_TOO_CLOSE_TO_EDGE_OF_MAP);
00179 }
00180
00181
00182 TerraformAddDirtyTileAround(ts, tile);
00183
00184
00185 TerraformSetHeightOfTile(ts, tile, height);
00186
00187
00188 total_cost.AddCost(_price.terraform);
00189
00190
00191 {
00192 const TileIndexDiffC *ttm;
00193
00194 static const TileIndexDiffC _terraform_tilepos[] = {
00195 { 1, 0},
00196 {-2, 0},
00197 { 1, 1},
00198 { 0, -2}
00199 };
00200
00201 for (ttm = _terraform_tilepos; ttm != endof(_terraform_tilepos); ttm++) {
00202 tile += ToTileIndexDiff(*ttm);
00203
00204
00205 int r = TerraformGetHeightOfTile(ts, tile);
00206 int height_diff = height - r;
00207
00208
00209 if (abs(height_diff) > 1) {
00210
00211 height_diff += (height_diff < 0 ? 1 : -1);
00212 CommandCost cost = TerraformTileHeight(ts, tile, r + height_diff);
00213 if (CmdFailed(cost)) return cost;
00214 total_cost.AddCost(cost);
00215 }
00216 }
00217 }
00218
00219 return total_cost;
00220 }
00221
00229 CommandCost CmdTerraformLand(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
00230 {
00231 TerraformerState ts;
00232 CommandCost total_cost(EXPENSES_CONSTRUCTION);
00233 int direction = (p2 != 0 ? 1 : -1);
00234
00235 _terraform_err_tile = 0;
00236
00237 ts.modheight_count = ts.tile_table_count = 0;
00238
00239
00240 if (tile + TileDiffXY(1, 1) >= MapSize()) return CMD_ERROR;
00241
00242
00243 if ((p1 & SLOPE_W) != 0) {
00244 TileIndex t = tile + TileDiffXY(1, 0);
00245 CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
00246 if (CmdFailed(cost)) return cost;
00247 total_cost.AddCost(cost);
00248 }
00249
00250 if ((p1 & SLOPE_S) != 0) {
00251 TileIndex t = tile + TileDiffXY(1, 1);
00252 CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
00253 if (CmdFailed(cost)) return cost;
00254 total_cost.AddCost(cost);
00255 }
00256
00257 if ((p1 & SLOPE_E) != 0) {
00258 TileIndex t = tile + TileDiffXY(0, 1);
00259 CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
00260 if (CmdFailed(cost)) return cost;
00261 total_cost.AddCost(cost);
00262 }
00263
00264 if ((p1 & SLOPE_N) != 0) {
00265 TileIndex t = tile + TileDiffXY(0, 0);
00266 CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
00267 if (CmdFailed(cost)) return cost;
00268 total_cost.AddCost(cost);
00269 }
00270
00271
00272 {
00273 int count;
00274 TileIndex *ti = ts.tile_table;
00275
00276 for (count = ts.tile_table_count; count != 0; count--, ti++) {
00277 TileIndex tile = *ti;
00278
00279
00280 uint z_N = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 0));
00281 uint z_W = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 0));
00282 uint z_S = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 1));
00283 uint z_E = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 1));
00284
00285
00286 uint z_min = min(min(z_N, z_W), min(z_S, z_E));
00287 uint z_max = max(max(z_N, z_W), max(z_S, z_E));
00288
00289
00290 uint tileh = (z_max > z_min + 1 ? SLOPE_STEEP : SLOPE_FLAT);
00291 if (z_W > z_min) tileh += SLOPE_W;
00292 if (z_S > z_min) tileh += SLOPE_S;
00293 if (z_E > z_min) tileh += SLOPE_E;
00294 if (z_N > z_min) tileh += SLOPE_N;
00295
00296
00297 if (direction == 1 && MayHaveBridgeAbove(tile) && IsBridgeAbove(tile) &&
00298 GetBridgeHeight(GetSouthernBridgeEnd(tile)) <= z_max * TILE_HEIGHT) {
00299 _terraform_err_tile = tile;
00300 return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
00301 }
00302
00303 if (direction == -1 && IsTunnelInWay(tile, z_min * TILE_HEIGHT)) {
00304 _terraform_err_tile = tile;
00305 return_cmd_error(STR_1002_EXCAVATION_WOULD_DAMAGE);
00306 }
00307
00308 CommandCost cost = _tile_type_procs[GetTileType(tile)]->terraform_tile_proc(tile, flags | DC_AUTO, z_min * TILE_HEIGHT, (Slope) tileh);
00309 if (CmdFailed(cost)) {
00310 _terraform_err_tile = tile;
00311 return cost;
00312 }
00313 total_cost.AddCost(cost);
00314 }
00315 }
00316
00317 if (flags & DC_EXEC) {
00318
00319 {
00320 int count;
00321 TerraformerHeightMod *mod;
00322
00323 mod = ts.modheight;
00324 for (count = ts.modheight_count; count != 0; count--, mod++) {
00325 TileIndex til = mod->tile;
00326
00327 SetTileHeight(til, mod->height);
00328 }
00329 }
00330
00331
00332 {
00333 int count;
00334 TileIndex *ti = ts.tile_table;
00335 for (count = ts.tile_table_count; count != 0; count--, ti++) {
00336 MarkTileDirtyByTile(*ti);
00337 }
00338 }
00339 }
00340 return total_cost;
00341 }
00342
00343
00351 CommandCost CmdLevelLand(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
00352 {
00353 int size_x, size_y;
00354 int ex;
00355 int ey;
00356 int sx, sy;
00357 uint h, oldh, curh;
00358 CommandCost money;
00359 CommandCost ret;
00360 CommandCost cost(EXPENSES_CONSTRUCTION);
00361
00362 if (p1 >= MapSize()) return CMD_ERROR;
00363
00364
00365 oldh = TileHeight(p1);
00366
00367
00368 h = oldh + p2;
00369
00370
00371 if (h > MAX_TILE_HEIGHT) return_cmd_error((oldh == 0) ? STR_1003_ALREADY_AT_SEA_LEVEL : STR_1004_TOO_HIGH);
00372
00373
00374 ex = TileX(tile);
00375 ey = TileY(tile);
00376 sx = TileX(p1);
00377 sy = TileY(p1);
00378 if (ex < sx) Swap(ex, sx);
00379 if (ey < sy) Swap(ey, sy);
00380 tile = TileXY(sx, sy);
00381
00382 size_x = ex - sx + 1;
00383 size_y = ey - sy + 1;
00384
00385 money.AddCost(GetAvailableMoneyForCommand());
00386
00387 BEGIN_TILE_LOOP(tile2, size_x, size_y, tile) {
00388 curh = TileHeight(tile2);
00389 while (curh != h) {
00390 ret = DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND);
00391 if (CmdFailed(ret)) break;
00392
00393 if (flags & DC_EXEC) {
00394 money.AddCost(-ret.GetCost());
00395 if (money.GetCost() < 0) {
00396 _additional_cash_required = ret.GetCost();
00397 return cost;
00398 }
00399 DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
00400 }
00401
00402 cost.AddCost(ret);
00403 curh += (curh > h) ? -1 : 1;
00404 }
00405 } END_TILE_LOOP(tile2, size_x, size_y, tile)
00406
00407 return (cost.GetCost() == 0) ? CMD_ERROR : cost;
00408 }