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