Skip to content

Commit

Permalink
Fix OpenTTD#8453: [Script] Don't truncate loan variation to 32bit
Browse files Browse the repository at this point in the history
  • Loading branch information
glx22 committed Dec 28, 2020
1 parent 94057e9 commit 2adeb35
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
33 changes: 19 additions & 14 deletions src/misc_cmd.cpp
Expand Up @@ -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
*/
Expand All @@ -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;
Expand All @@ -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;
}

Expand All @@ -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: 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
*/
Expand All @@ -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);
Expand All @@ -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;
}

Expand Down
4 changes: 3 additions & 1 deletion src/script/api/script_company.cpp
Expand Up @@ -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);
}

Expand Down

0 comments on commit 2adeb35

Please sign in to comment.