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