fork/slippc

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

Patrick CrainAdded most remaining unparsed data from the Slippi Replay File Spec to parser output (game and player bit flags, etc.); added original input file name to parser and analyzer output70622c9

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