World layout: team id ranges and the squad slot table as data
WorldLayout names the dimensions of a game world (league clubs, amateurs, foreign cup clubs, the pseudo team, national teams, squad slots and pools). classic_world_layout() holds the numbers of the original; squad_range() and team_average() ask it instead of literals. A test compares squad_range() with the former arithmetic for every team id and checks that inconsistent layouts are refused. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
0bcb0d2507
commit
74d6cde914
5 files changed
+210
-10
No files matched your search
+10
-10
@@ -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 <stdexcept>
|
||||
|
||||
#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;
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// WorldLayout: see world_layout.hpp.
|
||||
#include "game/world_layout.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <utility>
|
||||
|
||||
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
|
||||
@@ -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 <cstddef>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
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<uint32_t> squad_start; // per league club
|
||||
std::vector<uint8_t> 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
|
||||
Reference in new issue
Block a user