Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AIs can fail at requesting 170 years inflation loan with a 4% interest rate #8453

Closed
SamuXarick opened this issue Dec 27, 2020 · 2 comments
Closed
Labels
bug Something isn't working component: AI/Game script (squirrel) This issue is related to Squirrel (Scripting language)

Comments

@SamuXarick
Copy link
Contributor

maxloaninflation
The AI is setting loan amount to £1,641,693,900,000 and has a current loan of £82,050,000.

return ScriptObject::DoCommand(0,
abs(loan - GetLoanAmount()), 2,
(loan > GetLoanAmount()) ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN);

Following the code, abs(1,641,693,900,000 - 82,050,000) = 1,641,611,850,000 is passed to this function:
/**
* Executes a raw DoCommand for the script.
*/
static bool DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text = nullptr, Script_SuspendCallbackProc *callback = nullptr);

uint32 p1 transforms the value to 934,342,928
This ends up failing to set the loan even to the bugged value:

OpenTTD/src/misc_cmd.cpp

Lines 57 to 58 in edbb5f4

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;

p1 % LOAN_INTERVAL != 0 is true, returns CMD_ERROR

@glx22
Copy link
Contributor

glx22 commented Dec 27, 2020

I think it should be possible to split 64bit value in two 32bit values passed in p1 and p2, with lower 2 bits discarded in the call, then forced to 0 in CmdIncreaseLoan() when reconstruction the original 64bit value. Amount being a multiple of LOAN_INTERVAL it should be fine.

@glx22 glx22 added bug Something isn't working component: AI/Game script (squirrel) This issue is related to Squirrel (Scripting language) labels Dec 27, 2020
@James103
Copy link
Contributor

Possible workaround: In the AI script, set the loan value to incrementally higher amounts. For example, do the following:

AICompany.SetLoanAmount(2000000000);
AICompany.SetLoanAmount(4000000000);
AICompany.SetLoanAmount(6000000000);
AICompany.SetLoanAmount(8000000000);
...

until the target loan amount has been reached.

glx22 added a commit to glx22/OpenTTD that referenced this issue Dec 27, 2020
glx22 added a commit to glx22/OpenTTD that referenced this issue Dec 27, 2020
glx22 added a commit to glx22/OpenTTD that referenced this issue Dec 27, 2020
glx22 added a commit to glx22/OpenTTD that referenced this issue Dec 28, 2020
glx22 added a commit to glx22/OpenTTD that referenced this issue Dec 28, 2020
@glx22 glx22 closed this as completed in f7e48ca Dec 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working component: AI/Game script (squirrel) This issue is related to Squirrel (Scripting language)
Projects
None yet
Development

No branches or pull requests

3 participants