fork/slippc

git clone https://git.julia.blue/fork/slippc

Patrick CrainBumper parser and analyzer versions to synchronize with slip.py81596bb

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