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

+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