fork/slippc

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

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

0.8.1
48.7 KiB1182 linesraw
1#include "analyzer.h"
2
3namespace slip {
4
5Analyzer::Analyzer(int debug_level) {
6  _debug = debug_level;
7}
8
9Analyzer::~Analyzer() {
10  //Nothing to do yet
11}
12
13bool Analyzer::get1v1Ports(const SlippiReplay &s, Analysis *a) const {
14  unsigned num_players = 0;
15  for(uint8_t i = 0 ; i < 4; ++i) {
16    if (s.player[i].player_type != 3) {
17      if (num_players == 2) {
18        return false;
19      }
20      a->ap[num_players++].port = i;
21    }
22    if (i == 3 && num_players != 2) {
23      return false;
24    }
25  }
26  DOUT1("  Players found on ports " << a->ap[0].port << " and " << a->ap[1].port);
27  return true;
28}
29
30void Analyzer::computeAirtime(const SlippiReplay &s, Analysis *a) const {
31    for (unsigned pi = 0; pi < 2; ++pi) {
32      SlippiPlayer p = s.player[a->ap[pi].port];
33      unsigned airframes = 0;
34      for (unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
35        airframes += p.frame[f].airborne;
36      }
37
38      a->ap[pi].air_frames = airframes;
39      // float aircount = 100*((float)airframes / s.last_frame);
40      // std::cout << "  Airborne for " << aircount  << "% of match" << std::endl;
41    }
42}
43
44void Analyzer::getBasicGameInfo(const SlippiReplay &s, Analysis* a) const {
45  a->original_file     = s.original_file;
46  a->slippi_version    = s.slippi_version;
47  a->parser_version    = s.parser_version;
48  a->analyzer_version  = SLIPPC_VERSION;
49  a->parse_errors      = s.errors;
50  a->game_time         = s.start_time;
51  a->game_length       = s.frame_count;
52  a->timer             = s.timer;
53  a->end_type          = s.end_type;
54  a->lras_player       = s.lras;
55  a->ap[0].tag_player  = s.player[a->ap[0].port].tag;
56  a->ap[1].tag_player  = s.player[a->ap[1].port].tag;
57  a->ap[0].tag_css     = s.player[a->ap[0].port].tag_css;
58  a->ap[1].tag_css     = s.player[a->ap[1].port].tag_css;
59  a->ap[0].tag_code    = s.player[a->ap[0].port].tag_code;
60  a->ap[1].tag_code    = s.player[a->ap[1].port].tag_code;
61
62  a->ap[0].start_stocks  = s.player[a->ap[0].port].start_stocks;
63  a->ap[1].start_stocks  = s.player[a->ap[1].port].start_stocks;
64  a->ap[0].color         = s.player[a->ap[0].port].color;
65  a->ap[1].color         = s.player[a->ap[1].port].color;
66  a->ap[0].team_id       = s.player[a->ap[0].port].team_id;
67  a->ap[1].team_id       = s.player[a->ap[1].port].team_id;
68  a->ap[0].player_type   = s.player[a->ap[0].port].player_type;
69  a->ap[1].player_type   = s.player[a->ap[1].port].player_type;
70  a->ap[0].cpu_level     = s.player[a->ap[0].port].cpu_level;
71  a->ap[1].cpu_level     = s.player[a->ap[1].port].cpu_level;
72
73  a->ap[0].end_stocks  = s.player[a->ap[0].port].end_stocks;
74  a->ap[1].end_stocks  = s.player[a->ap[1].port].end_stocks;
75  a->ap[0].end_pct     = s.player[a->ap[0].port].frame[s.frame_count-1].percent_pre;
76  a->ap[1].end_pct     = s.player[a->ap[1].port].frame[s.frame_count-1].percent_pre;
77  a->ap[0].char_id     = s.player[a->ap[0].port].ext_char_id;
78  a->ap[1].char_id     = s.player[a->ap[1].port].ext_char_id;
79  a->stage_id          = s.stage;
80  a->ap[0].char_name   = CharExt::name[a->ap[0].char_id];
81  a->ap[1].char_name   = CharExt::name[a->ap[1].char_id];
82  a->stage_name        = Stage::name[s.stage];
83  a->winner_port       = s.winner_id;
84}
85
86void Analyzer::summarizeInteractions(const SlippiReplay &s, Analysis *a) const {
87  // std::cout << "  Summarizing player interactions" << std::endl;
88  for (unsigned f = (PLAYABLE_FRAME-LOAD_FRAME); f < s.frame_count; ++f) {
89    unsigned d = a->dynamics[f];
90    ++(a->ap[0].dyn_counts[d]); //Increase the counter for dynamics across all frames
91    if (d > Dynamic::DEFENSIVE && d < Dynamic::OFFENSIVE) {
92      //Index is the same for the 2nd player for neutral dynamics
93      ++(a->ap[1].dyn_counts[d]);
94    } else {
95      //Index is inverted for the 2nd player for non-neutral dynamics
96      ++(a->ap[1].dyn_counts[Dynamic::__LAST - d]);
97    }
98  }
99  // std::cout << std::fixed; //Show floats in fixed representation
100  // for (unsigned p = 0; p < 2; ++p) {
101  //   // std::cout << "    From the perspective of port " << int(a->ap[p].port+1) << " (" << a->ap[p].char_name << "):" << std::endl;
102  //   for (unsigned i = 0; i < Dynamic::__LAST; ++i) {
103  //     std::string n   = std::to_string(a->ap[p].dyn_counts[i]);
104  //     std::string pct = std::to_string((100*float(a->ap[p].dyn_counts[i])/s.frame_count)).substr(0,5);
105  //     std::cout << "      " << SPACE[6-n.length()] << n << " frames (" << pct
106  //       << "% of game) spent in " << Dynamic::name[i] << std::endl;
107  //   }
108  // }
109}
110
111void Analyzer::countLCancels(const SlippiReplay &s, Analysis *a) const {
112  for (unsigned pi = 0; pi < 2; ++pi) {
113    SlippiPlayer p = s.player[a->ap[pi].port];
114    unsigned cancels_hit  = 0;
115    unsigned cancels_miss = 0;
116    unsigned last_state   = 0;
117    for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
118      if (last_state == 0) {
119        if (p.frame[f].l_cancel == 1) {
120          cancels_hit += 1;
121        } else if (p.frame[f].l_cancel == 2) {
122          cancels_miss += 1;
123        }
124      }
125      last_state = p.frame[f].l_cancel;
126    }
127
128    a->ap[pi].l_cancels_hit    = cancels_hit;
129    a->ap[pi].l_cancels_missed = cancels_miss;
130    float cancels_pct = 100*((float)cancels_hit / (cancels_hit+cancels_miss));
131    // std::cout << "  Hit " << cancels_hit << "/" << cancels_hit+cancels_miss << " l cancels (" << cancels_pct << "%)" << std::endl;
132  }
133}
134
135void Analyzer::countButtons(const SlippiReplay &s, Analysis *a) const {
136  for (unsigned pi = 0; pi < 2; ++pi) {
137    SlippiPlayer p        = s.player[a->ap[pi].port];
138    uint16_t last_buttons = 0;
139    float    last_ax      = 0;
140    float    last_ay      = 0;
141    float    last_cx      = 0;
142    float    last_cy      = 0;
143    for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
144      // Add buttons pressed this frame to button count
145      uint16_t cur_buttons    = p.frame[f].buttons;
146      uint16_t new_buttons    = cur_buttons&(cur_buttons^last_buttons);
147      new_buttons            &= 0x0F70;  //Mask out unused bits
148      a->ap[pi].button_count += countBits(new_buttons);
149      last_buttons            = cur_buttons;
150
151      // Check analog stick for movement
152      float    cur_ax = p.frame[f].joy_x;
153      float    cur_ay = p.frame[f].joy_y;
154      a->ap[pi].astick_count += checkStickMovement(cur_ax,cur_ay,last_ax,last_ay);
155      last_ax = cur_ax;
156      last_ay = cur_ay;
157
158      // Check C stick for movement
159      float    cur_cx = p.frame[f].c_x;
160      float    cur_cy = p.frame[f].c_y;
161      a->ap[pi].cstick_count += checkStickMovement(cur_cx,cur_cy,last_cx,last_cy);
162      last_cx = cur_cx;
163      last_cy = cur_cy;
164    }
165  }
166}
167
168void Analyzer::countTechs(const SlippiReplay &s, Analysis *a) const {
169  for (unsigned pi = 0; pi < 2; ++pi) {
170    SlippiPlayer p             = s.player[a->ap[pi].port];
171    unsigned techs_hit         = 0;
172    unsigned walltechs_hit     = 0;  //And ceiling techs
173    unsigned walljumps_hit     = 0;
174    unsigned walljumptechs_hit = 0;
175    unsigned techs_missed      = 0;
176    bool     teching           = false;
177
178    for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
179      if (inTechState(p.frame[f])) {
180        if (not teching) {
181          teching = true;
182          if (p.frame[f].action_pre <= Action::DownSpotD) {
183            ++techs_missed;
184          } else if (p.frame[f].action_pre <= Action::PassiveStandB) {
185            ++techs_hit;
186          } else if (p.frame[f].action_pre == Action::PassiveWallJump) {
187            if (inDamagedState(p.frame[f-1]) || inTumble(p.frame[f-1])) {
188              ++walljumptechs_hit;
189            } else {
190              ++walljumps_hit;
191            }
192          } else {
193            ++walltechs_hit; //Or ceiling techs
194          }
195        }
196      } else {
197        teching = false;
198      }
199    }
200
201    a->ap[pi].techs         = techs_hit;
202    a->ap[pi].walltechs     = walltechs_hit;
203    a->ap[pi].walljumps     = walljumps_hit;
204    a->ap[pi].walltechjumps = walljumptechs_hit;
205    a->ap[pi].missed_techs  = techs_missed;
206
207    // std::cout << "  Hit "    << techs_hit         << " grounded techs"  << std::endl;
208    // std::cout << "  Hit "    << walltechs_hit     << " wall techs"      << std::endl;
209    // std::cout << "  Hit "    << walljumps_hit     << " wall jumps"      << std::endl;
210    // std::cout << "  Hit "    << walljumptechs_hit << " wall jump techs" << std::endl;
211    // std::cout << "  Missed " << techs_missed      << " techs"           << std::endl;
212  }
213}
214
215void Analyzer::countDashdances(const SlippiReplay &s, Analysis *a) const {
216  for (unsigned pi = 0; pi < 2; ++pi) {
217    SlippiPlayer p = s.player[a->ap[pi].port];
218    unsigned dashdances = 0;
219    for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
220      if (isDashdancing(p,f)) {
221        ++dashdances;
222      }
223    }
224
225    a->ap[pi].dashdances = dashdances;
226    // std::cout << "  Dashdanced " << dashdances  << " times" << std::endl;
227  }
228}
229
230//Code adapted from Fizzi's
231void Analyzer::countAirdodgesAndWavelands(const SlippiReplay &s, Analysis *a) const {
232  for (unsigned pi = 0; pi < 2; ++pi) {
233    SlippiPlayer p = s.player[a->ap[pi].port];
234    int airdodges  = 0;
235    unsigned wavelands  = 0;
236    unsigned wavedashes = 0;
237    bool     airdodging = false;
238    for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
239      if (maybeWavelanding(p,f)) {  //Waveland detection by Fizzi
240        //Look at the last 8 frames (including this one) re Fizzi
241        bool foundAirdodge  = false;
242        bool foundJumpsquat = false;
243        bool foundOther     = false;
244        for(unsigned i = 1; i < 8; ++i) {
245          if (isAirdodging(p.frame[f-i])) {
246            foundAirdodge = true;
247          } else if (isInJumpsquat(p.frame[f-i])) {
248            foundJumpsquat = true;
249            break;
250          } else {
251            foundOther = true;
252          }
253        }
254        if (foundAirdodge) {
255          if ((not foundJumpsquat) && (not foundOther)) {
256            continue; //If the airdodge is the only animation we found, proceed
257          } else {
258            --airdodges;  //Otherwise, subtract it from our airdodge count and keep checkings
259          }
260        }
261        if (foundJumpsquat) {  //If we were in jumpsquat at all recently, we're wavedashing
262          ++wavedashes;
263        } else if (foundOther) {  //If we were in any other animation besides airdodge, it's a waveland
264          ++wavelands;
265        }  //Otherwise, nothing special happened
266      } else if (isAirdodging(p.frame[f])) {
267        if (not airdodging) {
268          ++airdodges;
269          airdodging = true;
270        }
271      } else {
272        airdodging = false;
273      }
274    }
275
276    // it's possible the logic above outputs a negative airdodge count -> use a minimum as workaround:
277    a->ap[pi].airdodges  = airdodges > 0 ? airdodges : 0;
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    DOUT3("    " << f << " (" << frameAsTimer(f,s.timer) << ") P1 "
493      << Dynamic::name[cur_dynamic]);
494  }
495
496}
497
498void Analyzer::analyzePunishes(const SlippiReplay &s, Analysis *a) const {
499  const SlippiPlayer *p  = &(s.player[a->ap[0].port]);
500  const SlippiPlayer *o  = &(s.player[a->ap[1].port]);
501  unsigned pa            = 0; //Running tally of player attacks
502  unsigned oa            = 0; //Running tally of opponent attacks
503  unsigned pn            = 0; //Running tally of player punishes
504  unsigned on            = 0; //Running tally of opponent punishes
505  unsigned oLastInHitsun = 0;
506  unsigned pLastInHitsun = 0;
507  unsigned cur_dyn       = a->dynamics[FIRST_FRAME];
508  unsigned last_dyn      = a->dynamics[FIRST_FRAME];
509  Punish *pPunishes      = a->ap[0].punishes;
510  Punish *oPunishes      = a->ap[1].punishes;
511  Attack *pAttacks       = a->ap[0].attacks;
512  Attack *oAttacks       = a->ap[1].attacks;
513
514  for (unsigned f = FIRST_FRAME; f < s.frame_count; ++f) {
515    SlippiFrame pf = p->frame[f];
516    SlippiFrame of = o->frame[f];
517    cur_dyn        = a->dynamics[f];
518
519    oLastInHitsun = isInHitstun(o->frame[f]) ? f : oLastInHitsun;
520    pLastInHitsun = isInHitstun(p->frame[f]) ? f : pLastInHitsun;
521    bool oPoked    = ((f - oLastInHitsun) < POKE_THRES);  //Check if opponent has been poked recently
522    bool pPoked    = ((f - pLastInHitsun) < POKE_THRES);  //Check if we have been poked recently
523
524    //Check if we counter-attacked (switched from defense to offense or vice versa)
525    if (cur_dyn <= Dynamic::DEFENSIVE && last_dyn >= Dynamic::OFFENSIVE) {
526      ++a->ap[1].counters;  //If we went from offense to defense, we were countered
527    } else if (cur_dyn >= Dynamic::OFFENSIVE && last_dyn <= Dynamic::DEFENSIVE) {
528      ++a->ap[0].counters;  //If we went from defense to offense, we countered
529    }
530
531    //Check if either player lost a stock
532    if (pf.stocks < p->frame[f-1].stocks) {
533      unsigned ddir = deathDirection(*p,f);
534      if (oa > 0) {
535        oAttacks[oa-1].kill_dir = ddir;
536      }
537      if (oPunishes[on].num_moves > 0) {
538        oPunishes[on].kill_dir = ddir; //TODO: filter out self-destructs somehow
539      } else if (on > 0) {
540        oPunishes[on-1].kill_dir = ddir;
541      }
542      if (cur_dyn == Dynamic::EDGEGUARDING) {
543        ++(a->ap[1].reverse_edgeguards);
544      } else if (cur_dyn != Dynamic::RECOVERING && ddir != Dir::UP) {
545        ++(a->ap[0].self_destructs);
546      }
547    }
548    if (of.stocks < o->frame[f-1].stocks) {
549      unsigned ddir = deathDirection(*o,f);
550      if (pa > 0) {
551        pAttacks[pa-1].kill_dir = ddir;
552      }
553      if (pPunishes[pn].num_moves > 0) {
554        pPunishes[pn].kill_dir = ddir;
555      } else if (pn > 0) {
556        pPunishes[pn-1].kill_dir = ddir;
557      }
558      if (cur_dyn == Dynamic::RECOVERING) {
559        ++(a->ap[0].reverse_edgeguards);
560      } else if (cur_dyn != Dynamic::EDGEGUARDING && ddir != Dir::UP) {
561        ++(a->ap[1].self_destructs);
562      }
563    }
564
565    //Check if either player has dropped a punish off an opening
566    bool pPunishEnd = false;
567    bool oPunishEnd = false;
568    if (f == s.frame_count-1) {
569      pPunishEnd = true;
570      oPunishEnd = true;
571    } else if (cur_dyn != Dynamic::POKING) {
572      if (cur_dyn > Dynamic::DEFENSIVE && not pPoked) {
573        //If the opponent had a punish going and they are no longer on offense, end their punish
574        oPunishEnd = true;
575      }
576      if (cur_dyn < Dynamic::OFFENSIVE && not oPoked) {
577        //If we had a punish going and we are no longer on offense, end our punish
578        pPunishEnd = true;
579      }
580    }
581
582    //Update punish counter for ended punishes
583    if (pPunishEnd && pa > 0) {
584      if (pPunishes[pn].num_moves > 0) {
585        pPunishes[pn].end_frame    = f;
586        pPunishes[pn].end_pct      = of.percent_pre;
587        pPunishes[pn].last_move_id = pf.hit_with;
588        if(pPunishes[pn].num_moves > pAttacks[pa-1].hit_id) {
589          a->ap[0].neutral_wins += 1;
590        } else {
591          a->ap[0].pokes += 1;
592        }
593        ++pn;
594      }
595    }
596    if (oPunishEnd && oa > 0) {
597      if (oPunishes[on].num_moves > 0) {
598        oPunishes[on].end_frame    = f;
599        oPunishes[on].end_pct      = pf.percent_pre;
600        oPunishes[on].last_move_id = of.hit_with;
601        if(oPunishes[on].num_moves > oAttacks[oa-1].hit_id) {
602          a->ap[1].neutral_wins += 1;
603        } else {
604          a->ap[1].pokes += 1;
605        }
606        ++on;
607      }
608    }
609
610    //If the opponent just took damage
611    float o_damage_taken = of.percent_pre - o->frame[f-1].percent_pre;
612    if (o_damage_taken > 0) {
613      //Check for bubble damage
614      pAttacks[pa].move_id    = (isOffscreen(of) && o_damage_taken == 1) ? Move::BUBBLE : pf.hit_with;
615      //Last frame we actually took damage; frame before that gives us the animation frame our move hit
616      pAttacks[pa].anim_frame = p->frame[f-2].action_fc;
617      pAttacks[pa].punish_id  = pn;
618      if (  //Check if this is a consecutive hit from a multihit move
619       pa > 0 &&  //If this isn't our first move
620       pAttacks[pa].move_id == pAttacks[pa-1].move_id &&  //If the last move we hit with had the same id
621       pAttacks[pa].anim_frame > pAttacks[pa-1].anim_frame && //If the action frame counter is higher
622       //If the gap between animation and game frames is less than max hitlag for the move
623       //Formula: https://www.ssbwiki.com/Freeze_frame
624       (f - pAttacks[pa].anim_frame <= (pAttacks[pa-1].frame - pAttacks[pa-1].anim_frame + (pAttacks[pa-1].damage/3+3)))) {
625        pAttacks[pa].hit_id  = pAttacks[pa-1].hit_id+1;
626      } else {
627        pAttacks[pa].hit_id  = 1;
628        ++(a->ap[0].move_counts[pAttacks[pa].move_id]);
629      }
630      pAttacks[pa].frame      = f;
631      pAttacks[pa].damage     = o_damage_taken;
632      a->ap[0].dyn_damage[cur_dyn] += o_damage_taken; //Dynamic is normal for player
633      pAttacks[pa].opening    = cur_dyn;  //Dynamic is normal for player
634      pAttacks[pa].kill_dir   = Dir::NEUT;
635      ++pa;
636      //If this is the start of a combo
637      if (pPunishes[pn].num_moves == 0) {
638        pPunishes[pn].start_frame = f;
639        pPunishes[pn].start_pct   = o->frame[f-1].percent_pre;
640        pPunishes[pn].stocks      = o->frame[f-1].stocks;
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].stocks      = p->frame[f-1].stocks;
682        oPunishes[on].kill_dir    = Dir::NEUT;
683      }
684      a->ap[1].damage_dealt      += p_damage_taken;
685      oPunishes[on].end_frame     = f;
686      oPunishes[on].end_pct       = pf.percent_pre;
687      oPunishes[on].last_move_id  = of.hit_with;
688      oPunishes[on].num_moves    += 1;
689    }
690
691    last_dyn = cur_dyn;  //Update the last dynamic
692  }
693}
694
695void Analyzer::analyzeCancels(const SlippiReplay &s, Analysis *a) const {
696  for(unsigned pi = 0; pi < 2; ++pi) {
697    const SlippiPlayer *p = &(s.player[a->ap[pi].port]);
698    Attack *attacks       = a->ap[pi].attacks;
699    for(unsigned i = 0; attacks[i].frame > 0; ++i) {
700      unsigned f             = attacks[i].frame;
701      if (attacks[i].move_id >= Move::NAIR && attacks[i].move_id <= Move::DAIR) {
702        unsigned last_frame = attacks[i].frame+60;
703        if (last_frame > s.frame_count) {
704          last_frame = s.frame_count;
705        }
706        for(unsigned f = attacks[i].frame; f < last_frame; ++f) {
707          //Check if we had the opportunity to edge cancel
708          if (didEdgeCancelAerial(p->frame[f])) {
709            attacks[i].cancel_type = Cancel::EDGE;
710            break;
711          }
712          //Check if we had the opportunity to teeter cancel
713          if (didTeeterCancelAerial(p->frame[f])) {
714            attacks[i].cancel_type = Cancel::TEETER;
715            break;
716          }
717          //Check if we had the opportunity to autocancel
718          if (didAutoCancelAerial(p->frame[f])) {
719            attacks[i].cancel_type = Cancel::AUTO;
720            break;
721          }
722          //Check if we had the opportunity to L cancel
723          if (p->frame[f].l_cancel > 0) {
724            if (p->frame[f].l_cancel == 1) {
725              attacks[i].cancel_type = Cancel::L;
726            }
727            break;
728          }
729        }
730      }
731    }
732  }
733}
734
735void Analyzer::analyzeShield(const SlippiReplay &s, Analysis *a) const {
736  for(unsigned pi = 0; pi < 2; ++pi) {
737    const SlippiPlayer *p = &(s.player[a->ap[pi].port]);
738    for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
739      float shield_damage = (p->frame[f].shield - p->frame[f-1].shield);
740      if (shield_damage > 0) {
741        a->ap[pi].shield_time   += 1;
742        a->ap[pi].shield_damage += shield_damage;
743        if(p->frame[f].shield < a->ap[pi].shield_lowest) {
744          a->ap[pi].shield_lowest = p->frame[f].shield;
745        }
746      }
747    }
748  }
749}
750
751unsigned Analyzer::countTransitions(const SlippiReplay &s, Analysis *a, unsigned pnum, bool (*cb)(const SlippiFrame&)) const {
752  SlippiPlayer p = s.player[a->ap[pnum].port];
753  unsigned counter = 0;
754  bool active = false;
755  for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
756    if (cb(p.frame[f])) {
757      if(not active) {
758        ++counter;
759        active = true;
760      }
761    } else {
762      active = false;
763    }
764  }
765  return counter;
766}
767
768unsigned Analyzer::countTransitions(const SlippiReplay &s, Analysis *a, unsigned pnum, bool (*cb)(const SlippiPlayer &, const unsigned)) const {
769  SlippiPlayer p = s.player[a->ap[pnum].port];
770  unsigned counter = 0;
771  bool active = false;
772  for(unsigned f = (-LOAD_FRAME); f < s.frame_count; ++f) {
773    if (cb(p,f)) {
774      if(not active) {
775        ++counter;
776        active = true;
777      }
778    } else {
779      active = false;
780    }
781  }
782  return counter;
783}
784
785void Analyzer::countBasicAnimations(const SlippiReplay &s, Analysis *a) const {
786  for (unsigned pi = 0; pi < 2; ++pi) {
787    a->ap[pi].state_changes          = countTransitions(s,a,pi,didActionStateChange);
788
789    a->ap[pi].ledge_grabs            = countTransitions(s,a,pi,isOnLedge);
790    a->ap[pi].rolls                  = countTransitions(s,a,pi,isRolling);
791    a->ap[pi].spotdodges             = countTransitions(s,a,pi,isSpotdodging);
792    a->ap[pi].powershields           = countTransitions(s,a,pi,didPowerShield);
793    a->ap[pi].grabs                  = countTransitions(s,a,pi,isGrabbing);
794    a->ap[pi].taunts                 = countTransitions(s,a,pi,isTaunting);
795    a->ap[pi].meteor_cancels         = countTransitions(s,a,pi,didMeteorCancel);
796    a->ap[pi].hits_blocked           = countTransitions(s,a,pi,isInShieldstun);
797    a->ap[pi].edge_cancel_aerials    = countTransitions(s,a,pi,didEdgeCancelAerial);
798    a->ap[pi].edge_cancel_specials   = countTransitions(s,a,pi,didEdgeCancelSpecial);
799    a->ap[pi].teeter_cancel_aerials  = countTransitions(s,a,pi,didTeeterCancelAerial);
800    a->ap[pi].teeter_cancel_specials = countTransitions(s,a,pi,didTeeterCancelSpecial);
801    a->ap[pi].no_impact_lands        = countTransitions(s,a,pi,didNoImpactLand);
802    a->ap[pi].shield_drops           = countTransitions(s,a,pi,didShieldDrop);
803    a->ap[pi].pivots                 = countTransitions(s,a,pi,didPivot);
804    a->ap[pi].short_hops             = countTransitions(s,a,pi,didShortHop);
805    a->ap[pi].full_hops              = countTransitions(s,a,pi,didHop)-a->ap[pi].short_hops;
806
807    a->ap[pi].shield_breaks          = countTransitions(s,a,1-pi,isShieldBroken);
808    a->ap[pi].grab_escapes           = countTransitions(s,a,1-pi,isReleasing);
809    a->ap[pi].shield_stabs           = countTransitions(s,a,1-pi,wasShieldStabbed);
810    a->ap[pi].stage_spikes           = countTransitions(s,a,1-pi,wasStageSpiked);
811  }
812}
813
814void Analyzer::showActionStates(const SlippiReplay &s, Analysis *a) const {
815  const SlippiPlayer *p         = &(s.player[a->ap[0].port]);
816  const SlippiPlayer *o         = &(s.player[a->ap[1].port]);
817  for (unsigned f = FIRST_FRAME; f < s.frame_count; ++f) {
818    SlippiFrame pf = p->frame[f];
819    DOUT2("    " << f << " (" << frameAsTimer(f,s.timer) << ") P1 "
820      << Action::name[pf.action_pre] << " "
821      << " -> "
822      << " " << Action::name[pf.action_post]);
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  }
829}
830
831void Analyzer::countPhantoms(const SlippiReplay &s, Analysis *a) const {
832  const SlippiPlayer *p         = &(s.player[a->ap[0].port]);
833  const SlippiPlayer *o         = &(s.player[a->ap[1].port]);
834  for (unsigned f = FIRST_FRAME; f < s.frame_count; ++f) {
835    if (wasHitByPhantom(*p,*o,f)) {
836      ++(a->ap[1].phantom_hits);
837    }
838    if (wasHitByPhantom(*o,*p,f)) {
839      ++(a->ap[0].phantom_hits);
840    }
841  }
842}
843
844void Analyzer::countLedgedashes(const SlippiReplay &s, Analysis *a) const {
845  for (unsigned pi = 0; pi < 2; ++pi) {
846    const SlippiPlayer *p  = &(s.player[a->ap[pi].port]);
847    unsigned galint        = 0;
848    bool     offledge      = false;
849    for (unsigned f = FIRST_FRAME; f < s.frame_count; ++f) {
850      if (galint > 0) {
851        --galint;
852        if (not offledge) {
853          offledge = didReleaseLedge(*p,f);
854          continue;
855        }
856        SlippiFrame pf = p->frame[f];
857        bool landed    = isLanding(p->frame[f-1]) && (not isLanding(pf)) && (not isAirborne(pf));
858        if ((didNoImpactLand(pf) || landed) && (not isInHitlag(pf)) && (not isInHitstun(pf))) {
859          if (a->ap[pi].max_galint < galint) {
860            a->ap[pi].max_galint        = galint;
861          }
862          a->ap[pi].galint_ledgedashes += 1;
863          a->ap[pi].mean_galint        += galint;
864          f                            += galint;
865          galint                        = 0;
866          offledge                      = false;
867        }
868      }
869      if(didCliffCatchEnd(*p,f)) {
870        galint        = 30;
871        if (didReleaseLedge(*p,f)) {
872          offledge = true;
873        }
874      }
875    }
876    if (a->ap[pi].galint_ledgedashes > 0) {
877      a->ap[pi].mean_galint /= a->ap[pi].galint_ledgedashes;
878    }
879  }
880}
881
882void Analyzer::countMoves(const SlippiReplay &s, Analysis *a) const {
883  for (unsigned pi = 0; pi < 2; ++pi) {
884    const SlippiPlayer *p  = &(s.player[a->ap[pi].port]);
885    const unsigned pid = p->ext_char_id;
886    bool was_throw   = false;
887    bool was_normal  = false;
888    bool was_special = false;
889    bool was_misc    = false;
890    bool was_grab    = false;
891    bool was_pummel  = false;
892    for (unsigned f = FIRST_FRAME; f < s.frame_count; ++f) {
893      SlippiFrame pf = p->frame[f];
894      if (isThrowing(pf)) {
895        if (!(was_throw)) {
896          ++(a->ap[pi].used_throws);
897        }
898        was_throw = true;
899      } else if (isUsingNormalMove(pf)) {
900        if (!(was_normal)) {
901          ++(a->ap[pi].used_norm_moves);
902        }
903        was_normal = true;
904      } else if (isUsingSpecialMove(pf,pid)) {
905        if (!(was_special)) {
906          ++(a->ap[pi].used_spec_moves);
907        }
908        was_special = true;
909      } else if (isUsingMiscMove(pf)) {
910        if (!(was_misc)) {
911          ++(a->ap[pi].used_misc_moves);
912        }
913        was_misc = true;
914      } else if (isUsingGrab(pf)) {
915        if (!(was_grab)) {
916          ++(a->ap[pi].used_grabs);
917        }
918        was_grab = true;
919      } else if (isUsingPummel(pf)) {
920        if (!(was_pummel)) {
921          ++(a->ap[pi].used_pummels);
922        }
923        was_pummel = true;
924      } else {
925        was_throw   = false;
926        was_normal  = false;
927        was_special = false;
928        was_misc    = false;
929        was_grab    = false;
930        was_pummel  = false;
931      }
932    }
933    a->ap[pi].total_moves_used = a->ap[pi].used_throws + a->ap[pi].used_norm_moves
934      + a->ap[pi].used_spec_moves + a->ap[pi].used_misc_moves + a->ap[pi].used_grabs + a->ap[pi].used_pummels;
935    // std::cout << "Player " << pi+1 << " attempted: " << std::endl;
936    // std::cout << "  " <<  a->ap[pi].used_throws << " used_throws" << std::endl;
937    // std::cout << "  " <<  a->ap[pi].used_norm_moves << " used_norm_moves" << std::endl;
938    // std::cout << "  " <<  a->ap[pi].used_spec_moves << " used_spec_moves" << std::endl;
939    // std::cout << "  " <<  a->ap[pi].used_misc_moves << " used_misc_moves" << std::endl;
940    // std::cout << "  " <<  a->ap[pi].used_grabs << " used_grabs" << std::endl;
941    // std::cout << "  " <<  a->ap[pi].used_pummels << " used_pummels" << std::endl;
942    // std::cout << "  " <<  a->ap[pi].total_moves_used << " used_total_moves" << std::endl;
943  }
944}
945
946void Analyzer::countActionability(const SlippiReplay &s, Analysis *a) const {
947  for (unsigned pi = 0; pi < 2; ++pi) {
948    const SlippiPlayer *p  = &(s.player[a->ap[pi].port]);
949    bool     was_in_hitstun     = false;
950    unsigned hitstun_times      = 0; //Number of times we enter hitstun
951    unsigned hitstun_act        = 0; //Total number of frames we take to act out of hitstun
952    unsigned hitstun_act_cur    = 0; //Current number of frames we take to act out of hitstun
953    bool     was_in_shieldstun  = false;
954    unsigned shieldstun_times   = 0; //Number of times we enter shieldstun
955    unsigned shieldstun_act     = 0; //Total number of frames we take to act out of shieldstun
956    unsigned shieldstun_act_cur = 0; //Current number of frames we take to act out of shieldstun
957    bool     was_in_wait        = false;
958    unsigned wait_times         = 0; //Number of times we enter wait
959    unsigned wait_act           = 0; //Total number of frames we take to act out of wait
960    unsigned wait_act_cur       = 0; //Current number of frames we take to act out of wait
961    for (unsigned f = FIRST_FRAME; f < s.frame_count; ++f) {
962      SlippiFrame pf = p->frame[f];
963
964      // Count the number of frames we take to act out of hitstun
965      if (isInHitstun(pf)) {
966        if (! was_in_hitstun) {
967          was_in_hitstun = true;
968          ++hitstun_times;
969          hitstun_act_cur = 0;
970        }
971      } else if (was_in_hitstun) {
972        if (
973         inTumble(pf)            ||
974         isInDamageAnimation(pf) ||
975         isInAnyWait(pf)) {
976          ++hitstun_act_cur;
977          ++hitstun_act;
978        } else {
979          if ((MAX_WAIT < hitstun_act_cur) || wasStageSpiked(pf) || inMissedTechState(pf)) {
980            --hitstun_times;  //We're not trying to act out of stun
981          } else {
982            // std::cout << "hitstun act " << hitstun_act_cur << std::endl;
983            hitstun_act += hitstun_act_cur;
984          }
985          was_in_hitstun = false;
986        }
987      }
988
989      // Count the number of frames we take to act out of shieldstun
990      unsigned max_shield_wait = MAX_WAIT;
991      if (isInShieldstun(pf)) {
992        if (! was_in_shieldstun) {
993          was_in_shieldstun = true;
994          ++shieldstun_times;
995          shieldstun_act_cur = 0;
996        } else if (shieldstun_act_cur > 0) {
997          //We might be waiting in shield due to shield pressure,
998          //  so break the MAX_WAIT limit in these cases
999          max_shield_wait = MAX_WAIT + shieldstun_act_cur;
1000        }
1001      } else if (was_in_shieldstun) {
1002        if (isInShield(pf)) {
1003          ++shieldstun_act_cur;
1004        } else {
1005          if ((max_shield_wait < shieldstun_act_cur) ) {
1006            --shieldstun_times;  //We're not trying to act out of stun
1007          } else {
1008            // std::cout << "shieldstun act " << shieldstun_act_cur << std::endl;
1009            shieldstun_act += shieldstun_act_cur;
1010          }
1011          was_in_shieldstun = false;
1012        }
1013      }
1014
1015      // Count the number of frames we take to act out of shieldstun
1016      if (isInAnyWait(pf)) {
1017        if (! was_in_wait) {
1018          was_in_wait = true;
1019          ++wait_times;
1020          wait_act_cur = 0;
1021        } else {
1022          ++wait_act_cur;
1023        }
1024      } else if (was_in_wait) {
1025        if ((MAX_WAIT < wait_act_cur) ) {
1026          --wait_times;  //We're not trying to act out of wait
1027        } else {
1028          // std::cout << "wait act " << wait_act_cur << std::endl;
1029          wait_act += wait_act_cur;
1030        }
1031        was_in_wait = false;
1032      }
1033
1034    }
1035
1036    a->ap[pi].hitstun_times         = hitstun_times;
1037    a->ap[pi].hitstun_act_frames    = hitstun_act;
1038    a->ap[pi].shieldstun_times      = shieldstun_times;
1039    a->ap[pi].shieldstun_act_frames = shieldstun_act;
1040    a->ap[pi].wait_times            = wait_times;
1041    a->ap[pi].wait_act_frames       = wait_act;
1042  }
1043}
1044
1045void Analyzer::computeTrivialInfo(const SlippiReplay &s, Analysis *a) const {
1046  for (unsigned pi = 0; pi < 2; ++pi) {
1047    // Get damage per opening
1048    a->ap[pi].total_openings       = a->ap[pi].neutral_wins + a->ap[pi].pokes;
1049    if (a->ap[pi].total_openings > 0) {
1050      a->ap[pi].mean_opening_percent = a->ap[pi].damage_dealt / a->ap[pi].total_openings;
1051    }
1052
1053    // Get openings per kill and percent per kill
1054    unsigned o_end_stocks = a->ap[1-pi].end_stocks;
1055    unsigned o_stocks_lost = a->ap[1-pi].start_stocks - o_end_stocks;
1056    if (o_stocks_lost > 0) {
1057      a->ap[pi].mean_kill_openings = float(a->ap[pi].total_openings) / o_stocks_lost;
1058      float damage_dealt = a->ap[pi].damage_dealt;
1059      if (o_end_stocks > 0) {
1060        //Need to not include stocks we didn't take in our computation
1061        damage_dealt -= a->ap[1-pi].end_pct;
1062      }
1063      a->ap[pi].mean_kill_percent    = damage_dealt / o_stocks_lost;
1064      a->ap[1-pi].mean_death_percent = a->ap[pi].mean_kill_percent;
1065    }
1066
1067    // Get actions per minute
1068    unsigned total_actions =
1069      a->ap[pi].button_count + a->ap[pi].cstick_count + a->ap[pi].astick_count;
1070    a->ap[pi].apm        = total_actions * (3600.0f / a->game_length);
1071
1072    // Get action states per minute
1073    a->ap[pi].aspm       = a->ap[pi].state_changes * (3600.0f / a->game_length);
1074
1075    // Get total number of moves landed and move accuracy
1076    a->ap[pi].total_moves_landed = 0;
1077    for(unsigned i = 0; i < Move::__LAST; ++i) {
1078      a->ap[pi].total_moves_landed += a->ap[pi].move_counts[i];
1079    }
1080    if (a->ap[pi].total_moves_used > 0) {
1081      a->ap[pi].move_accuracy = float(a->ap[pi].total_moves_landed) / float(a->ap[pi].total_moves_used);
1082    }
1083
1084    // Get average actionability
1085    a->ap[pi].actionability = 0;
1086    if (a->ap[pi].shieldstun_times > 0) {
1087      a->ap[pi].actionability += float(a->ap[pi].shieldstun_act_frames) / a->ap[pi].shieldstun_times;
1088    }
1089    if (a->ap[pi].hitstun_times > 0) {
1090      a->ap[pi].actionability += float(a->ap[pi].hitstun_act_frames) / a->ap[pi].hitstun_times;
1091    }
1092    if (a->ap[pi].wait_times > 0) {
1093      a->ap[pi].actionability += float(a->ap[pi].wait_act_frames) / a->ap[pi].wait_times;
1094    }
1095    a->ap[pi].actionability /= 3.0f;
1096
1097    // Get number of times we won neutral relative to time spent in neutral / defense
1098    unsigned neut_frames =
1099      a->ap[pi].dyn_counts[Dynamic::TRADING]     +
1100      a->ap[pi].dyn_counts[Dynamic::POKING]      +
1101      a->ap[pi].dyn_counts[Dynamic::NEUTRAL]     +
1102      a->ap[pi].dyn_counts[Dynamic::POSITIONING] +
1103      a->ap[pi].dyn_counts[Dynamic::FOOTSIES]    +
1104      a->ap[pi].dyn_counts[Dynamic::RECOVERING]  +
1105      a->ap[pi].dyn_counts[Dynamic::ESCAPING]    +
1106      a->ap[pi].dyn_counts[Dynamic::PUNISHED]    +
1107      a->ap[pi].dyn_counts[Dynamic::GROUNDING]   +
1108      a->ap[pi].dyn_counts[Dynamic::PRESSURED]   +
1109      a->ap[pi].dyn_counts[Dynamic::DEFENSIVE]
1110      ;
1111    if (neut_frames > 0) {
1112      a->ap[pi].neutral_wins_per_min = a->ap[pi].total_openings / (float(neut_frames) / 3600.0f);
1113    }
1114  }
1115}
1116
1117Analysis* Analyzer::analyze(const SlippiReplay &s) {
1118  DOUT1("  Analyzing replay");
1119
1120  Analysis *a = new Analysis(s.frame_count);  //Structure for holding the analysis so far
1121
1122  //Verify this is a 1 v 1 match; can't analyze otherwise
1123  if (not get1v1Ports(s,a)) {
1124    FAIL("    Not a two player match; refusing to analyze further");
1125    a->success = false;
1126    return a;
1127  }
1128
1129  DOUT1("    Analyzing basic game info");
1130  getBasicGameInfo(s,a);
1131
1132  //Interaction-level stats
1133  DOUT1("    Analyzing player interactions");
1134  analyzeInteractions(s,a);
1135  DOUT1("    Summarizing player interactions");
1136  summarizeInteractions(s,a);
1137  DOUT1("    Analyzing players' punishes");
1138  analyzePunishes(s,a);
1139  DOUT1("    Analyzing players' cancelling techniques");
1140  analyzeCancels(s,a);
1141  DOUT1("    Analyzing players' shielding");
1142  analyzeShield(s,a);
1143
1144  //Player-level stats
1145  DOUT1("    Computing statistics based on animations");
1146  countBasicAnimations(s,a);
1147  DOUT1("    Computing air / ground time for each player");
1148  computeAirtime(s,a);
1149  DOUT1("    Counting l cancels");
1150  countLCancels(s,a);
1151  DOUT1("    Counting techs");
1152  countTechs(s,a);
1153  DOUT1("    Counting dashdances");
1154  countDashdances(s,a);
1155  DOUT1("    Counting airdodges, wavelands, and wavedashes");
1156  countAirdodgesAndWavelands(s,a);
1157  DOUT1("    Counting phantom hits");
1158  countPhantoms(s,a);
1159  DOUT1("    Counting button pushes");
1160  countButtons(s,a);
1161  DOUT1("    Counting ledgedashes");
1162  countLedgedashes(s,a);
1163  DOUT1("    Counting act out of wait / stun");
1164  countActionability(s,a);
1165  DOUT1("    Counting number of moves used");
1166  countMoves(s,a);
1167
1168  DOUT1("    Computing trivial match info");
1169  computeTrivialInfo(s,a);
1170
1171  //Debug stuff
1172  if (_debug >= 2) {
1173    DOUT2("    Showing action states");
1174    showActionStates(s,a);
1175  }
1176
1177  a->success = true;
1178  DOUT1("  Successfully analyzed replay!");
1179  return a;
1180}
1181
1182}