(by Codex) Persist a per-manager monthly bank robbery limit
This commit is contained in:
1 parent
30843fc9f3
commit
5caf23c452
7 files changed
+173
-11
No files matched your search
@@ -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);
|
||||
}
|
||||
@@ -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
@@ -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
@@ -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};
|
||||
|
||||
Reference in new issue
Block a user