(by Codex) Persist a per-manager monthly bank robbery limit

This commit is contained in:
Codex committed 2026-11-01 18:17:14 +01:00
1 parent 30843fc9f3
commit 5caf23c452
7 files changed
+173 -11

No files matched your search

+4
View File
@@ -1182,3 +1182,7 @@ add_test(NAME wide_number_unit_tests COMMAND wide_number_unit_tests)
add_executable(long_career_tests tests/long_career_tests.cpp)
target_link_libraries(long_career_tests PRIVATE bmhcore)
add_test(NAME long_career_tests COMMAND long_career_tests)
add_executable(bank_robbery_month_tests tests/bank_robbery_month_tests.cpp)
target_link_libraries(bank_robbery_month_tests PRIVATE bmhcore)
add_test(NAME bank_robbery_month_tests COMMAND bank_robbery_month_tests)
+13
View File
@@ -132,3 +132,16 @@ see [save format](formats/LONG_CAREER.md). Original programs and older OpenBMH
writers do not preserve this archive. Keep an OpenBMH backup when exchanging
careers with those writers. The native calendar still has its existing signed
16-bit season limit; this patch does not claim an unlimited calendar.
## Monthly high-cash loss limit
OpenBMH deviates from the original: the high-cash bank robbery can occur at most
once per manager and calendar month. The original threshold above 100 million
and random loss of 10–20 million remain. A blocked repeat consumes no random
draw. Month boundaries follow the same source calendar used by daily processing,
and the guard is stored in the optional BRM1 section of OBMHEXT1 saves.
An older save has no record of past robberies. Its first qualifying high-cash month
receives a grace period without a loss or random draw; subsequent saves retain
that guard. The next calendar month can trigger the ordinary loss. Import cannot
infer whether an event already occurred before this feature. Fresh histories
receive the same initial grace month.
+35
View File
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
#include "game/state.hpp"
#include <limits>
#include <optional>
namespace bmh {
// OpenBMH-owned OBMHEXT1 section BRM1: four little-endian blocked month-start keys.
// Unknown histories use UINT32_MAX; original save fields are not repurposed.
inline constexpr uint32_t kBankRobberyMonthExtension = 0x314d5242;
inline constexpr uint32_t kUnknownBankRobberyMonth = std::numeric_limits<uint32_t>::max();
inline constexpr uint32_t kBankRobberyMonthBytes = 16;
inline std::optional<uint32_t> bank_robbery_guard_month(const GameState& state, int manager) {
if (manager < 0 || manager > 3) throw FormatError("bank robbery: invalid manager");
if (!state.memory.has(kBankRobberyMonthExtension)) return {};
if (!state.memory.has(kBankRobberyMonthExtension + kBankRobberyMonthBytes - 1))
throw FormatError("bank robbery: truncated monthly guard");
const auto key = state.memory.u32(kBankRobberyMonthExtension + uint32_t(manager) * 4);
return key == kUnknownBankRobberyMonth ? std::nullopt : std::optional<uint32_t>{key};
}
inline void set_bank_robbery_guard_month(GameState& state, int manager, std::optional<uint32_t> key) {
if (manager < 0 || manager > 3) throw FormatError("bank robbery: invalid manager");
if (!state.memory.has(kBankRobberyMonthExtension)) {
state.memory.add_block(kBankRobberyMonthExtension, kBankRobberyMonthBytes);
for (uint32_t slot = 0; slot < 4; ++slot)
state.memory.set32(kBankRobberyMonthExtension + slot * 4, kUnknownBankRobberyMonth);
}
if (!state.memory.has(kBankRobberyMonthExtension + kBankRobberyMonthBytes - 1))
throw FormatError("bank robbery: truncated monthly guard");
state.memory.set32(kBankRobberyMonthExtension + uint32_t(manager) * 4, key.value_or(kUnknownBankRobberyMonth));
}
// The first absolute day of the source calendar month, distinct across years.
uint32_t bank_robbery_month_key(uint32_t date);
}
+9
View File
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "formats/executable_layout.hpp"
#include "game/save.hpp"
#include "game/bank_robbery.hpp"
#include "game/calendar.hpp"
#include <cstdio>
@@ -128,6 +129,14 @@ Bytes save_stream(const GameState& live) {
}
auto history = encode_long_career(g);
if (!history.empty()) extension.push_back({kLongCareerExtension, std::move(history)});
if (g.memory.has(kBankRobberyMonthExtension)) {
if (!g.memory.has(kBankRobberyMonthExtension + kBankRobberyMonthBytes - 1))
throw FormatError("savegame: truncated bank robbery monthly guard");
Bytes months(kBankRobberyMonthBytes);
for (uint32_t i = 0; i < months.size(); ++i)
months[i] = g.memory.u8(kBankRobberyMonthExtension + i);
extension.push_back({kBankRobberyMonthExtension, std::move(months)});
}
append_save_extension(stream, extension);
return stream;
}
+10 -5
View File
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "formats/executable_layout.hpp"
#include "game/state.hpp"
#include "game/bank_robbery.hpp"
#include <stdexcept>
@@ -99,11 +100,15 @@ GameState GameState::from_save(const SaveState& save, const DbCounts& c) {
g.foreign_strength = bytes(::bmh::layout::ForeignClubStrength);
g.memory = StaticMemory(save);
// memory OpenBMH saves behind the trailer (the English cup blocks, career.hpp)
bool long_career_seen = false;
for (const auto& b : save.extension()) {
if (b.address == kLongCareerExtension) {
if (long_career_seen) throw FormatError("long career: duplicate extension block");
decode_long_career(g, b.bytes); long_career_seen = true; continue;
bool long_career_seen=false,robbery_months_seen=false;
for(const auto& b:save.extension()){
if(b.address==kLongCareerExtension){
if(long_career_seen)throw FormatError("long career: duplicate extension block");
decode_long_career(g,b.bytes);long_career_seen=true;continue;
}
if(b.address==kBankRobberyMonthExtension){
if(robbery_months_seen||b.bytes.size()!=kBankRobberyMonthBytes)throw FormatError("savegame: invalid or duplicate bank robbery monthly guard");
robbery_months_seen=true;
}
if (!g.memory.has(b.address)) g.memory.add_block(b.address, uint32_t(b.bytes.size()));
for (size_t i = 0; i < b.bytes.size(); ++i) g.memory.set8(b.address + uint32_t(i), b.bytes[i]);
+21 -6
View File
@@ -2,6 +2,7 @@
#include "formats/executable_layout.hpp"
#include "game/week.hpp"
#include "game/calendar.hpp"
#include "game/bank_robbery.hpp"
#include "game/matchday.hpp"
#include "game/lineup.hpp"
@@ -59,12 +60,20 @@ void daily_interest(GameState& g, WatcomRandom& rand) {
const auto statistic = uint32_t(rd32(g.manager_records, rec + 0x870)) - uint32_t(interest.cash_and_deposit);
wr32(g.manager_records, rec + 0x870, std::bit_cast<int32_t>(statistic));
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)
const auto expense = uint32_t(rd32(g.manager_records, rec + 0x8a0)) + uint32_t(extra);
wr32(g.manager_records, rec + 0x8a0, std::bit_cast<int32_t>(expense));
book_money(g, k, extra, true);
if (rd32(g.manager_records, rec + 0x314) > 100000000) {
const auto robbery_month = bank_robbery_month_key(g.date);
const auto guard = bank_robbery_guard_month(g, k);
// OpenBMH deviates from the original: an unknown history receives
// a grace month; later losses occur at most once per source month.
if (!guard) {
set_bank_robbery_guard_month(g, k, robbery_month);
} else if (*guard != robbery_month) {
const int32_t extra = int32_t(rand.range(10000, 20000)) * 1000;
const auto expense = uint32_t(rd32(g.manager_records, rec + 0x8a0)) + uint32_t(extra);
wr32(g.manager_records, rec + 0x8a0, std::bit_cast<int32_t>(expense));
book_money(g, k, extra, true);
set_bank_robbery_guard_month(g, k, robbery_month);
}
}
}
}
@@ -382,6 +391,12 @@ void stock_market(GameState& g, GameRandom& game) {
namespace bmh {
uint32_t bank_robbery_month_key(uint32_t date) {
const int month = month_code(date);
while (date > 0 && month_code(date - 1) == month) --date;
return date;
}
int month_code(uint32_t date) {
// DeskState_DeskDate_RuntimeField: days per month (index 1-12; February 29 in leap years)
uint8_t days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
+81
View File
@@ -0,0 +1,81 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "game/bank_robbery.hpp"
#include "formats/executable_layout.hpp"
#include "game/save.hpp"
#include "game/week.hpp"
#include <bit>
#include <chrono>
#include <iostream>
#include <stdexcept>
using namespace bmh;
namespace {
void check(bool ok,const char* why){if(!ok)throw std::runtime_error(why);}
void cash(GameState& state,int manager,int32_t value) {
const size_t at=size_t(manager)*0xb0c+0x314;
for(size_t n=0;n<4;++n)state.manager_records.at(at+n)=uint8_t(uint32_t(value)>>(8*n));
}
uint32_t expense(const GameState& state,int manager) {
const size_t at=size_t(manager)*0xb0c+0x8a0;
return uint32_t(state.manager_records.at(at))|uint32_t(state.manager_records.at(at+1))<<8|
uint32_t(state.manager_records.at(at+2))<<16|uint32_t(state.manager_records.at(at+3))<<24;
}
GameState fixture() {
GameState state;state.level=1;state.manager_records.resize(5*0xb0c);state.manager_teams={0,1};
state.memory.add_block(layout::ManagerCount,1);state.memory.set8(layout::ManagerCount,2);
state.club_ids={0,0};state.club_names={'C',0};return state;
}
template<class F>void rejects(F function,const char* why){bool rejected=false;try{function();}catch(const FormatError&){rejected=true;}check(rejected,why);}
}
int main()try {
uint32_t expected_key=0;
for(uint32_t day=0;day<1800;++day) {
if(day&&month_code(day)!=month_code(day-1))expected_key=day;
check(bank_robbery_month_key(day)==expected_key,"monthly key disagrees with the source calendar or collides across years");
}
auto state=fixture();cash(state,0,90000000);WatcomRandom random(713);
const auto initial_calls=random.calls();daily_interest(state,random);
check(!bank_robbery_guard_month(state,0)&&random.calls()==initial_calls,
"ordinary low cash initializes a guard or consumes randomness");
const auto old_native=save_stream(state);
check(SaveState(old_native).extension().empty(),"ordinary native saves gain an unnecessary extension");
state=GameState::from_save(SaveState(old_native));cash(state,0,250000000);
daily_interest(state,random);
check(bank_robbery_guard_month(state,0)==0&&expense(state,0)==0&&random.calls()==initial_calls,
"an unknown imported history was robbed instead of receiving its grace month");
for(int retry=0;retry<5;++retry)daily_interest(state,random);
check(expense(state,0)==0&&random.calls()==initial_calls,"grace-month retries charge money or consume random draws");
state.date=31;daily_interest(state,random);
const auto loss=expense(state,0);
check(loss>=10000000&&loss<=20000000&&loss%1000==0&&random.calls()==initial_calls+1&&bank_robbery_guard_month(state,0)==31,
"the next month does not preserve the original threshold and loss distribution");
for(int retry=0;retry<5;++retry)daily_interest(state,random);
check(expense(state,0)==loss&&random.calls()==initial_calls+1,"a monthly repeat charges again or consumes RNG");
WatcomRandom save_random(25);const auto save=make_savegame(state,save_random);
const auto path=std::filesystem::temp_directory_path()/("openbmh-robbery-"+std::to_string(std::chrono::steady_clock::now().time_since_epoch().count())+".FBM");
write_savegame(path,save);const auto loaded_file=load_savegame(path);std::filesystem::remove(path);
check(loaded_file.checksum_ok,"monthly guard invalidated encrypted save checksum");
auto loaded=GameState::from_save(SaveState(loaded_file.stream));
WatcomRandom resumed_random(100);daily_interest(loaded,resumed_random);
check(bank_robbery_guard_month(loaded,0)==31&&expense(loaded,0)==loss&&resumed_random.calls()==0,
"save/load resets the monthly limit or draws a new loss");
cash(loaded,1,250000000);daily_interest(loaded,resumed_random);
check(bank_robbery_guard_month(loaded,1)==31&&expense(loaded,1)==0&&resumed_random.calls()==0,
"one manager's limit is reused by another manager or its grace draws RNG");
loaded.date=62;daily_interest(loaded,resumed_random);
check(bank_robbery_guard_month(loaded,0)==62&&bank_robbery_guard_month(loaded,1)==62&&resumed_random.calls()==2,
"the next calendar month does not permit exactly one independent draw per manager");
loaded.date=183;daily_interest(loaded,resumed_random);const auto december_calls=resumed_random.calls();
loaded.date=184;daily_interest(loaded,resumed_random);
check(resumed_random.calls()==december_calls+2,"December to January does not reset each manager's month");
loaded.date=365;daily_interest(loaded,resumed_random);const auto yearly_calls=resumed_random.calls();
check(bank_robbery_guard_month(loaded,0)==365,"a later July collides with an earlier year's July");
daily_interest(loaded,resumed_random);check(resumed_random.calls()==yearly_calls,"same-month retry after year rollover consumes RNG");
auto malformed=old_native;append_save_extension(malformed,{{kBankRobberyMonthExtension,Bytes(15,0)}});
rejects([&]{(void)GameState::from_save(SaveState(malformed));},"truncated monthly guard is accepted");
malformed=old_native;append_save_extension(malformed,{{kBankRobberyMonthExtension,Bytes(16,0)},{kBankRobberyMonthExtension,Bytes(16,0)}});
rejects([&]{(void)GameState::from_save(SaveState(malformed));},"duplicate monthly guards are accepted");
std::cout<<"Monthly robbery limit, source month/year boundaries, per-manager grace, no repeat RNG and encrypted save/load passed\n";
return 0;
}catch(const std::exception& error){std::cerr<<error.what()<<'\n';return 1;}