// SPDX-License-Identifier: GPL-3.0-only #include "formats/badge_policy.hpp" #include "formats/executable_layout.hpp" #include "ui/cup_draw.hpp" #include #include #include #include #include #include "game/cup.hpp" #include "game/rating.hpp" #include "game/messages.hpp" #include "game/match.hpp" #include "game/schedule.hpp" #include "game/state.hpp" #include "game/season.hpp" #include "ui/manager.hpp" namespace bmh::ui { namespace { constexpr std::array kTierSourceX{0,16,32,32,24}; constexpr int kCupOnlyTeams=30; bool valid_rect(const Image& dst,const Image& src,int sx,int sy,int x,int y,int w,int h) { return w>0&&h>0&&sx>=0&&sy>=0&&x>=0&&y>=0&&sx+w<=src.width&&sy+h<=src.height&& x+w<=dst.width&&y+h<=dst.height; } void copy(Image& dst,const Image& src,int sx,int sy,int x,int y,int w,int h) { // FormatError2 passes ff as the VGA plane bitmask. The copy is opaque and an // out-of-bounds rectangle is rejected in full by SourceMaskCopy. if(!valid_rect(dst,src,sx,sy,x,y,w,h))return; for(int yy=0;yy=0&&xx=0&&y-1=0&&yy=0&&x-1=0&&xx=0&&y+38=0&&yy=0&&x+38=5?league-5+1:0;} if(t-kCupOnlyTeams>=0)return (t-kCupOnlyTeams)/7-5+10; return 0; } int league_for_team(int id) { return id>0x164?0:team_league(id); } uint8_t manager_color(const GameState& game,int id,uint8_t base) { const int active=game.memory.has(::bmh::layout::ActiveManager)?game.memory.u8(::bmh::layout::ActiveManager):-1; const int count=game.memory.has(::bmh::layout::ManagerCount)?game.memory.u8(::bmh::layout::ManagerCount):int(game.manager_teams.size()); const bool active_context=game.memory.has(::bmh::layout::DeskPosition)&&game.memory.u8(::bmh::layout::DeskPosition)(4,game.manager_teams.size());++slot) { const size_t rec=slot*0xb0c+0xa8; if(rec>=game.manager_records.size()||!game.manager_records[rec])continue; if(game.manager_teams[slot]==id)return int(slot)==active&&active_context?0x7a:0x75; } if(game.memory.has(::bmh::layout::AlltimeTable_TeamColor_RuntimeField)&&game.memory.u16(::bmh::layout::AlltimeTable_TeamColor_RuntimeField)==id)return 0xf7; return base; } std::string source_field(int value,int width) { std::string result=std::to_string(value); if(int(result.size())=texts.strings.size())throw FormatError("cup draw: missing Source TEXTD entry"); return original_to_utf8(texts[i]); } } CupDrawProjection cup_draw_from_game(const GameState& game,const SeasonCupDrawEvent& event, const LeImage& exe,const TextDb& texts) { if(event.entry_address64||event.participants.size()6) throw FormatError("cup draw: invalid Source event"); const uint32_t entry=event.entry_address; const uint8_t flags=event.flags?event.flags:game.memory.u8(entry+0x14); const uint8_t qualifier=game.memory.u8(entry+0x1c); int round_index=((flags&4)?event.round:0)+1+qualifier; if(qualifier==1&&event.round==0)--round_index; if(round_index<0||round_index>31)throw FormatError("cup draw: invalid Source round text index"); CupDrawProjection out; out.phase=event.phase;out.entry_address=event.entry_address;out.competition=event.competition; out.round=event.round;out.offset=event.offset;out.count=event.count; out.flags=flags;out.group_stage=event.group_stage;out.group_index=event.group_index;out.group_count=event.group_count; out.heading=text_at(texts,size_t((::bmh::layout::CupDraw_FormatError_ExecutableField4-::bmh::layout::TextPointerTable)/4)+size_t(round_index)); out.footer=text_at(texts,size_t((::bmh::layout::CupDraw_FormatError_ExecutableField6-::bmh::layout::TextPointerTable)/4)+size_t(event.competition)); if(out.group_stage) { out.group_round_suffix=text_at(texts,size_t((::bmh::layout::CupDraw_FormatError_TextPointer6-::bmh::layout::TextPointerTable)/4)); const size_t round_digit_index=size_t((::bmh::layout::Calendar_Information_TextPointer2-::bmh::layout::TextPointerTable)/4); const size_t group_digit_index=size_t((::bmh::layout::CupDraw_FormatError_ExecutableField5-::bmh::layout::TextPointerTable)/4); std::string round_digit=text_at(texts,round_digit_index); std::string group_digit=text_at(texts,group_digit_index); if(round_digit.size()>16)round_digit[16]=char('0'+event.group_index+1); if(group_digit.size()>25) group_digit[25]=char(event.phase==SeasonCupDrawPhase::draw?'0'+event.group_index+1: (event.group_index==0?'1':'0'+event.group_index)); out.group_caption=group_digit; const size_t heading_index=size_t((::bmh::layout::CupDraw_FormatError_ExecutableField4-::bmh::layout::TextPointerTable)/4)+size_t(round_index); const size_t footer_index=size_t((::bmh::layout::CupDraw_FormatError_ExecutableField6-::bmh::layout::TextPointerTable)/4)+size_t(event.competition); if(heading_index==round_digit_index)out.heading=round_digit; else if(heading_index==group_digit_index)out.heading=group_digit; if(footer_index==round_digit_index)out.footer=round_digit; else if(footer_index==group_digit_index)out.footer=group_digit; const auto& form=texts[72]; for(size_t i=0;i=4&&event.competition<=7)|| (game.memory.has(::bmh::layout::CompetitionSelection)&&(game.memory.u32(::bmh::layout::CompetitionSelection)&1)); if(game.memory.has(::bmh::layout::CupDraw_FormatError_RuntimeField)) { const int speed=game.memory.u8(::bmh::layout::CupDraw_FormatError_RuntimeField); out.source_option_delay_ms=speed>=0&&speed<=5?(speed/2)*20:speed*20; } out.clubs.reserve(event.count); for(size_t i=0;i=game.club_heap_78.size())throw FormatError("cup draw: missing Source badge selector"); const uint16_t selector=uint16_t(game.club_heap_78[size_t(id)*2]|uint16_t(game.club_heap_78[size_t(id)*2+1])<<8); const uint8_t league=uint8_t(std::clamp(league_for_team(id),0,255)); const uint8_t base=event.group_stage?0x6a:(region_for_team(id)?0x67:0x5f); const uint8_t color=manager_color(game,id,base); const uint8_t reveal_color=color==base?0:0x27; std::string context; if(event.group_stage&&event.phase==SeasonCupDrawPhase::overview) { const bool human_national=game.manager_of(id)>=0|| (game.memory.has(::bmh::layout::CupDraw_FormatError_RuntimeField2)&&game.memory.has(::bmh::layout::CupDraw_FormatError_RuntimeField3)&&game.memory.u16(::bmh::layout::CupDraw_FormatError_RuntimeField2)==0x165&&game.memory.u16(::bmh::layout::CupDraw_FormatError_RuntimeField3)==id); if(id>=168&&!human_national) { if(size_t(id-168)>=game.foreign_strength.size())throw FormatError("cup draw: missing Source foreign-strength byte"); const uint8_t strength=game.foreign_strength[size_t(id-168)]; const unsigned band=strength>>5; context=text_at(texts,size_t((::bmh::layout::CupDraw_FormatError_TextPointer4-::bmh::layout::TextPointerTable)/4)+band)+original_to_utf8(exe.cstring(::bmh::layout::CupDraw_FormatError_TextPointer)); for(unsigned bit=0;bit<3;++bit) context+=original_to_utf8(exe.cstring(::bmh::layout::CupDraw_FormatError_ExecutableField3+(((strength>>(3-bit))&1u)*2u))); context+=original_to_utf8(exe.cstring(::bmh::layout::CupDraw_FormatError_ExecutableField)); } else { std::array values{}; if(id>=168&&id!=kNoTeam) { const auto snapshot=std::find_if(event.group_average_snapshots.begin(),event.group_average_snapshots.end(), [&](const auto& v){return v.runtime_id==id;}); if(snapshot==event.group_average_snapshots.end()) throw FormatError("cup draw: missing live TeamRating group-average snapshot"); values=snapshot->values; } else { values={team_average(game,id,0x0f1).value,team_average(game,id,0x0f2).value, team_average(game,id,0x0f4).value,team_average(game,id,0x0f8).value, team_average(game,id,0x0ff).value}; } context=original_to_utf8(exe.cstring(::bmh::layout::CupDraw_FormatError_ExecutableField2)); for(size_t i=0;i1) for(size_t i=0;i badges; struct Op { bool copy; std::array rect; int token; }; std::vector ops; std::vector revealed; size_t op_index=0,total_revealed=0; Impl(const std::filesystem::path& d,CupDrawProjection projection) :p(std::move(projection)),data(d),exe(load_le(asset_path(d,"BMH.exe"))), panel(load_vga(asset_path(d/"KB","67.VGA"))),background(load_vga(asset_path(d/"KB","56.VGA"))), widgets(load_vga(asset_path(d/"KB","0.VGA"))),tier(load_vga(asset_path(d/"KB","1.VGA"))), regions(load_vga(asset_path(d/"KB","24.VGA"))), animation(load_vga(asset_path(d/"KB","74.VGA"))),texts(load_textd(asset_path(d,"TEXTD.BLK"))), sprite(d),font(load_font(asset_path(d/"F","BMH3.FNT"))) { if(panel.width<272||panel.height<203||background.width<318||background.height<207|| p.count>64||p.clubs.size()>3;if(!badges.contains(atlas))badges.emplace(atlas,load_vga(asset_path(data/"W",std::to_string(atlas)+".VGA"))); } } } Image make_base() const { Image out=p.decorated_background?load_vga(asset_path(data/"KB","129.VGA")):Image(320,240,0); if(!p.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);} return out; } void draw_header(Image& out,const std::string& heading,const std::string& footer) const { const int x=172+(282-172)/2-sprite.text_width(heading)/2; sprite.draw(out,x,13,heading,5);sprite.draw(out,5,235,footer,5); } void draw_gradient_text(Image& out,int x,int baseline,const std::string& name,uint8_t color) const { const int top=baseline-font.baseline,width=font.text_width(name); int ramp=0;while(ramp<32&&exe.u8(::bmh::layout::Advertising_Text_RuntimeField2Unsigned+uint32_t(ramp))!=color)++ramp; if(!p.gradient_text||ramp==32){font.draw(out,x,top,name,color);return;} Image mask(std::max(1,width),font.height,0);font.draw(mask,0,0,name,1); for(int y=0;y=0&&x+xx=0&&top+y>3;copy(out,badges.at(atlas),(selector&7)*38,0,x,y,38,38); } } void group_marker(Image& out,const CupDrawClub& c,int x,int y) const { if(c.league_tier<5) { const int source_x=exe.u8(::bmh::layout::CupDraw_GroupMarker_RuntimeField+c.league_tier); // Copy -> UsesForTheseSprites skips the atlas's transparent color DesktopRuntimeField. copy_key0(out,tier,source_x,0x26,x,y,16,5); } else { const int region=c.country; const int sx=(region%12)*8+region%12,sy=(region/12)*6; copy(out,regions,sx,sy,x,y,9,6); } } int club_x(int n) const {return p.group_stage?38:((n&1)?168:38);} int club_y(int n) const {return p.group_stage?193+n*7:193+(n/2)*7;} void draw_revealed() { // Source restores only the bottom strip from the pre-reveal snapshot. copy(drawing,bottom,29,182,29,182,254,34); for(size_t j=0;j=ops.size()){ if(script==0){script=1;begin_script();return true;} const size_t window=p.group_stage?4:8,drop=p.group_stage?1:2; if(revealed.size()>=window)revealed.erase(revealed.begin(),revealed.begin()+drop); revealed.push_back(size_t(club));++total_revealed;draw_revealed();present_frame(true,true);++club;script=0;script_started=false; if(size_t(club)>=p.count)complete=true;else poll=true; return true; } const auto op=ops[op_index++]; if(op.copy){apply_script_copy(op.rect[0],op.rect[1],op.rect[2],op.rect[3],op.rect[4],op.rect[5]);return true;} const bool visible=op.token<=100; present_frame(visible,false);option_delay=p.source_option_delay_ms;wait=visible?op.token*20:(op.token-100)*20;held=true;return true; } void draw_overview() { screen=make_base(); if(p.group_stage){draw_group_overview();return;} const int count=int(p.count),firstrow=(64-count)/2; const int baseline=firstrow*3+26; // CupGroupOverviewEffects(param3=300) keeps the compact list. Its LoadGameEndTaxAnimationAssets backing // rectangle follows the rows, even when fewer than 64 clubs remain: // y=baseline-7 .. baseline+6*(count/2)-5, sampling KB56 at y-19. if(p.original_table_panel&&count!=0){ const int top=baseline-7,bottom=baseline+6*(count/2)-5; copy(screen,background,0,top-19,1,top+1,318,bottom-top-1); for(int x=0;x<320;++x)screen.at(x,top)=0x46; for(int y=top;y<=bottom;++y)screen.at(0,y)=0x46; for(int x=0;x<320;++x)screen.at(x,bottom)=0x40; for(int y=top;y<=bottom;++y)screen.at(319,y)=0x40; } draw_header(screen,p.footer,p.heading); for(int pair=0;pair2) { const std::string round=source_field(p.round+1,1)+p.group_round_suffix; draw_gradient_text(screen,2,22,utf8_to_original(round),0x5f); } const int fixture_baseline=count==2?0x68:0x27; const int fixture_top=fixture_baseline-8; const int fixture_bottom=fixture_baseline+16*(count/2)-6; if(p.original_table_panel&&count>0) { copy(screen,background,1,fixture_top-19,1,fixture_top+1,318,fixture_bottom-fixture_top-1); for(int x=0;x<320;++x){screen.at(x,fixture_top)=0x46;screen.at(x,fixture_bottom)=0x40;} for(int y=fixture_top;y<=fixture_bottom;++y){screen.at(0,y)=0x46;screen.at(319,y)=0x40;} } const int rounds=int(p.group_fixtures.size()); for(int i=0;iconst CupDrawClub& { const auto it=std::find_if(p.clubs.begin(),p.clubs.end(),[&](const CupDrawClub& c){return c.runtime_id==id;}); if(it==p.clubs.end())throw FormatError("cup draw: fixture references a club outside its owned group"); return *it; }; const auto& home=find(pair[0]);const auto& away=find(pair[1]); draw_gradient_text(screen,5,baseline,utf8_to_original(home.original_name),home.name_color); draw_gradient_text(screen,132,baseline,utf8_to_original(away.original_name),away.name_color); draw_gradient_text(screen,125,baseline,utf8_to_original(exetext(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer)),0x68); group_marker(screen,home,115,baseline-5); group_marker(screen,away,242,baseline-5); if(!home.source_context.empty())draw_gradient_text(screen,3,baseline+7,utf8_to_original(home.source_context),0x68); if(!away.source_context.empty())draw_gradient_text(screen,133,baseline+7,utf8_to_original(away.source_context),0x68); } if(count>2) { const int top=125,bottom=count*7+126; if(p.original_table_panel) { copy(screen,background,1,top-19,1,top+1,318,bottom-top-1); for(int x=0;x<320;++x){screen.at(x,top)=0x46;screen.at(x,bottom)=0x40;} for(int y=top;y<=bottom;++y){screen.at(0,y)=0x46;screen.at(319,y)=0x40;} } for(size_t i=0;ioriginal_name),r.name_color); const std::string rank=source_field(r.rank,2)+exetext(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer2); const std::string games=source_field(r.games,2)+exetext(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer4)+ source_field(r.wins,2)+exetext(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer5)+source_field(r.draws,2)+exetext(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer5)+ source_field(r.defeats,2)+exetext(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer4); std::string points=exetext(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer6); if(p.flags&0x80)points+=exetext(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer7); points+=source_field(r.points,2)+exetext(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer8); if(!(p.flags&0x80)) points+=exetext(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer9)+source_field(2*r.games-r.points,2)+exetext(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer8); std::string signed_diff; if(r.goals_for-r.goals_against<0)signed_diff=exetext(::bmh::layout::Bank_OriginalToUtf8_ExecutableField5); else if(r.goals_for-r.goals_against==0)signed_diff=exetext(::bmh::layout::Bank_OriginalToUtf8_ExecutableField7); else signed_diff=exetext(::bmh::layout::Bank_OriginalToUtf8_ExecutableField6); const std::string goals=source_field(r.goals_for,3)+exetext(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer9)+ source_field(r.goals_against,3)+exetext(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer10)+ signed_diff+source_field(std::abs(r.goals_for-r.goals_against),3)+exetext(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer11); draw_gradient_text(screen,1,baseline,utf8_to_original(rank),r.rank_color); if(p.previous_rank_visible) { const int old=r.previous_rank; draw_gradient_text(screen,12,baseline,utf8_to_original("("+source_field(old,2)+exetext(::bmh::layout::CupDraw_DrawGroupOverview_TextPointer3)), r.rank>(3*form))&7u; const unsigned glyph=(bits&4u)?(bits&3u):(bits&3u)+4u; if(!p.group_form_glyphs[glyph].empty()) draw_gradient_text(screen,207-form*6,baseline, utf8_to_original(p.group_form_glyphs[glyph]),0x6a); } draw_gradient_text(screen,206,baseline,utf8_to_original(points),0x6a); draw_gradient_text(screen,255,baseline,utf8_to_original(goals),0x6a); } } } void draw_overview_club(Image& out,const CupDrawClub& c,int x,int baseline) const { const std::string raw=utf8_to_original(c.original_name); const int width=font.text_width(raw);if(x+width<=out.width&&baseline-font.baseline>=0) draw_gradient_text(out,x,baseline,raw,c.name_color); if(c.league_tier<5)copy_key0(out,tier,kTierSourceX[c.league_tier],38,x+(x==2?88:90),baseline-5,16,5); } }; CupDrawScreen::CupDrawScreen(const std::filesystem::path& d,CupDrawProjection p) :impl_(std::make_shared(d,std::move(p))){} bool CupDrawScreen::advance_step(){auto& s=*impl_;s.present=false;s.flush=false;s.poll=false;s.held=false;s.option_delay=0;s.wait=0; if(s.complete)return false; if(s.p.phase==SeasonCupDrawPhase::overview){s.complete=true;return false;} if(s.initial<10){++s.initial;s.option_delay=s.p.source_option_delay_ms;s.poll=true; if(s.initial==10&&s.skip){s.aborted=true;s.complete=true;}return true;} if(s.aborted){s.complete=true;return false;} if(s.skip){s.aborted=true;s.complete=true;return false;} if(!s.initial_poll_done){s.initial_poll_done=true;s.poll=true;return true;} if(s.club>=int(s.p.count)){s.complete=true;return false;} if(!s.script_started){s.script_started=true;s.script=0;s.begin_script();} return s.process_script(); } bool CupDrawScreen::present_needed()const{return impl_->present;} bool CupDrawScreen::queue_flush_needed()const{return impl_->flush;} int CupDrawScreen::option_delay_milliseconds()const{return impl_->option_delay;} int CupDrawScreen::wait_milliseconds()const{return impl_->wait;} bool CupDrawScreen::input_poll_needed()const{return impl_->poll;} bool CupDrawScreen::held_mouse_check_needed()const{return impl_->held;} bool CupDrawScreen::reveal_complete()const{return impl_->complete&&impl_->p.phase==SeasonCupDrawPhase::draw&&!impl_->aborted;} bool CupDrawScreen::reveal_aborted()const{return impl_->aborted;} size_t CupDrawScreen::revealed_count()const{return impl_->total_revealed;} bool CupDrawScreen::source_input(uint8_t code){auto&s=*impl_;if(!s.poll)return false;if(code==2||code==8){ if(s.initial<10)s.skip=true;else{s.aborted=true;s.complete=true;}return true;}return false;} bool CupDrawScreen::source_held_mouse(uint8_t mask){auto&s=*impl_;if(!s.held)return false;if(mask&3){s.aborted=true;s.complete=true;s.club=int(s.p.count);s.poll=false;s.held=false;return true;}return false;} Image CupDrawScreen::render()const{return impl_->screen;} std::optional CupDrawScreen::hit_test_team(int x,int y)const { const auto&s=*impl_;if(s.p.phase!=SeasonCupDrawPhase::overview)return{}; if(s.p.group_stage) { for(size_t i=0;i=baseline)continue; const auto pair=s.p.group_fixtures[i]; if(x>0&&x<127)return pair[0]; if(x>125&&x<319)return pair[1]; } return{}; } const int first=(64-int(s.p.count))/2,base=first*3+26; for(size_t pair=0;pair=b)continue; if(x>0&&x<102)return s.p.clubs[pair*2].runtime_id; if(x>103&&x<319)return s.p.clubs[pair*2+1].runtime_id; } return{}; } } // namespace bmh::ui