Clean up replay intro pause code
[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 <string.h>
16
17 #include "gui.h"
18 #include "hud.h"
19 #include "set.h"
20 #include "demo.h"
21 #include "progress.h"
22 #include "audio.h"
23 #include "solid.h"
24 #include "config.h"
25 #include "st_shared.h"
26 #include "util.h"
27 #include "common.h"
28 #include "demo_dir.h"
29
30 #include "game_common.h"
31 #include "game_server.h"
32 #include "game_client.h"
33
34 #include "st_demo.h"
35 #include "st_title.h"
36
37 /*---------------------------------------------------------------------------*/
38
39 #define DEMO_LINE 4
40 #define DEMO_STEP 8
41
42 static Array items;
43
44 static int first = 0;
45 static int total = 0;
46
47 static int last_viewed = 0;
48
49 /*---------------------------------------------------------------------------*/
50
51 static int demo_action(int i)
52 {
53     audio_play(AUD_MENU, 1.0f);
54
55     switch (i)
56     {
57     case GUI_BACK:
58         return goto_state(&st_title);
59
60     case GUI_NEXT:
61         first += DEMO_STEP;
62         return goto_state(&st_demo);
63         break;
64
65     case GUI_PREV:
66         first -= DEMO_STEP;
67         return goto_state(&st_demo);
68         break;
69
70     case GUI_NULL:
71         return 1;
72         break;
73
74     default:
75         if (progress_replay(DEMO_GET(items, i)->filename))
76         {
77             last_viewed = i;
78             demo_play_goto(0);
79             return goto_state(&st_demo_play);
80         }
81         break;
82     }
83     return 1;
84 }
85
86 /*---------------------------------------------------------------------------*/
87
88 static struct thumb
89 {
90     int item;
91     int shot;
92     int name;
93 } thumbs[DEMO_STEP];
94
95 static int gui_demo_thumbs(int id)
96 {
97     int w = config_get_d(CONFIG_WIDTH);
98     int h = config_get_d(CONFIG_HEIGHT);
99
100     int jd, kd, ld;
101     int i, j;
102
103     struct thumb *thumb;
104
105     if ((jd = gui_varray(id)))
106         for (i = first; i < first + DEMO_STEP; i += DEMO_LINE)
107             if ((kd = gui_harray(jd)))
108             {
109                 for (j = i + DEMO_LINE - 1; j >= i; j--)
110                 {
111                     thumb = &thumbs[j % DEMO_STEP];
112
113                     thumb->item = j;
114
115                     if (j < total)
116                     {
117                         if ((ld = gui_vstack(kd)))
118                         {
119                             gui_space(ld);
120
121                             thumb->shot = gui_image(ld, " ", w / 6, h / 6);
122                             thumb->name = gui_state(ld, " ", GUI_SML, j, 0);
123
124                             gui_set_trunc(thumb->name, TRUNC_TAIL);
125
126                             gui_active(ld, j, 0);
127                         }
128                     }
129                     else
130                     {
131                         gui_space(kd);
132
133                         thumb->shot = 0;
134                         thumb->name = 0;
135                     }
136                 }
137             }
138
139     return jd;
140 }
141
142 static void gui_demo_update_thumbs(void)
143 {
144     int i;
145
146     for (i = 0; i < ARRAYSIZE(thumbs) && thumbs[i].shot && thumbs[i].name; i++)
147     {
148         gui_set_image(thumbs[i].shot, DEMO_GET(items, thumbs[i].item)->shot);
149         gui_set_label(thumbs[i].name, DEMO_GET(items, thumbs[i].item)->name);
150     }
151 }
152
153 static int name_id;
154 static int time_id;
155 static int coin_id;
156 static int date_id;
157 static int status_id;
158 static int player_id;
159
160 static int gui_demo_status(int id)
161 {
162     const char *status;
163     int jd, kd, ld;
164     int s;
165
166     /* Find the longest status string. */
167
168     for (status = "", s = GAME_NONE; s < GAME_MAX; s++)
169         if (strlen(status_to_str(s)) > strlen(status))
170             status = status_to_str(s);
171
172     /* Build info bar with dummy values. */
173
174     if ((jd = gui_hstack(id)))
175     {
176         gui_filler(jd);
177
178         if ((kd = gui_hstack(jd)))
179         {
180             if ((ld = gui_vstack(kd)))
181             {
182                 gui_filler(ld);
183
184                 time_id   = gui_clock(ld, 35000,  GUI_SML, GUI_NE);
185                 coin_id   = gui_count(ld, 100,    GUI_SML, 0);
186                 status_id = gui_label(ld, status, GUI_SML, GUI_SE,
187                                       gui_red, gui_red);
188
189                 gui_filler(ld);
190             }
191
192             if ((ld = gui_vstack(kd)))
193             {
194                 gui_filler(ld);
195
196                 gui_label(ld, _("Time"),   GUI_SML, GUI_NW, gui_wht, gui_wht);
197                 gui_label(ld, _("Coins"),  GUI_SML, 0,      gui_wht, gui_wht);
198                 gui_label(ld, _("Status"), GUI_SML, GUI_SW, gui_wht, gui_wht);
199
200                 gui_filler(ld);
201             }
202         }
203
204         gui_space(jd);
205
206         if ((kd = gui_vstack(jd)))
207         {
208             gui_filler(kd);
209
210             name_id   = gui_label(kd, " ", GUI_SML, GUI_NE, 0, 0);
211             player_id = gui_label(kd, " ", GUI_SML, 0,      0, 0);
212             date_id   = gui_label(kd, date_to_str(time(NULL)),
213                                   GUI_SML, GUI_SE, 0, 0);
214
215             gui_filler(kd);
216
217             gui_set_trunc(name_id,   TRUNC_TAIL);
218             gui_set_trunc(player_id, TRUNC_TAIL);
219         }
220
221         if ((kd = gui_vstack(jd)))
222         {
223             gui_filler(kd);
224
225             gui_label(kd, _("Replay"), GUI_SML, GUI_NW, gui_wht, gui_wht);
226             gui_label(kd, _("Player"), GUI_SML, 0,      gui_wht, gui_wht);
227             gui_label(kd, _("Date"),   GUI_SML, GUI_SW, gui_wht, gui_wht);
228
229             gui_filler(kd);
230         }
231
232         gui_filler(jd);
233     }
234
235     return jd;
236 }
237
238 static void gui_demo_update_status(int i)
239 {
240     const struct demo *d;
241
242     if (total > 0)
243         d = DEMO_GET(items, i < total ? i : 0);
244     else
245         return;
246
247     gui_set_label(name_id,   d->name);
248     gui_set_label(date_id,   date_to_str(d->date));
249     gui_set_label(player_id, d->player);
250
251     if (d->status == GAME_GOAL)
252         gui_set_color(status_id, gui_grn, gui_grn);
253     else
254         gui_set_color(status_id, gui_red, gui_red);
255
256     gui_set_label(status_id, status_to_str(d->status));
257     gui_set_count(coin_id, d->coins);
258     gui_set_clock(time_id, d->timer);
259 }
260
261 /*---------------------------------------------------------------------------*/
262
263 static int demo_gui(void)
264 {
265     int id, jd;
266
267     id = gui_vstack(0);
268
269     if (total)
270     {
271         if ((jd = gui_hstack(id)))
272         {
273
274             gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
275             gui_filler(jd);
276             gui_navig(jd, first > 0, first + DEMO_STEP < total);
277         }
278
279         gui_demo_thumbs(id);
280         gui_filler(id);
281         gui_demo_status(id);
282
283         gui_layout(id, 0, 0);
284
285         gui_demo_update_thumbs();
286         gui_demo_update_status(last_viewed);
287     }
288     else
289     {
290         gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0, 0);
291         gui_layout(id, 0, 0);
292     }
293
294     return id;
295 }
296
297 static int demo_enter(struct state *st, struct state *prev)
298 {
299     if (items)
300         demo_dir_free(items);
301
302     items = demo_dir_scan();
303     total = array_len(items);
304
305     audio_music_fade_to(0.5f, "bgm/inter.ogg");
306
307     return demo_gui();
308 }
309
310 static void demo_timer(int id, float dt)
311 {
312     if (total == 0 && time_state() > 4.0f)
313         goto_state(&st_title);
314
315     gui_timer(id, dt);
316 }
317
318 static void demo_point(int id, int x, int y, int dx, int dy)
319 {
320     int jd = shared_point_basic(id, x, y);
321     int i  = gui_token(jd);
322
323     if (jd && i >= 0 && !GUI_ISMSK(i))
324         gui_demo_update_status(i);
325 }
326
327 static void demo_stick(int id, int a, int v)
328 {
329     int jd = shared_stick_basic(id, a, v);
330     int i  = gui_token(jd);
331
332     if (jd && i >= 0 && !GUI_ISMSK(i))
333         gui_demo_update_status(i);
334 }
335
336 static int demo_buttn(int b, int d)
337 {
338     if (d)
339     {
340         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
341             return demo_action(total ? gui_token(gui_click()) : GUI_BACK);
342         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
343             return demo_action(GUI_BACK);
344     }
345     return 1;
346 }
347
348 /*---------------------------------------------------------------------------*/
349
350 static int standalone;
351 static int demo_paused;
352 static int show_hud;
353 static int check_compat;
354
355 static float prelude;
356
357 void demo_play_goto(int s)
358 {
359     standalone   = s;
360     check_compat = 1;
361 }
362
363 static int demo_play_gui(void)
364 {
365     int id;
366
367     if ((id = gui_vstack(0)))
368     {
369         gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
370         gui_layout(id, 0, 0);
371         gui_pulse(id, 1.2f);
372     }
373
374     return id;
375 }
376
377 static int demo_play_enter(struct state *st, struct state *prev)
378 {
379     if (demo_paused)
380     {
381         demo_paused = 0;
382         prelude = 0;
383         audio_music_fade_in(0.5f);
384         return 0;
385     }
386
387     /*
388      * Post-1.5.1 replays include view data in the first update, this
389      * line is currently left in for compatibility with older replays.
390      */
391     game_client_fly(0.0f);
392
393     if (check_compat && !game_compat_map)
394     {
395         goto_state(&st_demo_compat);
396         return 0;
397     }
398
399     prelude = 1.0f;
400
401     show_hud = 1;
402     hud_update(0);
403
404     return demo_play_gui();
405 }
406
407 static void demo_play_paint(int id, float t)
408 {
409     game_client_draw(0, t);
410
411     if (show_hud)
412         hud_paint();
413
414     if (time_state() < prelude)
415         gui_paint(id);
416 }
417
418 static void demo_play_timer(int id, float dt)
419 {
420     game_step_fade(dt);
421     gui_timer(id, dt);
422     hud_timer(dt);
423
424     /* Pause briefly before starting playback. */
425
426     if (time_state() < prelude)
427         return;
428
429     if (!demo_replay_step(dt))
430     {
431         demo_paused = 0;
432         goto_state(&st_demo_end);
433     }
434     else
435         progress_step();
436 }
437
438 static int demo_play_keybd(int c, int d)
439 {
440     if (d)
441     {
442         if (config_tst_d(CONFIG_KEY_PAUSE, c))
443         {
444             demo_paused = 1;
445             return goto_state(&st_demo_end);
446         }
447
448         if (c == SDLK_F6)
449             show_hud = !show_hud;
450     }
451     return 1;
452 }
453
454 static int demo_play_buttn(int b, int d)
455 {
456     if (d)
457     {
458         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
459         {
460             if (config_tst_d(CONFIG_KEY_PAUSE, SDLK_ESCAPE))
461                 demo_paused = 1;
462
463             return goto_state(&st_demo_end);
464         }
465     }
466     return 1;
467 }
468
469 /*---------------------------------------------------------------------------*/
470
471 #define DEMO_KEEP      0
472 #define DEMO_DEL       1
473 #define DEMO_QUIT      2
474 #define DEMO_REPLAY    3
475 #define DEMO_CONTINUE  4
476
477 static int demo_end_action(int i)
478 {
479     audio_play(AUD_MENU, 1.0f);
480
481     switch (i)
482     {
483     case DEMO_DEL:
484         demo_paused = 0;
485         return goto_state(&st_demo_del);
486     case DEMO_KEEP:
487         demo_paused = 0;
488         demo_replay_stop(0);
489         return goto_state(&st_demo);
490     case DEMO_QUIT:
491         demo_replay_stop(0);
492         return 0;
493     case DEMO_REPLAY:
494         demo_replay_stop(0);
495         progress_replay(curr_demo_replay()->filename);
496         return goto_state(&st_demo_play);
497     case DEMO_CONTINUE:
498         return goto_state(&st_demo_play);
499     }
500     return 1;
501 }
502
503 static int demo_end_gui(void)
504 {
505     int id, jd, kd;
506
507     if ((id = gui_vstack(0)))
508     {
509         if (demo_paused)
510             kd = gui_label(id, _("Replay Paused"), GUI_LRG, GUI_ALL,
511                            gui_gry, gui_red);
512         else
513             kd = gui_label(id, _("Replay Ends"),   GUI_LRG, GUI_ALL,
514                            gui_gry, gui_red);
515
516         if ((jd = gui_harray(id)))
517         {
518             int start_id = 0;
519
520             if (standalone)
521             {
522                 start_id = gui_start(jd, _("Quit"), GUI_SML, DEMO_QUIT, 1);
523             }
524             else
525             {
526                 start_id = gui_start(jd, _("Keep"), GUI_SML, DEMO_KEEP, 1);
527                 gui_state(jd, _("Delete"), GUI_SML, DEMO_DEL, 0);
528             }
529
530             if (demo_paused)
531             {
532                 gui_start(jd, _("Continue"), GUI_SML, DEMO_CONTINUE, 1);
533                 gui_toggle(start_id);
534             }
535             else
536                 gui_state(jd, _("Repeat"),   GUI_SML, DEMO_REPLAY,   0);
537         }
538
539         gui_pulse(kd, 1.2f);
540         gui_layout(id, 0, 0);
541     }
542
543     return id;
544 }
545
546 static int demo_end_enter(struct state *st, struct state *prev)
547 {
548     audio_music_fade_out(demo_paused ? 0.2f : 2.0f);
549
550     return demo_end_gui();
551 }
552
553 static void demo_end_paint(int id, float t)
554 {
555     game_client_draw(0, t);
556     gui_paint(id);
557
558     if (demo_paused)
559         hud_paint();
560 }
561
562 static int demo_end_keybd(int c, int d)
563 {
564     if (d)
565     {
566         if (demo_paused && config_tst_d(CONFIG_KEY_PAUSE, c))
567             return demo_end_action(DEMO_CONTINUE);
568     }
569     return 1;
570 }
571
572 static int demo_end_buttn(int b, int d)
573 {
574     if (d)
575     {
576         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
577             return demo_end_action(gui_token(gui_click()));
578
579         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
580         {
581             if (demo_paused)
582                 return demo_end_action(DEMO_CONTINUE);
583             else
584                 return demo_end_action(standalone ? DEMO_QUIT : DEMO_KEEP);
585         }
586     }
587     return 1;
588 }
589
590 /*---------------------------------------------------------------------------*/
591
592 static int demo_del_action(int i)
593 {
594     audio_play(AUD_MENU, 1.0f);
595
596     demo_replay_stop(i == DEMO_DEL);
597     return goto_state(&st_demo);
598 }
599
600 static int demo_del_gui(void)
601 {
602     int id, jd, kd;
603
604     if ((id = gui_vstack(0)))
605     {
606         kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
607
608         if ((jd = gui_harray(id)))
609         {
610             gui_start(jd, _("No"),  GUI_SML, DEMO_KEEP, 1);
611             gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL,  0);
612         }
613
614         gui_pulse(kd, 1.2f);
615         gui_layout(id, 0, 0);
616     }
617
618     return id;
619 }
620
621 static int demo_del_enter(struct state *st, struct state *prev)
622 {
623     audio_music_fade_out(2.0f);
624
625     return demo_del_gui();
626 }
627
628 static int demo_del_buttn(int b, int d)
629 {
630     if (d)
631     {
632         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
633             return demo_del_action(gui_token(gui_click()));
634         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
635             return demo_del_action(DEMO_KEEP);
636     }
637     return 1;
638 }
639
640 /*---------------------------------------------------------------------------*/
641
642 static int demo_compat_gui(void)
643 {
644     int id;
645
646     if ((id = gui_vstack(0)))
647     {
648         gui_label(id, _("Warning!"), GUI_MED, GUI_ALL, 0, 0);
649         gui_space(id);
650         gui_multi(id, _("The current replay was recorded with a\\"
651                         "different (or unknown) version of this level.\\"
652                         "Be prepared to encounter visual errors.\\"),
653                   GUI_SML, GUI_ALL, gui_wht, gui_wht);
654
655         gui_layout(id, 0, 0);
656     }
657
658     return id;
659 }
660
661 static int demo_compat_enter(struct state *st, struct state *prev)
662 {
663     check_compat = 0;
664
665     return demo_compat_gui();
666 }
667
668 static void demo_compat_timer(int id, float dt)
669 {
670     game_step_fade(dt);
671     gui_timer(id, dt);
672 }
673
674 static int demo_compat_buttn(int b, int d)
675 {
676     if (d)
677     {
678         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
679             return goto_state(&st_demo_play);
680         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
681             return goto_state(&st_demo_end);
682     }
683     return 1;
684 }
685
686 /*---------------------------------------------------------------------------*/
687
688 struct state st_demo = {
689     demo_enter,
690     shared_leave,
691     shared_paint,
692     demo_timer,
693     demo_point,
694     demo_stick,
695     shared_angle,
696     shared_click,
697     NULL,
698     demo_buttn,
699     1, 0
700 };
701
702 struct state st_demo_play = {
703     demo_play_enter,
704     shared_leave,
705     demo_play_paint,
706     demo_play_timer,
707     NULL,
708     NULL,
709     NULL,
710     NULL,
711     demo_play_keybd,
712     demo_play_buttn,
713     1, 0
714 };
715
716 struct state st_demo_end = {
717     demo_end_enter,
718     shared_leave,
719     demo_end_paint,
720     shared_timer,
721     shared_point,
722     shared_stick,
723     shared_angle,
724     shared_click,
725     demo_end_keybd,
726     demo_end_buttn,
727     1, 0
728 };
729
730 struct state st_demo_del = {
731     demo_del_enter,
732     shared_leave,
733     shared_paint,
734     shared_timer,
735     shared_point,
736     shared_stick,
737     shared_angle,
738     shared_click,
739     NULL,
740     demo_del_buttn,
741     1, 0
742 };
743
744 struct state st_demo_compat = {
745     demo_compat_enter,
746     shared_leave,
747     shared_paint,
748     demo_compat_timer,
749     shared_point,
750     shared_stick,
751     shared_angle,
752     shared_click,
753     NULL,
754     demo_compat_buttn,
755     1, 0
756 };