Files
Dreierpack-Manager/src/game/schedule.hpp
T
ClaudeandClaude 81046f89f2 Name the per-league stride of the results block; document the world layout
kLeagueResultsStride (records.hpp) replaces the literal 0x17C at 17 places, a
static_assert ties the block size to the league count. docs/architecture.md
describes WorldLayout, LeagueSystem and what is still fixed for a larger world.

Co-Authored-By: Claude <noreply@anthropic.com>
2026-11-11 08:34:41 +01:00

107 lines
5.0 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
#pragma once
// League fixtures and results.
//
// Team ids
// Every club has an index into all per-club tables (names, stadiums, squads, ...).
// Leagues are contiguous ranges of these ids: league 0 is ids 0..17, league 1 the next
// 18 ids and so on (sizes in kLeagueSizes, LeagueSizes; team -> league is
// TeamLeague2). When a new game starts the clubs of a league are shuffled by swapping
// all their table entries (swap function around 0x685xx), so a team's position inside
// its league is its "draw position" for the fixture list. The master database order is
// therefore NOT the order used by a running game; always take names from the savegame.
//
// Fixtures (GenerateFixtures)
// The fixture list is not stored. For each league size there is a fixed table of
// "round strings" loaded from the executable: round r of the first half of the season is a
// string of letters, each consecutive pair "xy" is one match, home team = draw position
// x - 'a', away team = y - 'a'. The second half repeats the rounds with home and away
// swapped, and so on for leagues that play more than twice (the round index wraps).
//
// Results (block LeagueResults, 0xD5C bytes)
// results[league * 380 + (matchday - 1) * 10 + match] = home_goals << 4 | away_goals
// Room for 9 leagues x 38 matchdays x 10 matches. 0xCC marks a match not yet played
// (the whole block is filled with 0xCC at season start, FUN at layout field in the dump);
// the code also tests (value & 0xCF) against 0xCC/0xCD/0xCE for postponed matches.
#include <cstdint>
#include <map>
#include <string>
#include <vector>
#include "game/records.hpp"
#include "game/world_layout.hpp"
namespace bmh {
class LeImage;
// Round strings are installation content. Sparse authored caches are supported
// for isolated callers; real sessions install the tables from their executable.
using FixtureTableCache = std::map<int, std::vector<std::string>>;
// Validates every supplied table before replacing the current cache. Throws
// std::invalid_argument for malformed rows, team ids, sizes or round counts.
void set_fixture_tables(FixtureTableCache tables);
void install_fixture_tables(const LeImage& exe);
// The league structure of the loaded game (sizes, calendar groups, promotion ladder, cup structure)
// is league_system() of world_layout.hpp. The German sizes are the default; app::CareerSession
// installs those of the loaded game from the EXE tables. One game per process.
//
// Installs the league sizes of the loaded game; throws std::invalid_argument unless they
// add up to the league teams of the world layout (138 in every version) and every league has a
// fixture table (20, 18, 16, 12 or 10 teams).
void set_league_sizes(const uint8_t (&sizes)[kLeagueCount]);
// Installs the calendar groups (first league and league count per competition bit) and derives
// the calendar bit of every league from them.
void set_league_groups(const uint8_t (&first)[8], const uint8_t (&count)[8]);
// First team id of a league (sum of the sizes of all previous leagues).
int league_first_team(int league);
// League of a team id, or kLeagueCount for teams outside league play (TeamLeague2).
int team_league(int team);
// The round-string table a league plays with (its team count): the original points leagues
// 0-4 at the table of the first league and 5-8 at the 12-team table (DE CareerFirstSteps2, EN
// LeagueTableTeams). The same as the league size except for the English fifth league (10 teams),
// which belongs to no calendar group and is never played.
int league_table_teams(int league);
struct Fixture {
int home; // team id
int away; // team id
};
// Matches of a league matchday (1-based), home/away as team ids. `rounds` is the number of
// rounds per half season, normally team count - 1 (the original passes matchdays / 2).
std::vector<Fixture> league_fixtures(int league, int matchday);
// Raw fixture generator, a direct port of GenerateFixtures:
// teams - league size, selects the round-string table
// matchday - 1-based
// rounds - rounds per half season (normally teams - 1)
// first_team - id of the league's first team, added to every draw position
std::vector<Fixture> generate_fixtures(int teams, int matchday, int rounds, int first_team);
constexpr uint8_t kNotPlayed = 0xCC;
// (one league's part of the block is kLeagueResultsStride bytes, records.hpp)
constexpr size_t kResultsSize = 0xD5C;
static_assert(kResultsSize == size_t(kLeagueCount) * kLeagueResultsStride, "the results block holds every league");
struct Result {
bool played = false;
int home_goals = 0;
int away_goals = 0;
};
// Decodes one result byte of block LeagueResults.
inline Result decode_result(uint8_t v) {
if ((v & 0xCF) >= 0xCC) return {};
return {true, v >> 4, v & 0x0F};
}
// Index into the results block for a league match.
inline size_t result_index(int league, int matchday, int match) {
return size_t(league) * kLeagueResultsStride + size_t(matchday - 1) * 10 + size_t(match);
}
} // namespace bmh