From 9e92d07b03f84a8d2f3bf659e5488f6d93fcb3cd Mon Sep 17 00:00:00 2001 From: Weyoune Date: Fri, 13 Nov 2026 23:47:09 +0100 Subject: [PATCH] Port overflow fixes for daily interest and dividends; pay Modern dividends per manager Daily interest and share dividends are computed in 64 bits before the division (ported from OpenBMH 1.0.3). In Modern each manager now receives the dividend of his own shares instead of the original's lost booking. Co-Authored-By: Claude Fable 5.2 --- docs/DIFFERENCES.md | 2 ++ src/game/balance_plan.cpp | 16 +++------------- src/game/month.cpp | 4 ++-- src/game/week.cpp | 32 ++++++++++++++++++++------------ src/game/week.hpp | 9 +++++++++ src/game/weekly.cpp | 2 +- src/modern/game/balance_plan.cpp | 16 +++------------- src/modern/game/month.cpp | 20 ++++++++++++-------- src/modern/game/week.cpp | 18 ++++++------------ src/modern/game/weekly.cpp | 2 +- 10 files changed, 59 insertions(+), 62 deletions(-) diff --git a/docs/DIFFERENCES.md b/docs/DIFFERENCES.md index 76f1977..925e8dd 100644 --- a/docs/DIFFERENCES.md +++ b/docs/DIFFERENCES.md @@ -43,9 +43,11 @@ describe deliberate changes; they are not a list of every known compatibility li - [src/modern/game/environment.hpp](../src/modern/game/environment.hpp): uses six monthly environment budgets across data editions while retaining the shared staff prices and academy player-generation strength bands. - [src/modern/game/highscore.cpp](../src/modern/game/highscore.cpp): reads the supported edition-specific high-score format from the loaded image. - [src/modern/game/matchday_setup.cpp](../src/modern/game/matchday_setup.cpp): in Modern worlds a referee may take a second match on a day with more matches than referees. +- [src/modern/game/month.cpp](../src/modern/game/month.cpp): in Modern every manager receives the monthly dividend of his own shares; the price product is kept in 64 bits until the division. - [src/modern/game/season.cpp](../src/modern/game/season.cpp): retains the enhanced academy player-generation strength bands with monthly youth budgets. - [src/modern/game/season.cpp](../src/modern/game/season.cpp): in Modern the age stops at 47, the most its five bits hold; real ages of a data set (a goalkeeper of 40) would otherwise run into the bits above. - [src/modern/game/season.hpp](../src/modern/game/season.hpp): retains the enhanced academy player-generation strength bands with monthly youth budgets. +- [src/modern/game/week.cpp](../src/modern/game/week.cpp): daily interest is computed in 64 bits before the annual-rate division, so debts beyond about 119 million no longer overflow. - [src/modern/game/week.cpp](../src/modern/game/week.cpp): uses the enhanced training rules: focus strength is at least five below its target, and completed skills rotate to another focus. - [src/modern/game/week.hpp](../src/modern/game/week.hpp): uses the enhanced assistant-training rules, retaining the selected skill until it reaches its maximum. - [src/modern/game/weekly.cpp](../src/modern/game/weekly.cpp): uses the supported weekly-event configuration, with its rule switch disabled. diff --git a/src/game/balance_plan.cpp b/src/game/balance_plan.cpp index e65d81d..a9c897d 100644 --- a/src/game/balance_plan.cpp +++ b/src/game/balance_plan.cpp @@ -56,19 +56,9 @@ uint32_t facility_level(const GameState& g, int m, int k) { // (balance 1 %, fixed deposit 2 % a year), positive an expense (overdraft 12 %, beyond the // credit line 18 %). The divisor is -36500. int32_t daily_interest_preview(const GameState& g, int m) { - constexpr int32_t kAnnual = -0x8e94; const int32_t balance = rec32(g, m, 0x314), deposit = rec32(g, m, 0x318), credit = rec32(g, m, 0x31c); - int32_t result = 0; - if (balance < 0) { - const int32_t with_credit = wrap(int64_t(balance) + credit); - if (with_credit < 0) result = wrap(int64_t(with_credit) * 0x12) / kAnnual; - int32_t below = wrap(-int64_t(credit)); - if (below < balance) below = balance; - result = wrap(int64_t(result) + wrap(int64_t(below) * 0xc) / kAnnual); - } else { - result = balance / kAnnual; - } - return wrap(int64_t(result) + wrap(int64_t(deposit / 10000) * 20000) / kAnnual); + const DailyInterestAmounts interest = daily_interest_amounts(balance, deposit, credit); + return interest.borrowing + interest.cash_and_deposit; } // SharesAndLoans(manager, flags, months): flags 1 the dividends of the manager's funds and @@ -78,7 +68,7 @@ int32_t shares_and_loans(const GameState& g, int m, int flags, int months) { int32_t sum = 0; if (flags & 1) { for (int k = 0; k < 6; k++) { - const uint32_t unit = g.memory.u32(::bmh::layout::FundBlock + uint32_t(k) * 0x1c) * uint32_t(kShareUnits[k]) / 10000; + const uint32_t unit = uint32_t((uint64_t(g.memory.u32(::bmh::layout::FundBlock + uint32_t(k) * 0x1c)) * uint32_t(kShareUnits[k])) / 10000); sum = wrap(int64_t(sum) + int64_t(unit * g.memory.u16(::bmh::layout::Advertising_OriginalShareAdjustment_RuntimeField + uint32_t(m) * 2 + uint32_t(k) * 0x1c))); } if (flags & 4) sum = wrap(int64_t(sum) * months); diff --git a/src/game/month.cpp b/src/game/month.cpp index ee02477..7fc99b6 100644 --- a/src/game/month.cpp +++ b/src/game/month.cpp @@ -669,8 +669,8 @@ void shares_and_loans(GameState& g) { // for the manager after the last one) int32_t dividend = 0; for (int m = 0; m < managers; m++) - dividend = int32_t(mem.u16(::bmh::layout::Advertising_OriginalShareAdjustment_RuntimeField + uint32_t(m) * 2 + uint32_t(k) * 0x1c) * - ((price * uint32_t(kShareFactor[k])) / 10000)); + dividend = int32_t(uint32_t(mem.u16(::bmh::layout::Advertising_OriginalShareAdjustment_RuntimeField + uint32_t(m) * 2 + uint32_t(k) * 0x1c) * + ((uint64_t(price) * uint32_t(kShareFactor[k])) / 10000))); book_money(g, managers, -dividend, true); add32(r, size_t(managers) * kRecord + 0x870, dividend); } diff --git a/src/game/week.cpp b/src/game/week.cpp index a76836b..c146d4c 100644 --- a/src/game/week.cpp +++ b/src/game/week.cpp @@ -23,6 +23,20 @@ void wr32(std::vector& v, size_t o, int32_t x) { } // namespace +DailyInterestAmounts daily_interest_amounts(int32_t cash, int32_t deposit, int32_t credit) { + DailyInterestAmounts result; + if (cash < 0) { + const int64_t with_credit = int64_t(cash) + credit; + int64_t borrowing = with_credit < 0 ? with_credit * 18 / -36500 : 0; + borrowing += std::max(int64_t(cash), -int64_t(credit)) * 12 / -36500; + result.borrowing = int32_t(borrowing); + } else { + result.cash_and_deposit = cash / -36500; + } + result.cash_and_deposit += int32_t(int64_t(deposit / 10000) * 20000 / -36500); + return result; +} + void daily_interest(GameState& g, WatcomRandom& rand) { // (DailyInterest before the day step: the training camp visits of today are over) for (size_t m = 0; m < g.manager_teams.size(); m++) g.manager_records.at(m * kManagerRecord + 0x21f) &= 0x7f; @@ -31,20 +45,14 @@ void daily_interest(GameState& g, WatcomRandom& rand) { const int32_t balance = rd32(g.manager_records, rec + 0x314); const int32_t deposit = rd32(g.manager_records, rec + 0x318); const int32_t credit = rd32(g.manager_records, rec + 0x31c); - int32_t amount = 0; // positive = cost + // widened to 64 bits before the division (see daily_interest_amounts) + const DailyInterestAmounts interest = daily_interest_amounts(balance, deposit, credit); if (balance < 0) { - if (balance + credit < 0) amount = ((balance + credit) * 18) / -36500; - int32_t used = -credit < balance ? balance : -credit; - amount += (used * 12) / -36500; - wr32(g.manager_records, rec + 0x89c, rd32(g.manager_records, rec + 0x89c) + amount); - book_money(g, k, amount, true); - amount = 0; - } else { - amount = balance / -36500; + wr32(g.manager_records, rec + 0x89c, int32_t(uint32_t(rd32(g.manager_records, rec + 0x89c)) + uint32_t(interest.borrowing))); + book_money(g, k, interest.borrowing, true); } - amount += ((deposit / 10000) * 20000) / -36500; - wr32(g.manager_records, rec + 0x870, rd32(g.manager_records, rec + 0x870) - amount); - book_money(g, k, amount, true); + wr32(g.manager_records, rec + 0x870, int32_t(uint32_t(rd32(g.manager_records, rec + 0x870)) - uint32_t(interest.cash_and_deposit))); + book_money(g, k, interest.cash_and_deposit, true); if (rd32(g.manager_records, rec + 0x314) > 100000000) { // read again after the bookings int32_t extra = int32_t(rand.range(10000, 20000)) * 1000; // BookMoney2 with the statistics flag set: counts as an expense (+0x8a0) diff --git a/src/game/week.hpp b/src/game/week.hpp index beb901d..c86cd6b 100644 --- a/src/game/week.hpp +++ b/src/game/week.hpp @@ -28,6 +28,15 @@ namespace bmh { // cleared for every manager. void daily_interest(GameState& g, WatcomRandom& rand); +// The day's interest of one account set. Products are widened to 64 bits before the +// annual-rate division, so large balances cannot overflow; the truncation at each +// division is kept. Positive = cost, negative = income. +struct DailyInterestAmounts { + int32_t borrowing = 0; + int32_t cash_and_deposit = 0; +}; +DailyInterestAmounts daily_interest_amounts(int32_t cash, int32_t deposit, int32_t credit); + } // namespace bmh namespace bmh { diff --git a/src/game/weekly.cpp b/src/game/weekly.cpp index e4671fa..5de53d6 100644 --- a/src/game/weekly.cpp +++ b/src/game/weekly.cpp @@ -215,7 +215,7 @@ void manager_offers(GameState& g, int manager, WatcomRandom& rand, MinuteReport& if (kind == 14) { for (int k = 0; k < 6; k++) { const uint32_t held = g.memory.u16(::bmh::layout::Advertising_OriginalShareAdjustment_RuntimeField + uint32_t(manager) * 2 + uint32_t(k) * 0x1c); - const uint32_t value = uint32_t(g.memory.u32(::bmh::layout::FundBlock + uint32_t(k) * 0x1c) * uint32_t(kShareFactor[k])) / 10000; + const uint32_t value = uint32_t((uint64_t(g.memory.u32(::bmh::layout::FundBlock + uint32_t(k) * 0x1c)) * uint32_t(kShareFactor[k])) / 10000); sum += int(uint32_t(rand.range(10, 20)) * (held * value)); } if (sum == 0) continue; diff --git a/src/modern/game/balance_plan.cpp b/src/modern/game/balance_plan.cpp index 21f7cab..a70f8e8 100644 --- a/src/modern/game/balance_plan.cpp +++ b/src/modern/game/balance_plan.cpp @@ -58,19 +58,9 @@ uint32_t facility_level(const ModernState& g, int m, int k) { // (balance 1 %, fixed deposit 2 % a year), positive an expense (overdraft 12 %, beyond the // credit line 18 %). The divisor is -36500. int32_t daily_interest_preview(const ModernState& g, int m) { - constexpr int32_t kAnnual = -0x8e94; const int32_t balance = rec32(g, m, 0x314), deposit = rec32(g, m, 0x318), credit = rec32(g, m, 0x31c); - int32_t result = 0; - if (balance < 0) { - const int32_t with_credit = wrap(int64_t(balance) + credit); - if (with_credit < 0) result = wrap(int64_t(with_credit) * 0x12) / kAnnual; - int32_t below = wrap(-int64_t(credit)); - if (below < balance) below = balance; - result = wrap(int64_t(result) + wrap(int64_t(below) * 0xc) / kAnnual); - } else { - result = balance / kAnnual; - } - return wrap(int64_t(result) + wrap(int64_t(deposit / 10000) * 20000) / kAnnual); + const DailyInterestAmounts interest = daily_interest_amounts(balance, deposit, credit); + return interest.borrowing + interest.cash_and_deposit; } // SharesAndLoans(manager, flags, months): flags 1 the dividends of the manager's funds and @@ -80,7 +70,7 @@ int32_t shares_and_loans(const ModernState& g, int m, int flags, int months) { int32_t sum = 0; if (flags & 1) { for (int k = 0; k < 6; k++) { - const uint32_t unit = g.memory.u32(::bmh::layout::FundBlock + uint32_t(k) * 0x1c) * uint32_t(kShareUnits[k]) / 10000; + const uint32_t unit = uint32_t((uint64_t(g.memory.u32(::bmh::layout::FundBlock + uint32_t(k) * 0x1c)) * uint32_t(kShareUnits[k])) / 10000); sum = wrap(int64_t(sum) + int64_t(unit * g.memory.u16(::bmh::layout::Advertising_OriginalShareAdjustment_RuntimeField + uint32_t(m) * 2 + uint32_t(k) * 0x1c))); } if (flags & 4) sum = wrap(int64_t(sum) * months); diff --git a/src/modern/game/month.cpp b/src/modern/game/month.cpp index 37a76c4..3e64ccf 100644 --- a/src/modern/game/month.cpp +++ b/src/modern/game/month.cpp @@ -662,14 +662,18 @@ void shares_and_loans(ModernState& g) { uint32_t price = mem.u32(at); price += ((price / 10000) * 0x19 / 10) * 10; mem.set32(at, price); - // (a bug of the original: only the last manager's dividend is kept and it is booked - // for the manager after the last one) - int32_t dividend = 0; - for (int m = 0; m < managers; m++) - dividend = int32_t(mem.u16(::bmh::layout::Advertising_OriginalShareAdjustment_RuntimeField + uint32_t(m) * 2 + uint32_t(k) * 0x1c) * - ((price * uint32_t(kShareFactor[k])) / 10000)); - modern::book_money(g, managers, -dividend, true); - add32(r, size_t(managers) * kRecord + 0x870, dividend); + // Modern deviates from Classic: every manager receives the dividend of his own shares. + // (Classic keeps the original's slip: only the last manager's dividend is computed and + // it is booked for the slot after the last manager, so nobody ever receives it.) + // The price product is kept in 64 bits until the division. + const uint64_t unit_dividend = (uint64_t(price) * uint32_t(kShareFactor[k])) / 10000; + for (int m = 0; m < managers; m++) { + const uint32_t held = mem.u16(::bmh::layout::Advertising_OriginalShareAdjustment_RuntimeField + uint32_t(m) * 2 + uint32_t(k) * 0x1c); + if (held == 0) continue; + const int32_t dividend = int32_t(std::min(held * unit_dividend, 0x7fffffffu)); + modern::book_money(g, m, -dividend, true); + add32(r, size_t(m) * kRecord + 0x870, dividend); + } } for (int m = 0; m < managers; m++) for (int l = 0; l <= managers; l++) { diff --git a/src/modern/game/week.cpp b/src/modern/game/week.cpp index d331f59..435bd25 100644 --- a/src/modern/game/week.cpp +++ b/src/modern/game/week.cpp @@ -32,20 +32,14 @@ void daily_interest(ModernState& g, WatcomRandom& rand) { const int32_t balance = rd32(g.manager_records, rec + 0x314); const int32_t deposit = rd32(g.manager_records, rec + 0x318); const int32_t credit = rd32(g.manager_records, rec + 0x31c); - int32_t amount = 0; // positive = cost + // widened to 64 bits before the division (see daily_interest_amounts) + const DailyInterestAmounts interest = daily_interest_amounts(balance, deposit, credit); if (balance < 0) { - if (balance + credit < 0) amount = ((balance + credit) * 18) / -36500; - int32_t used = -credit < balance ? balance : -credit; - amount += (used * 12) / -36500; - wr32(g.manager_records, rec + 0x89c, rd32(g.manager_records, rec + 0x89c) + amount); - modern::book_money(g, k, amount, true); - amount = 0; - } else { - amount = balance / -36500; + wr32(g.manager_records, rec + 0x89c, int32_t(uint32_t(rd32(g.manager_records, rec + 0x89c)) + uint32_t(interest.borrowing))); + modern::book_money(g, k, interest.borrowing, true); } - amount += ((deposit / 10000) * 20000) / -36500; - wr32(g.manager_records, rec + 0x870, rd32(g.manager_records, rec + 0x870) - amount); - modern::book_money(g, k, amount, true); + wr32(g.manager_records, rec + 0x870, int32_t(uint32_t(rd32(g.manager_records, rec + 0x870)) - uint32_t(interest.cash_and_deposit))); + modern::book_money(g, k, interest.cash_and_deposit, true); if (rd32(g.manager_records, rec + 0x314) > 100000000) { // read again after the bookings int32_t extra = int32_t(rand.range(10000, 20000)) * 1000; // BookMoney2 with the statistics flag set: counts as an expense (+0x8a0) diff --git a/src/modern/game/weekly.cpp b/src/modern/game/weekly.cpp index 875e81e..61a98a4 100644 --- a/src/modern/game/weekly.cpp +++ b/src/modern/game/weekly.cpp @@ -214,7 +214,7 @@ void manager_offers(ModernState& g, int manager, WatcomRandom& rand, MinuteRepor if (kind == 14) { for (int k = 0; k < 6; k++) { const uint32_t held = g.memory.u16(::bmh::layout::Advertising_OriginalShareAdjustment_RuntimeField + uint32_t(manager) * 2 + uint32_t(k) * 0x1c); - const uint32_t value = uint32_t(g.memory.u32(::bmh::layout::FundBlock + uint32_t(k) * 0x1c) * uint32_t(kShareFactor[k])) / 10000; + const uint32_t value = uint32_t((uint64_t(g.memory.u32(::bmh::layout::FundBlock + uint32_t(k) * 0x1c)) * uint32_t(kShareFactor[k])) / 10000); sum += int(uint32_t(rand.range(10, 20)) * (held * value)); } if (sum == 0) continue;