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