// SPDX-License-Identifier: GPL-3.0-only // Implements calendar_from_game() and reschedule_calendar_from_game() declared in calendar_state.hpp. // They project the saved season into CalendarState (dates, competition bitmaps, matchday ordinals, // reschedule options) without preparing fixtures, advancing the date or consuming random numbers. // The projection feeds CalendarScreen in calendar.cpp. #include "formats/executable_layout.hpp" #include "ui/calendar_state.hpp" #include #include #include #include "formats/le.hpp" #include "game/matchday_setup.hpp" #include "game/state.hpp" #include "game/reschedule.hpp" namespace bmh::ui { namespace { constexpr uint32_t kCalendar = ::bmh::layout::CompetitionCalendar; constexpr int kMaxSeasonDays = 366; void require_memory(const GameState& game, uint32_t address) { if (!game.memory.has(address)) throw FormatError("calendar: missing saved memory at " + std::to_string(address)); } } CalendarState calendar_from_game(const GameState& game, const LeImage& executable) { if (!executable.contains(::bmh::layout::CalendarState_CalendarFromGame_ExecutableField) || !executable.contains(::bmh::layout::CalendarState_CalendarFromGame_ExecutableField2)) throw FormatError("calendar: missing executable month lengths"); // The season origin and the day of the season are the live values of the game. Their copies in // the memory image hold only the day the game was loaded or saved; they are not kept up to date // while the career runs, so reading them failed from the first kickoff on. const uint32_t native_start = game.season_start; const int native_current = game.season_day(); if (game.date < native_start || native_current < 0) throw FormatError("calendar: the season origin lies after the current date"); const DeskDate first_native_date = desk_date(native_start, executable); if (first_native_date.month != 7 || first_native_date.day < 1 || first_native_date.day > 7) throw FormatError("calendar: native season start is outside the first July week"); const int leading_days = first_native_date.day - 1; if (native_current < 0 || native_current >= kMaxSeasonDays) throw FormatError("calendar: native season cursor is out of range"); if (native_start < uint32_t(leading_days)) throw FormatError("calendar: display calendar anchor underflow"); const uint32_t display_start = native_start - uint32_t(leading_days); const int current_display = native_current + leading_days; // CalendarCell reads 0x16e saved calendar words. Keep the whole source table // available even when this particular year uses only its first 365 entries. for (int day = 0; day < kMaxSeasonDays; ++day) { const uint64_t address = uint64_t(kCalendar) + uint64_t(day) * 2 + 1; if (address > std::numeric_limits::max()) throw FormatError("calendar: calendar address overflow"); require_memory(game, uint32_t(address)); } require_memory(game, ::bmh::layout::CalendarState_FormatError_RuntimeField2); require_memory(game, ::bmh::layout::CalendarState_FormatError_RuntimeField); require_memory(game, ::bmh::layout::CompetitionSelection); for (int league = 0; league < 9; ++league) { const uint32_t address = ::bmh::layout::LeagueCalendarBits + uint32_t(league); if (!executable.contains(address)) throw FormatError("calendar: missing executable league calendar class"); if (executable.u8(address) > 31) throw FormatError("calendar: invalid executable league calendar class"); } CalendarState state; state.season_start_counter = display_start; state.leading_days = leading_days; if (current_display >= kMaxSeasonDays) throw FormatError("calendar: current date is outside the display calendar"); std::array ordinals{}; state.days.reserve(kMaxSeasonDays); for (int offset = 0; offset <= kMaxSeasonDays; ++offset) { const uint64_t absolute = uint64_t(display_start) + uint64_t(offset); if (absolute > std::numeric_limits::max()) throw FormatError("calendar: day counter overflow"); const auto day_counter = uint32_t(absolute); const DeskDate date = desk_date(day_counter, executable); if (offset > 0 && date.month == 7 && date.day == 1) break; if (offset == kMaxSeasonDays) throw FormatError("calendar: season did not reach the next July 1"); const int native_day = offset - leading_days; const uint16_t raw = native_day >= 0 ? game.memory.u16(kCalendar + uint32_t(native_day) * 2) : 0; const uint32_t competitions = native_day >= 0 ? day_competitions(game, raw) : 0; CalendarDayProjection projection; projection.season_day = native_day; projection.day_counter = day_counter; projection.date = date; projection.raw_entry = raw; projection.competitions = competitions; projection.current_day = offset == current_display; for (int league = 0; league < 9; ++league) { const uint32_t mask = uint32_t(1) << executable.u8(::bmh::layout::LeagueCalendarBits + uint32_t(league)); if ((competitions & mask) != 0) { if (ordinals[size_t(league)] == std::numeric_limits::max()) throw FormatError("calendar: league matchday ordinal overflow"); projection.league_matchdays[size_t(league)] = ++ordinals[size_t(league)]; } } state.days.push_back(projection); } if (state.days.size() != 365 && state.days.size() != 366) throw FormatError("calendar: unsupported season length"); if (current_display >= int(state.days.size())) throw FormatError("calendar: current date is outside the season calendar"); state.current_day = current_display; // TextGradientDisabled (a settings switch) clear and the edition's gradient mode 2: the text // takes the original's six-row colour table, as on every other page. state.gradient_text = !game.memory.has(::bmh::layout::TextGradientDisabled) || (game.memory.u8(::bmh::layout::TextGradientDisabled) == 0 && executable.u8(::bmh::layout::TextGradientMode) == 2); return state; } CalendarState reschedule_calendar_from_game(const GameState& game, const LeImage& executable, int manager) { CalendarState state = calendar_from_game(game, executable); state.reschedule_manager = manager; for (const auto& match : reschedulable_matches(game, manager)) { RescheduleOption option; option.match = match; option.target_days = reschedule_target_days(game, manager, match); const int display_day = match.day + state.leading_days; if (match.day >= 0 && display_day >= 0 && size_t(display_day) < state.days.size()) state.days[size_t(display_day)].reschedule_source = true; for (const int day : option.target_days) if (day >= 0 && day + state.leading_days >= 0 && size_t(day + state.leading_days) < state.days.size()) state.days[size_t(day + state.leading_days)].reschedule_target = true; state.reschedule_options.push_back(std::move(option)); } state.moved_matches = moved_matches(game, manager); for (const auto& match : state.moved_matches) { const int display_day = match.day + state.leading_days; if (match.day >= 0 && display_day >= 0 && size_t(display_day) < state.days.size()) state.days[size_t(display_day)].reschedule_undo = true; } return state; } } // namespace bmh::ui