diff --git a/CMakeLists.txt b/CMakeLists.txt index d0e168a..9baa403 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -232,6 +232,7 @@ add_library(bmhcore STATIC src/game/reality_ball.cpp src/game/reality_duel.cpp src/game/reality_reporter.cpp + src/game/long_career.cpp src/game/matchday.cpp src/game/sportschau_replay.cpp src/game/sportschau_rating.cpp @@ -1178,3 +1179,6 @@ bmh_add_data_test(NAME transfer_seller_app_tests REQUIRED_OPTIONS BMH_TEST_TRANS add_executable(wide_number_unit_tests tests/wide_number_unit_tests.cpp) target_link_libraries(wide_number_unit_tests PRIVATE bmhcore) add_test(NAME wide_number_unit_tests COMMAND wide_number_unit_tests) +add_executable(long_career_tests tests/long_career_tests.cpp) +target_link_libraries(long_career_tests PRIVATE bmhcore) +add_test(NAME long_career_tests COMMAND long_career_tests) diff --git a/docs/COMPATIBILITY.md b/docs/COMPATIBILITY.md index fe77687..db9844e 100644 --- a/docs/COMPATIBILITY.md +++ b/docs/COMPATIBILITY.md @@ -112,3 +112,23 @@ boundaries. Post-match overview dismissal owns its full mouse gesture, including release after pointer movement, before the following calendar decision can open. The pitch uses each edition's original position-distance tables, atlas swatches and pointer bitmap for placement feedback; this display state is not serialized. +OpenBMH deviates from the original: cumulative human-club appearances, goals and +assists have optional 64-bit counters. Original byte/word fields keep their +modulo representation for gameplay/layout compatibility; roster projections use +the wider values. Tracking begins with the surviving legacy values, so high bits +already lost in an imported save cannot be recovered. A new arrival clears the +previous squad occupant's totals. Club promotion remaps counter ownership. + +Title-holder and cup-winner lists retain all observed seasons in the same optional +OpenBMH save extension. The Original nine-by-fifty table remains a rolling window. +The archive captures that window before it shifts, and the title list can page +through the archived rows. Late legacy imports start with their surviving fifty +seasons; earlier winners are unknown and are not fabricated. Stored winner names +are historical snapshots, so later renaming and promotion cannot change them. +The separate manager-achievements graph retains its Original fifty-season view. + +These records use a versioned `OBMHEXT1` extension after the Original name pools; +see [save format](formats/LONG_CAREER.md). Original programs and older OpenBMH +writers do not preserve this archive. Keep an OpenBMH backup when exchanging +careers with those writers. The native calendar still has its existing signed +16-bit season limit; this patch does not claim an unlimited calendar. diff --git a/docs/formats/LONG_CAREER.md b/docs/formats/LONG_CAREER.md new file mode 100644 index 0000000..028da30 --- /dev/null +++ b/docs/formats/LONG_CAREER.md @@ -0,0 +1,34 @@ +# Optional long-career statistics and title archive + +The existing `OBMHEXT1` container can contain block `0x4f424c43` (OpenBMH-owned, +not a guest memory address). Original state blocks and all four name pools retain +their sizes and ordering. No extension is emitted until an archive is needed. +Payload integers use little-endian encoding: + +| Field | Encoding | +|---|---| +| Payload version | u32, currently 1 | +| Player/club record count | u32 | +| Known season record count | u32 | +| Each player/club record | u32 key, then six u64 counters | +| Each season record | u32 absolute zero-based season, then nine names | +| Each name | u16 byte length followed by Original-encoding bytes | + +The player key is `(runtime_club << 12) | player_id`. The six counters are league, +national-cup and European-cup appearances, total appearances, goals and assists. +Club swaps remap keys; fresh squad entry initialization resets the new player's +club record. The Original byte/word fields continue to update normally. + +Season names follow the nine Original title categories. An empty name is a known +season with no holder for that category. Missing season records are unknown, +not an assertion that no competition was held. Names are owned snapshots at the +award/capture time, independent of future club-number and name-pool changes. +A legacy import contributes only its surviving window; no earlier information +is inferred. The title list uses the earliest known absolute season when paging +and compensates for the native rolling-window year offset. + +The decoder rejects unknown versions, truncated payloads, invalid/duplicate +identities and seasons, duplicate blocks and trailing bytes before publishing +archive state. Season records remain within the native signed 16-bit calendar +range. The surrounding FBM checksum covers the extension. Older writers may +discard extensions; Original-game roundtrip preservation is not promised. diff --git a/src/game/cup.cpp b/src/game/cup.cpp index 766843f..9ff2caa 100644 --- a/src/game/cup.cpp +++ b/src/game/cup.cpp @@ -622,7 +622,7 @@ void advance_cup(GameState& g, int id, WatcomRandom& rand, MinuteReport& report, g.manager_records.at(at) = uint8_t(n + 1), g.manager_records.at(at + 1) = uint8_t((n + 1) >> 8); } // ListTitleHoldersAf(winner, title): the list of title holders (Titles) - mem.set16(::bmh::layout::Titles + uint32_t(title) * 100 + uint32_t(std::min(g.season, 0x31)) * 2, winners[0]); + record_title_holder(g, title, winners[0]); } if (type == 0 && !winners.empty()) { // the national cup: winner (Cup_AdvanceCup_RuntimeField5) and the previous holder @@ -654,7 +654,7 @@ void advance_cup(GameState& g, int id, WatcomRandom& rand, MinuteReport& report, if (mem.u8(::bmh::layout::Cup_AdvanceCup_RuntimeField) == 0 && winner_display) winner_display(g, {7, winner}); if (mem.u8(::bmh::layout::Cup_AdvanceCup_RuntimeField) == 0 && mem.has(::bmh::layout::CareerSession_BeginFriendlyOpponentTable_ExecutableField2)) mem.set8(::bmh::layout::CareerSession_BeginFriendlyOpponentTable_ExecutableField2, 0); - mem.set16(::bmh::layout::Titles + 7 * 100 + uint32_t(std::min(g.season, 0x31)) * 2, winner); + record_title_holder(g, 7, winner); if (mem.has(::bmh::layout::Cup_AdvanceCup_RuntimeField7)) mem.set16(::bmh::layout::Cup_AdvanceCup_RuntimeField7, winner); const int manager = g.manager_of(winner); if (manager >= 0) { diff --git a/src/game/long_career.cpp b/src/game/long_career.cpp new file mode 100644 index 0000000..dac20c5 --- /dev/null +++ b/src/game/long_career.cpp @@ -0,0 +1,149 @@ +// SPDX-License-Identifier: GPL-3.0-only +#include "game/long_career.hpp" +#include "game/state.hpp" +#include "formats/executable_layout.hpp" +#include +#include +#include + +namespace bmh { +namespace { +uint32_t key(int team, int player) { + if (team < 0 || team > 0xfff || player < 0 || player >= 0xfff) + throw FormatError("long career: invalid player/club identity"); + return uint32_t(team) << 12 | uint32_t(player); +} +int manager_team(const GameState& g, int manager) { + if (manager >= 0 && size_t(manager) < g.manager_teams.size()) return g.manager_teams[size_t(manager)]; + const size_t at = size_t(manager) * 0xb0c + 0x290; + if (manager < 0 || manager > 4 || at + 1 >= g.manager_records.size()) + throw FormatError("long career: invalid manager"); + return int(g.manager_records[at] | uint16_t(g.manager_records[at + 1]) << 8); +} +PlayerClubStatistics legacy(const std::array& b) { + auto word = [&](size_t at) { return uint64_t(b[at] | uint16_t(b[at + 1]) << 8); }; + return {{b[7], b[8], b[9]}, word(0x48), word(0x4a), word(0x4c)}; +} +void increment(uint64_t& n) { + if (n == std::numeric_limits::max()) throw FormatError("long career: statistics counter exhausted"); + ++n; +} +void put(Bytes& b, uint64_t n, int width) { for (int i = 0; i < width; ++i) b.push_back(uint8_t(n >> (i * 8))); } +struct Reader { + const Bytes& bytes; size_t pos = 0; + uint64_t get(int width) { + if (size_t(width) > bytes.size() - pos) throw FormatError("long career: truncated extension"); + uint64_t n = 0; for (int i = 0; i < width; ++i) n |= uint64_t(bytes[pos++]) << (i * 8); return n; + } + std::string string() { + const size_t size = size_t(get(2)); + if (size > bytes.size() - pos) throw FormatError("long career: truncated title name"); + std::string s(bytes.begin() + pos, bytes.begin() + pos + size); pos += size; return s; + } +}; +} +PlayerClubStatistics player_club_statistics(const GameState& g, int team, int player, + const std::array& board) { + const auto found = g.player_statistics.find(key(team, player)); + return found == g.player_statistics.end() ? legacy(board) : found->second; +} +void record_player_statistics(GameState& g, int manager, int row, int player, int cls, int kind) { + const size_t at = size_t(manager) * 0x840 + size_t(row) * 0x58; + if (manager < 0 || row < 0 || row >= 24 || at + 0x58 > g.tactics.size() || cls < 0 || cls > 3 || kind < 0 || kind > 2) + throw FormatError("long career: invalid statistics row/event"); + std::array board; std::copy_n(g.tactics.begin() + at, board.size(), board.begin()); + const auto identity = key(manager_team(g, manager), player); + auto [found, inserted] = g.player_statistics.try_emplace(identity, legacy(board)); + auto next = found->second; + if (kind == 0) { if (cls < 3) increment(next.appearances[size_t(cls)]); increment(next.total); } + else if (kind == 1) increment(next.goals); + else increment(next.assists); + found->second = next; +} +void reset_player_statistics(GameState& g, int manager, int player) { + g.player_statistics.erase(key(manager_team(g, manager), player)); +} +void swap_statistics_clubs(GameState& g, int first, int second) { + PlayerStatisticsArchive next; + for (const auto& [identity, value] : g.player_statistics) { + const int club = int(identity >> 12), player = int(identity & 0xfff); + next.emplace(key(club == first ? second : club == second ? first : club, player), value); + } + g.player_statistics = std::move(next); +} +TitleHistoryArchive title_history_rows(const GameState& g) { + if (g.season < 0) throw FormatError("long career: negative season index"); + auto rows = g.title_history; + if (!g.memory.has(layout::Titles + 899)) return rows; + const int first = std::max(0, g.season - 49); + for (int slot = 0; slot < 50 && first + slot <= g.season; ++slot) { + const auto season = uint32_t(first + slot); + if (rows.contains(season)) continue; + std::array winners; + for (size_t category = 0; category < 9; ++category) { + const auto team = g.memory.u16(layout::Titles + uint32_t(category) * 100 + uint32_t(slot) * 2); + if (team != 0xffff) winners[category] = g.club_name(team); + } + rows.emplace(season, std::move(winners)); + } + return rows; +} +void capture_title_history(GameState& g) { g.title_history = title_history_rows(g); } +void record_title_holder(GameState& g, int category, uint16_t winner) { + if (category < 0 || category >= 9 || g.season < 0 || winner >= g.club_ids.size()) + throw FormatError("long career: invalid title award"); + capture_title_history(g); + const auto name = g.club_name(winner); + g.memory.set16(layout::Titles + uint32_t(category) * 100 + uint32_t(std::min(g.season, 49)) * 2, winner); + g.title_history[uint32_t(g.season)][size_t(category)] = name; +} +void shift_legacy_title_history(GameState& g) { + if (g.season <= 49) return; + for (uint32_t category = 0; category < 9; ++category) { + for (uint32_t i = 0; i < 98; ++i) + g.memory.set8(layout::Titles + category * 100 + i, + g.memory.u8(layout::Season_SeasonEndSteps_RuntimeField + category * 100 + i)); + g.memory.set16(layout::Season_SeasonEndSteps_RuntimeField2 + category * 100, 0xffff); + } +} +Bytes encode_long_career(const GameState& g) { + if (g.player_statistics.empty() && g.title_history.empty()) return {}; + Bytes b; put(b, 1, 4); put(b, g.player_statistics.size(), 4); put(b, g.title_history.size(), 4); + for (const auto& [identity, s] : g.player_statistics) { + put(b, identity, 4); for (const auto n : s.appearances) put(b, n, 8); + for (const auto n : {s.total, s.goals, s.assists}) put(b, n, 8); + } + for (const auto& [season, names] : g.title_history) { + if (season > 32767) throw FormatError("long career: archived season exceeds native calendar range"); + put(b, season, 4); + for (const auto& name : names) { + if (name.size() > UINT16_MAX) throw FormatError("long career: title name too long"); + put(b, name.size(), 2); b.insert(b.end(), name.begin(), name.end()); + } + } + return b; +} +void decode_long_career(GameState& g, const Bytes& bytes) { + Reader r{bytes}; + if (r.get(4) != 1) throw FormatError("long career: unsupported extension version"); + const auto players = r.get(4), seasons = r.get(4); + if (players > (bytes.size() - r.pos) / 52 || seasons > 32768) + throw FormatError("long career: invalid extension counts"); + PlayerStatisticsArchive stats; TitleHistoryArchive history; + for (uint64_t i = 0; i < players; ++i) { + const auto identity = uint32_t(r.get(4)); + if (identity > 0xffffff || (identity & 0xfff) == 0xfff) throw FormatError("long career: invalid archived identity"); + PlayerClubStatistics s; for (auto& n : s.appearances) n = r.get(8); + s.total = r.get(8); s.goals = r.get(8); s.assists = r.get(8); + if (!stats.emplace(identity, s).second) throw FormatError("long career: duplicate archived identity"); + } + for (uint64_t i = 0; i < seasons; ++i) { + const auto season = uint32_t(r.get(4)); + if (season > 32767) throw FormatError("long career: invalid archived season"); + std::array names; for (auto& name : names) name = r.string(); + if (!history.emplace(season, std::move(names)).second) throw FormatError("long career: duplicate archived season"); + } + if (r.pos != bytes.size()) throw FormatError("long career: trailing extension bytes"); + g.player_statistics = std::move(stats); g.title_history = std::move(history); +} +} diff --git a/src/game/long_career.hpp b/src/game/long_career.hpp new file mode 100644 index 0000000..0b10873 --- /dev/null +++ b/src/game/long_career.hpp @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-3.0-only +#pragma once +#include +#include +#include +#include +#include "formats/binary.hpp" + +namespace bmh { +struct GameState; +struct PlayerClubStatistics { + std::array appearances{}; + uint64_t total = 0, goals = 0, assists = 0; + bool operator==(const PlayerClubStatistics&) const = default; +}; +// Optional OBMHEXT1 block, outside the Original guest address space. Payload v1. +inline constexpr uint32_t kLongCareerExtension = 0x4f424c43; +using PlayerStatisticsArchive = std::map; +using TitleHistoryArchive = std::map>; +PlayerClubStatistics player_club_statistics(const GameState&, int team, int player, + const std::array& board); +void record_player_statistics(GameState&, int manager, int row, int player, int competition_class, int kind); +void reset_player_statistics(GameState&, int manager, int player); +void swap_statistics_clubs(GameState&, int first, int second); +TitleHistoryArchive title_history_rows(const GameState&); +void capture_title_history(GameState&); +void record_title_holder(GameState&, int category, uint16_t winner); +void shift_legacy_title_history(GameState&); +Bytes encode_long_career(const GameState&); +void decode_long_career(GameState&, const Bytes&); +} diff --git a/src/game/matchday.cpp b/src/game/matchday.cpp index 7e60bbf..6740179 100644 --- a/src/game/matchday.cpp +++ b/src/game/matchday.cpp @@ -242,6 +242,7 @@ void player_statistic(GameState& g, int type, int player, int manager, int slot, if (cls == 3 && (g.player_bonus.at(player) & 0x7F) < 0x7F) g.player_bonus.at(player)++; if (manager >= 0) { pay_bonus(g, manager, slot, 2, true); + record_player_statistics(g, manager, slot, player, cls, kind); if (cls < 3) g.tactics.at(board + 7 + cls)++; wr16(g.tactics, board + 0x48, uint16_t(rd16(g.tactics, board + 0x48) + 1)); } @@ -249,13 +250,17 @@ void player_statistic(GameState& g, int type, int player, int manager, int slot, } else if (kind == 1) { // goal if (manager >= 0) { pay_bonus(g, manager, slot, 0, true); + record_player_statistics(g, manager, slot, player, cls, kind); wr16(g.tactics, board + 0x4a, uint16_t(rd16(g.tactics, board + 0x4a) + 1)); if (cls == 0) g.manager_records.at(size_t(manager) * kManagerRecord + 0x1ae + std::min(minute, 89) / 5)++; } // league goals in bits 2-7 (a 64th goal carries into bit 8, as in the original) if (cls == 0) p.w364 = (w364 & 0xFFFFFF03u) | ((((w364 >> 2) & 0x3F) << 2) + 4); } else if (kind == 2) { // assist - if (manager >= 0) wr16(g.tactics, board + 0x4c, uint16_t(rd16(g.tactics, board + 0x4c) + 1)); + if (manager >= 0) { + record_player_statistics(g, manager, slot, player, cls, kind); + wr16(g.tactics, board + 0x4c, uint16_t(rd16(g.tactics, board + 0x4c) + 1)); + } if (cls == 0) p.w368 = (p.w368 & 0xFF03FFFFu) | ((((p.w368 >> 18) & 0x3F) + 1) << 18); } } @@ -776,7 +781,7 @@ void advance_league_rounds(GameState& g, uint32_t competitions, WatcomRandom& ra if (g.memory.u8(::bmh::layout::Cup_AdvanceCup_RuntimeField) == 0 && winner) winner(g, {0, uint16_t(champion)}); if (g.memory.u8(::bmh::layout::Cup_AdvanceCup_RuntimeField) == 0 && g.memory.has(::bmh::layout::CareerSession_BeginFriendlyOpponentTable_ExecutableField2)) g.memory.set8(::bmh::layout::CareerSession_BeginFriendlyOpponentTable_ExecutableField2, 0); - g.memory.set16(::bmh::layout::Titles + uint32_t(std::min(g.season, 0x31)) * 2, uint16_t(champion)); + record_title_holder(g, 0, uint16_t(champion)); const int manager = g.manager_of(champion); if (manager >= 0) { const size_t at = size_t(manager) * kManagerRecord + 0x8a4; diff --git a/src/game/national.cpp b/src/game/national.cpp index e09ec25..5b36dcf 100644 --- a/src/game/national.cpp +++ b/src/game/national.cpp @@ -77,6 +77,8 @@ void new_board_entry(GameState& g, int manager, int index, int slot, WatcomRando // OpenBMH deviates from the original: a new arrival must not inherit the // previous occupant's retirement/refusal/month-processing flags. e[0xb] = 0; + std::memset(e + 0x48, 0, 6); // Fresh arrivals do not inherit the previous player's totals. + reset_player_statistics(g, manager, g.squad_slots.at(size_t(slot)) & 0xfff); e[10] = 2; for (int k = 0; k < 4; k++) e[2 + k] = index < 2 ? 1 : uint8_t(rand.range(2, 0x27)); e[0x3f] = 0xff; diff --git a/src/game/save.cpp b/src/game/save.cpp index cd27944..4d2ba49 100644 --- a/src/game/save.cpp +++ b/src/game/save.cpp @@ -126,6 +126,8 @@ Bytes save_stream(const GameState& live) { for (uint32_t i = 0; i < size; ++i) bytes[i] = g.memory.u8(at + i); extension.push_back({at, std::move(bytes)}); } + auto history = encode_long_career(g); + if (!history.empty()) extension.push_back({kLongCareerExtension, std::move(history)}); append_save_extension(stream, extension); return stream; } diff --git a/src/game/season.cpp b/src/game/season.cpp index 739edeb..fa28827 100644 --- a/src/game/season.cpp +++ b/src/game/season.cpp @@ -787,6 +787,7 @@ void change_places(GameState& g, int a, int b) { mem.set32(::bmh::layout::Career_RemoveJoinLoans_RuntimeField + k * 4, (e & 0xfff) | uint32_t(seller) << 12 | uint32_t(buyer) << 22); } if (!g.club_table.empty() && a < 0xa8 && b < 0xa8) swap_bytes(g.club_table, size_t(a) * 0x4a, size_t(b) * 0x4a, 0x4a); + swap_statistics_clubs(g, a, b); std::swap(g.club_ids.at(size_t(a)), g.club_ids.at(size_t(b))); swap_bytes(g.club_heap_bc, size_t(a) * 2, size_t(b) * 2, 2); std::swap(g.team_morale.at(size_t(a)), g.team_morale.at(size_t(b))); @@ -1284,10 +1285,7 @@ SeasonTransitionOutcome season_end_steps(GameState& g, GameRandom& game, WatcomR TransferAllocatorContext allocator) { StaticMemory& mem = g.memory; if (g.season > 0x31) { // SeasonEndSteps - for (uint32_t k = 0; k < 9; k++) { - for (uint32_t i = 0; i < 0x62; i++) mem.set8(::bmh::layout::Titles + k * 100 + i, mem.u8(::bmh::layout::Season_SeasonEndSteps_RuntimeField + k * 100 + i)); - mem.set16(::bmh::layout::Season_SeasonEndSteps_RuntimeField2 + k * 100, 0xffff); - } + shift_legacy_title_history(g); for (size_t m = 0; m < g.manager_teams.size(); m++) { for (size_t i = 0; i < 0x93; i++) rec8(g, int(m), 0xda + i) = rec8(g, int(m), 0xdd + i); const size_t at = m * 0xB0C + 0x2aa; @@ -1610,6 +1608,7 @@ SeasonTransitionOutcome season_transition(GameState& g, GameRandom& game, Watcom } g.memory.set8(::bmh::layout::CareerSession_RuntimeError_RuntimeField4, 0); if (g.memory.has(::bmh::layout::Season_SeasonTransition_RuntimeField)) g.memory.set8(::bmh::layout::Season_SeasonTransition_RuntimeField, 0); + capture_title_history(g); // Preserve the current window before it advances/shifts. g.season = int16_t(g.season + 1); // SeasonIndex reset_club_moods(g, game, rand); european_setup(g, game, rand, cup_draw); diff --git a/src/game/state.cpp b/src/game/state.cpp index 7e12be8..6f3e3f5 100644 --- a/src/game/state.cpp +++ b/src/game/state.cpp @@ -99,7 +99,12 @@ GameState GameState::from_save(const SaveState& save, const DbCounts& c) { g.foreign_strength = bytes(::bmh::layout::ForeignClubStrength); g.memory = StaticMemory(save); // memory OpenBMH saves behind the trailer (the English cup blocks, career.hpp) + bool long_career_seen = false; for (const auto& b : save.extension()) { + if (b.address == kLongCareerExtension) { + if (long_career_seen) throw FormatError("long career: duplicate extension block"); + decode_long_career(g, b.bytes); long_career_seen = true; continue; + } if (!g.memory.has(b.address)) g.memory.add_block(b.address, uint32_t(b.bytes.size())); for (size_t i = 0; i < b.bytes.size(); ++i) g.memory.set8(b.address + uint32_t(i), b.bytes[i]); } @@ -110,6 +115,8 @@ GameState GameState::from_save(const SaveState& save, const DbCounts& c) { g.stadium_names = save.pool(3); g.player_name_offsets = words16(::bmh::layout::PlayerNameOffsets); g.season = int16_t(save.data(::bmh::layout::SeasonIndex)[0] | save.data(::bmh::layout::SeasonIndex)[1] << 8); + if (!g.title_history.empty() && (g.season < 0 || g.title_history.rbegin()->first > uint32_t(g.season))) + throw FormatError("long career: archived season is beyond the saved calendar"); return g; } diff --git a/src/game/state.hpp b/src/game/state.hpp index 6369a41..3b10f7f 100644 --- a/src/game/state.hpp +++ b/src/game/state.hpp @@ -17,6 +17,7 @@ #include "formats/textd.hpp" #include "game/memory.hpp" #include "game/random.hpp" +#include "game/long_career.hpp" #include "game/reality_motion.hpp" namespace bmh { @@ -36,6 +37,8 @@ constexpr uint16_t kEmptySlot = 0x0FFF; constexpr uint16_t kSlotInLineup = 0x8000; struct GameState { + PlayerStatisticsArchive player_statistics; + TitleHistoryArchive title_history; std::vector squad_slots; // *SquadSlots, all squads back to back (see squad_range) std::vector players; // *PlayerConditionTechniqueForm / *PlayerSkillWords / *PlayerIdentityWords, names not filled std::vector team_morale; // *TeamMorale, per team: bits 0-5 morale / 2, bits 6-7 flags diff --git a/src/ui/club_info.cpp b/src/ui/club_info.cpp index 291de46..81c11b9 100644 --- a/src/ui/club_info.cpp +++ b/src/ui/club_info.cpp @@ -28,7 +28,7 @@ void rect(Image& out, int x, int y, int w, int h, uint8_t color) { for (int yy = std::max(0, y); yy < std::min(out.height, y + h); ++yy) for (int xx = std::max(0, x); xx < std::min(out.width, x + w); ++xx) out.at(xx, yy) = color; } -std::string padded(int value, int width = 2) { +std::string padded(uint64_t value, int width = 2) { std::string s = std::to_string(value); if (int(s.size()) < width) s.insert(0, size_t(width - int(s.size())), '%'); return s; @@ -110,7 +110,10 @@ ClubInfoProjection club_info_from_game(const GameState& g, int team, player.cards = g.player_cards[id]; player.bonus = g.player_bonus[id]; } - if (board) player.manager_board = std::move(board); + if (board) { + player.manager_board = std::move(board); + if (player.player_id >= 0) player.statistics = player_club_statistics(g, team, player.player_id, *player.manager_board); + } if (!player.borrowed && player.player_id >= 0) { for (int loan = 0; loan < 200; ++loan) { const uint32_t address = ::bmh::layout::Career_RemoveJoinLoans_RuntimeField + uint32_t(loan) * 4; @@ -296,9 +299,9 @@ Image ClubInfoScreen::render() const { std::string attrs = padded(p.bonus & 127, 3) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5); if (p.manager_board) { const auto& b = *p.manager_board; - attrs += padded(b[7], 3) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5) + padded(b[9], 3) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5); - Bytes raw(b.begin(), b.end()); attrs += padded(le16(raw, 0x48), 3) + exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2) + - padded(le16(raw, 0x4a), 3) + exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2) + padded(le16(raw, 0x4c), 3) + exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField); + attrs += padded(p.statistics ? p.statistics->appearances[0] : b[7], 3) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5) + padded(p.statistics ? p.statistics->appearances[2] : b[9], 3) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5); + Bytes raw(b.begin(), b.end()); attrs += padded((p.statistics ? p.statistics->total : le16(raw, 0x48)), 3) + exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2) + + padded((p.statistics ? p.statistics->goals : le16(raw, 0x4a)), 3) + exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2) + padded((p.statistics ? p.statistics->assists : le16(raw, 0x4c)), 3) + exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField); } text(out, 165, baseline, attrs, 0x6c); } diff --git a/src/ui/club_info.hpp b/src/ui/club_info.hpp index 322a97f..465cb0c 100644 --- a/src/ui/club_info.hpp +++ b/src/ui/club_info.hpp @@ -36,6 +36,7 @@ struct ClubInfoPlayer { bool offer_name_color = false; // Present only when this runtime club belongs to a human manager. std::optional> manager_board; + std::optional statistics; }; // Owned inputs for one render of ClubInfoFromGame / LineupScreen. Build this from the actual diff --git a/src/ui/contracts.cpp b/src/ui/contracts.cpp index add8bcf..674a814 100644 --- a/src/ui/contracts.cpp +++ b/src/ui/contracts.cpp @@ -148,6 +148,7 @@ ContractsState contracts_from_game(const GameState& game, int manager_slot, uint view.source.name = game.player_name(player); view.source.attributes = source; view.source.cards = game.player_cards.at(size_t(player)); view.source.bonus = game.player_bonus.at(size_t(player)); std::copy_n(game.tactics.begin() + std::ptrdiff_t(at), 0x58, view.source.board.begin()); + view.source.statistics = player_club_statistics(game, team, player, view.source.board); state.players.push_back(std::move(view)); } if (!state.players.empty()) { @@ -314,6 +315,7 @@ Image ContractsScreen::render_roster() const { text(out, x, y, original_to_utf8(raw), color, -1); }; const auto fixed = [](uint32_t value, int width, bool grouped = false) { return original_number(value, width, grouped); }; + const auto count = [](uint64_t value) { auto s = std::to_string(value); if(s.size()<3)s.insert(0,3-s.size(),'%'); return s; }; const auto title = original_to_utf8(texts_[(::bmh::layout::Contracts_RenderRoster_TextPointer2 - ::bmh::layout::TextPointerTable) / 4]); sprite_font_.draw(out, SpriteFont::centered_title_x(sprite_font_.text_width(title)), 13, title, 5); sprite_font_.draw(out, 5, 235, state_.club_name, 5); @@ -374,9 +376,9 @@ Image ContractsScreen::render_roster() const { original_text(65, baseline, fixed(a.age(), 2) + texts_[(::bmh::layout::ClubInfo_Render_TextPointer5 - ::bmh::layout::TextPointerTable) / 4], 0x6c); for (int skill = 0; skill < 7; ++skill) copy_region(out, atlas_, 256 + a.skill(Player::Skill(skill)) * 16, 32, 87 + skill * 10, baseline - 5, 16, 5, true); auto word = [&](int at) { return uint16_t(s.board[at] | uint16_t(s.board[at + 1]) << 8); }; - original_text(165, baseline, fixed(s.bonus & 127, 3) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5) + fixed(s.board[7], 3) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5) + - fixed(s.board[9], 3) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5) + fixed(word(0x48), 3) + exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2) + - fixed(word(0x4a), 3) + exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2) + fixed(word(0x4c), 3) + exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField), 0x6c); + original_text(165, baseline, fixed(s.bonus & 127, 3) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5) + count(s.statistics ? s.statistics->appearances[0] : s.board[7]) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5) + + count(s.statistics ? s.statistics->appearances[2] : s.board[9]) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5) + count(s.statistics ? s.statistics->total : word(0x48)) + exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2) + + count(s.statistics ? s.statistics->goals : word(0x4a)) + exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2) + count(s.statistics ? s.statistics->assists : word(0x4c)) + exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField), 0x6c); } baseline += 7; } diff --git a/src/ui/lineup.cpp b/src/ui/lineup.cpp index e6ef68d..91eb238 100644 --- a/src/ui/lineup.cpp +++ b/src/ui/lineup.cpp @@ -83,7 +83,7 @@ void copy(Image& out, const Image& src, int sx, int sy, int x, int y, int w, int if (c != transparent && x+xx>=0 && x+xx=0 && y+yy= 0) + p.statistics = player_club_statistics(g, out.runtime_club, p.player_id, p.board); out.source="original_save_read_only"; return out; } LineupState lineup_from_game(const GameState& g,const LeImage& exe,uint64_t revision) { auto out=project(g.memory,exe,g.manager_teams,g.squad_slots,g.players,g.tactics,g.manager_options, g.player_cards,g.player_bonus,g.player_name_offsets,g.player_names,g.club_ids,g.club_names,revision); + for (auto& p : out.players) if (p.player_id >= 0) + p.statistics = player_club_statistics(g, out.runtime_club, p.player_id, p.board); out.source="engine_projection"; return out; } @@ -224,9 +229,9 @@ Image LineupScreen::render() const { } else { text(out,65,baseline,number(a.age())+texts_[(::bmh::layout::ClubInfo_Render_TextPointer5-::bmh::layout::TextPointerTable)/4],0x6c); for(int skill=0;skill<7;++skill)copy(out,atlas_,256+a.skill(Player::Skill(skill))*16,32,87+skill*10,baseline-5,16,5,0); - text(out,165,baseline,number(p.bonus&127,3)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5)+number(p.board[7],3)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5)+ - number(p.board[9],3)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5)+number(le16(Bytes(p.board.begin(),p.board.end()),0x48),3)+exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2)+ - number(le16(Bytes(p.board.begin(),p.board.end()),0x4a),3)+exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2)+number(le16(Bytes(p.board.begin(),p.board.end()),0x4c),3)+exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField),0x6c); + text(out,165,baseline,number(p.bonus&127,3)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5)+number(p.statistics ? p.statistics->appearances[0] : p.board[7],3)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5)+ + number(p.statistics ? p.statistics->appearances[2] : p.board[9],3)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5)+number((p.statistics ? p.statistics->total : le16(Bytes(p.board.begin(),p.board.end()),0x48)),3)+exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2)+ + number((p.statistics ? p.statistics->goals : le16(Bytes(p.board.begin(),p.board.end()),0x4a)),3)+exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2)+number((p.statistics ? p.statistics->assists : le16(Bytes(p.board.begin(),p.board.end()),0x4c)),3)+exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField),0x6c); } } button(out,1,215,texts_[(::bmh::layout::Lineup_Render_TextPointer-::bmh::layout::TextPointerTable)/4+(state_.view==LineupView::data_a?1:0)]); @@ -310,6 +315,8 @@ std::string LineupScreen::state_json() const { if(p.player_id>=0)s<<",\"starting\":"<<((p.slot&0x8000)?"true":"false")<<",\"bench\":"<<((p.slot&0x4000)?"true":"false") <<",\"position_type\":"<appearances[0] << ',' << p.statistics->appearances[1] << ',' << p.statistics->appearances[2] + << "],\"total_appearances\":" << p.statistics->total << ",\"club_goals\":" << p.statistics->goals << ",\"club_assists\":" << p.statistics->assists; s<<'}';} s<<"]}";return s.str(); } diff --git a/src/ui/lineup.hpp b/src/ui/lineup.hpp index 417dc97..3558a66 100644 --- a/src/ui/lineup.hpp +++ b/src/ui/lineup.hpp @@ -6,6 +6,7 @@ #include #include "formats/font.hpp" +#include "game/long_career.hpp" #include "formats/m3d.hpp" #include "formats/textd.hpp" #include "ui/setup.hpp" @@ -22,6 +23,7 @@ struct LineupPlayer { uint16_t cards = 0; uint8_t bonus = 0; std::array board{}; + std::optional statistics; }; struct LineupState { int manager_slot = -1, runtime_club = -1; diff --git a/src/ui/statistics_rankings.cpp b/src/ui/statistics_rankings.cpp index ec4e52c..d6925cb 100644 --- a/src/ui/statistics_rankings.cpp +++ b/src/ui/statistics_rankings.cpp @@ -59,7 +59,7 @@ StatisticsRankingState statistics_rankings_from_game(const GameState& game, cons int manager_slot, int category, int first_season, const Image& underlay) { if (category < 0 || category >= int(kTitleCount)) throw FormatError("statistics rankings: category outside 0..8"); - if (first_season < 0 || first_season >= int(kSeasonCount) || (first_season % 4) != 0) + if (first_season < 0 || (first_season % 4) != 0) throw FormatError("statistics rankings: Source page must start at a four-season boundary"); if (!game.memory.has(kTitles + kTitleCount * kSeasonCount * 2 - 1)) throw FormatError("statistics rankings: save lacks the Original Titles title-holder block"); @@ -76,7 +76,14 @@ StatisticsRankingState statistics_rankings_from_game(const GameState& game, cons const uint32_t manager_year_at = uint32_t(manager_slot) * 0xb0c + 0x2aa; out.season_number_base = int(exe.u16(::bmh::layout::Achievements_FormatError_RuntimeField2)) + int(game.manager_records[manager_year_at] | (uint16_t(game.manager_records[manager_year_at + 1]) << 8)); - out.current_season = std::clamp(game.season, 0, int(kSeasonCount) - 1); + const auto history = title_history_rows(game); + if (history.empty()) throw FormatError("statistics rankings: missing title history"); + out.history_first_season = int(history.begin()->first); + out.current_season = game.season - out.history_first_season; + if (out.current_season < 0 || first_season > out.current_season) + throw FormatError("statistics rankings: history page outside known career"); + out.season_number_base += out.history_first_season - std::max(0, game.season - 49); + out.season_known.assign(size_t(out.current_season) + 1, false); out.underlay = underlay; for (uint32_t index = 0; index < kTitleCount; ++index) { const uint8_t title_id = exe.u8(::bmh::layout::StatisticsRankings_FormatError_RuntimeField + index); @@ -84,18 +91,23 @@ StatisticsRankingState statistics_rankings_from_game(const GameState& game, cons if (title_id >= kTitleCount) throw FormatError("statistics rankings: Source title id outside nine stored lists"); out.categories[index] = StatisticsRankingCategory{title_id, alternate}; } - for (uint32_t category_index = 0; category_index < kTitleCount; ++category_index) { - for (uint32_t season = 0; season < kSeasonCount; ++season) { - // StatisticsRankingsFromGame reads Titles by selected control index (local_14). - // title_id only chooses the Original label pointer above. - const uint32_t at = kTitles + category_index * 100 + season * 2; - out.title_holders[category_index][season] = game.memory.u16(at); - const uint16_t team = out.title_holders[category_index][season]; - if (team != 0xffff) { - if (team >= game.club_ids.size()) - throw FormatError("statistics rankings: Source title holder has no club identity/name"); - out.club_names[category_index][season] = game.club_name(team); - out.club_name_colors[category_index][season] = original_name_color(game, team); + for (size_t category = 0; category < 9; ++category) { + const auto count = size_t(out.current_season) + 1; + out.title_holders[category].assign(count, 0xffff); + out.club_names[category].resize(count); + out.club_name_colors[category].assign(count, 0x5f); + out.title_present[category].assign(count, false); + for (const auto& [season, names] : history) { + if (int(season) > game.season) throw FormatError("statistics rankings: future archived season"); + const auto index = size_t(int(season) - out.history_first_season); + out.season_known[index] = true; + out.club_names[category][index] = names[category]; + out.title_present[category][index] = !names[category].empty(); + const int native_index = int(season) - std::max(0, game.season - 49); + if (native_index >= 0 && native_index < 50) { + const auto team = game.memory.u16(kTitles + uint32_t(category) * 100 + uint32_t(native_index) * 2); + out.title_holders[category][index] = team; + if (team != 0xffff) out.club_name_colors[category][index] = original_name_color(game, team); } } } @@ -109,11 +121,16 @@ StatisticsRankingsScreen::StatisticsRankingsScreen(const std::filesystem::path& button_art_(load_vga(asset_path(data / "KB", "0.VGA"))), texts_(load_textd(asset_path(data, "TEXTD.BLK"))) { if (state_.category < 0 || state_.category >= int(kTitleCount) || state_.current_season < 0 || - state_.current_season >= int(kSeasonCount) || state_.first_season < 0 || - state_.first_season >= int(kSeasonCount) || state_.first_season % 4 != 0 || + state_.first_season < 0 || state_.first_season > state_.current_season || state_.first_season % 4 != 0 || state_.underlay.width != 320 || state_.underlay.height != 240 || button_art_.width < 76 || button_art_.height < 84) throw FormatError("statistics rankings: incomplete Source projection"); + for (size_t i = 0; i < 9; ++i) + if (state_.club_names[i].size() != size_t(state_.current_season) + 1 || + state_.club_name_colors[i].size() != state_.club_names[i].size() || + state_.title_present[i].size() != state_.club_names[i].size() || + state_.season_known.size() != state_.club_names[i].size()) + throw FormatError("statistics rankings: incomplete archived rows"); for (size_t i = 0; i < state_.categories.size(); ++i) { const auto& category = state_.categories[i]; if (category.title_id >= kTitleCount) @@ -169,21 +186,22 @@ Image StatisticsRankingsScreen::render() const { for (int season = state_.first_season; season <= end; ++season) { const int visible = season - state_.first_season; const int y = 0x31 + visible * 6; - const uint16_t team = state_.title_holders[size_t(state_.category)][size_t(season)]; - if (season < state_.current_season || team != 0xffff) { + const bool has_title = state_.title_present[size_t(state_.category)][size_t(season)]; + if (season < state_.current_season || has_title) { const int source_season_number = state_.season_number_base + season + 1; std::string n = std::to_string(source_season_number); while (n.size() < 4) n.insert(n.begin(), '%'); n += "%%"; // FormatNumber(...,0xc4,&StatisticsRankingsRuntimeField): width-four Source number and suffix. - const uint8_t year_color = team == 0xffff && + const uint8_t year_color = !has_title && (state_.category == 5 || state_.category == 6) ? 0x67 : 0x6b; font_.draw(out, 100, y - font_.baseline, n, year_color); - if (team != 0xffff) { + if (has_title) { font_.draw(out, 130, y - font_.baseline, state_.club_names[size_t(state_.category)][size_t(season)], state_.club_name_colors[size_t(state_.category)][size_t(season)]); } else { - font_.draw(out, 130, y - font_.baseline, empty_label_, 0x67); + font_.draw(out, 130, y - font_.baseline, + state_.season_known[size_t(season)] ? empty_label_ : "Unbekannt", 0x67); } } } diff --git a/src/ui/statistics_rankings.hpp b/src/ui/statistics_rankings.hpp index 2af0b5e..e500ec5 100644 --- a/src/ui/statistics_rankings.hpp +++ b/src/ui/statistics_rankings.hpp @@ -27,9 +27,12 @@ struct StatisticsRankingCategory { struct StatisticsRankingState { std::array categories{}; - std::array, 9> title_holders{}; - std::array, 9> club_names{}; - std::array, 9> club_name_colors{}; + std::array, 9> title_holders{}; + std::array, 9> club_names{}; + std::array, 9> club_name_colors{}; + std::array, 9> title_present{}; + std::vector season_known; + int history_first_season = 0; int current_season = 0; // SeasonIndex: latest history index; current empty slot is retained. int manager_slot = -1; int season_number_base = 0; // AchievementsRuntimeField[manager] + Achievements_FormatError_RuntimeField2; StatisticsRankingsFromGame adds one. diff --git a/src/ui/statistics_rankings_tests.cpp b/src/ui/statistics_rankings_tests.cpp index f38b489..5d481fa 100644 --- a/src/ui/statistics_rankings_tests.cpp +++ b/src/ui/statistics_rankings_tests.cpp @@ -97,6 +97,28 @@ int main(int argc, char** argv) { screen.pending()->kind == StatisticsRankingsRequestKind::close, "right-click is the Source parent-return request"); + auto career = sample(); career.season = 300; + career.manager_records[0x2aa] = uint8_t(258); career.manager_records[0x2ab] = uint8_t(258 >> 8); + for (uint32_t season = 0; season <= 300; ++season) { + std::array titles; titles[0] = "Bayer"; career.title_history.emplace(season, titles); + } + auto wide = statistics_rankings_from_game(career, exe, 0, 0, 0, underlay); + check(wide.current_season == 300 && wide.history_first_season == 0 && wide.club_names[0].size() == 301 && + wide.club_names[0][0] == "Bayer" && wide.club_names[0][300] == "Bayer", + "archived title lists retain all 301 known seasons and original winner snapshots"); + check(wide.season_number_base == int(exe.u16(::bmh::layout::Achievements_FormatError_RuntimeField2)) + 7, + "absolute archive year labels compensate for the native 50-row window shift"); + StatisticsRankingsScreen long_screen(data, wide); + for (int n = 0; n < 100; ++n) long_screen.scroll(1); + check(long_screen.state().first_season == 288 && long_screen.render().width == 320, + "history paging reaches season 300 instead of stopping at the fiftieth row"); + for (int n = 0; n < 100; ++n) long_screen.scroll(-1); + check(long_screen.state().first_season == 0, "oldest archived title rows remain reachable"); + auto legacy = sample(); legacy.season = 300; + const auto imported = statistics_rankings_from_game(legacy, exe, 0, 0, 0, underlay); + check(imported.history_first_season == 251 && imported.current_season == 49, + "a legacy late-career save exposes only its surviving 50 seasons, without invented earlier winners"); + GameState missing; bool rejected = false; try { (void)statistics_rankings_from_game(missing, exe, 0, 0, 0, underlay); } diff --git a/src/ui/transfer_market.cpp b/src/ui/transfer_market.cpp index 48c1f96..a57cc73 100644 --- a/src/ui/transfer_market.cpp +++ b/src/ui/transfer_market.cpp @@ -32,7 +32,7 @@ void copy(Image& out,const Image& source,int sx,int sy,int x,int y,int w,int h,i if(pixel!=key&&x+xx>=0&&x+xx=0&&y+yy>10&0xfff)==id)p.offer_name_color=true; } } + if (p.manager_board && p.player_id >= 0) p.statistics = player_club_statistics(g, state.runtime_club, p.player_id, *p.manager_board); state.roster.players.push_back(std::move(p)); } for(size_t index=0;indexappearances[0] : b[7],3)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5)+padded(p.statistics ? p.statistics->appearances[2] : b[9],3)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5); + if(full){Bytes raw(b.begin(),b.end());stats+=padded((p.statistics ? p.statistics->total : le16(raw,0x48)),3)+exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2)+padded((p.statistics ? p.statistics->goals : le16(raw,0x4a)),3)+exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2)+padded((p.statistics ? p.statistics->assists : le16(raw,0x4c)),3)+exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField);} } original_text(out,165,baseline,stats,0x6c); } diff --git a/src/ui/transfer_market_tests.cpp b/src/ui/transfer_market_tests.cpp index 5dc2f91..f40af27 100644 --- a/src/ui/transfer_market_tests.cpp +++ b/src/ui/transfer_market_tests.cpp @@ -100,6 +100,18 @@ int main(int argc,char** argv){ check(!transfer_market_from_game(expired,5).squad[0].already_listed, "cleared rejected/expired pool entry immediately makes the player listable again"); + auto wide_game=fixture(); + wide_game.player_statistics.emplace(uint32_t(1)<<12, PlayerClubStatistics{{256,0,256},65536,0,0}); + const auto wide_state=transfer_market_from_game(wide_game,4); + check(wide_state.roster.players[0].statistics&&wide_state.roster.players[0].statistics->appearances[0]==256&& + wide_state.roster.players[0].statistics->total==65536, + "roster projection owns widened counts instead of showing wrapped Native bytes"); + TransferMarketScreen wide_screen(argv[1],wide_state),native_screen(argv[1],s); + wide_screen.click(65,219);native_screen.click(65,219); + wide_screen.click(10,219);native_screen.click(10,219); + check(wide_screen.render().pixels!=native_screen.render().pixels, + "actual transfer roster renderer visibly retains the 256th appearance"); + auto all=s;all.items.clear();for(int n=0;n<40;++n){auto item=s.items[0];item.list_entry=n;item.source_slot=2928+n;item.player=100+n;item.name="P"+std::to_string(n);item.position=n%4;item.strength=60+n%7;all.items.push_back(item);} TransferMarketScreen pages(argv[1],all);const auto first=visible(pages);std::set reached; for(int page=0;page<7;++page){const auto rows=visible(pages);check(rows.size()==size_t(page==6?4:6),"six original rows per page with final four");reached.insert(rows.begin(),rows.end());pages.click(210,45,true);} diff --git a/tests/long_career_tests.cpp b/tests/long_career_tests.cpp new file mode 100644 index 0000000..7acd269 --- /dev/null +++ b/tests/long_career_tests.cpp @@ -0,0 +1,116 @@ +// SPDX-License-Identifier: GPL-3.0-only +#include +#include +#include +#include +#include "formats/executable_layout.hpp" +#include "game/long_career.hpp" +#include "game/matchday.hpp" +#include "game/national.hpp" +#include "game/save.hpp" +using namespace bmh; +namespace { +void check(bool yes, const char* why) { if (!yes) throw std::runtime_error(why); } +GameState fixture() { + GameState g; g.season = 0; g.manager_teams = {0}; + g.manager_records.resize(5 * 0xb0c); g.tactics.resize(5 * 0x840); + g.players = {{"Synthetic", 70u << 7 | 70u << 14 | 70u, 0, 6u << 21 | 1u}}; + g.player_bonus.resize(1); g.squad_slots.resize(3072, 0xfff); g.squad_slots[0] = 0; + g.club_ids = {0, 5}; g.club_names = {'C','l','u','b',0,'R','i','v','a','l',0}; + g.memory.add_block(layout::Titles, 900); + for (uint32_t n = 0; n < 900; ++n) g.memory.set8(layout::Titles + n, 0xff); + g.memory.add_block(layout::ManagerCount, 1); g.memory.set8(layout::ManagerCount, 1); + return g; +} +std::array board(const GameState& g) { + std::array b; std::copy_n(g.tactics.begin(), b.size(), b.begin()); return b; +} +void counters() { + auto g = fixture(); + g.tactics[7] = g.tactics[8] = g.tactics[9] = 255; + for (size_t n = 0x48; n <= 0x4d; ++n) g.tactics[n] = 255; + player_statistic(g, 0, 0, 0, 0, 0, 0); + player_statistic(g, 1, 0, 0, 0, 0, 0); + player_statistic(g, 2, 0, 0, 0, 0, 0); + player_statistic(g, 0, 0, 0, 0, 1, 0); + player_statistic(g, 0, 0, 0, 0, 2, 0); + const auto stats = player_club_statistics(g, 0, 0, board(g)); + check(stats.appearances == std::array{256,256,256}, "all cumulative byte counters survive 255 -> 256"); + check(stats.total == 65538 && stats.goals == 65536 && stats.assists == 65536, "word totals survive 65535 -> 65536"); + check(g.tactics[7] == 0 && g.tactics[8] == 0 && g.tactics[9] == 0 && g.tactics[0x48] == 2, + "native byte/word fields retain compatible modulo representation"); + const auto native = save_stream(g); const SaveState saved(native); + auto loaded = GameState::from_save(saved); + check(loaded.player_statistics == g.player_statistics && save_stream(loaded) == native, + "optional wide counters survive exact native-stream save/load/save roundtrip"); + const auto payload = encode_long_career(g); + auto plain = g; plain.player_statistics.clear(); + const auto original = save_stream(plain); + check(original.size() + payload.size() + 18 == native.size() && + std::equal(original.begin(), original.end(), native.begin()), "extension leaves every Original block and name pool byte unchanged"); + swap_statistics_clubs(g, 0, 1); + check(player_club_statistics(g, 1, 0, board(g)) == stats, "club promotion remapping retains the same player's counts"); + swap_statistics_clubs(g, 0, 1); + WatcomRandom random(713); + new_board_entry(g, 0, 0, 0, random); + check(g.player_statistics.empty() && player_club_statistics(g, 0, 0, board(g)).total == 0, + "replacement player never inherits the previous occupant's wide/native totals"); + auto exhausted = fixture(); + exhausted.player_statistics.emplace(0, PlayerClubStatistics{{255,0,0}, UINT64_MAX,0,0}); + const auto exhausted_before = save_stream(exhausted); bool refused = false; + try { player_statistic(exhausted, 0, 0, 0, 0, 0, 0); } catch (const FormatError&) { refused = true; } + check(refused && save_stream(exhausted) == exhausted_before, "uint64 exhaustion is explicit and inert, never a silent clamp"); + auto wrapped = fixture(); // Already lost high bits cannot be reconstructed from a legacy import. + player_statistic(wrapped, 0, 0, 0, 0, 0, 0); + check(player_club_statistics(wrapped, 0, 0, board(wrapped)).total == 1, "legacy zero is a known lower bound, never fabricated prior appearances"); +} +void history() { + auto g = fixture(); + for (int season = 0; season <= 300; ++season) { + if (season) { + capture_title_history(g); ++g.season; shift_legacy_title_history(g); + } + record_title_holder(g, 0, uint16_t(season % 2)); + record_title_holder(g, 1, uint16_t((season + 1) % 2)); + } + check(g.title_history.size() == 301 && g.title_history.at(0)[0] == "Club" && + g.title_history.at(49)[0] == "Rival" && g.title_history.at(50)[0] == "Club" && + g.title_history.at(300)[1] == "Rival", "title and cup winners remain available across seasons 49/50/300"); + check(g.memory.u16(layout::Titles) == 1 && g.memory.u16(layout::Titles + 98) == 0, + "Original block still contains exactly the latest 50 seasons"); + const auto bytes = save_stream(g); const SaveState saved(bytes); auto loaded = GameState::from_save(saved); + check(loaded.title_history == g.title_history && save_stream(loaded) == bytes, "301 seasons persist in the optional save extension"); + g.club_names[0] = 'X'; + check(g.title_history.at(0)[0] == "Club", "winner names are owned snapshots, independent of later renaming/promotion"); + auto imported = fixture(); imported.season = 300; imported.memory.set16(layout::Titles, 0); + capture_title_history(imported); + check(imported.title_history.size() == 50 && imported.title_history.begin()->first == 251 && + !imported.title_history.contains(0), "old late-career imports retain only known rows and do not fabricate lost history"); +} +void malformed() { + auto g = fixture(); record_title_holder(g, 0, 0); const auto original = encode_long_career(g); + for (size_t size = 0; size < original.size(); ++size) { + auto old = g; bool refused = false; + try { decode_long_career(old, Bytes(original.begin(), original.begin() + size)); } + catch (const FormatError&) { refused = true; } + check(refused && old.title_history == g.title_history, "truncated archive rejects atomically"); + } + auto version = original; version[0] = 2; bool refused = false; + try { decode_long_career(g, version); } catch (const FormatError&) { refused = true; } + check(refused, "future archive versions are explicit unsupported outcomes"); + auto future = g; future.title_history.emplace(1, std::array{}); + const auto future_bytes = save_stream(future); refused = false; + try { (void)GameState::from_save(SaveState(future_bytes)); } catch (const FormatError&) { refused = true; } + check(refused, "future season history cannot silently bind to a different saved calendar"); + auto base = g; base.title_history.clear(); auto duplicate = save_stream(base); + append_save_extension(duplicate, {{kLongCareerExtension,original},{kLongCareerExtension,original}}); + refused = false; + try { (void)GameState::from_save(SaveState(duplicate)); } catch (const FormatError&) { refused = true; } + check(refused, "duplicate archive containers reject rather than overwrite history"); +} +} +int main() try { + counters(); history(); malformed(); + std::cout << "PASS cumulative 256/65536 counters, 301 title seasons, legacy compatibility and archive roundtrip/validation\n"; + return 0; +} catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; }