2008-01-07 00:45:05 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-01-07 00:45:05 +00:00
|
|
|
/** @file terraform_cmd.cpp Commands related to terraforming. */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "command_func.h"
|
|
|
|
#include "tunnel_map.h"
|
|
|
|
#include "bridge_map.h"
|
2011-02-07 22:29:47 +00:00
|
|
|
#include "viewport_func.h"
|
2010-07-19 17:17:36 +00:00
|
|
|
#include "genworld.h"
|
2010-11-21 17:42:18 +00:00
|
|
|
#include "object_base.h"
|
2011-01-04 22:50:09 +00:00
|
|
|
#include "company_base.h"
|
|
|
|
#include "company_func.h"
|
2008-01-07 00:45:05 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2014-09-20 12:11:15 +00:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2014-09-20 12:11:15 +00:00
|
|
|
/** Set of tiles. */
|
|
|
|
typedef std::set<TileIndex> TileIndexSet;
|
|
|
|
/** Mapping of tiles to their height. */
|
|
|
|
typedef std::map<TileIndex, int> TileIndexToHeightMap;
|
2008-01-07 00:45:05 +00:00
|
|
|
|
2014-09-20 12:11:15 +00:00
|
|
|
/** State of the terraforming. */
|
2008-01-07 00:45:05 +00:00
|
|
|
struct TerraformerState {
|
2014-09-20 12:11:15 +00:00
|
|
|
TileIndexSet dirty_tiles; ///< The tiles that need to be redrawn.
|
|
|
|
TileIndexToHeightMap tile_to_new_height; ///< The tiles for which the height has changed.
|
2008-01-07 00:45:05 +00:00
|
|
|
};
|
|
|
|
|
2008-04-04 15:48:15 +00:00
|
|
|
TileIndex _terraform_err_tile; ///< first tile we couldn't terraform
|
2008-01-07 00:45:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the TileHeight (height of north corner) of a tile as of current terraforming progress.
|
|
|
|
*
|
|
|
|
* @param ts TerraformerState.
|
|
|
|
* @param tile Tile.
|
|
|
|
* @return TileHeight.
|
|
|
|
*/
|
2008-04-04 15:48:15 +00:00
|
|
|
static int TerraformGetHeightOfTile(const TerraformerState *ts, TileIndex tile)
|
2008-01-07 00:45:05 +00:00
|
|
|
{
|
2014-09-20 12:11:15 +00:00
|
|
|
TileIndexToHeightMap::const_iterator it = ts->tile_to_new_height.find(tile);
|
|
|
|
return it != ts->tile_to_new_height.end() ? it->second : TileHeight(tile);
|
2008-01-07 00:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores the TileHeight (height of north corner) of a tile in a TerraformerState.
|
|
|
|
*
|
|
|
|
* @param ts TerraformerState.
|
|
|
|
* @param tile Tile.
|
|
|
|
* @param height New TileHeight.
|
|
|
|
*/
|
|
|
|
static void TerraformSetHeightOfTile(TerraformerState *ts, TileIndex tile, int height)
|
|
|
|
{
|
2014-09-20 12:11:15 +00:00
|
|
|
ts->tile_to_new_height[tile] = height;
|
2008-01-07 00:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a tile to the "tile_table" in a TerraformerState.
|
|
|
|
*
|
|
|
|
* @param ts TerraformerState.
|
|
|
|
* @param tile Tile.
|
|
|
|
* @ingroup dirty
|
|
|
|
*/
|
|
|
|
static void TerraformAddDirtyTile(TerraformerState *ts, TileIndex tile)
|
|
|
|
{
|
2014-09-20 12:11:15 +00:00
|
|
|
ts->dirty_tiles.insert(tile);
|
2008-01-07 00:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds all tiles that incident with the north corner of a specific tile to the "tile_table" in a TerraformerState.
|
|
|
|
*
|
|
|
|
* @param ts TerraformerState.
|
|
|
|
* @param tile Tile.
|
|
|
|
* @ingroup dirty
|
|
|
|
*/
|
|
|
|
static void TerraformAddDirtyTileAround(TerraformerState *ts, TileIndex tile)
|
|
|
|
{
|
2009-01-21 02:31:55 +00:00
|
|
|
/* Make sure all tiles passed to TerraformAddDirtyTile are within [0, MapSize()] */
|
|
|
|
if (TileY(tile) >= 1) TerraformAddDirtyTile(ts, tile + TileDiffXY( 0, -1));
|
|
|
|
if (TileY(tile) >= 1 && TileX(tile) >= 1) TerraformAddDirtyTile(ts, tile + TileDiffXY(-1, -1));
|
|
|
|
if (TileX(tile) >= 1) TerraformAddDirtyTile(ts, tile + TileDiffXY(-1, 0));
|
2008-01-07 00:45:05 +00:00
|
|
|
TerraformAddDirtyTile(ts, tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Terraform the north corner of a tile to a specific height.
|
|
|
|
*
|
|
|
|
* @param ts TerraformerState.
|
|
|
|
* @param tile Tile.
|
|
|
|
* @param height Aimed height.
|
2009-09-19 15:17:47 +00:00
|
|
|
* @return Error code or cost.
|
2008-01-07 00:45:05 +00:00
|
|
|
*/
|
|
|
|
static CommandCost TerraformTileHeight(TerraformerState *ts, TileIndex tile, int height)
|
|
|
|
{
|
|
|
|
assert(tile < MapSize());
|
|
|
|
|
|
|
|
/* Check range of destination height */
|
2009-04-21 23:40:56 +00:00
|
|
|
if (height < 0) return_cmd_error(STR_ERROR_ALREADY_AT_SEA_LEVEL);
|
2014-09-21 12:25:04 +00:00
|
|
|
if (height > _settings_game.construction.max_heightlevel) return_cmd_error(STR_ERROR_TOO_HIGH);
|
2008-01-07 00:45:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the terraforming has any effect.
|
|
|
|
* This can only be true, if multiple corners of the start-tile are terraformed (i.e. the terraforming is done by towns/industries etc.).
|
|
|
|
* In this case the terraforming should fail. (Don't know why.)
|
|
|
|
*/
|
|
|
|
if (height == TerraformGetHeightOfTile(ts, tile)) return CMD_ERROR;
|
|
|
|
|
2009-01-21 02:31:55 +00:00
|
|
|
/* Check "too close to edge of map". Only possible when freeform-edges is off. */
|
2008-01-07 00:45:05 +00:00
|
|
|
uint x = TileX(tile);
|
|
|
|
uint y = TileY(tile);
|
2009-01-21 02:31:55 +00:00
|
|
|
if (!_settings_game.construction.freeform_edges && ((x <= 1) || (y <= 1) || (x >= MapMaxX() - 1) || (y >= MapMaxY() - 1))) {
|
2008-01-07 00:45:05 +00:00
|
|
|
/*
|
|
|
|
* Determine a sensible error tile
|
|
|
|
*/
|
2009-01-19 15:06:11 +00:00
|
|
|
if (x == 1) x = 0;
|
|
|
|
if (y == 1) y = 0;
|
2008-01-07 00:45:05 +00:00
|
|
|
_terraform_err_tile = TileXY(x, y);
|
2009-04-21 23:40:56 +00:00
|
|
|
return_cmd_error(STR_ERROR_TOO_CLOSE_TO_EDGE_OF_MAP);
|
2008-01-07 00:45:05 +00:00
|
|
|
}
|
|
|
|
|
2010-10-30 17:51:07 +00:00
|
|
|
/* Mark incident tiles that are involved in the terraforming. */
|
2008-01-07 00:45:05 +00:00
|
|
|
TerraformAddDirtyTileAround(ts, tile);
|
|
|
|
|
|
|
|
/* Store the height modification */
|
|
|
|
TerraformSetHeightOfTile(ts, tile, height);
|
|
|
|
|
2008-04-04 15:48:15 +00:00
|
|
|
CommandCost total_cost(EXPENSES_CONSTRUCTION);
|
|
|
|
|
2008-01-07 00:45:05 +00:00
|
|
|
/* Increment cost */
|
2009-11-07 22:47:54 +00:00
|
|
|
total_cost.AddCost(_price[PR_TERRAFORM]);
|
2008-01-07 00:45:05 +00:00
|
|
|
|
|
|
|
/* Recurse to neighboured corners if height difference is larger than 1 */
|
|
|
|
{
|
|
|
|
const TileIndexDiffC *ttm;
|
|
|
|
|
2009-01-21 02:31:55 +00:00
|
|
|
TileIndex orig_tile = tile;
|
2008-01-07 00:45:05 +00:00
|
|
|
static const TileIndexDiffC _terraform_tilepos[] = {
|
|
|
|
{ 1, 0}, // move to tile in SE
|
|
|
|
{-2, 0}, // undo last move, and move to tile in NW
|
|
|
|
{ 1, 1}, // undo last move, and move to tile in SW
|
|
|
|
{ 0, -2} // undo last move, and move to tile in NE
|
|
|
|
};
|
|
|
|
|
|
|
|
for (ttm = _terraform_tilepos; ttm != endof(_terraform_tilepos); ttm++) {
|
|
|
|
tile += ToTileIndexDiff(*ttm);
|
|
|
|
|
2009-01-21 02:31:55 +00:00
|
|
|
if (tile >= MapSize()) continue;
|
|
|
|
/* Make sure we don't wrap around the map */
|
|
|
|
if (Delta(TileX(orig_tile), TileX(tile)) == MapSizeX() - 1) continue;
|
|
|
|
if (Delta(TileY(orig_tile), TileY(tile)) == MapSizeY() - 1) continue;
|
|
|
|
|
2008-01-07 00:45:05 +00:00
|
|
|
/* Get TileHeight of neighboured tile as of current terraform progress */
|
|
|
|
int r = TerraformGetHeightOfTile(ts, tile);
|
|
|
|
int height_diff = height - r;
|
|
|
|
|
|
|
|
/* Is the height difference to the neighboured corner greater than 1? */
|
|
|
|
if (abs(height_diff) > 1) {
|
|
|
|
/* Terraform the neighboured corner. The resulting height difference should be 1. */
|
|
|
|
height_diff += (height_diff < 0 ? 1 : -1);
|
|
|
|
CommandCost cost = TerraformTileHeight(ts, tile, r + height_diff);
|
2010-01-18 22:57:21 +00:00
|
|
|
if (cost.Failed()) return cost;
|
2008-01-07 00:45:05 +00:00
|
|
|
total_cost.AddCost(cost);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return total_cost;
|
|
|
|
}
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Terraform land
|
2008-01-07 00:45:05 +00:00
|
|
|
* @param tile tile to terraform
|
|
|
|
* @param flags for this command type
|
|
|
|
* @param p1 corners to terraform (SLOPE_xxx)
|
|
|
|
* @param p2 direction; eg up (non-zero) or down (zero)
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2008-01-07 00:45:05 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdTerraformLand(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2008-01-07 00:45:05 +00:00
|
|
|
{
|
2008-04-04 15:48:15 +00:00
|
|
|
_terraform_err_tile = INVALID_TILE;
|
|
|
|
|
2008-01-09 16:55:48 +00:00
|
|
|
CommandCost total_cost(EXPENSES_CONSTRUCTION);
|
2008-01-07 00:45:05 +00:00
|
|
|
int direction = (p2 != 0 ? 1 : -1);
|
2008-04-04 15:48:15 +00:00
|
|
|
TerraformerState ts;
|
2008-01-07 00:45:05 +00:00
|
|
|
|
|
|
|
/* Compute the costs and the terraforming result in a model of the landscape */
|
2009-01-22 00:23:37 +00:00
|
|
|
if ((p1 & SLOPE_W) != 0 && tile + TileDiffXY(1, 0) < MapSize()) {
|
2008-01-07 00:45:05 +00:00
|
|
|
TileIndex t = tile + TileDiffXY(1, 0);
|
|
|
|
CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
|
2010-01-18 22:57:21 +00:00
|
|
|
if (cost.Failed()) return cost;
|
2008-01-07 00:45:05 +00:00
|
|
|
total_cost.AddCost(cost);
|
|
|
|
}
|
|
|
|
|
2009-01-22 00:23:37 +00:00
|
|
|
if ((p1 & SLOPE_S) != 0 && tile + TileDiffXY(1, 1) < MapSize()) {
|
2008-01-07 00:45:05 +00:00
|
|
|
TileIndex t = tile + TileDiffXY(1, 1);
|
|
|
|
CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
|
2010-01-18 22:57:21 +00:00
|
|
|
if (cost.Failed()) return cost;
|
2008-01-07 00:45:05 +00:00
|
|
|
total_cost.AddCost(cost);
|
|
|
|
}
|
|
|
|
|
2009-01-22 00:23:37 +00:00
|
|
|
if ((p1 & SLOPE_E) != 0 && tile + TileDiffXY(0, 1) < MapSize()) {
|
2008-01-07 00:45:05 +00:00
|
|
|
TileIndex t = tile + TileDiffXY(0, 1);
|
|
|
|
CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
|
2010-01-18 22:57:21 +00:00
|
|
|
if (cost.Failed()) return cost;
|
2008-01-07 00:45:05 +00:00
|
|
|
total_cost.AddCost(cost);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((p1 & SLOPE_N) != 0) {
|
|
|
|
TileIndex t = tile + TileDiffXY(0, 0);
|
|
|
|
CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
|
2010-01-18 22:57:21 +00:00
|
|
|
if (cost.Failed()) return cost;
|
2008-01-07 00:45:05 +00:00
|
|
|
total_cost.AddCost(cost);
|
|
|
|
}
|
|
|
|
|
2010-11-21 17:42:18 +00:00
|
|
|
/* Check if the terraforming is valid wrt. tunnels, bridges and objects on the surface
|
|
|
|
* Pass == 0: Collect tileareas which are caused to be auto-cleared.
|
|
|
|
* Pass == 1: Collect the actual cost. */
|
|
|
|
for (int pass = 0; pass < 2; pass++) {
|
2014-09-20 12:11:15 +00:00
|
|
|
for (TileIndexSet::const_iterator it = ts.dirty_tiles.begin(); it != ts.dirty_tiles.end(); it++) {
|
|
|
|
TileIndex tile = *it;
|
2008-01-07 00:45:05 +00:00
|
|
|
|
2009-01-21 02:31:55 +00:00
|
|
|
assert(tile < MapSize());
|
|
|
|
/* MP_VOID tiles can be terraformed but as tunnels and bridges
|
|
|
|
* cannot go under / over these tiles they don't need checking. */
|
|
|
|
if (IsTileType(tile, MP_VOID)) continue;
|
|
|
|
|
2008-01-07 00:45:05 +00:00
|
|
|
/* Find new heights of tile corners */
|
2011-11-04 11:30:37 +00:00
|
|
|
int z_N = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 0));
|
|
|
|
int z_W = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 0));
|
|
|
|
int z_S = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 1));
|
|
|
|
int z_E = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 1));
|
2008-01-07 00:45:05 +00:00
|
|
|
|
|
|
|
/* Find min and max height of tile */
|
2011-11-04 11:30:37 +00:00
|
|
|
int z_min = min(min(z_N, z_W), min(z_S, z_E));
|
|
|
|
int z_max = max(max(z_N, z_W), max(z_S, z_E));
|
2008-01-07 00:45:05 +00:00
|
|
|
|
|
|
|
/* Compute tile slope */
|
2008-04-02 14:13:15 +00:00
|
|
|
Slope tileh = (z_max > z_min + 1 ? SLOPE_STEEP : SLOPE_FLAT);
|
|
|
|
if (z_W > z_min) tileh |= SLOPE_W;
|
|
|
|
if (z_S > z_min) tileh |= SLOPE_S;
|
|
|
|
if (z_E > z_min) tileh |= SLOPE_E;
|
|
|
|
if (z_N > z_min) tileh |= SLOPE_N;
|
2008-01-07 00:45:05 +00:00
|
|
|
|
2011-05-14 09:26:16 +00:00
|
|
|
if (pass == 0) {
|
|
|
|
/* Check if bridge would take damage */
|
2014-09-21 11:40:11 +00:00
|
|
|
if (IsBridgeAbove(tile)) {
|
|
|
|
int bridge_height = GetBridgeHeight(GetSouthernBridgeEnd(tile));
|
|
|
|
|
|
|
|
/* Check if bridge would take damage. */
|
|
|
|
if (direction == 1 && bridge_height <= z_max) {
|
|
|
|
_terraform_err_tile = tile; // highlight the tile under the bridge
|
|
|
|
return_cmd_error(STR_ERROR_MUST_DEMOLISH_BRIDGE_FIRST);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is the bridge above not too high afterwards? */
|
|
|
|
if (direction == -1 && bridge_height > (z_min + _settings_game.construction.max_bridge_height)) {
|
|
|
|
_terraform_err_tile = tile;
|
|
|
|
return_cmd_error(STR_ERROR_BRIDGE_TOO_HIGH_AFTER_LOWER_LAND);
|
|
|
|
}
|
2011-05-14 09:26:16 +00:00
|
|
|
}
|
|
|
|
/* Check if tunnel would take damage */
|
2011-11-04 10:28:31 +00:00
|
|
|
if (direction == -1 && IsTunnelInWay(tile, z_min)) {
|
2011-05-14 09:26:16 +00:00
|
|
|
_terraform_err_tile = tile; // highlight the tile above the tunnel
|
|
|
|
return_cmd_error(STR_ERROR_EXCAVATION_WOULD_DAMAGE);
|
|
|
|
}
|
2008-01-07 00:45:05 +00:00
|
|
|
}
|
2010-11-21 17:42:18 +00:00
|
|
|
|
|
|
|
/* Is the tile already cleared? */
|
|
|
|
const ClearedObjectArea *coa = FindClearedObject(tile);
|
|
|
|
bool indirectly_cleared = coa != NULL && coa->first_tile != tile;
|
|
|
|
|
2008-01-07 00:45:05 +00:00
|
|
|
/* Check tiletype-specific things, and add extra-cost */
|
2008-05-19 01:51:57 +00:00
|
|
|
const bool curr_gen = _generating_world;
|
|
|
|
if (_game_mode == GM_EDITOR) _generating_world = true; // used to create green terraformed land
|
2010-11-21 17:42:18 +00:00
|
|
|
DoCommandFlag tile_flags = flags | DC_AUTO | DC_FORCE_CLEAR_TILE;
|
|
|
|
if (pass == 0) {
|
|
|
|
tile_flags &= ~DC_EXEC;
|
|
|
|
tile_flags |= DC_NO_MODIFY_TOWN_RATING;
|
|
|
|
}
|
|
|
|
CommandCost cost;
|
|
|
|
if (indirectly_cleared) {
|
|
|
|
cost = DoCommand(tile, 0, 0, tile_flags, CMD_LANDSCAPE_CLEAR);
|
|
|
|
} else {
|
2011-11-04 10:30:10 +00:00
|
|
|
cost = _tile_type_procs[GetTileType(tile)]->terraform_tile_proc(tile, tile_flags, z_min, tileh);
|
2010-11-21 17:42:18 +00:00
|
|
|
}
|
2008-05-19 01:51:57 +00:00
|
|
|
_generating_world = curr_gen;
|
2010-01-18 22:57:21 +00:00
|
|
|
if (cost.Failed()) {
|
2008-01-07 00:45:05 +00:00
|
|
|
_terraform_err_tile = tile;
|
|
|
|
return cost;
|
|
|
|
}
|
2010-11-21 17:42:18 +00:00
|
|
|
if (pass == 1) total_cost.AddCost(cost);
|
2008-01-07 00:45:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-04 22:50:09 +00:00
|
|
|
Company *c = Company::GetIfValid(_current_company);
|
2014-09-20 12:11:15 +00:00
|
|
|
if (c != NULL && GB(c->terraform_limit, 16, 16) < ts.tile_to_new_height.size()) {
|
2011-01-04 22:50:09 +00:00
|
|
|
return_cmd_error(STR_ERROR_TERRAFORM_LIMIT_REACHED);
|
|
|
|
}
|
|
|
|
|
2008-01-07 00:45:05 +00:00
|
|
|
if (flags & DC_EXEC) {
|
|
|
|
/* change the height */
|
2014-09-20 12:11:15 +00:00
|
|
|
for (TileIndexToHeightMap::const_iterator it = ts.tile_to_new_height.begin();
|
|
|
|
it != ts.tile_to_new_height.end(); it++) {
|
|
|
|
TileIndex tile = it->first;
|
|
|
|
int height = it->second;
|
2008-01-07 00:45:05 +00:00
|
|
|
|
2014-09-20 12:11:15 +00:00
|
|
|
SetTileHeight(tile, (uint)height);
|
2008-01-07 00:45:05 +00:00
|
|
|
}
|
|
|
|
|
2014-09-20 12:11:15 +00:00
|
|
|
/* Finally mark the dirty tiles dirty */
|
|
|
|
for (TileIndexSet::const_iterator it = ts.dirty_tiles.begin(); it != ts.dirty_tiles.end(); it++) {
|
|
|
|
MarkTileDirtyByTile(*it);
|
2014-09-21 17:29:48 +00:00
|
|
|
|
|
|
|
int height = TerraformGetHeightOfTile(&ts, *it);
|
|
|
|
|
|
|
|
/* Now, if we alter the height of the map edge, we need to take care
|
|
|
|
* about repainting the affected areas outside map as well.
|
|
|
|
* Remember:
|
|
|
|
* Outside map, we assume that our landscape descends to
|
|
|
|
* height zero as fast as possible.
|
|
|
|
* Those simulated tiles (they don't exist as datastructure,
|
|
|
|
* only as concept in code) need to be repainted properly,
|
|
|
|
* otherwise we will get ugly glitches.
|
|
|
|
*
|
|
|
|
* Furthermore, note that we have to take care about the possibility,
|
|
|
|
* that landscape was higher before the change,
|
|
|
|
* so also tiles a bit outside need to be repainted.
|
|
|
|
*/
|
|
|
|
int x = TileX(*it);
|
|
|
|
int y = TileY(*it);
|
|
|
|
if (x == 0) {
|
|
|
|
if (y == 0) {
|
|
|
|
/* Height of the northern corner is altered. */
|
|
|
|
for (int cx = 0; cx >= -height - 1; cx--) {
|
|
|
|
for (int cy = 0; cy >= -height - 1; cy--) {
|
|
|
|
/* This means, tiles in the sector north of that
|
|
|
|
* corner need to be repainted.
|
|
|
|
*/
|
|
|
|
if (cx + cy >= -height - 2) {
|
|
|
|
/* But only tiles that actually might have changed. */
|
|
|
|
MarkTileDirtyByTileOutsideMap(cx, cy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (y < (int)MapMaxY()) {
|
|
|
|
for (int cx = 0; cx >= -height - 1; cx--) {
|
|
|
|
MarkTileDirtyByTileOutsideMap(cx, y);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int cx = 0; cx >= -height - 1; cx--) {
|
|
|
|
for (int cy = (int)MapMaxY(); cy <= (int)MapMaxY() + height + 1; cy++) {
|
|
|
|
if (cx + ((int)MapMaxY() - cy) >= -height - 2) {
|
|
|
|
MarkTileDirtyByTileOutsideMap(cx, cy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (x < (int)MapMaxX()) {
|
|
|
|
if (y == 0) {
|
|
|
|
for (int cy = 0; cy >= -height - 1; cy--) {
|
|
|
|
MarkTileDirtyByTileOutsideMap(x, cy);
|
|
|
|
}
|
|
|
|
} else if (y < (int)MapMaxY()) {
|
|
|
|
/* Nothing to be done here, we are inside the map. */
|
|
|
|
} else {
|
|
|
|
for (int cy = (int)MapMaxY(); cy <= (int)MapMaxY() + height + 1; cy++) {
|
|
|
|
MarkTileDirtyByTileOutsideMap(x, cy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (y == 0) {
|
|
|
|
for (int cx = (int)MapMaxX(); cx <= (int)MapMaxX() + height + 1; cx++) {
|
|
|
|
for (int cy = 0; cy >= -height - 1; cy--) {
|
|
|
|
if (((int)MapMaxX() - cx) + cy >= -height - 2) {
|
|
|
|
MarkTileDirtyByTileOutsideMap(cx, cy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (y < (int)MapMaxY()) {
|
|
|
|
for (int cx = (int)MapMaxX(); cx <= (int)MapMaxX() + height + 1; cx++) {
|
|
|
|
MarkTileDirtyByTileOutsideMap(cx, y);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int cx = (int)MapMaxX(); cx <= (int)MapMaxX() + height + 1; cx++) {
|
|
|
|
for (int cy = (int)MapMaxY(); cy <= (int)MapMaxY() + height + 1; cy++) {
|
|
|
|
if (((int)MapMaxX() - cx) + ((int)MapMaxY() - cy) >= -height - 2) {
|
|
|
|
MarkTileDirtyByTileOutsideMap(cx, cy);
|
|
|
|
}
|
|
|
|
}
|
2014-09-21 17:37:30 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-21 17:29:48 +00:00
|
|
|
}
|
2008-01-07 00:45:05 +00:00
|
|
|
}
|
2011-01-04 22:50:09 +00:00
|
|
|
|
2015-08-10 20:24:13 +00:00
|
|
|
if (c != NULL) c->terraform_limit -= (uint32)ts.tile_to_new_height.size() << 16;
|
2008-01-07 00:45:05 +00:00
|
|
|
}
|
|
|
|
return total_cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Levels a selected (rectangle) area of land
|
2008-01-07 00:45:05 +00:00
|
|
|
* @param tile end tile of area-drag
|
|
|
|
* @param flags for this command type
|
|
|
|
* @param p1 start tile of area drag
|
2010-12-13 11:21:53 +00:00
|
|
|
* @param p2 various bitstuffed data.
|
2010-12-13 15:15:02 +00:00
|
|
|
* bit 0: Whether to use the Orthogonal (0) or Diagonal (1) iterator.
|
2010-12-13 11:21:53 +00:00
|
|
|
* bits 1 - 2: Mode of leveling \c LevelMode.
|
2009-09-18 14:23:58 +00:00
|
|
|
* @param text unused
|
|
|
|
* @return the cost of this operation or an error
|
2008-01-07 00:45:05 +00:00
|
|
|
*/
|
2009-02-09 21:20:05 +00:00
|
|
|
CommandCost CmdLevelLand(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
|
2008-01-07 00:45:05 +00:00
|
|
|
{
|
|
|
|
if (p1 >= MapSize()) return CMD_ERROR;
|
|
|
|
|
2009-01-18 16:26:59 +00:00
|
|
|
_terraform_err_tile = INVALID_TILE;
|
|
|
|
|
2008-01-07 00:45:05 +00:00
|
|
|
/* remember level height */
|
2008-04-04 15:48:15 +00:00
|
|
|
uint oldh = TileHeight(p1);
|
2008-01-07 00:45:05 +00:00
|
|
|
|
|
|
|
/* compute new height */
|
2010-12-13 11:21:53 +00:00
|
|
|
uint h = oldh;
|
|
|
|
LevelMode lm = (LevelMode)GB(p2, 1, 2);
|
|
|
|
switch (lm) {
|
|
|
|
case LM_LEVEL: break;
|
|
|
|
case LM_RAISE: h++; break;
|
|
|
|
case LM_LOWER: h--; break;
|
|
|
|
default: return CMD_ERROR;
|
|
|
|
}
|
2008-01-07 00:45:05 +00:00
|
|
|
|
|
|
|
/* Check range of destination height */
|
2014-09-21 12:25:04 +00:00
|
|
|
if (h > _settings_game.construction.max_heightlevel) return_cmd_error((oldh == 0) ? STR_ERROR_ALREADY_AT_SEA_LEVEL : STR_ERROR_TOO_HIGH);
|
2008-01-07 00:45:05 +00:00
|
|
|
|
2008-04-04 15:48:15 +00:00
|
|
|
Money money = GetAvailableMoneyForCommand();
|
|
|
|
CommandCost cost(EXPENSES_CONSTRUCTION);
|
2010-12-13 11:21:53 +00:00
|
|
|
CommandCost last_error(lm == LM_LEVEL ? STR_ERROR_ALREADY_LEVELLED : INVALID_STRING_ID);
|
2010-03-20 17:13:00 +00:00
|
|
|
bool had_success = false;
|
2008-01-07 00:45:05 +00:00
|
|
|
|
2011-01-04 22:50:09 +00:00
|
|
|
const Company *c = Company::GetIfValid(_current_company);
|
|
|
|
int limit = (c == NULL ? INT32_MAX : GB(c->terraform_limit, 16, 16));
|
2011-01-05 08:33:21 +00:00
|
|
|
if (limit == 0) return_cmd_error(STR_ERROR_TERRAFORM_LIMIT_REACHED);
|
2011-01-04 22:50:09 +00:00
|
|
|
|
2014-02-02 14:53:52 +00:00
|
|
|
TileIterator *iter = HasBit(p2, 0) ? (TileIterator *)new DiagonalTileIterator(tile, p1) : new OrthogonalTileIterator(tile, p1);
|
2010-12-13 15:15:02 +00:00
|
|
|
for (; *iter != INVALID_TILE; ++(*iter)) {
|
|
|
|
TileIndex t = *iter;
|
2010-12-13 11:30:22 +00:00
|
|
|
uint curh = TileHeight(t);
|
2008-01-07 00:45:05 +00:00
|
|
|
while (curh != h) {
|
2010-12-13 11:30:22 +00:00
|
|
|
CommandCost ret = DoCommand(t, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND);
|
2010-03-20 17:13:00 +00:00
|
|
|
if (ret.Failed()) {
|
|
|
|
last_error = ret;
|
2011-01-04 22:50:09 +00:00
|
|
|
|
|
|
|
/* Did we reach the limit? */
|
|
|
|
if (ret.GetErrorMessage() == STR_ERROR_TERRAFORM_LIMIT_REACHED) limit = 0;
|
2010-03-20 17:13:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-01-07 00:45:05 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2008-04-04 15:48:15 +00:00
|
|
|
money -= ret.GetCost();
|
|
|
|
if (money < 0) {
|
2008-01-07 00:45:05 +00:00
|
|
|
_additional_cash_required = ret.GetCost();
|
2010-12-13 15:15:02 +00:00
|
|
|
delete iter;
|
2008-01-07 00:45:05 +00:00
|
|
|
return cost;
|
|
|
|
}
|
2010-12-13 11:30:22 +00:00
|
|
|
DoCommand(t, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
|
2011-01-04 22:50:09 +00:00
|
|
|
} else {
|
|
|
|
/* When we're at the terraform limit we better bail (unneeded) testing as well.
|
|
|
|
* This will probably cause the terraforming cost to be underestimated, but only
|
|
|
|
* when it's near the terraforming limit. Even then, the estimation is
|
|
|
|
* completely off due to it basically counting terraforming double, so it being
|
|
|
|
* cut off earlier might even give a better estimate in some cases. */
|
2011-01-14 18:45:14 +00:00
|
|
|
if (--limit <= 0) {
|
|
|
|
had_success = true;
|
|
|
|
break;
|
|
|
|
}
|
2008-01-07 00:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cost.AddCost(ret);
|
|
|
|
curh += (curh > h) ? -1 : 1;
|
2010-03-20 17:13:00 +00:00
|
|
|
had_success = true;
|
2008-01-07 00:45:05 +00:00
|
|
|
}
|
2011-01-04 22:50:09 +00:00
|
|
|
|
|
|
|
if (limit <= 0) break;
|
2009-07-26 21:50:30 +00:00
|
|
|
}
|
2008-01-07 00:45:05 +00:00
|
|
|
|
2010-12-13 15:15:02 +00:00
|
|
|
delete iter;
|
2010-03-20 17:13:00 +00:00
|
|
|
return had_success ? cost : last_error;
|
2008-01-07 00:45:05 +00:00
|
|
|
}
|