// SPDX-License-Identifier: GPL-3.0-only // Implements the league pairings screen declared in league_pairings.hpp: league_pairings_from_game() formats // the rows and button texts from the game, and LeaguePairingsScreen renders them and queues requests for the // session. It also defines the constants and time conversion helper declared in the header. #include "formats/executable_layout.hpp" #include "ui/league_pairings.hpp" #include #include #include #include "game/messages.hpp" #include "game/schedule.hpp" #include "game/season.hpp" #include "ui/desk_state.hpp" #include "ui/manager.hpp" namespace bmh::ui { namespace { std::string field(int n,int width) { auto s=std::to_string(n);if(int(s.size())(g.manager_teams.size(),4);++slot) { if(g.manager_teams[slot]!=team||g.manager_records.at(slot*0xb0c + 0xa8)==0)continue; return int(slot)==g.memory.u8(::bmh::layout::ActiveManager)&&g.memory.u8(::bmh::layout::DeskPosition)src.width||sy+h>src.height||x<0||y<0||x+w>out.width||y+h>out.height) throw FormatError("pairings: Original atlas bounds (source "+std::to_string(src.width)+"x"+ std::to_string(src.height)+", crop "+std::to_string(sx)+","+std::to_string(sy)+" "+ std::to_string(w)+"x"+std::to_string(h)+", destination "+std::to_string(x)+","+std::to_string(y)+")"); for(int yy=0;yy=500?1:0); } LeaguePairingsState league_pairings_from_game(const GameState& g,const LeaguePairingsOwner& owner, const LeImage& exe,uint64_t revision,uint64_t scope,uint64_t sequence, const std::optional& active_packet) { LeaguePairingsState out;out.source=league_pairings_view_source(g,owner,league_pairings_constants(exe)); out.identity={owner.manager,owner.runtime_club,owner.club_identity,owner.league,owner.round,revision,scope,sequence}; if(owner.active_phase&&!active_packet) throw FormatError("pairings: active presentation phase has no owned packet"); if(active_packet) { validate_league_pairings_active_packet_source(g,owner,*active_packet); if(active_packet->league!=owner.league||active_packet->round!=owner.round|| active_packet->rows.size()!=out.source.rows.size()) throw FormatError("pairings: active packet does not cover this projection"); for(size_t n=0;nrows[n]; if(source.source_round!=active.source_round||source.fixture_index!=active.fixture_index|| source.home.runtime_team!=active.home_runtime_team||source.home.club_identity!=active.home_club_identity|| source.away.runtime_team!=active.away_runtime_team||source.away.club_identity!=active.away_club_identity) throw FormatError("pairings: active packet row identity differs from visible fixture"); } out.active_packet=active_packet; } out.decorated=g.memory.u8(::bmh::layout::PlainBackground)==0;out.original_panel=g.memory.u8(::bmh::layout::SolidPanelBackground)==0; out.gradient=g.memory.u8(::bmh::layout::TextGradientDisabled)==0&&exe.u8(::bmh::layout::TextGradientMode)==2; out.secondary_leagues=g.memory.u8(::bmh::layout::CalendarState_FormatError_RuntimeField2)==0; // Keep original bytes in BMH3 fields. SpriteFont receives UTF8 explicitly. //51329 ->LeagueName uses the league names prepared by6b98e, including //the shared Regionalliga A/B text and foreign country's Native name. //TeamStatistics_SourceLeagueName_RuntimeField's temporary prefix is only set around other callers, not this Desk. const auto league_title=owner.league<5?league_name(g,owner.league):g.club_name(0x165+owner.league-4); if(owner.postponed_active) { out.title=message_text(g,::bmh::layout::LeaguePairings_FormatError_TextPointer12)+league_title; out.subtitle.clear(); } else if(owner.filter_active&&owner.filter_team>=0) { out.title=message_text(g,::bmh::layout::LeaguePairings_FormatError_TextPointer11)+league_title; out.subtitle=g.club_name(owner.filter_team); } else { out.title=message_text(g,::bmh::layout::LeaguePairings_FormatError_TextPointer10)+league_title; out.subtitle=owner.title_round+1>out.source.total_rounds?message_text(g,::bmh::layout::CareerSession_FormatError_TextPointer3): message_text(g,::bmh::layout::LeaguePairings_FormatError_TextPointer4)+field(owner.title_round+1,1)+exe.cstring(::bmh::layout::CareerSession_FormatError_TextPointer)+message_text(g,::bmh::layout::CareerSession_FormatError_TextPointer4); } const bool selected_filter=owner.filter_active&&owner.filter_team>=0; if(!selected_filter&&!owner.postponed_active)out.date=date_string(out.source.match_date,exe,true,g); for(size_t n=0;n<8;++n)out.buttons[n]=message_text(g,::bmh::layout::LeaguePairings_FormatError_TextPointer5 + uint32_t(n)*4); for(const auto& row:out.source.rows) { LeaguePairingsDisplayRow display; const std::array teams{&row.home,&row.away}; for(size_t side=0;side<2;++side) { const auto& team=*teams[side];display.names[side]=g.club_name(team.runtime_team); const bool selected=team.runtime_team==owner.search_first||team.runtime_team==owner.search_second|| (owner.filter_active&&team.runtime_team==owner.filter_team); display.name_colors[side]=name_color(g,team.runtime_team,selected?0xfb:0x5f); auto& detail=display.details[side]; if(team.previous_rank>=0)detail=field(team.previous_rank,2)+exe.cstring(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer8)+exe.cstring(::bmh::layout::LeaguePairings_FormatError_ExecutableField); detail+=exe.cstring(::bmh::layout::CupDraw_FormatError_ExecutableField2); detail+=field(team.strength[0],2)+message_text(g,::bmh::layout::CupDraw_FormatError_TextPointer5); detail+=field(team.strength[1],2)+message_text(g,::bmh::layout::LeaguePairings_FormatError_TextPointer8); detail+=field(team.strength[2],2)+message_text(g,::bmh::layout::LeaguePairings_FormatError_TextPointer9); detail+=field(team.strength[3],2)+exe.cstring(::bmh::layout::CupDraw_FormatError_TextPointer2); detail+=field(team.strength[4],2)+exe.cstring(::bmh::layout::CupDraw_FormatError_TextPointer3); } const uint8_t displayed_result=row.computer_tip_result.value_or(row.raw_result); if(owner.active_phase&&displayed_result==0xcc) { if(!active_packet)throw FormatError("pairings: unplayed active row has no descriptor"); const auto found=std::find_if(active_packet->rows.begin(),active_packet->rows.end(),[&](const auto& active) { return active.source_round==row.source_round&&active.fixture_index==row.fixture_index; }); if(found==active_packet->rows.end())throw FormatError("pairings: missing active row identity"); display.active=*found; display.temperature=format_number(g,int(found->temperature),0x81)+message_text(g,::bmh::layout::LeaguePairings_FormatError_TextPointer14); display.details[0]=found->referee_name+exe.cstring(::bmh::layout::LeaguePairings_FormatError_TextPointer2)+ message_text(g,::bmh::layout::LeaguePairings_FormatError_TextPointer6+uint32_t(found->referee_profile&3)*4)+exe.cstring(::bmh::layout::LeaguePairings_FormatError_TextPointer3)+ message_text(g,::bmh::layout::LeaguePairings_FormatError_TextPointer7+uint32_t((int(int8_t(found->referee_profile))>>3)&3)*4)+ exe.cstring(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer11); display.details[1].clear(); } if(league_pairings_played_source(displayed_result)) display.result=field(displayed_result>>4,1)+exe.cstring(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer9)+field(displayed_result&15,1); else if(row.postponed_date) {display.result=date_string(*row.postponed_date,exe,false,g);display.result_x=255;} if(!owner.postponed_active&&row.scheduled_date) { display.scheduled_ordinal=field(row.source_round+1,2)+message_text(g,::bmh::layout::CupDraw_FormatError_TextPointer6); display.scheduled_date=exe.cstring(::bmh::layout::LeaguePairings_FormatError_TextPointer)+date_string(*row.scheduled_date,exe,false,g)+exe.cstring(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer11); } if(!owner.active_phase&&!owner.postponed_active&&!selected_filter&&row.first_leg_result)display.first_leg=field(*row.first_leg_result&15,1)+exe.cstring(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer9)+ field(*row.first_leg_result>>4,1)+message_text(g,::bmh::layout::LeaguePairings_FormatError_TextPointer13); out.rows.push_back(std::move(display)); } return out; } LeaguePairingsScreen::LeaguePairingsScreen(const std::filesystem::path& data,LeaguePairingsState state) :exe_(load_le(asset_path(data,"BMH.exe"))),frame_(load_vga(asset_path(data/"KB","129.VGA"))), panel_(load_vga(asset_path(data/"KB","56.VGA"))),arrows_(load_vga(asset_path(data/"KB","1.VGA"))), widgets_(load_vga(asset_path(data/"KB","0.VGA"))),weather_(load_vga(asset_path(data/"KB","22.VGA"))), font_(load_font(asset_path(data/"F","BMH3.FNT"))), sprite_(data),state_(std::move(state)) { const auto& id=state_.identity; const auto& owner=state_.source.owner; if(id.manager<0||id.manager>=4||id.league<0||id.league>=9||id.round<0||id.round>=state_.source.total_rounds|| !id.scope||state_.source.owner.closed||state_.rows.size()!=state_.source.rows.size()|| !owner.supported||owner.manager!=id.manager||owner.runtime_club!=id.runtime_club|| owner.club_identity!=id.club_identity||owner.league!=id.league||owner.round!=id.round|| (!owner.postponed_active&&state_.rows.empty())||state_.rows.size()>10||state_.source.total_rows255||owner.postponed_offset<0|| owner.postponed_offset%10!=0||state_.source.page_offset!=owner.postponed_offset|| (state_.source.total_rows==0?owner.postponed_offset!=0: owner.postponed_offset>=state_.source.total_rows)|| state_.rows.size()!=size_t(state_.source.total_rows==0?0: std::min(10,state_.source.total_rows-owner.postponed_offset))))) throw FormatError("pairings: incomplete owned normal projection"); if(owner.computer_tip_active) { if(owner.postponed_active||owner.filter_active||owner.computer_tip_league!=owner.league|| owner.computer_tip_round!=owner.round||owner.computer_tip_results.size()!=state_.rows.size()|| (state_.source.preferences&0x10)==0) throw FormatError("pairings: incomplete Computer Tip projection"); for(size_t n=0;nrows) if(int(row.weather)*25+25>weather_.width||weather_.height<11) throw FormatError("pairings: Original weather atlas crop is out of bounds"); for(const auto& label:state_.buttons)if(label.empty())throw FormatError("pairings: missing Original button text"); (void)render(); // Validate every original atlas crop before exposing the screen. } LeaguePairingsScreen::LeaguePairingsScreen(const std::filesystem::path& data,LeaguePairingsState state,FamilyMode family) :LeaguePairingsScreen(data,std::move(state)) { if(family.identity.manager!=state_.identity.manager||family.identity.runtime_club!=state_.identity.runtime_club|| family.identity.club_identity!=state_.identity.club_identity) throw FormatError("pairings: mismatched family identity"); family_=std::move(family); } void LeaguePairingsScreen::text(Image& out,int x,int baseline,std::string_view value,uint8_t color) const { const int width=font_.text_width(value); if(x<0||x+width>out.width||baseline<5||baseline>=out.height)return; Image mask(std::max(width,1),font_.height);font_.draw(mask,0,0,value,1); int index=0;while(index<32&&exe_.u8(::bmh::layout::Advertising_Text_RuntimeField2 + uint32_t(index))!=color)++index; for(int yy=0;yyweather)*25,0,293,23+int(n)*15,25,11); const int temperature_x=305-font_.text_width(row.temperature)/2; text(out,temperature_x,34+int(n)*15,row.temperature,0x62); } text(out,125,y,exe_.cstring(::bmh::layout::LeaguePairings_Fill_TextPointer),0x68); for(int side=0;side<2;++side) { text(out,5+side*127,y,row.names[size_t(side)],row.name_colors[size_t(side)]); text(out,3+side*130,y+6,row.details[size_t(side)],0x68); } if(!state_.source.owner.postponed_active&&!row.scheduled_ordinal.empty())text(out,220,y,row.scheduled_ordinal,0x5f); const int result_x=!row.scheduled_date.empty()?319-font_.text_width(row.scheduled_date):row.result_x; text(out,result_x,y,row.scheduled_date.empty()?row.result:row.scheduled_date,0x5f);text(out,263,y+7,row.first_leg,0x69); } for(int n=0;n<8;++n) { const int x=1+(n/4)*74,y=176+(n%4)*13; const bool selected=pressed_button_==n||(state_.source.preferences&(1u<20) {const int amount=std::min(8,70-used-20); copy(out,widgets_,24,sy+10,dx+used,dy,amount,10);used+=amount;} const int left=70-used;copy(out,widgets_,45-left,sy+20,dx+used,dy,left,10); const auto label=original_to_utf8(state_.buttons[size_t(n)]); sprite_.draw(out,dx+35-sprite_.text_width(label)/2,dy+7,label,exe_.u8(::bmh::layout::Advertising_Button_RuntimeField + uint32_t(selected))); } selector(out,148,176,2);selector(out,205,176,3);selector(out,262,176,state_.source.owner.league_group); int league=state_.source.owner.league_group*5; for(int r=0;r<4;++r) { const int count=exe_.u8(::bmh::layout::CalendarLeagueCounts + uint32_t(state_.source.owner.league_group*4+r)); const int y=exe_.u8(::bmh::layout::LeaguePairings_Fill_RuntimeField)+exe_.u8(::bmh::layout::LeaguePairings_Fill_RuntimeField2)*r; for(int n=0;n=0)copy(out,arrows_,136+pressed_arrow_*26,0,149+pressed_arrow_*26,182,26,38); return out; } int LeaguePairingsScreen::button_at(int x,int y) const { for(int n=0;n<8;++n) {const int bx=1+n/4*74,by=176+n%4*13; if(x>=bx&&x<=bx+70&&y>=by&&y<=by+10)return n;}return -1; } int LeaguePairingsScreen::arrow_at(int x,int y) const { if(y>=182&&y<=220)for(int n=0;n<2;++n)if(x>=149+n*26&&x<=175+n*26)return n; return -1; } bool LeaguePairingsScreen::in_arrow(int arrow,int x,int y) const { return arrow>=0&&arrow<2&&x>=149+arrow*26&&x<=175+arrow*26&&y>=182&&y<=220; } void LeaguePairingsScreen::post(LeaguePairingsEvent e) {pending_=LeaguePairingsRequest{state_.identity,e};rejection_.clear();} void LeaguePairingsScreen::post_family(LeagueFamilyRequest request) { if(!family_||pending_family_action_)return; request.identity=family_->identity;request.from_child=2; pending_family_action_=std::move(request);rejection_.clear(); } bool LeaguePairingsScreen::tick_source_seconds(uint64_t now) { // Move toggles local28 only for param1==0. Phase/keyboard callers // carry their nonzero Source minute through the existing family identity. if(family_&&family_->identity.source_param2!=0)return false; if(pending_||pressed_button_>=0||pressed_arrow_>=0||!state_.active_packet||!last_presented_source_seconds_) return false; const uint64_t last=*last_presented_source_seconds_; if(now<=last||now-last<=1)return false; post({LeaguePairingsAction::presentation_redraw,state_.source.owner.active_phase?0:1}); return true; } void LeaguePairingsScreen::mark_presented_source_seconds(uint64_t now) { last_presented_source_seconds_=now; } std::optional LeaguePairingsScreen::selector_event(int x,int y) const { if(pending_)return std::nullopt; if(x>=205&&x<=260&&y>=176&&y<=216) { auto image=render();if(image.at(x,y)==0x19||image.at(x,y)==0x42)return std::nullopt; const int r=(y-exe_.u8(::bmh::layout::LeaguePairings_SelectorEvent_RuntimeField2))/exe_.u8(::bmh::layout::LeaguePairings_SelectorEvent_RuntimeField3);if(r<0||r>=4)return std::nullopt; const bool group=state_.source.owner.league_group; int column=0,league=0; if(!group) { const int count=exe_.u8(::bmh::layout::CalendarLeagueCounts + uint32_t(r));int left=233-count*4; while(columnleft+8)){++column;left+=8;} league=exe_.u8(::bmh::layout::CalendarFirstLeague + uint32_t(r))+column; } else { column=(exe_.u8(::bmh::layout::LeaguePairings_SelectorEvent_RuntimeField + uint32_t(r))*4+x-233)/8; league=exe_.u8(::bmh::layout::CareerSession_FormatError_RuntimeField4)+r+column; } if(league<0||league>=9)return std::nullopt; return LeaguePairingsEvent{LeaguePairingsAction::select_league,league}; } if(x>=262&&x<=317&&y>=176&&y<=216&&state_.secondary_leagues) { return LeaguePairingsEvent{LeaguePairingsAction::toggle_group}; } return std::nullopt; } bool LeaguePairingsScreen::pointer_down(int x,int y,bool right) { if(pending_||pending_family_action_)return false; if(right) { pressed_button_=pressed_arrow_=-1; if(family_)post_family({{},LeagueFamilyRequestKind::close,2,0}); else post({LeaguePairingsAction::close}); return true; } if(y>21&&y120?row.away:row.home; const auto& owner=state_.source.owner; const auto action=owner.filter_active?LeaguePairingsAction::filter_club: owner.search_first==-1?LeaguePairingsAction::team_info:LeaguePairingsAction::search_team; post({action, team.runtime_team,team.club_identity});return true; } if(const auto event=selector_event(x,y)) { // InLeaguePairings leaves CareerSession_BeginFriendlyOpponentTable_RuntimeField2 unchanged for the current league;Move does // not redraw or reset its clock. Consume without a pending action. if(event->kind==LeaguePairingsAction::select_league && event->value==state_.source.owner.league)return false; post(*event);return true; } // Source outer arrow bounds precede the per-arrow held/release helper. if(x>147&&x<203&&y>175&&y<226) { pressed_arrow_=x>175?1:0;pressed_button_=-1; if(!in_arrow(pressed_arrow_,x,y))pressed_arrow_=-1; return true; } pressed_button_=button_at(x,y);pressed_arrow_=-1;return pressed_button_>=0; } bool LeaguePairingsScreen::move(int x,int y) { if(pressed_button_>=0&&button_at(x,y)!=pressed_button_) {pressed_button_=-1;return true;} if(pressed_arrow_>=0&&!in_arrow(pressed_arrow_,x,y)) {pressed_arrow_=-1;return true;}return false; } bool LeaguePairingsScreen::release(int x,int y) { if(pending_||pending_family_action_)return false; const int button=pressed_button_,arrow=pressed_arrow_;pressed_button_=pressed_arrow_=-1; if(button>=0&&button_at(x,y)==button) { if(family_&&(button==6||button==7)) { post_family({{},LeagueFamilyRequestKind::navigate,2,uint8_t(button==6?1:3)}); return true; } if(button==5)return true; // Source ellipsis has no action. // OpenBMH deviates from the original: the computer tip is ignored silently once a visible result is known, instead of raising an engine error. if(button==4&&std::any_of(state_.source.rows.begin(),state_.source.rows.end(), [](const auto& row){return row.raw_result!=0xcc;}))return true; post({button==0?LeaguePairingsAction::reset:button==1?LeaguePairingsAction::search: button==2?LeaguePairingsAction::filter_by_club:button==3?LeaguePairingsAction::postponed_list: button==4?LeaguePairingsAction::computer_tip:LeaguePairingsAction::unsupported,button});return true; } if(in_arrow(arrow,x,y)) {post({arrow?LeaguePairingsAction::next:LeaguePairingsAction::previous});return true;} return button>=0||arrow>=0; } void LeaguePairingsScreen::reject_action(std::string reason) { if(!pending_||reason.empty())throw FormatError("pairings: no pending rejection"); pending_.reset();pressed_button_=pressed_arrow_=-1;rejection_=std::move(reason); } void LeaguePairingsScreen::reject_family_action(std::string reason) { if(!family_||!pending_family_action_||reason.empty())throw FormatError("pairings: no pending family rejection"); pending_family_action_.reset();pressed_button_=pressed_arrow_=-1;rejection_=std::move(reason); } std::string LeaguePairingsScreen::state_json() const { std::ostringstream out;const auto& id=state_.identity; out<<"{\"screen\":\"league_pairings\",\"manager\":"<rows.size():0) <<",\"last_presented_source_seconds\":"; if(last_presented_source_seconds_)out<<*last_presented_source_seconds_;else out<<"null"; out <<",\"pending\":"<<(pending_?"true":"false")<<",\"last_rejection\":"<referee_name)) <<",\"active_referee_profile\":"<referee_profile) <<",\"active_weather\":"<weather) <<",\"active_temperature\":"<temperature); if(r.computer_tip_result)out<<",\"computer_tip_result\":"<