Fix interpolation in title screen replays
[neverball] / share / cmd.h
1 /*
2  * Copyright (C) 2009 Neverball authors
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #ifndef CMD_H
16 #define CMD_H
17
18 /*
19  * In an attempt to improve replay compatibility, a few guidelines
20  * apply to command addition, removal, and modification:
21  *
22  * - New commands are added at the bottom of the list.
23  *
24  * - Existing commands are never modified nor removed.
25  *
26  * - The list is never reordered.  (It's tempting...)
27  *
28  * However, commands can be renamed (e.g., to add a "deprecated" tag,
29  * because it's superseded by another command).
30  */
31
32 enum cmd_type
33 {
34     CMD_NONE = 0,
35
36     CMD_END_OF_UPDATE,
37     CMD_MAKE_BALL,
38     CMD_MAKE_ITEM,
39     CMD_PICK_ITEM,
40     CMD_TILT_ANGLES,
41     CMD_SOUND,
42     CMD_TIMER,
43     CMD_STATUS,
44     CMD_COINS,
45     CMD_JUMP_ENTER,
46     CMD_JUMP_EXIT,
47     CMD_BODY_PATH,
48     CMD_BODY_TIME,
49     CMD_GOAL_OPEN,
50     CMD_SWCH_ENTER,
51     CMD_SWCH_TOGGLE,
52     CMD_SWCH_EXIT,
53     CMD_UPDATES_PER_SECOND,
54     CMD_BALL_RADIUS,
55     CMD_CLEAR_ITEMS,
56     CMD_CLEAR_BALLS,
57     CMD_BALL_POSITION,
58     CMD_BALL_BASIS,
59     CMD_BALL_PEND_BASIS,
60     CMD_VIEW_POSITION,
61     CMD_VIEW_CENTER,
62     CMD_VIEW_BASIS,
63     CMD_CURRENT_BALL,
64     CMD_PATH_FLAG,
65     CMD_STEP_SIMULATION,
66     CMD_MAP,
67     CMD_TILT_AXES,
68
69     CMD_MAX
70 };
71
72 /*
73  * Here are the members common to all structures.  Note that it
74  * explicitly says "enum cmd_type", not "int".  This allows GCC to
75  * catch and warn about unhandled command types in switch constructs
76  * (handy when adding new commands).
77  */
78
79 #define CMD_HEADER \
80     enum cmd_type type
81
82 struct cmd_end_of_update
83 {
84     CMD_HEADER;
85 };
86
87 struct cmd_make_ball
88 {
89     CMD_HEADER;
90 };
91
92 struct cmd_make_item
93 {
94     CMD_HEADER;
95     float p[3];
96     int   t;
97     int   n;
98 };
99
100 struct cmd_pick_item
101 {
102     CMD_HEADER;
103     int   hi;
104 };
105
106 struct cmd_tilt_angles
107 {
108     CMD_HEADER;
109     float x;
110     float z;
111 };
112
113 struct cmd_sound
114 {
115     CMD_HEADER;
116     char  *n;
117     float  a;
118 };
119
120 struct cmd_timer
121 {
122     CMD_HEADER;
123     float t;
124 };
125
126 struct cmd_status
127 {
128     CMD_HEADER;
129     int t;
130 };
131
132 struct cmd_coins
133 {
134     CMD_HEADER;
135     int n;
136 };
137
138 struct cmd_jump_enter
139 {
140     CMD_HEADER;
141 };
142
143 struct cmd_jump_exit
144 {
145     CMD_HEADER;
146 };
147
148 struct cmd_body_path
149 {
150     CMD_HEADER;
151     int bi;
152     int pi;
153 };
154
155 struct cmd_body_time
156 {
157     CMD_HEADER;
158     int   bi;
159     float t;
160 };
161
162 struct cmd_goal_open
163 {
164     CMD_HEADER;
165 };
166
167 struct cmd_swch_enter
168 {
169     CMD_HEADER;
170     int xi;
171 };
172
173 struct cmd_swch_toggle
174 {
175     CMD_HEADER;
176     int xi;
177 };
178
179 struct cmd_swch_exit
180 {
181     CMD_HEADER;
182     int xi;
183 };
184
185 struct cmd_updates_per_second
186 {
187     CMD_HEADER;
188     int n;
189 };
190
191 struct cmd_ball_radius
192 {
193     CMD_HEADER;
194     float r;
195 };
196
197 struct cmd_clear_items
198 {
199     CMD_HEADER;
200 };
201
202 struct cmd_clear_balls
203 {
204     CMD_HEADER;
205 };
206
207 struct cmd_ball_position
208 {
209     CMD_HEADER;
210     float p[3];
211 };
212
213 struct cmd_ball_basis
214 {
215     CMD_HEADER;
216     float e[2][3];
217 };
218
219 struct cmd_ball_pend_basis
220 {
221     CMD_HEADER;
222     float E[2][3];
223 };
224
225 struct cmd_view_position
226 {
227     CMD_HEADER;
228     float p[3];
229 };
230
231 struct cmd_view_center
232 {
233     CMD_HEADER;
234     float c[3];
235 };
236
237 struct cmd_view_basis
238 {
239     CMD_HEADER;
240     float e[2][3];
241 };
242
243 struct cmd_current_ball
244 {
245     CMD_HEADER;
246     int ui;
247 };
248
249 struct cmd_path_flag
250 {
251     CMD_HEADER;
252     int pi;
253     int f;
254 };
255
256 struct cmd_step_simulation
257 {
258     CMD_HEADER;
259     float dt;
260 };
261
262 struct cmd_map
263 {
264     CMD_HEADER;
265     char *name;
266     struct
267     {
268         int x, y;
269     } version;
270 };
271
272 struct cmd_tilt_axes
273 {
274     CMD_HEADER;
275     float x[3], z[3];
276 };
277
278 union cmd
279 {
280     CMD_HEADER;
281     struct cmd_end_of_update      eou;
282     struct cmd_make_ball          mkball;
283     struct cmd_make_item          mkitem;
284     struct cmd_pick_item          pkitem;
285     struct cmd_tilt_angles        tiltangles;
286     struct cmd_sound              sound;
287     struct cmd_timer              timer;
288     struct cmd_status             status;
289     struct cmd_coins              coins;
290     struct cmd_jump_enter         jumpenter;
291     struct cmd_jump_exit          jumpexit;
292     struct cmd_body_path          bodypath;
293     struct cmd_body_time          bodytime;
294     struct cmd_goal_open          goalopen;
295     struct cmd_swch_enter         swchenter;
296     struct cmd_swch_toggle        swchtoggle;
297     struct cmd_swch_exit          swchexit;
298     struct cmd_updates_per_second ups;
299     struct cmd_ball_radius        ballradius;
300     struct cmd_clear_items        clritems;
301     struct cmd_clear_balls        clrballs;
302     struct cmd_ball_position      ballpos;
303     struct cmd_ball_basis         ballbasis;
304     struct cmd_ball_pend_basis    ballpendbasis;
305     struct cmd_view_position      viewpos;
306     struct cmd_view_center        viewcenter;
307     struct cmd_view_basis         viewbasis;
308     struct cmd_current_ball       currball;
309     struct cmd_path_flag          pathflag;
310     struct cmd_step_simulation    stepsim;
311     struct cmd_map                map;
312     struct cmd_tilt_axes          tiltaxes;
313 };
314
315 #undef CMD_HEADER
316
317 #include "fs.h"
318
319 int cmd_put(fs_file, const union cmd *);
320 int cmd_get(fs_file, union cmd *);
321
322 void cmd_free(union cmd *);
323
324 /*---------------------------------------------------------------------------*/
325
326 struct cmd_state
327 {
328     int ups;                            /* Updates per second                */
329     int first_update;                   /* First update flag                 */
330     int next_update;                    /* Previous command was EOU          */
331     int curr_ball;                      /* Current ball index                */
332     int got_tilt_axes;                  /* Received tilt axes in this update */
333 };
334
335 #define cmd_state_init(cs) do { \
336     (cs)->ups = 0;              \
337     (cs)->first_update = 1;     \
338     (cs)->next_update = 0;      \
339     (cs)->curr_ball = 0;        \
340     (cs)->got_tilt_axes = 0;    \
341 } while (0)
342
343 /*---------------------------------------------------------------------------*/
344
345 #endif