new replay handling, warning, old replays are incopatible and maybe these is some...
[neverball] / ball / st_demo.c
1 /*   
2  * Copyright (C) 2003 Robert Kooima
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 #include "gui.h"
16 #include "hud.h"
17 #include "set.h"
18 #include "game.h"
19 #include "demo.h"
20 #include "level.h"
21 #include "audio.h"
22 #include "solid.h"
23 #include "config.h"
24
25 #include "st_demo.h"
26 #include "st_title.h"
27
28 /*---------------------------------------------------------------------------*/
29
30 #define DEMO_BACK -1
31 #define DEMO_NEXT -2
32 #define DEMO_PREV -3
33
34 #define DEMO_LINE 4
35 #define DEMO_STEP 8
36
37 static int first = 0;
38 static int total = 0;
39
40 static float replay_time;
41 static float global_time;
42
43 /*---------------------------------------------------------------------------*/
44
45 static int demo_action(int i)
46 {
47     audio_play(AUD_MENU, 1.0f);
48
49     switch (i)
50     {
51     case DEMO_BACK:
52         return goto_state(&st_title);
53
54     case DEMO_NEXT:
55         first += DEMO_STEP;
56         return goto_state(&st_demo);
57         break;
58
59     case DEMO_PREV:
60         first -= DEMO_STEP;
61         return goto_state(&st_demo);
62         break;
63
64     default:
65         if (level_replay(demo_name(i)))
66             return goto_state(&st_demo_play);
67     }
68     return 1;
69 }
70
71 static void demo_replay(int id, int i)
72 {
73     int w = config_get_d(CONFIG_WIDTH);
74     int h = config_get_d(CONFIG_HEIGHT);
75     int jd;
76
77     if ((jd = gui_vstack(id)))
78     {
79         gui_space(jd);
80
81         gui_image(jd, demo_shot(i), w / 6, h / 6);
82         gui_state(jd, demo_name(i), GUI_SML, i, 0);
83
84         gui_active(jd, i, 0);
85     }
86 }
87
88 static int name_id;
89 static int time_id;
90 static int coin_id;
91 static int date_id;
92 static int mode_id;
93 static int player_id;
94
95 static void demo_status(int i)
96 {
97     int m = demo_mode(i);
98     char * ms;
99     switch (m)
100     {
101         case MODE_CHALLENGE: ms = _("Challenge"); break;
102         case MODE_NORMAL:    ms = _("Normal");    break;
103         case MODE_PRACTICE:  ms = _("Practice");  break;
104         default: ms = "???";
105     }
106     gui_set_label(name_id,   demo_name(i));
107     gui_set_label(date_id,   demo_str_date(i));
108     gui_set_label(player_id, demo_player(i));
109     gui_set_label(mode_id,   ms);
110     gui_set_count(coin_id,   demo_coins(i));
111     gui_set_clock(time_id,   demo_clock(i));
112 }
113
114 static int demo_enter(void)
115 {
116     int i, j;
117     int id, jd, kd, ld;
118
119     total = demo_scan();
120
121     if ((id = gui_vstack(0)))
122     {
123         if (total == 0)
124         {
125             gui_label(id, _("No Replay"), GUI_MED, GUI_ALL, 0,0);
126             gui_filler(id);
127             gui_multi(id, _("You can save replay of you games.\\Currently, there is no replay saved."), GUI_SML, GUI_ALL, gui_wht, gui_wht);
128             gui_filler(id);
129             gui_start(id, _("Back"), GUI_SML, DEMO_BACK, 0);
130         }
131     else
132     {
133         if ((jd = gui_hstack(id)))
134         {
135
136             ld = gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
137             gui_filler(jd);
138
139             
140             
141             if (first + DEMO_STEP < total)
142                 gui_state(jd, _("Next"), GUI_SML, DEMO_NEXT, 0);
143             else
144                 gui_label(jd, _("Next"), GUI_SML, GUI_ALL,   gui_blk, gui_blk);
145
146             if (first > 0)
147                 gui_state(jd, _("Prev"), GUI_SML, DEMO_PREV, 0);
148             else
149                 gui_label(jd, _("Prev"), GUI_SML, GUI_ALL,   gui_blk, gui_blk);
150             
151             gui_start(jd, _("Back"), GUI_SML, DEMO_BACK, 0);
152         }
153         if ((jd = gui_varray(id)))
154             for (i = first; i < first + DEMO_STEP ; i += DEMO_LINE)
155                 if ((kd = gui_harray(jd)))
156                 {
157                     for (j = i + DEMO_LINE - 1; j >= i; j--)
158                         if (j < total)
159                             demo_replay(kd, j);
160                         else
161                             gui_space(kd);
162                 }
163         gui_filler(id);
164         if ((jd = gui_hstack(id)))
165         {
166             if((kd = gui_vstack(jd)))
167             {
168                 if ((ld = gui_harray(kd)))
169                 {
170                     coin_id = gui_count(ld, 100,          GUI_SML, GUI_RGT);
171                     gui_label(ld, _("Coins"),             GUI_SML, GUI_LFT, gui_wht, gui_wht);
172                     time_id = gui_clock(ld, 35000,        GUI_SML, GUI_RGT);
173                     gui_label(ld, _("Time"),              GUI_SML, GUI_LFT, gui_wht, gui_wht);
174                     name_id = gui_label(ld, demo_name(0), GUI_SML, GUI_RGT, 0, 0);
175                 }
176                 if ((ld = gui_harray(kd)))
177                 {
178                     mode_id = gui_label(ld, "..............", GUI_SML, GUI_RGT, 0, 0);
179                     gui_label(ld, _("Mode"),                  GUI_SML, GUI_LFT, gui_wht, gui_wht);
180                     player_id = gui_label(ld, demo_player(0), GUI_SML, GUI_RGT, 0, 0);
181                 }
182                 date_id = gui_label(kd, demo_str_date(0),     GUI_SML, GUI_RGT, 0, 0);
183             }
184             if((kd = gui_vstack(jd)))
185             {
186                 gui_label(kd, _("Name"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
187                 gui_label(kd, _("Player"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
188                 gui_label(kd, _("Date"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
189             }
190         }
191         demo_status(0);
192     }
193
194         gui_layout(id, 0, 0);
195     }
196
197     audio_music_fade_to(0.5f, "bgm/inter.ogg");
198
199     return id;
200 }
201
202 static void demo_leave(int id)
203 {
204     gui_delete(id);
205 }
206
207 static void demo_paint(int id, float st)
208 {
209     game_draw(0, st);
210     config_pop_matrix();
211     gui_paint(id);
212 }
213
214 static void demo_timer(int id, float dt)
215 {
216     gui_timer(id, dt);
217     audio_timer(dt);
218 }
219
220 static void demo_point(int id, int x, int y, int dx, int dy)
221 {
222     int jd = gui_point(id, x, y);
223     int i  = gui_token(jd);
224     gui_pulse(jd, 1.2f);
225     if (jd && i>=0)
226         demo_status(i);
227 }
228
229 static void demo_stick(int id, int a, int v)
230 {
231     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
232         gui_pulse(gui_stick(id, v, 0), 1.2f);
233     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
234         gui_pulse(gui_stick(id, 0, v), 1.2f);
235 }
236
237 static int demo_click(int b, int d)
238 {
239     if (b <= 0 && d == 1)
240         return demo_action(gui_token(gui_click()));
241     return 1;
242 }
243
244 static int demo_buttn(int b, int d)
245 {
246     if (d)
247     {
248         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
249             return demo_click(0, 1);
250         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
251             return demo_action(DEMO_BACK);
252     }
253     return 1;
254 }
255
256 /*---------------------------------------------------------------------------*/
257
258 static int demo_play_enter(void)
259 {
260     int id;
261
262     if ((id = gui_label(0, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn)))
263     {
264         gui_layout(id, 0, 0);
265         gui_pulse(id, 1.2f);
266     }
267
268     global_time = -1.f;
269     replay_time =  0.f;
270
271     game_set_fly(0.f);
272
273     return id;
274 }
275
276 static void demo_play_leave(int id)
277 {
278     gui_delete(id);
279 }
280
281 static void demo_play_paint(int id, float st)
282 {
283     game_draw(0, st);
284     hud_paint();
285
286     if (time_state() < 1.f)
287         gui_paint(id);
288 }
289
290 static void demo_play_timer(int id, float dt)
291 {
292     float t;
293
294     game_step_fade(dt);
295     gui_timer(id, dt);
296     audio_timer(dt);
297
298     global_time += dt;
299     hud_timer(dt);
300
301     /* Spin or skip depending on how fast the demo wants to run. */
302
303     while (replay_time < global_time)
304         if (demo_replay_step(&t))
305         {
306             replay_time += t;
307         }
308         else 
309         {
310             goto_state(&st_demo_end);
311             break;
312         }
313 }
314
315 static int demo_play_buttn(int b, int d)
316 {
317     if (d)
318     {
319         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
320             return goto_state(&st_demo_end);
321     }
322     return 1;
323 }
324
325 /*---------------------------------------------------------------------------*/
326
327 #define DEMO_KEEP 0
328 #define DEMO_DEL  1
329
330 static int demo_end_action(int i)
331 {
332     audio_play(AUD_MENU, 1.0f);
333
334     if (i == DEMO_DEL)
335         return goto_state(&st_demo_del);
336
337     else
338     {
339         demo_replay_stop(0);
340         return goto_state(&st_demo);
341     }
342 }
343
344 static int demo_end_enter(void)
345 {
346     int id, jd, kd;
347
348     if ((id = gui_vstack(0)))
349     {
350         kd = gui_label(id, _("Replay Ends"), GUI_MED, GUI_ALL, gui_gry, gui_red);
351
352         if ((jd = gui_harray(id)))
353         {
354             gui_state(jd, _("Delete"), GUI_SML, DEMO_DEL,  0);
355             gui_start(jd, _("Keep"),   GUI_SML, DEMO_KEEP, 1);
356         }
357
358         gui_pulse(kd, 1.2f);
359         gui_layout(id, 0, 0);
360     }
361     audio_music_fade_out(2.0f);
362
363     return id;
364 }
365
366 static void demo_end_leave(int id)
367 {
368     gui_delete(id);
369 }
370
371 static void demo_end_paint(int id, float st)
372 {
373     game_draw(0, st);
374     gui_paint(id);
375 }
376
377 static void demo_end_timer(int id, float dt)
378 {
379     float g[3] = { 0.0f, -9.8f, 0.0f };
380
381     if (time_state() < 2.f)
382         game_step(g, dt, 0);
383                 
384     gui_timer(id, dt);
385     audio_timer(dt);
386 }
387
388 static void demo_end_point(int id, int x, int y, int dx, int dy)
389 {
390     gui_pulse(gui_point(id, x, y), 1.2f);
391 }
392
393 static void demo_end_stick(int id, int a, int v)
394 {
395     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
396         gui_pulse(gui_stick(id, v, 0), 1.2f);
397     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
398         gui_pulse(gui_stick(id, 0, v), 1.2f);
399 }
400
401 static int demo_end_click(int b, int d)
402 {
403     if (d && b < 0)
404         return demo_end_action(gui_token(gui_click()));
405     return 1;
406 }
407
408 static int demo_end_buttn(int b, int d)
409 {
410     if (d)
411     {
412         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
413             return demo_end_action(gui_token(gui_click()));
414         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
415             return demo_end_action(DEMO_KEEP);
416     }
417     return 1;
418 }
419
420 /*---------------------------------------------------------------------------*/
421
422 static int demo_del_action(int i)
423 {
424     audio_play(AUD_MENU, 1.0f);
425
426     demo_replay_stop(i == DEMO_DEL);
427     return goto_state(&st_demo);
428 }
429
430 static int demo_del_enter(void)
431 {
432     int id, jd, kd;
433
434     if ((id = gui_vstack(0)))
435     {
436         kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
437
438         if ((jd = gui_harray(id)))
439         {
440             gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL,  0);
441             gui_start(jd, _("No"),  GUI_SML, DEMO_KEEP, 1);
442         }
443
444         gui_pulse(kd, 1.2f);
445         gui_layout(id, 0, 0);
446     }
447     audio_music_fade_out(2.0f);
448
449     return id;
450 }
451
452 static void demo_del_leave(int id)
453 {
454     gui_delete(id);
455 }
456
457 static void demo_del_paint(int id, float st)
458 {
459     game_draw(0, st);
460     gui_paint(id);
461 }
462
463 static void demo_del_timer(int id, float dt)
464 {
465     gui_timer(id, dt);
466     audio_timer(dt);
467 }
468
469 static void demo_del_point(int id, int x, int y, int dx, int dy)
470 {
471     gui_pulse(gui_point(id, x, y), 1.2f);
472 }
473
474 static void demo_del_stick(int id, int a, int v)
475 {
476     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
477         gui_pulse(gui_stick(id, v, 0), 1.2f);
478     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
479         gui_pulse(gui_stick(id, 0, v), 1.2f);
480 }
481
482 static int demo_del_click(int b, int d)
483 {
484     if (d && b < 0)
485         return demo_del_action(gui_token(gui_click()));
486     return 1;
487 }
488
489 static int demo_del_buttn(int b, int d)
490 {
491     if (d)
492     {
493         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
494             return demo_del_action(gui_token(gui_click()));
495         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
496             return demo_del_action(DEMO_KEEP);
497     }
498     return 1;
499 }
500
501 /*---------------------------------------------------------------------------*/
502
503 struct state st_demo = {
504     demo_enter,
505     demo_leave,
506     demo_paint,
507     demo_timer,
508     demo_point,
509     demo_stick,
510     demo_click,
511     NULL,
512     demo_buttn,
513     0
514 };
515
516 struct state st_demo_play = {
517     demo_play_enter,
518     demo_play_leave,
519     demo_play_paint,
520     demo_play_timer,
521     NULL,
522     NULL,
523     NULL,
524     NULL,
525     demo_play_buttn,
526     0
527 };
528
529 struct state st_demo_end = {
530     demo_end_enter,
531     demo_end_leave,
532     demo_end_paint,
533     demo_end_timer,
534     demo_end_point,
535     demo_end_stick,
536     demo_end_click,
537     NULL,
538     demo_end_buttn,
539     1, 0
540 };
541
542 struct state st_demo_del = {
543     demo_del_enter,
544     demo_del_leave,
545     demo_del_paint,
546     demo_del_timer,
547     demo_del_point,
548     demo_del_stick,
549     demo_del_click,
550     NULL,
551     demo_del_buttn,
552     1, 0
553 };