fork/slippc

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

Patrick CrainUnify version strings between parser, analyzer, and compressor into SLIPPC_VERSION46ef8c0

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