fork/slippc

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

Patrick CrainAdded documentation to replay.h; updated readme and changelog; minor tweaks to output of parser and analyzer, including original input filename; bumped parser and analyzer versions to 0.7.07d49c2f

0.7.0
15.6 KiB357 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.7.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
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:
26  int _debug; //Current debug level
27
28  bool     get1v1Ports                (const SlippiReplay &s, Analysis *a) const;
29  void     analyzeInteractions        (const SlippiReplay &s, Analysis *a) const;
30  void     analyzePunishes            (const SlippiReplay &s, Analysis *a) const;
31  void     analyzeCancels             (const SlippiReplay &s, Analysis *a) const;
32  void     analyzeShield              (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     countButtons               (const SlippiReplay &s, Analysis *a) const;
38  void     countTechs                 (const SlippiReplay &s, Analysis *a) const;
39  void     countDashdances            (const SlippiReplay &s, Analysis *a) const;
40  void     countAirdodgesAndWavelands (const SlippiReplay &s, Analysis *a) const;
41  void     countBasicAnimations       (const SlippiReplay &s, Analysis *a) const;
42  void     countPhantoms              (const SlippiReplay &s, Analysis *a) const;
43  void     countLedgedashes           (const SlippiReplay &s, Analysis *a) const;
44  void     countActionability         (const SlippiReplay &s, Analysis *a) const;
45  void     countMoves                 (const SlippiReplay &s, Analysis *a) const;
46  void     showActionStates           (const SlippiReplay &s, Analysis *a) const;
47  void     computeTrivialInfo         (const SlippiReplay &s, Analysis *a) const;
48  unsigned countTransitions           (const SlippiReplay &s, Analysis *a, unsigned pnum, bool (*cb)(const SlippiFrame&)) const;
49  unsigned countTransitions           (const SlippiReplay &s, Analysis *a, unsigned pnum, bool (*cb)(const SlippiPlayer &, const unsigned)) const;
50
51  static inline float getHitStun(const SlippiFrame &f) {
52    return f.hitstun;
53  }
54  static inline float playerDistance(const SlippiFrame &pf, const SlippiFrame &of) {
55    float xd = pf.pos_x_pre - of.pos_x_pre;
56    float yd = pf.pos_y_pre - of.pos_y_pre;
57    return sqrt(xd*xd+yd*yd);
58  }
59  static inline bool isOffStage(const SlippiReplay &s, const SlippiFrame &f) {
60    return isAirborne(f) && (
61      f.pos_x_pre >  Stage::ledge[s.stage] ||
62      f.pos_x_pre < -Stage::ledge[s.stage] ||
63      f.pos_y_pre <  -10.0f);  //Smaller than zero to account for ECB shenanigans
64  }
65  static 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
72    return isInHitlag(p.frame[f-2]) &&
73      (not isInHitlag(o.frame[f-2])) &&
74      (not isInHitstun(p.frame[f])) &&
75      (not isThrowing(o.frame[f])) &&
76      p.frame[f-1].percent_pre < p.frame[f].percent_post
77      ;
78  }
79  static inline bool wasShieldStabbed(const SlippiPlayer &p, const unsigned f) {
80    return 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  }
84  static inline bool wasStageSpiked(const SlippiFrame &f) {
85    return (f.action_pre == Action::FlyReflectWall || f.action_pre == Action::FlyReflectCeil)
86      && f.action_post <= Action::DeadUpFallHitCameraIce;
87  }
88  static inline bool didEdgeCancelAerial(const SlippiFrame &f) {
89    return f.action_post >= Action::Fall
90      && f.action_post <= Action::FallB
91      && f.action_pre >= Action::LandingAirN
92      && f.action_pre <= Action::LandingAirLw;
93  }
94  static inline bool didTeeterCancelAerial(const SlippiFrame &f) {
95    return f.action_post >= Action::Ottotto
96      && f.action_post <= Action::OttottoWait
97      && f.action_pre >= Action::LandingAirN
98      && f.action_pre <= Action::LandingAirLw;
99  }
100  static inline bool didAutoCancelAerial(const SlippiFrame &f) {
101    return f.action_post == Action::Landing
102      && f.action_pre >= Action::AttackAirN
103      && f.action_pre <= Action::AttackAirLw;
104  }
105  static inline bool didNoImpactLand(const SlippiFrame &f) {
106    return f.action_pre >= Action::JumpF
107      && f.action_pre <= Action::JumpAerialB
108      && f.action_post == Action::Wait;
109  }
110  static inline bool didShieldDrop(const SlippiFrame &f) {
111    return f.action_pre >= Action::GuardOn
112      && f.action_pre <= Action::GuardOff
113      && f.action_post == Action::Pass;
114  }
115  static inline bool didEdgeCancelSpecial(const SlippiFrame &f) {
116    return f.action_post >= Action::Fall
117      && f.action_post <= Action::FallB
118      && f.action_pre == Action::LandingFallSpecial;
119  }
120  static inline bool didTeeterCancelSpecial(const SlippiFrame &f) {
121    return f.action_post >= Action::Ottotto
122      && f.action_post <= Action::OttottoWait
123      && f.action_pre == Action::LandingFallSpecial;
124  }
125  static inline bool didPivot(const SlippiPlayer &p, const unsigned f) {
126    return 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  }
132  static inline bool isJumpHeld(const SlippiPlayer &p, const unsigned f) {
133    return p.frame[f].buttons & 0x0C00; //0000 1100 0000 0000
134  }
135  static inline bool didHop(const SlippiPlayer &p, const unsigned f) {
136    return p.frame[f-1].action_post == Action::KneeBend
137      && (p.frame[f].action_post == Action::JumpF || p.frame[f].action_post == Action::JumpB);
138  }
139  static inline bool didShortHop(const SlippiPlayer &p, const unsigned f) {
140    return didHop(p,f) && (not isJumpHeld(p,f));
141  }
142  static inline bool didPowerShield(const SlippiPlayer &p, const unsigned f) {
143    return (p.frame[f].flags_4 & 0x20) && (not(p.frame[f-1].flags_4 & 0x20));
144  }
145  static inline bool didMeteorCancel(const SlippiPlayer &p, const unsigned f) {
146    return 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  }
153  static inline bool didCliffCatchEnd(const SlippiPlayer &p, const unsigned f) {
154    return p.frame[f-1].action_pre == Action::CliffCatch && p.frame[f].action_pre != Action::CliffCatch;
155  }
156  static inline bool didReleaseLedge(const SlippiPlayer &p, const unsigned f) {
157    return (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  }
160  static inline unsigned deathDirection(const SlippiPlayer &p, const unsigned f) {
161    if (p.frame[f].action_post == Action::DeadDown)  { return Dir::DOWN; }
162    if (p.frame[f].action_post == Action::DeadLeft)  { return Dir::LEFT; }
163    if (p.frame[f].action_post == Action::DeadRight) { return Dir::RIGHT; }
164    if (p.frame[f].action_post <  Action::Sleep)     { return Dir::UP; }
165    return 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.
170  static inline bool maybeWavelanding(const SlippiPlayer &p, const unsigned f) {
171    //Code credit to Fizzi
172    return p.frame[f].action_pre == Action::LandingFallSpecial && (
173      p.frame[f-1].action_pre == Action::EscapeAir || (
174        p.frame[f-1].action_pre >= Action::KneeBend &&
175        p.frame[f-1].action_pre <= Action::FallAerialB
176        )
177      );
178  }
179  static 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
182    return (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  }
186  static inline bool isShieldBroken(const SlippiFrame &f) {
187    return f.action_pre == Action::ShieldBreakFly
188      || f.action_pre == Action::ShieldBreakFall;
189  }
190  static inline bool isInJumpsquat(const SlippiFrame &f) {
191    return f.action_pre == Action::KneeBend;
192  }
193  static inline bool isSpotdodging(const SlippiFrame &f) {
194    return f.action_pre == Action::Escape;
195  }
196  static inline bool isAirdodging(const SlippiFrame &f) {
197    return f.action_pre == Action::EscapeAir;
198  }
199  static inline bool isGrabbing(const SlippiFrame &f) {
200    return (f.action_pre >= Action::CatchPull) && (f.action_pre <= Action::CatchAttack);
201  }
202  static inline bool isTaunting(const SlippiFrame &f) {
203    return (f.action_pre == Action::AppealR) || (f.action_pre == Action::AppealL);
204  }
205  static inline bool isReleasing(const SlippiFrame &f) {
206    return f.action_pre == Action::CatchCut;
207  }
208  static inline bool isRolling(const SlippiFrame &f) {
209    return (f.action_pre == Action::EscapeF)|| (f.action_pre == Action::EscapeB);
210  }
211  static inline bool isDodging(const SlippiFrame &f) {
212    return (f.action_pre >= Action::EscapeF) && (f.action_pre <= Action::Escape);
213  }
214  static inline bool isLanding(const SlippiFrame &f) {
215    return (f.action_post == Action::Landing) || (f.action_post == Action::LandingFallSpecial);
216  }
217  static inline bool inTumble(const SlippiFrame &f) {
218    return f.action_pre == Action::DamageFall;
219  }
220  static inline bool inDamagedState(const SlippiFrame &f) {
221    return (f.action_pre >= Action::DamageHi1) && (f.action_pre <= Action::DamageFlyRoll);
222  }
223  static inline bool inMissedTechState(const SlippiFrame &f) {
224    return (f.action_pre >= Action::DownBoundU) && (f.action_pre <= Action::DownSpotD);
225  }
226  //Excludes wall techs, wall jumps, and ceiling techs
227  static inline bool inFloorTechState(const SlippiFrame &f) {
228    return (f.action_pre >= Action::DownBoundU) && (f.action_pre <= Action::PassiveStandB);
229  }
230  //Includes wall techs, wall jumps, and ceiling techs
231  static inline bool inTechState(const SlippiFrame &f) {
232    return (f.action_pre >= Action::DownBoundU) && (f.action_pre <= Action::PassiveCeil);
233  }
234  static inline bool isInShield(const SlippiFrame &f) {
235    return f.action_pre >= Action::GuardOn && f.action_pre <= Action::GuardReflect;
236  }
237  static inline bool isInShieldstun(const SlippiFrame &f) {
238    return f.action_pre == Action::GuardSetOff;
239  }
240  static inline bool isGrabbed(const SlippiFrame &f) {
241    return
242      ((f.action_pre >= Action::CapturePulledHi) && (f.action_pre <= Action::CaptureFoot)) ||
243      ((f.action_pre >= Action::CaptureCaptain) && (f.action_pre <= Action::ThrownKirby));
244  }
245  static inline bool isThrown(const SlippiFrame &f) {
246    return (f.action_pre >= Action::ThrownF) && (f.action_pre <= Action::ThrownLwWomen);
247  }
248  static inline bool isThrowing(const SlippiFrame &f) {
249    return (f.action_pre >= Action::ThrowF) && (f.action_pre <= Action::ThrowLw);
250  }
251  static inline bool isUsingNormalMove(const SlippiFrame &f) {
252    return (f.action_pre >= Action::Attack11) && (f.action_pre <= Action::AttackAirLw);
253  }
254  static inline bool isUsingSpecialMove(const SlippiFrame &f, const unsigned pid) {
255    for(unsigned i = 0; CharExt::special[pid][i] > 0; ++i) {
256      if (f.action_pre == CharExt::special[pid][i]) {
257        return true;
258      }
259    }
260    return false;
261  }
262  static inline bool isUsingMiscMove(const SlippiFrame &f) {
263    return
264      f.action_pre == Action::DownAttackU      ||  //Getup attack up
265      f.action_pre == Action::DownAttackD      ||  //Getup attack down
266      f.action_pre == Action::CliffAttackSlow  ||  //Ledge attack >=100%
267      f.action_pre == Action::CliffAttackQuick     //Ledge attack <100%
268      ;
269  }
270  static inline bool isUsingGrab(const SlippiFrame &f) {
271    return f.action_pre == Action::Catch;
272  }
273  static inline bool isUsingPummel(const SlippiFrame &f) {
274    return f.action_pre == Action::CatchAttack;
275  }
276  static inline bool isInWait(const SlippiFrame &f) {
277    return f.action_pre == Action::Wait;
278  }
279  static inline bool isInAnyWait(const SlippiFrame &f) {
280    return f.action_pre == Action::Wait || ((f.action_pre >= Action::Wait1) && (f.action_pre <= Action::SquatWaitItem));
281  }
282  static inline bool isOnLedge(const SlippiFrame &f) {
283    return f.action_pre == Action::CliffWait;
284  }
285  static inline bool didActionStateChange(const SlippiFrame &f) {
286    return f.action_pre != f.action_post;
287  }
288  static inline bool isAirborne(const SlippiFrame &f) {
289    return f.airborne;
290  }
291  static inline bool isInHitlag(const SlippiFrame &f) {
292    return f.flags_2 & 0x20;
293  }
294  static inline bool isShielding(const SlippiFrame &f) {
295    return f.flags_3 & 0x80;
296  }
297  static inline bool isInHitstun(const SlippiFrame &f) {
298    return f.flags_4 & 0x02;
299  }
300  static inline bool isInDamageAnimation(const SlippiFrame &f) {
301    return f.action_pre >= Action::DamageHi1 && f.action_pre <= Action::DamageFlyRoll;
302  }
303  static inline bool isOffscreen(const SlippiFrame &f) {
304    return f.flags_5 & 0x80;
305  }
306  static inline bool isDead(const SlippiFrame &f) {
307    return (f.flags_5 & 0x10) || f.action_pre < Action::Sleep;
308  }
309  static 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
311    if (x1 < 0 && x2 > 0) { return 1; }
312    if (x1 > 0 && x2 < 0) { return 1; }
313    if (y1 < 0 && y2 > 0) { return 1; }
314    if (y1 > 0 && y2 < 0) { return 1; }
315
316    // If the higher absolute magnitude between x and y shifted,
317    //  that's a movement
318    float ax1 = (x1 > 0) ? x1 : -x1;
319    float ax2 = (x2 > 0) ? x2 : -x2;
320    float ay1 = (y1 > 0) ? y1 : -y1;
321    float ay2 = (y2 > 0) ? y2 : -y2;
322    if (ax1 > ay1 && ax2 < ay2) { return 1; }
323    if (ax1 < ay1 && ax2 > ay2) { return 1; }
324
325    // If we moved from neutral to non neutral, that's a movement
326    if (ax1 < neut && ay1 < neut) {
327      if (ax2 > neut || ay2 > neut) { return 1; }
328    }
329
330    return 0;
331  }
332  static inline std::string frameAsTimer(unsigned fnum, unsigned startmins) {
333    if (startmins == 0) {
334      return "no timer";
335    }
336    int startframes = 3600*startmins; //Total number of frames in the match
337    int elapsed    = fnum+LOAD_FRAME; //Number of frames elapsed since timer started
338    if (elapsed < 0) {
339      return std::to_string(startmins) + ":00:00";
340    }
341    int remaining = startframes-elapsed;
342    int lmins     = remaining/3600;
343    int lsecs     = (remaining%3600)/60;
344    int lframes   = remaining%60;
345    return 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:
350  Analyzer(int debug_level);
351  ~Analyzer();
352  Analysis* analyze(const SlippiReplay &s);
353};
354
355}
356
357#endif /* ANALYZER_H_ */