src/terraform_cmd.cpp
changeset 8210 cc873256f63a
child 8230 64f28fe2d5c8
equal deleted inserted replaced
8209:0bbae499a1a2 8210:cc873256f63a
       
     1 /* $Id$ */
       
     2 
       
     3 /** @file terraform_cmd.cpp Commands related to terraforming. */
       
     4 
       
     5 #include "stdafx.h"
       
     6 #include "openttd.h"
       
     7 #include "strings_type.h"
       
     8 #include "table/strings.h"
       
     9 #include "command_func.h"
       
    10 #include "tile_map.h"
       
    11 #include "tunnel_map.h"
       
    12 #include "bridge_map.h"
       
    13 #include "variables.h"
       
    14 #include "functions.h"
       
    15 #include "economy_func.h"
       
    16 
       
    17 /*
       
    18  * In one terraforming command all four corners of a initial tile can be raised/lowered (though this is not available to the player).
       
    19  * The maximal amount of height modifications is archieved when raising a complete flat land from sea level to MAX_TILE_HEIGHT or vice versa.
       
    20  * This affects all corners with a manhatten distance smaller than MAX_TILE_HEIGHT to one of the initial 4 corners.
       
    21  * Their maximal amount is computed to 4 * \sum_{i=1}^{h_max} i  =  2 * h_max * (h_max + 1).
       
    22  */
       
    23 static const int TERRAFORMER_MODHEIGHT_SIZE = 2 * MAX_TILE_HEIGHT * (MAX_TILE_HEIGHT + 1);
       
    24 
       
    25 /*
       
    26  * The maximal amount of affected tiles (i.e. the tiles that incident with one of the corners above, is computed similiar to
       
    27  * 1 + 4 * \sum_{i=1}^{h_max} (i+1)  =  1 + 2 * h_max + (h_max + 3).
       
    28  */
       
    29 static const int TERRAFORMER_TILE_TABLE_SIZE = 1 + 2 * MAX_TILE_HEIGHT * (MAX_TILE_HEIGHT + 3);
       
    30 
       
    31 struct TerraformerHeightMod {
       
    32 	TileIndex tile;   ///< Referenced tile.
       
    33 	byte height;      ///< New TileHeight (height of north corner) of the tile.
       
    34 };
       
    35 
       
    36 struct TerraformerState {
       
    37 	int modheight_count;                                         ///< amount of entries in "modheight".
       
    38 	int tile_table_count;                                        ///< amount of entries in "tile_table".
       
    39 
       
    40 	/**
       
    41 	 * Dirty tiles, i.e.\ at least one corner changed.
       
    42 	 *
       
    43 	 * This array contains the tiles which are or will be marked as dirty.
       
    44 	 *
       
    45 	 * @ingroup dirty
       
    46 	 */
       
    47 	TileIndex tile_table[TERRAFORMER_TILE_TABLE_SIZE];
       
    48 	TerraformerHeightMod modheight[TERRAFORMER_MODHEIGHT_SIZE];  ///< Height modifications.
       
    49 };
       
    50 
       
    51 TileIndex _terraform_err_tile;
       
    52 
       
    53 /**
       
    54  * Gets the TileHeight (height of north corner) of a tile as of current terraforming progress.
       
    55  *
       
    56  * @param ts TerraformerState.
       
    57  * @param tile Tile.
       
    58  * @return TileHeight.
       
    59  */
       
    60 static int TerraformGetHeightOfTile(TerraformerState *ts, TileIndex tile)
       
    61 {
       
    62 	TerraformerHeightMod *mod = ts->modheight;
       
    63 	int count;
       
    64 
       
    65 	for (count = ts->modheight_count; count != 0; count--, mod++) {
       
    66 		if (mod->tile == tile) return mod->height;
       
    67 	}
       
    68 
       
    69 	/* TileHeight unchanged so far, read value from map. */
       
    70 	return TileHeight(tile);
       
    71 }
       
    72 
       
    73 /**
       
    74  * Stores the TileHeight (height of north corner) of a tile in a TerraformerState.
       
    75  *
       
    76  * @param ts TerraformerState.
       
    77  * @param tile Tile.
       
    78  * @param height New TileHeight.
       
    79  */
       
    80 static void TerraformSetHeightOfTile(TerraformerState *ts, TileIndex tile, int height)
       
    81 {
       
    82 	/* Find tile in the "modheight" table.
       
    83 	 * Note: In a normal user-terraform command the tile will not be found in the "modheight" table.
       
    84 	 *       But during house- or industry-construction multiple corners can be terraformed at once. */
       
    85 	TerraformerHeightMod *mod = ts->modheight;
       
    86 	int count = ts->modheight_count;
       
    87 	while ((count > 0) && (mod->tile != tile)) {
       
    88 		mod++;
       
    89 		count--;
       
    90 	}
       
    91 
       
    92 	/* New entry? */
       
    93 	if (count == 0) {
       
    94 		assert(ts->modheight_count < TERRAFORMER_MODHEIGHT_SIZE);
       
    95 		ts->modheight_count++;
       
    96 	}
       
    97 
       
    98 	/* Finally store the new value */
       
    99 	mod->tile = tile;
       
   100 	mod->height = (byte)height;
       
   101 }
       
   102 
       
   103 /**
       
   104  * Adds a tile to the "tile_table" in a TerraformerState.
       
   105  *
       
   106  * @param ts TerraformerState.
       
   107  * @param tile Tile.
       
   108  * @ingroup dirty
       
   109  */
       
   110 static void TerraformAddDirtyTile(TerraformerState *ts, TileIndex tile)
       
   111 {
       
   112 	int count;
       
   113 	TileIndex *t;
       
   114 
       
   115 	count = ts->tile_table_count;
       
   116 
       
   117 	for (t = ts->tile_table; count != 0; count--, t++) {
       
   118 		if (*t == tile) return;
       
   119 	}
       
   120 
       
   121 	assert(ts->tile_table_count < TERRAFORMER_TILE_TABLE_SIZE);
       
   122 
       
   123 	ts->tile_table[ts->tile_table_count++] = tile;
       
   124 }
       
   125 
       
   126 /**
       
   127  * Adds all tiles that incident with the north corner of a specific tile to the "tile_table" in a TerraformerState.
       
   128  *
       
   129  * @param ts TerraformerState.
       
   130  * @param tile Tile.
       
   131  * @ingroup dirty
       
   132  */
       
   133 static void TerraformAddDirtyTileAround(TerraformerState *ts, TileIndex tile)
       
   134 {
       
   135 	TerraformAddDirtyTile(ts, tile + TileDiffXY( 0, -1));
       
   136 	TerraformAddDirtyTile(ts, tile + TileDiffXY(-1, -1));
       
   137 	TerraformAddDirtyTile(ts, tile + TileDiffXY(-1,  0));
       
   138 	TerraformAddDirtyTile(ts, tile);
       
   139 }
       
   140 
       
   141 /**
       
   142  * Terraform the north corner of a tile to a specific height.
       
   143  *
       
   144  * @param ts TerraformerState.
       
   145  * @param tile Tile.
       
   146  * @param height Aimed height.
       
   147  * @param return Error code or cost.
       
   148  */
       
   149 static CommandCost TerraformTileHeight(TerraformerState *ts, TileIndex tile, int height)
       
   150 {
       
   151 	CommandCost total_cost = CommandCost();
       
   152 
       
   153 	assert(tile < MapSize());
       
   154 
       
   155 	/* Check range of destination height */
       
   156 	if (height < 0) return_cmd_error(STR_1003_ALREADY_AT_SEA_LEVEL);
       
   157 	if (height > MAX_TILE_HEIGHT) return_cmd_error(STR_1004_TOO_HIGH);
       
   158 
       
   159 	/*
       
   160 	 * Check if the terraforming has any effect.
       
   161 	 * This can only be true, if multiple corners of the start-tile are terraformed (i.e. the terraforming is done by towns/industries etc.).
       
   162 	 * In this case the terraforming should fail. (Don't know why.)
       
   163 	 */
       
   164 	if (height == TerraformGetHeightOfTile(ts, tile)) return CMD_ERROR;
       
   165 
       
   166 	/* Check "too close to edge of map" */
       
   167 	uint x = TileX(tile);
       
   168 	uint y = TileY(tile);
       
   169 	if ((x <= 1) || (y <= 1) || (x >= MapMaxX() - 1) || (y >= MapMaxY() - 1)) {
       
   170 		/*
       
   171 		 * Determine a sensible error tile
       
   172 		 * Note: If x and y are both zero this will disable the error tile. (Tile 0 cannot be highlighted :( )
       
   173 		 */
       
   174 		if ((x == 1) && (y != 0)) x = 0;
       
   175 		if ((y == 1) && (x != 0)) y = 0;
       
   176 		_terraform_err_tile = TileXY(x, y);
       
   177 		return_cmd_error(STR_0002_TOO_CLOSE_TO_EDGE_OF_MAP);
       
   178 	}
       
   179 
       
   180 	/* Mark incident tiles, that are involved in the terraforming */
       
   181 	TerraformAddDirtyTileAround(ts, tile);
       
   182 
       
   183 	/* Store the height modification */
       
   184 	TerraformSetHeightOfTile(ts, tile, height);
       
   185 
       
   186 	/* Increment cost */
       
   187 	total_cost.AddCost(_price.terraform);
       
   188 
       
   189 	/* Recurse to neighboured corners if height difference is larger than 1 */
       
   190 	{
       
   191 		const TileIndexDiffC *ttm;
       
   192 
       
   193 		static const TileIndexDiffC _terraform_tilepos[] = {
       
   194 			{ 1,  0}, // move to tile in SE
       
   195 			{-2,  0}, // undo last move, and move to tile in NW
       
   196 			{ 1,  1}, // undo last move, and move to tile in SW
       
   197 			{ 0, -2}  // undo last move, and move to tile in NE
       
   198 		};
       
   199 
       
   200 		for (ttm = _terraform_tilepos; ttm != endof(_terraform_tilepos); ttm++) {
       
   201 			tile += ToTileIndexDiff(*ttm);
       
   202 
       
   203 			/* Get TileHeight of neighboured tile as of current terraform progress */
       
   204 			int r = TerraformGetHeightOfTile(ts, tile);
       
   205 			int height_diff = height - r;
       
   206 
       
   207 			/* Is the height difference to the neighboured corner greater than 1? */
       
   208 			if (abs(height_diff) > 1) {
       
   209 				/* Terraform the neighboured corner. The resulting height difference should be 1. */
       
   210 				height_diff += (height_diff < 0 ? 1 : -1);
       
   211 				CommandCost cost = TerraformTileHeight(ts, tile, r + height_diff);
       
   212 				if (CmdFailed(cost)) return cost;
       
   213 				total_cost.AddCost(cost);
       
   214 			}
       
   215 		}
       
   216 	}
       
   217 
       
   218 	return total_cost;
       
   219 }
       
   220 
       
   221 /** Terraform land
       
   222  * @param tile tile to terraform
       
   223  * @param flags for this command type
       
   224  * @param p1 corners to terraform (SLOPE_xxx)
       
   225  * @param p2 direction; eg up (non-zero) or down (zero)
       
   226  * @return error or cost of terraforming
       
   227  */
       
   228 CommandCost CmdTerraformLand(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
       
   229 {
       
   230 	TerraformerState ts;
       
   231 	CommandCost total_cost = CommandCost();
       
   232 	int direction = (p2 != 0 ? 1 : -1);
       
   233 
       
   234 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
       
   235 
       
   236 	_terraform_err_tile = 0;
       
   237 
       
   238 	ts.modheight_count = ts.tile_table_count = 0;
       
   239 
       
   240 	/* Make an extra check for map-bounds cause we add tiles to the originating tile */
       
   241 	if (tile + TileDiffXY(1, 1) >= MapSize()) return CMD_ERROR;
       
   242 
       
   243 	/* Compute the costs and the terraforming result in a model of the landscape */
       
   244 	if ((p1 & SLOPE_W) != 0) {
       
   245 		TileIndex t = tile + TileDiffXY(1, 0);
       
   246 		CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
       
   247 		if (CmdFailed(cost)) return cost;
       
   248 		total_cost.AddCost(cost);
       
   249 	}
       
   250 
       
   251 	if ((p1 & SLOPE_S) != 0) {
       
   252 		TileIndex t = tile + TileDiffXY(1, 1);
       
   253 		CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
       
   254 		if (CmdFailed(cost)) return cost;
       
   255 		total_cost.AddCost(cost);
       
   256 	}
       
   257 
       
   258 	if ((p1 & SLOPE_E) != 0) {
       
   259 		TileIndex t = tile + TileDiffXY(0, 1);
       
   260 		CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
       
   261 		if (CmdFailed(cost)) return cost;
       
   262 		total_cost.AddCost(cost);
       
   263 	}
       
   264 
       
   265 	if ((p1 & SLOPE_N) != 0) {
       
   266 		TileIndex t = tile + TileDiffXY(0, 0);
       
   267 		CommandCost cost = TerraformTileHeight(&ts, t, TileHeight(t) + direction);
       
   268 		if (CmdFailed(cost)) return cost;
       
   269 		total_cost.AddCost(cost);
       
   270 	}
       
   271 
       
   272 	/* Check if the terraforming is valid wrt. tunnels, bridges and objects on the surface */
       
   273 	{
       
   274 		int count;
       
   275 		TileIndex *ti = ts.tile_table;
       
   276 
       
   277 		for (count = ts.tile_table_count; count != 0; count--, ti++) {
       
   278 			TileIndex tile = *ti;
       
   279 
       
   280 			/* Find new heights of tile corners */
       
   281 			uint z_N = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 0));
       
   282 			uint z_W = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 0));
       
   283 			uint z_S = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(1, 1));
       
   284 			uint z_E = TerraformGetHeightOfTile(&ts, tile + TileDiffXY(0, 1));
       
   285 
       
   286 			/* Find min and max height of tile */
       
   287 			uint z_min = min(min(z_N, z_W), min(z_S, z_E));
       
   288 			uint z_max = max(max(z_N, z_W), max(z_S, z_E));
       
   289 
       
   290 			/* Compute tile slope */
       
   291 			uint tileh = (z_max > z_min + 1 ? SLOPE_STEEP : SLOPE_FLAT);
       
   292 			if (z_W > z_min) tileh += SLOPE_W;
       
   293 			if (z_S > z_min) tileh += SLOPE_S;
       
   294 			if (z_E > z_min) tileh += SLOPE_E;
       
   295 			if (z_N > z_min) tileh += SLOPE_N;
       
   296 
       
   297 			/* Check if bridge would take damage */
       
   298 			if (direction == 1 && MayHaveBridgeAbove(tile) && IsBridgeAbove(tile) &&
       
   299 					GetBridgeHeight(GetSouthernBridgeEnd(tile)) <= z_max * TILE_HEIGHT) {
       
   300 				_terraform_err_tile = tile; // highlight the tile under the bridge
       
   301 				return_cmd_error(STR_5007_MUST_DEMOLISH_BRIDGE_FIRST);
       
   302 			}
       
   303 			/* Check if tunnel would take damage */
       
   304 			if (direction == -1 && IsTunnelInWay(tile, z_min * TILE_HEIGHT)) {
       
   305 				_terraform_err_tile = tile; // highlight the tile above the tunnel
       
   306 				return_cmd_error(STR_1002_EXCAVATION_WOULD_DAMAGE);
       
   307 			}
       
   308 			/* Check tiletype-specific things, and add extra-cost */
       
   309 			CommandCost cost = _tile_type_procs[GetTileType(tile)]->terraform_tile_proc(tile, flags | DC_AUTO, z_min * TILE_HEIGHT, (Slope) tileh);
       
   310 			if (CmdFailed(cost)) {
       
   311 				_terraform_err_tile = tile;
       
   312 				return cost;
       
   313 			}
       
   314 			total_cost.AddCost(cost);
       
   315 		}
       
   316 	}
       
   317 
       
   318 	if (flags & DC_EXEC) {
       
   319 		/* change the height */
       
   320 		{
       
   321 			int count;
       
   322 			TerraformerHeightMod *mod;
       
   323 
       
   324 			mod = ts.modheight;
       
   325 			for (count = ts.modheight_count; count != 0; count--, mod++) {
       
   326 				TileIndex til = mod->tile;
       
   327 
       
   328 				SetTileHeight(til, mod->height);
       
   329 			}
       
   330 		}
       
   331 
       
   332 		/* finally mark the dirty tiles dirty */
       
   333 		{
       
   334 			int count;
       
   335 			TileIndex *ti = ts.tile_table;
       
   336 			for (count = ts.tile_table_count; count != 0; count--, ti++) {
       
   337 				MarkTileDirtyByTile(*ti);
       
   338 			}
       
   339 		}
       
   340 	}
       
   341 	return total_cost;
       
   342 }
       
   343 
       
   344 
       
   345 /** Levels a selected (rectangle) area of land
       
   346  * @param tile end tile of area-drag
       
   347  * @param flags for this command type
       
   348  * @param p1 start tile of area drag
       
   349  * @param p2 height difference; eg raise (+1), lower (-1) or level (0)
       
   350  * @return  error or cost of terraforming
       
   351  */
       
   352 CommandCost CmdLevelLand(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
       
   353 {
       
   354 	int size_x, size_y;
       
   355 	int ex;
       
   356 	int ey;
       
   357 	int sx, sy;
       
   358 	uint h, oldh, curh;
       
   359 	CommandCost money;
       
   360 	CommandCost ret;
       
   361 	CommandCost cost;
       
   362 
       
   363 	if (p1 >= MapSize()) return CMD_ERROR;
       
   364 
       
   365 	SET_EXPENSES_TYPE(EXPENSES_CONSTRUCTION);
       
   366 
       
   367 	/* remember level height */
       
   368 	oldh = TileHeight(p1);
       
   369 
       
   370 	/* compute new height */
       
   371 	h = oldh + p2;
       
   372 
       
   373 	/* Check range of destination height */
       
   374 	if (h > MAX_TILE_HEIGHT) return_cmd_error((oldh == 0) ? STR_1003_ALREADY_AT_SEA_LEVEL : STR_1004_TOO_HIGH);
       
   375 
       
   376 	/* make sure sx,sy are smaller than ex,ey */
       
   377 	ex = TileX(tile);
       
   378 	ey = TileY(tile);
       
   379 	sx = TileX(p1);
       
   380 	sy = TileY(p1);
       
   381 	if (ex < sx) Swap(ex, sx);
       
   382 	if (ey < sy) Swap(ey, sy);
       
   383 	tile = TileXY(sx, sy);
       
   384 
       
   385 	size_x = ex - sx + 1;
       
   386 	size_y = ey - sy + 1;
       
   387 
       
   388 	money.AddCost(GetAvailableMoneyForCommand());
       
   389 
       
   390 	BEGIN_TILE_LOOP(tile2, size_x, size_y, tile) {
       
   391 		curh = TileHeight(tile2);
       
   392 		while (curh != h) {
       
   393 			ret = DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND);
       
   394 			if (CmdFailed(ret)) break;
       
   395 
       
   396 			if (flags & DC_EXEC) {
       
   397 				money.AddCost(-ret.GetCost());
       
   398 				if (money.GetCost() < 0) {
       
   399 					_additional_cash_required = ret.GetCost();
       
   400 					return cost;
       
   401 				}
       
   402 				DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
       
   403 			}
       
   404 
       
   405 			cost.AddCost(ret);
       
   406 			curh += (curh > h) ? -1 : 1;
       
   407 		}
       
   408 	} END_TILE_LOOP(tile2, size_x, size_y, tile)
       
   409 
       
   410 	return (cost.GetCost() == 0) ? CMD_ERROR : cost;
       
   411 }