fork/slippc

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

ChrisuChange input calculation for APM to formulas used in slippi-js8d79a66

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