changeset 14232:860ea22258ba draft

(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.
author rubidium <rubidium@openttd.org>
date Mon, 11 Jan 2010 20:21:56 +0000
parents ae8cf918bc27
children 613d8459ebe1
files src/command.cpp src/command_type.h src/company_cmd.cpp src/functions.h src/vehicle_cmd.cpp
diffstat 5 files changed, 20 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/command.cpp
+++ b/src/command.cpp
@@ -424,14 +424,12 @@
 		res = proc(tile, flags & ~DC_EXEC, p1, p2, text);
 		SetTownRatingTestMode(false);
 		if (CmdFailed(res)) {
-			res.SetGlobalErrorMessage();
 			goto error;
 		}
 
 		if (_docommand_recursive == 1 &&
 				!(flags & DC_QUERY_COST) &&
 				!(flags & DC_BANKRUPT) &&
-				res.GetCost() != 0 &&
 				!CheckCompanyHasMoney(res)) {
 			goto error;
 		}
@@ -446,8 +444,8 @@
 	 * themselves to the cost object at some point */
 	res = proc(tile, flags, p1, p2, text);
 	if (CmdFailed(res)) {
+error:
 		res.SetGlobalErrorMessage();
-error:
 		_docommand_recursive--;
 		return CMD_ERROR;
 	}
@@ -579,7 +577,10 @@
 			goto show_error;
 		}
 		/* 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
--- a/src/command_type.h
+++ b/src/command_type.h
@@ -108,6 +108,17 @@
 	}
 
 	/**
+	 * 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
 	 * @return the error message, if succeeded INVALID_STRING_ID
 	 */
--- a/src/company_cmd.cpp
+++ b/src/company_cmd.cpp
@@ -156,13 +156,13 @@
 	SetWindowDirty(WC_FINANCES, cid);
 }
 
-bool CheckCompanyHasMoney(CommandCost cost)
+bool CheckCompanyHasMoney(CommandCost &cost)
 {
 	if (cost.GetCost() > 0) {
 		const Company *c = Company::GetIfValid(_current_company);
 		if (c != NULL && cost.GetCost() > c->money) {
 			SetDParam(0, cost.GetCost());
-			_error_message = STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY;
+			cost.MakeError(STR_ERROR_NOT_ENOUGH_CASH_REQUIRES_CURRENCY);
 			return false;
 		}
 	}
--- a/src/functions.h
+++ b/src/functions.h
@@ -23,7 +23,7 @@
 void TileLoopClearHelper(TileIndex tile);
 
 /* company_cmd.cpp */
-bool CheckCompanyHasMoney(CommandCost cost);
+bool CheckCompanyHasMoney(CommandCost &cost);
 void SubtractMoneyFromCompany(CommandCost cost);
 void SubtractMoneyFromCompanyFract(CompanyID company, CommandCost cost);
 bool CheckOwnership(Owner owner, TileIndex tile = 0);
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -588,7 +588,7 @@
 			/* 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));
 		}
-		return CMD_ERROR;
+		return total_cost;
 	}
 
 	return total_cost;