summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorglx22 <glx@openttd.org>2020-12-27 23:38:45 +0100
committerglx22 <glx22@users.noreply.github.com>2020-12-28 16:51:15 +0100
commitf7e48cac87e4f9dbf052a21d8221967903dbebfa (patch)
tree48c4c6a4ac0fe54f8eb699d8a6d25e7abe479aab
parent94057e9b181842e8d0cae1aaaada8e4ba6d131b9 (diff)
downloadopenttd-f7e48cac87e4f9dbf052a21d8221967903dbebfa.tar.xz
Fix #8453: [Script] Don't truncate loan variation to 32bit
-rw-r--r--src/misc_cmd.cpp33
-rw-r--r--src/script/api/script_company.cpp4
2 files changed, 22 insertions, 15 deletions
diff --git a/src/misc_cmd.cpp b/src/misc_cmd.cpp
index 2b32c06bf..c1c81535a 100644
--- a/src/misc_cmd.cpp
+++ b/src/misc_cmd.cpp
@@ -25,14 +25,18 @@
#include "safeguards.h"
+/* Make sure we can discard lower 2 bits of 64bit amount when passing it to Cmd[In|De]creaseLoan() */
+static_assert((LOAN_INTERVAL & 3) == 0);
+
/**
* Increase the loan of your company.
* @param tile unused
* @param flags operation to perform
- * @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
+ * @param p1 higher half of amount to increase the loan with, multitude of LOAN_INTERVAL. Only used when (p2 & 3) == 2.
+ * @param p2 (bit 2-31) - lower half of amount (lower 2 bits assumed to be 0)
+ * (bit 0-1) - when 0: loans LOAN_INTERVAL
+ * when 1: loans the maximum loan permitting money (press CTRL),
+ * when 2: loans the amount specified in p1 and p2
* @param text unused
* @return the cost of this operation or an error
*/
@@ -46,7 +50,7 @@ CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
}
Money loan;
- switch (p2) {
+ switch (p2 & 3) {
default: return CMD_ERROR; // Invalid method
case 0: // Take some extra loan
loan = LOAN_INTERVAL;
@@ -55,8 +59,8 @@ CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
loan = _economy.max_loan - c->current_loan;
break;
case 2: // Take the given amount of loan
- if ((int32)p1 < LOAN_INTERVAL || c->current_loan + (int32)p1 > _economy.max_loan || p1 % LOAN_INTERVAL != 0) return CMD_ERROR;
- loan = p1;
+ loan = ((uint64)p1 << 32) | (p2 & 0xFFFFFFFC);
+ if (loan < LOAN_INTERVAL || c->current_loan + loan > _economy.max_loan || loan % LOAN_INTERVAL != 0) return CMD_ERROR;
break;
}
@@ -76,10 +80,11 @@ CommandCost CmdIncreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
* Decrease the loan of your company.
* @param tile unused
* @param flags operation to perform
- * @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
+ * @param p1 higher half of amount to decrease the loan with, multitude of LOAN_INTERVAL. Only used when (p2 & 3) == 2.
+ * @param p2 (bit 2-31) - lower half of amount (lower 2 bits assumed to be 0)
+ * (bit 0-1) - 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 and p2
* @param text unused
* @return the cost of this operation or an error
*/
@@ -90,7 +95,7 @@ CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
if (c->current_loan == 0) return_cmd_error(STR_ERROR_LOAN_ALREADY_REPAYED);
Money loan;
- switch (p2) {
+ switch (p2 & 3) {
default: return CMD_ERROR; // Invalid method
case 0: // Pay back one step
loan = min(c->current_loan, (Money)LOAN_INTERVAL);
@@ -100,8 +105,8 @@ CommandCost CmdDecreaseLoan(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
loan -= loan % LOAN_INTERVAL;
break;
case 2: // Repay the given amount of loan
- if (p1 % LOAN_INTERVAL != 0 || (int32)p1 < LOAN_INTERVAL || p1 > c->current_loan) return CMD_ERROR; // Invalid amount to loan
- loan = p1;
+ loan = ((uint64)p1 << 32) | (p2 & 0xFFFFFFFC);
+ if (loan % LOAN_INTERVAL != 0 || loan < LOAN_INTERVAL || loan > c->current_loan) return CMD_ERROR; // Invalid amount to loan
break;
}
diff --git a/src/script/api/script_company.cpp b/src/script/api/script_company.cpp
index 5769c97fa..93bba6332 100644
--- a/src/script/api/script_company.cpp
+++ b/src/script/api/script_company.cpp
@@ -204,8 +204,10 @@
if (loan == GetLoanAmount()) return true;
+ Money amount = abs(loan - GetLoanAmount());
+
return ScriptObject::DoCommand(0,
- abs(loan - GetLoanAmount()), 2,
+ amount >> 32, (amount & 0xFFFFFFFC) | 2,
(loan > GetLoanAmount()) ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN);
}