summaryrefslogtreecommitdiff
path: root/src/economy.cpp
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2010-05-31 20:22:57 +0000
committerfrosch <frosch@openttd.org>2010-05-31 20:22:57 +0000
commit9db3cde73af598852da13b748eef31a8207cfcf2 (patch)
tree996d3e9c49ce7c3611a70ed4fa6f03774b4cafe9 /src/economy.cpp
parent1d05fbebbef72722d400ab4f7693dde11205a7f4 (diff)
downloadopenttd-9db3cde73af598852da13b748eef31a8207cfcf2.tar.xz
(svn r19914) -Codechange: Wrap a helper class around temporary assignments of _current_company to ensure proper restoration.
Diffstat (limited to 'src/economy.cpp')
-rw-r--r--src/economy.cpp33
1 files changed, 17 insertions, 16 deletions
diff --git a/src/economy.cpp b/src/economy.cpp
index 4107852e3..21e9516c3 100644
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -47,6 +47,7 @@
#include "core/pool_func.hpp"
#include "newgrf.h"
#include "engine_base.h"
+#include "core/backup_type.hpp"
#include "table/strings.h"
#include "table/sprites.h"
@@ -304,7 +305,7 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner)
#endif /* ENABLE_NETWORK */
Town *t;
- CompanyID old = _current_company;
+ Backup<CompanyByte> cur_company(_current_company, old_owner);
assert(old_owner != new_owner);
@@ -313,7 +314,6 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner)
uint i;
/* See if the old_owner had shares in other companies */
- _current_company = old_owner;
FOR_ALL_COMPANIES(c) {
for (i = 0; i < 4; i++) {
if (c->share_owners[i] == old_owner) {
@@ -327,9 +327,10 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner)
}
/* Sell all the shares that people have on this company */
+ Backup<CompanyByte> cur_company2(_current_company);
c = Company::Get(old_owner);
for (i = 0; i < 4; i++) {
- _current_company = c->share_owners[i];
+ cur_company2.Change(c->share_owners[i]);
if (_current_company != INVALID_OWNER) {
/* Sell the shares */
CommandCost res = DoCommand(0, old_owner, 0, DC_EXEC, CMD_SELL_SHARE_IN_COMPANY);
@@ -338,10 +339,9 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner)
SubtractMoneyFromCompany(res);
}
}
+ cur_company2.Restore();
}
- _current_company = old_owner;
-
/* Temporarily increase the company's money, to be sure that
* removing his/her property doesn't fail because of lack of money.
* Not too drastically though, because it could overflow */
@@ -472,7 +472,7 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner)
/* Change colour of existing windows */
if (new_owner != INVALID_OWNER) ChangeWindowOwner(old_owner, new_owner);
- _current_company = old;
+ cur_company.Restore();
MarkWholeScreenDirty();
}
@@ -553,11 +553,13 @@ static void CompaniesGenStatistics()
Station *st;
Company *c;
+ Backup<CompanyByte> cur_company(_current_company);
FOR_ALL_STATIONS(st) {
- _current_company = st->owner;
+ cur_company.Change(st->owner);
CommandCost cost(EXPENSES_PROPERTY, _price[PR_STATION_VALUE] >> 1);
SubtractMoneyFromCompany(cost);
}
+ cur_company.Restore();
if (!HasBit(1 << 0 | 1 << 3 | 1 << 6 | 1 << 9, _cur_month))
return;
@@ -686,8 +688,9 @@ static void CompaniesPayInterest()
{
const Company *c;
+ Backup<CompanyByte> cur_company(_current_company);
FOR_ALL_COMPANIES(c) {
- _current_company = c->index;
+ cur_company.Change(c->index);
/* Over a year the paid interest should be "loan * interest percentage",
* but... as that number is likely not dividable by 12 (pay each month),
@@ -705,6 +708,7 @@ static void CompaniesPayInterest()
SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, _price[PR_STATION_VALUE] >> 2));
}
+ cur_company.Restore();
}
static void HandleEconomyFluctuations()
@@ -1023,8 +1027,7 @@ CargoPayment::~CargoPayment()
if (this->visual_profit == 0) return;
- CompanyID old_company = _current_company;
- _current_company = this->front->owner;
+ Backup<CompanyByte> cur_company(_current_company, this->front->owner);
SubtractMoneyFromCompany(CommandCost(this->front->GetExpenseType(true), -this->route_profit));
this->front->profit_this_year += this->visual_profit << 8;
@@ -1039,7 +1042,7 @@ CargoPayment::~CargoPayment()
ShowFeederIncomeAnimation(this->front->x_pos, this->front->y_pos, this->front->z_pos, this->visual_profit);
}
- _current_company = old_company;
+ cur_company.Restore();
}
/**
@@ -1426,8 +1429,6 @@ void CompaniesMonthlyLoop()
RecomputePrices();
}
CompaniesPayInterest();
- /* Reset the _current_company flag */
- _current_company = OWNER_NONE;
HandleEconomyFluctuations();
}
@@ -1457,14 +1458,14 @@ static void DoAcquireCompany(Company *c)
}
value = CalculateCompanyValue(c) >> 2;
- CompanyID old_company = _current_company;
+ Backup<CompanyByte> cur_company(_current_company);
for (i = 0; i != 4; i++) {
if (c->share_owners[i] != COMPANY_SPECTATOR) {
- _current_company = c->share_owners[i];
+ cur_company.Change(c->share_owners[i]);
SubtractMoneyFromCompany(CommandCost(EXPENSES_OTHER, -value));
}
}
- _current_company = old_company;
+ cur_company.Restore();
if (c->is_ai) AI::Stop(c->index);