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 <noreply@anthropic.com>
This commit is contained in:
1 parent
f0979455d1
commit
9e92d07b03
10 files changed
+59
-62
No files matched your search
@@ -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);
|
||||
|
||||
+2
-2
@@ -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);
|
||||
}
|
||||
|
||||
+20
-12
@@ -23,6 +23,20 @@ void wr32(std::vector<uint8_t>& 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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
+1
-1
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<uint64_t>(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++) {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user