fork/slippc

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

Patrick CrainFixed code for measuring GALINT off ledgedashes and NILs from ledgedaeb52a

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