From d43d09a378169ccfe57a77ad448474cbf555d5d3 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 30 Oct 2026 18:02:52 +0100 Subject: [PATCH] Read the original edition calendar for gameplay, desktop and saves (by Codex) --- CMakeLists.txt | 5 ++++ docs/formats/EXECUTABLE_LAYOUT.md | 10 +++++++ docs/release/1.0/RELEASE_NOTES.md | 2 ++ docs/release/1.0/VALIDATION.md | 4 ++- src/app/career_session.cpp | 2 ++ src/formats/executable_layout.hpp | 2 ++ src/game/calendar.cpp | 23 ++++++++++++++++ src/game/calendar.hpp | 11 ++++++++ src/game/save.cpp | 5 ++-- src/game/week.cpp | 5 ++-- src/ui/desk_state.cpp | 9 +++++-- src/ui/desk_state.hpp | 2 +- src/ui/desktop_tests.cpp | 10 +++++-- tests/calendar_epoch_tests.cpp | 45 +++++++++++++++++++++++++++++++ tests/runtime_fixture_tables.hpp | 2 ++ tests/season_sim.cpp | 4 +++ 16 files changed, 130 insertions(+), 11 deletions(-) create mode 100644 src/game/calendar.cpp create mode 100644 src/game/calendar.hpp create mode 100644 tests/calendar_epoch_tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a3dd78..4ccb2e8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -245,6 +245,7 @@ add_library(bmhcore STATIC src/game/weekly.cpp src/game/messages.cpp src/game/table.cpp + src/game/calendar.cpp src/game/week.cpp src/game/lineup.cpp src/game/national.cpp @@ -1127,3 +1128,7 @@ set_tests_properties(season_player_reset_tests PROPERTIES TIMEOUT 10) add_executable(lineup_foreign_limit_tests tests/lineup_foreign_limit_tests.cpp) target_link_libraries(lineup_foreign_limit_tests PRIVATE bmhcore) add_test(NAME lineup_foreign_limit_tests COMMAND lineup_foreign_limit_tests) + +add_executable(calendar_epoch_tests tests/calendar_epoch_tests.cpp) +target_link_libraries(calendar_epoch_tests PRIVATE bmhui) +add_test(NAME calendar_epoch_tests COMMAND calendar_epoch_tests) diff --git a/docs/formats/EXECUTABLE_LAYOUT.md b/docs/formats/EXECUTABLE_LAYOUT.md index 746a74f..68dcdaf 100644 --- a/docs/formats/EXECUTABLE_LAYOUT.md +++ b/docs/formats/EXECUTABLE_LAYOUT.md @@ -1991,3 +1991,13 @@ declarations change. Game text, resort, image and audio fallbacks belong outside | `EnglishSixTeamFixtureTable` | `0x6eed4` | English image adapter | | `EnglishTwelveTeamFixtureTable` | `0x6f0bc` | English image adapter | | `EnglishTwentyTeamFixtureTable` | `0x6ee40` | English image adapter | + +## Verified edition-dependent parameters + +| Field | Address | Use | +|---|---|---| +| `CalendarDateSetup` | `0x21270` | Verified date setup: weekday byte at +4, initial year dword at +13. Original CD uses 1995/Saturday; checked Diamond editions use 2026/Wednesday. | +| `ForeignBadgeGateInstruction` | `0x10070` | Verified original drawing gate; operand at +3 selects neutral foreign crests or individual W-sheet selectors. | +| `EnvironmentBudgetSteps` | `0xa50ff` | Six budget increments in the verified monthly environment implementation. | + +Calendar presentation, engine weekday processing and save-list headers share the supplied installation's epoch. The original four-year date arithmetic is preserved; this reader does not substitute a different calendar system or rewrite the installation. Unknown date setup instructions and invalid values are rejected. diff --git a/docs/release/1.0/RELEASE_NOTES.md b/docs/release/1.0/RELEASE_NOTES.md index 2e5ea60..f0c7281 100644 --- a/docs/release/1.0/RELEASE_NOTES.md +++ b/docs/release/1.0/RELEASE_NOTES.md @@ -15,3 +15,5 @@ The automatic lineup reads the original foreign-player limit switch: enabled edi Known limits: Football Limited remains experimental, the floppy executable is unsupported, and intentional implementation differences remain listed in docs/DIFFERENCES.md. The supplied original-defect catalogue is an ongoing compatibility reference, not a claim that all its entries have received independent runtime acceptance. The season reset processes each player only once, including temporary offer references to an active club player. This prevents double ageing and regeneration of a still-owned player into a second club. The correction is covered by a synthetic cached-offer regression and long runs through eleven to twelve season-end desks. + +The start year and weekday are read from the supplied edition for the desktop, engine and save-list headers. Original CD therefore starts in 1995 on Saturday; checked Diamond editions retain 2026 on Wednesday. The original date arithmetic remains intact. diff --git a/docs/release/1.0/VALIDATION.md b/docs/release/1.0/VALIDATION.md index 882d61f..d690648 100644 --- a/docs/release/1.0/VALIDATION.md +++ b/docs/release/1.0/VALIDATION.md @@ -3,7 +3,7 @@ Scope: recorded automated/original-data checks. Exact final source revision and native platform build records accompany the published release. - Linux x64 Release build: all targets compiled. -- CTest: 34 passed, 157 skipped, zero failures (191 registered tests). Skips require separately configured original-data or save fixtures; they are not acceptance passes. +- CTest: 35 passed, 157 skipped, zero failures (192 registered tests). Skips require separately configured original-data or save fixtures; they are not acceptance passes. - Synthetic tests cover unchanged executable image loading, nine original strength ranges, atomic rejection of malformed ranges, unsupported environment guards, and legacy/extended/truncated crest gates. - Public source scan: zero findings; generated differences catalogue matches source markers. - Version output: both GUI and console identify 1.0.0-rc.1. @@ -31,3 +31,5 @@ Long-run diagnostics with seed 4711 and the existing explicit contract-renewal p After the final lineup, qualification and once-per-player season-reset corrections, each German edition completed 600 turns using seed 4711. Original CD reached twelve season-end desks; Diamond 3.6 and 3.7 reached eleven each. With the existing explicit contract-renewal and advertising policies enabled, all three simulator runs returned success, with no duplicate club players or other invariant failures. With contract renewals alone, all three still completed 600 turns without duplicate club players; only the test policy's excessive-debt warning remained. The latter is not presented as a successful financial strategy. The duplicate-player defect was reproduced and captured before correction: a temporary offer referenced a player already owned by a club, then the extended market-retirement pass regenerated that player a second time. Frozen private before/after saves and a cached-reference regression support the correction. Save-envelope diagnostics use a separate RNG and do not consume either gameplay stream. + +Calendar epoch regression covers the original CD and Diamond parameters, engine weekdays, desktop dates, save-header dates, year rollover and atomic rejection of invalid/unknown setup instructions. The original and checked Diamond date routines differ only in their weekday/year operands. After installing the original CD calendar parameters, the 600-turn run with contract and advertising policies completed eleven season-end desks without invariant failures. diff --git a/src/app/career_session.cpp b/src/app/career_session.cpp index 173f487..a6ed18e 100644 --- a/src/app/career_session.cpp +++ b/src/app/career_session.cpp @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-3.0-only #include "formats/badge_policy.hpp" +#include "game/calendar.hpp" #include "formats/executable_layout.hpp" #include "app/career_session.hpp" @@ -544,6 +545,7 @@ static void install_league_sizes(const LeImage& exe) { install_career_rules(exe); install_environment_rules(exe); install_original_badge_policy(exe); + install_calendar_rules(exe); } CareerSession::CareerSession(const std::filesystem::path& data, uint32_t watcom_seed) diff --git a/src/formats/executable_layout.hpp b/src/formats/executable_layout.hpp index 2e6d8b7..732827d 100644 --- a/src/formats/executable_layout.hpp +++ b/src/formats/executable_layout.hpp @@ -1986,3 +1986,5 @@ inline constexpr auto EnvironmentVerifiedHookEnd = 0x7b020; } namespace bmh::layout { inline constexpr auto ForeignBadgeGateInstruction = 0x10070; } + +namespace bmh::layout { inline constexpr auto CalendarDateSetup = 0x21270; } diff --git a/src/game/calendar.cpp b/src/game/calendar.cpp new file mode 100644 index 0000000..56f3f70 --- /dev/null +++ b/src/game/calendar.cpp @@ -0,0 +1,23 @@ +// SPDX-License-Identifier: GPL-3.0-only +#include "game/calendar.hpp" +#include "formats/executable_layout.hpp" +#include "formats/le.hpp" +namespace bmh { +namespace { CalendarEpoch installed_epoch{2026, 2}; } +CalendarEpoch calendar_epoch_from_executable(const LeImage& exe) { + constexpr auto setup = layout::CalendarDateSetup; + constexpr uint8_t weekday_prefix[] = {0xc6,0x44,0x24,0x04}; + constexpr uint8_t month_and_year_prefix[] = {0xc6,0x44,0x24,0x0c,0x07,0xc7,0x04,0x24}; + if (!exe.contains(setup) || !exe.contains(setup + 16)) + throw FormatError("calendar: missing original date setup"); + for (uint32_t i=0;i9999||weekday>6)throw FormatError("calendar: invalid original epoch"); + return {int(year),weekday}; +} +void install_calendar_rules(const LeImage& exe) { installed_epoch=calendar_epoch_from_executable(exe); } +CalendarEpoch calendar_epoch() { return installed_epoch; } +} diff --git a/src/game/calendar.hpp b/src/game/calendar.hpp new file mode 100644 index 0000000..6087ddd --- /dev/null +++ b/src/game/calendar.hpp @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: GPL-3.0-only +#pragma once +namespace bmh { +class LeImage; +struct CalendarEpoch { int year; int weekday; }; +// Read verified original date-setup operands; unknown routines are rejected. +CalendarEpoch calendar_epoch_from_executable(const LeImage& exe); +void install_calendar_rules(const LeImage& exe); +// Isolated authored states default to the established synthetic calendar. +CalendarEpoch calendar_epoch(); +} diff --git a/src/game/save.cpp b/src/game/save.cpp index affcb2a..cd27944 100644 --- a/src/game/save.cpp +++ b/src/game/save.cpp @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-3.0-only #include "formats/executable_layout.hpp" #include "game/save.hpp" +#include "game/calendar.hpp" #include #include @@ -25,7 +26,7 @@ void put32(Bytes& d, size_t at, uint32_t x) { for (int i = 0; i < 4 && at + size_t(i) < d.size(); i++) d[at + size_t(i)] = uint8_t(x >> (8 * i)); } -// DeskDate2(date, buffer, 0): "TT.MM.JJJJ" of a day counter (day 0 = 1 July 2026; a +// DeskDate2(date, buffer, 0): "TT.MM.JJJJ" of a day counter (day 0 = 1 July of the supplied edition; a // four-year leap cycle that starts with the third year). std::string date_text(uint32_t counter) { static const int kDays[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; @@ -45,7 +46,7 @@ std::string date_text(uint32_t counter) { if (++month == 13) month = 1, ++years; } char buf[16]; - std::snprintf(buf, sizeof buf, "%02d.%02d.%04d", day, month, 2026 + years); + std::snprintf(buf, sizeof buf, "%02d.%02d.%04d", day, month, calendar_epoch().year + years); return buf; } diff --git a/src/game/week.cpp b/src/game/week.cpp index a76836b..8221bba 100644 --- a/src/game/week.cpp +++ b/src/game/week.cpp @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-3.0-only #include "formats/executable_layout.hpp" #include "game/week.hpp" +#include "game/calendar.hpp" #include "game/matchday.hpp" #include "game/lineup.hpp" @@ -93,9 +94,7 @@ int attribute(Player& p, int area, bool write, int delta, int max) { } // namespace int weekday_code(uint32_t date) { - int c = int(date % 7) + 2; - if (c > 6) c = int(date % 7) - 5; - return c; + return int((date % 7 + uint32_t(calendar_epoch().weekday)) % 7); } void familiarity_decay(GameState& g, GameRandom& game, WatcomRandom& rand) { diff --git a/src/ui/desk_state.cpp b/src/ui/desk_state.cpp index 1baeceb..cdd5f54 100644 --- a/src/ui/desk_state.cpp +++ b/src/ui/desk_state.cpp @@ -2,6 +2,7 @@ #include "formats/badge_policy.hpp" #include "formats/executable_layout.hpp" #include "ui/desk_state.hpp" +#include "game/calendar.hpp" #include #include @@ -462,7 +463,11 @@ void validate_desk_state(const DeskState& d) { DeskDate desk_date(uint32_t counter, const LeImage& exe) { if (counter == 0xffff || counter > 3000000) throw FormatError("desk: unsupported date counter"); - DeskDate d; d.weekday = int((counter + 2) % 7); + // Partial data-only images use the explicitly installed calendar. Full + // executable images must supply their verified original date setup. + const auto epoch = exe.contains(layout::CalendarDateSetup) + ? calendar_epoch_from_executable(exe) : calendar_epoch(); + DeskDate d; d.weekday = int((counter + uint32_t(epoch.weekday)) % 7); int cycle = 3, years = 0; while (counter > uint32_t(364 + (cycle == 4))) { if (cycle == 4) { cycle = 0; --counter; } @@ -479,7 +484,7 @@ DeskDate desk_date(uint32_t counter, const LeImage& exe) { d.day -= month_days(); if (++d.month == 13) { d.month = 1; ++years; } } - d.year = 2026 + years; + d.year = epoch.year + years; return d; } diff --git a/src/ui/desk_state.hpp b/src/ui/desk_state.hpp index 459e15f..7bd70ec 100644 --- a/src/ui/desk_state.hpp +++ b/src/ui/desk_state.hpp @@ -113,7 +113,7 @@ struct DeskState { uint64_t revision = 0; }; -// Original DeskDate2, including its four-year leap cycle and Wednesday epoch. +// Original DeskDate2, including its four-year leap cycle and edition-specific epoch. DeskDate desk_date(uint32_t day_counter, const LeImage& executable); DeskState desk_from_save(const SaveState&, const LeImage&, uint64_t revision = 0); // Uses authoritative typed fields where StaticMemory contains stale copies. diff --git a/src/ui/desktop_tests.cpp b/src/ui/desktop_tests.cpp index cf4fc70..a473ff9 100644 --- a/src/ui/desktop_tests.cpp +++ b/src/ui/desktop_tests.cpp @@ -601,11 +601,17 @@ int main(int argc, char** argv) { const std::filesystem::path data = argv[1]; const auto exe = bmh::test::load_executable(asset_path(data,"BMH.exe")); paired_selection_matrix(exe); if(argc>3)paired_saved_projection(argv[3],exe); - // Gregorian dates are an independent oracle for the original's observed year range. + // The date arithmetic is checked against a controlled epoch aligned with + // the original leap cycle, independently of the installation's start year. + auto oracle_exe = exe; + const uint8_t oracle_year[] = {0xea,0x07,0,0}; + const uint8_t oracle_weekday = 2; + oracle_exe.patch(layout::CalendarDateSetup+13,oracle_year,4); + oracle_exe.patch(layout::CalendarDateSetup+4,&oracle_weekday,1); using namespace std::chrono; const sys_days epoch = year{2026}/7/1; for (uint32_t i = 0; i < 1461; ++i) { - const auto d = desk_date(i,exe); const year_month_day oracle(epoch + days{i}); + const auto d = desk_date(i,oracle_exe); const year_month_day oracle(epoch + days{i}); check(d.year == int(oracle.year()) && d.month == int(unsigned(oracle.month())) && d.day == int(unsigned(oracle.day())),"calendar/leap boundary mismatch"); } diff --git a/tests/calendar_epoch_tests.cpp b/tests/calendar_epoch_tests.cpp new file mode 100644 index 0000000..badd705 --- /dev/null +++ b/tests/calendar_epoch_tests.cpp @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: GPL-3.0-only +#include "game/calendar.hpp" +#include "game/week.hpp" +#include "game/save.hpp" +#include "formats/executable_layout.hpp" +#include "formats/le.hpp" +#include "ui/desk_state.hpp" +#include +#include +#include + +void check(bool ok,const char* message){if(!ok)throw std::runtime_error(message);} +bmh::LeImage image(int year,int weekday) { + constexpr uint32_t base=0x10000; + constexpr size_t image_size=0xb0000; + auto exe=bmh::LeImage::from_bytes(base,bmh::Bytes(image_size,0)); + const uint8_t setup[]={0xc6,0x44,0x24,0x04,uint8_t(weekday),0xc6,0x44,0x24,0x0c,0x07,0xc7,0x04,0x24, + uint8_t(year),uint8_t(year>>8),0,0}; + exe.patch(bmh::layout::CalendarDateSetup,setup,sizeof setup); + const uint8_t months[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; + exe.patch(bmh::layout::DeskState_DeskDate_RuntimeField,months,sizeof months); + return exe; +} +int main()try { + for(const auto [year,day]:{std::pair{1995,5},std::pair{2026,2}}) { + auto exe=image(year,day);bmh::install_calendar_rules(exe); + check(bmh::weekday_code(0)==day && bmh::weekday_code(7)==day,"engine weekday ignores edition epoch"); + const auto date=bmh::ui::desk_date(0,exe); + check(date.year==year && date.month==7 && date.day==1 && date.weekday==day,"desktop date ignores edition epoch"); + bmh::GameState g; + g.memory.add_block(bmh::layout::ManagerCount,1); + const auto header=bmh::save_header(g); + const auto expected="01.07."+std::to_string(year); + check(std::string(reinterpret_cast(header.data()+1))==expected,"save-list date ignores edition epoch"); + const auto rollover=bmh::ui::desk_date(184,exe); + check(rollover.year==year+1 && rollover.month==1 && rollover.day==1,"edition year rollover incorrect"); + } + auto bad=image(2026,7);bool rejected=false; + try{bmh::install_calendar_rules(bad);}catch(const bmh::FormatError&){rejected=true;} + check(rejected && bmh::calendar_epoch().year==2026 && bmh::calendar_epoch().weekday==2,"invalid epoch partially installed"); + auto different=image(1995,5);const uint8_t unknown=0;different.patch(bmh::layout::CalendarDateSetup,&unknown,1); + rejected=false;try{bmh::calendar_epoch_from_executable(different);}catch(const bmh::FormatError&){rejected=true;} + check(rejected,"unknown date routine was guessed"); + std::cout<<"Edition calendar epoch, save header and year rollover passed\n";return 0; +}catch(const std::exception&e){std::cerr<