diff --git a/CMakeLists.txt b/CMakeLists.txt index 4ff64f9..ec862e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1139,6 +1139,10 @@ if(MSVC) endif() add_test(NAME quit_confirmation_unit_tests COMMAND quit_confirmation_unit_tests) +add_executable(modern_calendar_tests tests/modern_calendar_tests.cpp) +target_link_libraries(modern_calendar_tests PRIVATE bmh_season_modern) +add_test(NAME modern_calendar_tests COMMAND modern_calendar_tests) + add_executable(world_layout_tests tests/world_layout_tests.cpp) target_link_libraries(world_layout_tests PRIVATE bmhcore) add_test(NAME world_layout_tests COMMAND world_layout_tests) diff --git a/docs/new_engine/README.md b/docs/new_engine/README.md index b98a26f..b48496b 100644 --- a/docs/new_engine/README.md +++ b/docs/new_engine/README.md @@ -53,7 +53,7 @@ Für 0.3 gilt außerdem: |---|---|---| | M0 | Classic-Referenz festhalten, Datensatz lokal bereitstellen | erledigt (Classic-Regressionen des öffentlichen Codes: Issue #14) | | M1 | `WorldLayout`, Abzweig `src/modern/` | Abzweig und Layout erledigt; vereinsbezogene Speicherblöcke folgen mit M3 | -| M2 | Spielpläne je Liga (Round-Robin), Kalender mit 38 Spieltagen | offen | +| M2 | Spielpläne je Liga, Kalender mit 38 Spieltagen | Engine-Teil erledigt (Tabelle je Ligagröße, Ligatermine, Aussetzen); Test mit echten Daten in M3 | | M3 | Modern-Weltlader, Namen (Vor-/Nachname, UTF-8), Asset-Schlüssel | offen | | M4 | Trainingslager: 18 Lager, drei Regionen, 6-Sterne-Wirkung | offen | | M5 | Datenrevision 1.4 des Modern-Datensatzes (privat), Wirtschaftsprüfung | offen | diff --git a/docs/new_engine/datenformat.md b/docs/new_engine/datenformat.md index 550dd07..531e3b4 100644 --- a/docs/new_engine/datenformat.md +++ b/docs/new_engine/datenformat.md @@ -13,8 +13,8 @@ Status: *geplant* (noch nicht in der Engine), *umgesetzt* (Engine liest/prüft e | Nr. | Bereich | Änderung | Phase | Status | |---|---|---|---|---| | F1 | Manifest | Laufzeit `runtime:modern-v1`, spielbare Modern-Welt | M3 | geplant | -| F2 | Ligen | Größe je Liga frei (12–20), Generator je Liga | M2 | geplant | -| F3 | Kalender | Ligatermine als Datumsliste | M2 | geplant | +| F2 | Ligen | Größe je Liga frei (12–20), Generator je Liga | M2 | Engine: Größe je Liga umgesetzt; Generator vorerst Tabelle passender Größe | +| F3 | Kalender | Ligatermine als Datumsliste | M2 | Engine-Funktion umgesetzt; Anbindung an den Weltlader mit M3 | | F4 | Trainingslager | Katalog mit Karte und X/Y-Lage, Ort, Bild, Qualität 0–6 | M4 | geplant | | F5 | Namen | Vor- und Nachname Pflicht, UTF-8 | M3 | geplant | | F6 | Kader | bis 24 Plätze je Verein, Mindestgröße | M3 | geplant | @@ -32,6 +32,7 @@ Status: *geplant* (noch nicht in der Engine), *umgesetzt* (Engine liest/prüft e - `competitions[].stages[].teams` darf je Liga zwischen 12 und 20 liegen; die Mitgliederliste (`members`), `teams` und die Teams der Slots müssen übereinstimmen. - `generator`: `round-robin` für jede Größe; `legacy:` nur, wenn `rule:legacy-fixtures` eine Tabelle für genau `n` Teams hat. +- Stand 0.3: Die Engine spielt jede Liga mit der Spielplantabelle ihrer Größe (`rule:legacy-fixtures`, 12–20 Teams). `round-robin` wird gelesen, aber noch wie die gleich große Tabelle behandelt; nur die Reihenfolge der Paarungen unterscheidet sich. Ein eigener Round-Robin-Generator folgt bei Bedarf. - Spieltage einer Liga: `2 × (teams − 1)` bei `legs: 2`. - Die Classic-Zusatzfelder `x-legacy.league_index`, `calendar_bit`, `relegation_places`, `europe_places` bleiben und werden in 0.3 weiter gelesen. - **Editor:** Größe zwischen 12 und 20 zulassen; Generator `legacy:` nur anbieten, wenn die Tabelle existiert; Warnung, wenn Mitglieder, `teams` und Slots nicht übereinstimmen. diff --git a/src/game/schedule.cpp b/src/game/schedule.cpp index afc15c5..92e8e20 100644 --- a/src/game/schedule.cpp +++ b/src/game/schedule.cpp @@ -124,7 +124,10 @@ std::vector generate_fixtures(int teams, int matchday, int rounds, int return out; } -int league_table_teams(int league) { return league < 5 ? kLeagueSizes[0] : kLeagueSizes[5]; } +int league_table_teams(int league) { + if (kLeagueTablesPerLeague) return kLeagueSizes[league]; + return league < 5 ? kLeagueSizes[0] : kLeagueSizes[5]; +} void set_league_sizes(const uint8_t (&sizes)[kLeagueCount]) { int total = 0; @@ -132,9 +135,10 @@ void set_league_sizes(const uint8_t (&sizes)[kLeagueCount]) { const auto has_table = [](int teams) { return teams == 20 || teams == 18 || teams == 16 || teams == 12; }; if (total != world_layout().league_teams || !has_table(sizes[0]) || !has_table(sizes[5])) throw std::invalid_argument("league sizes do not describe 138 league teams with fixture tables"); - for (int l = 0; l < kLeagueCount; l++) - if (sizes[l] < 2 || sizes[l] > (l < 5 ? sizes[0] : sizes[5])) + for (int l = 0; l < kLeagueCount; l++) { + if (kLeagueTablesPerLeague ? !has_table(sizes[l]) : sizes[l] < 2 || sizes[l] > (l < 5 ? sizes[0] : sizes[5])) throw std::invalid_argument("a league is larger than its fixture table"); + } for (int l = 0; l < kLeagueCount; l++) kLeagueSizes[l] = sizes[l]; } diff --git a/src/game/schedule.hpp b/src/game/schedule.hpp index 49bb103..f377bdc 100644 --- a/src/game/schedule.hpp +++ b/src/game/schedule.hpp @@ -98,6 +98,9 @@ int team_league(int team); // 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); +// Modern worlds play every league on the table of its own size (international leagues of +// 20 and 18 clubs side by side). Classic leaves this off: leagues 5-8 share the table of league 5. +inline bool kLeagueTablesPerLeague = false; struct Fixture { int home; // team id diff --git a/src/modern/game/matchday.cpp b/src/modern/game/matchday.cpp index 3759833..24aa82c 100644 --- a/src/modern/game/matchday.cpp +++ b/src/modern/game/matchday.cpp @@ -719,6 +719,8 @@ void advance_league_rounds(ModernState& g, uint32_t competitions, WatcomRandom& const int end = bit < 4 ? bit + 1 : 8; for (int c = bit; c < end; c++) { for (int league = kFirst[c]; league < kFirst[c] + kCount[c]; league++) { + // Modern: a league that has played all its rounds sits out (see setup_matchday). + if (g.league_rounds.at(size_t(league)) >= league_matchdays(league)) continue; if (league == 0) { for (size_t m = 0; m < g.manager_teams.size(); m++) { modern::advance_youth_teams(g, int(m), rand); diff --git a/src/modern/game/matchday_setup.cpp b/src/modern/game/matchday_setup.cpp index 837936a..474658e 100644 --- a/src/modern/game/matchday_setup.cpp +++ b/src/modern/game/matchday_setup.cpp @@ -562,6 +562,9 @@ std::vector setup_matchday(ModernState& g, uint32_t competitions, co for (int k = from; k < to; k++) { // FixturesFromGenerateFixtures(k) for (int league = kGroupLeague[k]; league < kGroupLeague[k] + kGroupLeagues[k]; league++) { + // Modern: leagues of different sizes share a calendar group; a league whose rounds + // are all played sits out the remaining dates of the group (docs/new_engine). + if (g.league_rounds.at(size_t(league)) >= league_matchdays(league)) continue; turn_step = "setup: winter postponements"; winter_postponements(g, ctx.season_day, league, game, rand); const int round = g.league_rounds.at(size_t(league)); diff --git a/src/modern/game/world_calendar.cpp b/src/modern/game/world_calendar.cpp new file mode 100644 index 0000000..af6cb7a --- /dev/null +++ b/src/modern/game/world_calendar.cpp @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: GPL-3.0-only +#include "modern/game/world_calendar.hpp" + +#include +#include +#include + +#include "formats/executable_layout.hpp" + +namespace bmh::modern { + +namespace { + +constexpr uint32_t kCalendar = ::bmh::layout::CompetitionCalendar; +constexpr int kCalendarDays = 366; +constexpr uint16_t kSpecialDay = 0x4000; + +// Days since 1970-01-01 of a proleptic Gregorian date (H. Hinnant's days_from_civil). +long days_from_civil(int y, int m, int d) { + y -= m <= 2; + const long era = (y >= 0 ? y : y - 399) / 400; + const unsigned yoe = unsigned(y - era * 400); + const unsigned doy = unsigned((153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1); + const unsigned doe = yoe * 365 + yoe / 4 - yoe / 100 + doy; + return era * 146097 + long(doe) - 719468; +} + +} // namespace + +int season_day_of(const std::string& date, int start_year) { + int y = 0, m = 0, d = 0; + if (date.size() != 10 || std::sscanf(date.c_str(), "%4d-%2d-%2d", &y, &m, &d) != 3 || m < 1 || m > 12 || d < 1 || + d > 31) + return -1; + return int(days_from_civil(y, m, d) - days_from_civil(start_year, 7, 1)); +} + +LeagueDatesResult install_league_dates(ModernState& g, int calendar_bit, const std::vector& season_days) { + if (calendar_bit < 0 || calendar_bit > 4) throw std::invalid_argument("league dates: calendar bit outside 0-4"); + if (!g.memory.has(kCalendar) || !g.memory.has(kCalendar + kCalendarDays * 2 - 1)) + throw std::invalid_argument("league dates: the season calendar is not loaded"); + const uint16_t bit = uint16_t(1u << calendar_bit); + const auto entry = [&](int day) { return g.memory.u16(kCalendar + uint32_t(day) * 2); }; + const auto set = [&](int day, uint16_t v) { g.memory.set16(kCalendar + uint32_t(day) * 2, v); }; + for (int day = 0; day < kCalendarDays; ++day) + if (!(entry(day) & kSpecialDay)) set(day, uint16_t(entry(day) & ~bit)); + std::vector days = season_days; + std::sort(days.begin(), days.end()); + LeagueDatesResult result; + for (const int wanted : days) { + if (wanted < 0 || wanted >= kCalendarDays) throw std::invalid_argument("league dates: date outside the season"); + int day = wanted; + while (day < kCalendarDays && (entry(day) & (kSpecialDay | bit))) ++day; + if (day == kCalendarDays) throw std::invalid_argument("league dates: no free day left in the season"); + if (day != wanted) + result.moved.push_back("season day " + std::to_string(wanted) + " -> " + std::to_string(day)); + set(day, uint16_t(entry(day) | bit)); + ++result.dates; + } + return result; +} + +} // namespace bmh::modern diff --git a/src/modern/game/world_calendar.hpp b/src/modern/game/world_calendar.hpp new file mode 100644 index 0000000..5aae753 --- /dev/null +++ b/src/modern/game/world_calendar.hpp @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-3.0-only +#pragma once +// League dates of a Modern world in the season calendar (docs/new_engine, M2). +// +// The season calendar (CompetitionCalendar) has one 16-bit entry per season day, day 0 being +// 1 July. Bits 0-4 start the league matchdays of the calendar groups; entries with bit 14 set +// are special days (indoor tournament, season change, ...) whose low bits mean something +// else. A Modern world gives the league dates of a group as a list of dates; this module +// replaces the group's bit on the ordinary days by those dates. + +#include +#include + +#include "modern/game/modern_state.hpp" + +namespace bmh::modern { + +// Season day (0 = 1 July of `start_year`) of an ISO date "YYYY-MM-DD"; -1 for a malformed date. +int season_day_of(const std::string& date, int start_year); + +struct LeagueDatesResult { + int dates = 0; // dates placed + std::vector moved; // dates moved to the next free ordinary day +}; + +// Sets `calendar_bit` (0-4) exactly on the given season days: the bit is cleared on every +// ordinary day first; a date on a special day (bit 14) or on a day that already has the bit +// moves to the next ordinary day. Throws std::invalid_argument when a date cannot be placed +// within the 366 days or the calendar is not loaded. +LeagueDatesResult install_league_dates(ModernState& g, int calendar_bit, const std::vector& season_days); + +} // namespace bmh::modern diff --git a/tests/modern_calendar_tests.cpp b/tests/modern_calendar_tests.cpp new file mode 100644 index 0000000..1d3cdc4 --- /dev/null +++ b/tests/modern_calendar_tests.cpp @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-3.0-only +// Modern league dates: season days of ISO dates and placing a league group's dates in the +// season calendar around special days (docs/new_engine, M2). Synthetic calendar only. +#include +#include +#include + +#include "formats/executable_layout.hpp" +#include "modern/game/world_calendar.hpp" + +using namespace bmh; +using namespace bmh::modern; + +namespace { +int failures = 0; +void check(bool ok, const char* what) { + if (!ok) { + std::fprintf(stderr, "FAIL: %s\n", what); + ++failures; + } +} +constexpr uint32_t kCalendar = ::bmh::layout::CompetitionCalendar; +uint16_t entry(const ModernState& g, int day) { return g.memory.u16(kCalendar + uint32_t(day) * 2); } +} // namespace + +int main() { + check(season_day_of("2026-07-01", 2026) == 0, "1 July is day 0"); + check(season_day_of("2026-08-08", 2026) == 38, "8 August"); + check(season_day_of("2027-02-28", 2026) == 242, "28 February"); + check(season_day_of("2027-07-01", 2026) == 365, "next 1 July"); + check(season_day_of("2026-13-01", 2026) == -1 && season_day_of("26-07-01", 2026) == -1, "malformed dates"); + + ModernState g; + g.memory.add_block(kCalendar, 366 * 2); + // Ordinary days with the old group-4 dates, a German league day, special days. + for (int day = 10; day < 100; day += 10) g.memory.set16(kCalendar + uint32_t(day) * 2, 0x10); + g.memory.set16(kCalendar + 40 * 2, 0x11); // Bundesliga and group 4 + g.memory.set16(kCalendar + 50 * 2, 0x4002); // indoor tournament: special + g.memory.set16(kCalendar + 51 * 2, 0x4006); // special + g.memory.set16(kCalendar + 300 * 2, 0x4014); // special day that happens to carry bit 4 + + const auto r = install_league_dates(g, 4, {50, 5, 52, 360}); + check(r.dates == 4, "all dates placed"); + check(entry(g, 10) == 0 && entry(g, 90) == 0, "old ordinary group dates cleared"); + check(entry(g, 40) == 0x01, "other bits of a cleared day stay"); + check(entry(g, 300) == 0x4014, "special days are not touched"); + check(entry(g, 5) == 0x10 && entry(g, 360) == 0x10, "dates on ordinary days"); + check(entry(g, 50) == 0x4002 && entry(g, 51) == 0x4006, "special days keep their meaning"); + check(entry(g, 52) == 0x10 && entry(g, 53) == 0x10, "a date on a special day moves to the next free day"); + check(r.moved.size() == 2, "both moved dates are reported (50 -> 52 pushes 52 -> 53)"); + + bool threw = false; + try { + install_league_dates(g, 4, {365, 365}); + } catch (const std::invalid_argument&) { + threw = true; + } + check(threw, "a date that cannot be placed is rejected"); + threw = false; + try { + ModernState empty; + install_league_dates(empty, 4, {1}); + } catch (const std::invalid_argument&) { + threw = true; + } + check(threw, "a missing calendar is rejected"); + + if (failures) return 1; + std::puts("modern calendar checks passed"); + return 0; +}