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