(svn r19914) -Codechange: Wrap a helper class around temporary assignments of _current_company to ensure proper restoration.

This commit is contained in:
frosch
2010-05-31 20:22:57 +00:00
parent 1d05fbebbe
commit 9db3cde73a
19 changed files with 256 additions and 117 deletions

View File

@@ -45,6 +45,7 @@
#include "townname_func.h"
#include "townname_type.h"
#include "core/random_func.hpp"
#include "core/backup_type.hpp"
#include "table/strings.h"
#include "table/town_land.h"
@@ -515,7 +516,7 @@ static void TileLoop_Town(TileIndex tile)
}
}
_current_company = OWNER_TOWN;
Backup<CompanyByte> cur_company(_current_company, OWNER_TOWN);
if ((hs->building_flags & BUILDING_HAS_1_TILE) &&
HasBit(t->flags, TOWN_IS_FUNDED) &&
@@ -530,7 +531,7 @@ static void TileLoop_Town(TileIndex tile)
if (GB(r, 24, 8) >= 12) BuildTownHouse(t, tile);
}
_current_company = OWNER_NONE;
cur_company.Restore();
}
static CommandCost ClearTile_Town(TileIndex tile, DoCommandFlag flags)
@@ -1296,8 +1297,7 @@ static bool GrowTown(Town *t)
};
/* Current "company" is a town */
CompanyID old_company = _current_company;
_current_company = OWNER_TOWN;
Backup<CompanyByte> cur_company(_current_company, OWNER_TOWN);
TileIndex tile = t->xy; // The tile we are working with ATM
@@ -1306,7 +1306,7 @@ static bool GrowTown(Town *t)
for (ptr = _town_coord_mod; ptr != endof(_town_coord_mod); ++ptr) {
if (GetTownRoadBits(tile) != ROAD_NONE) {
int r = GrowTownAtRoad(t, tile);
_current_company = old_company;
cur_company.Restore();
return r != 0;
}
tile = TILE_ADD(tile, ToTileIndexDiff(*ptr));
@@ -1321,7 +1321,7 @@ static bool GrowTown(Town *t)
if (!IsTileType(tile, MP_HOUSE) && GetTileSlope(tile, NULL) == SLOPE_FLAT) {
if (DoCommand(tile, 0, 0, DC_AUTO | DC_NO_WATER, CMD_LANDSCAPE_CLEAR).Succeeded()) {
DoCommand(tile, GenRandomRoadBits(), t->index, DC_EXEC | DC_AUTO, CMD_BUILD_ROAD);
_current_company = old_company;
cur_company.Restore();
return true;
}
}
@@ -1329,7 +1329,7 @@ static bool GrowTown(Town *t)
}
}
_current_company = old_company;
cur_company.Restore();
return false;
}
@@ -2383,10 +2383,9 @@ static bool DoBuildStatueOfCompany(TileIndex tile, TownID town_id)
return false;
}
CompanyID old = _current_company;
_current_company = OWNER_NONE;
Backup<CompanyByte> cur_company(_current_company, OWNER_NONE);
CommandCost r = DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
_current_company = old;
cur_company.Restore();
if (r.Failed()) return false;