fork/slippc

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

Patrick CrainUpdated version to 0.5.0; added Slippi Code metadata to parser and analyzer outputs9683729

0.5.0
9.0 KiB198 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_code"    ,escape_json(s.player[pp].tag_code)) << ",\n";
89    ss << JSTR(1,"tag_player"  ,escape_json(s.player[pp].tag))      << ",\n";
90
91    if (s.player[p].player_type == 3) {
92      ss << SPACE[ILEV] << "\"frames\" : []\n";
93    } else {
94      ss << SPACE[ILEV] << "\"frames\" : [\n";
95      for(unsigned f = 0; f < s.frame_count; ++f) {
96        ss << SPACE[ILEV*2] << "{";
97
98        int a = 0; //True for only the first thing output per line
99        if (CHANGED(follower))
100          ss << JEND(a) << JUIN(2,"follower"      ,s.player[p].frame[f].follower);
101        if (CHANGED(seed))
102          ss << JEND(a) << JUIN(2,"seed"          ,s.player[p].frame[f].seed);
103        if (CHANGED(action_pre))
104          ss << JEND(a) << JUIN(2,"action_pre"    ,s.player[p].frame[f].action_pre);
105        if (CHANGED(pos_x_pre))
106          ss << JEND(a) << JFLT(2,"pos_x_pre"     ,s.player[p].frame[f].pos_x_pre);
107        if (CHANGED(pos_y_pre))
108          ss << JEND(a) << JFLT(2,"pos_y_pre"     ,s.player[p].frame[f].pos_y_pre);
109        if (CHANGED(face_dir_pre))
110          ss << JEND(a) << JFLT(2,"face_dir_pre"  ,s.player[p].frame[f].face_dir_pre);
111        if (CHANGED(joy_x))
112          ss << JEND(a) << JFLT(2,"joy_x"         ,s.player[p].frame[f].joy_x);
113        if (CHANGED(joy_y))
114          ss << JEND(a) << JFLT(2,"joy_y"         ,s.player[p].frame[f].joy_y);
115        if (CHANGED(c_x))
116          ss << JEND(a) << JFLT(2,"c_x"           ,s.player[p].frame[f].c_x);
117        if (CHANGED(c_y))
118          ss << JEND(a) << JFLT(2,"c_y"           ,s.player[p].frame[f].c_y);
119        if (CHANGED(trigger))
120          ss << JEND(a) << JFLT(2,"trigger"       ,s.player[p].frame[f].trigger);
121        if (CHANGED(buttons))
122          ss << JEND(a) << JUIN(2,"buttons"       ,s.player[p].frame[f].buttons);
123        if (CHANGED(phys_l))
124          ss << JEND(a) << JFLT(2,"phys_l"        ,s.player[p].frame[f].phys_l);
125        if (CHANGED(phys_r))
126          ss << JEND(a) << JFLT(2,"phys_r"        ,s.player[p].frame[f].phys_r);
127        if (CHANGED(ucf_x))
128          ss << JEND(a) << JUIN(2,"ucf_x"         ,s.player[p].frame[f].ucf_x);
129        if (CHANGED(percent_pre))
130          ss << JEND(a) << JFLT(2,"percent_pre"   ,s.player[p].frame[f].percent_pre);
131        if (CHANGED(char_id))
132          ss << JEND(a) << JUIN(2,"char_id"       ,s.player[p].frame[f].char_id);
133        if (CHANGED(action_post))
134          ss << JEND(a) << JUIN(2,"action_post"   ,s.player[p].frame[f].action_post);
135        if (CHANGED(pos_x_post))
136          ss << JEND(a) << JFLT(2,"pos_x_post"    ,s.player[p].frame[f].pos_x_post);
137        if (CHANGED(pos_y_post))
138          ss << JEND(a) << JFLT(2,"pos_y_post"    ,s.player[p].frame[f].pos_y_post);
139        if (CHANGED(face_dir_post))
140          ss << JEND(a) << JFLT(2,"face_dir_post" ,s.player[p].frame[f].face_dir_post);
141        if (CHANGED(percent_post))
142          ss << JEND(a) << JFLT(2,"percent_post"  ,s.player[p].frame[f].percent_post);
143        if (CHANGED(shield))
144          ss << JEND(a) << JFLT(2,"shield"        ,s.player[p].frame[f].shield);
145        if (CHANGED(hit_with))
146          ss << JEND(a) << JUIN(2,"hit_with"      ,s.player[p].frame[f].hit_with);
147        if (CHANGED(combo))
148          ss << JEND(a) << JUIN(2,"combo"         ,s.player[p].frame[f].combo);
149        if (CHANGED(hurt_by))
150          ss << JEND(a) << JUIN(2,"hurt_by"       ,s.player[p].frame[f].hurt_by);
151        if (CHANGED(stocks))
152          ss << JEND(a) << JUIN(2,"stocks"        ,s.player[p].frame[f].stocks);
153        if (CHANGED(action_fc))
154          ss << JEND(a) << JFLT(2,"action_fc"     ,s.player[p].frame[f].action_fc);
155        if (CHANGED(flags_1))
156          ss << JEND(a) << JUIN(2,"flags_1"       ,s.player[p].frame[f].flags_1);
157        if (CHANGED(flags_2))
158          ss << JEND(a) << JUIN(2,"flags_2"       ,s.player[p].frame[f].flags_2);
159        if (CHANGED(flags_3))
160          ss << JEND(a) << JUIN(2,"flags_3"       ,s.player[p].frame[f].flags_3);
161        if (CHANGED(flags_4))
162          ss << JEND(a) << JUIN(2,"flags_4"       ,s.player[p].frame[f].flags_4);
163        if (CHANGED(flags_5))
164          ss << JEND(a) << JUIN(2,"flags_5"       ,s.player[p].frame[f].flags_5);
165        if (CHANGED(hitstun))
166          ss << JEND(a) << JUIN(2,"hitstun"       ,s.player[p].frame[f].hitstun);
167        if (CHANGED(airborne))
168          ss << JEND(a) << JUIN(2,"airborne"      ,s.player[p].frame[f].airborne);
169        if (CHANGED(ground_id))
170          ss << JEND(a) << JUIN(2,"ground_id"     ,s.player[p].frame[f].ground_id);
171        if (CHANGED(jumps))
172          ss << JEND(a) << JUIN(2,"jumps"         ,s.player[p].frame[f].jumps);
173        if (CHANGED(l_cancel))
174          ss << JEND(a) << JUIN(2,"l_cancel"      ,s.player[p].frame[f].l_cancel);
175        if (CHANGED(alive))
176          ss << JEND(a) << JINT(2,"alive"         ,s.player[p].frame[f].alive);
177
178        if (f < s.frame_count-1) {
179          ss << "\n" << SPACE[ILEV*2] << "},\n";
180        } else {
181          ss << "\n" << SPACE[ILEV*2] << "}\n";
182        }
183      }
184      ss << SPACE[ILEV*2] << "]\n";
185    }
186    if (p == 7) {
187      ss << SPACE[ILEV] << "}\n";
188    } else {
189      ss << SPACE[ILEV] << "},\n";
190    }
191  }
192  ss << "]\n";
193
194  ss << "}" << std::endl;
195  return ss.str();
196}
197
198}