// SPDX-License-Identifier: GPL-3.0-only #include "formats/executable_layout.hpp" #include "alltime_table.hpp" #include #include "game/table.hpp" #include "ui/manager.hpp" #include "ui/setup.hpp" namespace bmh::ui { namespace { constexpr uint32_t kFirst=::bmh::layout::First, kLeague=::bmh::layout::AlltimeTable_Layout_ExecutableField; std::string original_text(const TextDb& t,uint32_t at){if(at<::bmh::layout::TextPointerTable||((at-::bmh::layout::TextPointerTable)&3U))throw FormatError("alltime: invalid TEXTD address");return original_to_utf8(t[(at-::bmh::layout::TextPointerTable)/4]);} void fill(Image&o,int x,int y,int w,int h,uint8_t c){for(int j=0;js.width||sy+h>s.height||x+w>o.width||y+h>o.height)throw FormatError("alltime: Original asset bounds");for(int j=0;j3; unsigned separators=0; if(grouped){for(size_t p=digits.size();p>3;p-=3){digits.insert(p-3,1,'.');++separators;}} const unsigned groups=grouped?(width-1)/3:0;const unsigned field=width+separators; if(digits.size()separators)digits.insert(0,groups-separators,' ');return digits; } uint8_t team_color(const GameState&g,int team){size_t count=std::min(g.manager_teams.size(),4);int active=g.memory.has(::bmh::layout::ActiveManager)?g.memory.u8(::bmh::layout::ActiveManager):-1;int managers=g.memory.has(::bmh::layout::ManagerCount)?g.memory.u8(::bmh::layout::ManagerCount):int(count);bool current=g.memory.has(::bmh::layout::DeskPosition)&&g.memory.u8(::bmh::layout::DeskPosition)=g.manager_records.size()||g.manager_records[at]==0)continue;if(g.manager_teams[slot]==team)return int(slot)==active&¤t?0x7a:0x75;}if(g.memory.has(::bmh::layout::AlltimeTable_TeamColor_RuntimeField)&&g.memory.u16(::bmh::layout::AlltimeTable_TeamColor_RuntimeField)==team)return 0xf7;return 0x5f;} void button(Image&out,const Image&a,const SpriteFont&spr,const LeImage&exe,int x,int y,std::string_view label,bool selected){constexpr int width=96,height=10;int dx=x+(selected?2:0),dy=y+(selected?2:0);if(selected){auto bg=out.at(x,y+height);fill(out,x,y,width+1,2,bg);fill(out,x,y,2,height+1,bg);}else{fill(out,x+2,y+height,width-1,2,0x40);fill(out,x+width,y+2,2,height-2,0x40);}int sy=selected?44:74;copy(out,a,24,sy,dx,dy,31,height,0xc0);int used=31;while(width-used>20){int count=std::min(8,width-used-20);copy(out,a,24,sy+height,dx+used,dy,count,height,0xc0);used+=count;}int tail=width-used;copy(out,a,45-tail,sy+2*height,dx+used,dy,tail,height,0xc0);spr.draw(out,dx+width/2-spr.text_width(label)/2,dy+height/2+2,label,exe.u8(::bmh::layout::Advertising_Button_RuntimeField + uint32_t(selected)));} } AllTimeTableState alltime_table_from_game(const GameState&g,const LeImage&exe,DeskRequest owner,const Image&underlay,int nation,bool relative){ if(nation<0||nation>4)throw FormatError("alltime: nation outside 0..4");if(underlay.width!=320||underlay.height!=240)throw FormatError("alltime: parent underlay must be 320x240");AllTimeTableState s;s.owner=owner;s.underlay=underlay;s.nation=nation;s.relative=relative;s.mask=uint8_t((1U<=168)continue;const auto&r=s.records[size_t(team)];int played=int(r.wins)+r.draws+r.losses;if(!played)continue;uint32_t p=r.points,gf=r.goals_for,ga=r.goals_against;if(relative){p=p*full*10U/uint32_t(played);gf=gf*full*10U/uint32_t(played);ga=ga*full*10U/uint32_t(played);}AllTimeRow row;row.team=team;row.played=played;row.points=p;row.goals_for=gf;row.goals_against=ga;row.points_key=p*0x10001U+0x7d000000U;row.goal_key=gf*0x10001U-ga*0x10000U+0x7d000000U;s.rows.push_back(row);}} std::stable_sort(s.rows.begin(),s.rows.end(),[](const auto&a,const auto&b){if(a.points_key!=b.points_key)return a.points_key>b.points_key;return a.goal_key>b.goal_key;});if(s.rows.size()>24)s.rows.resize(24);return s; } AllTimeTableScreen::AllTimeTableScreen(const std::filesystem::path&data,AllTimeTableState s):state_(std::move(s)),data_(data),exe_(load_le(asset_path(data,"BMH.exe"))),font_(load_font(asset_path(data/"F","BMH3.FNT"))),header_(load_font(asset_path(data/"F","BMH2.FNT"))),sprite_(data),window_(load_vga(asset_path(data/"KB","56.VGA"))),buttons_(load_vga(asset_path(data/"KB","0.VGA"))),texts_(load_textd(asset_path(data,"TEXTD.BLK"))){if(state_.underlay.width!=320||state_.underlay.height!=240)throw FormatError("alltime: invalid parent frame");} Image AllTimeTableScreen::render()const{Image out=state_.underlay; if(state_.has_data){int spacing=state_.rows.size()<15?8:state_.rows.size()>19?6:7;for(size_t i=0;i=bx&&x<=bx+96&&y>=by&&y<=by+10){pressed_=i;return true;}}return false;} bool AllTimeTableScreen::release(int x,int y){if(pending_)return false;int i=pressed_;pressed_=-1;if(i<0)return false;int bx=12+(i%3)*99,by=177+(i/3)*12;if(xbx+96||yby+10)return true;if(i<5){state_.mask=uint8_t((state_.mask&0x20)^(1U<