fork/slippc

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

Patrick CrainUpdated parser to decode tags properly and record CPU levels; updated parser version to 0.2.0d3d6f97

0.4.0
8.9 KiB197 linesraw
1#include "replay.h"
2
3//JSON Output shortcuts
4#define JFLT(i,k,n) SPACE[ILEV*(i)] << "\"" << (k) << "\" : " << float(n)
5#define JINT(i,k,n) SPACE[ILEV*(i)] << "\"" << (k) << "\" : " << int32_t(n)
6#define JUIN(i,k,n) SPACE[ILEV*(i)] << "\"" << (k) << "\" : " << uint32_t(n)
7#define JSTR(i,k,s) SPACE[ILEV*(i)] << "\"" << (k) << "\" : \"" << (s) << "\""
8//Logic for outputting a line only if it changed since last frame (or if we're in full output mode)
9#define CHANGED(field) (not delta) || (f == 0) || (s.player[p].frame[f].field != s.player[p].frame[f-1].field)
10//Logic for outputting a comma or not depending on whether we're the first element in a JSON object
11#define JEND(a) ((a++ == 0) ? "\n" : ",\n")
12
13namespace slip {
14
15void SlippiReplay::setFrames(int32_t max_frames) {
16  this->last_frame  = max_frames;
17  this->frame_count = max_frames-this->first_frame;
18  for(unsigned i = 0; i < 4; ++i) {
19    if (this->player[i].player_type != 3) {
20      this->player[i].frame = new SlippiFrame[this->frame_count];
21      if (this->player[i].ext_char_id == CharExt::CLIMBER) { //Extra player for Ice Climbers
22        this->player[i+4].frame = new SlippiFrame[this->frame_count];
23      }
24    }
25  }
26}
27
28void SlippiReplay::cleanup() {
29  for(unsigned i = 0; i < 4; ++i) {
30    if (this->player[i].player_type != 3) {
31      delete [] this->player[i].frame;
32      if (this->player[i].ext_char_id == CharExt::CLIMBER) { //Extra player for Ice Climbers
33        delete [] this->player[i+4].frame;
34      }
35    }
36  }
37}
38
39std::string SlippiReplay::replayAsJson(bool delta) {
40  SlippiReplay s = (*this);
41  std::stringstream ss;
42  ss << "{" << std::endl;
43
44  ss << JSTR(0,"slippi_version", s.slippi_version) << ",\n";
45  ss << JSTR(0,"parser_version", s.parser_version) << ",\n";
46  ss << JSTR(0,"game_start_raw", s.game_start_raw) << ",\n";
47  ss << JSTR(0,"start_time"    , s.start_time)     << ",\n";
48  ss << JSTR(0,"played_on"     , s.played_on)      << ",\n";
49  ss << JUIN(0,"timer"         , s.timer)          << ",\n";
50  ss << JINT(0,"items"         , s.items)          << ",\n";
51  ss << JUIN(0,"teams"         , s.teams)          << ",\n";
52  ss << JUIN(0,"stage"         , s.stage)          << ",\n";
53  ss << JUIN(0,"seed"          , s.seed)           << ",\n";
54  ss << JUIN(0,"pal"           , s.pal)            << ",\n";
55  ss << JUIN(0,"frozen"        , s.frozen)         << ",\n";
56  ss << JUIN(0,"end_type"      , s.end_type)       << ",\n";
57  ss << JINT(0,"lras"          , s.lras)           << ",\n";
58  ss << JUIN(0,"sudden_death"  , s.sudden_death)   << ",\n";
59  ss << JINT(0,"first_frame"   , s.first_frame)    << ",\n";
60  ss << JINT(0,"last_frame"    , s.last_frame)     << ",\n";
61  ss << JINT(0,"frame_count"   , s.frame_count)    << ",\n";
62  ss << "\"metadata\" : " << s.metadata << "\n},\n";
63
64  ss << "\"players\" : [\n";
65  for(unsigned p = 0; p < 8; ++p) {
66    unsigned pp = (p % 4);
67    if(p > 3 && s.player[pp].ext_char_id != CharExt::CLIMBER) { //If we're not Ice climbers
68      if (p == 7) {
69        ss << SPACE[ILEV] << "{}\n";
70      } else {
71        ss << SPACE[ILEV] << "{},\n";
72      }
73      continue;
74    }
75
76    ss << SPACE[ILEV] << "{\n";
77    ss << JUIN(1,"player_id"   ,pp)                                << ",\n";
78    ss << JUIN(1,"is_follower" ,p > 3)                             << ",\n";
79    ss << JUIN(1,"ext_char_id" ,s.player[pp].ext_char_id)          << ",\n";
80    ss << JUIN(1,"player_type" ,s.player[pp].player_type)          << ",\n";
81    ss << JUIN(1,"start_stocks",s.player[pp].start_stocks)         << ",\n";
82    ss << JUIN(1,"color"       ,s.player[pp].color)                << ",\n";
83    ss << JUIN(1,"team_id"     ,s.player[pp].team_id)              << ",\n";
84    ss << JUIN(1,"cpu_level"   ,s.player[pp].cpu_level)            << ",\n";
85    ss << JUIN(1,"dash_back"   ,s.player[pp].dash_back)            << ",\n";
86    ss << JUIN(1,"shield_drop" ,s.player[pp].shield_drop)          << ",\n";
87    ss << JSTR(1,"tag_css"     ,escape_json(s.player[pp].tag_css)) << ",\n";
88    ss << JSTR(1,"tag_player"  ,escape_json(s.player[pp].tag))     << ",\n";
89
90    if (s.player[p].player_type == 3) {
91      ss << SPACE[ILEV] << "\"frames\" : []\n";
92    } else {
93      ss << SPACE[ILEV] << "\"frames\" : [\n";
94      for(unsigned f = 0; f < s.frame_count; ++f) {
95        ss << SPACE[ILEV*2] << "{";
96
97        int a = 0; //True for only the first thing output per line
98        if (CHANGED(follower))
99          ss << JEND(a) << JUIN(2,"follower"      ,s.player[p].frame[f].follower);
100        if (CHANGED(seed))
101          ss << JEND(a) << JUIN(2,"seed"          ,s.player[p].frame[f].seed);
102        if (CHANGED(action_pre))
103          ss << JEND(a) << JUIN(2,"action_pre"    ,s.player[p].frame[f].action_pre);
104        if (CHANGED(pos_x_pre))
105          ss << JEND(a) << JFLT(2,"pos_x_pre"     ,s.player[p].frame[f].pos_x_pre);
106        if (CHANGED(pos_y_pre))
107          ss << JEND(a) << JFLT(2,"pos_y_pre"     ,s.player[p].frame[f].pos_y_pre);
108        if (CHANGED(face_dir_pre))
109          ss << JEND(a) << JFLT(2,"face_dir_pre"  ,s.player[p].frame[f].face_dir_pre);
110        if (CHANGED(joy_x))
111          ss << JEND(a) << JFLT(2,"joy_x"         ,s.player[p].frame[f].joy_x);
112        if (CHANGED(joy_y))
113          ss << JEND(a) << JFLT(2,"joy_y"         ,s.player[p].frame[f].joy_y);
114        if (CHANGED(c_x))
115          ss << JEND(a) << JFLT(2,"c_x"           ,s.player[p].frame[f].c_x);
116        if (CHANGED(c_y))
117          ss << JEND(a) << JFLT(2,"c_y"           ,s.player[p].frame[f].c_y);
118        if (CHANGED(trigger))
119          ss << JEND(a) << JFLT(2,"trigger"       ,s.player[p].frame[f].trigger);
120        if (CHANGED(buttons))
121          ss << JEND(a) << JUIN(2,"buttons"       ,s.player[p].frame[f].buttons);
122        if (CHANGED(phys_l))
123          ss << JEND(a) << JFLT(2,"phys_l"        ,s.player[p].frame[f].phys_l);
124        if (CHANGED(phys_r))
125          ss << JEND(a) << JFLT(2,"phys_r"        ,s.player[p].frame[f].phys_r);
126        if (CHANGED(ucf_x))
127          ss << JEND(a) << JUIN(2,"ucf_x"         ,s.player[p].frame[f].ucf_x);
128        if (CHANGED(percent_pre))
129          ss << JEND(a) << JFLT(2,"percent_pre"   ,s.player[p].frame[f].percent_pre);
130        if (CHANGED(char_id))
131          ss << JEND(a) << JUIN(2,"char_id"       ,s.player[p].frame[f].char_id);
132        if (CHANGED(action_post))
133          ss << JEND(a) << JUIN(2,"action_post"   ,s.player[p].frame[f].action_post);
134        if (CHANGED(pos_x_post))
135          ss << JEND(a) << JFLT(2,"pos_x_post"    ,s.player[p].frame[f].pos_x_post);
136        if (CHANGED(pos_y_post))
137          ss << JEND(a) << JFLT(2,"pos_y_post"    ,s.player[p].frame[f].pos_y_post);
138        if (CHANGED(face_dir_post))
139          ss << JEND(a) << JFLT(2,"face_dir_post" ,s.player[p].frame[f].face_dir_post);
140        if (CHANGED(percent_post))
141          ss << JEND(a) << JFLT(2,"percent_post"  ,s.player[p].frame[f].percent_post);
142        if (CHANGED(shield))
143          ss << JEND(a) << JFLT(2,"shield"        ,s.player[p].frame[f].shield);
144        if (CHANGED(hit_with))
145          ss << JEND(a) << JUIN(2,"hit_with"      ,s.player[p].frame[f].hit_with);
146        if (CHANGED(combo))
147          ss << JEND(a) << JUIN(2,"combo"         ,s.player[p].frame[f].combo);
148        if (CHANGED(hurt_by))
149          ss << JEND(a) << JUIN(2,"hurt_by"       ,s.player[p].frame[f].hurt_by);
150        if (CHANGED(stocks))
151          ss << JEND(a) << JUIN(2,"stocks"        ,s.player[p].frame[f].stocks);
152        if (CHANGED(action_fc))
153          ss << JEND(a) << JFLT(2,"action_fc"     ,s.player[p].frame[f].action_fc);
154        if (CHANGED(flags_1))
155          ss << JEND(a) << JUIN(2,"flags_1"       ,s.player[p].frame[f].flags_1);
156        if (CHANGED(flags_2))
157          ss << JEND(a) << JUIN(2,"flags_2"       ,s.player[p].frame[f].flags_2);
158        if (CHANGED(flags_3))
159          ss << JEND(a) << JUIN(2,"flags_3"       ,s.player[p].frame[f].flags_3);
160        if (CHANGED(flags_4))
161          ss << JEND(a) << JUIN(2,"flags_4"       ,s.player[p].frame[f].flags_4);
162        if (CHANGED(flags_5))
163          ss << JEND(a) << JUIN(2,"flags_5"       ,s.player[p].frame[f].flags_5);
164        if (CHANGED(hitstun))
165          ss << JEND(a) << JUIN(2,"hitstun"       ,s.player[p].frame[f].hitstun);
166        if (CHANGED(airborne))
167          ss << JEND(a) << JUIN(2,"airborne"      ,s.player[p].frame[f].airborne);
168        if (CHANGED(ground_id))
169          ss << JEND(a) << JUIN(2,"ground_id"     ,s.player[p].frame[f].ground_id);
170        if (CHANGED(jumps))
171          ss << JEND(a) << JUIN(2,"jumps"         ,s.player[p].frame[f].jumps);
172        if (CHANGED(l_cancel))
173          ss << JEND(a) << JUIN(2,"l_cancel"      ,s.player[p].frame[f].l_cancel);
174        if (CHANGED(alive))
175          ss << JEND(a) << JINT(2,"alive"         ,s.player[p].frame[f].alive);
176
177        if (f < s.frame_count-1) {
178          ss << "\n" << SPACE[ILEV*2] << "},\n";
179        } else {
180          ss << "\n" << SPACE[ILEV*2] << "}\n";
181        }
182      }
183      ss << SPACE[ILEV*2] << "]\n";
184    }
185    if (p == 7) {
186      ss << SPACE[ILEV] << "}\n";
187    } else {
188      ss << SPACE[ILEV] << "},\n";
189    }
190  }
191  ss << "]\n";
192
193  ss << "}" << std::endl;
194  return ss.str();
195}
196
197}