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.1
40.4 KiB964 linesraw
1#include "analyzer.h"
2
3//Debug output convenience macros
4#define DOUT1(s) if (_debug >= 1) { std::cout << s; }
5#define DOUT2(s) if (_debug >= 2) { std::cout << s; }
6
7namespace slip {
8
9Analyzer::Analyzer(int debug_level) {
10  _debug = debug_level;
11}
12
13Analyzer::~Analyzer() {
14  //Nothing to do yet
15}
16
17bool Analyzer::get1v1Ports(const SlippiReplay &s, Analysis *a) const {
18  unsigned num_players = 0;
19  for(uint8_t i = 0 ; i < 4; ++i) {
20    if (s.player[i].player_type != 3) {
21      if (num_players == 2) {
22        return false;
23      }
24      a->ap[num_players++].port = i;
25    }
26    if (i == 3 && num_players != 2) {
27      return false;
28    }
29  }
30  DOUT1("Players found on ports " << a->ap[0].port << " and " << a->ap[1].port << std::endl);
31  return true;
32}
33
34void Analyzer::computeAirtime(const SlippiReplay &s, Analysis *a) const {
35    for (unsigned pi = 0; pi < 2; ++pi) {
36      SlippiPlayer p = s.player[a->ap[pi].port];
37      unsigned airframes = 0;
38      for (unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
39        airframes += p.frame[f].airborne;
40      }
41
42      a->ap[pi].air_frames = airframes;
43      // float aircount = 100*((float)airframes / s.last_frame);
44      // std::cout << "  Airborne for " << aircount  << "% of match" << std::endl;
45    }
46}
47
48void Analyzer::getBasicGameInfo(const SlippiReplay &s, Analysis* a) const {
49  a->slippi_version    = s.slippi_version;
50  a->parser_version    = s.parser_version;
51  a->analyzer_version  = ANALYZER_VERSION;
52  a->game_time         = s.start_time;
53  a->game_length       = s.frame_count;
54  a->ap[0].tag_player  = s.player[a->ap[0].port].tag;
55  a->ap[1].tag_player  = s.player[a->ap[1].port].tag;
56  a->ap[0].tag_css     = s.player[a->ap[0].port].tag_css;
57  a->ap[1].tag_css     = s.player[a->ap[1].port].tag_css;
58  a->ap[0].tag_code    = s.player[a->ap[0].port].tag_code;
59  a->ap[1].tag_code    = s.player[a->ap[1].port].tag_code;
60
61  a->ap[0].start_stocks  = s.player[a->ap[0].port].start_stocks;
62  a->ap[1].start_stocks  = s.player[a->ap[1].port].start_stocks;
63  a->ap[0].color         = s.player[a->ap[0].port].color;
64  a->ap[1].color         = s.player[a->ap[1].port].color;
65  a->ap[0].team_id       = s.player[a->ap[0].port].team_id;
66  a->ap[1].team_id       = s.player[a->ap[1].port].team_id;
67  a->ap[0].player_type   = s.player[a->ap[0].port].player_type;
68  a->ap[1].player_type   = s.player[a->ap[1].port].player_type;
69  a->ap[0].cpu_level     = s.player[a->ap[0].port].cpu_level;
70  a->ap[1].cpu_level     = s.player[a->ap[1].port].cpu_level;
71
72  a->ap[0].end_stocks  = s.player[a->ap[0].port].end_stocks;
73  a->ap[1].end_stocks  = s.player[a->ap[1].port].end_stocks;
74  a->ap[0].end_pct     = s.player[a->ap[0].port].frame[s.frame_count-1].percent_pre;
75  a->ap[1].end_pct     = s.player[a->ap[1].port].frame[s.frame_count-1].percent_pre;
76  a->ap[0].char_id     = s.player[a->ap[0].port].ext_char_id;
77  a->ap[1].char_id     = s.player[a->ap[1].port].ext_char_id;
78  a->stage_id          = s.stage;
79  a->ap[0].char_name   = CharExt::name[a->ap[0].char_id];
80  a->ap[1].char_name   = CharExt::name[a->ap[1].char_id];
81  a->stage_name        = Stage::name[s.stage];
82  a->winner_port       = s.winner_id;
83}
84
85void Analyzer::summarizeInteractions(const SlippiReplay &s, Analysis *a) const {
86  // std::cout << "  Summarizing player interactions" << std::endl;
87  for (unsigned f = (PLAYABLE_FRAME-LOAD_FRAME); f < s.frame_count; ++f) {
88    unsigned d = a->dynamics[f];
89    ++(a->ap[0].dyn_counts[d]); //Increase the counter for dynamics across all frames
90    if (d > Dynamic::DEFENSIVE && d < Dynamic::OFFENSIVE) {
91      //Index is the same for the 2nd player for neutral dynamics
92      ++(a->ap[1].dyn_counts[d]);
93    } else {
94      //Index is inverted for the 2nd player for non-neutral dynamics
95      ++(a->ap[1].dyn_counts[Dynamic::__LAST - d]);
96    }
97  }
98  // std::cout << std::fixed; //Show floats in fixed representation
99  // for (unsigned p = 0; p < 2; ++p) {
100  //   // std::cout << "    From the perspective of port " << int(a->ap[p].port+1) << " (" << a->ap[p].char_name << "):" << std::endl;
101  //   for (unsigned i = 0; i < Dynamic::__LAST; ++i) {
102  //     std::string n   = std::to_string(a->ap[p].dyn_counts[i]);
103  //     std::string pct = std::to_string((100*float(a->ap[p].dyn_counts[i])/s.frame_count)).substr(0,5);
104  //     std::cout << "      " << SPACE[6-n.length()] << n << " frames (" << pct
105  //       << "% of game) spent in " << Dynamic::name[i] << std::endl;
106  //   }
107  // }
108}
109
110void Analyzer::countLCancels(const SlippiReplay &s, Analysis *a) const {
111  for (unsigned pi = 0; pi < 2; ++pi) {
112    SlippiPlayer p = s.player[a->ap[pi].port];
113    unsigned cancels_hit  = 0;
114    unsigned cancels_miss = 0;
115    unsigned last_state   = 0;
116    for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
117      if (last_state == 0) {
118        if (p.frame[f].l_cancel == 1) {
119          cancels_hit += 1;
120        } else if (p.frame[f].l_cancel == 2) {
121          cancels_miss += 1;
122        }
123      }
124      last_state = p.frame[f].l_cancel;
125    }
126
127    a->ap[pi].l_cancels_hit    = cancels_hit;
128    a->ap[pi].l_cancels_missed = cancels_miss;
129    float cancels_pct = 100*((float)cancels_hit / (cancels_hit+cancels_miss));
130    // std::cout << "  Hit " << cancels_hit << "/" << cancels_hit+cancels_miss << " l cancels (" << cancels_pct << "%)" << std::endl;
131  }
132}
133
134void Analyzer::countButtons(const SlippiReplay &s, Analysis *a) const {
135  for (unsigned pi = 0; pi < 2; ++pi) {
136    SlippiPlayer p        = s.player[a->ap[pi].port];
137    uint16_t last_buttons = 0;
138    float    last_ax      = 0;
139    float    last_ay      = 0;
140    float    last_cx      = 0;
141    float    last_cy      = 0;
142    for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
143      // Add buttons pressed this frame to button count
144      uint16_t cur_buttons    = p.frame[f].buttons;
145      uint16_t new_buttons    = cur_buttons&(cur_buttons^last_buttons);
146      new_buttons            &= 0x0F70;  //Mask out unused bits
147      a->ap[pi].button_count += countBits(new_buttons);
148      last_buttons            = cur_buttons;
149
150      // Check analog stick for movement
151      float    cur_ax = p.frame[f].joy_x;
152      float    cur_ay = p.frame[f].joy_y;
153      a->ap[pi].astick_count += checkStickMovement(cur_ax,cur_ay,last_ax,last_ay);
154      last_ax = cur_ax;
155      last_ay = cur_ay;
156
157      // Check C stick for movement
158      float    cur_cx = p.frame[f].c_x;
159      float    cur_cy = p.frame[f].c_y;
160      a->ap[pi].cstick_count += checkStickMovement(cur_cx,cur_cy,last_cx,last_cy);
161      last_cx = cur_cx;
162      last_cy = cur_cy;
163    }
164  }
165}
166
167void Analyzer::countTechs(const SlippiReplay &s, Analysis *a) const {
168  for (unsigned pi = 0; pi < 2; ++pi) {
169    SlippiPlayer p             = s.player[a->ap[pi].port];
170    unsigned techs_hit         = 0;
171    unsigned walltechs_hit     = 0;  //And ceiling techs
172    unsigned walljumps_hit     = 0;
173    unsigned walljumptechs_hit = 0;
174    unsigned techs_missed      = 0;
175    bool     teching           = false;
176
177    for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
178      if (inTechState(p.frame[f])) {
179        if (not teching) {
180          teching = true;
181          if (p.frame[f].action_pre <= Action::DownSpotD) {
182            ++techs_missed;
183          } else if (p.frame[f].action_pre <= Action::PassiveStandB) {
184            ++techs_hit;
185          } else if (p.frame[f].action_pre == Action::PassiveWallJump) {
186            if (inDamagedState(p.frame[f-1]) || inTumble(p.frame[f-1])) {
187              ++walljumptechs_hit;
188            } else {
189              ++walljumps_hit;
190            }
191          } else {
192            ++walltechs_hit; //Or ceiling techs
193          }
194        }
195      } else {
196        teching = false;
197      }
198    }
199
200    a->ap[pi].techs         = techs_hit;
201    a->ap[pi].walltechs     = walltechs_hit;
202    a->ap[pi].walljumps     = walljumps_hit;
203    a->ap[pi].walltechjumps = walljumptechs_hit;
204    a->ap[pi].missed_techs  = techs_missed;
205
206    // std::cout << "  Hit "    << techs_hit         << " grounded techs"  << std::endl;
207    // std::cout << "  Hit "    << walltechs_hit     << " wall techs"      << std::endl;
208    // std::cout << "  Hit "    << walljumps_hit     << " wall jumps"      << std::endl;
209    // std::cout << "  Hit "    << walljumptechs_hit << " wall jump techs" << std::endl;
210    // std::cout << "  Missed " << techs_missed      << " techs"           << std::endl;
211  }
212}
213
214void Analyzer::countDashdances(const SlippiReplay &s, Analysis *a) const {
215  for (unsigned pi = 0; pi < 2; ++pi) {
216    SlippiPlayer p = s.player[a->ap[pi].port];
217    unsigned dashdances = 0;
218    for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
219      if (isDashdancing(p,f)) {
220        ++dashdances;
221      }
222    }
223
224    a->ap[pi].dashdances = dashdances;
225    // std::cout << "  Dashdanced " << dashdances  << " times" << std::endl;
226  }
227}
228
229//Code adapted from Fizzi's
230void Analyzer::countAirdodgesAndWavelands(const SlippiReplay &s, Analysis *a) const {
231  for (unsigned pi = 0; pi < 2; ++pi) {
232    SlippiPlayer p = s.player[a->ap[pi].port];
233    unsigned airdodges  = 0;
234    unsigned wavelands  = 0;
235    unsigned wavedashes = 0;
236    bool     airdodging = false;
237    for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
238      if (maybeWavelanding(p,f)) {  //Waveland detection by Fizzi
239        //Look at the last 8 frames (including this one) re Fizzi
240        bool foundAirdodge  = false;
241        bool foundJumpsquat = false;
242        bool foundOther     = false;
243        for(unsigned i = 1; i < 8; ++i) {
244          if (isAirdodging(p.frame[f-i])) {
245            foundAirdodge = true;
246          } else if (isInJumpsquat(p.frame[f-i])) {
247            foundJumpsquat = true;
248            break;
249          } else {
250            foundOther = true;
251          }
252        }
253        if (foundAirdodge) {
254          if ((not foundJumpsquat) && (not foundOther)) {
255            continue; //If the airdodge is the only animation we found, proceed
256          } else {
257            --airdodges;  //Otherwise, subtract it from our airdodge count and keep checkings
258          }
259        }
260        if (foundJumpsquat) {  //If we were in jumpsquat at all recently, we're wavedashing
261          ++wavedashes;
262        } else if (foundOther) {  //If we were in any other animation besides airdodge, it's a waveland
263          ++wavelands;
264        }  //Otherwise, nothing special happened
265      } else if (isAirdodging(p.frame[f])) {
266        if (not airdodging) {
267          ++airdodges;
268          airdodging = true;
269        }
270      } else {
271        airdodging = false;
272      }
273    }
274
275    a->ap[pi].airdodges  = airdodges;
276    a->ap[pi].wavelands  = wavelands;
277    a->ap[pi].wavedashes = wavedashes;
278    // std::cout << "  Airdodged "  << airdodges   << " times" << std::endl;
279    // std::cout << "  Wavelanded " << wavelands   << " times" << std::endl;
280    // std::cout << "  Wavedashed " << wavedashes  << " times" << std::endl;
281  }
282}
283
284void Analyzer::analyzeInteractions(const SlippiReplay &s, Analysis *a) const {
285  // std::cout << "  Analyzing player interactions" << std::endl;
286  const SlippiPlayer *p                = &(s.player[a->ap[0].port]);
287  const SlippiPlayer *o                = &(s.player[a->ap[1].port]);
288
289  unsigned cur_dynamic                 = Dynamic::POSITIONING; // Current dynamic in effect
290
291  unsigned oLastInHitsun               = 0;  //Last frame opponent was stuck in hitstun
292  unsigned pLastInHitsun               = 0;  //Last frame we were stuck in hitstun
293  unsigned oLastGrounded               = 0;  //Last frame opponent touched solid ground
294  unsigned pLastGrounded               = 0;  //Last frame we touched solid ground'
295  bool     oHitThisFrame               = false;
296  bool     pHitThisFrame               = false;
297  bool     oHitLastFrame               = false;
298  bool     pHitLastFrame               = false;
299
300  //All interactions analyzed from perspective of p (lower port player)
301  for (unsigned f = (PLAYABLE_FRAME-LOAD_FRAME); f < s.frame_count; ++f) {
302    //Important variables for each frame
303    SlippiFrame of     = o->frame[f];
304    SlippiFrame pf     = p->frame[f];
305    oHitLastFrame = oHitThisFrame;
306    oHitThisFrame = false;
307    bool oInHitstun    = isInHitstun(of);
308      if (oInHitstun)    {
309        oLastInHitsun = f;
310        if (of.percent_post > o->frame[f-1].percent_post) {
311          oHitThisFrame = true; //Check if opponent was hit this frame by looking at % last frame
312        }
313      }
314    pHitLastFrame = pHitThisFrame;
315    pHitThisFrame = false;
316    bool pInHitstun    = isInHitstun(pf);
317      if (pInHitstun)    {
318        pLastInHitsun = f;
319        if (pf.percent_post > p->frame[f-1].percent_post) {
320          pHitThisFrame = true; //Check if we were hit this frame by looking at % last frame
321        }
322      }
323    bool oAirborne     = isAirborne(of);
324      if (not oAirborne) { oLastGrounded = f; }
325    bool pAirborne     = isAirborne(pf);
326      if (not pAirborne) { pLastGrounded = f; }
327    bool oIsGrabbed    = isGrabbed(of);
328    bool pIsGrabbed    = isGrabbed(pf);
329    bool oOnLedge      = isOnLedge(of);
330    bool pOnLedge      = isOnLedge(pf);
331    bool oShielding    = isShielding(of);
332    bool pShielding    = isShielding(pf);
333    bool oInShieldstun = isInShieldstun(of);
334    bool pInShieldstun = isInShieldstun(pf);
335    bool oTeching      = inFloorTechState(of);
336    bool pTeching      = inFloorTechState(pf);
337    bool oThrown       = isThrown(of);
338    bool pThrown       = isThrown(pf);
339    bool oHitOffStage  = isOffStage(s,of) && oInHitstun;  //Check if opponent is in hitstun offstage
340    bool pHitOffStage  = isOffStage(s,pf) && pInHitstun;  //Check if we are in hitstun offstage
341    bool oPoked        = ((f - oLastInHitsun) < POKE_THRES);  //Check if opponent has been poked recently
342    bool pPoked        = ((f - pLastInHitsun) < POKE_THRES);  //Check if we have been poked recently
343
344    //If we're airborne and haven't touched the ground since last entering hitstun, we're being punished
345    bool oBeingPunished = oAirborne && (oLastGrounded < oLastInHitsun);
346    bool pBeingPunished = pAirborne && (pLastGrounded < pLastInHitsun);
347
348    //If we also haven't been in hitstun for at least SHARK_THRES frames, we're being sharked
349    bool oBeingSharked = oBeingPunished && ((f - oLastInHitsun) > SHARK_THRES);
350    bool pBeingSharked = pBeingPunished && ((f - pLastInHitsun) > SHARK_THRES);
351
352    //Determine whether neutral would be considered footsies or positioning
353    // unsigned neut_dyn = ( oPoked || pPoked ) ? Dynamic::POKING
354    //   : ((playerDistance(pf,of) > FOOTSIE_THRES) ? Dynamic::POSITIONING : Dynamic::FOOTSIES);
355    unsigned neut_dyn = ((playerDistance(pf,of) > FOOTSIE_THRES) ? Dynamic::POSITIONING : Dynamic::FOOTSIES);
356
357    //First few checks are largely agnostic to cur_dynamic
358    if (isDead(of) || isDead(pf)) {
359      cur_dynamic = Dynamic::POSITIONING;
360    } else if (oHitOffStage && cur_dynamic != Dynamic::RECOVERING) {  //If the opponent is offstage and in hitstun, they're being [possibly reverse] edgeguarded
361      cur_dynamic = Dynamic::EDGEGUARDING;
362    } else if (pHitOffStage && cur_dynamic != Dynamic::EDGEGUARDING) {  //If we're offstage and in hitstun, we're being [possibly reverse] edgeguarded
363      cur_dynamic = Dynamic::RECOVERING;
364    } else if (oIsGrabbed) {  //If we grab the opponent in neutral, it's pressure; on offense, it's a techchase
365      if (cur_dynamic != Dynamic::PRESSURING) {  //Need this to prevent grabs from overwriting themselves
366        cur_dynamic = (cur_dynamic >= Dynamic::OFFENSIVE) ? Dynamic::TECHCHASING : Dynamic::PRESSURING;
367      }
368    } else if (pIsGrabbed) {  //If we are grabbed by the opponent in neutral, we're pressured; on defense, we're techchased
369      if (cur_dynamic != Dynamic::PRESSURED) {  //Need this to prevent grabs from overwriting themselves
370        cur_dynamic = (cur_dynamic <= Dynamic::DEFENSIVE) ? Dynamic::ESCAPING : Dynamic::PRESSURED;
371      }
372    } else if (oInShieldstun) {  //If our opponent is in shieldstun, we are pressureing
373      cur_dynamic = Dynamic::PRESSURING;
374    } else if (pInShieldstun) {  //If we are in shieldstun, we are pressured
375      cur_dynamic = Dynamic::PRESSURED;
376    } else if (oTeching) {  //If opponent is teching, we are techchasing
377      cur_dynamic = Dynamic::TECHCHASING;
378    } else if (pTeching) {  //If we are teching, we are escaping a techchase
379      cur_dynamic = Dynamic::ESCAPING;
380    } else if (oInHitstun && pInHitstun) {  //If we're both in hitstun and neither of us are offstage, it's a trade
381      // cur_dynamic = Dynamic::TRADING;
382      cur_dynamic = Dynamic::POKING; //Update 2019-11-05: trading is no longer a used dynamic; now just poking
383    } else {  //Everything else depends on the cur_dynamic
384      switch (cur_dynamic) {
385        case Dynamic::PRESSURING:
386          if (oHitLastFrame && not oIsGrabbed) { //If opponent was actually hit last frame by a non pummel, we're punishing
387            cur_dynamic = Dynamic::PUNISHING;
388          } else if (oThrown) {  //If the opponent is being thrown, this is a techchase opportunity
389            cur_dynamic = Dynamic::TECHCHASING;
390          } else if ((not oInHitstun) && (not oShielding) && (not oOnLedge) && (not oAirborne)) {  //If the opponent touches the ground w/o shielding, we're back to neutral
391            cur_dynamic = neut_dyn;
392          }
393          break;
394        case Dynamic::PRESSURED:
395          if (pHitLastFrame && not pIsGrabbed) { //If we were actually hit last frame by a non pummel, we're being punishied
396            cur_dynamic = Dynamic::PUNISHED;
397          } else if (pThrown) {  //If we are being thrown, opponent has a techchase opportunity
398            cur_dynamic = Dynamic::ESCAPING;
399          } else if ((not pInHitstun) && (not pShielding) && (not pOnLedge) && (not pAirborne)) {  //If we touch the ground w/o shielding, we're back to neutral
400            cur_dynamic = neut_dyn;
401          }
402          break;
403        case Dynamic::EDGEGUARDING:
404          if (oOnLedge || (not oAirborne && not oPoked)) {  //If they make it back, we're back to neutral
405            cur_dynamic = neut_dyn;
406          }
407          break;
408        case Dynamic::RECOVERING:
409          if (pOnLedge || (not pAirborne && not pPoked)) {  //If we make it back, we're back to neutral
410            cur_dynamic = neut_dyn;
411          }
412          break;
413        case Dynamic::TECHCHASING:
414          if (oHitLastFrame && (not oTeching) && oAirborne) {
415            cur_dynamic = Dynamic::PUNISHING; //If we started comboing an opponent after the techchase, it's a punish
416          } else if ((not oInHitstun) && (not oIsGrabbed) && (not oThrown) && (not oTeching) && (not oShielding)) {
417            cur_dynamic = oAirborne ? Dynamic::SHARKING : neut_dyn;  //If the opponent escaped our tech chase, back to neutral it is
418          }
419          break;
420        case Dynamic::ESCAPING:
421          if (pHitLastFrame && (not pTeching) && pAirborne) {
422            cur_dynamic = Dynamic::PUNISHED; //If we started comboing an opponent after the techchase, it's a punish
423          } else if ((not pInHitstun) && (not pIsGrabbed) && (not pThrown) && (not pTeching) && (not pShielding)) {
424            cur_dynamic = pAirborne ? Dynamic::GROUNDING : neut_dyn;  //If we escaped our opponent's tech chase, back to neutral it is
425          }
426          break;
427        case Dynamic::PUNISHING:
428          if (oBeingSharked) {  //If we let too much hitstun elapse, but our opponent is still airborne, we're sharking
429            cur_dynamic = Dynamic::SHARKING;
430          } else if ((not oAirborne) && (not oPoked)) {  //If our opponent has outright landed, we're back to neutral
431            cur_dynamic = neut_dyn;
432          }
433          break;
434        case Dynamic::PUNISHED:
435          if (pBeingSharked) {  //If we survive punishes long enough, but are still airborne, we're being sharked
436            cur_dynamic = Dynamic::GROUNDING;
437          } else if ((not pAirborne) && (not pPoked)) {  //If we have outright landed, we're back to neutral
438            cur_dynamic = neut_dyn;
439          }
440          break;
441        case Dynamic::SHARKING:
442          if (oHitLastFrame) {
443            cur_dynamic = Dynamic::PUNISHING;  //If we hit our opponent LAST frame, we're comboing again
444          } else if (pInHitstun) { //If our opponent hits us while sharking, we're back to poking
445            cur_dynamic = Dynamic::POKING;
446          } else if (not oAirborne) {  //If our opponent has outright landed, we're back to neutral
447            cur_dynamic = neut_dyn;
448          }
449          break;
450        case Dynamic::GROUNDING:
451          if (pHitLastFrame) {
452            cur_dynamic = Dynamic::PUNISHED;  //If our opponent hit us LAST frame, we're being comboed again
453          } else if (oInHitstun) { //If we hit our opponent while grounding, we're back to poking
454            cur_dynamic = Dynamic::POKING;
455          } else if (not pAirborne) {  //If we have outright landed, we're back to neutral
456            cur_dynamic = neut_dyn;
457          }
458          break;
459        case Dynamic::POKING:
460          if (oPoked && oHitThisFrame) {
461            cur_dynamic = Dynamic::PUNISHING; //If we have poked our opponent recently and hit them again, we're punishing
462          } else if (pPoked && pHitThisFrame) {
463            cur_dynamic = Dynamic::PUNISHED; //If we have been poked recently and got hit again, we're being punished
464          } else if (oBeingSharked && not pPoked) {
465            cur_dynamic = Dynamic::SHARKING; //If we'd otherwise be considered as punishing, we're sharking
466          } else if (pBeingSharked && not oPoked) {
467            cur_dynamic = Dynamic::GROUNDING; //If we'd otherwise be considered as being punished, we're being sharked
468          } else {
469            cur_dynamic = ( oPoked || pPoked ) ? Dynamic::POKING : neut_dyn;  //We're in neutral if nobody has been hit recently
470          }
471          break;
472        case Dynamic::POSITIONING:
473        case Dynamic::FOOTSIES:
474        case Dynamic::TRADING:
475          if (oBeingSharked) {
476            cur_dynamic = Dynamic::SHARKING; //If we are sharking, update accordingly
477          } else if (pBeingSharked) {
478            cur_dynamic = Dynamic::GROUNDING; //If we are being sharked, update accordingly
479          } else {
480            cur_dynamic = ( oPoked || pPoked ) ? Dynamic::POKING : neut_dyn;  //If we were trading before, we're in neutral now that we're no longer trading
481          }
482          break;
483        default:
484          break;
485      }
486    }
487
488    //Aggregate results
489    a->dynamics[f] = cur_dynamic;  //Set the dynamic for this frame to the current dynamic computed
490    DOUT1("    " << f << " (" << frameAsTimer(f,s.timer) << ") P1 "
491      << Dynamic::name[cur_dynamic] << " "
492      << std::endl);
493  }
494
495}
496
497void Analyzer::analyzePunishes(const SlippiReplay &s, Analysis *a) const {
498  const SlippiPlayer *p  = &(s.player[a->ap[0].port]);
499  const SlippiPlayer *o  = &(s.player[a->ap[1].port]);
500  unsigned pa            = 0; //Running tally of player attacks
501  unsigned oa            = 0; //Running tally of opponent attacks
502  unsigned pn            = 0; //Running tally of player punishes
503  unsigned on            = 0; //Running tally of opponent punishes
504  unsigned oLastInHitsun = 0;
505  unsigned pLastInHitsun = 0;
506  unsigned cur_dyn       = a->dynamics[FIRST_FRAME];
507  unsigned last_dyn      = a->dynamics[FIRST_FRAME];
508  Punish *pPunishes      = a->ap[0].punishes;
509  Punish *oPunishes      = a->ap[1].punishes;
510  Attack *pAttacks       = a->ap[0].attacks;
511  Attack *oAttacks       = a->ap[1].attacks;
512
513  for (unsigned f = FIRST_FRAME; f < s.frame_count; ++f) {
514    SlippiFrame pf = p->frame[f];
515    SlippiFrame of = o->frame[f];
516    cur_dyn        = a->dynamics[f];
517
518    oLastInHitsun = isInHitstun(o->frame[f]) ? f : oLastInHitsun;
519    pLastInHitsun = isInHitstun(p->frame[f]) ? f : pLastInHitsun;
520    bool oPoked    = ((f - oLastInHitsun) < POKE_THRES);  //Check if opponent has been poked recently
521    bool pPoked    = ((f - pLastInHitsun) < POKE_THRES);  //Check if we have been poked recently
522
523    //Check if we counter-attacked (switched from defense to offense or vice versa)
524    if (cur_dyn <= Dynamic::DEFENSIVE && last_dyn >= Dynamic::OFFENSIVE) {
525      ++a->ap[1].counters;  //If we went from offense to defense, we were countered
526    } else if (cur_dyn >= Dynamic::OFFENSIVE && last_dyn <= Dynamic::DEFENSIVE) {
527      ++a->ap[0].counters;  //If we went from defense to offense, we countered
528    }
529
530    //Check if either player lost a stock
531    if (pf.stocks < p->frame[f-1].stocks) {
532      unsigned ddir = deathDirection(*p,f);
533      if (oa > 0) {
534        oAttacks[oa-1].kill_dir = ddir;
535      }
536      if (oPunishes[on].num_moves > 0) {
537        oPunishes[on].kill_dir = ddir; //TODO: filter out self-destructs somehow
538      } else if (on > 0) {
539        oPunishes[on-1].kill_dir = ddir;
540      }
541      if (cur_dyn == Dynamic::EDGEGUARDING) {
542        ++(a->ap[1].reverse_edgeguards);
543      } else if (cur_dyn != Dynamic::RECOVERING && ddir != Dir::UP) {
544        ++(a->ap[0].self_destructs);
545      }
546    }
547    if (of.stocks < o->frame[f-1].stocks) {
548      unsigned ddir = deathDirection(*o,f);
549      if (pa > 0) {
550        pAttacks[pa-1].kill_dir = ddir;
551      }
552      if (pPunishes[pn].num_moves > 0) {
553        pPunishes[pn].kill_dir = ddir;
554      } else if (pn > 0) {
555        pPunishes[pn-1].kill_dir = ddir;
556      }
557      if (cur_dyn == Dynamic::RECOVERING) {
558        ++(a->ap[0].reverse_edgeguards);
559      } else if (cur_dyn != Dynamic::EDGEGUARDING && ddir != Dir::UP) {
560        ++(a->ap[1].self_destructs);
561      }
562    }
563
564    //Check if either player has dropped a punish off an opening
565    bool pPunishEnd = false;
566    bool oPunishEnd = false;
567    if (f == s.frame_count-1) {
568      pPunishEnd = true;
569      oPunishEnd = true;
570    } else if (cur_dyn != Dynamic::POKING) {
571      if (cur_dyn > Dynamic::DEFENSIVE && not pPoked) {
572        //If the opponent had a punish going and they are no longer on offense, end their punish
573        oPunishEnd = true;
574      }
575      if (cur_dyn < Dynamic::OFFENSIVE && not oPoked) {
576        //If we had a punish going and we are no longer on offense, end our punish
577        pPunishEnd = true;
578      }
579    }
580
581    //Update punish counter for ended punishes
582    if (pPunishEnd && pa > 0) {
583      if (pPunishes[pn].num_moves > 0) {
584        pPunishes[pn].end_frame    = f;
585        pPunishes[pn].end_pct      = of.percent_pre;
586        pPunishes[pn].last_move_id = pf.hit_with;
587        if(pPunishes[pn].num_moves > pAttacks[pa-1].hit_id) {
588          a->ap[0].neutral_wins += 1;
589        } else {
590          a->ap[0].pokes += 1;
591        }
592        ++pn;
593      }
594    }
595    if (oPunishEnd && oa > 0) {
596      if (oPunishes[on].num_moves > 0) {
597        oPunishes[on].end_frame    = f;
598        oPunishes[on].end_pct      = pf.percent_pre;
599        oPunishes[on].last_move_id = of.hit_with;
600        if(oPunishes[on].num_moves > oAttacks[oa-1].hit_id) {
601          a->ap[1].neutral_wins += 1;
602        } else {
603          a->ap[1].pokes += 1;
604        }
605        ++on;
606      }
607    }
608
609    //If the opponent just took damage
610    float o_damage_taken = of.percent_pre - o->frame[f-1].percent_pre;
611    if (o_damage_taken > 0) {
612      //Check for bubble damage
613      pAttacks[pa].move_id    = (isOffscreen(of) && o_damage_taken == 1) ? Move::BUBBLE : pf.hit_with;
614      //Last frame we actually took damage; frame before that gives us the animation frame our move hit
615      pAttacks[pa].anim_frame = p->frame[f-2].action_fc;
616      pAttacks[pa].punish_id  = pn;
617      if (  //Check if this is a consecutive hit from a multihit move
618       pa > 0 &&  //If this isn't our first move
619       pAttacks[pa].move_id == pAttacks[pa-1].move_id &&  //If the last move we hit with had the same id
620       pAttacks[pa].anim_frame > pAttacks[pa-1].anim_frame && //If the action frame counter is higher
621       //If the gap between animation and game frames is less than max hitlag for the move
622       //Formula: https://www.ssbwiki.com/Freeze_frame
623       (f - pAttacks[pa].anim_frame <= (pAttacks[pa-1].frame - pAttacks[pa-1].anim_frame + (pAttacks[pa-1].damage/3+3)))) {
624        pAttacks[pa].hit_id  = pAttacks[pa-1].hit_id+1;
625      } else {
626        pAttacks[pa].hit_id  = 1;
627      }
628      pAttacks[pa].frame      = f;
629      pAttacks[pa].damage     = o_damage_taken;
630      a->ap[0].dyn_damage[cur_dyn] += o_damage_taken; //Dynamic is normal for player
631      pAttacks[pa].opening    = cur_dyn;  //Dynamic is normal for player
632      pAttacks[pa].kill_dir   = Dir::NEUT;
633      ++pa;
634      //If this is the start of a combo
635      if (pPunishes[pn].num_moves == 0) {
636        pPunishes[pn].start_frame = f;
637        pPunishes[pn].start_pct   = o->frame[f-1].percent_pre;
638        pPunishes[pn].kill_dir    = Dir::NEUT;
639      }
640      a->ap[0].damage_dealt      += o_damage_taken;
641      pPunishes[pn].end_frame     = f;
642      pPunishes[pn].end_pct       = of.percent_pre;
643      pPunishes[pn].last_move_id  = pf.hit_with;
644      pPunishes[pn].num_moves    += 1;
645      ++(a->ap[0].move_counts[pf.hit_with]);
646    }
647
648    //If we just took damage
649    float p_damage_taken = pf.percent_pre - p->frame[f-1].percent_pre;
650    if (p_damage_taken > 0) {
651      //Check for bubble damage
652      oAttacks[oa].move_id    = (isOffscreen(pf) && p_damage_taken == 1)  ? Move::BUBBLE : of.hit_with;
653      //Last frame we actually took damage; frame before that gives us the animation frame our move hit
654      oAttacks[oa].anim_frame = o->frame[f-2].action_fc;
655      oAttacks[oa].punish_id  = on;
656      if (  //Check if this is a consecutive hit from a multihit move
657       oa > 0 &&  //If this isn't our first move
658       oAttacks[oa].move_id == oAttacks[oa-1].move_id &&  //If the last move we hit with had the same id
659       oAttacks[oa].anim_frame > oAttacks[oa-1].anim_frame && //If the action frame counter is higher
660       //If the gap between animation and game frames is less than max hitlag for the move
661       (f - oAttacks[oa].anim_frame <= (oAttacks[oa-1].frame - oAttacks[oa-1].anim_frame + (oAttacks[oa-1].damage/3+3)))) {
662        oAttacks[oa].hit_id  = oAttacks[oa-1].hit_id+1;
663      } else {
664        oAttacks[oa].hit_id  = 1;
665      }
666      oAttacks[oa].frame      = f;
667      oAttacks[oa].damage     = p_damage_taken;
668      //Dynamic needs to be flipped around for opponent
669      int oDynamic = (cur_dyn > Dynamic::OFFENSIVE || cur_dyn < Dynamic::DEFENSIVE) ? Dynamic::__LAST-cur_dyn : cur_dyn;
670      a->ap[1].dyn_damage[oDynamic] += p_damage_taken; //Dynamic needs to be flipped around for opponent
671      oAttacks[oa].opening    =  oDynamic;
672      oAttacks[oa].kill_dir   = Dir::NEUT;
673      ++oa;
674      //If this is the start of a combo
675      if (oPunishes[on].num_moves == 0) {
676        oPunishes[on].start_frame = f;
677        oPunishes[on].start_pct   = p->frame[f-1].percent_pre;
678        oPunishes[on].kill_dir    = Dir::NEUT;
679      }
680      a->ap[1].damage_dealt      += p_damage_taken;
681      oPunishes[on].end_frame     = f;
682      oPunishes[on].end_pct       = pf.percent_pre;
683      oPunishes[on].last_move_id  = of.hit_with;
684      oPunishes[on].num_moves    += 1;
685      ++(a->ap[1].move_counts[of.hit_with]);
686    }
687
688    last_dyn = cur_dyn;  //Update the last dynamic
689  }
690}
691
692void Analyzer::analyzeCancels(const SlippiReplay &s, Analysis *a) const {
693  for(unsigned pi = 0; pi < 2; ++pi) {
694    const SlippiPlayer *p = &(s.player[a->ap[pi].port]);
695    Attack *attacks       = a->ap[pi].attacks;
696    for(unsigned i = 0; attacks[i].frame > 0; ++i) {
697      unsigned f             = attacks[i].frame;
698      if (attacks[i].move_id >= Move::NAIR && attacks[i].move_id <= Move::DAIR) {
699        unsigned last_frame = attacks[i].frame+60;
700        if (last_frame > s.frame_count) {
701          last_frame = s.frame_count;
702        }
703        for(unsigned f = attacks[i].frame; f < last_frame; ++f) {
704          //Check if we had the opportunity to edge cancel
705          if (didEdgeCancelAerial(p->frame[f])) {
706            attacks[i].cancel_type = Cancel::EDGE;
707            break;
708          }
709          //Check if we had the opportunity to teeter cancel
710          if (didTeeterCancelAerial(p->frame[f])) {
711            attacks[i].cancel_type = Cancel::TEETER;
712            break;
713          }
714          //Check if we had the opportunity to autocancel
715          if (didAutoCancelAerial(p->frame[f])) {
716            attacks[i].cancel_type = Cancel::AUTO;
717            break;
718          }
719          //Check if we had the opportunity to L cancel
720          if (p->frame[f].l_cancel > 0) {
721            if (p->frame[f].l_cancel == 1) {
722              attacks[i].cancel_type = Cancel::L;
723            }
724            break;
725          }
726        }
727      }
728    }
729  }
730}
731
732void Analyzer::analyzeShield(const SlippiReplay &s, Analysis *a) const {
733  for(unsigned pi = 0; pi < 2; ++pi) {
734    const SlippiPlayer *p = &(s.player[a->ap[pi].port]);
735    for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
736      float shield_damage = (p->frame[f].shield - p->frame[f-1].shield);
737      if (shield_damage > 0) {
738        a->ap[pi].shield_time   += 1;
739        a->ap[pi].shield_damage += shield_damage;
740        if(p->frame[f].shield < a->ap[pi].shield_lowest) {
741          a->ap[pi].shield_lowest = p->frame[f].shield;
742        }
743      }
744    }
745  }
746}
747
748unsigned Analyzer::countTransitions(const SlippiReplay &s, Analysis *a, unsigned pnum, bool (*cb)(const SlippiFrame&)) const {
749  SlippiPlayer p = s.player[a->ap[pnum].port];
750  unsigned counter = 0;
751  bool active = false;
752  for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
753    if (cb(p.frame[f])) {
754      if(not active) {
755        ++counter;
756        active = true;
757      }
758    } else {
759      active = false;
760    }
761  }
762  return counter;
763}
764
765unsigned Analyzer::countTransitions(const SlippiReplay &s, Analysis *a, unsigned pnum, bool (*cb)(const SlippiPlayer &, const unsigned)) const {
766  SlippiPlayer p = s.player[a->ap[pnum].port];
767  unsigned counter = 0;
768  bool active = false;
769  for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
770    if (cb(p,f)) {
771      if(not active) {
772        ++counter;
773        active = true;
774      }
775    } else {
776      active = false;
777    }
778  }
779  return counter;
780}
781
782void Analyzer::countBasicAnimations(const SlippiReplay &s, Analysis *a) const {
783  for (unsigned pi = 0; pi < 2; ++pi) {
784    a->ap[pi].state_changes          = countTransitions(s,a,pi,didActionStateChange);
785
786    a->ap[pi].ledge_grabs            = countTransitions(s,a,pi,isOnLedge);
787    a->ap[pi].rolls                  = countTransitions(s,a,pi,isRolling);
788    a->ap[pi].spotdodges             = countTransitions(s,a,pi,isSpotdodging);
789    a->ap[pi].powershields           = countTransitions(s,a,pi,didPowerShield);
790    a->ap[pi].grabs                  = countTransitions(s,a,pi,isGrabbing);
791    a->ap[pi].taunts                 = countTransitions(s,a,pi,isTaunting);
792    a->ap[pi].meteor_cancels         = countTransitions(s,a,pi,didMeteorCancel);
793    a->ap[pi].hits_blocked           = countTransitions(s,a,pi,isInShieldstun);
794    a->ap[pi].edge_cancel_aerials    = countTransitions(s,a,pi,didEdgeCancelAerial);
795    a->ap[pi].edge_cancel_specials   = countTransitions(s,a,pi,didEdgeCancelSpecial);
796    a->ap[pi].teeter_cancel_aerials  = countTransitions(s,a,pi,didTeeterCancelAerial);
797    a->ap[pi].teeter_cancel_specials = countTransitions(s,a,pi,didTeeterCancelSpecial);
798    a->ap[pi].no_impact_lands        = countTransitions(s,a,pi,didNoImpactLand);
799    a->ap[pi].shield_drops           = countTransitions(s,a,pi,didShieldDrop);
800    a->ap[pi].pivots                 = countTransitions(s,a,pi,didPivot);
801    a->ap[pi].short_hops             = countTransitions(s,a,pi,didShortHop);
802    a->ap[pi].full_hops              = countTransitions(s,a,pi,didHop)-a->ap[pi].short_hops;
803
804    a->ap[pi].shield_breaks          = countTransitions(s,a,1-pi,isShieldBroken);
805    a->ap[pi].grab_escapes           = countTransitions(s,a,1-pi,isReleasing);
806    a->ap[pi].shield_stabs           = countTransitions(s,a,1-pi,wasShieldStabbed);
807    a->ap[pi].stage_spikes           = countTransitions(s,a,1-pi,wasStageSpiked);
808  }
809}
810
811void Analyzer::showActionStates(const SlippiReplay &s, Analysis *a) const {
812  const SlippiPlayer *p         = &(s.player[a->ap[0].port]);
813  const SlippiPlayer *o         = &(s.player[a->ap[1].port]);
814  for (unsigned f = FIRST_FRAME; f < s.frame_count; ++f) {
815    SlippiFrame pf = p->frame[f];
816    DOUT2("    " << f << " (" << frameAsTimer(f,s.timer) << ") P1 "
817      << Action::name[pf.action_pre] << " "
818      << " -> "
819      << " " << Action::name[pf.action_post]
820      << std::endl);
821    SlippiFrame of = o->frame[f];
822    DOUT2("    " << f << " (" << frameAsTimer(f,s.timer) << ") P2 "
823      << Action::name[of.action_pre] << " "
824      << " -> "
825      << " " << Action::name[of.action_post]
826      << std::endl);
827  }
828}
829
830void Analyzer::countPhantoms(const SlippiReplay &s, Analysis *a) const {
831  const SlippiPlayer *p         = &(s.player[a->ap[0].port]);
832  const SlippiPlayer *o         = &(s.player[a->ap[1].port]);
833  for (unsigned f = FIRST_FRAME; f < s.frame_count; ++f) {
834    if (wasHitByPhantom(*p,*o,f)) {
835      ++(a->ap[1].phantom_hits);
836    }
837    if (wasHitByPhantom(*o,*p,f)) {
838      ++(a->ap[0].phantom_hits);
839    }
840  }
841}
842
843void Analyzer::analyzeLedgedashes(const SlippiReplay &s, Analysis *a) const {
844  for (unsigned pi = 0; pi < 2; ++pi) {
845    const SlippiPlayer *p  = &(s.player[a->ap[pi].port]);
846    unsigned galint        = 0;
847    bool     offledge      = false;
848    for (unsigned f = FIRST_FRAME; f < s.frame_count; ++f) {
849      if (galint > 0) {
850        --galint;
851        if (not offledge) {
852          offledge = didReleaseLedge(*p,f);
853          continue;
854        }
855        SlippiFrame pf = p->frame[f];
856        bool landed    = isLanding(p->frame[f-1]) && (not isLanding(pf)) && (not isAirborne(pf));
857        if ((didNoImpactLand(pf) || landed) && (not isInHitlag(pf)) && (not isInHitstun(pf))) {
858          if (a->ap[pi].max_galint < galint) {
859            a->ap[pi].max_galint        = galint;
860          }
861          a->ap[pi].galint_ledgedashes += 1;
862          a->ap[pi].mean_galint        += galint;
863          f                            += galint;
864          galint                        = 0;
865          offledge                      = false;
866        }
867      }
868      if(didCliffCatchEnd(*p,f)) {
869        galint        = 30;
870        if (didReleaseLedge(*p,f)) {
871          offledge = true;
872        }
873      }
874    }
875    if (a->ap[pi].galint_ledgedashes > 0) {
876      a->ap[pi].mean_galint /= a->ap[pi].galint_ledgedashes;
877    }
878  }
879}
880
881void Analyzer::computeTrivialInfo(const SlippiReplay &s, Analysis *a) const {
882  for (unsigned pi = 0; pi < 2; ++pi) {
883    a->ap[pi].total_openings       = a->ap[pi].neutral_wins + a->ap[pi].pokes + a->ap[pi].counters;
884    if (a->ap[pi].total_openings > 0) {
885      a->ap[pi].mean_opening_percent = a->ap[pi].damage_dealt / a->ap[pi].total_openings;
886    }
887
888    unsigned o_stocks_lost = a->ap[1-pi].start_stocks - a->ap[1-pi].end_stocks;
889    if (o_stocks_lost > 0) {
890      a->ap[pi].mean_kill_openings = float(a->ap[pi].total_openings) / o_stocks_lost;
891      a->ap[pi].mean_kill_percent  = a->ap[pi].damage_dealt / o_stocks_lost;
892    }
893
894    // Get actions per minute
895    unsigned total_actions =
896      a->ap[pi].button_count + a->ap[pi].cstick_count + a->ap[pi].astick_count;
897    a->ap[pi].apm        = total_actions * (3600.0f / a->game_length);
898    // Get action states per minute
899    a->ap[pi].aspm       = a->ap[pi].state_changes * (3600.0f / a->game_length);
900  }
901}
902
903Analysis* Analyzer::analyze(const SlippiReplay &s) {
904  DOUT1("Analyzing replay" << std::endl);
905
906  Analysis *a = new Analysis(s.frame_count);  //Structure for holding the analysis so far
907
908  //Verify this is a 1 v 1 match; can't analyze otherwise
909  if (not get1v1Ports(s,a)) {
910    std::cerr << "  Not a two player match; refusing to analyze further" << std::endl;
911    a->success = false;
912    return a;
913  }
914
915  DOUT1("  Analyzing basic game info" << std::endl);
916  getBasicGameInfo(s,a);
917
918  //Interaction-level stats
919  DOUT1("  Analyzing player interactions" << std::endl);
920  analyzeInteractions(s,a);
921  DOUT1("  Summarizing player interactions" << std::endl);
922  summarizeInteractions(s,a);
923  DOUT1("  Analyzing players' punishes" << std::endl);
924  analyzePunishes(s,a);
925  DOUT1("  Analyzing players' cancelling techniques" << std::endl);
926  analyzeCancels(s,a);
927  DOUT1("  Analyzing players' shielding" << std::endl);
928  analyzeShield(s,a);
929  DOUT1("  Analyzing ledgedashes" << std::endl);
930  analyzeLedgedashes(s,a);
931
932  //Player-level stats
933  DOUT1("  Computing statistics based on animations" << std::endl);
934  countBasicAnimations(s,a);
935  DOUT1("  Computing air / ground time for each player" << std::endl);
936  computeAirtime(s,a);
937  DOUT1("  Counting l cancels" << std::endl);
938  countLCancels(s,a);
939  DOUT1("  Counting techs" << std::endl);
940  countTechs(s,a);
941  DOUT1("  Counting dashdances" << std::endl);
942  countDashdances(s,a);
943  DOUT1("  Counting airdodges, wavelands, and wavedashes" << std::endl);
944  countAirdodgesAndWavelands(s,a);
945  DOUT1("  Counting phantom hits" << std::endl);
946  countPhantoms(s,a);
947  DOUT1("  Counting button pushes" << std::endl);
948  countButtons(s,a);
949
950  DOUT1("  Computing trivial match info" << std::endl);
951  computeTrivialInfo(s,a);
952
953  //Debug stuff
954  if (_debug >= 2) {
955    DOUT2("  Showing action states" << std::endl);
956    showActionStates(s,a);
957  }
958
959  a->success = true;
960  DOUT1("Successfully analyzed replay!" << std::endl);
961  return a;
962}
963
964}