Modern: fixture table per league size, league dates and finished leagues (M2)
- schedule: kLeagueTablesPerLeague lets every league use the table of its own size (international leagues of 20 and 18 side by side); Classic leaves it off. - Modern: a league that has played all its rounds sits out the remaining dates of its calendar group. - modern/game/world_calendar: season days of ISO dates and placing a league group's dates in the season calendar around special days, with tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f2fcb28f65
commit
a4a37f1cb2
10 files changed
+189
-6
No files matched your search
@@ -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);
|
||||
|
||||
@@ -562,6 +562,9 @@ std::vector<MatchGroup> 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));
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#include "modern/game/world_calendar.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <stdexcept>
|
||||
|
||||
#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<int>& 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<int> 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
|
||||
@@ -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 <string>
|
||||
#include <vector>
|
||||
|
||||
#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<std::string> 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<int>& season_days);
|
||||
|
||||
} // namespace bmh::modern
|
||||
Reference in new issue
Block a user