(svn r9294) [NoAI] -Codechange: make changing the loan to 'any' amount a single command. noai
authorrubidium
Sun, 18 Mar 2007 20:56:03 +0000
branchnoai
changeset 9462 a4f49aab1367
parent 9461 ab1079656642
child 9463 4019df972af1
(svn r9294) [NoAI] -Codechange: make changing the loan to 'any' amount a single command.
src/ai/api/ai_accounting.hpp
src/ai/api/ai_company.cpp
src/ai/api/ai_road.hpp
src/misc_cmd.cpp
--- a/src/ai/api/ai_accounting.hpp	Sun Mar 18 20:15:57 2007 +0000
+++ b/src/ai/api/ai_accounting.hpp	Sun Mar 18 20:56:03 2007 +0000
@@ -51,7 +51,7 @@
 	DefSQClass <AIAccounting> SQAIAccounting("AIAccounting");
 	SQAIAccounting.PreRegister(engine);
 	SQAIAccounting.AddConstructor(engine);
-	SQAIAccounting.DefSQFunction(engine, &AIAccounting::GetCosts, "GetCosts");
+	SQAIAccounting.DefSQFunction(engine, &AIAccounting::GetCosts,   "GetCosts");
 	SQAIAccounting.DefSQFunction(engine, &AIAccounting::ResetCosts, "ResetCosts");
 	SQAIAccounting.PostRegister(engine);
 }
--- a/src/ai/api/ai_company.cpp	Sun Mar 18 20:15:57 2007 +0000
+++ b/src/ai/api/ai_company.cpp	Sun Mar 18 20:56:03 2007 +0000
@@ -59,19 +59,9 @@
 		return false;
 	}
 
-	/* When we get/repay everything at once (or the maximum we can repay),
-	 * use the shortcut for that. Otherwise send several commands at once */
-	if (loan == 0) {
-		return this->DoCommand(0, 0, true, DC_EXEC, CMD_DECREASE_LOAN);
-	}
-	if (loan == this->GetMaxLoanAmount()) {
-		return this->DoCommand(0, 0, true, DC_EXEC, CMD_INCREASE_LOAN);
-	}
+	if (loan == this->GetLoanAmount()) return true;
 
-	bool increase = loan > this->GetLoanAmount();
-	for (uint diff_loan = abs(loan - this->GetLoanAmount()) / this->GetLoanInterval(); diff_loan > 0; diff_loan--) {
-		if (!this->DoCommand(0, 0, false, DC_EXEC, increase ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN)) return false;
-	}
-
-	return true;
+	return this->DoCommand(0,
+			abs(loan - this->GetLoanAmount()), 2, DC_EXEC,
+			(loan > this->GetLoanAmount()) ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN);
 }
--- a/src/ai/api/ai_road.hpp	Sun Mar 18 20:15:57 2007 +0000
+++ b/src/ai/api/ai_road.hpp	Sun Mar 18 20:56:03 2007 +0000
@@ -108,6 +108,7 @@
 	DefSQClass <AIRoad> SQAIRoad("AIRoad");
 	SQAIRoad.PreRegister(engine);
 	SQAIRoad.AddConstructor(engine);
+	SQAIRoad.DefSQFunction(engine, &AIRoad::IsRoadTile,        "IsRoadTile");
 	SQAIRoad.DefSQFunction(engine, &AIRoad::BuildRoad,         "BuildRoad");
 	SQAIRoad.DefSQFunction(engine, &AIRoad::BuildRoadDepot,    "BuildRoadDepot");
 	SQAIRoad.DefSQFunction(engine, &AIRoad::BuildRoadStation,  "BuildRoadStation");
--- a/src/misc_cmd.cpp	Sun Mar 18 20:15:57 2007 +0000
+++ b/src/misc_cmd.cpp	Sun Mar 18 20:56:03 2007 +0000
@@ -117,23 +117,41 @@
 
 /** Increase the loan of your company.
  * @param tile unused
- * @param p1 unused
- * @param p2 when set, loans the maximum amount in one go (press CTRL)
+ * @param p1 amount to increase the loan with, multitude of LOAN_INTERVAL. Only used when p2 == 2.
+ * @param p2 when 0: loans LOAN_INTERVAL
+ *           when 1: loans the maximum loan permitting money (press CTRL),
+ *           when 2: loans the amount specified in p1
  */
 int32 CmdIncreaseLoan(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
-	Player *p;
-
-	p = GetPlayer(_current_player);
+	Player *p = GetPlayer(_current_player);
 
 	if (p->current_loan >= _economy.max_loan) {
 		SetDParam(0, _economy.max_loan);
 		return_cmd_error(STR_702B_MAXIMUM_PERMITTED_LOAN);
 	}
 
+	/* Invalid method */
+	if (p2 >= 3) return CMD_ERROR;
+
+	/* Invalid amount to increase the loan with */
+	if (p2 == 2 && (((int32)p1 < LOAN_INTERVAL) || p->current_loan + (int32)p1 > _economy.max_loan || (p1 % LOAN_INTERVAL) != 0)) return CMD_ERROR;
+
 	if (flags & DC_EXEC) {
-		/* Loan the maximum amount or not? */
-		int32 loan = (p2) ? _economy.max_loan - p->current_loan : LOAN_INTERVAL;
+		int32 loan;
+		/* Amount to loan */
+		switch (p2) {
+			default: NOT_REACHED();
+			case 0:
+				loan = LOAN_INTERVAL;
+				break;
+			case 1:
+				loan = _economy.max_loan - p->current_loan;
+				break;
+			case 2:
+				loan = p1;
+				break;
+		}
 
 		p->money64 += loan;
 		p->current_loan += loan;
@@ -146,28 +164,31 @@
 
 /** Decrease the loan of your company.
  * @param tile unused
- * @param p1 unused
- * @param p2 when set, pays back the maximum loan permitting money (press CTRL)
+ * @param p1 amount to decrease the loan with, multitude of LOAN_INTERVAL. Only used when p2 == 2.
+ * @param p2 when 0: pays back LOAN_INTERVAL
+ *           when 1: pays back the maximum loan permitting money (press CTRL),
+ *           when 2: pays back the amount specified in p1
  */
 int32 CmdDecreaseLoan(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
 {
-	Player *p;
-	int32 loan;
-
-	p = GetPlayer(_current_player);
+	Player *p = GetPlayer(_current_player);
 
 	if (p->current_loan == 0) return_cmd_error(STR_702D_LOAN_ALREADY_REPAYED);
 
-	loan = p->current_loan;
-
-	/* p2 is true while CTRL is pressed (repay all possible loan, or max money you have)
-	 * Repay any loan in chunks of 10.000 (LOAN_INTERVAL) pounds */
-	if (p2) {
-		loan = min(loan, p->player_money);
-		loan = max(loan, (int32)LOAN_INTERVAL);
-		loan -= loan % LOAN_INTERVAL;
-	} else {
-		loan = min(loan, LOAN_INTERVAL);
+	int32 loan;
+	switch (p2) {
+		default: return CMD_ERROR; // Invalid method
+		case 0:
+			loan = min(p->current_loan, LOAN_INTERVAL);
+			break;
+		case 1:
+			loan = max(min(p->current_loan, p->player_money), (int32)LOAN_INTERVAL);
+			loan -= loan % LOAN_INTERVAL;
+			break;
+		case 2:
+			if ((p1 % LOAN_INTERVAL != 0) || ((int32)p1 < LOAN_INTERVAL)) return CMD_ERROR; // Invalid amount to loan
+			loan = p1;
+			break;
 	}
 
 	if (p->player_money < loan) {