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