fork/slippc

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

Patrick CrainAdded Windows makefilec826ef3

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