ball/set: ignore files not ending with ".txt"
[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_enter(void)
264 {
265     int id, jd;
266
267     if (items)
268         demo_dir_free(items);
269
270     items = demo_dir_scan();
271     total = array_len(items);
272
273     id = gui_vstack(0);
274
275     if (total)
276     {
277         if ((jd = gui_hstack(id)))
278         {
279
280             gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
281             gui_filler(jd);
282             gui_navig(jd, first > 0, first + DEMO_STEP < total);
283         }
284
285         gui_demo_thumbs(id);
286         gui_filler(id);
287         gui_demo_status(id);
288
289         gui_layout(id, 0, 0);
290
291         gui_demo_update_thumbs();
292         gui_demo_update_status(last_viewed);
293     }
294     else
295     {
296         gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0, 0);
297         gui_layout(id, 0, 0);
298     }
299
300     audio_music_fade_to(0.5f, "bgm/inter.ogg");
301
302     return id;
303 }
304
305 static void demo_timer(int id, float dt)
306 {
307     if (total == 0 && time_state() > 4.0f)
308         goto_state(&st_title);
309
310     gui_timer(id, dt);
311 }
312
313 static void demo_point(int id, int x, int y, int dx, int dy)
314 {
315     int jd = shared_point_basic(id, x, y);
316     int i  = gui_token(jd);
317
318     if (jd && i >= 0 && !GUI_ISMSK(i))
319         gui_demo_update_status(i);
320 }
321
322 static void demo_stick(int id, int a, int v)
323 {
324     int jd = shared_stick_basic(id, a, v);
325     int i  = gui_token(jd);
326
327     if (jd && i >= 0 && !GUI_ISMSK(i))
328         gui_demo_update_status(i);
329 }
330
331 static int demo_buttn(int b, int d)
332 {
333     if (d)
334     {
335         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
336             return demo_action(total ? gui_token(gui_click()) : GUI_BACK);
337         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
338             return demo_action(GUI_BACK);
339     }
340     return 1;
341 }
342
343 /*---------------------------------------------------------------------------*/
344
345 static int standalone;
346 static int demo_paused;
347 static int show_hud;
348 static int check_compat;
349
350 void demo_play_goto(int s)
351 {
352     standalone   = s;
353     check_compat = 1;
354 }
355
356 static int demo_play_enter(void)
357 {
358     int id;
359
360     if (demo_paused)
361     {
362         demo_paused = 0;
363         audio_music_fade_in(0.5f);
364         return 0;
365     }
366
367     /*
368      * Post-1.5.1 replays include view data in the first update, these
369      * two lines are currently left in for compatibility with older
370      * replays.
371      */
372
373     game_set_fly(0.f, game_client_file());
374     game_client_step(NULL);
375
376     if (check_compat && !game_compat_map)
377     {
378         goto_state(&st_demo_compat);
379         return 0;
380     }
381
382     if ((id = gui_vstack(0)))
383     {
384         gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
385         gui_layout(id, 0, 0);
386         gui_pulse(id, 1.2f);
387     }
388
389     show_hud = 1;
390     hud_update(0);
391
392     return id;
393 }
394
395 static void demo_play_paint(int id, float t)
396 {
397     game_draw(0, t);
398
399     if (show_hud)
400         hud_paint();
401
402     if (time_state() < 1.f)
403         gui_paint(id);
404 }
405
406 static void demo_play_timer(int id, float dt)
407 {
408     game_step_fade(dt);
409     gui_timer(id, dt);
410     hud_timer(dt);
411
412     /*
413      * Introduce a one-second pause at the start of replay playback.  (One
414      * second is the time during which the "Replay" label is being displayed.)
415      * HACK ALERT!  "id == 0" means we got here from the pause screen, so no
416      * label has been created and there's no need to wait.
417      */
418
419     if (id != 0 && time_state() < 1.0f)
420         return;
421
422     /* Spin or skip depending on how fast the demo wants to run. */
423
424     if (!demo_replay_step(dt))
425     {
426         demo_paused = 0;
427         goto_state(&st_demo_end);
428     }
429     else
430         progress_step();
431 }
432
433 static int demo_play_keybd(int c, int d)
434 {
435     if (d)
436     {
437         if (config_tst_d(CONFIG_KEY_PAUSE, c))
438         {
439             demo_paused = 1;
440             return goto_state(&st_demo_end);
441         }
442
443         if (c == SDLK_F6)
444             show_hud = !show_hud;
445     }
446     return 1;
447 }
448
449 static int demo_play_buttn(int b, int d)
450 {
451     if (d)
452     {
453         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
454         {
455             if (config_tst_d(CONFIG_KEY_PAUSE, SDLK_ESCAPE))
456                 demo_paused = 1;
457
458             return goto_state(&st_demo_end);
459         }
460     }
461     return 1;
462 }
463
464 /*---------------------------------------------------------------------------*/
465
466 #define DEMO_KEEP      0
467 #define DEMO_DEL       1
468 #define DEMO_QUIT      2
469 #define DEMO_REPLAY    3
470 #define DEMO_CONTINUE  4
471
472 static int demo_end_action(int i)
473 {
474     audio_play(AUD_MENU, 1.0f);
475
476     switch (i)
477     {
478     case DEMO_DEL:
479         demo_paused = 0;
480         return goto_state(&st_demo_del);
481     case DEMO_KEEP:
482         demo_paused = 0;
483         demo_replay_stop(0);
484         return goto_state(&st_demo);
485     case DEMO_QUIT:
486         demo_replay_stop(0);
487         return 0;
488     case DEMO_REPLAY:
489         demo_replay_stop(0);
490         progress_replay(curr_demo_replay()->filename);
491         return goto_state(&st_demo_play);
492     case DEMO_CONTINUE:
493         return goto_state(&st_demo_play);
494     }
495     return 1;
496 }
497
498 static int demo_end_enter(void)
499 {
500     int id, jd, kd;
501
502     if ((id = gui_vstack(0)))
503     {
504         if (demo_paused)
505             kd = gui_label(id, _("Replay Paused"), GUI_LRG, GUI_ALL,
506                            gui_gry, gui_red);
507         else
508             kd = gui_label(id, _("Replay Ends"),   GUI_LRG, GUI_ALL,
509                            gui_gry, gui_red);
510
511         if ((jd = gui_harray(id)))
512         {
513             int start_id = 0;
514
515             if (standalone)
516             {
517                 start_id = gui_start(jd, _("Quit"), GUI_SML, DEMO_QUIT, 1);
518             }
519             else
520             {
521                 start_id = gui_start(jd, _("Keep"), GUI_SML, DEMO_KEEP, 1);
522                 gui_state(jd, _("Delete"), GUI_SML, DEMO_DEL, 0);
523             }
524
525             if (demo_paused)
526             {
527                 gui_start(jd, _("Continue"), GUI_SML, DEMO_CONTINUE, 1);
528                 gui_toggle(start_id);
529             }
530             else
531                 gui_state(jd, _("Repeat"),   GUI_SML, DEMO_REPLAY,   0);
532         }
533
534         gui_pulse(kd, 1.2f);
535         gui_layout(id, 0, 0);
536     }
537
538     audio_music_fade_out(demo_paused ? 0.2f : 2.0f);
539
540     return id;
541 }
542
543 static void demo_end_paint(int id, float t)
544 {
545     game_draw(0, t);
546     gui_paint(id);
547
548     if (demo_paused)
549         hud_paint();
550 }
551
552 static int demo_end_keybd(int c, int d)
553 {
554     if (d)
555     {
556         if (demo_paused && config_tst_d(CONFIG_KEY_PAUSE, c))
557             return demo_end_action(DEMO_CONTINUE);
558     }
559     return 1;
560 }
561
562 static int demo_end_buttn(int b, int d)
563 {
564     if (d)
565     {
566         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
567             return demo_end_action(gui_token(gui_click()));
568
569         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
570         {
571             if (demo_paused)
572                 return demo_end_action(DEMO_CONTINUE);
573             else
574                 return demo_end_action(standalone ? DEMO_QUIT : DEMO_KEEP);
575         }
576     }
577     return 1;
578 }
579
580 /*---------------------------------------------------------------------------*/
581
582 static int demo_del_action(int i)
583 {
584     audio_play(AUD_MENU, 1.0f);
585
586     demo_replay_stop(i == DEMO_DEL);
587     return goto_state(&st_demo);
588 }
589
590 static int demo_del_enter(void)
591 {
592     int id, jd, kd;
593
594     if ((id = gui_vstack(0)))
595     {
596         kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
597
598         if ((jd = gui_harray(id)))
599         {
600             gui_start(jd, _("No"),  GUI_SML, DEMO_KEEP, 1);
601             gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL,  0);
602         }
603
604         gui_pulse(kd, 1.2f);
605         gui_layout(id, 0, 0);
606     }
607     audio_music_fade_out(2.0f);
608
609     return id;
610 }
611
612 static int demo_del_buttn(int b, int d)
613 {
614     if (d)
615     {
616         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
617             return demo_del_action(gui_token(gui_click()));
618         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
619             return demo_del_action(DEMO_KEEP);
620     }
621     return 1;
622 }
623
624 /*---------------------------------------------------------------------------*/
625
626 static int demo_compat_enter(void)
627 {
628     int id;
629
630     check_compat = 0;
631
632     if ((id = gui_vstack(0)))
633     {
634         gui_label(id, _("Warning!"), GUI_MED, GUI_ALL, 0, 0);
635         gui_space(id);
636         gui_multi(id, _("The current replay was recorded with a\\"
637                         "different (or unknown) version of this level.\\"
638                         "Be prepared to encounter visual errors.\\"),
639                   GUI_SML, GUI_ALL, gui_wht, gui_wht);
640
641         gui_layout(id, 0, 0);
642     }
643
644     return id;
645 }
646
647 static void demo_compat_timer(int id, float dt)
648 {
649     game_step_fade(dt);
650     gui_timer(id, dt);
651 }
652
653 static int demo_compat_buttn(int b, int d)
654 {
655     if (d)
656     {
657         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
658             return goto_state(&st_demo_play);
659         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
660             return goto_state(&st_demo_end);
661     }
662     return 1;
663 }
664
665 /*---------------------------------------------------------------------------*/
666
667 struct state st_demo = {
668     demo_enter,
669     shared_leave,
670     shared_paint,
671     demo_timer,
672     demo_point,
673     demo_stick,
674     shared_angle,
675     shared_click,
676     NULL,
677     demo_buttn,
678     1, 0
679 };
680
681 struct state st_demo_play = {
682     demo_play_enter,
683     shared_leave,
684     demo_play_paint,
685     demo_play_timer,
686     NULL,
687     NULL,
688     NULL,
689     NULL,
690     demo_play_keybd,
691     demo_play_buttn,
692     1, 0
693 };
694
695 struct state st_demo_end = {
696     demo_end_enter,
697     shared_leave,
698     demo_end_paint,
699     shared_timer,
700     shared_point,
701     shared_stick,
702     shared_angle,
703     shared_click,
704     demo_end_keybd,
705     demo_end_buttn,
706     1, 0
707 };
708
709 struct state st_demo_del = {
710     demo_del_enter,
711     shared_leave,
712     shared_paint,
713     shared_timer,
714     shared_point,
715     shared_stick,
716     shared_angle,
717     shared_click,
718     NULL,
719     demo_del_buttn,
720     1, 0
721 };
722
723 struct state st_demo_compat = {
724     demo_compat_enter,
725     shared_leave,
726     shared_paint,
727     demo_compat_timer,
728     shared_point,
729     shared_stick,
730     shared_angle,
731     shared_click,
732     NULL,
733     demo_compat_buttn,
734     1, 0
735 };