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 <noreply@anthropic.com>
This commit is contained in:
1 parent
d42c855843
commit
7c08e39607
7 files changed
+165
-3
No files matched your search
@@ -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());
|
||||
|
||||
@@ -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<AlmanacStanding> 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<AlmanacManager> managers;
|
||||
std::vector<std::string> referees; // index 0 = none
|
||||
std::vector<AlmanacMatch> matches; // in the order played
|
||||
std::vector<AlmanacTable> tables; // final league tables, at the end of the season
|
||||
std::vector<AlmanacWinner> 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
|
||||
|
||||
@@ -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<int>(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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user