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