2 * Copyright (C) 2003 Robert Kooima
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.
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.
25 #include "st_shared.h"
30 #include "game_common.h"
31 #include "game_server.h"
32 #include "game_client.h"
37 /*---------------------------------------------------------------------------*/
47 static int last_viewed = 0;
49 /*---------------------------------------------------------------------------*/
51 static int demo_action(int i)
53 audio_play(AUD_MENU, 1.0f);
58 return goto_state(&st_title);
62 return goto_state(&st_demo);
67 return goto_state(&st_demo);
75 if (progress_replay(DEMO_GET(items, i)->filename))
79 return goto_state(&st_demo_play);
86 /*---------------------------------------------------------------------------*/
88 static void demo_replay(int id, int i)
90 int w = config_get_d(CONFIG_WIDTH);
91 int h = config_get_d(CONFIG_HEIGHT);
96 trunc_string(DEMO_GET(items, i)->name, nam, sizeof (nam));
98 if ((jd = gui_vstack(id)))
101 gui_image(jd, DEMO_GET(items, i)->shot, w / 6, h / 6);
102 gui_state(jd, nam, GUI_SML, i, 0);
104 gui_active(jd, i, 0);
112 static int status_id;
113 static int player_id;
115 static int gui_demo_status(int id, const struct demo *d)
124 /* Build a long name */
125 memset(noname, 'M', MAXNAM - 1);
126 noname[MAXNAM - 1] = '\0';
128 /* Get a long status */
129 status = status_to_str(0);
131 for (i = 1; i <= GAME_FALL; i++)
133 k = strlen(status_to_str(i));
137 status = status_to_str(i);
143 status = status_to_str(d->status);
146 if ((jd = gui_hstack(id)))
150 if ((kd = gui_hstack(jd)))
152 if ((ld = gui_vstack(kd)))
156 time_id = gui_clock(ld, (d ? d->timer : 35000),
158 coin_id = gui_count(ld, (d ? d->coins : 100),
160 status_id = gui_label(ld, status, GUI_SML, GUI_SE,
163 if (d && d->status == GAME_GOAL)
164 gui_set_color(status_id, gui_grn, gui_grn);
169 if ((ld = gui_vstack(kd)))
173 gui_label(ld, _("Time"), GUI_SML, GUI_NW,
175 gui_label(ld, _("Coins"), GUI_SML, 0,
177 gui_label(ld, _("Status"), GUI_SML, GUI_SW,
186 if ((kd = gui_vstack(jd)))
190 name_id = gui_label(kd, (d ? d->name : noname),
191 GUI_SML, GUI_NE, 0, 0);
192 player_id = gui_label(kd, (d ? d->player : noname),
194 date_id = gui_label(kd, (d ? date_to_str(d->date) :
195 date_to_str(time(NULL))),
196 GUI_SML, GUI_SE, 0, 0);
201 if ((kd = gui_vstack(jd)))
205 gui_label(kd, _("Replay"), GUI_SML, GUI_NW, gui_wht, gui_wht);
206 gui_label(kd, _("Player"), GUI_SML, 0, gui_wht, gui_wht);
207 gui_label(kd, _("Date"), GUI_SML, GUI_SW, gui_wht, gui_wht);
218 static void gui_demo_update_status(int i)
220 const struct demo *d;
223 d = DEMO_GET(items, i < total ? i : 0);
227 gui_set_label(name_id, d->name);
228 gui_set_label(date_id, date_to_str(d->date));
229 gui_set_label(player_id, d->player);
231 if (d->status == GAME_GOAL)
232 gui_set_color(status_id, gui_grn, gui_grn);
234 gui_set_color(status_id, gui_red, gui_red);
236 gui_set_label(status_id, status_to_str(d->status));
237 gui_set_count(coin_id, d->coins);
238 gui_set_clock(time_id, d->timer);
241 /*---------------------------------------------------------------------------*/
243 static int demo_enter(void)
249 demo_dir_free(items);
251 items = demo_dir_scan(config_user(""));
252 total = array_len(items);
258 if ((jd = gui_hstack(id)))
261 gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
263 gui_navig(jd, first > 0, first + DEMO_STEP < total);
266 if ((jd = gui_varray(id)))
267 for (i = first; i < first + DEMO_STEP ; i += DEMO_LINE)
268 if ((kd = gui_harray(jd)))
270 for (j = i + DEMO_LINE - 1; j >= i; j--)
277 gui_demo_status(id, NULL);
278 gui_layout(id, 0, 0);
279 gui_demo_update_status(last_viewed);
283 gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0, 0);
284 gui_layout(id, 0, 0);
287 audio_music_fade_to(0.5f, "bgm/inter.ogg");
292 static void demo_timer(int id, float dt)
294 if (total == 0 && time_state() > 4.0f)
295 goto_state(&st_title);
300 static void demo_point(int id, int x, int y, int dx, int dy)
302 int jd = shared_point_basic(id, x, y);
303 int i = gui_token(jd);
305 if (jd && i >= 0 && !GUI_ISMSK(i))
306 gui_demo_update_status(i);
309 static void demo_stick(int id, int a, int v)
311 int jd = shared_stick_basic(id, a, v);
312 int i = gui_token(jd);
314 if (jd && i >= 0 && !GUI_ISMSK(i))
315 gui_demo_update_status(i);
318 static int demo_buttn(int b, int d)
322 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
323 return demo_action(total ? gui_token(gui_click()) : GUI_BACK);
324 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
325 return demo_action(GUI_BACK);
330 /*---------------------------------------------------------------------------*/
332 static int standalone;
333 static int demo_paused;
335 static int check_compat;
337 void demo_play_goto(int s)
343 static int demo_play_enter(void)
350 audio_music_fade_in(0.5f);
354 if (check_compat && !game_compat_map)
356 goto_state(&st_demo_compat);
360 if ((id = gui_vstack(0)))
362 gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
363 gui_layout(id, 0, 0);
369 game_set_fly(0.f, game_client_file());
370 game_client_step(NULL);
375 static void demo_play_paint(int id, float t)
382 if (time_state() < 1.f)
386 static void demo_play_timer(int id, float dt)
393 * Introduce a one-second pause at the start of replay playback. (One
394 * second is the time during which the "Replay" label is being displayed.)
395 * HACK ALERT! "id == 0" means we got here from the pause screen, so no
396 * label has been created and there's no need to wait.
399 if (id != 0 && time_state() < 1.0f)
402 /* Spin or skip depending on how fast the demo wants to run. */
404 if (!demo_replay_step(dt))
407 goto_state(&st_demo_end);
413 static int demo_play_keybd(int c, int d)
417 if (config_tst_d(CONFIG_KEY_PAUSE, c))
420 return goto_state(&st_demo_end);
424 show_hud = !show_hud;
429 static int demo_play_buttn(int b, int d)
433 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
435 if (config_tst_d(CONFIG_KEY_PAUSE, SDLK_ESCAPE))
438 return goto_state(&st_demo_end);
444 /*---------------------------------------------------------------------------*/
449 #define DEMO_REPLAY 3
450 #define DEMO_CONTINUE 4
452 static int demo_end_action(int i)
454 audio_play(AUD_MENU, 1.0f);
460 return goto_state(&st_demo_del);
464 return goto_state(&st_demo);
470 progress_replay(curr_demo_replay()->filename);
471 return goto_state(&st_demo_play);
473 return goto_state(&st_demo_play);
478 static int demo_end_enter(void)
482 if ((id = gui_vstack(0)))
485 kd = gui_label(id, _("Replay Paused"), GUI_LRG, GUI_ALL,
488 kd = gui_label(id, _("Replay Ends"), GUI_LRG, GUI_ALL,
491 if ((jd = gui_harray(id)))
497 start_id = gui_start(jd, _("Quit"), GUI_SML, DEMO_QUIT, 1);
501 start_id = gui_start(jd, _("Keep"), GUI_SML, DEMO_KEEP, 1);
502 gui_state(jd, _("Delete"), GUI_SML, DEMO_DEL, 0);
507 gui_start(jd, _("Continue"), GUI_SML, DEMO_CONTINUE, 1);
508 gui_toggle(start_id);
511 gui_state(jd, _("Repeat"), GUI_SML, DEMO_REPLAY, 0);
515 gui_layout(id, 0, 0);
518 audio_music_fade_out(demo_paused ? 0.2f : 2.0f);
523 static void demo_end_paint(int id, float t)
532 static int demo_end_keybd(int c, int d)
536 if (demo_paused && config_tst_d(CONFIG_KEY_PAUSE, c))
537 return demo_end_action(DEMO_CONTINUE);
542 static int demo_end_buttn(int b, int d)
546 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
547 return demo_end_action(gui_token(gui_click()));
549 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
552 return demo_end_action(DEMO_CONTINUE);
554 return demo_end_action(standalone ? DEMO_QUIT : DEMO_KEEP);
560 /*---------------------------------------------------------------------------*/
562 static int demo_del_action(int i)
564 audio_play(AUD_MENU, 1.0f);
566 demo_replay_stop(i == DEMO_DEL);
567 return goto_state(&st_demo);
570 static int demo_del_enter(void)
574 if ((id = gui_vstack(0)))
576 kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
578 if ((jd = gui_harray(id)))
580 gui_start(jd, _("No"), GUI_SML, DEMO_KEEP, 1);
581 gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL, 0);
585 gui_layout(id, 0, 0);
587 audio_music_fade_out(2.0f);
592 static int demo_del_buttn(int b, int d)
596 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
597 return demo_del_action(gui_token(gui_click()));
598 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
599 return demo_del_action(DEMO_KEEP);
604 /*---------------------------------------------------------------------------*/
606 static int demo_compat_enter(void)
612 if ((id = gui_vstack(0)))
614 gui_label(id, _("Warning!"), GUI_MED, GUI_ALL, 0, 0);
616 gui_multi(id, _("The replay you're about to view was\\"
617 "recorded with a different (or unknown)\\"
618 "version of this map. Be prepared to\\"
619 "encounter visual errors.\\"),
620 GUI_SML, GUI_ALL, gui_wht, gui_wht);
622 gui_layout(id, 0, 0);
625 game_set_fly(0.f, game_client_file());
626 game_client_step(NULL);
631 static void demo_compat_timer(int id, float dt)
637 static int demo_compat_buttn(int b, int d)
641 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
642 return goto_state(&st_demo_play);
643 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
644 return goto_state(&st_demo_end);
649 /*---------------------------------------------------------------------------*/
651 struct state st_demo = {
665 struct state st_demo_play = {
679 struct state st_demo_end = {
693 struct state st_demo_del = {
707 struct state st_demo_compat = {