Changeset - r14165:d2ba2529435c
[Not reviewed]
master
0 3 0
rubidium - 15 years ago 2010-01-04 18:49:27
rubidium@openttd.org
(svn r18723) -Codechange: also simplify looping over an area when building trees, desert, rocky areas or leveling land
3 files changed with 75 insertions and 118 deletions:
0 comments (0 inline, 0 general)
src/terraform_cmd.cpp
Show inline comments
 
@@ -378,25 +378,14 @@ CommandCost CmdLevelLand(TileIndex tile,
 
	if (h > MAX_TILE_HEIGHT) return_cmd_error((oldh == 0) ? STR_ERROR_ALREADY_AT_SEA_LEVEL : STR_ERROR_TOO_HIGH);
 
	if (p2 == 0) _error_message = STR_ERROR_ALREADY_LEVELLED;
 

	
 
	/* make sure sx,sy are smaller than ex,ey */
 
	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);
 

	
 
	int size_x = ex - sx + 1;
 
	int size_y = ey - sy + 1;
 

	
 
	Money money = GetAvailableMoneyForCommand();
 
	CommandCost cost(EXPENSES_CONSTRUCTION);
 

	
 
	TILE_LOOP(tile2, size_x, size_y, tile) {
 
		uint curh = TileHeight(tile2);
 
	TileArea ta(tile, p1);
 
	TILE_AREA_LOOP(tile, ta) {
 
		uint curh = TileHeight(tile);
 
		while (curh != h) {
 
			CommandCost ret = DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND);
 
			CommandCost ret = DoCommand(tile, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND);
 
			if (CmdFailed(ret)) break;
 

	
 
			if (flags & DC_EXEC) {
 
@@ -405,7 +394,7 @@ CommandCost CmdLevelLand(TileIndex tile,
 
					_additional_cash_required = ret.GetCost();
 
					return cost;
 
				}
 
				DoCommand(tile2, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
 
				DoCommand(tile, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
 
			}
 

	
 
			cost.AddCost(ret);
src/terraform_gui.cpp
Show inline comments
 
@@ -50,21 +50,12 @@ void CcTerraform(bool success, TileIndex
 
/** Scenario editor command that generates desert areas */
 
static void GenerateDesertArea(TileIndex end, TileIndex start)
 
{
 
	int size_x, size_y;
 
	int sx = TileX(start);
 
	int sy = TileY(start);
 
	int ex = TileX(end);
 
	int ey = TileY(end);
 

	
 
	if (_game_mode != GM_EDITOR) return;
 

	
 
	if (ex < sx) Swap(ex, sx);
 
	if (ey < sy) Swap(ey, sy);
 
	size_x = (ex - sx) + 1;
 
	size_y = (ey - sy) + 1;
 
	_generating_world = true;
 

	
 
	_generating_world = true;
 
	TILE_LOOP(tile, size_x, size_y, TileXY(sx, sy)) {
 
	TileArea ta(start, end);
 
	TILE_AREA_LOOP(tile, ta) {
 
		SetTropicZone(tile, (_ctrl_pressed) ? TROPICZONE_NORMAL : TROPICZONE_DESERT);
 
		DoCommandP(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
 
		MarkTileDirtyByTile(tile);
 
@@ -75,21 +66,12 @@ static void GenerateDesertArea(TileIndex
 
/** Scenario editor command that generates rocky areas */
 
static void GenerateRockyArea(TileIndex end, TileIndex start)
 
{
 
	int size_x, size_y;
 
	bool success = false;
 
	int sx = TileX(start);
 
	int sy = TileY(start);
 
	int ex = TileX(end);
 
	int ey = TileY(end);
 

	
 
	if (_game_mode != GM_EDITOR) return;
 

	
 
	if (ex < sx) Swap(ex, sx);
 
	if (ey < sy) Swap(ey, sy);
 
	size_x = (ex - sx) + 1;
 
	size_y = (ey - sy) + 1;
 
	bool success = false;
 
	TileArea ta(start, end);
 

	
 
	TILE_LOOP(tile, size_x, size_y, TileXY(sx, sy)) {
 
	TILE_AREA_LOOP(tile, ta) {
 
		switch (GetTileType(tile)) {
 
			case MP_TREES:
 
				if (GetTreeGround(tile) == TREE_GROUND_SHORE) continue;
src/tree_cmd.cpp
Show inline comments
 
@@ -345,98 +345,84 @@ CommandCost CmdPlantTree(TileIndex tile,
 
{
 
	StringID msg = INVALID_STRING_ID;
 
	CommandCost cost(EXPENSES_OTHER);
 
	int ex;
 
	int ey;
 
	int sx, sy, x, y;
 

	
 
	if (p2 >= MapSize()) return CMD_ERROR;
 
	/* Check the tree type. It can be random or some valid value within the current climate */
 
	if (p1 != UINT_MAX && p1 - _tree_base_by_landscape[_settings_game.game_creation.landscape] >= _tree_count_by_landscape[_settings_game.game_creation.landscape]) return CMD_ERROR;
 

	
 
	/* make sure sx,sy are smaller than ex, ey */
 
	ex = TileX(tile);
 
	ey = TileY(tile);
 
	sx = TileX(p2);
 
	sy = TileY(p2);
 
	if (ex < sx) Swap(ex, sx);
 
	if (ey < sy) Swap(ey, sy);
 
	TileArea ta(tile, p2);
 
	TILE_AREA_LOOP(tile, ta) {
 
		switch (GetTileType(tile)) {
 
			case MP_TREES:
 
				/* no more space for trees? */
 
				if (_game_mode != GM_EDITOR && GetTreeCount(tile) == 4) {
 
					msg = STR_ERROR_TREE_ALREADY_HERE;
 
					continue;
 
				}
 

	
 
				if (flags & DC_EXEC) {
 
					AddTreeCount(tile, 1);
 
					MarkTileDirtyByTile(tile);
 
				}
 
				/* 2x as expensive to add more trees to an existing tile */
 
				cost.AddCost(_price[PR_BUILD_TREES] * 2);
 
				break;
 

	
 
	for (x = sx; x <= ex; x++) {
 
		for (y = sy; y <= ey; y++) {
 
			TileIndex tile = TileXY(x, y);
 
			case MP_WATER:
 
				if (!IsCoast(tile) || IsSlopeWithOneCornerRaised(GetTileSlope(tile, NULL))) {
 
					msg = STR_ERROR_CAN_T_BUILD_ON_WATER;
 
					continue;
 
				}
 
			/* FALL THROUGH */
 
			case MP_CLEAR:
 
				if (IsBridgeAbove(tile)) {
 
					msg = STR_ERROR_SITE_UNSUITABLE;
 
					continue;
 
				}
 

	
 
			switch (GetTileType(tile)) {
 
				case MP_TREES:
 
					/* no more space for trees? */
 
					if (_game_mode != GM_EDITOR && GetTreeCount(tile) == 4) {
 
						msg = STR_ERROR_TREE_ALREADY_HERE;
 
						continue;
 
				if (IsTileType(tile, MP_CLEAR)) {
 
					/* Remove fields or rocks. Note that the ground will get barrened */
 
					switch (GetClearGround(tile)) {
 
						case CLEAR_FIELDS:
 
						case CLEAR_ROCKS: {
 
							CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
							if (CmdFailed(ret)) return ret;
 
							cost.AddCost(ret);
 
							break;
 
						}
 

	
 
						default: break;
 
					}
 
				}
 

	
 
					if (flags & DC_EXEC) {
 
						AddTreeCount(tile, 1);
 
						MarkTileDirtyByTile(tile);
 
					}
 
					/* 2x as expensive to add more trees to an existing tile */
 
					cost.AddCost(_price[PR_BUILD_TREES] * 2);
 
					break;
 
				if (_game_mode != GM_EDITOR && Company::IsValidID(_current_company)) {
 
					Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
 
					if (t != NULL) ChangeTownRating(t, RATING_TREE_UP_STEP, RATING_TREE_MAXIMUM, flags);
 
				}
 

	
 
				case MP_WATER:
 
					if (!IsCoast(tile) || IsSlopeWithOneCornerRaised(GetTileSlope(tile, NULL))) {
 
						msg = STR_ERROR_CAN_T_BUILD_ON_WATER;
 
						continue;
 
					}
 
				/* FALL THROUGH */
 
				case MP_CLEAR:
 
					if (IsBridgeAbove(tile)) {
 
						msg = STR_ERROR_SITE_UNSUITABLE;
 
						continue;
 
				if (flags & DC_EXEC) {
 
					TreeType treetype;
 

	
 
					treetype = (TreeType)p1;
 
					if (treetype == TREE_INVALID) {
 
						treetype = GetRandomTreeType(tile, GB(Random(), 24, 8));
 
						if (treetype == TREE_INVALID) treetype = TREE_CACTUS;
 
					}
 

	
 
					if (IsTileType(tile, MP_CLEAR)) {
 
						/* Remove fields or rocks. Note that the ground will get barrened */
 
						switch (GetClearGround(tile)) {
 
							case CLEAR_FIELDS:
 
							case CLEAR_ROCKS: {
 
								CommandCost ret = DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
 
								if (CmdFailed(ret)) return ret;
 
								cost.AddCost(ret);
 
								break;
 
							}
 

	
 
							default: break;
 
						}
 
					}
 

	
 
					if (_game_mode != GM_EDITOR && Company::IsValidID(_current_company)) {
 
						Town *t = ClosestTownFromTile(tile, _settings_game.economy.dist_local_authority);
 
						if (t != NULL) ChangeTownRating(t, RATING_TREE_UP_STEP, RATING_TREE_MAXIMUM, flags);
 
					}
 
					/* Plant full grown trees in scenario editor */
 
					PlantTreesOnTile(tile, treetype, 0, _game_mode == GM_EDITOR ? 3 : 0);
 
					MarkTileDirtyByTile(tile);
 

	
 
					if (flags & DC_EXEC) {
 
						TreeType treetype;
 

	
 
						treetype = (TreeType)p1;
 
						if (treetype == TREE_INVALID) {
 
							treetype = GetRandomTreeType(tile, GB(Random(), 24, 8));
 
							if (treetype == TREE_INVALID) treetype = TREE_CACTUS;
 
						}
 
					/* When planting rainforest-trees, set tropiczone to rainforest in editor. */
 
					if (_game_mode == GM_EDITOR && IsInsideMM(treetype, TREE_RAINFOREST, TREE_CACTUS))
 
						SetTropicZone(tile, TROPICZONE_RAINFOREST);
 
				}
 
				cost.AddCost(_price[PR_BUILD_TREES]);
 
				break;
 

	
 
						/* Plant full grown trees in scenario editor */
 
						PlantTreesOnTile(tile, treetype, 0, _game_mode == GM_EDITOR ? 3 : 0);
 
						MarkTileDirtyByTile(tile);
 

	
 
						/* When planting rainforest-trees, set tropiczone to rainforest in editor. */
 
						if (_game_mode == GM_EDITOR && IsInsideMM(treetype, TREE_RAINFOREST, TREE_CACTUS))
 
							SetTropicZone(tile, TROPICZONE_RAINFOREST);
 
					}
 
					cost.AddCost(_price[PR_BUILD_TREES]);
 
					break;
 

	
 
				default:
 
					msg = STR_ERROR_SITE_UNSUITABLE;
 
					break;
 
			}
 
			default:
 
				msg = STR_ERROR_SITE_UNSUITABLE;
 
				break;
 
		}
 
	}
 

	
0 comments (0 inline, 0 general)