fork/slippc
git clone https://git.julia.blue/fork/slippc
c826ef3
0.4.0
1#include "analyzer.h" 2 3//Debug output convenience macros 4#define DOUT1 (s ) if (_debug >= 1) { std::cout << s; } 5#define DOUT2 (s ) if (_debug >= 2) { std::cout << s; } 6 7namespace slip { 8 9Analyzer ::Analyzer (int debug_level ) { 10_debug = debug_level ; 11} 12 13Analyzer ::~Analyzer () { 14//Nothing to do yet 15} 16 17bool Analyzer ::get1v1Ports (const SlippiReplay & s ,Analysis * a )const { 18unsigned num_players = 0 ; 19for (uint8_t i = 0 ;i < 4 ;++ i ) { 20if (s .player [i ].player_type != 3 ) { 21if (num_players == 2 ) { 22return false; 23 } 24a -> ap [num_players ++ ].port = i ; 25 } 26if (i == 3 && num_players != 2 ) { 27return false; 28 } 29 } 30DOUT1 ("Players found on ports " <<a -> ap [0 ].port <<" and " <<a -> ap [1 ].port << std::endl ); 31return true; 32} 33 34void Analyzer ::computeAirtime (const SlippiReplay & s ,Analysis * a )const { 35for (unsigned pi = 0 ;pi < 2 ;++ pi ) { 36SlippiPlayer p = s .player [a -> ap [pi ].port ]; 37unsigned airframes = 0 ; 38for (unsigned f = (- LOAD_FRAME );f < s .frame_count ;++ f ) { 39airframes += p .frame [f ].airborne ; 40 } 41 42a -> ap [pi ].air_frames = airframes ; 43// float aircount = 100*((float)airframes / s.last_frame); 44// std::cout << " Airborne for " << aircount << "% of match" << std::endl; 45 } 46} 47 48void Analyzer ::getBasicGameInfo (const SlippiReplay & s ,Analysis * a )const { 49a -> slippi_version = s .slippi_version ; 50a -> parser_version = s .parser_version ; 51a -> analyzer_version = ANALYZER_VERSION ; 52a -> game_time = s .start_time ; 53a -> game_length = s .frame_count ; 54a -> ap [0 ].tag_player = s .player [a -> ap [0 ].port ].tag ; 55a -> ap [1 ].tag_player = s .player [a -> ap [1 ].port ].tag ; 56a -> ap [0 ].tag_css = s .player [a -> ap [0 ].port ].tag_css ; 57a -> ap [1 ].tag_css = s .player [a -> ap [1 ].port ].tag_css ; 58 59a -> ap [0 ].start_stocks = s .player [a -> ap [0 ].port ].start_stocks ; 60a -> ap [1 ].start_stocks = s .player [a -> ap [1 ].port ].start_stocks ; 61a -> ap [0 ].color = s .player [a -> ap [0 ].port ].color ; 62a -> ap [1 ].color = s .player [a -> ap [1 ].port ].color ; 63a -> ap [0 ].team_id = s .player [a -> ap [0 ].port ].team_id ; 64a -> ap [1 ].team_id = s .player [a -> ap [1 ].port ].team_id ; 65a -> ap [0 ].player_type = s .player [a -> ap [0 ].port ].player_type ; 66a -> ap [1 ].player_type = s .player [a -> ap [1 ].port ].player_type ; 67a -> ap [0 ].cpu_level = s .player [a -> ap [0 ].port ].cpu_level ; 68a -> ap [1 ].cpu_level = s .player [a -> ap [1 ].port ].cpu_level ; 69 70a -> ap [0 ].end_stocks = s .player [a -> ap [0 ].port ].frame [s .frame_count - 1 ].stocks ; 71a -> ap [1 ].end_stocks = s .player [a -> ap [1 ].port ].frame [s .frame_count - 1 ].stocks ; 72a -> ap [0 ].end_pct = s .player [a -> ap [0 ].port ].frame [s .frame_count - 1 ].percent_pre ; 73a -> ap [1 ].end_pct = s .player [a -> ap [1 ].port ].frame [s .frame_count - 1 ].percent_pre ; 74a -> ap [0 ].char_id = s .player [a -> ap [0 ].port ].ext_char_id ; 75a -> ap [1 ].char_id = s .player [a -> ap [1 ].port ].ext_char_id ; 76a -> stage_id = s .stage ; 77a -> ap [0 ].char_name = CharExt ::name [a -> ap [0 ].char_id ]; 78a -> ap [1 ].char_name = CharExt ::name [a -> ap [1 ].char_id ]; 79a -> stage_name = Stage ::name [s .stage ]; 80} 81 82void Analyzer ::summarizeInteractions (const SlippiReplay & s ,Analysis * a )const { 83// std::cout << " Summarizing player interactions" << std::endl; 84for (unsigned f = (PLAYABLE_FRAME - LOAD_FRAME );f < s .frame_count ;++ f ) { 85unsigned d = a -> dynamics [f ]; 86++ (a -> ap [0 ].dyn_counts [d ]);//Increase the counter for dynamics across all frames 87if (d > Dynamic ::DEFENSIVE && d < Dynamic ::OFFENSIVE ) { 88//Index is the same for the 2nd player for neutral dynamics 89++ (a -> ap [1 ].dyn_counts [d ]); 90 }else { 91//Index is inverted for the 2nd player for non-neutral dynamics 92++ (a -> ap [1 ].dyn_counts [Dynamic ::__LAST - d ]); 93 } 94 } 95// std::cout << std::fixed; //Show floats in fixed representation 96// for (unsigned p = 0; p < 2; ++p) { 97// // std::cout << " From the perspective of port " << int(a->ap[p].port+1) << " (" << a->ap[p].char_name << "):" << std::endl; 98// for (unsigned i = 0; i < Dynamic::__LAST; ++i) { 99// std::string n = std::to_string(a->ap[p].dyn_counts[i]); 100// std::string pct = std::to_string((100*float(a->ap[p].dyn_counts[i])/s.frame_count)).substr(0,5); 101// std::cout << " " << SPACE[6-n.length()] << n << " frames (" << pct 102// << "% of game) spent in " << Dynamic::name[i] << std::endl; 103// } 104// } 105} 106 107void Analyzer ::countLCancels (const SlippiReplay & s ,Analysis * a )const { 108for (unsigned pi = 0 ;pi < 2 ;++ pi ) { 109SlippiPlayer p = s .player [a -> ap [pi ].port ]; 110unsigned cancels_hit = 0 ; 111unsigned cancels_miss = 0 ; 112unsigned last_state = 0 ; 113for (unsigned f = (- LOAD_FRAME );f < s .frame_count ;++ f ) { 114if (last_state == 0 ) { 115if (p .frame [f ].l_cancel == 1 ) { 116cancels_hit += 1 ; 117 }else if (p .frame [f ].l_cancel == 2 ) { 118cancels_miss += 1 ; 119 } 120 } 121last_state = p .frame [f ].l_cancel ; 122 } 123 124a -> ap [pi ].l_cancels_hit = cancels_hit ; 125a -> ap [pi ].l_cancels_missed = cancels_miss ; 126float cancels_pct = 100 * ((float )cancels_hit / (cancels_hit + cancels_miss )); 127// std::cout << " Hit " << cancels_hit << "/" << cancels_hit+cancels_miss << " l cancels (" << cancels_pct << "%)" << std::endl; 128 } 129} 130 131void Analyzer ::countTechs (const SlippiReplay & s ,Analysis * a )const { 132for (unsigned pi = 0 ;pi < 2 ;++ pi ) { 133SlippiPlayer p = s .player [a -> ap [pi ].port ]; 134unsigned techs_hit = 0 ; 135unsigned walltechs_hit = 0 ;//And ceiling techs 136unsigned walljumps_hit = 0 ; 137unsigned walljumptechs_hit = 0 ; 138unsigned techs_missed = 0 ; 139bool teching = false; 140 141for (unsigned f = (- LOAD_FRAME );f < s .frame_count ;++ f ) { 142if (inTechState (p .frame [f ])) { 143if (notteching ) { 144teching = true; 145if (p .frame [f ].action_pre <=Action ::DownSpotD ) { 146++ techs_missed ; 147 }else if (p .frame [f ].action_pre <=Action ::PassiveStandB ) { 148++ techs_hit ; 149 }else if (p .frame [f ].action_pre == Action ::PassiveWallJump ) { 150if (inDamagedState (p .frame [f - 1 ])|| inTumble (p .frame [f - 1 ])) { 151++ walljumptechs_hit ; 152 }else { 153++ walljumps_hit ; 154 } 155 }else { 156++ walltechs_hit ;//Or ceiling techs 157 } 158 } 159 }else { 160teching = false; 161 } 162 } 163 164a -> ap [pi ].techs = techs_hit ; 165a -> ap [pi ].walltechs = walltechs_hit ; 166a -> ap [pi ].walljumps = walljumps_hit ; 167a -> ap [pi ].walltechjumps = walljumptechs_hit ; 168a -> ap [pi ].missed_techs = techs_missed ; 169 170// std::cout << " Hit " << techs_hit << " grounded techs" << std::endl; 171// std::cout << " Hit " << walltechs_hit << " wall techs" << std::endl; 172// std::cout << " Hit " << walljumps_hit << " wall jumps" << std::endl; 173// std::cout << " Hit " << walljumptechs_hit << " wall jump techs" << std::endl; 174// std::cout << " Missed " << techs_missed << " techs" << std::endl; 175 } 176} 177 178void Analyzer ::countDashdances (const SlippiReplay & s ,Analysis * a )const { 179for (unsigned pi = 0 ;pi < 2 ;++ pi ) { 180SlippiPlayer p = s .player [a -> ap [pi ].port ]; 181unsigned dashdances = 0 ; 182for (unsigned f = (- LOAD_FRAME );f < s .frame_count ;++ f ) { 183if (isDashdancing (p ,f )) { 184++ dashdances ; 185 } 186 } 187 188a -> ap [pi ].dashdances = dashdances ; 189// std::cout << " Dashdanced " << dashdances << " times" << std::endl; 190 } 191} 192 193//Code adapted from Fizzi's 194void Analyzer ::countAirdodgesAndWavelands (const SlippiReplay & s ,Analysis * a )const { 195for (unsigned pi = 0 ;pi < 2 ;++ pi ) { 196SlippiPlayer p = s .player [a -> ap [pi ].port ]; 197unsigned airdodges = 0 ; 198unsigned wavelands = 0 ; 199unsigned wavedashes = 0 ; 200bool airdodging = false; 201for (unsigned f = (- LOAD_FRAME );f < s .frame_count ;++ f ) { 202if (maybeWavelanding (p ,f )) {//Waveland detection by Fizzi 203//Look at the last 8 frames (including this one) re Fizzi 204bool foundAirdodge = false; 205bool foundJumpsquat = false; 206bool foundOther = false; 207for (unsigned i = 1 ;i < 8 ;++ i ) { 208if (isAirdodging (p .frame [f - i ])) { 209foundAirdodge = true; 210 }else if (isInJumpsquat (p .frame [f - i ])) { 211foundJumpsquat = true; 212break ; 213 }else { 214foundOther = true; 215 } 216 } 217if (foundAirdodge ) { 218if ((notfoundJumpsquat )&& (notfoundOther )) { 219continue ;//If the airdodge is the only animation we found, proceed 220 }else { 221-- airdodges ;//Otherwise, subtract it from our airdodge count and keep checkings 222 } 223 } 224if (foundJumpsquat ) {//If we were in jumpsquat at all recently, we're wavedashing 225++ wavedashes ; 226 }else if (foundOther ) {//If we were in any other animation besides airdodge, it's a waveland 227++ wavelands ; 228 }//Otherwise, nothing special happened 229 }else if (isAirdodging (p .frame [f ])) { 230if (notairdodging ) { 231++ airdodges ; 232airdodging = true; 233 } 234 }else { 235airdodging = false; 236 } 237 } 238 239a -> ap [pi ].airdodges = airdodges ; 240a -> ap [pi ].wavelands = wavelands ; 241a -> ap [pi ].wavedashes = wavedashes ; 242// std::cout << " Airdodged " << airdodges << " times" << std::endl; 243// std::cout << " Wavelanded " << wavelands << " times" << std::endl; 244// std::cout << " Wavedashed " << wavedashes << " times" << std::endl; 245 } 246} 247 248void Analyzer ::analyzeInteractions (const SlippiReplay & s ,Analysis * a )const { 249// std::cout << " Analyzing player interactions" << std::endl; 250const SlippiPlayer * p = & (s .player [a -> ap [0 ].port ]); 251const SlippiPlayer * o = & (s .player [a -> ap [1 ].port ]); 252 253unsigned cur_dynamic = Dynamic ::POSITIONING ;// Current dynamic in effect 254 255unsigned oLastInHitsun = 0 ;//Last frame opponent was stuck in hitstun 256unsigned pLastInHitsun = 0 ;//Last frame we were stuck in hitstun 257unsigned oLastGrounded = 0 ;//Last frame opponent touched solid ground 258unsigned pLastGrounded = 0 ;//Last frame we touched solid ground' 259bool oHitThisFrame = false; 260bool pHitThisFrame = false; 261bool oHitLastFrame = false; 262bool pHitLastFrame = false; 263 264//All interactions analyzed from perspective of p (lower port player) 265for (unsigned f = (PLAYABLE_FRAME - LOAD_FRAME );f < s .frame_count ;++ f ) { 266//Important variables for each frame 267SlippiFrame of = o -> frame [f ]; 268SlippiFrame pf = p -> frame [f ]; 269oHitLastFrame = oHitThisFrame ; 270oHitThisFrame = false; 271bool oInHitstun = isInHitstun (of ); 272if (oInHitstun ) { 273oLastInHitsun = f ; 274if (of .percent_post > o -> frame [f - 1 ].percent_post ) { 275oHitThisFrame = true;//Check if opponent was hit this frame by looking at % last frame 276 } 277 } 278pHitLastFrame = pHitThisFrame ; 279pHitThisFrame = false; 280bool pInHitstun = isInHitstun (pf ); 281if (pInHitstun ) { 282pLastInHitsun = f ; 283if (pf .percent_post > p -> frame [f - 1 ].percent_post ) { 284pHitThisFrame = true;//Check if we were hit this frame by looking at % last frame 285 } 286 } 287bool oAirborne = isAirborne (of ); 288if (notoAirborne ) {oLastGrounded = f ; } 289bool pAirborne = isAirborne (pf ); 290if (notpAirborne ) {pLastGrounded = f ; } 291bool oIsGrabbed = isGrabbed (of ); 292bool pIsGrabbed = isGrabbed (pf ); 293bool oOnLedge = isOnLedge (of ); 294bool pOnLedge = isOnLedge (pf ); 295bool oShielding = isShielding (of ); 296bool pShielding = isShielding (pf ); 297bool oInShieldstun = isInShieldstun (of ); 298bool pInShieldstun = isInShieldstun (pf ); 299bool oTeching = inFloorTechState (of ); 300bool pTeching = inFloorTechState (pf ); 301bool oThrown = isThrown (of ); 302bool pThrown = isThrown (pf ); 303bool oHitOffStage = isOffStage (s ,of )&& oInHitstun ;//Check if opponent is in hitstun offstage 304bool pHitOffStage = isOffStage (s ,pf )&& pInHitstun ;//Check if we are in hitstun offstage 305bool oPoked = ((f - oLastInHitsun )< POKE_THRES );//Check if opponent has been poked recently 306bool pPoked = ((f - pLastInHitsun )< POKE_THRES );//Check if we have been poked recently 307 308//If we're airborne and haven't touched the ground since last entering hitstun, we're being punished 309bool oBeingPunished = oAirborne && (oLastGrounded < oLastInHitsun ); 310bool pBeingPunished = pAirborne && (pLastGrounded < pLastInHitsun ); 311 312//If we also haven't been in hitstun for at least SHARK_THRES frames, we're being sharked 313bool oBeingSharked = oBeingPunished && ((f - oLastInHitsun )> SHARK_THRES ); 314bool pBeingSharked = pBeingPunished && ((f - pLastInHitsun )> SHARK_THRES ); 315 316//Determine whether neutral would be considered footsies or positioning 317// unsigned neut_dyn = ( oPoked || pPoked ) ? Dynamic::POKING 318// : ((playerDistance(pf,of) > FOOTSIE_THRES) ? Dynamic::POSITIONING : Dynamic::FOOTSIES); 319unsigned neut_dyn = ((playerDistance (pf ,of )> FOOTSIE_THRES ) ?Dynamic ::POSITIONING :Dynamic ::FOOTSIES ); 320 321//First few checks are largely agnostic to cur_dynamic 322if (isDead (of )|| isDead (pf )) { 323cur_dynamic = Dynamic ::POSITIONING ; 324 }else if (oHitOffStage && cur_dynamic != Dynamic ::RECOVERING ) {//If the opponent is offstage and in hitstun, they're being [possibly reverse] edgeguarded 325cur_dynamic = Dynamic ::EDGEGUARDING ; 326 }else if (pHitOffStage && cur_dynamic != Dynamic ::EDGEGUARDING ) {//If we're offstage and in hitstun, we're being [possibly reverse] edgeguarded 327cur_dynamic = Dynamic ::RECOVERING ; 328 }else if (oIsGrabbed ) {//If we grab the opponent in neutral, it's pressure; on offense, it's a techchase 329if (cur_dynamic != Dynamic ::PRESSURING ) {//Need this to prevent grabs from overwriting themselves 330cur_dynamic = (cur_dynamic >=Dynamic ::OFFENSIVE ) ?Dynamic ::TECHCHASING :Dynamic ::PRESSURING ; 331 } 332 }else if (pIsGrabbed ) {//If we are grabbed by the opponent in neutral, we're pressured; on defense, we're techchased 333if (cur_dynamic != Dynamic ::PRESSURED ) {//Need this to prevent grabs from overwriting themselves 334cur_dynamic = (cur_dynamic <=Dynamic ::DEFENSIVE ) ?Dynamic ::ESCAPING :Dynamic ::PRESSURED ; 335 } 336 }else if (oInShieldstun ) {//If our opponent is in shieldstun, we are pressureing 337cur_dynamic = Dynamic ::PRESSURING ; 338 }else if (pInShieldstun ) {//If we are in shieldstun, we are pressured 339cur_dynamic = Dynamic ::PRESSURED ; 340 }else if (oTeching ) {//If opponent is teching, we are techchasing 341cur_dynamic = Dynamic ::TECHCHASING ; 342 }else if (pTeching ) {//If we are teching, we are escaping a techchase 343cur_dynamic = Dynamic ::ESCAPING ; 344 }else if (oInHitstun && pInHitstun ) {//If we're both in hitstun and neither of us are offstage, it's a trade 345// cur_dynamic = Dynamic::TRADING; 346cur_dynamic = Dynamic ::POKING ;//Update 2019-11-05: trading is no longer a used dynamic; now just poking 347 }else {//Everything else depends on the cur_dynamic 348switch (cur_dynamic ) { 349case Dynamic ::PRESSURING : 350if (oHitLastFrame && notoIsGrabbed ) {//If opponent was actually hit last frame by a non pummel, we're punishing 351cur_dynamic = Dynamic ::PUNISHING ; 352 }else if (oThrown ) {//If the opponent is being thrown, this is a techchase opportunity 353cur_dynamic = Dynamic ::TECHCHASING ; 354 }else if ((notoInHitstun )&& (notoShielding )&& (notoOnLedge )&& (notoAirborne )) {//If the opponent touches the ground w/o shielding, we're back to neutral 355cur_dynamic = neut_dyn ; 356 } 357break ; 358case Dynamic ::PRESSURED : 359if (pHitLastFrame && notpIsGrabbed ) {//If we were actually hit last frame by a non pummel, we're being punishied 360cur_dynamic = Dynamic ::PUNISHED ; 361 }else if (pThrown ) {//If we are being thrown, opponent has a techchase opportunity 362cur_dynamic = Dynamic ::ESCAPING ; 363 }else if ((notpInHitstun )&& (notpShielding )&& (notpOnLedge )&& (notpAirborne )) {//If we touch the ground w/o shielding, we're back to neutral 364cur_dynamic = neut_dyn ; 365 } 366break ; 367case Dynamic ::EDGEGUARDING : 368if (oOnLedge || (notoAirborne && notoPoked )) {//If they make it back, we're back to neutral 369cur_dynamic = neut_dyn ; 370 } 371break ; 372case Dynamic ::RECOVERING : 373if (pOnLedge || (notpAirborne && notpPoked )) {//If we make it back, we're back to neutral 374cur_dynamic = neut_dyn ; 375 } 376break ; 377case Dynamic ::TECHCHASING : 378if (oHitLastFrame && (notoTeching )&& oAirborne ) { 379cur_dynamic = Dynamic ::PUNISHING ;//If we started comboing an opponent after the techchase, it's a punish 380 }else if ((notoInHitstun )&& (notoIsGrabbed )&& (notoThrown )&& (notoTeching )&& (notoShielding )) { 381cur_dynamic = oAirborne ?Dynamic ::SHARKING :neut_dyn ;//If the opponent escaped our tech chase, back to neutral it is 382 } 383break ; 384case Dynamic ::ESCAPING : 385if (pHitLastFrame && (notpTeching )&& pAirborne ) { 386cur_dynamic = Dynamic ::PUNISHED ;//If we started comboing an opponent after the techchase, it's a punish 387 }else if ((notpInHitstun )&& (notpIsGrabbed )&& (notpThrown )&& (notpTeching )&& (notpShielding )) { 388cur_dynamic = pAirborne ?Dynamic ::GROUNDING :neut_dyn ;//If we escaped our opponent's tech chase, back to neutral it is 389 } 390break ; 391case Dynamic ::PUNISHING : 392if (oBeingSharked ) {//If we let too much hitstun elapse, but our opponent is still airborne, we're sharking 393cur_dynamic = Dynamic ::SHARKING ; 394 }else if ((notoAirborne )&& (notoPoked )) {//If our opponent has outright landed, we're back to neutral 395cur_dynamic = neut_dyn ; 396 } 397break ; 398case Dynamic ::PUNISHED : 399if (pBeingSharked ) {//If we survive punishes long enough, but are still airborne, we're being sharked 400cur_dynamic = Dynamic ::GROUNDING ; 401 }else if ((notpAirborne )&& (notpPoked )) {//If we have outright landed, we're back to neutral 402cur_dynamic = neut_dyn ; 403 } 404break ; 405case Dynamic ::SHARKING : 406if (oHitLastFrame ) { 407cur_dynamic = Dynamic ::PUNISHING ;//If we hit our opponent LAST frame, we're comboing again 408 }else if (pInHitstun ) {//If our opponent hits us while sharking, we're back to poking 409cur_dynamic = Dynamic ::POKING ; 410 }else if (notoAirborne ) {//If our opponent has outright landed, we're back to neutral 411cur_dynamic = neut_dyn ; 412 } 413break ; 414case Dynamic ::GROUNDING : 415if (pHitLastFrame ) { 416cur_dynamic = Dynamic ::PUNISHED ;//If our opponent hit us LAST frame, we're being comboed again 417 }else if (oInHitstun ) {//If we hit our opponent while grounding, we're back to poking 418cur_dynamic = Dynamic ::POKING ; 419 }else if (notpAirborne ) {//If we have outright landed, we're back to neutral 420cur_dynamic = neut_dyn ; 421 } 422break ; 423case Dynamic ::POKING : 424if (oPoked && oHitThisFrame ) { 425cur_dynamic = Dynamic ::PUNISHING ;//If we have poked our opponent recently and hit them again, we're punishing 426 }else if (pPoked && pHitThisFrame ) { 427cur_dynamic = Dynamic ::PUNISHED ;//If we have been poked recently and got hit again, we're being punished 428 }else if (oBeingSharked && notpPoked ) { 429cur_dynamic = Dynamic ::SHARKING ;//If we'd otherwise be considered as punishing, we're sharking 430 }else if (pBeingSharked && notoPoked ) { 431cur_dynamic = Dynamic ::GROUNDING ;//If we'd otherwise be considered as being punished, we're being sharked 432 }else { 433cur_dynamic = (oPoked || pPoked ) ?Dynamic ::POKING :neut_dyn ;//We're in neutral if nobody has been hit recently 434 } 435break ; 436case Dynamic ::POSITIONING : 437case Dynamic ::FOOTSIES : 438case Dynamic ::TRADING : 439if (oBeingSharked ) { 440cur_dynamic = Dynamic ::SHARKING ;//If we are sharking, update accordingly 441 }else if (pBeingSharked ) { 442cur_dynamic = Dynamic ::GROUNDING ;//If we are being sharked, update accordingly 443 }else { 444cur_dynamic = (oPoked || pPoked ) ?Dynamic ::POKING :neut_dyn ;//If we were trading before, we're in neutral now that we're no longer trading 445 } 446break ; 447default : 448break ; 449 } 450 } 451 452//Aggregate results 453a -> dynamics [f ]= cur_dynamic ;//Set the dynamic for this frame to the current dynamic computed 454DOUT1 (" " <<f <<" (" <<frameAsTimer (f ,s .timer ) <<") P1 " 455 <<Dynamic ::name [cur_dynamic ] <<" " 456 << std::endl ); 457 } 458 459} 460 461void Analyzer ::analyzePunishes (const SlippiReplay & s ,Analysis * a )const { 462const SlippiPlayer * p = & (s .player [a -> ap [0 ].port ]); 463const SlippiPlayer * o = & (s .player [a -> ap [1 ].port ]); 464unsigned pa = 0 ;//Running tally of player attacks 465unsigned oa = 0 ;//Running tally of opponent attacks 466unsigned pn = 0 ;//Running tally of player punishes 467unsigned on = 0 ;//Running tally of opponent punishes 468unsigned oLastInHitsun = 0 ; 469unsigned pLastInHitsun = 0 ; 470unsigned cur_dyn = a -> dynamics [FIRST_FRAME ]; 471unsigned last_dyn = a -> dynamics [FIRST_FRAME ]; 472Punish * pPunishes = a -> ap [0 ].punishes ; 473Punish * oPunishes = a -> ap [1 ].punishes ; 474Attack * pAttacks = a -> ap [0 ].attacks ; 475Attack * oAttacks = a -> ap [1 ].attacks ; 476 477for (unsigned f = FIRST_FRAME ;f < s .frame_count ;++ f ) { 478SlippiFrame pf = p -> frame [f ]; 479SlippiFrame of = o -> frame [f ]; 480cur_dyn = a -> dynamics [f ]; 481 482oLastInHitsun = isInHitstun (o -> frame [f ]) ?f :oLastInHitsun ; 483pLastInHitsun = isInHitstun (p -> frame [f ]) ?f :pLastInHitsun ; 484bool oPoked = ((f - oLastInHitsun )< POKE_THRES );//Check if opponent has been poked recently 485bool pPoked = ((f - pLastInHitsun )< POKE_THRES );//Check if we have been poked recently 486 487//Check if we counter-attacked (switched from defense to offense or vice versa) 488if (cur_dyn <=Dynamic ::DEFENSIVE && last_dyn >=Dynamic ::OFFENSIVE ) { 489++ a -> ap [1 ].counters ;//If we went from offense to defense, we were countered 490 }else if (cur_dyn >=Dynamic ::OFFENSIVE && last_dyn <=Dynamic ::DEFENSIVE ) { 491++ a -> ap [0 ].counters ;//If we went from defense to offense, we countered 492 } 493 494//Check if either player lost a stock 495if (pf .stocks < p -> frame [f - 1 ].stocks ) { 496unsigned ddir = deathDirection (* p ,f ); 497if (oa > 0 ) { 498oAttacks [oa - 1 ].kill_dir = ddir ; 499 } 500if (oPunishes [on ].num_moves > 0 ) { 501oPunishes [on ].kill_dir = ddir ;//TODO: filter out self-destructs somehow 502 }else if (on > 0 ) { 503oPunishes [on - 1 ].kill_dir = ddir ; 504 } 505if (cur_dyn == Dynamic ::EDGEGUARDING ) { 506++ (a -> ap [1 ].reverse_edgeguards ); 507 }else if (cur_dyn != Dynamic ::RECOVERING && ddir != Dir ::UP ) { 508++ (a -> ap [0 ].self_destructs ); 509 } 510 } 511if (of .stocks < o -> frame [f - 1 ].stocks ) { 512unsigned ddir = deathDirection (* o ,f ); 513if (pa > 0 ) { 514pAttacks [pa - 1 ].kill_dir = ddir ; 515 } 516if (pPunishes [pn ].num_moves > 0 ) { 517pPunishes [pn ].kill_dir = ddir ; 518 }else if (pn > 0 ) { 519pPunishes [pn - 1 ].kill_dir = ddir ; 520 } 521if (cur_dyn == Dynamic ::RECOVERING ) { 522++ (a -> ap [0 ].reverse_edgeguards ); 523 }else if (cur_dyn != Dynamic ::EDGEGUARDING && ddir != Dir ::UP ) { 524++ (a -> ap [1 ].self_destructs ); 525 } 526 } 527 528//Check if either player has dropped a punish off an opening 529bool pPunishEnd = false; 530bool oPunishEnd = false; 531if (f == s .frame_count - 1 ) { 532pPunishEnd = true; 533oPunishEnd = true; 534 }else if (cur_dyn != Dynamic ::POKING ) { 535if (cur_dyn > Dynamic ::DEFENSIVE && notpPoked ) { 536//If the opponent had a punish going and they are no longer on offense, end their punish 537oPunishEnd = true; 538 } 539if (cur_dyn < Dynamic ::OFFENSIVE && notoPoked ) { 540//If we had a punish going and we are no longer on offense, end our punish 541pPunishEnd = true; 542 } 543 } 544 545//Update punish counter for ended punishes 546if (pPunishEnd && pa > 0 ) { 547if (pPunishes [pn ].num_moves > 0 ) { 548pPunishes [pn ].end_frame = f ; 549pPunishes [pn ].end_pct = of .percent_pre ; 550pPunishes [pn ].last_move_id = pf .hit_with ; 551if (pPunishes [pn ].num_moves > pAttacks [pa - 1 ].hit_id ) { 552a -> ap [0 ].neutral_wins += 1 ; 553 }else { 554a -> ap [0 ].pokes += 1 ; 555 } 556++ pn ; 557 } 558 } 559if (oPunishEnd && oa > 0 ) { 560if (oPunishes [on ].num_moves > 0 ) { 561oPunishes [on ].end_frame = f ; 562oPunishes [on ].end_pct = pf .percent_pre ; 563oPunishes [on ].last_move_id = of .hit_with ; 564if (oPunishes [on ].num_moves > oAttacks [oa - 1 ].hit_id ) { 565a -> ap [1 ].neutral_wins += 1 ; 566 }else { 567a -> ap [1 ].pokes += 1 ; 568 } 569++ on ; 570 } 571 } 572 573//If the opponent just took damage 574float o_damage_taken = of .percent_pre - o -> frame [f - 1 ].percent_pre ; 575if (o_damage_taken > 0 ) { 576//Check for bubble damage 577pAttacks [pa ].move_id = (isOffscreen (of )&& o_damage_taken == 1 ) ?Move ::BUBBLE :pf .hit_with ; 578//Last frame we actually took damage; frame before that gives us the animation frame our move hit 579pAttacks [pa ].anim_frame = p -> frame [f - 2 ].action_fc ; 580pAttacks [pa ].punish_id = pn ; 581if (//Check if this is a consecutive hit from a multihit move 582pa > 0 && //If this isn't our first move 583pAttacks [pa ].move_id == pAttacks [pa - 1 ].move_id && //If the last move we hit with had the same id 584pAttacks [pa ].anim_frame > pAttacks [pa - 1 ].anim_frame && //If the action frame counter is higher 585//If the gap between animation and game frames is less than max hitlag for the move 586//Formula: https://www.ssbwiki.com/Freeze_frame 587 (f - pAttacks [pa ].anim_frame <= (pAttacks [pa - 1 ].frame - pAttacks [pa - 1 ].anim_frame + (pAttacks [pa - 1 ].damage /3 + 3 )))) { 588pAttacks [pa ].hit_id = pAttacks [pa - 1 ].hit_id + 1 ; 589 }else { 590pAttacks [pa ].hit_id = 1 ; 591 } 592pAttacks [pa ].frame = f ; 593pAttacks [pa ].damage = o_damage_taken ; 594a -> ap [0 ].dyn_damage [cur_dyn ]+= o_damage_taken ;//Dynamic is normal for player 595pAttacks [pa ].opening = cur_dyn ;//Dynamic is normal for player 596pAttacks [pa ].kill_dir = Dir ::NEUT ; 597++ pa ; 598//If this is the start of a combo 599if (pPunishes [pn ].num_moves == 0 ) { 600pPunishes [pn ].start_frame = f ; 601pPunishes [pn ].start_pct = o -> frame [f - 1 ].percent_pre ; 602pPunishes [pn ].kill_dir = Dir ::NEUT ; 603 } 604a -> ap [0 ].damage_dealt += o_damage_taken ; 605pPunishes [pn ].end_frame = f ; 606pPunishes [pn ].end_pct = of .percent_pre ; 607pPunishes [pn ].last_move_id = pf .hit_with ; 608pPunishes [pn ].num_moves += 1 ; 609++ (a -> ap [0 ].move_counts [pf .hit_with ]); 610 } 611 612//If we just took damage 613float p_damage_taken = pf .percent_pre - p -> frame [f - 1 ].percent_pre ; 614if (p_damage_taken > 0 ) { 615//Check for bubble damage 616oAttacks [oa ].move_id = (isOffscreen (pf )&& p_damage_taken == 1 ) ?Move ::BUBBLE :of .hit_with ; 617//Last frame we actually took damage; frame before that gives us the animation frame our move hit 618oAttacks [oa ].anim_frame = o -> frame [f - 2 ].action_fc ; 619oAttacks [oa ].punish_id = on ; 620if (//Check if this is a consecutive hit from a multihit move 621oa > 0 && //If this isn't our first move 622oAttacks [oa ].move_id == oAttacks [oa - 1 ].move_id && //If the last move we hit with had the same id 623oAttacks [oa ].anim_frame > oAttacks [oa - 1 ].anim_frame && //If the action frame counter is higher 624//If the gap between animation and game frames is less than max hitlag for the move 625 (f - oAttacks [oa ].anim_frame <= (oAttacks [oa - 1 ].frame - oAttacks [oa - 1 ].anim_frame + (oAttacks [oa - 1 ].damage /3 + 3 )))) { 626oAttacks [oa ].hit_id = oAttacks [oa - 1 ].hit_id + 1 ; 627 }else { 628oAttacks [oa ].hit_id = 1 ; 629 } 630oAttacks [oa ].frame = f ; 631oAttacks [oa ].damage = p_damage_taken ; 632//Dynamic needs to be flipped around for opponent 633int oDynamic = (cur_dyn > Dynamic ::OFFENSIVE || cur_dyn < Dynamic ::DEFENSIVE ) ?Dynamic ::__LAST - cur_dyn :cur_dyn ; 634a -> ap [1 ].dyn_damage [oDynamic ]+= p_damage_taken ;//Dynamic needs to be flipped around for opponent 635oAttacks [oa ].opening = oDynamic ; 636oAttacks [oa ].kill_dir = Dir ::NEUT ; 637++ oa ; 638//If this is the start of a combo 639if (oPunishes [on ].num_moves == 0 ) { 640oPunishes [on ].start_frame = f ; 641oPunishes [on ].start_pct = p -> frame [f - 1 ].percent_pre ; 642oPunishes [on ].kill_dir = Dir ::NEUT ; 643 } 644a -> ap [1 ].damage_dealt += p_damage_taken ; 645oPunishes [on ].end_frame = f ; 646oPunishes [on ].end_pct = pf .percent_pre ; 647oPunishes [on ].last_move_id = of .hit_with ; 648oPunishes [on ].num_moves += 1 ; 649++ (a -> ap [1 ].move_counts [of .hit_with ]); 650 } 651 652last_dyn = cur_dyn ;//Update the last dynamic 653 } 654} 655 656void Analyzer ::analyzeCancels (const SlippiReplay & s ,Analysis * a )const { 657for (unsigned pi = 0 ;pi < 2 ;++ pi ) { 658const SlippiPlayer * p = & (s .player [a -> ap [pi ].port ]); 659Attack * attacks = a -> ap [pi ].attacks ; 660for (unsigned i = 0 ;attacks [i ].frame > 0 ;++ i ) { 661unsigned f = attacks [i ].frame ; 662if (attacks [i ].move_id >=Move ::NAIR && attacks [i ].move_id <=Move ::DAIR ) { 663unsigned last_frame = attacks [i ].frame + 60 ; 664if (last_frame > s .frame_count ) { 665last_frame = s .frame_count ; 666 } 667for (unsigned f = attacks [i ].frame ;f < last_frame ;++ f ) { 668//Check if we had the opportunity to edge cancel 669if (didEdgeCancelAerial (p -> frame [f ])) { 670attacks [i ].cancel_type = Cancel ::EDGE ; 671break ; 672 } 673//Check if we had the opportunity to teeter cancel 674if (didTeeterCancelAerial (p -> frame [f ])) { 675attacks [i ].cancel_type = Cancel ::TEETER ; 676break ; 677 } 678//Check if we had the opportunity to autocancel 679if (didAutoCancelAerial (p -> frame [f ])) { 680attacks [i ].cancel_type = Cancel ::AUTO ; 681break ; 682 } 683//Check if we had the opportunity to L cancel 684if (p -> frame [f ].l_cancel > 0 ) { 685if (p -> frame [f ].l_cancel == 1 ) { 686attacks [i ].cancel_type = Cancel ::L ; 687 } 688break ; 689 } 690 } 691 } 692 } 693 } 694} 695 696void Analyzer ::analyzeShield (const SlippiReplay & s ,Analysis * a )const { 697for (unsigned pi = 0 ;pi < 2 ;++ pi ) { 698const SlippiPlayer * p = & (s .player [a -> ap [pi ].port ]); 699for (unsigned f = (- LOAD_FRAME );f < s .frame_count ;++ f ) { 700float shield_damage = (p -> frame [f ].shield - p -> frame [f - 1 ].shield ); 701if (shield_damage > 0 ) { 702a -> ap [pi ].shield_time += 1 ; 703a -> ap [pi ].shield_damage += shield_damage ; 704if (p -> frame [f ].shield < a -> ap [pi ].shield_lowest ) { 705a -> ap [pi ].shield_lowest = p -> frame [f ].shield ; 706 } 707 } 708 } 709 } 710} 711 712unsigned Analyzer ::countTransitions (const SlippiReplay & s ,Analysis * a ,unsigned pnum ,bool (* cb )(const SlippiFrame & ))const { 713SlippiPlayer p = s .player [a -> ap [pnum ].port ]; 714unsigned counter = 0 ; 715bool active = false; 716for (unsigned f = (- LOAD_FRAME );f < s .frame_count ;++ f ) { 717if (cb (p .frame [f ])) { 718if (notactive ) { 719++ counter ; 720active = true; 721 } 722 }else { 723active = false; 724 } 725 } 726return counter ; 727} 728 729unsigned Analyzer ::countTransitions (const SlippiReplay & s ,Analysis * a ,unsigned pnum ,bool (* cb )(const SlippiPlayer & ,const unsigned ))const { 730SlippiPlayer p = s .player [a -> ap [pnum ].port ]; 731unsigned counter = 0 ; 732bool active = false; 733for (unsigned f = (- LOAD_FRAME );f < s .frame_count ;++ f ) { 734if (cb (p ,f )) { 735if (notactive ) { 736++ counter ; 737active = true; 738 } 739 }else { 740active = false; 741 } 742 } 743return counter ; 744} 745 746void Analyzer ::countBasicAnimations (const SlippiReplay & s ,Analysis * a )const { 747for (unsigned pi = 0 ;pi < 2 ;++ pi ) { 748a -> ap [pi ].ledge_grabs = countTransitions (s ,a ,pi ,isOnLedge ); 749a -> ap [pi ].rolls = countTransitions (s ,a ,pi ,isRolling ); 750a -> ap [pi ].spotdodges = countTransitions (s ,a ,pi ,isSpotdodging ); 751a -> ap [pi ].powershields = countTransitions (s ,a ,pi ,didPowerShield ); 752a -> ap [pi ].grabs = countTransitions (s ,a ,pi ,isGrabbing ); 753a -> ap [pi ].taunts = countTransitions (s ,a ,pi ,isTaunting ); 754a -> ap [pi ].meteor_cancels = countTransitions (s ,a ,pi ,didMeteorCancel ); 755a -> ap [pi ].hits_blocked = countTransitions (s ,a ,pi ,isInShieldstun ); 756a -> ap [pi ].edge_cancel_aerials = countTransitions (s ,a ,pi ,didEdgeCancelAerial ); 757a -> ap [pi ].edge_cancel_specials = countTransitions (s ,a ,pi ,didEdgeCancelSpecial ); 758a -> ap [pi ].teeter_cancel_aerials = countTransitions (s ,a ,pi ,didTeeterCancelAerial ); 759a -> ap [pi ].teeter_cancel_specials = countTransitions (s ,a ,pi ,didTeeterCancelSpecial ); 760a -> ap [pi ].no_impact_lands = countTransitions (s ,a ,pi ,didNoImpactLand ); 761a -> ap [pi ].shield_drops = countTransitions (s ,a ,pi ,didShieldDrop ); 762a -> ap [pi ].pivots = countTransitions (s ,a ,pi ,didPivot ); 763a -> ap [pi ].short_hops = countTransitions (s ,a ,pi ,didShortHop ); 764a -> ap [pi ].full_hops = countTransitions (s ,a ,pi ,didHop )- a -> ap [pi ].short_hops ; 765 766a -> ap [pi ].shield_breaks = countTransitions (s ,a ,1 - pi ,isShieldBroken ); 767a -> ap [pi ].grab_escapes = countTransitions (s ,a ,1 - pi ,isReleasing ); 768a -> ap [pi ].shield_stabs = countTransitions (s ,a ,1 - pi ,wasShieldStabbed ); 769a -> ap [pi ].stage_spikes = countTransitions (s ,a ,1 - pi ,wasStageSpiked ); 770 } 771} 772 773void Analyzer ::showActionStates (const SlippiReplay & s ,Analysis * a )const { 774const SlippiPlayer * p = & (s .player [a -> ap [0 ].port ]); 775const SlippiPlayer * o = & (s .player [a -> ap [1 ].port ]); 776for (unsigned f = FIRST_FRAME ;f < s .frame_count ;++ f ) { 777SlippiFrame pf = p -> frame [f ]; 778DOUT2 (" " <<f <<" (" <<frameAsTimer (f ,s .timer ) <<") P1 " 779 <<Action ::name [pf .action_pre ] <<" " 780 <<" -> " 781 <<" " <<Action ::name [pf .action_post ] 782 << std::endl ); 783SlippiFrame of = o -> frame [f ]; 784DOUT2 (" " <<f <<" (" <<frameAsTimer (f ,s .timer ) <<") P2 " 785 <<Action ::name [of .action_pre ] <<" " 786 <<" -> " 787 <<" " <<Action ::name [of .action_post ] 788 << std::endl ); 789 } 790} 791 792void Analyzer ::countPhantoms (const SlippiReplay & s ,Analysis * a )const { 793const SlippiPlayer * p = & (s .player [a -> ap [0 ].port ]); 794const SlippiPlayer * o = & (s .player [a -> ap [1 ].port ]); 795for (unsigned f = FIRST_FRAME ;f < s .frame_count ;++ f ) { 796if (wasHitByPhantom (* p ,* o ,f )) { 797++ (a -> ap [1 ].phantom_hits ); 798 } 799if (wasHitByPhantom (* o ,* p ,f )) { 800++ (a -> ap [0 ].phantom_hits ); 801 } 802 } 803} 804 805void Analyzer ::analyzeLedgedashes (const SlippiReplay & s ,Analysis * a )const { 806for (unsigned pi = 0 ;pi < 2 ;++ pi ) { 807const SlippiPlayer * p = & (s .player [a -> ap [pi ].port ]); 808unsigned galint = 0 ; 809bool attemptedDash = false; 810for (unsigned f = FIRST_FRAME ;f < s .frame_count ;++ f ) { 811if (galint > 0 ) { 812-- galint ; 813if (notattemptedDash ) { 814if (didAttemptLedgedash (* p ,f )) { 815attemptedDash = true; 816 }else { 817continue ; 818 } 819 } 820if (not(isAirborne (p -> frame [f ]))&& (notisLanding (p -> frame [f ]))) { 821a -> ap [pi ].galint_ledgedashes += 1 ; 822a -> ap [pi ].mean_galint += galint ; 823f += galint ; 824galint = 0 ; 825 } 826 }else if (didCliffCatchEnd (* p ,f )) { 827galint = 29 ; 828attemptedDash = false; 829 } 830 } 831if (a -> ap [pi ].galint_ledgedashes > 0 ) { 832a -> ap [pi ].mean_galint /=a -> ap [pi ].galint_ledgedashes ; 833 } 834 } 835} 836 837void Analyzer ::computeTrivialInfo (const SlippiReplay & s ,Analysis * a )const { 838for (unsigned pi = 0 ;pi < 2 ;++ pi ) { 839a -> ap [pi ].total_openings = a -> ap [pi ].neutral_wins + a -> ap [pi ].pokes + a -> ap [pi ].counters ; 840if (a -> ap [pi ].total_openings > 0 ) { 841a -> ap [pi ].mean_opening_percent = a -> ap [pi ].damage_dealt /a -> ap [pi ].total_openings ; 842 } 843 844unsigned o_stocks_lost = a -> ap [1 - pi ].start_stocks - a -> ap [1 - pi ].end_stocks ; 845if (o_stocks_lost > 0 ) { 846a -> ap [pi ].mean_kill_openings = float (a -> ap [pi ].total_openings ) /o_stocks_lost ; 847a -> ap [pi ].mean_kill_percent = a -> ap [pi ].damage_dealt /o_stocks_lost ; 848 } 849 } 850} 851 852Analysis * Analyzer ::analyze (const SlippiReplay & s ) { 853DOUT1 ("Analyzing replay" << std::endl ); 854 855Analysis * a = new Analysis (s .frame_count );//Structure for holding the analysis so far 856 857//Verify this is a 1 v 1 match; can't analyze otherwise 858if (notget1v1Ports (s ,a )) { 859 std::cerr <<" Not a two player match; refusing to analyze further" << std::endl ; 860a -> success = false; 861return a ; 862 } 863 864DOUT1 (" Analyzing basic game info" << std::endl ); 865getBasicGameInfo (s ,a ); 866 867//Interaction-level stats 868DOUT1 (" Analyzing player interactions" << std::endl ); 869analyzeInteractions (s ,a ); 870DOUT1 (" Summarizing player interactions" << std::endl ); 871summarizeInteractions (s ,a ); 872DOUT1 (" Analyzing players' punishes" << std::endl ); 873analyzePunishes (s ,a ); 874DOUT1 (" Analyzing players' cancelling techniques" << std::endl ); 875analyzeCancels (s ,a ); 876DOUT1 (" Analyzing players' shielding" << std::endl ); 877analyzeShield (s ,a ); 878DOUT1 (" Analyzing ledgedashes" << std::endl ); 879analyzeLedgedashes (s ,a ); 880 881//Player-level stats 882DOUT1 (" Computing statistics based on animations" << std::endl ); 883countBasicAnimations (s ,a ); 884DOUT1 (" Computing air / ground time for each player" << std::endl ); 885computeAirtime (s ,a ); 886DOUT1 (" Counting l cancels" << std::endl ); 887countLCancels (s ,a ); 888DOUT1 (" Counting techs" << std::endl ); 889countTechs (s ,a ); 890DOUT1 (" Counting dashdances" << std::endl ); 891countDashdances (s ,a ); 892DOUT1 (" Counting airdodges, wavelands, and wavedashes" << std::endl ); 893countAirdodgesAndWavelands (s ,a ); 894DOUT1 (" Counting phantom hits" << std::endl ); 895countPhantoms (s ,a ); 896 897DOUT1 (" Computing trivial match info" << std::endl ); 898computeTrivialInfo (s ,a ); 899 900//Debug stuff 901if (_debug >=2 ) { 902DOUT2 (" Showing action states" << std::endl ); 903showActionStates (s ,a ); 904 } 905 906a -> success = true; 907DOUT1 ("Successfully analyzed replay!" << std::endl ); 908return a ; 909} 910 911}