Files
Dreierpack-Manager/src/modern/game/national.cpp
T

309 lines
15 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
#include "formats/executable_layout.hpp"
#include "modern/game/modern_state.hpp"
#include "modern/game/national.hpp"
#include <algorithm>
#include <array>
#include <cstring>
#include <vector>
#include "game/lineup.hpp"
#include "game/match_engine.hpp"
#include "modern/game/month.hpp"
#include "modern/game/weekly.hpp"
namespace bmh::modern {
namespace {
constexpr size_t kManagerRecord = 0xb0c;
constexpr size_t kBoard = 0x840; // one manager's tactics board (24 x 0x58)
constexpr size_t kBoardSlot = 0x58;
constexpr uint32_t kDesktops = ::bmh::layout::Desktops; // desktop layouts, 0x360 per manager slot
constexpr size_t kDesktop = 0x360;
constexpr uint8_t kNominated[4] = {3, 5, 7, 5}; // NationalRuntimeField: keepers, defence, midfield, attack
constexpr int kMinNationalSquad = 14; // Modern: fewer nominees keep a national team on its drawn rating
// the manager's team (record +0x290) as the port caches it
void refresh_manager_teams(ModernState& g) {
for (size_t m = 0; m < g.manager_teams.size(); m++) {
const size_t at = m * kManagerRecord + 0x290;
g.manager_teams[m] = int16_t(g.manager_records.at(at) | g.manager_records.at(at + 1) << 8);
}
}
// a byte of the original's static data, created on first use (not every one is saved)
uint8_t& static_byte(ModernState& g, uint32_t address) {
if (!g.memory.has(address)) g.memory.add_block(address, 1);
return g.memory.at(address);
}
} // namespace
void swap_manager_slots(ModernState& g, int a, int b) {
if (a == b) return;
std::swap_ranges(g.manager_records.begin() + long(size_t(a) * kManagerRecord),
g.manager_records.begin() + long(size_t(a + 1) * kManagerRecord),
g.manager_records.begin() + long(size_t(b) * kManagerRecord));
std::swap_ranges(g.tactics.begin() + long(size_t(a) * kBoard), g.tactics.begin() + long(size_t(a + 1) * kBoard),
g.tactics.begin() + long(size_t(b) * kBoard));
if (g.memory.has(kDesktops))
for (size_t i = 0; i < kDesktop; i++) {
const uint32_t pa = kDesktops + uint32_t(size_t(a) * kDesktop + i);
const uint32_t pb = kDesktops + uint32_t(size_t(b) * kDesktop + i);
const uint8_t t = g.memory.u8(pa);
g.memory.set8(pa, g.memory.u8(pb));
g.memory.set8(pb, t);
}
refresh_manager_teams(g);
}
int add_to_squad(ModernState& g, int manager, int from_slot, int squad_start, WatcomRandom& rand) {
// FirstFreeSlotSquad(start, 0x18): the first free slot of the squad
int slot = -1;
for (int s = squad_start; s < squad_start + 24; s++)
if (g.squad_slots.at(size_t(s)) == kEmptySlot) {
slot = s;
break;
}
if (slot < 0) return -1; // (the original shows the message PendingOffer_FormatError_ExecutableField)
g.squad_slots.at(size_t(slot)) = g.squad_slots.at(size_t(from_slot));
modern::new_board_entry(g, manager, slot - squad_start, slot, rand);
return slot;
}
void new_board_entry(ModernState& g, int manager, int index, int slot, WatcomRandom& rand) {
uint8_t* e = &g.tactics.at(size_t(manager) * kBoard + size_t(index) * kBoardSlot);
std::memset(e + 7, 0, 3);
std::memset(e + 0x19, 0, 0x24);
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;
e[0x46] = 0xff, e[0x47] = 0x0f; // u16 0xfff
e[0x44] = 0xff, e[0x45] = 0x0f;
e[0] = 10;
e[1] = 4;
e[0xe] = 0x59;
e[0xf] = uint8_t(rand.range(0x28, 0x3c));
e[0x11] = 0;
e[0x43] = 0;
e[0x4e] = 0, e[0x4f] = 0;
e[0x3d] = index < 11 ? 0 : index <= 0x10 ? 1 : 2;
e[0xc] = 0x18;
// AddToSquad(slot, 0, W(95, 105), &fee, &salary (+0x50), 2 years, no extra)
const int percent = rand.range(0x5f, 0x69);
uint32_t fee = 0, salary = 0;
modern::market_value(g, slot, 0, percent, fee, salary, 2, 0, rand);
for (int k = 0; k < 4; k++) e[0x50 + k] = uint8_t(salary >> (8 * k));
std::memset(e + 0x12, 0, 7);
}
void nominate_national_squads(ModernState& g) {
const WorldLayout& w = world_layout();
if (w.national_slots == 0) return;
// the strongest fit players of each nationality per position, the quotas and the strength of
// the German nomination (without its bonus for clubs abroad: every nation plays abroad)
const int nations = w.national_teams;
struct Picks {
int16_t best[4][7] = {}, slot[4][7] = {}; // ascending per position, as for Germany
int found[4] = {}; // nominees per position (up to the quota)
};
std::vector<Picks> picks(static_cast<size_t>(nations));
for (size_t s = 0; s < transfer_list_slot(); s++) {
const int id = g.squad_slots.at(s) & 0xfff;
if (id == 0xfff) continue;
const int n = modern::nationality(g, id);
if (n < 1 || n >= nations) continue; // 0: Germany, nominated above
const Player& p = g.players.at(size_t(id));
const bool fit = (p.w35c >> 27) == 0 || ((p.w364 >> 16) & 0x1f) == 0;
if (!fit) continue;
const int pos = int(p.w364 & 3);
int16_t strength = int16_t(p.strength(false));
if (pos > 1) strength = int16_t(strength + ((p.w364 >> 2) & 0x3f) + (((p.w368 >> 18) & 0x3f) >> 1));
Picks& pick = picks[size_t(n)];
int16_t* row = pick.best[pos];
int16_t* at = pick.slot[pos];
int i = 0;
while (i < kNominated[pos] && strength > row[i]) i++;
if (i == 0) continue;
for (int k = 0; k < i - 1; k++) row[k] = row[k + 1], at[k] = at[k + 1];
row[i - 1] = strength;
at[i - 1] = int16_t(s);
pick.found[pos] = std::min<int>(pick.found[pos] + 1, kNominated[pos]);
}
for (int n = 1; n < nations; n++) {
const int team = no_team() + n;
const SquadRange range = squad_range(team);
for (size_t s = range.first; s < range.first + range.count; s++) g.squad_slots.at(s) = kEmptySlot;
// A nation needs a goalkeeper and 14 players; with fewer it keeps its drawn rating.
const int* f = picks[size_t(n)].found;
if (f[0] == 0 || f[0] + f[1] + f[2] + f[3] < kMinNationalSquad) continue;
size_t next = range.first;
for (int pos = 0; pos < 4; pos++)
for (int k = kNominated[pos] - f[pos]; k < kNominated[pos]; k++) // the found ones fill the top
g.squad_slots.at(next++) = uint16_t(g.squad_slots.at(size_t(picks[size_t(n)].slot[pos][k])) & 0x0fff);
auto_lineup(g, team, 100);
}
}
bool national_team_day(ModernState& g, WatcomRandom& rand, MinuteReport& report) {
StaticMemory& mem = g.memory;
const SquadRange national = squad_range(no_team());
// NationalSquadEmptied: the national squad is emptied
for (size_t s = national.first; s < national.first + national.count; s++) g.squad_slots.at(s) = kEmptySlot;
// SwapManagerSlots2 (called whether or not a take-over is already in effect): a manager flagged
// as Bundestrainer takes the national team over in slot 0. ActiveManager is what the day
// step's manager loop (DayStepRest) leaves: the number of managers.
(void)mem;
(void)report;
modern::national_coach_take_over(g, int(g.manager_teams.size()), uint16_t(g.date - g.season_start));
const uint8_t coach = static_byte(g, ::bmh::layout::CoachActive); // CoachActive
if (coach == 0) modern::swap_manager_slots(g, 4, 0); // slot 0 becomes the spare slot 4
// the strongest available German players per position (NationalRuntimeField of each), kept in
// ascending order: a stronger one pushes the weakest out
int16_t best[4][7] = {}, slots[4][7] = {};
for (size_t s = 0; s < transfer_list_slot(); s++) {
const int id = g.squad_slots.at(s) & 0xfff;
if (id == 0xfff) continue;
const Player& p = g.players.at(size_t(id));
const bool fit = (p.w35c >> 27) == 0 || ((p.w364 >> 16) & 0x1f) == 0;
if (!fit || modern::nationality(g, id) != 0) continue;
const int pos = int(p.w364 & 3);
int16_t strength = int16_t(p.strength(false)); // Strength(w35c, 0)
if (pos > 1) strength = int16_t(strength + ((p.w364 >> 2) & 0x3f) + (((p.w368 >> 18) & 0x3f) >> 1));
if (s > 0x86f) strength = int16_t((strength * 0xb) / 10); // (the clubs from slot 0x870 on)
int i = 0;
// NationalTeamDay caps the insertion index at the positional quota. Guard
// before indexing: the full seven-player row has no C++ eighth element.
while (i < kNominated[pos] && strength > best[pos][i]) i++;
if (i == 0) continue;
const int j = i - 1;
// Int16T: entries 1..j move down one place, the new one takes place j
for (int k = 0; k < j; k++) best[pos][k] = best[pos][k + 1], slots[pos][k] = slots[pos][k + 1];
best[pos][j] = strength;
slots[pos][j] = int16_t(s);
}
// PendingOfferAnswer for each, weakest first, into the board of slot 0
for (int pos = 0; pos < 4; pos++)
for (int k = 0; k < kNominated[pos]; k++) modern::add_to_squad(g, 0, slots[pos][k], int(national.first), rand);
// Int2(0x165, 0xe4) with manager 0's option byte (ManagerAutomation) at 1
const uint8_t option = g.manager_options.at(0);
g.manager_options.at(0) = 1;
try {
auto_lineup(g, no_team(), 0xe4);
} catch (const std::exception& ex) {
report.fail(std::string("national team line-up: ") + ex.what());
}
nominate_national_squads(g); // Modern: the other nations' squads (none in a Classic layout)
if (coach == 0) modern::swap_manager_slots(g, 4, 0);
g.manager_options.at(0) = option;
return coach != 0; // (with a Bundestrainer his desk follows: AdvanceTurn's local_28 = 4)
}
// ---------------------------------------------------------------------------------------
// The Bundestrainer (national coach). A manager flagged in his record (+0x21c), every
// manager in the world game without leagues (CompetitionSelection == 2) and the only manager of a
// one-manager game coaches the national team on international days: SwapManagerSlots2 moves his
// slot 0 to the spare slot 4 and puts the national team's record (kept in slot 4 between the
// international periods) into slot 0; NationalCoachHandBack swaps them back. CoachActive tells
// whether the swap is in effect, OriginalManagerCount keeps the number of managers meanwhile.
namespace {
void set_manager_count(ModernState& g, int managers) {
static_byte(g, ::bmh::layout::ManagerCount) = uint8_t(managers); // ManagerCount
g.manager_teams.assign(size_t(managers), 0); // (the typed copy, one entry per manager)
}
} // namespace
int ModernState::human_manager_slot(int runtime_slot) const {
if (runtime_slot < 0 || !memory.has(::bmh::layout::CoachActive) || memory.u8(::bmh::layout::CoachActive) == 0) return runtime_slot;
if (runtime_slot == 4) return 0;
if (runtime_slot != 0) return runtime_slot;
const int count = memory.u8(::bmh::layout::OriginalManagerCount);
if (count == 1 || memory.u32(::bmh::layout::CompetitionSelection) == 2) return 0;
// The source chose the first eligible original record. While coaching,
// record four preserves original human zero; the other records are frozen.
// Read those flags, rather than guessing from a possibly duplicated name.
for (int human = 0; human < count && human < 4; ++human) {
const size_t record = size_t(human == 0 ? 4 : human) * kManagerRecord;
if (record + 0x21c < manager_records.size() && manager_records[record + 0x21c] != 0) return human;
}
throw FormatError("national coach identity is unavailable");
}
int ModernState::manager_record_slot(int human_slot) const {
if (!memory.has(::bmh::layout::CoachActive) || memory.u8(::bmh::layout::CoachActive) == 0) return human_slot;
const int coach = human_manager_slot(0);
if (human_slot == coach) return 0;
return human_slot == 0 ? 4 : human_slot;
}
void national_coach_take_over(ModernState& g, int active_manager, uint16_t source_season_day) {
StaticMemory& mem = g.memory;
int managers = int(g.manager_teams.size());
static_byte(g, ::bmh::layout::OriginalManagerCount) = uint8_t(managers); // OriginalManagerCount = ManagerCount
static_byte(g, ::bmh::layout::ClubPlayerDetail_InvalidArgument_RuntimeField3) = uint8_t(active_manager); // ClubPlayerDetail_InvalidArgument_RuntimeField3 = ActiveManager
// DaysIntoSeason == 1 (the first day after the saved season origin): the national team's
// mood byte loses its top bit before coach eligibility is checked.
const bool first_day = source_season_day == 1;
if (first_day) g.team_morale.at(no_team()) &= 0x7f;
const bool world_only = mem.u32(::bmh::layout::CompetitionSelection) == 2;
// (the loop bound is ManagerCount itself, which a take-over sets to 1)
for (int m = 0; m < managers; m++) {
uint8_t* rec = &g.manager_records.at(size_t(m) * kManagerRecord);
if (rec[0x21c] == 0 && !world_only && managers != 1) continue;
// the national team's record (slot 4) gets the coach's portrait (+0x221) and name
// (+0xa8, StrcpySoHallReplacesItsCurrentTournament = strcpy)
uint8_t* spare = &g.manager_records.at(4 * kManagerRecord);
spare[0x221] = rec[0x221];
for (size_t i = 0;; i++) {
spare[0xa8 + i] = rec[0xa8 + i];
if (rec[0xa8 + i] == 0) break;
}
managers = 1;
set_manager_count(g, 1);
modern::swap_manager_slots(g, 4, 0); // SwapManagerSlots(4, 0)
uint8_t* coach = &g.manager_records.at(0);
coach[0x290] = uint8_t(no_team() & 0xff), coach[0x291] = uint8_t(no_team() >> 8); // CupDraw_FormatError_RuntimeField2 = 0x165
coach[0x218] = g.manager_records.at(4 * kManagerRecord + 0x218); // NationalRuntimeField2 = NationalRuntimeField3
g.manager_teams.at(0) = no_team();
auto_lineup(g, no_team(), 0xe4); // Int2(0x165, 0xe4)
static_byte(g, ::bmh::layout::CoachActive) = 1;
if (first_day) g.team_morale.at(no_team()) |= 0x80;
}
}
void national_coach_hand_back(ModernState& g) {
if (static_byte(g, ::bmh::layout::CoachActive) == 0) return;
const int managers = static_byte(g, ::bmh::layout::OriginalManagerCount);
set_manager_count(g, managers); // ManagerCount = OriginalManagerCount
modern::swap_manager_slots(g, 4, 0); // SwapManagerSlots(4, 0) (refreshes the typed teams)
static_byte(g, ::bmh::layout::CoachActive) = 0;
}
bool national_coach_active(const ModernState& g) { return !g.manager_teams.empty() && g.manager_teams[0] == no_team(); }
void national_coach_after_load(ModernState& g) {
// NationalCoachAfterLoad's end, loading as well as saving: the take-over is done again
if (g.memory.has(::bmh::layout::CoachActive) && g.memory.u8(::bmh::layout::CoachActive) != 0)
modern::national_coach_take_over(g, g.memory.has(::bmh::layout::ActiveManager) ? g.memory.u8(::bmh::layout::ActiveManager) : 0,
uint16_t(g.date - g.season_start));
}
ModernState national_coach_save_form(const ModernState& g) {
// NationalCoachAfterLoad's start: the slots swapped back, CoachActive kept at 1
ModernState s = g;
if (s.memory.has(::bmh::layout::CoachActive) && s.memory.u8(::bmh::layout::CoachActive) != 0) {
modern::national_coach_hand_back(s);
s.memory.set8(::bmh::layout::CoachActive, 1);
}
return s;
}
} // namespace bmh::modern