// SPDX-License-Identifier: GPL-3.0-only // Implements the lineup screen declared in lineup.hpp: lineup_from_game() and lineup_from_save() project // the squad, and LineupScreen renders both data views and converts clicks into LineupRequest values. // Swapping or preparing is done by the session, not here. #include "formats/executable_layout.hpp" #include "ui/lineup.hpp" #include "ui/button_frame.hpp" #include #include #include "formats/savestate.hpp" #include "game/state.hpp" #include "ui/manager.hpp" #include "ui/draw.hpp" namespace bmh::ui { namespace { Bytes block(const SaveState& s, uint32_t address) { const auto& b = s.block(address); return Bytes(s.stream().begin() + b.offset, s.stream().begin() + b.offset + b.size); } std::vector words(const Bytes& b) { if (b.size() % 2) throw FormatError("lineup: odd word table"); std::vector out; for (size_t at = 0; at < b.size(); at += 2) out.push_back(le16(b,at)); return out; } std::string name(const Bytes& b, size_t offset) { if (offset >= b.size()) throw FormatError("lineup: name outside string pool"); const auto end = std::find(b.begin() + offset, b.end(), 0); if (end == b.end()) throw FormatError("lineup: unterminated name"); return std::string(b.begin() + offset, end); } LineupState project(const StaticMemory& memory, const LeImage& exe, const std::vector& teams, const std::vector& squad, const std::vector& players, const Bytes& tactics, const Bytes& options, const std::vector& cards, const Bytes& bonus, const std::vector& player_offsets, const Bytes& player_names, const std::vector& club_offsets, const Bytes& club_names, uint64_t revision) { LineupState out; out.manager_slot = memory.u8(::bmh::layout::ActiveManager); out.revision = revision; if (out.manager_slot < 0 || out.manager_slot > 3 || size_t(out.manager_slot) >= teams.size()) throw FormatError("lineup: unsupported active manager"); out.runtime_club = teams.at(out.manager_slot); const int split = exe.u16(::bmh::layout::TeamsWithSquads), limit = exe.u16(::bmh::layout::LeagueClubCount); const bool national = out.runtime_club == kNoTeam && out.manager_slot == 0 && teams.size() == 1 && ((memory.u32(::bmh::layout::CompetitionSelection) & 2) != 0 || (memory.has(::bmh::layout::CoachActive) && memory.u8(::bmh::layout::CoachActive) != 0)); if (out.runtime_club < 0 || (out.runtime_club >= limit && !national)) throw FormatError("lineup: only domestic league squads are supported"); const size_t count = national || out.runtime_club < split ? 24 : 16; const size_t first = national ? 3072 : out.runtime_club < split ? size_t(out.runtime_club) * 24 : size_t(out.runtime_club) * 16 + split * 8; const size_t board_base = size_t(out.manager_slot) * 0x840; if (first + count > squad.size() || board_base + count * 0x58 > tactics.size() || size_t(out.manager_slot) * 5 >= options.size()) throw FormatError("lineup: truncated squad/tactics/options"); if (size_t(out.runtime_club) >= club_offsets.size()) throw FormatError("lineup: club outside name table"); out.club_name = name(club_names,club_offsets.at(out.runtime_club)); out.saved_display_flags = memory.u16(::bmh::layout::CareerSession_AnyOf_ExecutableField + uint32_t(out.manager_slot) * 2); // A saved pitch view is retained as metadata. Only the two complete team // lists are currently rendered; this is exposed explicitly in state_json(). out.view = (out.saved_display_flags & 3) == 2 ? LineupView::data_b : LineupView::data_a; out.gradient_text = memory.u8(::bmh::layout::TextGradientDisabled) == 0 && exe.u8(::bmh::layout::TextGradientMode) == 2; out.decorated_background = memory.u8(::bmh::layout::PlainBackground) == 0; out.automatic_formation = options.at(size_t(out.manager_slot) * 5); for (size_t row = 0; row < count; ++row) { LineupPlayer p; p.row = int(row); p.slot = squad.at(first + row); std::copy_n(tactics.begin() + board_base + row * 0x58,0x58,p.board.begin()); const int id = p.slot & 0x0fff; if (id != 0x0fff) { if (size_t(id) >= players.size() || size_t(id) >= player_offsets.size() || size_t(id) >= cards.size() || size_t(id) >= bonus.size()) throw FormatError("lineup: player outside authoritative tables"); p.player_id = id; p.attributes = players.at(id); p.name = name(player_names,player_offsets.at(id)); p.cards = cards.at(id); p.bonus = bonus.at(id); } out.players.push_back(std::move(p)); } return out; } void rect(Image& out, int x, int y, int w, int h, uint8_t color) { draw::fill(out, x, y, w, h, color); } void copy(Image& out, const Image& src, int sx, int sy, int x, int y, int w, int h, int transparent = -1) { if (sx < 0 || sy < 0 || sx+w > src.width || sy+h > src.height) throw FormatError("lineup: original atlas bounds " + std::to_string(sx) + "," + std::to_string(sy) + ":" + std::to_string(w) + "x" + std::to_string(h) + " in " + std::to_string(src.width) + "x" + std::to_string(src.height)); draw::copy_clipped(out, src, sx, sy, x, y, w, h, transparent); } std::string number(uint64_t value, int width = 2) { auto s=std::to_string(value); if (int(s.size()) teams; for(int i=0;i players; for(size_t at=0;at= 0) p.statistics = player_club_statistics(g, out.runtime_club, p.player_id, p.board); out.source="original_save_read_only"; return out; } LineupState lineup_from_game(const GameState& g,const LeImage& exe,uint64_t revision) { auto out=project(g.memory,exe,g.manager_teams,g.squad_slots,g.players,g.tactics,g.manager_options, g.player_cards,g.player_bonus,g.player_name_offsets,g.player_names,g.club_ids,g.club_names,revision); for (auto& p : out.players) if (p.player_id >= 0) p.statistics = player_club_statistics(g, out.runtime_club, p.player_id, p.board); out.source="engine_projection"; return out; } std::string lineup_request_json(const LineupRequest& r) { std::ostringstream s; s<<"{\"kind\":"<3||state_.runtime_club<0|| (state_.players.size()!=24&&state_.players.size()!=16)) throw FormatError("lineup: invalid screen state"); for(size_t i=0;i=0)x+=(right-x)/2-width/2; if(x<0||x+width>out.width||baseline<5||baseline>=out.height)return; Image mask(std::max(1,width),6);font_.draw(mask,0,0,value,1); int gradient=0;while(gradient<32&&exe_.u8(::bmh::layout::Advertising_Text_RuntimeField2+gradient)!=color)++gradient; for(int yy=0;yy<6;++yy)for(int xx=0;xx=128)continue; const int glyph=exe_.u8(::bmh::layout::ClubInfo_Raw_RuntimeField3 + c); if(!glyph){x+=2;continue;} const int sx=exe_.u16(::bmh::layout::ClubInfo_Raw_RuntimeField+glyph*2),width=exe_.u16(::bmh::layout::ClubInfo_Raw_RuntimeField2+glyph*2)-sx; copy(out,letters_,sx,(font-4)*7,x,baseline-6,width,7,0);x+=width; } } void LineupScreen::button(Image& out,int x,int y,std::string_view value,bool pressed) const { constexpr int width=55; // a button held down by the mouse is drawn pressed (button_frame.hpp): moved by two pixels, other label mode const int shift=draw_wide_button_frame(out,widgets_,x,y,width,pressed); const int mode=exe_.u8(::bmh::layout::Advertising_Button_RuntimeField+int(pressed)); x+=shift;y+=shift; if(mode==3)text(out,x,y+7,value,0,x+width); else { int length=0;for(unsigned char c:value){const int g=exe_.u8(::bmh::layout::ClubInfo_Raw_RuntimeField3 + c);length+=g?exe_.u16(::bmh::layout::ClubInfo_Raw_RuntimeField2+g*2)-exe_.u16(::bmh::layout::ClubInfo_Raw_RuntimeField+g*2):2;} sprite_text(out,x+width/2-length/2,y+7,value,mode); } } std::vector> LineupScreen::rows() const { std::vector> out;int baseline=36; for(size_t i=0;i=10)text(out,0,baseline,exe_.cstring(::bmh::layout::ClubInfo_Render_TextPointer),color); } const uint8_t name_color=selected_==row?0x27:hovered_==row?0x3e:uint8_t(row%2?0x67:0x6b); text(out,13,baseline,p.name,name_color); if(state_.view==LineupView::data_a) { text(out,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(out,atlas_,208+value*16,field==0?17:22,field==0?88:103,baseline-5,16,5,0);} std::string attrs=number(a.condition())+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField)+number(a.technique())+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField)+ number(a.form())+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField2)+number(a.strength())+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField3)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField4); text(out,117,baseline,attrs,0x6c); copy(out,atlas_,304,int(p.board[0xe]>>6)*3,179,baseline-4,16,3,0); const int injury=(a.w364>>16)&31,marker=a.w35c>>27; if(marker==0&&injury==0){const int status=p.slot&0x8000?0:p.slot&0x4000?1:2;copy(out,atlas_,272,status*5,191,baseline-5,32,5,0);} else {const int status=marker==0?5:marker==31?8:injury==0?4:3;copy(out,atlas_,197,(status-3)*6+1,196,baseline-5,10,5);} const int nationality=a.nationality()+((p.bonus&0x80)>>2); copy(out,flags_,(nationality%12)*9,(nationality/12)*6,217,baseline-6,9,6); text(out,232,baseline,number(p.board[0xf]),p.board[0xf]<61?0x66:p.board[0xf]<71?0x27:0xfb); text(out,247,baseline,number(a.w364>>26)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField4)+number((a.w364>>2)&63)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField4)+ number((a.w368>>18)&63)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField4)+number((a.w368>>14)&15)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField4)+number((a.w368>>24)&7)+exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField),0x6c); } else { text(out,65,baseline,number(a.age())+texts_[(::bmh::layout::ClubInfo_Render_TextPointer5-::bmh::layout::TextPointerTable)/4],0x6c); for(int skill=0;skill<7;++skill)copy(out,atlas_,256+a.skill(Player::Skill(skill))*16,32,87+skill*10,baseline-5,16,5,0); text(out,165,baseline,number(p.bonus&127,3)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5)+number(p.statistics ? p.statistics->appearances[0] : p.board[7],3)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5)+ number(p.statistics ? p.statistics->appearances[2] : p.board[9],3)+exe_.cstring(::bmh::layout::ClubInfo_Render_ExecutableField5)+number((p.statistics ? p.statistics->total : le16(Bytes(p.board.begin(),p.board.end()),0x48)),3)+exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2)+ number((p.statistics ? p.statistics->goals : le16(Bytes(p.board.begin(),p.board.end()),0x4a)),3)+exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField2)+number((p.statistics ? p.statistics->assists : le16(Bytes(p.board.begin(),p.board.end()),0x4c)),3)+exe_.cstring(::bmh::layout::ClubInfo_Raw_ExecutableField),0x6c); } } button(out,1,215,texts_[(::bmh::layout::Lineup_Render_TextPointer-::bmh::layout::TextPointerTable)/4+(state_.view==LineupView::data_a?1:0)],pressed_button_==0); button(out,58,215,texts_[(::bmh::layout::Lineup_Render_TextPointer2-::bmh::layout::TextPointerTable)/4],pressed_button_==1); return out; } LineupRequest LineupScreen::request(LineupRequestKind kind,int first,int second) { LineupRequest r;r.kind=kind;r.manager_slot=state_.manager_slot;r.runtime_club=state_.runtime_club;r.revision=state_.revision; r.first_row=first;r.second_row=second; if(first>=0){const auto& p=state_.players.at(size_t(first));r.first_player=p.player_id;r.first_slot=p.slot;} if(second>=0){const auto& p=state_.players.at(size_t(second));r.second_player=p.player_id;r.second_slot=p.slot;} pending_=r;rejection_.clear();return r; } LineupRequest LineupScreen::initialize() { if(pending_||close_||state_.entry_prepared)return {}; return request(LineupRequestKind::prepare_lineup); } // Mouse DOWN on a bottom button (Daten A/B, Spielfeld): drawn pressed until the release. bool LineupScreen::press(int x,int y) { int id=-1; if(!pending_&&!close_&&y>=215&&y<=224){if(x>=1&&x<=56)id=0;else if(x>=58&&x<=113)id=1;} const bool changed=id!=pressed_button_;pressed_button_=id;return changed; } LineupRequest LineupScreen::click(int x,int y,bool right) { pressed_button_=-1; // the release ends the pressed look if(pending_||close_)return {}; if(right){if(selected_)selected_.reset();else close_=true;return {};} if(y>=215&&y<=224){ if(x>=1&&x<=56){state_.view=state_.view==LineupView::data_a?LineupView::data_b:LineupView::data_a;return {};} if(x>=58&&x<=113)return request(LineupRequestKind::open_pitch); } if(x<0||x>=320)return {}; for(const auto& [row,baseline]:rows())if(y>=baseline-7&&y=63)return request(LineupRequestKind::open_player_info,row); if(!selected_){selected_=row;return {};} if(*selected_==row){selected_.reset();return {};} return request(LineupRequestKind::swap_players,*selected_,row); } return {}; } bool LineupScreen::move(int x,int y) { if(pending_||close_)return false; std::optional next; if(x>=0&&x<63)for(const auto& [row,baseline]:rows())if(y>=baseline-7&&ykind == LineupRequestKind::open_pitch || pending_->kind == LineupRequestKind::open_player_info)) throw FormatError("lineup: local navigation requires context resolution, not an engine acknowledgement"); if(!pending_||updated.revision<=state_.revision||updated.manager_slot!=state_.manager_slot||updated.runtime_club!=state_.runtime_club) throw FormatError("lineup: expected newer state for the same manager/club"); if(updated.players.size()!=state_.players.size())throw FormatError("lineup: squad size changed"); if(pending_->kind==LineupRequestKind::prepare_lineup&&!updated.entry_prepared) throw FormatError("lineup: entry acknowledgement must confirm preparation"); for(size_t row=0;rowkind != kind || (kind != LineupRequestKind::open_pitch && kind != LineupRequestKind::open_player_info)) throw FormatError("lineup: expected a local navigation request"); pending_.reset(); selected_.reset(); hovered_.reset(); rejection_.clear(); } std::string LineupScreen::state_json() const { std::ostringstream s;s<<"{\"screen\":\"lineup\",\"view\":"<=0)s<<",\"starting\":"<<((p.slot&0x8000)?"true":"false")<<",\"bench\":"<<((p.slot&0x4000)?"true":"false") <<",\"position_type\":"<appearances[0] << ',' << p.statistics->appearances[1] << ',' << p.statistics->appearances[2] << "],\"total_appearances\":" << p.statistics->total << ",\"club_goals\":" << p.statistics->goals << ",\"club_assists\":" << p.statistics->assists; s<<'}';} s<<"]}";return s.str(); } } // namespace bmh::ui