set shots from the first level, not the second
[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 =
166                         gui_label(md, (d ? d->player : noname), GUI_SML,
167                                   GUI_RGT, 0, 0);
168                     coin_id =
169                         gui_count(md, (d ? d->coins : 100), GUI_SML, GUI_RGT);
170                     state_id =
171                         gui_label(md, state, GUI_SML, GUI_RGT, gui_red,
172                                   gui_red);
173                 }
174                 if ((md = gui_vstack(ld)))
175                 {
176                     gui_label(md, _("Player"), GUI_SML, GUI_LFT, gui_wht,
177                               gui_wht);
178                     gui_label(md, _("Coins"), GUI_SML, GUI_LFT, gui_wht,
179                               gui_wht);
180                     gui_label(md, _("State"), GUI_SML, GUI_LFT, gui_wht,
181                               gui_wht);
182                 }
183                 if ((md = gui_vstack(ld)))
184                 {
185                     name_id =
186                         gui_label(md, (d ? d->name : noname), GUI_SML, GUI_RGT,
187                                   0, 0);
188                     time_id =
189                         gui_clock(md, (d ? d->timer : 35000), GUI_SML, GUI_RGT);
190                     mode_id = gui_label(md, mode, GUI_SML, GUI_RGT, 0, 0);
191                 }
192             }
193             level_id =
194                 gui_label(kd, (d ? d->file : "M"), GUI_SML, GUI_RGT, gui_wht,
195                           gui_wht);
196             date_id =
197                 gui_label(kd, (d ? date_to_str(d->date) : "M"), GUI_SML,
198                           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, _("Level"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
206             gui_label(kd, _("Date"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
207         }
208         if (d && (d->state == GAME_GOAL || d->state == GAME_SPEC))
209             gui_set_color(state_id, gui_grn, gui_grn);
210     }
211     return jd;
212 }
213
214 static void gui_demo_update_status(int i)
215 {
216     const struct demo *d = get_demo(i);
217
218     gui_set_label(name_id,   d->name);
219     gui_set_label(date_id,   date_to_str(d->date));
220     gui_set_label(level_id,  d->file);
221     gui_set_label(player_id, d->player);
222     gui_set_label(mode_id,   mode_to_str(d->mode));
223     
224     if (d->state == GAME_GOAL || d->state == GAME_SPEC)
225         gui_set_color(state_id, gui_grn, gui_grn);
226     else
227         gui_set_color(state_id, gui_red, gui_red);
228     
229     gui_set_label(state_id, state_to_str(d->state));
230     gui_set_count(coin_id, d->coins);
231     gui_set_clock(time_id, d->timer);
232 }
233
234 static int demo_enter(void)
235 {
236     int i, j;
237     int id, jd, kd, ld;
238
239     total = demo_scan();
240
241     id = gui_vstack(0);
242     if (total == 0)
243     {
244         gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0,0);
245         gui_filler(id);
246         gui_multi(id, _("You can save replays of your games.\\"
247                         "Currently, there are no replays saved."),
248                   GUI_SML, GUI_ALL, gui_wht, gui_wht);
249         gui_filler(id);
250         gui_start(id, _("Back"), GUI_SML, GUI_BACK, 0);
251         gui_layout(id, 0, 0);
252     }
253     else
254     {
255         if ((jd = gui_hstack(id)))
256         {
257
258             ld = gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
259             gui_filler(jd);
260             gui_back_prev_next(jd, first > 0, first + DEMO_STEP < total);
261         }
262         if ((jd = gui_varray(id)))
263             for (i = first; i < first + DEMO_STEP ; i += DEMO_LINE)
264                 if ((kd = gui_harray(jd)))
265                 {
266                     for (j = i + DEMO_LINE - 1; j >= i; j--)
267                         if (j < total)
268                             demo_replay(kd, j);
269                         else
270                             gui_space(kd);
271                 }
272         gui_filler(id);
273         gui_demo_status(id, NULL);
274         gui_layout(id, 0, 0);
275         gui_demo_update_status(0);
276     }
277
278     audio_music_fade_to(0.5f, "bgm/inter.ogg");
279
280     return id;
281 }
282
283 static void demo_point(int id, int x, int y, int dx, int dy)
284 {
285     int jd = shared_point_basic(id, x, y);
286     int i  = gui_token(jd);
287     if (jd && i>=0)
288         gui_demo_update_status(i);
289 }
290
291 static void demo_stick(int id, int a, int v)
292 {
293     int jd = shared_stick_basic(id, a, v);
294     int i  = gui_token(jd);
295     if (jd && i>=0)
296         gui_demo_update_status(i);
297 }
298
299 static int demo_buttn(int b, int d)
300 {
301     if (d)
302     {
303         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
304             return demo_action(gui_token(gui_click()));
305         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
306             return demo_action(GUI_BACK);
307     }
308     return 1;
309 }
310
311 /*---------------------------------------------------------------------------*/
312
313 static int simple_play;
314 int goto_demo_play(int simple)
315 {
316     simple_play = simple;
317     return goto_state(&st_demo_play);
318 }
319
320 static int demo_play_enter(void)
321 {
322     int id;
323
324     if ((id = gui_vstack(0)))
325     {
326         gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
327         gui_layout(id, 0, 0);
328         gui_pulse(id, 1.2f);
329     }
330
331     global_time = -1.f;
332     replay_time =  0.f;
333
334     hud_update(0);
335
336     game_set_fly(0.f);
337
338     return id;
339 }
340
341 static void demo_play_paint(int id, float st)
342 {
343     game_draw(0, st);
344     hud_paint();
345
346     if (time_state() < 1.f)
347         gui_paint(id);
348 }
349
350 static void demo_play_timer(int id, float dt)
351 {
352     float t;
353
354     game_step_fade(dt);
355     gui_timer(id, dt);
356     audio_timer(dt);
357
358     global_time += dt;
359     hud_timer(dt);
360
361     /* Spin or skip depending on how fast the demo wants to run. */
362
363     while (replay_time < global_time)
364         if (demo_replay_step(&t))
365         {
366             replay_time += t;
367         }
368         else 
369         {
370             goto_state(&st_demo_end);
371             break;
372         }
373 }
374
375 static int demo_play_buttn(int b, int d)
376 {
377     if (d)
378     {
379         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
380             return goto_state(&st_demo_end);
381     }
382     return 1;
383 }
384
385 /*---------------------------------------------------------------------------*/
386
387 #define DEMO_KEEP    0
388 #define DEMO_DEL     1
389 #define DEMO_QUIT    2
390 #define DEMO_REPLAY  3
391
392 static int demo_end_action(int i)
393 {
394     audio_play(AUD_MENU, 1.0f);
395
396     switch (i)
397     {
398     case DEMO_DEL: 
399         return goto_state(&st_demo_del);
400     case DEMO_KEEP:
401         demo_replay_stop(0);
402         return goto_state(&st_demo);
403     case DEMO_QUIT:
404         demo_replay_stop(0);
405         return 0;
406     case DEMO_REPLAY:
407         demo_replay_stop(0);
408         level_replay(curr_demo_replay()->filename);
409         return goto_state(&st_demo_play);
410     }
411     return 1;
412 }
413
414 static int demo_end_enter(void)
415 {
416     int id, jd, kd;
417
418     if ((id = gui_vstack(0)))
419     {
420         kd = gui_label(id, _("Replay Ends"), GUI_LRG, GUI_ALL, gui_gry, gui_red);
421
422         if ((jd = gui_harray(id)))
423         {
424             gui_start(jd, _("Replay Again"), GUI_SML, DEMO_REPLAY, 0);
425             if (simple_play)
426                 gui_start(jd, _("OK"),       GUI_SML, DEMO_QUIT,   1);
427             else
428             {
429                 gui_start(jd, _("Keep"),     GUI_SML, DEMO_KEEP,   1);
430                 gui_state(jd, _("Delete"),   GUI_SML, DEMO_DEL,    0);
431             }
432         }
433
434         gui_filler(id);
435
436         if ((jd = gui_hstack(id)))
437         {
438             gui_filler(jd);
439             gui_demo_status(jd, curr_demo_replay());
440             gui_filler(jd);
441         }
442
443         gui_pulse(kd, 1.2f);
444         gui_layout(id, 0, 0);
445     }
446     audio_music_fade_out(2.0f);
447
448     return id;
449 }
450
451 static void demo_end_timer(int id, float dt)
452 {
453     float gg[3] = { 0.0f,  9.8f, 0.0f };
454     float gf[3] = { 0.0f, -9.8f, 0.0f };
455     int state = curr_demo_replay()->state;
456
457     if (time_state() < 2.f && state != GAME_NONE)
458         game_step((state == GAME_GOAL || state == GAME_SPEC) ? gg : gf, dt, NULL);
459                 
460     gui_timer(id, dt);
461     audio_timer(dt);
462 }
463
464 static int demo_end_buttn(int b, int d)
465 {
466     if (d)
467     {
468         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
469             return demo_end_action(gui_token(gui_click()));
470         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
471             return demo_end_action(simple_play ? DEMO_QUIT : DEMO_KEEP);
472     }
473     return 1;
474 }
475
476 /*---------------------------------------------------------------------------*/
477
478 static int demo_del_action(int i)
479 {
480     audio_play(AUD_MENU, 1.0f);
481
482     demo_replay_stop(i == DEMO_DEL);
483     return goto_state(&st_demo);
484 }
485
486 static int demo_del_enter(void)
487 {
488     int id, jd, kd;
489
490     if ((id = gui_vstack(0)))
491     {
492         kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
493
494         if ((jd = gui_harray(id)))
495         {
496             gui_start(jd, _("No"),  GUI_SML, DEMO_KEEP, 1);
497             gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL,  0);
498         }
499
500         gui_pulse(kd, 1.2f);
501         gui_layout(id, 0, 0);
502     }
503     audio_music_fade_out(2.0f);
504
505     return id;
506 }
507
508 static int demo_del_buttn(int b, int d)
509 {
510     if (d)
511     {
512         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
513             return demo_del_action(gui_token(gui_click()));
514         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
515             return demo_del_action(DEMO_KEEP);
516     }
517     return 1;
518 }
519
520 /*---------------------------------------------------------------------------*/
521
522 struct state st_demo = {
523     demo_enter,
524     shared_leave,
525     shared_paint,
526     shared_timer,
527     demo_point,
528     demo_stick,
529     shared_click,
530     NULL,
531     demo_buttn,
532     0
533 };
534
535 struct state st_demo_play = {
536     demo_play_enter,
537     shared_leave,
538     demo_play_paint,
539     demo_play_timer,
540     NULL,
541     NULL,
542     NULL,
543     NULL,
544     demo_play_buttn,
545     0
546 };
547
548 struct state st_demo_end = {
549     demo_end_enter,
550     shared_leave,
551     shared_paint,
552     demo_end_timer,
553     shared_point,
554     shared_stick,
555     shared_click,
556     NULL,
557     demo_end_buttn,
558     1, 0
559 };
560
561 struct state st_demo_del = {
562     demo_del_enter,
563     shared_leave,
564     shared_paint,
565     shared_timer,
566     shared_point,
567     shared_stick,
568     shared_click,
569     NULL,
570     demo_del_buttn,
571     1, 0
572 };