diff --git a/CMakeLists.txt b/CMakeLists.txt index 8afcb26..82ced6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -271,6 +271,7 @@ add_library(bmhcore STATIC src/game/career.cpp src/game/save.cpp src/game/digest.cpp + src/game/world_layout.cpp src/game/turn.cpp src/game/turn_driver.cpp ) @@ -1187,6 +1188,11 @@ add_executable(classic_rules_unit_tests tests/classic_rules_unit_tests.cpp) target_link_libraries(classic_rules_unit_tests PRIVATE bmhcore) add_test(NAME classic_rules_unit_tests COMMAND classic_rules_unit_tests) +# The Classic world layout equals the numbers the engine used as literals (no game data needed). +add_executable(world_layout_tests tests/world_layout_tests.cpp) +target_link_libraries(world_layout_tests PRIVATE bmhcore) +add_test(NAME world_layout_tests COMMAND world_layout_tests) + add_executable(season_player_reset_tests tests/season_player_reset_tests.cpp) target_link_libraries(season_player_reset_tests PRIVATE bmhcore) add_test(NAME season_player_reset_tests COMMAND season_player_reset_tests) diff --git a/src/game/state.cpp b/src/game/state.cpp index 59046dc..de506ac 100644 --- a/src/game/state.cpp +++ b/src/game/state.cpp @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-3.0-only // GameState construction and accessors: building the state from a loaded save and the database // counts, manager of a team, club and player names, formation and formation positions. -// Squad table layout constants are defined here. Draws no random numbers. +// The squad table comes from the world layout (world_layout.hpp). Draws no random numbers. // Interface in state.hpp; every other file in this directory works on GameState. #include "formats/executable_layout.hpp" #include "game/state.hpp" @@ -10,16 +10,12 @@ #include #include "game/rating.hpp" +#include "game/world_layout.hpp" namespace bmh { namespace { -// Squad table layout constants of V2.24 (TeamsWithSquads, LargeSquadClubCount, NoTeamSquadStart). -constexpr int kBigSquadTeams = 90; -constexpr size_t kNoTeamSquadStart = 3072; -constexpr int kTeamsWithSquads = 168; - // C-style signed division by 8 as compiled by Watcom (rounds toward zero). int div8(int x) { return x / 8; } @@ -172,14 +168,18 @@ void GameState::formation_position(int team, int slot, int& x, int& y) const { } SquadRange squad_range(int team) { - if (team == kNoTeam) return {kNoTeamSquadStart, 24}; - if (team < kBigSquadTeams) return {size_t(team) * 24, 24}; - if (team < 138) return {size_t(team) * 16 + kBigSquadTeams * 8, 16}; + // The squad table of the world layout (world_layout.hpp). Classic: 24 slots for the 90 domestic clubs, 16 for + // the 48 international ones from slot 2160, 24 for kNoTeam at 3072. + const WorldLayout& w = world_layout(); + if (team == w.no_team()) return {w.pool_end, w.no_team_slots}; + // (a negative id is no team: the table arithmetic of the original gives a range far outside the slots) + if (team < 0) return {size_t(team) * 24, 24}; + if (team < w.league_teams) return {w.squad_start[size_t(team)], w.squad_size[size_t(team)]}; return {0, 0}; } TeamAverage team_average(const GameState& g, int team, uint16_t flags, WatcomRandom* rand) { - if (team >= kTeamsWithSquads && team != kNoTeam) { + if (team >= world_layout().first_foreign_cup() && team != world_layout().no_team()) { // TeamRating(team, profile 2 at layout field): a random value around the club's level if (!rand) throw std::invalid_argument("team_average: a team without squad needs the Watcom generator"); RatingContext ctx; diff --git a/src/game/world_layout.cpp b/src/game/world_layout.cpp new file mode 100644 index 0000000..15f14f3 --- /dev/null +++ b/src/game/world_layout.cpp @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-3.0-only +// WorldLayout: see world_layout.hpp. +#include "game/world_layout.hpp" + +#include +#include + +namespace bmh { + +namespace { + +WorldLayout& current() { + static WorldLayout layout = classic_world_layout(); + return layout; +} + +} // namespace + +WorldLayout classic_world_layout() { + // Squad table of the original (TeamsWithSquads, LargeSquadClubCount, NoTeamSquadStart): the 90 + // domestic clubs have 24 slots each, the 48 international clubs 16 each from slot 90 * 24. + WorldLayout w; + w.squad_start.resize(size_t(w.league_teams)); + w.squad_size.resize(size_t(w.league_teams)); + for (int team = 0; team < w.league_teams; ++team) { + const bool domestic = team < w.domestic_teams; + w.squad_start[size_t(team)] = domestic ? uint32_t(team) * 24 : uint32_t(team) * 16 + uint32_t(w.domestic_teams) * 8; + w.squad_size[size_t(team)] = domestic ? 24 : 16; + } + return w; +} + +const WorldLayout& world_layout() { return current(); } + +void set_world_layout(WorldLayout layout) { + const size_t teams = size_t(layout.league_teams); + if (layout.league_teams <= 0 || layout.domestic_teams < 0 || layout.domestic_teams > layout.league_teams || + layout.amateur_teams < 0 || layout.foreign_cup_teams < 0 || layout.national_teams < 1 || + layout.squad_start.size() != teams || layout.squad_size.size() != teams) + throw std::invalid_argument("world layout: inconsistent team ranges"); + // Squads lie back to back in id order before the pools; the pools and the squad of "no club" + // follow in their fixed order. + uint32_t next = 0; + for (size_t team = 0; team < teams; ++team) { + if (layout.squad_start[team] != next || layout.squad_size[team] == 0) + throw std::invalid_argument("world layout: squads must be consecutive"); + next += layout.squad_size[team]; + } + if (layout.transfer_list != next || layout.free_agents != layout.transfer_list + 44 || + layout.foreign_offers != layout.free_agents + 80 || layout.pool_end != layout.foreign_offers + 20 || + layout.total_slots < layout.pool_end + layout.no_team_slots) + throw std::invalid_argument("world layout: pools must follow the squads"); + // Squad slot values hold 12-bit player ids; loans store 10-bit club ids. + if (layout.total_teams() > 1023) throw std::invalid_argument("world layout: too many teams"); + current() = std::move(layout); +} + +} // namespace bmh diff --git a/src/game/world_layout.hpp b/src/game/world_layout.hpp new file mode 100644 index 0000000..c816485 --- /dev/null +++ b/src/game/world_layout.hpp @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: GPL-3.0-only +#pragma once +// The dimensions of a game world: team id ranges and the squad slot table. +// +// The original game fixes them (and so does every supported edition, German and English): +// ids 0..137 the 138 league clubs: 90 clubs of the five domestic leagues with 24 squad slots, +// 48 clubs of the four international leagues with 16 +// ids 138..167 30 amateur clubs (cup newcomers), without squads +// ids 168..356 189 foreign cup clubs without squads (a strength byte each) +// id 357 the pseudo team "no club" (kNoTeam, 0x165): national team / player pool, 24 slots +// ids 358..418 61 national teams +// and behind the squads the three player pools (transfer list, free agents, offers from abroad). +// +// Until 1.0.12 these numbers stood as literals all over the engine (138, 168, 0x165, 90 * 24, ...). +// WorldLayout names them in one place. OpenBMH Classic never installs another layout: +// classic_world_layout() returns exactly the original numbers, so every function that asks the +// layout computes what it computed before. A later edition with more leagues (OpenBMH Extended) +// installs its own layout instead of patching literals. +// +// One game per process, like the league tables of schedule.hpp. + +#include +#include +#include + +namespace bmh { + +struct WorldLayout { + // Team id ranges, in this order: league clubs, amateur clubs, squadless foreign cup clubs, + // the pseudo team "no club", national teams. + int league_teams = 138; // ids 0 .. league_teams-1 have squads + int domestic_teams = 90; // the first league clubs (the five domestic leagues) + int amateur_teams = 30; // ids league_teams .. +amateur_teams-1, no squads + int foreign_cup_teams = 189; // squadless foreign clubs (27 nations x 7) + int national_teams = 62; // "no club" (the first of them) and the 61 national teams + + // Squad slot table: first slot and slot count per league club, then the pools. + std::vector squad_start; // per league club + std::vector squad_size; // per league club + uint32_t transfer_list = 0xB70; // 44 slots: players on the transfer list + uint32_t free_agents = 0xB9C; // 80 slots: players without a club + uint32_t foreign_offers = 0xBEC; // 20 slots: players offered from abroad + uint32_t pool_end = 0xC00; // end of the pools = first slot of "no club" + uint32_t no_team_slots = 24; // the squad of "no club" + uint32_t total_slots = 3150; // including the reserve behind "no club" + + int first_amateur() const { return league_teams; } + // The first team without a squad of its own that is a foreign cup club (Classic: 168); the + // clubs before it are "the clubs with league records" (league clubs + amateurs). + int first_foreign_cup() const { return league_teams + amateur_teams; } + int no_team() const { return first_foreign_cup() + foreign_cup_teams; } + int total_teams() const { return no_team() + national_teams; } + bool has_squad(int team) const { return (team >= 0 && team < league_teams) || team == no_team(); } +}; + +// The original dimensions (V2.24 and the supported German and English editions). +WorldLayout classic_world_layout(); + +// The layout of the running game (Classic unless another one was installed). +const WorldLayout& world_layout(); + +// Installs a layout; throws std::invalid_argument when its ranges or its slot table are inconsistent. +void set_world_layout(WorldLayout layout); + +} // namespace bmh diff --git a/tests/world_layout_tests.cpp b/tests/world_layout_tests.cpp new file mode 100644 index 0000000..20466fe --- /dev/null +++ b/tests/world_layout_tests.cpp @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-3.0-only +// The Classic world layout must give exactly the numbers that stood as literals in the engine until +// 1.0.12, and squad_range() must return what its former table arithmetic returned for every team id. +// A layout with inconsistent ranges must be refused. Needs no game data. +#include +#include +#include + +#include "game/state.hpp" +#include "game/world_layout.hpp" + +using namespace bmh; + +namespace { +void check(bool value, const char* message) { + if (!value) throw std::runtime_error(message); +} + +// squad_range as it was written before the world layout existed (state.cpp up to 1.0.12). +SquadRange former_squad_range(int team) { + if (team == 0x165) return {3072, 24}; + if (team < 90) return {size_t(team) * 24, 24}; + if (team < 138) return {size_t(team) * 16 + 90 * 8, 16}; + return {0, 0}; +} +} // namespace + +int main() { + try { + const WorldLayout w = classic_world_layout(); + check(w.league_teams == 138 && w.domestic_teams == 90 && w.amateur_teams == 30 && w.foreign_cup_teams == 189 && + w.national_teams == 62, "Classic team ranges are not the original ones"); + check(w.first_amateur() == 138 && w.first_foreign_cup() == 168 && w.no_team() == 0x165 && w.no_team() == kNoTeam && + w.total_teams() == 419, "derived team ids differ from 138 / 168 / 0x165 / 419"); + check(w.transfer_list == 90 * 24 + 48 * 16 && w.transfer_list == 0xB70 && w.free_agents == 0xB9C && + w.foreign_offers == 0xBEC && w.pool_end == 0xC00 && w.pool_end == 3072 && w.no_team_slots == 24 && + w.total_slots == 3096 + 54, "pool offsets differ from the original slot table"); + check(w.has_squad(0) && w.has_squad(137) && !w.has_squad(138) && !w.has_squad(167) && !w.has_squad(168) && + w.has_squad(0x165) && !w.has_squad(0x166) && !w.has_squad(-1), "has_squad differs"); + // the running game uses the Classic layout unless another one is installed + check(world_layout().league_teams == 138 && world_layout().squad_start.size() == 138, "default layout is not Classic"); + for (int team = -3; team < 450; ++team) { + const SquadRange now = squad_range(team), then = former_squad_range(team); + if (now.first != then.first || now.count != then.count) + throw std::runtime_error("squad_range differs from the former table for team " + std::to_string(team)); + } + // inconsistent layouts are refused and leave the installed one alone + const auto refused = [](WorldLayout layout) { + try { set_world_layout(std::move(layout)); } catch (const std::invalid_argument&) { return true; } + return false; + }; + WorldLayout bad = classic_world_layout(); bad.squad_size[5] = 20; // squads no longer back to back + check(refused(bad), "a gap in the squad table was accepted"); + bad = classic_world_layout(); bad.squad_start.pop_back(); // table shorter than the club count + check(refused(bad), "a short squad table was accepted"); + bad = classic_world_layout(); bad.transfer_list += 16; // pools not behind the squads + check(refused(bad), "pools away from the squads were accepted"); + bad = classic_world_layout(); bad.foreign_cup_teams = 900; // more teams than a loan can name + check(refused(bad), "more than 1023 teams were accepted"); + check(world_layout().league_teams == 138 && squad_range(137).first == 137 * 16 + 720, "a refused layout changed the game"); + // a consistent layout is accepted (here: the Classic one again) + set_world_layout(classic_world_layout()); + check(squad_range(89).first == 89 * 24 && squad_range(90).first == 2160 && squad_range(0x165).first == 3072, + "installing the Classic layout changed the squad table"); + std::printf("world layout: Classic numbers, squad table and validation passed\n"); + return 0; + } catch (const std::exception& e) { + std::fprintf(stderr, "%s\n", e.what()); + return 1; + } +}