diff -r eec5a7dcbf61 -r fcf5fb2548eb src/terraform_cmd.cpp --- a/src/terraform_cmd.cpp Mon Apr 14 20:32:36 2008 +0000 +++ b/src/terraform_cmd.cpp Tue Apr 15 00:47:19 2008 +0000 @@ -35,8 +35,8 @@ }; struct TerraformerState { - int modheight_count; ///< amount of entries in "modheight". - int tile_table_count; ///< amount of entries in "tile_table". + int modheight_count; ///< amount of entries in "modheight". + int tile_table_count; ///< amount of entries in "tile_table". /** * Dirty tiles, i.e.\ at least one corner changed. @@ -49,7 +49,7 @@ TerraformerHeightMod modheight[TERRAFORMER_MODHEIGHT_SIZE]; ///< Height modifications. }; -TileIndex _terraform_err_tile; +TileIndex _terraform_err_tile; ///< first tile we couldn't terraform /** * Gets the TileHeight (height of north corner) of a tile as of current terraforming progress. @@ -58,12 +58,11 @@ * @param tile Tile. * @return TileHeight. */ -static int TerraformGetHeightOfTile(TerraformerState *ts, TileIndex tile) +static int TerraformGetHeightOfTile(const TerraformerState *ts, TileIndex tile) { - TerraformerHeightMod *mod = ts->modheight; - int count; + const TerraformerHeightMod *mod = ts->modheight; - for (count = ts->modheight_count; count != 0; count--, mod++) { + for (int count = ts->modheight_count; count != 0; count--, mod++) { if (mod->tile == tile) return mod->height; } @@ -85,6 +84,7 @@ * But during house- or industry-construction multiple corners can be terraformed at once. */ TerraformerHeightMod *mod = ts->modheight; int count = ts->modheight_count; + while ((count > 0) && (mod->tile != tile)) { mod++; count--; @@ -110,12 +110,9 @@ */ static void TerraformAddDirtyTile(TerraformerState *ts, TileIndex tile) { - int count; - TileIndex *t; + int count = ts->tile_table_count; - count = ts->tile_table_count; - - for (t = ts->tile_table; count != 0; count--, t++) { + for (TileIndex *t = ts->tile_table; count != 0; count--, t++) { if (*t == tile) return; } @@ -149,8 +146,6 @@ */ static CommandCost TerraformTileHeight(TerraformerState *ts, TileIndex tile, int height) { - CommandCost total_cost(EXPENSES_CONSTRUCTION); - assert(tile < MapSize()); /* Check range of destination height */ @@ -184,6 +179,8 @@ /* Store the height modification */ TerraformSetHeightOfTile(ts, tile, height); + CommandCost total_cost(EXPENSES_CONSTRUCTION); + /* Increment cost */ total_cost.AddCost(_price.terraform); @@ -228,17 +225,17 @@ */ CommandCost CmdTerraformLand(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) { - TerraformerState ts; + /* Make an extra check for map-bounds cause we add tiles to the originating tile */ + if (tile + TileDiffXY(1, 1) >= MapSize()) return CMD_ERROR; + + _terraform_err_tile = INVALID_TILE; + CommandCost total_cost(EXPENSES_CONSTRUCTION); int direction = (p2 != 0 ? 1 : -1); - - _terraform_err_tile = 0; + TerraformerState ts; ts.modheight_count = ts.tile_table_count = 0; - /* Make an extra check for map-bounds cause we add tiles to the originating tile */ - if (tile + TileDiffXY(1, 1) >= MapSize()) return CMD_ERROR; - /* Compute the costs and the terraforming result in a model of the landscape */ if ((p1 & SLOPE_W) != 0) { TileIndex t = tile + TileDiffXY(1, 0); @@ -270,10 +267,9 @@ /* Check if the terraforming is valid wrt. tunnels, bridges and objects on the surface */ { - int count; TileIndex *ti = ts.tile_table; - for (count = ts.tile_table_count; count != 0; count--, ti++) { + for (int count = ts.tile_table_count; count != 0; count--, ti++) { TileIndex tile = *ti; /* Find new heights of tile corners */ @@ -287,11 +283,11 @@ uint z_max = max(max(z_N, z_W), max(z_S, z_E)); /* Compute tile slope */ - uint 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; + 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; /* Check if bridge would take damage */ if (direction == 1 && MayHaveBridgeAbove(tile) && IsBridgeAbove(tile) && @@ -305,7 +301,7 @@ return_cmd_error(STR_1002_EXCAVATION_WOULD_DAMAGE); } /* Check tiletype-specific things, and add extra-cost */ - CommandCost cost = _tile_type_procs[GetTileType(tile)]->terraform_tile_proc(tile, flags | DC_AUTO, z_min * TILE_HEIGHT, (Slope) tileh); + CommandCost cost = _tile_type_procs[GetTileType(tile)]->terraform_tile_proc(tile, flags | DC_AUTO, z_min * TILE_HEIGHT, tileh); if (CmdFailed(cost)) { _terraform_err_tile = tile; return cost; @@ -350,49 +346,41 @@ */ CommandCost CmdLevelLand(TileIndex tile, uint32 flags, uint32 p1, uint32 p2) { - int size_x, size_y; - int ex; - int ey; - int sx, sy; - uint h, oldh, curh; - CommandCost money; - CommandCost ret; - CommandCost cost(EXPENSES_CONSTRUCTION); - if (p1 >= MapSize()) return CMD_ERROR; /* remember level height */ - oldh = TileHeight(p1); + uint oldh = TileHeight(p1); /* compute new height */ - h = oldh + p2; + uint h = oldh + p2; /* Check range of destination height */ if (h > MAX_TILE_HEIGHT) return_cmd_error((oldh == 0) ? STR_1003_ALREADY_AT_SEA_LEVEL : STR_1004_TOO_HIGH); /* make sure sx,sy are smaller than ex,ey */ - ex = TileX(tile); - ey = TileY(tile); - sx = TileX(p1); - sy = TileY(p1); + int ex = TileX(tile); + int ey = TileY(tile); + int sx = TileX(p1); + int sy = TileY(p1); if (ex < sx) Swap(ex, sx); if (ey < sy) Swap(ey, sy); tile = TileXY(sx, sy); - size_x = ex - sx + 1; - size_y = ey - sy + 1; + int size_x = ex - sx + 1; + int size_y = ey - sy + 1; - money.AddCost(GetAvailableMoneyForCommand()); + Money money = GetAvailableMoneyForCommand(); + CommandCost cost(EXPENSES_CONSTRUCTION); BEGIN_TILE_LOOP(tile2, size_x, size_y, tile) { - curh = TileHeight(tile2); + uint curh = TileHeight(tile2); while (curh != h) { - ret = DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND); + CommandCost ret = DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND); if (CmdFailed(ret)) break; if (flags & DC_EXEC) { - money.AddCost(-ret.GetCost()); - if (money.GetCost() < 0) { + money -= ret.GetCost(); + if (money < 0) { _additional_cash_required = ret.GetCost(); return cost; }