fork/slippc

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

Patrick CrainAdded winner port and end stocks to parser JSON output; added winner port to analysis JSON outputd1100d1

0.6.0
13.1 KiB305 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#define ICHANGED(field) (not delta) || (f == 0) || (s.item[i].frame[f].field != s.item[i].frame[f-1].field)
11//Logic for outputting a comma or not depending on whether we're the first element in a JSON object
12#define JEND(a) ((a++ == 0) ? "\n" : ",\n")
13
14namespace slip {
15
16void SlippiReplay::setFrames(int32_t max_frames) {
17  this->last_frame  = max_frames;
18  this->frame_count = max_frames-this->first_frame;
19  for(unsigned i = 0; i < 4; ++i) {
20    if (this->player[i].player_type != 3) {
21      this->player[i].frame = new SlippiFrame[this->frame_count];
22      if (this->player[i].ext_char_id == CharExt::CLIMBER) { //Extra player for Ice Climbers
23        this->player[i+4].frame = new SlippiFrame[this->frame_count];
24      }
25    }
26  }
27  for(unsigned i = 0; i < MAX_ITEMS; ++i) {
28    this->item[i].frame = new SlippiItemFrame[this->frame_count];
29  }
30}
31
32void SlippiReplay::cleanup() {
33  for(unsigned i = 0; i < 4; ++i) {
34    if (this->player[i].player_type != 3) {
35      delete [] this->player[i].frame;
36      if (this->player[i].ext_char_id == CharExt::CLIMBER) { //Extra player for Ice Climbers
37        delete [] this->player[i+4].frame;
38      }
39    }
40  }
41  for(unsigned i = 0; i < MAX_ITEMS; ++i) {
42    delete [] this->item[i].frame;
43  }
44}
45
46std::string SlippiReplay::replayAsJson(bool delta) {
47  SlippiReplay s = (*this);
48
49  uint8_t _slippi_maj = (s.slippi_version_raw >> 24) & 0xff;
50  uint8_t _slippi_min = (s.slippi_version_raw >> 16) & 0xff;
51  uint8_t _slippi_rev = (s.slippi_version_raw >>  8) & 0xff;
52
53  std::stringstream ss;
54  ss << "{" << std::endl;
55
56  ss << JSTR(0,"slippi_version", s.slippi_version) << ",\n";
57  ss << JSTR(0,"parser_version", s.parser_version) << ",\n";
58  ss << JSTR(0,"game_start_raw", s.game_start_raw) << ",\n";
59  ss << JSTR(0,"start_time"    , s.start_time)     << ",\n";
60  ss << JSTR(0,"played_on"     , s.played_on)      << ",\n";
61  ss << JINT(0,"winner_id"     , s.winner_id)    << ",\n";
62  ss << JUIN(0,"timer"         , s.timer)          << ",\n";
63  ss << JINT(0,"items_on"      , s.items_on)       << ",\n";
64  ss << JUIN(0,"teams"         , s.teams)          << ",\n";
65  ss << JUIN(0,"stage"         , s.stage)          << ",\n";
66  ss << JUIN(0,"seed"          , s.seed)           << ",\n";
67  ss << JUIN(0,"pal"           , s.pal)            << ",\n";
68  ss << JUIN(0,"frozen"        , s.frozen)         << ",\n";
69  ss << JUIN(0,"end_type"      , s.end_type)       << ",\n";
70  ss << JINT(0,"lras"          , s.lras)           << ",\n";
71  ss << JUIN(0,"sudden_death"  , s.sudden_death)   << ",\n";
72  ss << JINT(0,"first_frame"   , s.first_frame)    << ",\n";
73  ss << JINT(0,"last_frame"    , s.last_frame)     << ",\n";
74  ss << JINT(0,"frame_count"   , s.frame_count)    << ",\n";
75  ss << "\"metadata\" : " << s.metadata << "\n},\n";
76
77  ss << "\"players\" : [\n";
78  for(unsigned p = 0; p < 8; ++p) {
79    unsigned pp = (p % 4);
80    if(p > 3 && s.player[pp].ext_char_id != CharExt::CLIMBER) { //If we're not Ice climbers
81      if (p == 7) {
82        ss << SPACE[ILEV] << "{}\n";
83      } else {
84        ss << SPACE[ILEV] << "{},\n";
85      }
86      continue;
87    }
88
89    ss << SPACE[ILEV] << "{\n";
90    ss << JUIN(1,"player_id"   ,pp)                                 << ",\n";
91    ss << JUIN(1,"is_follower" ,p > 3)                              << ",\n";
92    ss << JUIN(1,"ext_char_id" ,s.player[pp].ext_char_id)           << ",\n";
93    ss << JUIN(1,"player_type" ,s.player[pp].player_type)           << ",\n";
94    ss << JUIN(1,"start_stocks",s.player[pp].start_stocks)          << ",\n";
95    ss << JUIN(1,"end_stocks"  ,s.player[pp].end_stocks)            << ",\n";
96    ss << JUIN(1,"color"       ,s.player[pp].color)                 << ",\n";
97    ss << JUIN(1,"team_id"     ,s.player[pp].team_id)               << ",\n";
98    ss << JUIN(1,"cpu_level"   ,s.player[pp].cpu_level)             << ",\n";
99    ss << JUIN(1,"dash_back"   ,s.player[pp].dash_back)             << ",\n";
100    ss << JUIN(1,"shield_drop" ,s.player[pp].shield_drop)           << ",\n";
101    ss << JSTR(1,"tag_css"     ,escape_json(s.player[pp].tag_css))  << ",\n";
102    ss << JSTR(1,"tag_code"    ,escape_json(s.player[pp].tag_code)) << ",\n";
103    ss << JSTR(1,"tag_player"  ,escape_json(s.player[pp].tag))      << ",\n";
104
105    if (s.player[p].player_type == 3) {
106      ss << SPACE[ILEV] << "\"frames\" : []\n";
107    } else {
108      ss << SPACE[ILEV] << "\"frames\" : [\n";
109      for(unsigned f = 0; f < s.frame_count; ++f) {
110        ss << SPACE[ILEV*2] << "{";
111
112        int a = 0; //True for only the first thing output per line
113        if (CHANGED(follower))
114          ss << JEND(a) << JUIN(2,"follower"      ,s.player[p].frame[f].follower);
115        if (CHANGED(seed))
116          ss << JEND(a) << JUIN(2,"seed"          ,s.player[p].frame[f].seed);
117        if (CHANGED(action_pre))
118          ss << JEND(a) << JUIN(2,"action_pre"    ,s.player[p].frame[f].action_pre);
119        if (CHANGED(pos_x_pre))
120          ss << JEND(a) << JFLT(2,"pos_x_pre"     ,s.player[p].frame[f].pos_x_pre);
121        if (CHANGED(pos_y_pre))
122          ss << JEND(a) << JFLT(2,"pos_y_pre"     ,s.player[p].frame[f].pos_y_pre);
123        if (CHANGED(face_dir_pre))
124          ss << JEND(a) << JFLT(2,"face_dir_pre"  ,s.player[p].frame[f].face_dir_pre);
125        if (CHANGED(joy_x))
126          ss << JEND(a) << JFLT(2,"joy_x"         ,s.player[p].frame[f].joy_x);
127        if (CHANGED(joy_y))
128          ss << JEND(a) << JFLT(2,"joy_y"         ,s.player[p].frame[f].joy_y);
129        if (CHANGED(c_x))
130          ss << JEND(a) << JFLT(2,"c_x"           ,s.player[p].frame[f].c_x);
131        if (CHANGED(c_y))
132          ss << JEND(a) << JFLT(2,"c_y"           ,s.player[p].frame[f].c_y);
133        if (CHANGED(trigger))
134          ss << JEND(a) << JFLT(2,"trigger"       ,s.player[p].frame[f].trigger);
135        if (CHANGED(buttons))
136          ss << JEND(a) << JUIN(2,"buttons"       ,s.player[p].frame[f].buttons);
137        if (CHANGED(phys_l))
138          ss << JEND(a) << JFLT(2,"phys_l"        ,s.player[p].frame[f].phys_l);
139        if (CHANGED(phys_r))
140          ss << JEND(a) << JFLT(2,"phys_r"        ,s.player[p].frame[f].phys_r);
141        if (CHANGED(ucf_x))
142          ss << JEND(a) << JUIN(2,"ucf_x"         ,s.player[p].frame[f].ucf_x);
143        if (CHANGED(percent_pre))
144          ss << JEND(a) << JFLT(2,"percent_pre"   ,s.player[p].frame[f].percent_pre);
145        if (CHANGED(char_id))
146          ss << JEND(a) << JUIN(2,"char_id"       ,s.player[p].frame[f].char_id);
147        if (CHANGED(action_post))
148          ss << JEND(a) << JUIN(2,"action_post"   ,s.player[p].frame[f].action_post);
149        if (CHANGED(pos_x_post))
150          ss << JEND(a) << JFLT(2,"pos_x_post"    ,s.player[p].frame[f].pos_x_post);
151        if (CHANGED(pos_y_post))
152          ss << JEND(a) << JFLT(2,"pos_y_post"    ,s.player[p].frame[f].pos_y_post);
153        if (CHANGED(face_dir_post))
154          ss << JEND(a) << JFLT(2,"face_dir_post" ,s.player[p].frame[f].face_dir_post);
155        if (CHANGED(percent_post))
156          ss << JEND(a) << JFLT(2,"percent_post"  ,s.player[p].frame[f].percent_post);
157        if (CHANGED(shield))
158          ss << JEND(a) << JFLT(2,"shield"        ,s.player[p].frame[f].shield);
159        if (CHANGED(hit_with))
160          ss << JEND(a) << JUIN(2,"hit_with"      ,s.player[p].frame[f].hit_with);
161        if (CHANGED(combo))
162          ss << JEND(a) << JUIN(2,"combo"         ,s.player[p].frame[f].combo);
163        if (CHANGED(hurt_by))
164          ss << JEND(a) << JUIN(2,"hurt_by"       ,s.player[p].frame[f].hurt_by);
165        if (CHANGED(stocks))
166          ss << JEND(a) << JUIN(2,"stocks"        ,s.player[p].frame[f].stocks);
167        if (CHANGED(action_fc))
168          ss << JEND(a) << JFLT(2,"action_fc"     ,s.player[p].frame[f].action_fc);
169
170        if(MIN_VERSION(2,0,0)) {
171          if (CHANGED(flags_1))
172            ss << JEND(a) << JUIN(2,"flags_1"       ,s.player[p].frame[f].flags_1);
173          if (CHANGED(flags_2))
174            ss << JEND(a) << JUIN(2,"flags_2"       ,s.player[p].frame[f].flags_2);
175          if (CHANGED(flags_3))
176            ss << JEND(a) << JUIN(2,"flags_3"       ,s.player[p].frame[f].flags_3);
177          if (CHANGED(flags_4))
178            ss << JEND(a) << JUIN(2,"flags_4"       ,s.player[p].frame[f].flags_4);
179          if (CHANGED(flags_5))
180            ss << JEND(a) << JUIN(2,"flags_5"       ,s.player[p].frame[f].flags_5);
181          if (CHANGED(hitstun))
182            ss << JEND(a) << JUIN(2,"hitstun"       ,s.player[p].frame[f].hitstun);
183          if (CHANGED(airborne))
184            ss << JEND(a) << JUIN(2,"airborne"      ,s.player[p].frame[f].airborne);
185          if (CHANGED(ground_id))
186            ss << JEND(a) << JUIN(2,"ground_id"     ,s.player[p].frame[f].ground_id);
187          if (CHANGED(jumps))
188            ss << JEND(a) << JUIN(2,"jumps"         ,s.player[p].frame[f].jumps);
189          if (CHANGED(l_cancel))
190            ss << JEND(a) << JUIN(2,"l_cancel"      ,s.player[p].frame[f].l_cancel);
191          if (CHANGED(alive))
192            ss << JEND(a) << JINT(2,"alive"         ,s.player[p].frame[f].alive);
193        }
194
195        if(MIN_VERSION(2,1,0)) {
196          if (CHANGED(hurtbox))
197            ss << JEND(a) << JUIN(2,"hurtbox"       ,s.player[p].frame[f].hurtbox);
198        }
199
200        if(MIN_VERSION(3,5,0)) {
201          if (CHANGED(self_air_x))
202            ss << JEND(a) << JFLT(2,"self_air_x"    ,s.player[p].frame[f].self_air_x);
203          if (CHANGED(self_air_y))
204            ss << JEND(a) << JFLT(2,"self_air_y"    ,s.player[p].frame[f].self_air_y);
205          if (CHANGED(attack_x))
206            ss << JEND(a) << JFLT(2,"attack_x"      ,s.player[p].frame[f].attack_x);
207          if (CHANGED(attack_y))
208            ss << JEND(a) << JFLT(2,"attack_y"      ,s.player[p].frame[f].attack_y);
209          if (CHANGED(self_grd_x))
210            ss << JEND(a) << JFLT(2,"self_grd_x"    ,s.player[p].frame[f].self_grd_x);
211        }
212
213        if(MIN_VERSION(3,8,0)) {
214          if (CHANGED(hitlag))
215            ss << JEND(a) << JFLT(2,"hitlag"        ,s.player[p].frame[f].hitlag);
216        }
217
218
219        if (f < s.frame_count-1) {
220          ss << "\n" << SPACE[ILEV*2] << "},\n";
221        } else {
222          ss << "\n" << SPACE[ILEV*2] << "}\n";
223        }
224      }
225      ss << SPACE[ILEV*2] << "]\n";
226    }
227    if (p == 7) {
228      ss << SPACE[ILEV] << "}\n";
229    } else {
230      ss << SPACE[ILEV] << "},\n";
231    }
232  }
233  if (MAX_VERSION(3,0,0)) {
234    ss << "]\n";
235  } else {
236    ss << "],\n";
237    ss << "\"items\" : [\n";
238    for(unsigned i = 0; i < MAX_ITEMS; ++i) {
239      if (s.item[i].spawn_id > MAX_ITEMS) {
240        break;
241      }
242      ss << SPACE[ILEV] << "{\n";
243      ss << JUIN(1,"spawn_id" ,s.item[i].spawn_id)           << ",\n";
244      ss << JUIN(1,"item_type",s.item[i].type)               << ",\n";
245      ss << SPACE[ILEV] << "\"frames\" : [\n";
246
247      for(unsigned f = 0; f < s.item[i].num_frames; ++f) {
248        ss << SPACE[ILEV*2] << "{";
249        int a = 0; //True for only the first thing output per line
250
251        ss << JEND(a) << JUIN(2,"frame"      ,s.item[i].frame[f].frame);
252        if (ICHANGED(state))
253          ss << JEND(a) << JUIN(2,"state"      ,s.item[i].frame[f].state);
254        if (ICHANGED(face_dir))
255          ss << JEND(a) << JFLT(2,"face_dir"   ,s.item[i].frame[f].face_dir);
256        if (ICHANGED(xvel))
257          ss << JEND(a) << JFLT(2,"xvel"       ,s.item[i].frame[f].xvel);
258        if (ICHANGED(yvel))
259          ss << JEND(a) << JFLT(2,"yvel"       ,s.item[i].frame[f].yvel);
260        if (ICHANGED(xpos))
261          ss << JEND(a) << JFLT(2,"xpos"       ,s.item[i].frame[f].xpos);
262        if (ICHANGED(ypos))
263          ss << JEND(a) << JFLT(2,"ypos"       ,s.item[i].frame[f].ypos);
264        if (ICHANGED(damage))
265          ss << JEND(a) << JUIN(2,"damage"     ,s.item[i].frame[f].damage);
266        if (ICHANGED(expire))
267          ss << JEND(a) << JFLT(2,"expire"     ,s.item[i].frame[f].expire);
268
269        if(MIN_VERSION(3,2,0)) {
270          if (ICHANGED(flags_1))
271            ss << JEND(a) << JUIN(2,"flags_1"     ,s.item[i].frame[f].flags_1);
272          if (ICHANGED(flags_2))
273            ss << JEND(a) << JUIN(2,"flags_2"     ,s.item[i].frame[f].flags_2);
274          if (ICHANGED(flags_3))
275            ss << JEND(a) << JUIN(2,"flags_3"     ,s.item[i].frame[f].flags_3);
276          if (ICHANGED(flags_4))
277            ss << JEND(a) << JUIN(2,"flags_4"     ,s.item[i].frame[f].flags_4);
278          if(MIN_VERSION(3,6,0)) {
279            if (ICHANGED(owner))
280              ss << JEND(a) << JINT(2,"owner"      ,s.item[i].frame[f].owner);
281          }
282        }
283
284        if (f+1 == s.item[i].num_frames) {
285          ss << "\n" << SPACE[ILEV*2] << "}\n";
286        } else {
287          ss << "\n" << SPACE[ILEV*2] << "},\n";
288        }
289
290      }
291
292      if (s.item[i+1].spawn_id > MAX_ITEMS) {
293        ss << SPACE[ILEV] << "]}\n";
294      } else {
295        ss << SPACE[ILEV] << "]},\n";
296      }
297    }
298    ss << "]\n";
299  }
300
301  ss << "}" << std::endl;
302  return ss.str();
303}
304
305}