(svn r18783) -Codechange: make CheckCompanyHasMoney set an error on the CommandCost it tests when you don't have enough money instead of setting a global variable.

pull/155/head
rubidium 15 years ago
parent 89443276b1
commit 9313532ddd

@ -424,14 +424,12 @@ CommandCost DoCommand(TileIndex tile, uint32 p1, uint32 p2, DoCommandFlag flags,
res = proc(tile, flags & ~DC_EXEC, p1, p2, text); res = proc(tile, flags & ~DC_EXEC, p1, p2, text);
SetTownRatingTestMode(false); SetTownRatingTestMode(false);
if (CmdFailed(res)) { if (CmdFailed(res)) {
res.SetGlobalErrorMessage();
goto error; goto error;
} }
if (_docommand_recursive == 1 && if (_docommand_recursive == 1 &&
!(flags & DC_QUERY_COST) && !(flags & DC_QUERY_COST) &&
!(flags & DC_BANKRUPT) && !(flags & DC_BANKRUPT) &&
res.GetCost() != 0 &&
!CheckCompanyHasMoney(res)) { !CheckCompanyHasMoney(res)) {
goto error; goto error;
} }
@ -446,8 +444,8 @@ CommandCost DoCommand(TileIndex tile, uint32 p1, uint32 p2, DoCommandFlag flags,
* themselves to the cost object at some point */ * themselves to the cost object at some point */
res = proc(tile, flags, p1, p2, text); res = proc(tile, flags, p1, p2, text);
if (CmdFailed(res)) { if (CmdFailed(res)) {
res.SetGlobalErrorMessage();
error: error:
res.SetGlobalErrorMessage();
_docommand_recursive--; _docommand_recursive--;
return CMD_ERROR; return CMD_ERROR;
} }
@ -579,7 +577,10 @@ bool DoCommandP(TileIndex tile, uint32 p1, uint32 p2, uint32 cmd, CommandCallbac
goto show_error; goto show_error;
} }
/* no money? Only check if notest is off */ /* no money? Only check if notest is off */
if (!notest && res.GetCost() != 0 && !CheckCompanyHasMoney(res)) goto show_error; if (!notest && res.GetCost() != 0 && !CheckCompanyHasMoney(res)) {
res.SetGlobalErrorMessage();
goto show_error;
}
} }
#ifdef ENABLE_NETWORK #ifdef ENABLE_NETWORK

@ -107,6 +107,17 @@ public:
if (this->message != INVALID_STRING_ID) _error_message = this->message; if (this->message != INVALID_STRING_ID) _error_message = this->message;
} }
/**
* Makes this CommandCost behave like an error command.
* @param mesasge the error message.
*/
void MakeError(StringID message)
{
assert(message != INVALID_STRING_ID);
this->success = false;
this->message = message;
}
/** /**
* Returns the error message of a command * Returns the error message of a command
* @return the error message, if succeeded INVALID_STRING_ID * @return the error message, if succeeded INVALID_STRING_ID

@ -156,13 +156,13 @@ void InvalidateCompanyWindows(const Company *company)
SetWindowDirty(WC_FINANCES, cid); SetWindowDirty(WC_FINANCES, cid);
} }
bool CheckCompanyHasMoney(CommandCost cost) bool CheckCompanyHasMoney(CommandCost &cost)
{ {
if (cost.GetCost() > 0) { if (cost.GetCost() > 0) {
const Company *c = Company::GetIfValid(_current_company); const Company *c = Company::GetIfValid(_current_company);
if (c != NULL && cost.GetCost() > c->money) { if (c != NULL && cost.GetCost() > c->money) {
SetDParam(0, cost.GetCost()); SetDParam(0, cost.GetCost());
_error_message = STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY; cost.MakeError(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY);
return false; return false;
} }
} }

@ -23,7 +23,7 @@ void DrawClearLandFence(const TileInfo *ti);
void TileLoopClearHelper(TileIndex tile); void TileLoopClearHelper(TileIndex tile);
/* company_cmd.cpp */ /* company_cmd.cpp */
bool CheckCompanyHasMoney(CommandCost cost); bool CheckCompanyHasMoney(CommandCost &cost);
void SubtractMoneyFromCompany(CommandCost cost); void SubtractMoneyFromCompany(CommandCost cost);
void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cost); void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cost);
bool CheckOwnership(Owner owner, TileIndex tile = 0); bool CheckOwnership(Owner owner, TileIndex tile = 0);

@ -588,7 +588,7 @@ CommandCost CmdCloneVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
/* The vehicle has already been bought, so now it must be sold again. */ /* The vehicle has already been bought, so now it must be sold again. */
DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front)); DoCommand(w_front->tile, w_front->index, 1, flags, GetCmdSellVeh(w_front));
} }
return CMD_ERROR; return total_cost;
} }
return total_cost; return total_cost;

Loading…
Cancel
Save