// SPDX-License-Identifier: GPL-3.0-only #include "game/reality_duel.hpp" #include #include namespace bmh { namespace { int source_manager(const GameState& state,int team) { const int m=state.manager_of(team);if (m>=0) return m; if (!state.manager_teams.empty() && state.manager_teams[0]==kNoTeam && state.manager_records.size()>=4*0xb0c+0x292 && le16(state.manager_records,4*0xb0c+0x290)==team) return 4; return -1; } } RealityFoulResult reality_foul(const GameState& state,const RealitySetup& scene,int offender,bool defending, int ball_actor,uint8_t referee,bool muted,WatcomRandom& rand,const RealityForcedEvent& forced_event, const RealitySound& sound) { if (offender<0 || offender>=22 || ball_actor<0 || ball_actor>=22) throw FormatError("Reality foul: invalid figure"); const auto& placement=scene.players[size_t(offender)];const int slot=placement.squad_slot; if (slot<0 || size_t(slot)>=state.squad_slots.size()) throw FormatError("Reality foul: missing Source squad slot"); const int player=state.squad_slots[size_t(slot)]&0xfff; if (player==0xfff || size_t(player)>=state.players.size()) throw FormatError("Reality foul: missing Source player"); const int team=scene.teams[size_t(offender/11)],manager=source_manager(state,team); const auto& p=state.players[size_t(player)]; int chance=std::clamp((int((p.w35c>>14)&127)-int((p.w35c>>7)&127))*4+100,0,200); // Literal RealityFoul indexes by player ID minus squad start, not roster row. const int board_row=std::clamp(player-int(squad_range(team).first),0,24); if (manager>=0) { const size_t at=size_t(manager)*0x840+size_t(board_row)*0x58+5; if (at>=state.tactics.size()) throw FormatError("Reality foul: Source board row outside supplied memory"); chance=(int(state.tactics[at])+chance)*100/239; } RealityFoulResult result;if (rand.range(0,100)>=chance) return result; result.cues.push_back({ball_actor,defending ? 19 : 18}); result.whistle=rand.range(0,1)==0 && !muted; if (result.whistle && sound) sound(21); // RealityFoul, before the forced event. result.result=2; if (rand.range(0,(referee&3)+1)==0) return result; result.cues.push_back({-1,20}); // RealityFoul does not assign MatchEngine_Minute_ExecutableField2 for this cue. if (!forced_event) throw FormatError("Reality foul: real forced card/injury engine callback required"); result.event_result=forced_event(slot,offender>10); result.event_kind=result.event_result&0x7f; result.result=3; if (result.event_result) { result.cues.push_back({defending ? offender : ball_actor,21});result.event_stops_scene=true; } if (!defending) result.scene_status=4; else { const auto ball=scene.grid.ball();const int distance=42-std::abs(scene.attacking_side*42-ball.first); result.restart=distance<7 && ball.second>3 && ball.second<21 ? 2 : 1; } return result; } RealityDribbleDecision continue_reality_dribble(const GameState& state,RealitySetup& scene,const LeImage& exe,const RealityBallDecision& decision, uint8_t referee,bool muted,WatcomRandom& rand,const RealityForcedEvent& forced_event, const RealitySound& sound) { if (decision.boundary!=RealityBallBoundary::dribble_or_duel || decision.actor<0 || decision.actor>=22 || decision.side!=scene.attacking_side) throw FormatError("Reality dribble: invalid continuation"); RealityDribbleDecision result;result.ball=decision;result.ball.action=2;result.ball.airborne=0; result.ball.receiver_offside=false; if (decision.speed_ability