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