(by Codex) Preserve long-career player counts and title history
This commit is contained in:
1 parent
8e2f491d5e
commit
30843fc9f3
23 files changed
+491
-47
No files matched your search
+2
-2
@@ -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<int>(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<int>(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) {
|
||||
|
||||
@@ -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 <algorithm>
|
||||
#include <limits>
|
||||
#include <stdexcept>
|
||||
|
||||
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<uint8_t, 0x58>& 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<uint64_t>::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<uint8_t, 0x58>& 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<uint8_t, 0x58> 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<std::string, 9> 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<std::string, 9> 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
#pragma once
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include "formats/binary.hpp"
|
||||
|
||||
namespace bmh {
|
||||
struct GameState;
|
||||
struct PlayerClubStatistics {
|
||||
std::array<uint64_t, 3> 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<uint32_t, PlayerClubStatistics>;
|
||||
using TitleHistoryArchive = std::map<uint32_t, std::array<std::string, 9>>;
|
||||
PlayerClubStatistics player_club_statistics(const GameState&, int team, int player,
|
||||
const std::array<uint8_t, 0x58>& 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&);
|
||||
}
|
||||
@@ -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<int>(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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
+3
-4
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<uint16_t> squad_slots; // *SquadSlots, all squads back to back (see squad_range)
|
||||
std::vector<Player> players; // *PlayerConditionTechniqueForm / *PlayerSkillWords / *PlayerIdentityWords, names not filled
|
||||
std::vector<uint8_t> team_morale; // *TeamMorale, per team: bits 0-5 morale / 2, bits 6-7 flags
|
||||
|
||||
Reference in new issue
Block a user