Save and load Modern careers (M6)

Modern saves (save_profile modern-career-v1) name their world by id and
data revision and hold the career only; loading reinstalls the world
dimensions. Club names now follow promotion and relegation. The season
sim gains MODERN_SAVE_CHECK for a save/load round trip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ClaudeandClaude Opus 5.5 committed 2026-10-23 23:22:13 +02:00
1 parent 6f55622307
commit 28b59d9890
11 files changed
+265 -14

No files matched your search

+33 -8
View File
@@ -4,6 +4,7 @@
#include "modern/app/modern_session.hpp"
#include "modern/content/world_expand.hpp"
#include "modern/content/world_package.hpp"
#include "modern/content/world_save.hpp"
#include "modern/game/balance_plan.hpp"
#include "modern/game/bank_credit.hpp"
#include "modern/game/career.hpp"
@@ -584,6 +585,8 @@ ModernSession::ModernSession(const std::filesystem::path& data, uint32_t watcom_
db_(load_database(data / "M3D.MAN")),
texts_(load_textd(data / "TEXTD.BLK")),
rand_(watcom_seed) {
set_world_layout(classic_world_layout()); // a previous game in this process may have left a world
kLeagueTablesPerLeague = false;
install_league_sizes(exe_);
turn_driver_.alternate_match_event_roll = exe_.u8(::bmh::layout::AlternateEventRoll) != 0;
turn_driver_.disable_match_events = exe_.u8(::bmh::layout::MatchEventsDisabled) != 0;
@@ -627,6 +630,8 @@ ModernSession::ModernSession(const std::filesystem::path &data, uint32_t watcom_
const std::filesystem::path &save_path, bool importing_legacy)
: data_directory_(data), exe_(load_le(data / "BMH.exe")), db_(load_database(data / "M3D.MAN")),
texts_(load_textd(data / "TEXTD.BLK")), rand_(watcom_seed) {
set_world_layout(classic_world_layout()); // a previous game in this process may have left a world
kLeagueTablesPerLeague = false;
install_league_sizes(exe_);
turn_driver_.alternate_match_event_roll = exe_.u8(::bmh::layout::AlternateEventRoll) != 0;
turn_driver_.disable_match_events = exe_.u8(::bmh::layout::MatchEventsDisabled) != 0;
@@ -639,10 +644,23 @@ ModernSession::ModernSession(const std::filesystem::path &data, uint32_t watcom_
if (const auto resource = content::find(data); resource && !importing_legacy) {
if (content::key(save_path.extension()) != ".obs")
throw FormatError("0.2 loads native OBS saves; use Add to migrate legacy games");
native = content::load_career(save_path, *resource->content);
g_ = std::move(native->state);
rand_ = native->random;
game_ = native->game;
if (const World* world = current_world()) {
// A Modern career: the save names its world; the dimensions come from the world.
auto saved = load_world_career(save_path, *world);
install_world_dimensions(*world);
g_ = std::move(saved.state);
rand_ = saved.random;
game_ = saved.game;
native.emplace();
native->driver = std::move(saved.driver);
native->session = std::move(saved.session);
native->header = std::move(saved.header);
} else {
native = content::load_career(save_path, *resource->content);
g_ = std::move(native->state);
rand_ = native->random;
game_ = native->game;
}
} else {
const Savegame file = load_savegame(save_path);
original_english_save = file.version == "V2.20";
@@ -7791,8 +7809,12 @@ void ModernSession::flush_native_autosave() {
const auto resource = content::find(data_directory_);
if (!resource)
throw FormatError("Native content unavailable");
content::save_career(*autosave_directory_ / "AUTOSAVE.obs", *resource->content, g_, rand_, game_,
turn_driver_, true, native_session_context());
if (const World* world = current_world())
save_world_career(*autosave_directory_ / "AUTOSAVE.obs", *world, g_, rand_, game_, turn_driver_, true,
native_session_context());
else
content::save_career(*autosave_directory_ / "AUTOSAVE.obs", *resource->content, g_, rand_, game_,
turn_driver_, true, native_session_context());
} catch (const std::exception &e) {
autosave_error_ = e.what();
}
@@ -7825,8 +7847,11 @@ bool ModernSession::save_game(const std::filesystem::path& path, int manager_slo
if (ec || (exists && !overwrite)) { error = exists ? "overwrite_confirmation_required" : "save_target_unavailable"; return false; }
try {
if (const auto resource = content::find(data_directory_)) {
content::save_career(path, *resource->content, g_, rand_, game_, turn_driver_, overwrite,
native_session_context());
if (const World* world = current_world())
save_world_career(path, *world, g_, rand_, game_, turn_driver_, overwrite, native_session_context());
else
content::save_career(path, *resource->content, g_, rand_, game_, turn_driver_, overwrite,
native_session_context());
return true;
}
WatcomRandom staged_rand = rand_;
+17 -4
View File
@@ -168,6 +168,22 @@ std::string legacy_encoding(const std::string& utf8) {
return out;
}
void install_world_dimensions(const World& world) {
int league_clubs = 0, german_clubs = 0;
uint8_t sizes[kLeagueCount] = {};
for (const WorldLeague& l : world.leagues) {
sizes[l.index] = uint8_t(l.members.size());
league_clubs += int(l.members.size());
if (l.index < 5) german_clubs += int(l.members.size());
}
WorldLayout layout = modern_layout(league_clubs, german_clubs);
// The league sizes are checked against the layout and need the per-league fixture tables.
kLeagueTablesPerLeague = true;
set_world_layout(layout);
set_league_sizes(sizes);
kNewcomerFirst = world_layout().first_amateur();
}
ExpandReport expand_to_world(ModernState& g, const World& world) {
const WorldLayout old = world_layout();
if (old.league_teams != 138 || old.total_teams() != 419) fail("the world expands a Classic start state only");
@@ -355,10 +371,7 @@ ExpandReport expand_to_world(ModernState& g, const World& world) {
g.squad_slots = std::move(slots);
stage("squads");
set_world_layout(layout);
kLeagueTablesPerLeague = true;
set_league_sizes(sizes);
kNewcomerFirst = layout.first_amateur();
install_world_dimensions(world);
stage("layout and league sizes");
// --- league dates of the international group (calendar bit 4) -------------------------
+5
View File
@@ -28,6 +28,11 @@ struct ExpandReport {
// league sizes and calendar. Throws FormatError when the world does not fit.
ExpandReport expand_to_world(ModernState& g, const World& world);
// Installs the dimensions of the world for a running Modern game: layout, league sizes, the
// per-league fixture tables and the first newcomer club. A new career gets them from
// expand_to_world, a loaded one from the session.
void install_world_dimensions(const World& world);
// Installs the world's training camp catalogue, or the built-in one when the world has none
// (docs/new_engine/trainingslager.md). Classic data keeps the eight original resorts.
void install_training_camps(ModernState& g, const World& world);
+124
View File
@@ -0,0 +1,124 @@
// SPDX-License-Identifier: GPL-3.0-only
#include "modern/content/world_save.hpp"
#include <algorithm>
#include <bit>
#include <utility>
#include "content/digest.hpp"
#include "content/package.hpp"
#include "content/save.hpp"
#include "formats/native_state.hpp"
#include "import/detail.hpp"
#include "modern/game/save.hpp"
namespace bmh::modern {
namespace {
using namespace obw::detail;
J generator(uint32_t seed, uint64_t calls) {
return O{{"seed", int64_t(seed)}, {"calls", std::to_string(calls)}};
}
uint64_t draw_count(const J& value) {
const auto s = str(get(value, "calls"));
if (s.empty() || s.size() > 20 || !std::all_of(s.begin(), s.end(), [](unsigned char c) { return c >= '0' && c <= '9'; }))
fail("invalid random draw counter");
return std::stoull(s);
}
uint32_t seed_of(const J& value) {
const auto n = num(get(value, "seed"));
if (n < 0 || n > int64_t(UINT32_MAX)) fail("invalid random seed");
return uint32_t(n);
}
J strings(const std::vector<std::string>& values) {
A out;
for (const auto& v : values) out.push_back(v);
return out;
}
std::vector<std::string> strings(const J& value) {
std::vector<std::string> out;
for (const J& v : arr(value)) out.push_back(str(v));
return out;
}
std::string world_identity(const World& world) { return world.id + "@" + world.data_revision; }
} // namespace
void save_world_career(const std::filesystem::path& path, const World& world, const ModernState& state,
const WatcomRandom& random, const GameRandom& game, const TurnDriver& driver, bool overwrite,
const cli::Json& session) {
if (driver.stage != TurnDriver::Stage::Empty && driver.stage != TurnDriver::Stage::MatchdayDesks &&
driver.stage != TurnDriver::Stage::SeasonEndDesk)
fail("save unavailable outside a manager desk");
obw::Documents docs;
put(docs, "manifest.json",
O{{"format", "openbmh-world"}, {"format_version", "1.1"}, {"kind", "save"},
{"save_profile", "modern-career-v1"}, {"requires", A{J("runtime:modern-v1")}},
{"world", O{{"id", world.id}, {"data_revision", world.data_revision}, {"title", world.title}}}});
put(docs, "state/career.json", encode_native_state(state));
put(docs, "state/modern.json",
O{{"schema", "modern-state@1.0"},
{"club_ids", strings(state.world_club_id)},
{"club_names", strings(state.world_club_name)},
{"club_short_names", strings(state.world_club_short)},
{"player_ids", strings(state.world_player_id)},
{"player_first", strings(state.player_first)},
{"player_last", strings(state.player_last)},
{"player_nations", strings(state.player_nation)}});
put(docs, "state/rng.json",
O{{"schema", "classic-rng@1.0"},
{"watcom", generator(random.seed(), random.calls())},
{"game", generator(uint32_t(game.seed()), game.calls())}});
put(docs, "state/session.json", session);
put(docs, "state/driver.json", content::driver_checkpoint(driver));
put(docs, "state/information.json", encode_native_bytes(modern::save_header(state)));
O checks;
for (const auto& [name, value] : docs)
if (name.starts_with("state/")) checks[name] = content::sha256(value);
put(docs, "state/integrity.json", O{{"schema", "save-integrity@1.0"}, {"entries", std::move(checks)}});
content::write_archive(docs, path, overwrite);
}
SavedWorldCareer load_world_career(const std::filesystem::path& path, const World& world) {
const auto docs = content::read_archive(path);
const auto manifest = parse(docs, "manifest.json");
if (str(get(manifest, "kind")) != "save" || str(get(manifest, "save_profile")) != "modern-career-v1")
fail("not a Modern career save");
const auto& saved_world = get(manifest, "world");
if (str(get(saved_world, "id")) != world.id || str(get(saved_world, "data_revision")) != world.data_revision)
fail("the save belongs to another world: " + str(get(saved_world, "id")) + "@" +
str(get(saved_world, "data_revision")) + ", loaded " + world_identity(world));
const auto integrity = parse(docs, "state/integrity.json");
if (str(get(integrity, "schema")) != "save-integrity@1.0") fail("unsupported save integrity");
O checks;
for (const auto& [name, value] : docs)
if (name.starts_with("state/") && name != "state/integrity.json") checks[name] = content::sha256(value);
if (cli::dump_json(get(integrity, "entries")) != cli::dump_json(checks)) fail("damaged Modern save");
SavedWorldCareer out;
out.state = ModernState(decode_native_state(parse(docs, "state/career.json")));
const auto modern = parse(docs, "state/modern.json");
if (str(get(modern, "schema")) != "modern-state@1.0") fail("unsupported Modern state");
out.state.world_club_id = strings(get(modern, "club_ids"));
out.state.world_club_name = strings(get(modern, "club_names"));
out.state.world_club_short = strings(get(modern, "club_short_names"));
out.state.world_player_id = strings(get(modern, "player_ids"));
out.state.player_first = strings(get(modern, "player_first"));
out.state.player_last = strings(get(modern, "player_last"));
out.state.player_nation = strings(get(modern, "player_nations"));
if (out.state.world_player_id.size() != out.state.players.size() ||
out.state.world_club_id.size() != out.state.club_ids.size())
fail("Modern names do not match the saved state");
const auto generators = parse(docs, "state/rng.json");
if (str(get(generators, "schema")) != "classic-rng@1.0") fail("unsupported RNG checkpoint");
out.random.restore(seed_of(get(generators, "watcom")), draw_count(get(generators, "watcom")));
out.game.restore(std::bit_cast<int32_t>(seed_of(get(generators, "game"))), draw_count(get(generators, "game")));
out.session = parse(docs, "state/session.json");
out.driver = parse(docs, "state/driver.json");
out.header = decode_native_bytes(parse(docs, "state/information.json"), 172);
return out;
}
} // namespace bmh::modern
+35
View File
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: GPL-3.0-only
#pragma once
// Saves of Modern careers (docs/new_engine, M6; data format F8: save_profile modern-career-v1).
//
// A Classic save embeds its sealed world. A Modern world is large (artwork included), so a
// Modern save names its world (id, data revision) and holds the career only: the game state,
// the Modern additions (names and identities by runtime index), both random generators, the
// turn driver and the session context. Loading needs the same world.
#include <filesystem>
#include "cli/json.hpp"
#include "game/random.hpp"
#include "game/turn_driver.hpp"
#include "modern/content/world.hpp"
#include "modern/game/modern_state.hpp"
namespace bmh::modern {
struct SavedWorldCareer {
ModernState state;
WatcomRandom random{0};
GameRandom game{0};
cli::Json driver, session;
Bytes header;
};
void save_world_career(const std::filesystem::path& path, const World& world, const ModernState& state,
const WatcomRandom& random, const GameRandom& game, const TurnDriver& driver, bool overwrite,
const cli::Json& session = {});
// Throws FormatError when the save is damaged, not a Modern save or belongs to another world.
SavedWorldCareer load_world_career(const std::filesystem::path& path, const World& world);
} // namespace bmh::modern
+3
View File
@@ -788,6 +788,9 @@ void change_places(ModernState& g, int a, int b) {
}
if (!g.club_table.empty() && a < first_foreign_cup() && b < first_foreign_cup()) swap_bytes(g.club_table, size_t(a) * 0x4a, size_t(b) * 0x4a, 0x4a);
std::swap(g.club_ids.at(size_t(a)), g.club_ids.at(size_t(b)));
// Modern world names and identities move with the club (empty with Classic data).
for (auto* names : {&g.world_club_id, &g.world_club_name, &g.world_club_short})
if (size_t(std::max(a, b)) < names->size()) std::swap((*names)[size_t(a)], (*names)[size_t(b)]);
swap_bytes(g.club_heap_bc, size_t(a) * 2, size_t(b) * 2, 2);
std::swap(g.team_morale.at(size_t(a)), g.team_morale.at(size_t(b)));
swap_mem8(::bmh::layout::ClubShortNameOffsets);