fork/slippc

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

Patrick CrainAdded actions per minuet and action state changes per minute stats to analyzer; added total counts for stick movements and button pushes; added max GALINT stat30c55ad

0.6.0
13.7 KiB317 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.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:
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     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     showActionStates           (const SlippiReplay &s, Analysis *a) const;
44  void     computeTrivialInfo         (const SlippiReplay &s, Analysis *a) const;
45  unsigned countTransitions           (const SlippiReplay &s, Analysis *a, unsigned pnum, bool (*cb)(const SlippiFrame&)) const;
46  unsigned countTransitions           (const SlippiReplay &s, Analysis *a, unsigned pnum, bool (*cb)(const SlippiPlayer &, const unsigned)) const;
47
48  static inline float getHitStun(const SlippiFrame &f) {
49    return f.hitstun;
50  }
51  static inline float playerDistance(const SlippiFrame &pf, const SlippiFrame &of) {
52    float xd = pf.pos_x_pre - of.pos_x_pre;
53    float yd = pf.pos_y_pre - of.pos_y_pre;
54    return sqrt(xd*xd+yd*yd);
55  }
56  static inline bool isOffStage(const SlippiReplay &s, const SlippiFrame &f) {
57    return isAirborne(f) && (
58      f.pos_x_pre >  Stage::ledge[s.stage] ||
59      f.pos_x_pre < -Stage::ledge[s.stage] ||
60      f.pos_y_pre <  0);
61  }
62  static 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
69    return isInHitlag(p.frame[f-2]) &&
70      (not isInHitlag(o.frame[f-2])) &&
71      (not isInHitstun(p.frame[f])) &&
72      (not isThrowing(o.frame[f])) &&
73      p.frame[f-1].percent_pre < p.frame[f].percent_post
74      ;
75  }
76  static inline bool wasShieldStabbed(const SlippiPlayer &p, const unsigned f) {
77    return 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  }
81  static inline bool wasStageSpiked(const SlippiFrame &f) {
82    return (f.action_pre == Action::FlyReflectWall || f.action_pre == Action::FlyReflectCeil)
83      && f.action_post <= Action::DeadUpFallHitCameraIce;
84  }
85  static inline bool didEdgeCancelAerial(const SlippiFrame &f) {
86    return f.action_post >= Action::Fall
87      && f.action_post <= Action::FallB
88      && f.action_pre >= Action::LandingAirN
89      && f.action_pre <= Action::LandingAirLw;
90  }
91  static inline bool didTeeterCancelAerial(const SlippiFrame &f) {
92    return f.action_post >= Action::Ottotto
93      && f.action_post <= Action::OttottoWait
94      && f.action_pre >= Action::LandingAirN
95      && f.action_pre <= Action::LandingAirLw;
96  }
97  static inline bool didAutoCancelAerial(const SlippiFrame &f) {
98    return f.action_post == Action::Landing
99      && f.action_pre >= Action::AttackAirN
100      && f.action_pre <= Action::AttackAirLw;
101  }
102  static inline bool didNoImpactLand(const SlippiFrame &f) {
103    return f.action_pre >= Action::JumpF
104      && f.action_pre <= Action::JumpAerialB
105      && f.action_post == Action::Wait;
106  }
107  static inline bool didShieldDrop(const SlippiFrame &f) {
108    return f.action_pre >= Action::GuardOn
109      && f.action_pre <= Action::GuardOff
110      && f.action_post == Action::Pass;
111  }
112  static inline bool didEdgeCancelSpecial(const SlippiFrame &f) {
113    return f.action_post >= Action::Fall
114      && f.action_post <= Action::FallB
115      && f.action_pre == Action::LandingFallSpecial;
116  }
117  static inline bool didTeeterCancelSpecial(const SlippiFrame &f) {
118    return f.action_post >= Action::Ottotto
119      && f.action_post <= Action::OttottoWait
120      && f.action_pre == Action::LandingFallSpecial;
121  }
122  static inline bool didPivot(const SlippiPlayer &p, const unsigned f) {
123    return 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  }
129  static inline bool isJumpHeld(const SlippiPlayer &p, const unsigned f) {
130    return p.frame[f].buttons & 0x0C00; //0000 1100 0000 0000
131  }
132  static inline bool didHop(const SlippiPlayer &p, const unsigned f) {
133    return p.frame[f-1].action_post == Action::KneeBend
134      && (p.frame[f].action_post == Action::JumpF || p.frame[f].action_post == Action::JumpB);
135  }
136  static inline bool didShortHop(const SlippiPlayer &p, const unsigned f) {
137    return didHop(p,f) && (not isJumpHeld(p,f));
138  }
139  static inline bool didPowerShield(const SlippiPlayer &p, const unsigned f) {
140    return (p.frame[f].flags_4 & 0x20) && (not(p.frame[f-1].flags_4 & 0x20));
141  }
142  static inline bool didMeteorCancel(const SlippiPlayer &p, const unsigned f) {
143    return 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  }
150  static inline bool didCliffCatchEnd(const SlippiPlayer &p, const unsigned f) {
151    return p.frame[f-1].action_pre == Action::CliffCatch && p.frame[f].action_pre != Action::CliffCatch;
152  }
153  static inline bool didReleaseLedge(const SlippiPlayer &p, const unsigned f) {
154    return (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  }
157  static inline unsigned deathDirection(const SlippiPlayer &p, const unsigned f) {
158    if (p.frame[f].action_post == Action::DeadDown)  { return Dir::DOWN; }
159    if (p.frame[f].action_post == Action::DeadLeft)  { return Dir::LEFT; }
160    if (p.frame[f].action_post == Action::DeadRight) { return Dir::RIGHT; }
161    if (p.frame[f].action_post <  Action::Sleep)     { return Dir::UP; }
162    return 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.
167  static inline bool maybeWavelanding(const SlippiPlayer &p, const unsigned f) {
168    //Code credit to Fizzi
169    return p.frame[f].action_pre == Action::LandingFallSpecial && (
170      p.frame[f-1].action_pre == Action::EscapeAir || (
171        p.frame[f-1].action_pre >= Action::KneeBend &&
172        p.frame[f-1].action_pre <= Action::FallAerialB
173        )
174      );
175  }
176  static 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
179    return (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  }
183  static inline bool isShieldBroken(const SlippiFrame &f) {
184    return f.action_pre == Action::ShieldBreakFly
185      || f.action_pre == Action::ShieldBreakFall;
186  }
187  static inline bool isInJumpsquat(const SlippiFrame &f) {
188    return f.action_pre == Action::KneeBend;
189  }
190  static inline bool isSpotdodging(const SlippiFrame &f) {
191    return f.action_pre == Action::Escape;
192  }
193  static inline bool isAirdodging(const SlippiFrame &f) {
194    return f.action_pre == Action::EscapeAir;
195  }
196  static inline bool isGrabbing(const SlippiFrame &f) {
197    return (f.action_pre >= Action::CatchPull) && (f.action_pre <= Action::CatchAttack);
198  }
199  static inline bool isTaunting(const SlippiFrame &f) {
200    return (f.action_pre == Action::AppealR) || (f.action_pre == Action::AppealL);
201  }
202  static inline bool isReleasing(const SlippiFrame &f) {
203    return f.action_pre == Action::CatchCut;
204  }
205  static inline bool isRolling(const SlippiFrame &f) {
206    return (f.action_pre == Action::EscapeF)|| (f.action_pre == Action::EscapeB);
207  }
208  static inline bool isDodging(const SlippiFrame &f) {
209    return (f.action_pre >= Action::EscapeF) && (f.action_pre <= Action::Escape);
210  }
211  static inline bool isLanding(const SlippiFrame &f) {
212    return (f.action_post == Action::Landing) || (f.action_post == Action::LandingFallSpecial);
213  }
214  static inline bool inTumble(const SlippiFrame &f) {
215    return f.action_pre == Action::DamageFall;
216  }
217  static inline bool inDamagedState(const SlippiFrame &f) {
218    return (f.action_pre >= Action::DamageHi1) && (f.action_pre <= Action::DamageFlyRoll);
219  }
220  static inline bool inMissedTechState(const SlippiFrame &f) {
221    return (f.action_pre >= Action::DownBoundU) && (f.action_pre <= Action::DownSpotD);
222  }
223  //Excludes wall techs, wall jumps, and ceiling techs
224  static inline bool inFloorTechState(const SlippiFrame &f) {
225    return (f.action_pre >= Action::DownBoundU) && (f.action_pre <= Action::PassiveStandB);
226  }
227  //Includes wall techs, wall jumps, and ceiling techs
228  static inline bool inTechState(const SlippiFrame &f) {
229    return (f.action_pre >= Action::DownBoundU) && (f.action_pre <= Action::PassiveCeil);
230  }
231  static inline bool isInShieldstun(const SlippiFrame &f) {
232    return f.action_pre == Action::GuardSetOff;
233  }
234  static inline bool isGrabbed(const SlippiFrame &f) {
235    return
236      ((f.action_pre >= Action::CapturePulledHi) && (f.action_pre <= Action::CaptureFoot)) ||
237      ((f.action_pre >= Action::CaptureCaptain) && (f.action_pre <= Action::ThrownKirby));
238  }
239  static inline bool isThrown(const SlippiFrame &f) {
240    return (f.action_pre >= Action::ThrownF) && (f.action_pre <= Action::ThrownLwWomen);
241  }
242  static inline bool isThrowing(const SlippiFrame &f) {
243    return (f.action_pre >= Action::ThrowF) && (f.action_pre <= Action::ThrowLw);
244  }
245  static inline bool isOnLedge(const SlippiFrame &f) {
246    return f.action_pre == Action::CliffWait;
247  }
248  static inline bool didActionStateChange(const SlippiFrame &f) {
249    return f.action_pre != f.action_post;
250  }
251  static inline bool isAirborne(const SlippiFrame &f) {
252    return f.airborne;
253  }
254  static inline bool isInHitlag(const SlippiFrame &f) {
255    return f.flags_2 & 0x20;
256  }
257  static inline bool isShielding(const SlippiFrame &f) {
258    return f.flags_3 & 0x80;
259  }
260  static inline bool isInHitstun(const SlippiFrame &f) {
261    return f.flags_4 & 0x02;
262  }
263  static inline bool isOffscreen(const SlippiFrame &f) {
264    return f.flags_5 & 0x80;
265  }
266  static inline bool isDead(const SlippiFrame &f) {
267    return (f.flags_5 & 0x10) || f.action_pre < Action::Sleep;
268  }
269  static 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
271    if (x1 < 0 && x2 > 0) { return 1; }
272    if (x1 > 0 && x2 < 0) { return 1; }
273    if (y1 < 0 && y2 > 0) { return 1; }
274    if (y1 > 0 && y2 < 0) { return 1; }
275
276    // If the higher absolute magnitude between x and y shifted,
277    //  that's a movement
278    float ax1 = (x1 > 0) ? x1 : -x1;
279    float ax2 = (x2 > 0) ? x2 : -x2;
280    float ay1 = (y1 > 0) ? y1 : -y1;
281    float ay2 = (y2 > 0) ? y2 : -y2;
282    if (ax1 > ay1 && ax2 < ay2) { return 1; }
283    if (ax1 < ay1 && ax2 > ay2) { return 1; }
284
285    // If we moved from neutral to non neutral, that's a movement
286    if (ax1 < neut && ay1 < neut) {
287      if (ax2 > neut || ay2 > neut) { return 1; }
288    }
289
290    return 0;
291  }
292  static inline std::string frameAsTimer(unsigned fnum, unsigned startmins) {
293    if (startmins == 0) {
294      return "no timer";
295    }
296    int startframes = 3600*startmins; //Total number of frames in the match
297    int elapsed    = fnum+LOAD_FRAME; //Number of frames elapsed since timer started
298    if (elapsed < 0) {
299      return std::to_string(startmins) + ":00:00";
300    }
301    int remaining = startframes-elapsed;
302    int lmins     = remaining/3600;
303    int lsecs     = (remaining%3600)/60;
304    int lframes   = remaining%60;
305    return 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:
310  Analyzer(int debug_level);
311  ~Analyzer();
312  Analysis* analyze(const SlippiReplay &s);
313};
314
315}
316
317#endif /* ANALYZER_H_ */