fork/slippc

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

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

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