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