From ebe3c797dce90e5265a81389d8babbb1d0a999ac Mon Sep 17 00:00:00 2001 From: rubidium Date: Sun, 11 May 2014 20:25:26 +0000 Subject: [PATCH] (svn r26585) -Fix/Feature [FS#5942]: don't truncate money to 32 bits --- src/script/api/script_company.cpp | 18 +++++++++++------- src/script/api/script_company.hpp | 14 +++++++++----- src/script/squirrel_helper.hpp | 6 ++++-- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/script/api/script_company.cpp b/src/script/api/script_company.cpp index 53f58c9147..a5fecf8ead 100644 --- a/src/script/api/script_company.cpp +++ b/src/script/api/script_company.cpp @@ -196,11 +196,11 @@ return LOAN_INTERVAL; } -/* static */ bool ScriptCompany::SetLoanAmount(int32 loan) +/* static */ bool ScriptCompany::SetLoanAmount(Money loan) { EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY); EnforcePrecondition(false, loan >= 0); - EnforcePrecondition(false, (loan % GetLoanInterval()) == 0); + EnforcePrecondition(false, ((int64)loan % GetLoanInterval()) == 0); EnforcePrecondition(false, loan <= GetMaxLoanAmount()); EnforcePrecondition(false, (loan - GetLoanAmount() + GetBankBalance(COMPANY_SELF)) >= 0); @@ -211,12 +211,12 @@ (loan > GetLoanAmount()) ? CMD_INCREASE_LOAN : CMD_DECREASE_LOAN); } -/* static */ bool ScriptCompany::SetMinimumLoanAmount(int32 loan) +/* static */ bool ScriptCompany::SetMinimumLoanAmount(Money loan) { EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY); EnforcePrecondition(false, loan >= 0); - int32 over_interval = loan % GetLoanInterval(); + Money over_interval = (int64)loan % GetLoanInterval(); if (over_interval != 0) loan += GetLoanInterval() - over_interval; EnforcePrecondition(false, loan <= GetMaxLoanAmount()); @@ -226,10 +226,12 @@ return GetLoanAmount() == loan; } -/* static */ bool ScriptCompany::ChangeBankBalance(CompanyID company, int32 delta, ExpensesType expenses_type) +/* static */ bool ScriptCompany::ChangeBankBalance(CompanyID company, Money delta, ExpensesType expenses_type) { EnforcePrecondition(false, ScriptObject::GetCompany() == OWNER_DEITY); EnforcePrecondition(false, expenses_type < (ExpensesType)::EXPENSES_END); + EnforcePrecondition(false, delta >= INT32_MIN); + EnforcePrecondition(false, delta <= INT32_MAX); company = ResolveCompanyID(company); EnforcePrecondition(false, company != COMPANY_INVALID); @@ -280,12 +282,14 @@ return ::Company::Get((CompanyID)company)->settings.engine_renew_months; } -/* static */ bool ScriptCompany::SetAutoRenewMoney(uint32 money) +/* static */ bool ScriptCompany::SetAutoRenewMoney(Money money) { + EnforcePrecondition(false, money >= 0); + EnforcePrecondition(false, (int64)money <= UINT32_MAX); return ScriptObject::DoCommand(0, ::GetCompanySettingIndex("company.engine_renew_money"), money, CMD_CHANGE_COMPANY_SETTING); } -/* static */ uint32 ScriptCompany::GetAutoRenewMoney(CompanyID company) +/* static */ Money ScriptCompany::GetAutoRenewMoney(CompanyID company) { company = ResolveCompanyID(company); if (company == COMPANY_INVALID) return 0; diff --git a/src/script/api/script_company.hpp b/src/script/api/script_company.hpp index 00adc795f8..5327930813 100644 --- a/src/script/api/script_company.hpp +++ b/src/script/api/script_company.hpp @@ -145,7 +145,7 @@ public: * @game @pre Valid ScriptCompanyMode active in scope. * @return True if the loan could be set to your requested amount. */ - static bool SetLoanAmount(int32 loan); + static bool SetLoanAmount(Money loan); /** * Sets the minimum amount to loan, i.e. the given amount of loan rounded up. @@ -155,7 +155,7 @@ public: * @game @pre Valid ScriptCompanyMode active in scope. * @return True if we could allocate a minimum of 'loan' loan. */ - static bool SetMinimumLoanAmount(int32 loan); + static bool SetMinimumLoanAmount(Money loan); /** * Gets the amount your company have loaned. @@ -194,10 +194,12 @@ public: * @param expenses_type The account in the finances window that will register the cost. * @game @pre No ScriptCompanyMode active in scope. * @pre ResolveCompanyID(company) != COMPANY_INVALID. + * @pre delta >= -2**31 + * @pre delta < 2**31 * @note You need to create your own news message to inform about costs/gifts that you create using this command. * @api -ai */ - static bool ChangeBankBalance(CompanyID company, int32 delta, ExpensesType expenses_type); + static bool ChangeBankBalance(CompanyID company, Money delta, ExpensesType expenses_type); /** * Get the income of the company in the given quarter. @@ -314,9 +316,11 @@ public: * Set the minimum money needed to autorenew an engine for your company. * @param money The new minimum required money for autorenew to work. * @return True if autorenew money has been modified. + * @pre money >= 0 + * @pre money < 2**32 * @api -game */ - static bool SetAutoRenewMoney(uint32 money); + static bool SetAutoRenewMoney(Money money); /** * Return the minimum money needed to autorenew an engine for a company. @@ -324,7 +328,7 @@ public: * @pre ResolveCompanyID(company) != COMPANY_INVALID. * @return The minimum required money for autorenew to work. */ - static uint32 GetAutoRenewMoney(CompanyID company); + static Money GetAutoRenewMoney(CompanyID company); }; DECLARE_POSTFIX_INCREMENT(ScriptCompany::CompanyID) diff --git a/src/script/squirrel_helper.hpp b/src/script/squirrel_helper.hpp index ff00712c07..760d6c3bf4 100644 --- a/src/script/squirrel_helper.hpp +++ b/src/script/squirrel_helper.hpp @@ -85,8 +85,8 @@ namespace SQConvert { template <> inline int Return (HSQUIRRELVM vm, int8 res) { sq_pushinteger(vm, res); return 1; } template <> inline int Return (HSQUIRRELVM vm, int16 res) { sq_pushinteger(vm, res); return 1; } template <> inline int Return (HSQUIRRELVM vm, int32 res) { sq_pushinteger(vm, res); return 1; } - template <> inline int Return (HSQUIRRELVM vm, int64 res) { sq_pushinteger(vm, ClampToI32(res)); return 1; } - template <> inline int Return (HSQUIRRELVM vm, Money res) { sq_pushinteger(vm, ClampToI32(res)); return 1; } + template <> inline int Return (HSQUIRRELVM vm, int64 res) { sq_pushinteger(vm, res); return 1; } + template <> inline int Return (HSQUIRRELVM vm, Money res) { sq_pushinteger(vm, res); return 1; } template <> inline int Return (HSQUIRRELVM vm, bool res) { sq_pushbool (vm, res); return 1; } template <> inline int Return (HSQUIRRELVM vm, char *res) { if (res == NULL) sq_pushnull(vm); else { sq_pushstring(vm, OTTD2SQ(res), -1); free(res); } return 1; } template <> inline int Return(HSQUIRRELVM vm, const char *res) { if (res == NULL) sq_pushnull(vm); else { sq_pushstring(vm, OTTD2SQ(res), -1); } return 1; } @@ -104,6 +104,8 @@ namespace SQConvert { template <> inline int8 GetParam(ForceType , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } template <> inline int16 GetParam(ForceType , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } template <> inline int32 GetParam(ForceType , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } + template <> inline int64 GetParam(ForceType , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } + template <> inline Money GetParam(ForceType , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQInteger tmp; sq_getinteger (vm, index, &tmp); return tmp; } template <> inline bool GetParam(ForceType , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQBool tmp; sq_getbool (vm, index, &tmp); return tmp != 0; } template <> inline void *GetParam(ForceType , HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr) { SQUserPointer tmp; sq_getuserpointer(vm, index, &tmp); return tmp; } template <> inline const char *GetParam(ForceType, HSQUIRRELVM vm, int index, SQAutoFreePointers *ptr)