fork/slippc
git clone https://git.julia.blue/fork/slippc
8d79a66
master
1#ifndef ANALYZER_H_ 2#define ANALYZER_H_ 3 4#include <math.h> //sqrt 5 6#include "enums.h" 7#include "replay.h" 8#include "analysis.h" 9 10//Version number for the analyzer 11const unsigned TIMER_MINS = 8 ;//Assuming a fixed 8 minute time for now (TODO: might need to change later) 12const unsigned SHARK_THRES = 15 ;//Minimum frames to be out of hitstun before comboing becomes sharking 13const unsigned POKE_THRES = 30 ;//Frames since either player entered hitstun to consider neutral a poke 14const float FOOTSIE_THRES = 10.0f ;//Distance cutoff between FOOTSIES and POSITIONING dynamics 15const unsigned MAX_WAIT = 15 ;//If we don't act out of wait or stun for this many frames, we're not trying to move 16 17namespace slip { 18 19enum JoystickRegion { 20DZ = 0 , 21NE = 1 , 22SE = 2 , 23SW = 3 , 24NW = 4 , 25N = 5 , 26E = 6 , 27S = 7 , 28W = 8 , 29}; 30 31class Analyzer { 32private : 33int _debug ;//Current debug level 34 35bool get1v1Ports (const SlippiReplay & s ,Analysis * a )const ; 36void analyzeInteractions (const SlippiReplay & s ,Analysis * a )const ; 37void analyzePunishes (const SlippiReplay & s ,Analysis * a )const ; 38void analyzeCancels (const SlippiReplay & s ,Analysis * a )const ; 39void analyzeShield (const SlippiReplay & s ,Analysis * a )const ; 40void getBasicGameInfo (const SlippiReplay & s ,Analysis * a )const ; 41void summarizeInteractions (const SlippiReplay & s ,Analysis * a )const ; 42void computeAirtime (const SlippiReplay & s ,Analysis * a )const ; 43void countLCancels (const SlippiReplay & s ,Analysis * a )const ; 44void countButtons (const SlippiReplay & s ,Analysis * a )const ; 45void countTechs (const SlippiReplay & s ,Analysis * a )const ; 46void countDashdances (const SlippiReplay & s ,Analysis * a )const ; 47void countAirdodgesAndWavelands (const SlippiReplay & s ,Analysis * a )const ; 48void countBasicAnimations (const SlippiReplay & s ,Analysis * a )const ; 49void countPhantoms (const SlippiReplay & s ,Analysis * a )const ; 50void countLedgedashes (const SlippiReplay & s ,Analysis * a )const ; 51void countActionability (const SlippiReplay & s ,Analysis * a )const ; 52void countMoves (const SlippiReplay & s ,Analysis * a )const ; 53void showActionStates (const SlippiReplay & s ,Analysis * a )const ; 54void computeTrivialInfo (const SlippiReplay & s ,Analysis * a )const ; 55unsigned countTransitions (const SlippiReplay & s ,Analysis * a ,unsigned pnum ,bool (* cb )(const SlippiFrame & ))const ; 56unsigned countTransitions (const SlippiReplay & s ,Analysis * a ,unsigned pnum ,bool (* cb )(const SlippiPlayer & ,const unsigned ))const ; 57 58static inline float getHitStun (const SlippiFrame & f ) { 59return f .hitstun ; 60 } 61static inline float playerDistance (const SlippiFrame & pf ,const SlippiFrame & of ) { 62float xd = pf .pos_x_pre - of .pos_x_pre ; 63float yd = pf .pos_y_pre - of .pos_y_pre ; 64return sqrt (xd * xd + yd * yd ); 65 } 66static inline bool isOffStage (const SlippiReplay & s ,const SlippiFrame & f ) { 67return isAirborne (f )&& ( 68f .pos_x_pre > Stage ::ledge [s .stage ]|| 69f .pos_x_pre < - Stage ::ledge [s .stage ]|| 70f .pos_y_pre < -10.0f );//Smaller than zero to account for ECB shenanigans 71 } 72static inline bool wasHitByPhantom (const SlippiPlayer & p ,const SlippiPlayer & o ,const unsigned f ) { 73//Phantom detection: 74//Defender is in hitlag at least 2 frames before taking damage 75//Attacker is NOT in hitlag at least 2 frames before taking damage 76//Defender is NOT in hitstun -> TODO: misses edge cases where players are hit by phantoms while in hitstun from another attack 77//Attacker is not throwing 78//Defender's damage increased since last frame 79return isInHitlag (p .frame [f - 2 ])&& 80 (not isInHitlag (o .frame [f - 2 ]))&& 81 (not isInHitstun (p .frame [f ]))&& 82 (not isThrowing (o .frame [f ]))&& 83p .frame [f - 1 ].percent_pre < p .frame [f ].percent_post 84 ; 85 } 86static inline bool wasShieldStabbed (const SlippiPlayer & p ,const unsigned f ) { 87return p .frame [f - 1 ].action_post >=Action ::GuardOn 88&& p .frame [f - 1 ].action_post <=Action ::GuardReflect 89&& p .frame [f ].percent_post > p .frame [f - 1 ].percent_post ; 90 } 91static inline bool wasStageSpiked (const SlippiFrame & f ) { 92return (f .action_pre == Action ::FlyReflectWall || f .action_pre == Action ::FlyReflectCeil ) 93&& f .action_post <=Action ::DeadUpFallHitCameraIce ; 94 } 95static inline bool didEdgeCancelAerial (const SlippiFrame & f ) { 96return f .action_post >=Action ::Fall 97&& f .action_post <=Action ::FallB 98&& f .action_pre >=Action ::LandingAirN 99&& f .action_pre <=Action ::LandingAirLw ; 100 } 101static inline bool didTeeterCancelAerial (const SlippiFrame & f ) { 102return f .action_post >=Action ::Ottotto 103&& f .action_post <=Action ::OttottoWait 104&& f .action_pre >=Action ::LandingAirN 105&& f .action_pre <=Action ::LandingAirLw ; 106 } 107static inline bool didAutoCancelAerial (const SlippiFrame & f ) { 108return f .action_post == Action ::Landing 109&& f .action_pre >=Action ::AttackAirN 110&& f .action_pre <=Action ::AttackAirLw ; 111 } 112static inline bool didNoImpactLand (const SlippiFrame & f ) { 113return f .action_pre >=Action ::JumpF 114&& f .action_pre <=Action ::JumpAerialB 115&& f .action_post == Action ::Wait ; 116 } 117static inline bool didShieldDrop (const SlippiFrame & f ) { 118return f .action_pre >=Action ::GuardOn 119&& f .action_pre <=Action ::GuardOff 120&& f .action_post == Action ::Pass ; 121 } 122static inline bool didEdgeCancelSpecial (const SlippiFrame & f ) { 123return f .action_post >=Action ::Fall 124&& f .action_post <=Action ::FallB 125&& f .action_pre == Action ::LandingFallSpecial ; 126 } 127static inline bool didTeeterCancelSpecial (const SlippiFrame & f ) { 128return f .action_post >=Action ::Ottotto 129&& f .action_post <=Action ::OttottoWait 130&& f .action_pre == Action ::LandingFallSpecial ; 131 } 132static inline bool didPivot (const SlippiPlayer & p ,const unsigned f ) { 133return p .frame [f ].action_pre == Action ::Turn 134&& p .frame [f - 1 ].action_pre == Action ::Dash 135&& p .frame [f ].action_post != Action ::Dash 136&& (not isInHitstun (p .frame [f ])) 137 ; 138 } 139static inline bool isJumpHeld (const SlippiPlayer & p ,const unsigned f ) { 140return p .frame [f ].buttons & 0x0C00 ;//0000 1100 0000 0000 141 } 142static inline bool didHop (const SlippiPlayer & p ,const unsigned f ) { 143return p .frame [f - 1 ].action_post == Action ::KneeBend 144&& (p .frame [f ].action_post == Action ::JumpF || p .frame [f ].action_post == Action ::JumpB ); 145 } 146static inline bool didShortHop (const SlippiPlayer & p ,const unsigned f ) { 147return didHop (p ,f )&& (not isJumpHeld (p ,f )); 148 } 149static inline bool didPowerShield (const SlippiPlayer & p ,const unsigned f ) { 150return (p .frame [f ].flags_4 & 0x20 )&& (not (p .frame [f - 1 ].flags_4 & 0x20 )); 151 } 152static inline bool didMeteorCancel (const SlippiPlayer & p ,const unsigned f ) { 153return isInHitstun (p .frame [f - 1 ]) 154&& (getHitStun (p .frame [f - 1 ]) >=2.0f ) 155&& (!isInHitstun (p .frame [f ])) 156&& (p .frame [f ].action_post > Action ::Wait1 157|| p .frame [f ].action_post == Action ::JumpAerialF 158|| p .frame [f ].action_post == Action ::JumpAerialB ); 159 } 160static inline bool didCliffCatchEnd (const SlippiPlayer & p ,const unsigned f ) { 161return p .frame [f - 1 ].action_pre == Action ::CliffCatch && p .frame [f ].action_pre != Action ::CliffCatch ; 162 } 163static inline bool didReleaseLedge (const SlippiPlayer & p ,const unsigned f ) { 164return (p .frame [f - 1 ].action_pre == Action ::CliffWait || p .frame [f - 1 ].action_pre == Action ::CliffCatch ) 165&& p .frame [f ].action_pre == Action ::Fall ; 166 } 167static inline unsigned deathDirection (const SlippiPlayer & p ,const unsigned f ) { 168if (p .frame [f ].action_post == Action ::DeadDown ) {return Dir ::DOWN ; } 169if (p .frame [f ].action_post == Action ::DeadLeft ) {return Dir ::LEFT ; } 170if (p .frame [f ].action_post == Action ::DeadRight ) {return Dir ::RIGHT ; } 171if (p .frame [f ].action_post < Action ::Sleep ) {return Dir ::UP ; } 172return Dir ::NEUT ; 173 } 174//NOTE: the next few functions do not check for valid frame indices 175// This is technically unsafe, but boolean shortcut logic should ensure the unsafe 176// portions never get called. 177static inline bool maybeWavelanding (const SlippiPlayer & p ,const unsigned f ) { 178//Code credit to Fizzi 179return p .frame [f ].action_pre == Action ::LandingFallSpecial && ( 180p .frame [f - 1 ].action_pre == Action ::EscapeAir || ( 181p .frame [f - 1 ].action_pre >=Action ::KneeBend && 182p .frame [f - 1 ].action_pre <=Action ::FallAerialB 183 ) 184 ); 185 } 186static inline bool isDashdancing (const SlippiPlayer & p ,const unsigned f ) { 187//Code credit to Fizzi. This SHOULD never thrown an exception, since we 188// should never be in turn animation before frame 2 189return (p .frame [f ].action_pre == Action ::Dash ) 190&& (p .frame [f - 1 ].action_pre == Action ::Turn ) 191&& (p .frame [f - 2 ].action_pre == Action ::Dash ); 192 } 193static inline bool isShieldBroken (const SlippiFrame & f ) { 194return f .action_pre == Action ::ShieldBreakFly 195|| f .action_pre == Action ::ShieldBreakFall ; 196 } 197static inline bool isInJumpsquat (const SlippiFrame & f ) { 198return f .action_pre == Action ::KneeBend ; 199 } 200static inline bool isSpotdodging (const SlippiFrame & f ) { 201return f .action_pre == Action ::Escape ; 202 } 203static inline bool isAirdodging (const SlippiFrame & f ) { 204return f .action_pre == Action ::EscapeAir ; 205 } 206static inline bool isGrabbing (const SlippiFrame & f ) { 207return (f .action_pre >=Action ::CatchPull )&& (f .action_pre <=Action ::CatchAttack ); 208 } 209static inline bool isTaunting (const SlippiFrame & f ) { 210return (f .action_pre == Action ::AppealR )|| (f .action_pre == Action ::AppealL ); 211 } 212static inline bool isReleasing (const SlippiFrame & f ) { 213return f .action_pre == Action ::CatchCut ; 214 } 215static inline bool isRolling (const SlippiFrame & f ) { 216return (f .action_pre == Action ::EscapeF )|| (f .action_pre == Action ::EscapeB ); 217 } 218static inline bool isDodging (const SlippiFrame & f ) { 219return (f .action_pre >=Action ::EscapeF )&& (f .action_pre <=Action ::Escape ); 220 } 221static inline bool isLanding (const SlippiFrame & f ) { 222return (f .action_post == Action ::Landing )|| (f .action_post == Action ::LandingFallSpecial ); 223 } 224static inline bool inTumble (const SlippiFrame & f ) { 225return f .action_pre == Action ::DamageFall ; 226 } 227static inline bool inDamagedState (const SlippiFrame & f ) { 228return (f .action_pre >=Action ::DamageHi1 )&& (f .action_pre <=Action ::DamageFlyRoll ); 229 } 230static inline bool inMissedTechState (const SlippiFrame & f ) { 231return (f .action_pre >=Action ::DownBoundU )&& (f .action_pre <=Action ::DownSpotD ); 232 } 233//Excludes wall techs, wall jumps, and ceiling techs 234static inline bool inFloorTechState (const SlippiFrame & f ) { 235return (f .action_pre >=Action ::DownBoundU )&& (f .action_pre <=Action ::PassiveStandB ); 236 } 237//Includes wall techs, wall jumps, and ceiling techs 238static inline bool inTechState (const SlippiFrame & f ) { 239return (f .action_pre >=Action ::DownBoundU )&& (f .action_pre <=Action ::PassiveCeil ); 240 } 241static inline bool isInShield (const SlippiFrame & f ) { 242return f .action_pre >=Action ::GuardOn && f .action_pre <=Action ::GuardReflect ; 243 } 244static inline bool isInShieldstun (const SlippiFrame & f ) { 245return f .action_pre == Action ::GuardSetOff ; 246 } 247static inline bool isGrabbed (const SlippiFrame & f ) { 248return 249 ((f .action_pre >=Action ::CapturePulledHi )&& (f .action_pre <=Action ::CaptureFoot ))|| 250 ((f .action_pre >=Action ::CaptureCaptain )&& (f .action_pre <=Action ::ThrownKirby )); 251 } 252static inline bool isThrown (const SlippiFrame & f ) { 253return (f .action_pre >=Action ::ThrownF )&& (f .action_pre <=Action ::ThrownLwWomen ); 254 } 255static inline bool isThrowing (const SlippiFrame & f ) { 256return (f .action_pre >=Action ::ThrowF )&& (f .action_pre <=Action ::ThrowLw ); 257 } 258static inline bool isUsingNormalMove (const SlippiFrame & f ) { 259return (f .action_pre >=Action ::Attack11 )&& (f .action_pre <=Action ::AttackAirLw ); 260 } 261static inline bool isUsingSpecialMove (const SlippiFrame & f ,const unsigned pid ) { 262for (unsigned i = 0 ;CharExt ::special [pid ][i ]> 0 ;++ i ) { 263if (f .action_pre == CharExt ::special [pid ][i ]) { 264return true; 265 } 266 } 267return false; 268 } 269static inline bool isUsingMiscMove (const SlippiFrame & f ) { 270return 271f .action_pre == Action ::DownAttackU || //Getup attack up 272f .action_pre == Action ::DownAttackD || //Getup attack down 273f .action_pre == Action ::CliffAttackSlow || //Ledge attack >=100% 274f .action_pre == Action ::CliffAttackQuick //Ledge attack <100% 275 ; 276 } 277static inline bool isUsingGrab (const SlippiFrame & f ) { 278return f .action_pre == Action ::Catch ; 279 } 280static inline bool isUsingPummel (const SlippiFrame & f ) { 281return f .action_pre == Action ::CatchAttack ; 282 } 283static inline bool isInWait (const SlippiFrame & f ) { 284return f .action_pre == Action ::Wait ; 285 } 286static inline bool isInAnyWait (const SlippiFrame & f ) { 287return f .action_pre == Action ::Wait || ((f .action_pre >=Action ::Wait1 )&& (f .action_pre <=Action ::SquatWaitItem )); 288 } 289static inline bool isOnLedge (const SlippiFrame & f ) { 290return f .action_pre == Action ::CliffWait ; 291 } 292static inline bool didActionStateChange (const SlippiFrame & f ) { 293return f .action_pre != f .action_post ; 294 } 295static inline bool isAirborne (const SlippiFrame & f ) { 296return f .airborne ; 297 } 298static inline bool isInHitlag (const SlippiFrame & f ) { 299return f .flags_2 & 0x20 ; 300 } 301static inline bool isShielding (const SlippiFrame & f ) { 302return f .flags_3 & 0x80 ; 303 } 304static inline bool isInHitstun (const SlippiFrame & f ) { 305return f .flags_4 & 0x02 ; 306 } 307static inline bool isInDamageAnimation (const SlippiFrame & f ) { 308return f .action_pre >=Action ::DamageHi1 && f .action_pre <=Action ::DamageFlyRoll ; 309 } 310static inline bool isOffscreen (const SlippiFrame & f ) { 311return f .flags_5 & 0x80 ; 312 } 313static inline bool isDead (const SlippiFrame & f ) { 314return (f .flags_5 & 0x10 )|| f .action_pre < Action ::Sleep ; 315 } 316static inline JoystickRegion getJoystickRegion (float x ,float y ,float neut ) { 317if (x >=neut && y >=neut ) { 318return JoystickRegion ::NE ; 319 } 320else if (x >=neut && y <=- neut ) { 321return JoystickRegion ::SE ; 322 } 323else if (x <=- neut && y <=- neut ) { 324return JoystickRegion ::SW ; 325 } 326else if (x <=- neut && y >=neut ) { 327return JoystickRegion ::NW ; 328 } 329else if (y >=neut ) { 330return JoystickRegion ::N ; 331 } 332else if (x >=neut ) { 333return JoystickRegion ::E ; 334 } 335else if (y <=- neut ) { 336return JoystickRegion ::S ; 337 } 338else if (x <=- neut ) { 339return JoystickRegion ::W ; 340 } 341 342return JoystickRegion ::DZ ; 343 } 344static inline unsigned checkStickMovement (float x1 ,float y1 ,float x2 ,float y2 ,float neut = 0.2875 ) { 345// logic copied from slippi-js 346// https://github.com/project-slippi/slippi-js/blob/master/src/common/stats/inputs.ts#L97 347 348auto region1 = getJoystickRegion (x1 ,y1 ,neut ); 349 autoregion2 = getJoystickRegion (x2 ,y2 ,neut ); 350 351return region1 != JoystickRegion ::DZ && region1 != region2 ?1 :0 ; 352 } 353static inline unsigned checkTriggerMovement (float trigCurrent ,float trigLast ,float threshold = 0.3 ) { 354// logic copied from slippi-js 355// https://github.com/project-slippi/slippi-js/blob/master/src/common/stats/inputs.ts#L110 356 357return trigLast < threshold && trigCurrent >=threshold ?1 :0 ; 358 } 359static inline std ::string frameAsTimer (unsigned fnum ,unsigned startmins ) { 360if (startmins == 0 ) { 361return "no timer" ; 362 } 363int startframes = 3600 * startmins ;//Total number of frames in the match 364int elapsed = fnum + LOAD_FRAME ;//Number of frames elapsed since timer started 365if (elapsed < 0 ) { 366return std ::to_string (startmins )+ ":00:00" ; 367 } 368int remaining = startframes - elapsed ; 369int lmins = remaining /3600 ; 370int lsecs = (remaining %3600 )/60 ; 371int lframes = remaining %60 ; 372return std ::to_string (lmins )+ ":" 373+ (lsecs < 10 ?"0" :"" )+ std ::to_string (lsecs )+ ":" 374+ (lframes < 6 ?"0" :"" )+ std ::to_string (int (100 * (float )lframes /60.0f )); 375 } 376public : 377Analyzer (int debug_level ); 378~ Analyzer (); 379Analysis * analyze ( const SlippiReplay & s); 380}; 381 382} 383 384#endif /* ANALYZER_H_ */