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