No Replays screen: removed Back button and added a time-out.
[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 "game.h"
21 #include "demo.h"
22 #include "levels.h"
23 #include "audio.h"
24 #include "solid.h"
25 #include "config.h"
26 #include "st_shared.h"
27 #include "util.h"
28
29 #include "st_demo.h"
30 #include "st_title.h"
31
32 /*---------------------------------------------------------------------------*/
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 GUI_BACK:
52         return goto_state(&st_title);
53
54     case GUI_NEXT:
55         first += DEMO_STEP;
56         return goto_state(&st_demo);
57         break;
58
59     case GUI_PREV:
60         first -= DEMO_STEP;
61         return goto_state(&st_demo);
62         break;
63
64     case GUI_NULL:
65         return 1;
66         break;
67
68     default:
69         if (level_replay(demo_get(i)->filename))
70             demo_play_goto(0);
71             return goto_state(&st_demo_play);
72     }
73     return 1;
74 }
75
76 /*---------------------------------------------------------------------------*/
77
78 static void demo_replay(int id, int i)
79 {
80     int w = config_get_d(CONFIG_WIDTH);
81     int h = config_get_d(CONFIG_HEIGHT);
82     int jd;
83
84     char nam[MAXNAM + 3];
85
86     if ((jd = gui_vstack(id)))
87     {
88         gui_space(jd);
89         gui_image(jd, demo_get(i)->shot, w / 6, h / 6);
90
91         nam[MAXNAM - 1] = '\0';
92         strncpy(nam, demo_get(i)->name, MAXNAM);
93         if (nam[MAXNAM - 1] != '\0')
94         {
95             nam[MAXNAM - 2] = '.';
96             nam[MAXNAM - 1] = '.';
97             nam[MAXNAM + 0] = '.';
98             nam[MAXNAM + 1] = '\0';
99         }
100         gui_state(jd, nam, GUI_SML, i, 0);
101
102         gui_active(jd, i, 0);
103     }
104 }
105
106 static int name_id;
107 static int time_id;
108 static int coin_id;
109 static int date_id;
110 static int mode_id;
111 static int status_id;
112 static int player_id;
113
114 /* Create a layout for some demo info.  If d is NULL, try to reserve enough
115  * space. */
116
117 static int gui_demo_status(int id, const struct demo *d)
118 {
119     char noname[MAXNAM];
120     const char *mode, *status;
121     int i, j, k;
122     int jd, kd, ld, md;
123
124     if (d == NULL)
125     {
126         /* Build a long name */
127         memset(noname, 'M', MAXNAM - 1);
128         noname[MAXNAM - 1] = '\0';
129
130         /* Get a long mode */
131         mode = mode_to_str(0, 0);
132         j = strlen(mode);
133
134         for (i = 1; i <= MODE_COUNT; i++)
135         {
136             k = strlen(mode_to_str(i, 0));
137
138             if (k > j)
139             {
140                 j = k;
141                 mode = mode_to_str(i, 0);
142             }
143         }
144
145         /* Get a long status */
146         status = status_to_str(0);
147         j = strlen(status);
148         for (i = 1; i <= GAME_FALL; i++)
149         {
150             k = strlen(status_to_str(i));
151             if (k > j)
152             {
153                 j = k;
154                 status = status_to_str(i);
155             }
156         }
157     }
158     else
159     {
160         mode = mode_to_str(d->mode, 0);
161         status = status_to_str(d->status);
162     }
163
164     if ((jd = gui_hstack(id)))
165     {
166         if ((kd = gui_vstack(jd)))
167         {
168             if ((ld = gui_harray(kd)))
169             {
170                 if ((md = gui_vstack(ld)))
171                 {
172                     player_id = gui_label(md, (d ? d->player : noname),
173                                           GUI_SML, GUI_RGT, 0, 0);
174                     coin_id = gui_count(md, (d ? d->coins : 100),
175                                         GUI_SML, GUI_RGT);
176                     status_id = gui_label(md, status, GUI_SML, GUI_RGT,
177                                          gui_red, gui_red);
178                 }
179                 if ((md = gui_vstack(ld)))
180                 {
181                     gui_label(md, _("Player"), GUI_SML, GUI_LFT,
182                               gui_wht, gui_wht);
183                     gui_label(md, _("Coins"), GUI_SML, GUI_LFT,
184                               gui_wht, gui_wht);
185                     gui_label(md, _("State"), GUI_SML, GUI_LFT,
186                               gui_wht, gui_wht);
187                 }
188                 if ((md = gui_vstack(ld)))
189                 {
190                     name_id = gui_label(md, (d ? d->name : noname),
191                                         GUI_SML, GUI_RGT, 0, 0);
192                     time_id = gui_clock(md, (d ? d->timer : 35000),
193                                         GUI_SML, GUI_RGT);
194                     mode_id = gui_label(md, mode, GUI_SML, GUI_RGT, 0, 0);
195                 }
196             }
197             date_id = gui_label(kd, (d ? date_to_str(d->date) : "M"),
198                                 GUI_SML, GUI_RGT, 0, 0);
199         }
200         if ((kd = gui_vstack(jd)))
201         {
202             gui_label(kd, _("Replay"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
203             gui_label(kd, _("Time"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
204             gui_label(kd, _("Mode"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
205             gui_label(kd, _("Date"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
206         }
207         if (d && d->status == GAME_GOAL)
208             gui_set_color(status_id, gui_grn, gui_grn);
209     }
210     return jd;
211 }
212
213 static void gui_demo_update_status(int i)
214 {
215     const struct demo *d = demo_get(i);
216
217     gui_set_label(name_id,   d->name);
218     gui_set_label(date_id,   date_to_str(d->date));
219     gui_set_label(player_id, d->player);
220     gui_set_label(mode_id,   mode_to_str(d->mode, 0));
221
222     if (d->status == GAME_GOAL)
223         gui_set_color(status_id, gui_grn, gui_grn);
224     else
225         gui_set_color(status_id, gui_red, gui_red);
226
227     gui_set_label(status_id, status_to_str(d->status));
228     gui_set_count(coin_id, d->coins);
229     gui_set_clock(time_id, d->timer);
230 }
231
232 /*---------------------------------------------------------------------------*/
233
234 static int demo_enter(void)
235 {
236     int i, j;
237     int id, jd, kd;
238
239     id = gui_vstack(0);
240
241     if ((total = demo_scan()))
242     {
243         if ((jd = gui_hstack(id)))
244         {
245
246             gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
247             gui_filler(jd);
248             gui_back_prev_next(jd, first > 0, first + DEMO_STEP < total);
249         }
250
251         if ((jd = gui_varray(id)))
252             for (i = first; i < first + DEMO_STEP ; i += DEMO_LINE)
253                 if ((kd = gui_harray(jd)))
254                 {
255                     for (j = i + DEMO_LINE - 1; j >= i; j--)
256                         if (j < total)
257                             demo_replay(kd, j);
258                         else
259                             gui_space(kd);
260                 }
261         gui_filler(id);
262         gui_demo_status(id, NULL);
263         gui_layout(id, 0, 0);
264         gui_demo_update_status(0);
265     }
266     else
267     {
268         gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0, 0);
269         gui_layout(id, 0, 0);
270     }
271
272     audio_music_fade_to(0.5f, "bgm/inter.ogg");
273
274     return id;
275 }
276
277 static void demo_timer(int id, float dt)
278 {
279     if (total == 0 && time_state() > 4.0f)
280         goto_state(&st_title);
281
282     gui_timer(id, dt);
283     audio_timer(dt);
284 }
285
286 static void demo_point(int id, int x, int y, int dx, int dy)
287 {
288     int jd = shared_point_basic(id, x, y);
289     int i  = gui_token(jd);
290
291     if (jd && i >= 0)
292         gui_demo_update_status(i);
293 }
294
295 static void demo_stick(int id, int a, int v)
296 {
297     int jd = shared_stick_basic(id, a, v);
298     int i  = gui_token(jd);
299
300     if (jd && i >= 0)
301         gui_demo_update_status(i);
302 }
303
304 static int demo_buttn(int b, int d)
305 {
306     if (d)
307     {
308         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
309             return demo_action(total ? gui_token(gui_click()) : GUI_BACK);
310         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
311             return demo_action(GUI_BACK);
312     }
313     return 1;
314 }
315
316 /*---------------------------------------------------------------------------*/
317
318 /* Play demo from command line. */
319 static int simple_play;
320
321 static int demo_paused;
322
323 void demo_play_goto(int simple)
324 {
325     simple_play = simple;
326 }
327
328 static int demo_play_enter(void)
329 {
330     int id;
331
332     if (demo_paused)
333     {
334         demo_paused = 0;
335         audio_music_fade_in(0.5f);
336         return 0;
337     }
338
339     if ((id = gui_vstack(0)))
340     {
341         gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
342         gui_layout(id, 0, 0);
343         gui_pulse(id, 1.2f);
344     }
345
346     global_time = -1.f;
347     replay_time =  0.f;
348
349     hud_update(0);
350
351     game_set_fly(0.f);
352
353     return id;
354 }
355
356 static void demo_play_paint(int id, float st)
357 {
358     game_draw(0, st);
359     hud_paint();
360
361     if (time_state() < 1.f)
362         gui_paint(id);
363 }
364
365 static void demo_play_timer(int id, float dt)
366 {
367     float t;
368
369     game_step_fade(dt);
370     gui_timer(id, dt);
371     audio_timer(dt);
372
373     global_time += dt;
374     hud_timer(dt);
375
376     /* Spin or skip depending on how fast the demo wants to run. */
377
378     while (replay_time < global_time)
379         if (demo_replay_step(&t))
380         {
381             replay_time += t;
382         }
383         else
384         {
385             demo_paused = 0;
386             goto_state(&st_demo_end);
387             break;
388         }
389 }
390
391 static int demo_play_keybd(int c, int d)
392 {
393     if (d)
394     {
395         if (config_tst_d(CONFIG_KEY_PAUSE, c))
396         {
397             demo_paused = 1;
398             return goto_state(&st_demo_end);
399         }
400     }
401     return 1;
402 }
403
404 static int demo_play_buttn(int b, int d)
405 {
406     if (d)
407     {
408         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
409         {
410             if (! (SDL_GetModState() & (KMOD_SHIFT | KMOD_CTRL | KMOD_ALT | KMOD_META)))
411                 demo_paused = 1;
412             return goto_state(&st_demo_end);
413         }
414     }
415     return 1;
416 }
417
418 /*---------------------------------------------------------------------------*/
419
420 #define DEMO_KEEP    0
421 #define DEMO_DEL     1
422 #define DEMO_QUIT    2
423 #define DEMO_REPLAY  3
424 #define DEMO_CONTINUE  4
425
426 static int demo_end_action(int i)
427 {
428     audio_play(AUD_MENU, 1.0f);
429
430     switch (i)
431     {
432     case DEMO_DEL:
433         return goto_state(&st_demo_del);
434     case DEMO_KEEP:
435         demo_replay_stop(0);
436         return goto_state(&st_demo);
437     case DEMO_QUIT:
438         demo_replay_stop(0);
439         return 0;
440     case DEMO_REPLAY:
441         demo_replay_stop(0);
442         level_replay(curr_demo_replay()->filename);
443         return goto_state(&st_demo_play);
444     case DEMO_CONTINUE:
445         return goto_state(&st_demo_play);
446     }
447     return 1;
448 }
449
450 static int demo_end_enter(void)
451 {
452     int id, jd, kd;
453
454     if ((id = gui_vstack(0)))
455     {
456         if (demo_paused)
457             kd = gui_label(id, _("Replay Paused"), GUI_LRG, GUI_ALL,
458                            gui_gry, gui_red);
459         else
460             kd = gui_label(id, _("Replay Ends"), GUI_LRG, GUI_ALL,
461                            gui_gry, gui_red);
462
463         if ((jd = gui_harray(id)))
464         {
465             if (demo_paused)
466                 gui_start(jd, _("Continue"), GUI_SML, DEMO_CONTINUE, 0);
467             else
468                 gui_start(jd, _("Replay Again"), GUI_SML, DEMO_REPLAY, 0);
469
470             if (simple_play)
471                 gui_start(jd, _("OK"),       GUI_SML, DEMO_QUIT,   1);
472             else
473             {
474                 gui_start(jd, _("Keep"),     GUI_SML, DEMO_KEEP,   1);
475                 gui_state(jd, _("Delete"),   GUI_SML, DEMO_DEL,    0);
476             }
477         }
478
479         gui_filler(id);
480
481         if ((jd = gui_hstack(id)))
482         {
483             gui_filler(jd);
484             gui_demo_status(jd, curr_demo_replay());
485             gui_filler(jd);
486         }
487
488         gui_pulse(kd, 1.2f);
489         gui_layout(id, 0, 0);
490     }
491
492     audio_music_fade_out(demo_paused ? 0.2f : 2.0f);
493
494     return id;
495 }
496
497 static void demo_end_paint(int id, float st)
498 {
499     game_draw(0, st);
500     gui_paint(id);
501
502     if (demo_paused)
503         hud_paint();
504 }
505
506 static int demo_end_keybd(int c, int d)
507 {
508     if (d)
509     {
510         if (demo_paused && config_tst_d(CONFIG_KEY_PAUSE, c))
511             return demo_end_action(DEMO_CONTINUE);
512     }
513     return 1;
514 }
515
516 static int demo_end_buttn(int b, int d)
517 {
518     if (d)
519     {
520         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
521             return demo_end_action(gui_token(gui_click()));
522         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
523             return demo_end_action(demo_paused
524                                    ? DEMO_CONTINUE
525                                    : (simple_play ? DEMO_QUIT : DEMO_KEEP));
526     }
527     return 1;
528 }
529
530 /*---------------------------------------------------------------------------*/
531
532 static int demo_del_action(int i)
533 {
534     audio_play(AUD_MENU, 1.0f);
535
536     demo_replay_stop(i == DEMO_DEL);
537     return goto_state(&st_demo);
538 }
539
540 static int demo_del_enter(void)
541 {
542     int id, jd, kd;
543
544     if ((id = gui_vstack(0)))
545     {
546         kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
547
548         if ((jd = gui_harray(id)))
549         {
550             gui_start(jd, _("No"),  GUI_SML, DEMO_KEEP, 1);
551             gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL,  0);
552         }
553
554         gui_pulse(kd, 1.2f);
555         gui_layout(id, 0, 0);
556     }
557     audio_music_fade_out(2.0f);
558
559     return id;
560 }
561
562 static int demo_del_buttn(int b, int d)
563 {
564     if (d)
565     {
566         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
567             return demo_del_action(gui_token(gui_click()));
568         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
569             return demo_del_action(DEMO_KEEP);
570     }
571     return 1;
572 }
573
574 /*---------------------------------------------------------------------------*/
575
576 struct state st_demo = {
577     demo_enter,
578     shared_leave,
579     shared_paint,
580     demo_timer,
581     demo_point,
582     demo_stick,
583     shared_click,
584     NULL,
585     demo_buttn,
586     1, 0
587 };
588
589 struct state st_demo_play = {
590     demo_play_enter,
591     shared_leave,
592     demo_play_paint,
593     demo_play_timer,
594     NULL,
595     NULL,
596     NULL,
597     demo_play_keybd,
598     demo_play_buttn,
599     1, 0
600 };
601
602 struct state st_demo_end = {
603     demo_end_enter,
604     shared_leave,
605     demo_end_paint,
606     shared_timer,
607     shared_point,
608     shared_stick,
609     shared_click,
610     demo_end_keybd,
611     demo_end_buttn,
612     1, 0
613 };
614
615 struct state st_demo_del = {
616     demo_del_enter,
617     shared_leave,
618     shared_paint,
619     shared_timer,
620     shared_point,
621     shared_stick,
622     shared_click,
623     NULL,
624     demo_del_buttn,
625     1, 0
626 };