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