// SPDX-License-Identifier: GPL-3.0-only #include "formats/executable_layout.hpp" #include "game/reality_reporter.hpp" #include #include #include "game/messages.hpp" namespace bmh { namespace { struct Node { char token = 0; int selector = 0, variants = 0; std::string literal; std::vector> branches; }; // Parse before evaluation: structural traversal cannot draw random numbers. // The Source cursor skips entire unselected nested %x...#x expressions. std::vector parse(std::string_view text,size_t& at,int depth) { if(depth>32)throw FormatError("Reality reporter: template nesting too deep"); std::vector nodes; while(at=text.size())throw FormatError("Reality reporter: truncated token"); n.token=text[at++]; if(n.token=='x') { if(at+2>text.size()||text[at]<'0'||text[at]>'9'||text[at+1]<'0'||text[at+1]>'9') throw FormatError("Reality reporter: invalid selector digits"); n.selector=text[at]-'0';n.variants=text[at+1]-'0';at+=2; for(;;) { n.branches.push_back(parse(text,at,depth+1)); if(at>=text.size()||text[at++]!='#')throw FormatError("Reality reporter: missing branch terminator"); if(at=pool.size())throw FormatError("Reality reporter: missing referee name"); std::string out;for(;offset22)throw FormatError("Reality reporter: missing selected figure"); return scene.players[size_t(one_based-1)].player; } std::string evaluate(const std::vector& nodes,const GameState& game,const RealitySetup& scene, const LeImage& exe,const RealityReporterFrame& frame,const RealityReporterState& state,WatcomRandom& rand) { std::string out; for(const auto& node:nodes) { if(!node.token)out+=node.literal; else if(node.token=='x') { const int index=node.selector+10*node.variants; if(node.selector && index>=int(state.selectors.size()))throw FormatError("Reality reporter: selector outside Source context"); const int choice=node.selector?state.selectors[size_t(index)]:rand.range(1,node.variants); const size_t branch=size_t(choice<2?0:choice-1); // Source walks to #x without output when the selector exceeds the branches. if(branch20)value=uint8_t((value/5)*5); out+=format_number(game,value,1); } // All other Source percent tokens are consumed without output. if(out.size()>=500)throw FormatError("Reality reporter: expanded text exceeds Source buffer"); } return out; } } // namespace RealityReporterState initialize_reality_reporter(const LeImage& exe) { RealityReporterState state; for(size_t i=0;i1) throw FormatError("Reality reporter: template needs its actual Source player side"); size_t at=0;const auto nodes=parse(text,at,0); return evaluate(nodes,game,scene,exe,frame,state,rand); } RealityReporterResult report_reality(const GameState& game,const RealitySetup& scene,const LeImage& exe, RealityReporterFrame& frame,RealityReporterState& state,int figure,int side,uint8_t code,WatcomRandom& rand) { RealityReporterResult out; if(figure==100){state.last_side=state.last_owner=state.last_player=state.last_action=-1;return out;} if(figure<0||figure>=22||side<0||side>1||frame.player_side>1||code>21) throw FormatError("Reality reporter: invalid figure/side/cue"); state.current_player=scene.players[size_t(figure)].player; state.selectors[12]=frame.card_kind; auto cue=[&](int value){out.codes.push_back(uint8_t(value));}; if(code)cue(code); else { const int area=reality_penalty_area(scene,exe,1-side,scene.grid.ball().first,scene.grid.ball().second); const auto ball=scene.grid.ball(),old=scene.grid.previous_ball(); // InitializeRealityReporter uses signed chars for both differences; preserve byte wrapping. const int dx=int8_t(std::abs(ball.first-old.first)),dy=int8_t(std::abs(ball.second-old.second)); if(dy<=dx&&!frame.incoming_bh)throw FormatError("Reality reporter: missing Source incoming BH"); const int high=dy>dx?dy:int(int8_t(*frame.incoming_bh)); // BL stays dx even when dy is smaller (verified at 69210..6926d). const int low=dx; state.selectors[2]=uint8_t(2-frame.airborne); state.selectors[3]=uint8_t((high>2)+1+(high>9)*2); state.selectors[4]=uint8_t(3-2*(low>=10&&dy<=4)+(dy>16)); state.selectors[5]=uint8_t(2-area); state.selectors[6]=uint8_t(side==state.last_side?2-(frame.distance_before6)+1); state.selectors[8]=uint8_t((ball.first>42)+1+(ball.second<11||ball.second>13)); state.selectors[9]=frame.scene_status; state.selectors[11]=uint8_t(2-int(frame.received)); if(low>7&&state.selectors[4]==3)state.selectors[4]=2; if(frame.action==2&&frame.opponent)frame.action=uint8_t(frame.lost_duel?9:10); if(frame.action==0&&frame.received&&frame.distance_before<6&&rand.range(0,1)!=0)frame.action=16; if(!(frame.action!=0&&frame.action==state.last_action&&state.current_player==state.last_player)) { if(state.last_owner!=-1&&state.current_player!=state.last_owner){cue(7);state.selectors[1]=2;state.last_player=state.current_player;} state.selectors[1]=uint8_t(state.current_player==state.last_player&&state.selectors[1]!=1?1:2); if(frame.action==0&&frame.distance_before>34)frame.action=8; cue(frame.action);state.last_owner=state.current_player; if(frame.action==0&&frame.intercepted)cue(13); if(frame.action==1) { state.last_owner=frame.receiver_player; if(frame.blocked_attempt) { if(!frame.inaccurate&&frame.line_selected[size_t(1-frame.player_side)])cue(6); else if(frame.inaccurate&&rand.range(0,2)==0)cue(4); } if(!frame.inaccurate&&!frame.line_selected[size_t(1-frame.player_side)]&&frame.offside&&int(frame.distance_after)=22)throw FormatError("Reality reporter: missing pass target figure"); const auto target=scene.grid.position(frame.receiver); const int enemy=scene.grid.occupant(target.first,target.second,1-side); if(enemy){frame.opponent=uint8_t(enemy);cue(14);} } } if(frame.scene_status)cue((frame.scene_status==3)+11); } } if(code&&state.last_action==9){out.suppressed=true;return out;} // %a/%b are fixed home/away afaf2/afaf4, independent of possession side. for(uint8_t id:out.codes)out.text.push_back(reality_reporter_text(game,scene,exe,frame,state,exe.string_at_pointer(::bmh::layout::RealityReporter_FormatError_TextPointer+uint32_t(id)*4),rand)); state.last_action=frame.action;state.last_player=state.current_player; if(frame.action==9){state.last_owner=player(scene,frame.opponent);state.last_player=state.last_owner;} state.last_side=side; return out; } } // namespace bmh