From 7c08e396076e2e25450e57379d80717dabb61945 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 24 Oct 2026 09:01:39 +0200 Subject: [PATCH] Keep final tables and competition winners in the almanac At the start of the season transition, before promotion moves the clubs, the almanac stores the final table of every league (rank, games, wins, draws, defeats, goals, points) and each league's champion and runner-up. Every decided cup (national cup, European cups, supercup, play-off, national tournaments, each indoor tournament and the Masters) is noted with its date, last stage and runner-up. Almanac format version 2; version 1 seasons are still read. Co-Authored-By: Claude Opus 5.5 --- docs/new_engine/almanach.md | 9 +++- src/modern/game/almanac.cpp | 96 +++++++++++++++++++++++++++++++++- src/modern/game/almanac.hpp | 37 +++++++++++++ src/modern/game/cup.cpp | 6 +++ src/modern/game/season.cpp | 1 + tests/modern_almanac_tests.cpp | 7 +++ tests/modern_season_sim.cpp | 12 +++++ 7 files changed, 165 insertions(+), 3 deletions(-) diff --git a/docs/new_engine/almanach.md b/docs/new_engine/almanach.md index 156b970..c495dad 100644 --- a/docs/new_engine/almanach.md +++ b/docs/new_engine/almanach.md @@ -22,6 +22,13 @@ Pro Spiel (`AlmanacMatch`): | Aufstellungen | je Seite die 11 Startspieler, dann die Bank (nur Ligavereine; Länder- und Amateurteams haben in der Engine keine Kader) | | Ereignisse | Minute, Seite, Spieler und ggf. zweiter Spieler: Tor (mit Vorlage und Art: Spiel, Elfmeter/Freistoß, Nachschuss, Ecke), Fehlschuss, Gelb, Gelb-Rot, Rot, Verletzung, Wechsel (ein/aus), Elfmeter im Elfmeterschießen | +Pro Saison zusätzlich: + +| Feld | Inhalt | +|---|---| +| Abschlusstabellen (`tables`) | alle Ligen in Platzierungsreihenfolge: Platz, Verein, Spiele, S/U/N, Tore, Gegentore, Punkte (nach einem Punktabzug). Aufgenommen zu Beginn des Saisonwechsels, bevor Auf- und Abstieg die Vereine verschieben | +| Sieger (`winners`) | in der Reihenfolge, in der sie feststehen: Meister jeder Liga (mit Vizemeister) und jeder entschiedene Pokalwettbewerb (DFB-Pokal, Europapokale, Supercup, Relegation, Länderturniere, jedes Hallenturnier und das Masters) mit Datum, letzter Stufe und Finalgegner bzw. Gruppenzweitem. Nicht: die Behälter für Wiederholungsspiele und Freundschaftsspiele (Pokal-ID 9 und 10) | + ## Woher die Daten kommen (keine Wirkung auf das Spiel) - Tore, Fehlschüsse, Karten, Verletzungen: das Ereignisprotokoll der Spielgruppe (`MatchGroup::log`), das die Engine wie das Original für **jedes** Spiel führt. @@ -40,7 +47,7 @@ Pro Spiel (`AlmanacMatch`): - Jede Saison hat ihre eigenen Tabellen (Ligen, Vereine, Spieler, Manager, Schiedsrichter) und ist damit für sich lesbar. - Abgeschlossene Saisons liegen im Spielstand als `history/season-.bin` und werden nie wieder geschrieben; die laufende als `history/current.bin`. Beide stehen in `state/integrity.json`. -- Binärformat Version 1 (Little Endian): `OBAL`, u16 Version, Saisonkopf, Tabellen, Spiele. Text: u16 Länge + UTF-8. Einzelheiten in `encode_almanac_season`. +- Binärformat Version 2 (Little Endian): `OBAL`, u16 Version, Saisonkopf, Tabellen (Ligen, Vereine, Spieler, Manager, Schiedsrichter), Spiele, Abschlusstabellen, Sieger. Version 1 (ohne Abschlusstabellen und Sieger) wird weiter gelesen. Text: u16 Länge + UTF-8. Einzelheiten in `encode_almanac_season`. - Größe: etwa 0,5 MB pro Saison im (komprimierten) Spielstand, mit allen Ereignissen und Aufstellungen. Ein Spielstand nach einer Saison ist ~700 KB statt ~210 KB. ## Für die UI (Codex) diff --git a/src/modern/game/almanac.cpp b/src/modern/game/almanac.cpp index adfd339..dfcc28b 100644 --- a/src/modern/game/almanac.cpp +++ b/src/modern/game/almanac.cpp @@ -9,6 +9,8 @@ #include "formats/binary.hpp" #include "game/match_engine.hpp" #include "game/presentation.hpp" +#include "game/schedule.hpp" +#include "game/table.hpp" #include "modern/game/cup.hpp" #include "modern/game/matchday.hpp" #include "modern/game/modern_state.hpp" @@ -18,7 +20,8 @@ namespace bmh::modern { namespace { constexpr size_t kManagerRecord = 0xB0C; -constexpr uint32_t kVersion = 1; +// Version 2 adds the final tables and the winners; version 1 seasons are still read. +constexpr uint32_t kVersion = 2; // Original single-byte text (ISO 646-DE) to UTF-8, as the interfaces show it. std::string utf8(std::string_view original) { @@ -218,13 +221,25 @@ std::string encode_almanac_season(const AlmanacSeason& s) { for (const AlmanacEvent& e : m.events) w.u8(uint32_t(e.kind)), w.u8(e.minute), w.u8(e.side), w.u8(e.detail), w.u16(e.player), w.u16(e.other); } + w.u8(uint32_t(s.tables.size())); + for (const AlmanacTable& t : s.tables) { + w.u8(t.league), w.u8(uint32_t(t.rows.size())); + for (const AlmanacStanding& row : t.rows) { + w.u16(row.club), w.u8(row.rank), w.u8(row.played), w.u8(row.won), w.u8(row.drawn), w.u8(row.lost); + w.u8(row.goals_for), w.u8(row.goals_against), w.u16(uint32_t(uint16_t(row.points))); + } + } + w.u16(uint32_t(s.winners.size())); + for (const AlmanacWinner& x : s.winners) + w.u8(x.type), w.u8(x.league), w.u8(x.stage), w.u8(x.detail), w.u32(x.date), w.u16(x.winner), w.u16(x.runner_up); return std::move(w.out); } AlmanacSeason decode_almanac_season(const std::string& bytes) { if (bytes.size() < 6 || bytes.compare(0, 4, "OBAL") != 0) throw FormatError("almanac: not an almanac season"); Reader r{bytes, 4}; - if (r.u16() != kVersion) throw FormatError("almanac: unknown season version"); + const uint32_t version = r.u16(); + if (version < 1 || version > kVersion) throw FormatError("almanac: unknown season version"); AlmanacSeason s; s.season = int16_t(r.u16()), s.start_year = int(r.u16()), s.display_start_year = int(r.u16()); for (uint32_t n = r.u16(); n > 0; --n) { @@ -272,11 +287,88 @@ AlmanacSeason decode_almanac_season(const std::string& bytes) { if (m.home >= s.clubs.size() || m.away >= s.clubs.size()) throw FormatError("almanac: unknown club in a match"); s.matches.push_back(std::move(m)); } + if (version >= 2) { + for (uint32_t n = r.u8(); n > 0; --n) { + AlmanacTable t; + t.league = uint8_t(r.u8()); + for (uint32_t k = r.u8(); k > 0; --k) { + AlmanacStanding row; + row.club = uint16_t(r.u16()); + row.rank = uint8_t(r.u8()), row.played = uint8_t(r.u8()), row.won = uint8_t(r.u8()); + row.drawn = uint8_t(r.u8()), row.lost = uint8_t(r.u8()); + row.goals_for = uint8_t(r.u8()), row.goals_against = uint8_t(r.u8()); + row.points = int16_t(r.u16()); + if (row.club >= s.clubs.size()) throw FormatError("almanac: unknown club in a table"); + t.rows.push_back(row); + } + s.tables.push_back(std::move(t)); + } + for (uint32_t n = r.u16(); n > 0; --n) { + AlmanacWinner x; + x.type = uint8_t(r.u8()), x.league = uint8_t(r.u8()), x.stage = uint8_t(r.u8()), x.detail = uint8_t(r.u8()); + x.date = r.u32(); + x.winner = uint16_t(r.u16()), x.runner_up = uint16_t(r.u16()); + if (x.winner >= s.clubs.size() || (x.runner_up != 0xffff && x.runner_up >= s.clubs.size())) + throw FormatError("almanac: unknown club among the winners"); + s.winners.push_back(x); + } + } if (r.at != bytes.size()) throw FormatError("almanac: trailing bytes after the season"); s.index(); return s; } +void almanac_final_tables(ModernState& g) try { + AlmanacSeason& season = current_season(g); + season.tables.clear(); + std::erase_if(season.winners, [](const AlmanacWinner& x) { return x.type == 0; }); + for (int league = 0; league < kLeagueCount; ++league) { + const LeagueTable t = compute_league_table(g, league, 0, league_matchdays(league), 7, 1); + const int first = league_first_team(league); + AlmanacTable table; + table.league = uint8_t(league); + for (int i = 0; i < t.size; ++i) { + AlmanacStanding row; + row.club = season.club(club_of(g, first + i)); + row.rank = uint8_t(t.rank(i) + 1); + row.played = uint8_t(t.games(i)), row.won = t.wins[size_t(i)], row.lost = t.defeats[size_t(i)]; + row.drawn = uint8_t(std::max(0, t.games(i) - t.wins[size_t(i)] - t.defeats[size_t(i)])); + row.goals_for = t.goals_for[size_t(i)], row.goals_against = t.goals_against[size_t(i)]; + row.points = t.points[size_t(i)]; + table.rows.push_back(row); + } + std::stable_sort(table.rows.begin(), table.rows.end(), + [](const AlmanacStanding& a, const AlmanacStanding& b) { return a.rank < b.rank; }); + if (!table.rows.empty()) { + AlmanacWinner champion; + champion.league = uint8_t(league); + champion.date = g.date; + champion.winner = table.rows[0].club; + if (table.rows.size() > 1) champion.runner_up = table.rows[1].club; + season.winners.push_back(champion); + } + season.tables.push_back(std::move(table)); + } +} catch (const std::exception& e) { + std::fprintf(stderr, "almanac: final tables not recorded: %s\n", e.what()); +} + +void almanac_cup_winner(ModernState& g, int cup, int stage, int winner, int runner_up, bool masters) try { + const int teams = world_layout().total_teams(); + if (winner < 0 || winner >= teams) return; + AlmanacSeason& season = current_season(g); + AlmanacWinner x; + x.type = uint8_t(cup + 1); + x.stage = uint8_t(stage); + x.detail = masters ? 1 : 0; + x.date = g.date; + x.winner = season.club(club_of(g, winner)); + if (runner_up >= 0 && runner_up < teams) x.runner_up = season.club(club_of(g, runner_up)); + season.winners.push_back(x); +} catch (const std::exception& e) { + std::fprintf(stderr, "almanac: winner not recorded: %s\n", e.what()); +} + void new_player_identity(ModernState& g, int player) { if (player < 0 || size_t(player) >= g.players.size()) return; if (g.world_player_id.size() < g.players.size()) g.world_player_id.resize(g.players.size()); diff --git a/src/modern/game/almanac.hpp b/src/modern/game/almanac.hpp index 35da92a..988f185 100644 --- a/src/modern/game/almanac.hpp +++ b/src/modern/game/almanac.hpp @@ -98,6 +98,34 @@ struct AlmanacLeague { bool operator==(const AlmanacLeague&) const = default; }; +// A row of a final league table (in rank order). +struct AlmanacStanding { + uint16_t club = 0; // index into the season's clubs + uint8_t rank = 0; // 1 = champion (equal ranks are possible as the engine ranks them) + uint8_t played = 0, won = 0, drawn = 0, lost = 0; + uint8_t goals_for = 0, goals_against = 0; + int16_t points = 0; // after a point deduction of a human manager + bool operator==(const AlmanacStanding&) const = default; +}; +struct AlmanacTable { + uint8_t league = 0; // league index (season header: id and name) + std::vector rows; + bool operator==(const AlmanacTable&) const = default; +}; + +// The winner of a competition, noted when it is decided: league champions from the final +// tables, cups when their last stage ends (single final: the loser is the runner-up; a final +// group, e.g. the indoor tournament: its second). +struct AlmanacWinner { + uint8_t type = 0; // as AlmanacMatch: 0 league, else cup id + 1 + uint8_t league = 0xff; // league index for type 0 + uint8_t stage = 0xff; // the last cup stage (cup table index) + uint8_t detail = 0; // 1 = the indoor Masters (after the five tournaments) + uint32_t date = 0; // day counter + uint16_t winner = 0, runner_up = 0xffff; // indices into the season's clubs (0xffff none) + bool operator==(const AlmanacWinner&) const = default; +}; + struct AlmanacSeason { int season = -1; // seasons played before (GameState::season) int start_year = 0; // calendar year the season starts in @@ -108,6 +136,8 @@ struct AlmanacSeason { std::vector managers; std::vector referees; // index 0 = none std::vector matches; // in the order played + std::vector tables; // final league tables, at the end of the season + std::vector winners; // in the order decided bool operator==(const AlmanacSeason&) const = default; @@ -149,6 +179,13 @@ AlmanacSeason decode_almanac_season(const std::string& bytes); // stable identity so the almanac does not merge the two careers. void new_player_identity(ModernState& g, int player); +// --- Season results ----------------------------------------------------------------------- +// The final tables of every league and their champions; the season core calls it once the +// leagues are complete, before promotion moves the clubs. +void almanac_final_tables(ModernState& g); +// A cup is decided (its last stage ended): cup id, last stage, winner and runner-up (-1 none). +void almanac_cup_winner(ModernState& g, int cup, int stage, int winner, int runner_up, bool masters = false); + // --- Recording (the Modern turn driver) --------------------------------------------------- // One matchday as the turn driver plays it: begin after kick-off (line-ups, rounds), the // minutes for substitutions, finish with the match engine's show events before the diff --git a/src/modern/game/cup.cpp b/src/modern/game/cup.cpp index 7f88d56..c6bda6b 100644 --- a/src/modern/game/cup.cpp +++ b/src/modern/game/cup.cpp @@ -605,6 +605,11 @@ void advance_cup(ModernState& g, int id, WatcomRandom& rand, MinuteReport& repor if (!losers.empty()) mem.set16(e + 0x24, losers[0]); } if (type == 0xc) remove_competition(mem, 0xc); + // The almanac notes every decided competition (not the replay and friendly + // containers 9 and 10): the runner-up is the final's loser or the group's second. + if (type != 9 && type != 10 && !winners.empty()) + modern::almanac_cup_winner(g, type, entry, winners[0], + winners.size() > 1 ? winners[1] : losers.empty() ? -1 : losers[0]); // CupRuntimeField5: the title of the competition (0xFF: none) static constexpr uint8_t kTitle[14] = {1, 2, 3, 4, 5, 0xff, 6, 0xff, 8, 0xff, 0xff, 0xff, 0xff, 0xff}; const uint8_t title = type >= 0 && type < 14 ? kTitle[type] : 0xff; @@ -656,6 +661,7 @@ void advance_cup(ModernState& g, int id, WatcomRandom& rand, MinuteReport& repor winner_display(g, {7, winner}); if (mem.u8(::bmh::layout::Cup_AdvanceCup_RuntimeField) == 0 && mem.has(::bmh::layout::CareerSession_BeginFriendlyOpponentTable_ExecutableField2)) mem.set8(::bmh::layout::CareerSession_BeginFriendlyOpponentTable_ExecutableField2, 0); mem.set16(::bmh::layout::Titles + 7 * 100 + uint32_t(std::min(g.season, 0x31)) * 2, winner); + modern::almanac_cup_winner(g, 0xb, entry, winner, -1, true); // the Masters (almanac) if (mem.has(::bmh::layout::Cup_AdvanceCup_RuntimeField7)) mem.set16(::bmh::layout::Cup_AdvanceCup_RuntimeField7, winner); const int manager = g.manager_of(winner); if (manager >= 0) { diff --git a/src/modern/game/season.cpp b/src/modern/game/season.cpp index 384396c..ba5ed27 100644 --- a/src/modern/game/season.cpp +++ b/src/modern/game/season.cpp @@ -1603,6 +1603,7 @@ SeasonTransitionOutcome season_transition(ModernState& g, GameRandom& game, Watc } g.memory.set8(::bmh::layout::CareerSession_RuntimeError_RuntimeField4, 0); if (g.memory.has(::bmh::layout::Season_SeasonTransition_RuntimeField)) g.memory.set8(::bmh::layout::Season_SeasonTransition_RuntimeField, 0); + modern::almanac_final_tables(g); // final tables and champions of the season that ended g.season = int16_t(g.season + 1); // SeasonIndex modern::reset_club_moods(g, game, rand); european_setup(g, game, rand, cup_draw); diff --git a/tests/modern_almanac_tests.cpp b/tests/modern_almanac_tests.cpp index df34041..60089b3 100644 --- a/tests/modern_almanac_tests.cpp +++ b/tests/modern_almanac_tests.cpp @@ -41,6 +41,13 @@ AlmanacSeason sample() { m.events.push_back({AlmanacEventKind::Substitution, 80, 0, 0, b, a}); m.events.push_back({AlmanacEventKind::ShootoutKick, 121, 1, 1, a, kAlmanacNone}); s.matches.push_back(m); + AlmanacTable t; + t.league = 0; + t.rows.push_back({home, 1, 34, 25, 5, 4, 80, 30, 77}); + t.rows.push_back({away, 2, 34, 20, 6, 8, 70, 40, -2}); + s.tables.push_back(t); + s.winners.push_back({0, 0, 0xff, 0, 300, home, away}); + s.winners.push_back({12, 0xff, 4, 1, 310, away, 0xffff}); return s; } } // namespace diff --git a/tests/modern_season_sim.cpp b/tests/modern_season_sim.cpp index 1795c5d..6a5a6f9 100644 --- a/tests/modern_season_sim.cpp +++ b/tests/modern_season_sim.cpp @@ -129,6 +129,18 @@ void print_almanac(const modern::ModernState& g) { std::printf("almanac: %zu seasons, %zu matches, %zu events, %zu bytes uncompressed, %u new player identities\n", seasons.size(), matches, events, bytes, g.almanac.generated_players); std::printf("almanac: %zu matches whose goal events differ from the result, %zu without line-ups\n", mismatched, without_lineup); + for (const auto& s : seasons) { + if (s.tables.empty() && s.winners.empty()) continue; + std::printf("almanac season %d: %zu final tables, %zu winners\n", s.start_year, s.tables.size(), s.winners.size()); + for (const auto& t : s.tables) + if (!t.rows.empty()) + std::printf(" league %d: %s %d pts, last %s %d pts (%zu clubs)\n", int(t.league), s.clubs[t.rows.front().club].name.c_str(), + int(t.rows.front().points), s.clubs[t.rows.back().club].name.c_str(), int(t.rows.back().points), t.rows.size()); + for (const auto& w : s.winners) + if (w.type != 0) + std::printf(" cup %d%s (stage %d): %s, runner-up %s\n", int(w.type) - 1, w.detail ? " Masters" : "", int(w.stage), + s.clubs[w.winner].name.c_str(), w.runner_up == 0xffff ? "-" : s.clubs[w.runner_up].name.c_str()); + } std::vector> top; for (const auto& [id, row] : scorers) top.emplace_back(row.first, row.second + " (" + id + ")"); std::sort(top.begin(), top.end(), [](const auto& a, const auto& b) { return a.first > b.first; });