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