Files
Dreierpack-Manager/src/ui/contracts.cpp
T
ClaudeandClaude Sonnet 5.5 551d564efb More buttons without a lasting state look pressed while held
Advertising offers and notices, the player card, the training camp booking and
notice, the file dialog (page button, overwrite question) and the bank notices
and credit fee question now draw the pressed look from the mouse press until
the release. Manager notices share source_dialog_button_at for their OK/Ja/Nein.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-08 18:36:48 +01:00

909 lines
60 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
// Implements the contracts screen declared in contracts.hpp: contracts_from_game() builds the player rows
// and premiums from the game, and ContractsScreen renders list, negotiation and insurance views and converts
// input into ContractRequest values. Applying an offer or insurance is left to the session.
#include "formats/executable_layout.hpp"
#include "ui/contracts.hpp"
#include "ui/button_frame.hpp"
#include "ui/game_end.hpp"
#include <algorithm>
#include <limits>
#include <sstream>
#include "game/state.hpp"
#include "ui/manager.hpp"
namespace bmh::ui {
namespace {
// Record sizes: one manager record, the per-manager block of player board entries (24 slots)
// and one board entry (per-player data such as contract, insurance and premiums).
constexpr size_t kManagerRecord = 0xb0c;
constexpr size_t kBoard = 0x840;
constexpr size_t kBoardSlot = 0x58;
// Copies a block from a widget/atlas image to the target; throws on out-of-range rectangles.
// With `transparent` set, palette index 0 is skipped.
void copy_region(Image& target, const Image& source, int sx, int sy, int x, int y, int width, int height, bool transparent = false) {
if (sx < 0 || sy < 0 || x < 0 || y < 0 || width < 0 || height < 0 ||
sx + width > source.width || sy + height > source.height || x + width > target.width || y + height > target.height)
throw FormatError("contracts: invalid original widget bounds");
for (int yy = 0; yy < height; ++yy) for (int xx = 0; xx < width; ++xx) {
const uint8_t value = source.pixels[size_t(sy + yy) * size_t(source.width) + size_t(sx + xx)];
if (!transparent || value != 0) target.at(x + xx, y + yy) = value;
}
}
// Forward declaration; fill() is defined below and is used by frame().
void fill(Image& target, int x, int y, int width, int height, uint8_t color);
// Tiles the 16x16 background pattern stored in the executable over a rectangle.
void pattern(Image& out, const LeImage& exe, int x, int y, int width, int height) {
for (int yy = 0; yy < height; ++yy)
for (int xx = 0; xx < width; ++xx)
out.at(x + xx, y + yy) = exe.u8(::bmh::layout::Advertising_PatternRect_RuntimeField + size_t(yy % 16) * 16 + size_t(xx % 16));
}
// Draws a panel in the original style: optional drop shadow, tiled pattern inside, four 4x4 corners
// and edge strips (copied in pieces of at most 74 pixels horizontally and 96 vertically).
void frame(Image& out, const LeImage& exe, const Image& widgets, int x, int y, int width, int height, bool shadow = true) {
if (shadow) { fill(out, x + 2, y + height, width, 2, 0x40); fill(out, x + width, y + 2, 2, height, 0x40); }
pattern(out, exe, x + 4, y + 4, width - 7, height - 7);
copy_region(out, widgets, 0, 0, x, y, 4, 4);
copy_region(out, widgets, 4, 0, x + width - 4, y, 4, 4);
copy_region(out, widgets, 0, 4, x, y + height - 4, 4, 4);
copy_region(out, widgets, 4, 4, x + width - 4, y + height - 4, 4, 4);
for (int at = 4; at < width - 4; at += 74) {
const int n = std::min(74, width - 4 - at);
copy_region(out, widgets, 0, 104, x + at, y, n, 4);
copy_region(out, widgets, 0, 108, x + at, y + height - 4, n, 4);
}
for (int at = 4; at < height - 4; at += 96) {
const int n = std::min(96, height - 4 - at);
copy_region(out, widgets, 0, 8, x, y + at, 4, n);
copy_region(out, widgets, 4, 8, x + width - 4, y + at, 4, n);
}
}
// Fills a rectangle with one palette index; throws if it leaves the image.
void fill(Image& target, int x, int y, int width, int height, uint8_t color) {
if (x < 0 || y < 0 || width < 0 || height < 0 || x + width > target.width || y + height > target.height)
throw FormatError("contracts: invalid fill bounds");
for (int row = 0; row < height; ++row)
std::fill_n(target.pixels.begin() + size_t(y + row) * size_t(target.width) + size_t(x), size_t(width), color);
}
// Formats a number right-aligned in `width` characters; with `grouped`, '.' is inserted as thousands separator
// and the result is padded with spaces for missing separators so columns stay aligned.
// FormatGroupedDigits: '%' is the original bitmap font's blank padding glyph.
std::string original_number(uint32_t value, int width, bool grouped = false) {
std::string digits = std::to_string(value);
const int actual = int(digits.size());
const int padding = std::max(0, width - actual);
digits.insert(0, size_t(padding), '%');
if (grouped) {
for (int from = actual - 3; from > 0; from -= 3)
digits.insert(size_t(padding + from), ".");
const int wanted = width > 3 ? (width - 4) / 3 + 1 : 0;
const int used = actual > 3 ? (actual - 1) / 3 : 0;
if (wanted > used) digits.insert(0, size_t(wanted - used), ' ');
}
return digits;
}
// Amount with the currency text ' EU', e.g. for the contract panels.
std::string money(uint32_t value, int width = 5) {
return original_number(value, width, true) + " EU";
}
// Number with thousands separators and no padding.
std::string plain_number(uint32_t value) {
return original_number(value, 0, true);
}
}
// Builds the contracts view data for one manager: club, manager identity (name, portrait, badge) and one row
// per squad player with remaining months, monthly salary, insurance data and the premium values of the board entry.
// The team premiums are the per-component minimum over all players. Called when the screen opens and after each acknowledgement.
ContractsState contracts_from_game(const GameState& game, int manager_slot, uint64_t revision) {
if (manager_slot < 0 || manager_slot >= 4 || size_t(manager_slot) >= game.manager_teams.size() ||
size_t(manager_slot) * kManagerRecord + kManagerRecord > game.manager_records.size())
throw FormatError("contracts: invalid manager slot");
const int team = game.manager_teams[size_t(manager_slot)];
if (team == kNoTeam) throw FormatError("contracts: Vereinsverträge sind für Nationalteams nicht verfügbar");
const SquadRange range = squad_range(team);
const size_t board = size_t(manager_slot) * kBoard;
if (range.count > 24 || range.first + range.count > game.squad_slots.size() || board + range.count * kBoardSlot > game.tactics.size())
throw FormatError("contracts: invalid squad bounds");
ContractsState state;
state.manager_slot = manager_slot;
state.runtime_club = team;
state.revision = revision;
state.mode_three = game.memory.u8(::bmh::layout::GameMode) == 3;
state.gradient_text = game.memory.u8(::bmh::layout::TextGradientDisabled) == 0;
state.decorated_background = game.memory.u8(::bmh::layout::PlainBackground) == 0;
state.currency_numerator = game.memory.has(::bmh::layout::CurrencyMultiplier) ? game.memory.u32(::bmh::layout::CurrencyMultiplier) : 1;
state.currency_denominator = game.memory.has(::bmh::layout::CurrencyDivisor) ? game.memory.u32(::bmh::layout::CurrencyDivisor) : 1;
if (!state.currency_denominator) throw FormatError("contracts: invalid currency divisor");
state.club_name = original_to_utf8(game.club_name(team));
const size_t manager = size_t(manager_slot) * kManagerRecord;
if (manager + 0x23a > game.manager_records.size()) throw FormatError("contracts: manager identity is unavailable");
const auto name_begin = game.manager_records.begin() + std::ptrdiff_t(manager + 0xa8);
const auto name_end = std::find(name_begin, name_begin + 26, uint8_t(0));
state.manager_name = original_to_utf8(std::string_view(
reinterpret_cast<const char*>(&*name_begin), size_t(name_end - name_begin)));
state.manager_portrait = game.manager_records[manager + 0x221];
if (state.manager_portrait < 0 || state.manager_portrait > 11) throw FormatError("contracts: invalid manager portrait");
if (size_t(team) * 2 + 2 > game.club_heap_78.size()) throw FormatError("contracts: club badge identity is unavailable");
state.badge_sprite = (int(game.club_heap_78[size_t(team) * 2]) |
int(game.club_heap_78[size_t(team) * 2 + 1]) << 8) & 0x1ff;
for (size_t row = 0; row < range.count; ++row) {
const int player = game.squad_slots.at(range.first + row) & 0x0fff;
if (player == 0x0fff) continue;
if (player < 0 || size_t(player) >= game.players.size())
throw FormatError("contracts: invalid player in squad");
const size_t at = board + row * kBoardSlot;
const auto& source = game.players[size_t(player)];
ContractPlayer view;
view.row = int(row);
view.player_id = player;
view.name = original_to_utf8(game.player_name(player));
view.age = source.age();
view.position = source.position();
// Remaining contract in months (255 is shown with a special text by render_roster).
view.remaining_months = game.tactics.at(at + 0x0c);
view.current_monthly_salary = uint32_t(game.tactics.at(at + 0x50)) |
uint32_t(game.tactics.at(at + 0x51)) << 8 |
uint32_t(game.tactics.at(at + 0x52)) << 16 |
uint32_t(game.tactics.at(at + 0x53)) << 24;
view.insurance = game.tactics.at(at + 0x43);
// Insurance can be taken out only if there is none yet, flags 0x60 are clear and the player has no marker/injury in the top bits of w35c.
view.insurance_available = view.insurance == 0 && (game.tactics.at(at + 0x0b) & 0x60) == 0 &&
(source.w35c >> 27) == 0;
// Top bit of the byte at 0x4f blocks new contract offers for this player; the low 7 bits belong to the insurance fee.
view.offer_blocked = (game.tactics.at(at + 0x4f) & 0x80) != 0;
// 15-bit insurance fee: byte 0x4e plus the low 7 bits of byte 0x4f.
view.insurance_fee = uint16_t(game.tactics.at(at + 0x4e)) |
uint16_t(game.tactics.at(at + 0x4f) & 0x7f) << 8;
for (size_t premium = 0; premium < view.team_premiums.size(); ++premium)
view.team_premiums[premium] = game.tactics.at(at + 0x12 + premium);
view.attributes = {uint8_t(source.condition()), uint8_t(source.technique()), uint8_t(source.form()), uint8_t(source.strength())};
view.board_flags = game.tactics.at(at + 0xb);
view.source.row = int(row); view.source.player_id = player;
view.source.slot = game.squad_slots.at(range.first + row);
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()) {
state.team_premiums = state.players.front().team_premiums;
for (const auto& player : state.players)
for (size_t i = 0; i < state.team_premiums.size(); ++i)
state.team_premiums[i] = std::min(state.team_premiums[i], player.team_premiums[i]);
}
return state;
}
// Serialises a ContractRequest as JSON (kind, ids, quote, term, demand, salary, bonuses).
std::string contract_request_json(const ContractRequest& r) {
const char* kind = "demands";
switch (r.kind) {
case ContractActionKind::request_demands: kind = "demands"; break;
case ContractActionKind::submit_offer: kind = "offer"; break;
case ContractActionKind::submit_bonuses: kind = "bonuses"; break;
case ContractActionKind::insure_player: kind = "insurance"; break;
}
std::ostringstream out;
out << "{\"kind\":\"" << kind
<< "\",\"request_id\":" << r.request_id << ",\"revision\":" << r.revision
<< ",\"manager_slot\":" << r.manager_slot << ",\"runtime_club\":" << r.runtime_club
<< ",\"row\":" << r.row << ",\"player_id\":" << r.player_id
<< ",\"quote_id\":" << r.quote_id << ",\"term_column\":" << unsigned(r.term_column)
<< ",\"demand\":" << r.demand << ",\"monthly_salary\":" << r.monthly_salary
<< ",\"insurance_months\":" << unsigned(r.insurance_months) << ",\"proposed_bonuses\":[";
for (size_t i = 0; i < r.proposed_bonuses.size(); ++i) { if (i) out << ','; out << unsigned(r.proposed_bonuses[i]); }
out << "]}";
return out.str();
}
// Serialises the contracts state (players, demands, feedback) as JSON for diagnostics and tests.
std::string contracts_state_json(const ContractsState& s) {
std::ostringstream out;
out << "{\"manager_slot\":" << s.manager_slot << ",\"runtime_club\":" << s.runtime_club
<< ",\"revision\":" << s.revision << ",\"club_name\":" << json_quote(s.club_name)
<< ",\"manager_name\":" << json_quote(s.manager_name) << ",\"manager_portrait\":" << s.manager_portrait
<< ",\"badge_sprite\":" << s.badge_sprite
<< ",\"selected_row\":" << s.selected_row << ",\"quote_id\":" << s.quote_id
<< ",\"quote_row\":" << s.quote_row << ",\"players\":[";
for (size_t i = 0; i < s.players.size(); ++i) {
const auto& p = s.players[i];
if (i) out << ',';
out << "{\"row\":" << p.row << ",\"player_id\":" << p.player_id
<< ",\"name\":" << json_quote(p.name) << ",\"age\":" << p.age
<< ",\"position\":" << p.position << ",\"remaining_months\":" << unsigned(p.remaining_months)
<< ",\"current_monthly_salary\":" << p.current_monthly_salary << '}';
}
out << "],\"demands\":";
if (s.demands) {
out << '[';
for (size_t i = 0; i < s.demands->size(); ++i) { if (i) out << ','; out << (*s.demands)[i]; }
out << ']';
} else out << "null";
out << ",\"feedback\":" << json_quote(s.feedback) << '}';
return out.str();
}
// Loads fonts, textures and executable data, validates the state, and loads the club badge image
// (the widget image is used as the badge atlas when the club has a fallback badge).
ContractsScreen::ContractsScreen(const std::filesystem::path& data, ContractsState state)
: state_(std::move(state)), exe_(load_le(asset_path(data, "BMH.exe"))),
font_(load_font(asset_path(data / "F", "BMH3.FNT"))),
large_font_(load_font(asset_path(data / "F", "BMH2.FNT"))), sprite_font_(data),
texts_(load_textd(asset_path(data, "TEXTD.BLK"))),
atlas_(load_vga(asset_path(data / "KB", "2.VGA"))),
flags_(load_vga(asset_path(data / "KB", "24.VGA"))),
background_(load_vga(asset_path(data / "KB", "129.VGA"))),
widgets_(load_vga(asset_path(data / "KB", "0.VGA"))),
calculator_(load_vga(asset_path(data / "KB", "6.VGA"))),
faces_(load_vga(asset_path(data / "KB", "26.VGA"))) {
if (atlas_.width == 314) { Image padded(320, atlas_.height, 0); padded.blit(atlas_,0,0); atlas_ = std::move(padded); }
if (state_.manager_slot < 0 || state_.manager_slot > 3 || state_.runtime_club < 0 ||
state_.players.size() > 24 || background_.width != 320 || background_.height != 240)
throw FormatError("contracts: invalid state");
if (state_.manager_portrait < 0 || state_.manager_portrait > 11 || state_.badge_sprite < 0 || state_.badge_sprite > 0x1ff)
throw FormatError("contracts: invalid manager identity");
if (source_badge_fallback(state_.runtime_club)) badge_atlas_ = widgets_;
else badge_atlas_ = load_vga(asset_path(data / "W", std::to_string(state_.badge_sprite >> 3) + ".VGA"));
}
// Draws UTF-8 text with the small font at a baseline; nothing is drawn if it does not fit left of `right`.
// The colour index may be replaced by the executable's vertical text gradient when gradient text is enabled.
void ContractsScreen::text(Image& out, int x, int baseline, std::string_view value, uint8_t color, int right) const {
std::string bytes = utf8_to_original(value);
const int limit = right < 0 ? out.width : std::min(right, out.width);
if (font_.text_width(bytes) > limit - x) return;
const int top = baseline - font_.baseline;
if (x < 0 || top < 0 || top + font_.height > out.height) return;
const int width = std::max(1, font_.text_width(bytes));
Image mask(width, font_.height, 0);
font_.draw(mask, 0, 0, bytes, 1);
int gradient = 0;
while (gradient < 32 && exe_.u8(::bmh::layout::Advertising_Text_RuntimeField2 + gradient) != color) ++gradient;
for (int yy = 0; yy < font_.height; ++yy)
for (int xx = 0; xx < width; ++xx)
if (mask.at(xx, yy)) out.at(x + xx, top + yy) = state_.gradient_text && exe_.u8(::bmh::layout::TextGradientMode) == 2 && gradient < 32 ? exe_.u8(::bmh::layout::Advertising_Text_RuntimeField + gradient * 6 + std::min(yy, 5)) : color;
}
// Queues a request for the session: stamps it with a unique id, the state revision, manager and club.
// Only one request can be pending; further input is ignored until it is acknowledged or rejected.
void ContractsScreen::emit(ContractRequest request) {
if (pending_) return;
request.request_id = state_.request_id_base + next_request_id_++;
request.revision = state_.revision;
request.manager_slot = state_.manager_slot;
request.runtime_club = state_.runtime_club;
pending_ = request;
}
// Opens the number-entry calculator over the current screen. `target` selects what is edited:
// 0..6 = a bonus/premium (0..2 percent, max 20; 3..6 max 1000), -2 = insurance months (max 60),
// other negative = the salary offer (max 200000). Sets the maximum, six preset buttons and the initial value;
// presets above the maximum are marked UINT32_MAX (hidden) and the last two presets are 0 and the maximum.
void ContractsScreen::open_calculator(int target, Page return_page, uint32_t initial) {
calculator_backdrop_ = render();
calculator_bonus_index_ = target;
calculator_return_ = return_page;
calculator_initial_ = initial;
salary_input_ = initial;
if (target >= 0) {
calculator_max_ = target < 3 ? 20u : 1000u;
const uint32_t first = target < 3 ? 1u : uint32_t(50 - 10 * (target - 3));
const uint32_t step = first;
for (size_t i = 0; i < 6; ++i) calculator_presets_[i] = first + uint32_t(i) * step;
} else if (target == -2) {
calculator_max_ = 60;
for (size_t i = 0; i < 6; ++i) calculator_presets_[i] = 12u + uint32_t(i) * 6u;
} else {
// Salary offer: presets are spaced by a step derived from the initial value (about a tenth, rounded to a power of ten)
// starting three steps below the value rounded down to thousands.
calculator_max_ = 200000;initial=std::min(initial,calculator_max_);
uint32_t factor=1000, step=0;
while(!step && factor) {step=factor*((initial/factor)/10);if(!step)factor/=10;}
if(!step)step=1;
initial=std::min(initial,calculator_max_);
const uint32_t rounded = 1000u * (initial / 1000u);
const uint32_t first = rounded >= 3u * step ? rounded - 3u * step : 0u;
for (size_t i = 0; i < 6; ++i) calculator_presets_[i] = first + uint32_t(i) * step;
}
salary_input_=std::min(initial,calculator_max_);calculator_initial_=salary_input_;
for(auto& preset:calculator_presets_)if(preset>calculator_max_)preset=UINT32_MAX;
calculator_presets_[6] = 0;
calculator_presets_[7] = calculator_max_;
page_ = Page::calculator;
editing_salary_ = true;
}
// Typed characters while the calculator is open: digits are appended unless the maximum would be exceeded,
// backspace removes the last digit. Returns true if the input changed.
bool ContractsScreen::text(std::string_view value) {
if (pending_ || !editing_salary_) return false;
bool changed = false;
for (char c : value) {
if (c == '\b' || c == 127) { salary_input_ /= 10; changed = true; }
else if (c >= '0' && c <= '9') {
const uint32_t digit = uint32_t(c - '0');
if (salary_input_ <= (calculator_max_ - digit) / 10) {
salary_input_ = salary_input_ * 10 + digit;
changed = true;
}
}
}
return changed;
}
// Draws the roster page: background (or plain bars), title, club name, column header, and one row per player.
// The three roster tabs share the layout of the lineup screen; tab_ 0 and 1 show attribute/status and statistics pages,
// tab_ 2 and 3 show age, monthly salary, remaining months and insurance.
// LineupScreen, contract mode: the same roster as lineup, including its
// original fixed-width number strings, jersey marks, and bitmap icons.
Image ContractsScreen::render_roster() const {
Image out = state_.decorated_background ? background_ : Image(320, 240);
if (!state_.decorated_background) {
fill(out, 0, 4, 320, 13, 0x44); fill(out, 1, 5, 318, 11, 0x39);
fill(out, 0, 228, 320, 10, 0x44); fill(out, 1, 229, 318, 8, 0x39);
}
// Draws a string in the original encoding; with `right` it is centred between x and right.
auto original_text = [&](int x, int y, std::string_view raw, uint8_t color, int right = -1) {
const int width = font_.text_width(raw);
if (right >= 0) x += (right - x) / 2 - width / 2;
text(out, x, y, original_to_utf8(raw), color, -1);
};
// Fixed-width number helper (width characters, optional thousands grouping).
const auto fixed = [](uint32_t value, int width, bool grouped = false) { return original_number(value, width, grouped); };
// Three-character number padded with '%' (used for appearance and goal counters).
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);
const auto header = tab_ == 0 ? texts_[(::bmh::layout::ClubInfo_Render_TextPointer3 - ::bmh::layout::TextPointerTable) / 4] : tab_ == 1 ? texts_[(::bmh::layout::ClubInfo_Render_TextPointer4 - ::bmh::layout::TextPointerTable) / 4] : exe_.cstring(exe_.u32(::bmh::layout::Contracts_RenderRoster_TextPointer));
original_text(0, 26, header, 0x51);
int baseline = 36;
// Rows start at baseline 36 with 7 pixels per row; the baseline grows by 2 where the status bits (0xc000) of the
// slot change from the previous player (a visual gap). `preceding` remembers the previous slot word.
uint16_t preceding = 0;
for (size_t visual = 0; visual < state_.players.size(); ++visual) {
const auto& p = state_.players[visual]; const auto& s = p.source; const auto& a = s.attributes;
if (visual && (s.slot & 0xc000) != (preceding & 0xc000)) baseline += 2;
preceding = s.slot;
if (s.slot & 0xc000) {
const int jersey = (s.cards & 31) + 1;
const uint8_t color = s.slot & 0x1000 ? 0xf3 : s.slot & 0x8000 ? 0x6c : 0x68;
original_text(4, baseline, std::string(1, char('0' + jersey % 10)), color);
if (jersey >= 10) original_text(0, baseline, exe_.cstring(::bmh::layout::ClubInfo_Render_TextPointer), color);
}
// Name colour: selected row, hovered row, blocked contract (tab 2 only) or alternating row colours.
const uint8_t name_color = p.row == state_.selected_row ? 0x27 : p.row == hovered_row_ ? 0x3e :
tab_ == 2 && (p.offer_blocked || (p.board_flags & 0x20)) ? 0xf7 : visual % 2 ? 0x67 : 0x6b;
original_text(13, baseline, s.name, name_color);
if (tab_ != 1) {
original_text(66, baseline, texts_[(::bmh::layout::ClubInfo_Render_TextPointer2 - ::bmh::layout::TextPointerTable) / 4 + a.position()], 0x6c, 82);
for (int field = 0; field < 2; ++field) {
int value = (a.w35c >> (field == 0 ? 24 : 21)) & 7; if (value > 4) --value;
copy_region(out, atlas_, 208 + value * 16, field == 0 ? 17 : 22,
field == 0 ? 88 : 103, baseline - 5, 16, 5, true);
}
std::string values = fixed(a.condition(), 2) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField) + fixed(a.technique(), 2) +
exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField) + fixed(a.form(), 2) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField2) + fixed(a.strength(), 2) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField3);
if (tab_ == 2 || tab_ == 3) {
values += fixed(a.age(), 2) + texts_[(::bmh::layout::Contracts_RenderRoster_TextPointer3 - ::bmh::layout::TextPointerTable) / 4];
const uint32_t numerator = state_.currency_numerator, denominator = state_.currency_denominator;
// Amount scaled with the currency multiplier/divisor followed by the currency suffix.
const auto currency = [&](uint32_t value, int width) { return fixed(uint32_t(value * numerator) / denominator, width, true) + exe_.cstring(::bmh::layout::CurrencySuffixText); };
values += currency(p.current_monthly_salary, 6) + exe_.cstring(::bmh::layout::Contracts_RenderRoster_ExecutableField2);
values += p.remaining_months == 255 ? exe_.cstring(::bmh::layout::Contracts_RenderRoster_ExecutableField3) : fixed(p.remaining_months, 2) + exe_.cstring(::bmh::layout::Contracts_RenderRoster_ExecutableField4);
values += p.insurance == 0 ? texts_[(::bmh::layout::Contracts_RenderRoster_TextPointer4 - ::bmh::layout::TextPointerTable) / 4] : currency(p.insurance_fee, 5) +
exe_.cstring(::bmh::layout::Contracts_RenderRoster_ExecutableField2) + fixed(p.insurance & 0x3f, 2) + exe_.cstring(::bmh::layout::Contracts_RenderRoster_ExecutableField);
original_text(117, baseline, values, 0x6c);
} else {
original_text(117, baseline, values + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField4), 0x6c);
copy_region(out, atlas_, 304, int(s.board[0xe] >> 6) * 3, 179, baseline - 4, 16, 3, true);
const int injury = (a.w364 >> 16) & 31, marker = a.w35c >> 27;
if (!marker && !injury) {
const int status = s.slot & 0x8000 ? 0 : s.slot & 0x4000 ? 1 : 2;
copy_region(out, atlas_, 272, status * 5, 191, baseline - 5, 32, 5, true);
} else {
const int status = !marker ? 5 : marker == 31 ? 8 : !injury ? 4 : 3;
copy_region(out, atlas_, 197, (status - 3) * 6 + 1, 196, baseline - 5, 10, 5);
}
const int nation = a.nationality() + ((s.bonus & 0x80) >> 2);
copy_region(out, flags_, nation % 12 * 9, nation / 12 * 6, 217, baseline - 6, 9, 6);
original_text(232, baseline, fixed(s.board[0xf], 2), s.board[0xf] < 61 ? 0x66 : s.board[0xf] < 71 ? 0x27 : 0xfb);
original_text(247, baseline, fixed(a.w364 >> 26, 2) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField4) + fixed((a.w364 >> 2) & 63, 2) +
exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField4) + fixed((a.w368 >> 18) & 63, 2) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField4) +
fixed((a.w368 >> 14) & 15, 2) + exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField4) + fixed((a.w368 >> 24) & 7, 2) + exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField), 0x6c);
}
} else {
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);
// Reads a little-endian 16-bit value from the player's board entry.
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) + 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;
}
return out;
}
// Renders the complete screen. The base is the roster, or the saved backdrop while the calculator or the
// result dialog is shown; on top come the tab buttons and the negotiation panel, calculator or result notice.
Image ContractsScreen::render() const {
Image out(320, 240);
if (page_ == Page::result && result_backdrop_) out = *result_backdrop_;
else if (page_ == Page::calculator && calculator_backdrop_) out = *calculator_backdrop_;
else out = render_roster();
if (page_ == Page::result) {
// The original manager response is drawn over the still-open contract
// panel; the saved pixels below it are the exact pre-ACK screen.
}
const auto selected = std::find_if(state_.players.begin(), state_.players.end(),
[&](const auto& p) { return p.row == state_.selected_row; });
// Tab captions come from text entries 117..120; the fourth tab (team premiums) exists only in mode three.
const std::array<std::string,4> tabs{original_to_utf8(texts_[117]),original_to_utf8(texts_[118]),original_to_utf8(texts_[119]),original_to_utf8(texts_[120])};
for (int i = 0; i < (state_.mode_three?4:3); ++i) {
const int x = i * 60 + 2, w = i == 3 ? 71 : 51;
if(i!=tab_) {fill(out,x+2,225,w,2,0x40);fill(out,x+w,217,2,10,0x40);}
else {const uint8_t color=out.at(x,225);fill(out,x,215,w+1,2,color);fill(out,x,215,2,11,color);}
const int source_y = i == tab_ ? 44 : 74;
const int dest_y = 215 + (i == tab_ ? 2 : 0);
const int dest_x = x + (i == tab_ ? 2 : 0);
copy_region(out, widgets_, 24, source_y, dest_x, dest_y, 31, 10);
int tab_at = 31, remain = w - 31;
while (remain > 20) { const int n = std::min(8, remain - 20); copy_region(out, widgets_, 24, source_y + 10, dest_x + tab_at, dest_y, n, 10); tab_at += n; remain -= n; }
if (remain > 0) copy_region(out, widgets_, 45 - remain, source_y + 20, dest_x + tab_at, dest_y, remain, 10);
sprite_font_.draw(out, dest_x + w/2 - sprite_font_.text_width(tabs[size_t(i)])/2, dest_y+7, tabs[size_t(i)], exe_.u8(::bmh::layout::Advertising_Button_RuntimeField + (i==tab_)));
}
// Draws a framed panel.
auto panel = [&](int x, int y, int w, int h, bool shadow = true) {
frame(out, exe_, widgets_, x, y, w, h, shadow);
};
// Draws a button of width w: 31 pixel left part, repeated strips and the right end, with the label centred.
// `id` is the button's number on the page; the one held down by the mouse is drawn pressed (button_frame.hpp).
auto button = [&](int id, int x, int y, int w, std::string_view label) {
const bool down = pressed_button_ == id;
const int shift = draw_wide_button_frame(out, widgets_, x, y, w, down);
const int font = exe_.u8(::bmh::layout::Advertising_Button_RuntimeField + int(down));
sprite_font_.draw(out, x + shift + (w - sprite_font_.text_width(label)) / 2, y + shift + 7, label, font);
};
// Looks up a text from the original text table by its pointer-table address.
auto db = [&](uint32_t address) { return original_to_utf8(texts_[(address-::bmh::layout::TextPointerTable)/4]); };
// Draws text centred between two x positions.
auto centered = [&](int left,int right,int y,std::string_view value,uint8_t color) {
const int x=left+(right-left)/2-font_.text_width(utf8_to_original(value))/2;
text(out,x,y,value,color,-1);
};
// Negotiation panel: player offer page or premium page. It is placed at y=22 for team premiums or for players
// in rows >= 13, otherwise at y=130, so the selected player's row stays visible.
const bool negotiating = page_ == Page::player_offer || page_ == Page::team_premiums;
if (negotiating) {
const bool team = page_ == Page::team_premiums && team_premium_mode_;
const int top = team || selected == state_.players.end() || selected->row >=13 ?22:130;
panel(0,top,319,90,false);
const auto heading = db(::bmh::layout::Contracts_Out_ExecutableField3 + (page_==Page::team_premiums?4:0)) + db(::bmh::layout::Contracts_Out_ExecutableField5) + (team?db(::bmh::layout::Contracts_Out_ExecutableField6):selected->name);
large_font_.draw(out,5,top+11-large_font_.baseline,utf8_to_original(heading),108);
fill(out,5,top+12,309,1,108);
if(!team && selected!=state_.players.end()) {
std::string current=db(::bmh::layout::Contracts_Out_ExecutableField7)+selected->name;
if(!selected->remaining_months)current+=db(::bmh::layout::Contracts_Out_ExecutableField8);
else current+=db(::bmh::layout::Contracts_Out_ExecutableField9)+std::to_string(selected->remaining_months)+db(::bmh::layout::Contracts_Out_ExecutableField10)+money(selected->current_monthly_salary)+db(::bmh::layout::ClubPlayerDetail_Copy_ExecutableField4);
text(out,5,top+19,current,108,-1);
std::string insurance=db(::bmh::layout::Contracts_Out_TextPointer4);
if(selected->insurance) insurance=db(::bmh::layout::Contracts_Out_TextPointer3)+std::to_string(selected->insurance&63)+db(::bmh::layout::Contracts_Out_ExecutableField11)+
db(::bmh::layout::ClubPlayerDetail_Copy_TextPointer6+((selected->insurance>>6)&3)*4)+db(::bmh::layout::Contracts_Out_ExecutableField12)+money(selected->insurance_fee)+db(::bmh::layout::Contracts_Out_ExecutableField13);
text(out,5,top+25,insurance,108,-1);
}
if(page_==Page::player_offer && selected!=state_.players.end()) {
text(out,5,top+46,db(::bmh::layout::Contracts_Out_ExecutableField14),106,-1);text(out,5,top+54,db(::bmh::layout::Contracts_Out_TextPointer5),106,-1);
if(state_.mode_three)text(out,5,top+72,db(::bmh::layout::Contracts_Out_TextPointer6),106,-1);
// Four contract-term columns (1..4 years), 60 pixels wide from x=70: the entered salary offer for the chosen term,
// the club's demand for each term (UINT32_MAX = no quote yet) and, in mode three, the insurance row.
for(int term=0;term<4;++term) {
const int left=70+60*term;
centered(left,left+60,top+37,std::to_string(term+1)+db(::bmh::layout::Contracts_Out_ExecutableField15)+(term?exe_.cstring(::bmh::layout::ClubPlayerDetail_RenderCard_TextPointer):""),107);
if(salary_offer_set_ && term==offer_term_) centered(left,left+60,top+46,money(offer_salary_),108);
// Demand is shown in a different colour (251) if no quote exists or the player cannot receive offers.
const uint32_t demand=state_.demands?(*state_.demands)[size_t(term)]:UINT32_MAX;
centered(left,left+60,top+54,demand==UINT32_MAX?db(::bmh::layout::Contracts_Out_ExecutableField16):money(demand),
demand==UINT32_MAX || selected->offer_blocked || (selected->board_flags&0x60)?251:95);
if(state_.mode_three) {
centered(left,left+55,top+64,db(::bmh::layout::ClubPlayerDetail_Copy_TextPointer6+term*4),107);
// Insurance fee shown per insurance term column; the active insurance term is highlighted.
const uint32_t fee=(((uint32_t(term*10+10)*selected->current_monthly_salary)/1000)*10);
centered(left,left+60,top+72,original_number(fee,5,true)+db(::bmh::layout::ClubPlayerDetail_Copy_ExecutableField4),selected->insurance && ((selected->insurance>>6)&3)==term?39:106);
}
}
} else {
// Premium page: seven bonus lines. The shown amount is the percentage (rate times 1 for the first three lines,
// times 5 for the others) of the basis salary: the player's monthly salary, or for the team page a value derived from
// the summed salaries and the number of players.
uint32_t base=0; int occupied=0;
if(team)for(const auto& p:state_.players){base+=p.current_monthly_salary;++occupied;}
else if(selected!=state_.players.end())base=selected->current_monthly_salary;
for(int i=0;i<7;++i) {
text(out,25,top+32+i*7,db(::bmh::layout::Contracts_Out_ExecutableField4+i*4),exe_.u8(::bmh::layout::Contracts_Out_TextPointer2+i),-1);
const unsigned rate=proposed_bonuses_[size_t(i)];
if(!rate) text(out,132,top+32+i*7,db(::bmh::layout::Contracts_Out_TextPointer8),108,-1);
else {
const unsigned percent=rate*(i<3?1:5);
uint32_t salary=base;
if(team&&i<3&&occupied){salary/=occupied;if(i==2)salary*=11;if(i==1)salary*=occupied;}
text(out,97,top+32+i*7,original_number(percent,4,true)+db(::bmh::layout::Contracts_Out_TextPointer7)+money((uint32_t(percent)*salary)/100,7)+exe_.cstring(::bmh::layout::Contracts_Out_TextPointer),108,-1);
}
}
}
button(0,5,top+76,51,db(::bmh::layout::Contracts_Out_ExecutableField17));button(1,90,top+76,80,db(::bmh::layout::Contracts_Out_ExecutableField18));
if(!team&&state_.mode_three)button(2,261,top+76,51,db(page_==Page::player_offer?::bmh::layout::Contracts_Out_ExecutableField2: ::bmh::layout::Contracts_Out_ExecutableField));
} else if (page_ == Page::calculator) {
// Calculator: background image at (10,10), drop shadow, label, 15 keys (digits and the letters D, R, C, J, N),
// 8 preset buttons and the current input right-aligned. Key and preset positions come from tables in the executable.
copy_region(out, calculator_, 0, 0, 10, 10, 113, 136);
fill(out,123,12,2,136,0x40);fill(out,12,146,113,2,0x40);
const auto label=calculator_bonus_index_==-2?db(::bmh::layout::Contracts_Out_ExecutableField19):calculator_bonus_index_<0?db(::bmh::layout::Contracts_Out_ExecutableField14):db(::bmh::layout::Contracts_Out_ExecutableField4+calculator_bonus_index_*4);
centered(18,115,24,label,98);
for(int i=0;i<15;++i) {
const int x=10+exe_.u16(::bmh::layout::Advertising_Click_RuntimeField+i*2), y=10+exe_.u8(::bmh::layout::Advertising_Click_RuntimeField2+i);
copy_region(out,calculator_,0,136,x,y,15,14);
text(out,x+5,y+9,std::string(1,"0123456789DRCJN"[i]),98,-1);
}
for(int i=0;i<8;++i) {
const int x=10+exe_.u16(::bmh::layout::Advertising_Click_RuntimeField+(i+15)*2), y=10+exe_.u8(::bmh::layout::Advertising_Click_RuntimeField2+i+15);
if(calculator_presets_[size_t(i)]==UINT32_MAX)continue;
const auto value=plain_number(calculator_presets_[size_t(i)]);
text(out,x+47-font_.text_width(value),y+12,value,98,-1);
}
const auto value=plain_number(salary_input_);
text(out,116-font_.text_width(value),120,value,0,-1);
} else if (page_ == Page::result) {
// Result page: the manager's answer is shown as the original-style notice dialog over the saved backdrop.
TaxDialogProjection notice;
notice.manager_name=utf8_to_original(state_.manager_name);
notice.portrait=state_.manager_portrait;
notice.badge_sprite=state_.badge_sprite;
notice.badge_fallback=source_badge_fallback(state_.runtime_club);
notice.body=utf8_to_original(state_.feedback);
notice.source_manager_flags=0x80;
draw_source_dialog(out,{widgets_,faces_,&badge_atlas_,font_,large_font_,
exe_,sprite_font_,texts_},notice,state_.gradient_text,
pressed_button_==3?std::optional<GameEndTaxDialog::Button>(GameEndTaxDialog::Button::acknowledge):std::nullopt);
}
return out;
}
// Handles a mouse click on the current page and returns true if the screen changed.
// Result page: the OK button closes the notice. Roster: right click on a name asks for the player's demands
// (a request that the session acknowledges), the tab row switches tabs, left click on a name shows player info.
// Calculator: keys, presets and accept/cancel. Offer and premium pages: term columns, premium lines and the buttons
// at top+76 (back, send, switch page).
// Mouse DOWN on one of the buttons below the offer or premium panel (back, send, switch page): drawn pressed until the
// release. Returns true when the picture changed.
bool ContractsScreen::press(int x, int y) {
int id = -1;
if (page_ == Page::result) {
// OK of the manager's answer (id 3)
if (source_dialog_button_at(TaxDialogKind::acknowledgement, false, x, y) == GameEndTaxDialog::Button::acknowledge) id = 3;
} else if (page_ == Page::player_offer || page_ == Page::team_premiums) {
const bool team = page_ == Page::team_premiums && team_premium_mode_;
const auto selected = std::find_if(state_.players.begin(), state_.players.end(), [&](const auto& p) { return p.row == state_.selected_row; });
const int top = team || selected == state_.players.end() || selected->row >= 13 ? 22 : 130;
if (y >= top + 76 && y <= top + 85) {
if (x < 60) id = 0;
else if (x >= 90 && x <= 170) id = 1;
else if (x >= 261 && x <= 312 && !team && state_.mode_three) id = 2;
}
}
const bool changed = id != pressed_button_;
pressed_button_ = id;
return changed;
}
bool ContractsScreen::click(int x, int y, bool right) {
pressed_button_ = -1; // the release ends the pressed look
if (page_ == Page::result) {
if (x >= 95 && x <= 220 && y >= 166 && y <= 176) { page_ = result_return_; result_backdrop_.reset(); state_.feedback.clear(); return true; }
return false;
}
if (right && page_ == Page::roster) {
if (x >= 61 || y < 29) { close_requested_ = true; return true; }
const ContractPlayer* p = nullptr; int baseline = 36;
for (size_t i=0;i<state_.players.size();++i) {
const auto& candidate=state_.players[i];
if (i && (candidate.source.slot&0xc000)!=(state_.players[i-1].source.slot&0xc000)) baseline+=2;
if(y>=baseline-7 && y<baseline+2) {p=&candidate;break;} baseline+=7;
}
if (!p) {close_requested_=true;return true;}
state_.selected_row = p->row;
ContractRequest request; request.kind = ContractActionKind::request_demands; request.row = p->row; request.player_id = p->player_id;
emit(request); page_ = Page::player_offer; return true;
}
if (page_ == Page::roster && x < 18 && y < 18) { close_requested_ = true; pending_.reset(); return true; }
if (pending_) return false;
if(right && page_==Page::calculator){page_=calculator_return_;calculator_backdrop_.reset();editing_salary_=false;return true;}
if(right && page_!=Page::roster){page_=Page::roster;return true;}
if (page_ == Page::calculator) {
// Applies the calculator value for the edited target: bonus (stored as value, or value/5 for the lines from 3),
// insurance (creates an insure_player request, max 60 months) or the salary offer for the chosen term.
auto accept_calculator = [&]() {
if (calculator_bonus_index_ >= 0) {
const auto selected_player = std::find_if(state_.players.begin(), state_.players.end(), [&](const auto& p) { return p.row == state_.selected_row; });
const uint32_t maximum = calculator_bonus_index_ < 3 ? 20u : 1000u;
if ((!team_premium_mode_ && selected_player == state_.players.end()) || salary_input_ > maximum) return false;
proposed_bonuses_[size_t(calculator_bonus_index_)] = uint8_t(calculator_bonus_index_ < 3 ? salary_input_ : salary_input_ / 5);
page_ = Page::team_premiums; editing_salary_ = false; calculator_bonus_index_ = -1; return true;
}
if (calculator_bonus_index_ == -2) {
const auto selected_player = std::find_if(state_.players.begin(), state_.players.end(), [&](const auto& p) { return p.row == state_.selected_row; });
if (selected_player == state_.players.end() || salary_input_ < 1 || salary_input_ > 60) return false;
ContractRequest req; req.kind = ContractActionKind::insure_player; req.row = selected_player->row;
req.player_id = selected_player->player_id; req.quote_id = state_.quote_id; req.term_column = insurance_term_;
req.insurance_months = uint8_t(salary_input_); emit(req); calculator_bonus_index_ = -1; return true;
}
if (calculator_return_ == Page::player_offer && calculator_bonus_index_ == -1) {
offer_salary_ = salary_input_; offer_term_ = selected_term_; salary_offer_set_ = true;
}
page_ = calculator_return_; calculator_backdrop_.reset(); editing_salary_ = false; return true;
};
if (x >= 69 && x < 85 && y >= 125 && y < 139) {
salary_input_ = 0;
return true;
}
// Digit keys type, then the editing keys in order: backspace, reset to initial value, clear, accept, cancel.
// Key positions are read from the original calculator hitbox table.
for (int i = 0; i < 15; ++i) {
const int key_x = 10 + exe_.u16(::bmh::layout::Advertising_Click_RuntimeField + size_t(i) * 2);
const int key_y = 10 + exe_.u8(::bmh::layout::Advertising_Click_RuntimeField2 + size_t(i));
if (x < key_x || x >= key_x + 16 || y < key_y || y >= key_y + 14) continue;
if (i <= 9) return text(std::to_string(i));
if (i == 10) { salary_input_ /= 10; return true; }
if (i == 11) { salary_input_ = calculator_initial_; return true; }
if (i == 12) { salary_input_ = 0; return true; }
if (i == 13) return accept_calculator();
if (i == 14) { page_ = calculator_return_; calculator_backdrop_.reset(); editing_salary_ = false; calculator_bonus_index_ = -1; return true; }
}
for (int i = 0; i < 8; ++i) {
const int preset_x = 10 + exe_.u16(::bmh::layout::Advertising_Click_RuntimeField + size_t(i + 15) * 2);
const int preset_y = 10 + exe_.u8(::bmh::layout::Advertising_Click_RuntimeField2 + size_t(i + 15));
if (x < preset_x || x >= preset_x + 53 || y < preset_y || y >= preset_y + 14) continue;
if(calculator_presets_[size_t(i)]==UINT32_MAX)return false;
salary_input_ = calculator_presets_[size_t(i)];
return true;
}
return false;
}
if (page_ == Page::player_offer) {
const auto p = std::find_if(state_.players.begin(), state_.players.end(), [&](const auto& v) { return v.row == state_.selected_row; });
if (p == state_.players.end()) return false;
const int top = p->row < 13 ? 130 : 22;
if (y >= top + 76 && y <= top + 85 && x < 60) { page_ = Page::roster; return true; }
if (y >= top + 76 && y <= top + 85 && x >= 261 && x <= 312) {
team_premium_mode_ = false; page_ = Page::team_premiums; return true;
}
if (y >= top + 76 && y <= top + 85 && x >= 90 && x <= 170 && state_.demands) {
if (!salary_offer_set_ && proposed_bonuses_ == p->team_premiums) { page_ = Page::roster; return true; }
ContractRequest request; request.kind = salary_offer_set_ ? ContractActionKind::submit_offer : ContractActionKind::submit_bonuses;
request.row = state_.quote_row; request.quote_id = state_.quote_id; request.player_id = p->player_id;
if (salary_offer_set_) { request.term_column = offer_term_; request.demand = (*state_.demands)[offer_term_]; request.monthly_salary = offer_salary_; }
request.proposed_bonuses = proposed_bonuses_;
if (request.demand == std::numeric_limits<uint32_t>::max()) return false;
emit(request); return true;
}
if (y >= top + 40 && y < top + 58 && x >= 70 && x < 310 && !p->offer_blocked && !(p->board_flags & 0x60)) {
selected_term_ = uint8_t(std::clamp((x - 70) / 60, 0, 3));
const uint32_t initial = state_.demands && state_.quote_row == state_.selected_row ? (*state_.demands)[selected_term_] : 0;
open_calculator(-1, page_, initial); return true;
}
if (y >= top + 58 && y < top + 74 && x >= 70 && x < 310 && state_.mode_three) {
if(!p->insurance_available) {
const uint32_t address=p->board_flags&0x20?::bmh::layout::CareerSession_Reject_TextPointer:p->board_flags&0x40?::bmh::layout::CareerSession_Reject_TextPointer2:p->insurance?::bmh::layout::CareerSession_Reject_TextPointer3: ::bmh::layout::CareerSession_Reject_TextPointer4;
result_backdrop_=render();state_.feedback=original_to_utf8(texts_[(address-::bmh::layout::TextPointerTable)/4]);result_return_=page_;page_=Page::result;return true;
}
insurance_term_ = uint8_t(std::clamp((x - 70) / 60, 0, 3));
open_calculator(-2, page_, 12); return true;
}
return false;
}
if (page_ == Page::team_premiums) {
const auto selected_player = std::find_if(state_.players.begin(), state_.players.end(), [&](const auto& player) { return player.row == state_.selected_row; });
const int top = team_premium_mode_ || selected_player == state_.players.end() || selected_player->row >= 13 ? 22 : 130;
if (y >= top + 76 && y <= top + 85 && x < 60) { page_ = Page::roster; return true; }
if (y >= top + 76 && y <= top + 85 && x >= 261 && x <= 312 && !team_premium_mode_) { page_ = Page::player_offer; return true; }
if (y >= top + 76 && y <= top + 85 && x >= 90 && x <= 170) {
ContractRequest req;
req.proposed_bonuses = proposed_bonuses_;
if (team_premium_mode_) { req.kind = ContractActionKind::submit_bonuses; req.row = -1; req.player_id = -1; }
else if (salary_offer_set_ && state_.demands && selected_player != state_.players.end()) {
req.kind = ContractActionKind::submit_offer; req.row = selected_player->row; req.player_id = selected_player->player_id;
req.quote_id = state_.quote_id; req.term_column = offer_term_; req.demand = (*state_.demands)[offer_term_]; req.monthly_salary = offer_salary_;
} else if (selected_player != state_.players.end()) {
req.kind = ContractActionKind::submit_bonuses; req.row = selected_player->row; req.player_id = selected_player->player_id; req.quote_id = state_.quote_id;
} else return false;
if (!salary_offer_set_ && req.proposed_bonuses == (team_premium_mode_ ? state_.team_premiums : selected_player->team_premiums)) { page_ = Page::roster; return true; }
emit(req); return true;
}
for (int index = 0; index < 7; ++index) {
if (y >= top + 25 + index * 7 && y < top + 32 + index * 7 && x >= 5 && x <= 300 && (team_premium_mode_ || selected_player != state_.players.end())) {
const uint32_t initial = proposed_bonuses_[size_t(index)];
open_calculator(index, Page::team_premiums, initial); return true;
}
}
return false;
}
if (y >= 215 && y <= 224) {
const int which = x < 60 ? 0 : x < 120 ? 1 : x < 180 ? 2 : x < 250 ? 3 : -1;
if (which < 0 || (!state_.mode_three && which==3)) return false;
tab_ = uint8_t(which);
page_ = which == 3 ? Page::team_premiums : Page::roster;
if (which == 3) {
team_premium_mode_ = true; proposed_bonuses_ = state_.team_premiums;
}
return true;
}
if(page_==Page::roster && x>=0 && x<61) {
int baseline=36;
for(size_t i=0;i<state_.players.size();++i) {
const auto& p=state_.players[i];
if(i&&(p.source.slot&0xc000)!=(state_.players[i-1].source.slot&0xc000))baseline+=2;
if(y>=baseline-7 && y<baseline+2){player_info_row_=p.row;return true;}baseline+=7;
}
}
return false;
}
// Called when the pointer-hold timer fires: on the roster it behaves like a right click (opens the talk with the
// player under the pointer), on the calculator and the negotiation pages it discards the local input and goes back.
bool ContractsScreen::process_timer_expired(int pointer_x, int pointer_y) {
//InstallDefaultHooks(0x80) dispatches event8 against the actual name-column row:
//outside the list it returns; over a player it enters the normal talk.
if (pending_ || close_requested_) return false;
if (page_ == Page::roster) return click(pointer_x, pointer_y, true);
if (page_ == Page::calculator) {
//BalanceFromGame event8 restores its backdrop and returns ffffffff;40c65
//does not write the proposed bonus on that sentinel. No offer/insurance
//command and no invented N-key delay belong to this local return.
salary_input_ = calculator_initial_;
page_ = calculator_return_;
calculator_backdrop_.reset();
calculator_bonus_index_ = -1;
editing_salary_ = false;
return true;
}
if (page_ == Page::player_offer || page_ == Page::team_premiums) {
//41232 checks event8 after the offer button branch. A timer owns
//neither that button nor its mutations: discard the local negotiation.
page_ = Page::roster;
return true;
}
return false; // result notices retain their separate physical input owner
}
// Pointer movement over the roster names (x 0..60): updates the hovered row; returns true if it changed.
bool ContractsScreen::move(int x,int y) {
if (page_ != Page::roster || pending_) return false;
int next=-1, baseline=36;
for(size_t i=0;i<state_.players.size();++i) {
const auto& p=state_.players[i];
if(i&&(p.source.slot&0xc000)!=(state_.players[i-1].source.slot&0xc000))baseline+=2;
if(x<61&&x>=0&&y>=baseline-7&&y<baseline+2){next=p.row;break;}baseline+=7;
}
if(next==hovered_row_)return false;
hovered_row_=next;return true;
}
// Keyboard input: Esc goes back one page or closes the screen (ignored in the calculator), digits, backspace and clear edit the calculator value.
bool ContractsScreen::key(std::string_view key) {
if (key == "ESC" || key == "ESCAPE") {
if (page_ == Page::calculator) return false;
if (page_ != Page::roster) { page_ = Page::roster; return true; }
close_requested_ = true; pending_.reset(); return true;
}
if (pending_) return false;
if (page_ == Page::calculator && key == "BACKSPACE") { salary_input_ /= 10; return true; }
if (page_ == Page::calculator && key == "CLEAR") { salary_input_ = 0; return true; }
if (page_ == Page::calculator && key.size() == 1 && key[0] >= '0' && key[0] <= '9') {
const uint32_t digit = uint32_t(key[0] - '0');
const uint32_t max = calculator_bonus_index_ >= 0 ? (calculator_bonus_index_ < 3 ? 20u : 1000u) : calculator_bonus_index_ == -2 ? 60u : 200000u;
if (salary_input_ <= (max - digit) / 10) salary_input_ = salary_input_ * 10 + digit;
return true;
}
return false;
}
// Session reply to a demands request: checks that it matches the pending request and player, takes over the new
// state with the demands and quote id, resets the form and opens the player offer page.
void ContractsScreen::acknowledge_quote(const ContractsState& state) {
if (!pending_ || pending_->kind != ContractActionKind::request_demands || state.revision <= state_.revision ||
state.manager_slot != state_.manager_slot || state.runtime_club != state_.runtime_club || !state.demands ||
state.quote_row != pending_->row || state.quote_id == 0)
throw FormatError("contracts: quote acknowledgement does not match request");
const auto old = std::find_if(state_.players.begin(), state_.players.end(), [&](const auto& p) { return p.row == pending_->row; });
const auto fresh = std::find_if(state.players.begin(), state.players.end(), [&](const auto& p) { return p.row == pending_->row; });
if (old == state_.players.end() || fresh == state.players.end() || old->player_id != pending_->player_id || fresh->player_id != old->player_id)
throw FormatError("contracts: quote player identity changed");
const uint64_t request_id_base = state_.request_id_base;
proposed_bonuses_ = fresh->team_premiums;
state_ = state;
state_.selected_row = pending_->row;
state_.request_id_base = request_id_base;
salary_input_ = 0;
selected_term_ = 0;
salary_offer_set_ = false;
offer_salary_ = 0;
offer_term_ = 0;
editing_salary_ = false;
pending_.reset();
page_ = Page::player_offer;
}
// Session reply to an offer (or bonus) request: takes over the new state and shows the manager's answer as result page.
void ContractsScreen::acknowledge_offer(const ContractsState& state) {
if (!pending_ || (pending_->kind != ContractActionKind::submit_offer && pending_->kind != ContractActionKind::submit_bonuses) || state.revision <= state_.revision ||
state.manager_slot != state_.manager_slot || state.runtime_club != state_.runtime_club)
throw FormatError("contracts: offer acknowledgement does not match request");
result_backdrop_ = render();
const uint64_t request_id_base = state_.request_id_base;
state_ = state;
state_.request_id_base = request_id_base;
state_.selected_row = pending_->row;
state_.demands.reset(); state_.quote_id = 0; state_.quote_row = -1;
editing_salary_ = false;
pending_.reset();
result_return_=Page::roster;page_ = Page::result;
}
// Session reply to a bonus request: takes over the new state; the result page is shown only if there is a message.
void ContractsScreen::acknowledge_bonus_offer(const ContractsState& state) {
if (!pending_ || pending_->kind != ContractActionKind::submit_bonuses ||
state.revision <= state_.revision || state.manager_slot != state_.manager_slot || state.runtime_club != state_.runtime_club)
throw FormatError("contracts: bonus acknowledgement does not match request");
result_backdrop_ = render();
const uint64_t base = state_.request_id_base;
state_ = state; state_.request_id_base = base; pending_.reset(); result_return_=Page::roster;page_ = state.feedback.empty() ? Page::roster : Page::result;
}
// Session reply to an insurance request: checks quote and player identity, takes over the state, back to the offer page.
void ContractsScreen::acknowledge_insurance(const ContractsState& state) {
if (!pending_ || pending_->kind != ContractActionKind::insure_player || state.revision <= state_.revision ||
state.manager_slot != state_.manager_slot || state.runtime_club != state_.runtime_club ||
state.quote_id != state_.quote_id || state.quote_row != pending_->row || !state.demands)
throw FormatError("contracts: insurance acknowledgement does not match request");
const auto before = std::find_if(state_.players.begin(), state_.players.end(), [&](const auto& p) { return p.row == pending_->row; });
const auto after = std::find_if(state.players.begin(), state.players.end(), [&](const auto& p) { return p.row == pending_->row; });
if (before == state_.players.end() || after == state.players.end() || before->player_id != pending_->player_id || after->player_id != before->player_id)
throw FormatError("contracts: insurance player identity changed");
const auto base = state_.request_id_base;
state_ = state; state_.request_id_base = base; pending_.reset(); page_ = Page::player_offer;
calculator_backdrop_.reset(); editing_salary_ = false;
}
// Session rejection of the pending request: shows the reason as feedback; a rejected insurance returns to the offer page.
void ContractsScreen::reject(std::string feedback) {
if (!pending_) throw FormatError("contracts: no action to reject");
const auto kind = pending_->kind;
pending_.reset(); state_.feedback = std::move(feedback); editing_salary_ = false;
if (kind == ContractActionKind::insure_player) page_ = Page::player_offer;
}
// State as JSON for tests: contracts state plus calculator input, selected term, editing flag and page.
std::string ContractsScreen::state_json() const {
auto out = contracts_state_json(state_);
out.pop_back();
out += ",\"monthly_salary_input\":" + std::to_string(salary_input_) +
",\"selected_term\":" + std::to_string(unsigned(selected_term_)) +
",\"editing_salary\":" + std::string(editing_salary_ ? "true" : "false") +
",\"page\":" + std::to_string(unsigned(page_)) + "}";
return out;
}
} // namespace bmh::ui