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"
29 #include "game_common.h"
30 #include "game_server.h"
31 #include "game_client.h"
36 /*---------------------------------------------------------------------------*/
44 static int last_viewed = 0;
46 /*---------------------------------------------------------------------------*/
48 static int demo_action(int i)
50 audio_play(AUD_MENU, 1.0f);
55 return goto_state(&st_title);
59 return goto_state(&st_demo);
64 return goto_state(&st_demo);
72 if (progress_replay(demo_get(i)->filename))
76 return goto_state(&st_demo_play);
83 /*---------------------------------------------------------------------------*/
85 static void demo_replay(int id, int i)
87 int w = config_get_d(CONFIG_WIDTH);
88 int h = config_get_d(CONFIG_HEIGHT);
93 if ((jd = gui_vstack(id)))
96 gui_image(jd, demo_get(i)->shot, w / 6, h / 6);
98 nam[MAXNAM - 1] = '\0';
99 strncpy(nam, demo_get(i)->name, MAXNAM);
100 if (nam[MAXNAM - 1] != '\0')
102 nam[MAXNAM - 2] = '.';
103 nam[MAXNAM - 1] = '.';
104 nam[MAXNAM + 0] = '.';
105 nam[MAXNAM + 1] = '\0';
107 gui_state(jd, nam, GUI_SML, i, 0);
109 gui_active(jd, i, 0);
117 static int status_id;
118 static int player_id;
120 static int gui_demo_status(int id, const struct demo *d)
129 /* Build a long name */
130 memset(noname, 'M', MAXNAM - 1);
131 noname[MAXNAM - 1] = '\0';
133 /* Get a long status */
134 status = status_to_str(0);
136 for (i = 1; i <= GAME_FALL; i++)
138 k = strlen(status_to_str(i));
142 status = status_to_str(i);
148 status = status_to_str(d->status);
151 if ((jd = gui_hstack(id)))
155 if ((kd = gui_hstack(jd)))
157 if ((ld = gui_vstack(kd)))
161 time_id = gui_clock(ld, (d ? d->timer : 35000),
163 coin_id = gui_count(ld, (d ? d->coins : 100),
165 status_id = gui_label(ld, status, GUI_SML, GUI_SE,
168 if (d && d->status == GAME_GOAL)
169 gui_set_color(status_id, gui_grn, gui_grn);
174 if ((ld = gui_vstack(kd)))
178 gui_label(ld, _("Time"), GUI_SML, GUI_NW,
180 gui_label(ld, _("Coins"), GUI_SML, 0,
182 gui_label(ld, _("Status"), GUI_SML, GUI_SW,
191 if ((kd = gui_vstack(jd)))
195 name_id = gui_label(kd, (d ? d->name : noname),
196 GUI_SML, GUI_NE, 0, 0);
197 player_id = gui_label(kd, (d ? d->player : noname),
199 date_id = gui_label(kd, (d ? date_to_str(d->date) :
200 date_to_str(time(NULL))),
201 GUI_SML, GUI_SE, 0, 0);
206 if ((kd = gui_vstack(jd)))
210 gui_label(kd, _("Replay"), GUI_SML, GUI_NW, gui_wht, gui_wht);
211 gui_label(kd, _("Player"), GUI_SML, 0, gui_wht, gui_wht);
212 gui_label(kd, _("Date"), GUI_SML, GUI_SW, gui_wht, gui_wht);
223 static void gui_demo_update_status(int i)
225 const struct demo *d;
227 if ((d = demo_get(i)) == NULL && (d = demo_get(0)) == NULL)
230 gui_set_label(name_id, d->name);
231 gui_set_label(date_id, date_to_str(d->date));
232 gui_set_label(player_id, d->player);
234 if (d->status == GAME_GOAL)
235 gui_set_color(status_id, gui_grn, gui_grn);
237 gui_set_color(status_id, gui_red, gui_red);
239 gui_set_label(status_id, status_to_str(d->status));
240 gui_set_count(coin_id, d->coins);
241 gui_set_clock(time_id, d->timer);
244 /*---------------------------------------------------------------------------*/
246 static int demo_enter(void)
253 if ((total = demo_scan()))
255 if ((jd = gui_hstack(id)))
258 gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
260 gui_navig(jd, first > 0, first + DEMO_STEP < total);
263 if ((jd = gui_varray(id)))
264 for (i = first; i < first + DEMO_STEP ; i += DEMO_LINE)
265 if ((kd = gui_harray(jd)))
267 for (j = i + DEMO_LINE - 1; j >= i; j--)
274 gui_demo_status(id, NULL);
275 gui_layout(id, 0, 0);
276 gui_demo_update_status(last_viewed);
280 gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0, 0);
281 gui_layout(id, 0, 0);
284 audio_music_fade_to(0.5f, "bgm/inter.ogg");
289 static void demo_timer(int id, float dt)
291 if (total == 0 && time_state() > 4.0f)
292 goto_state(&st_title);
297 static void demo_point(int id, int x, int y, int dx, int dy)
299 int jd = shared_point_basic(id, x, y);
300 int i = gui_token(jd);
302 if (jd && i >= 0 && !GUI_ISMSK(i))
303 gui_demo_update_status(i);
306 static void demo_stick(int id, int a, int v)
308 int jd = shared_stick_basic(id, a, v);
309 int i = gui_token(jd);
311 if (jd && i >= 0 && !GUI_ISMSK(i))
312 gui_demo_update_status(i);
315 static int demo_buttn(int b, int d)
319 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
320 return demo_action(total ? gui_token(gui_click()) : GUI_BACK);
321 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
322 return demo_action(GUI_BACK);
327 /*---------------------------------------------------------------------------*/
329 static int standalone;
330 static int demo_paused;
332 static int check_compat;
334 void demo_play_goto(int s)
340 static int demo_play_enter(void)
347 audio_music_fade_in(0.5f);
351 if (check_compat && !game_compat_map)
353 goto_state(&st_demo_compat);
357 if ((id = gui_vstack(0)))
359 gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
360 gui_layout(id, 0, 0);
366 game_set_fly(0.f, game_client_file());
367 game_client_step(NULL);
372 static void demo_play_paint(int id, float t)
379 if (time_state() < 1.f)
383 static void demo_play_timer(int id, float dt)
390 * Introduce a one-second pause at the start of replay playback. (One
391 * second is the time during which the "Replay" label is being displayed.)
392 * HACK ALERT! "id == 0" means we got here from the pause screen, so no
393 * label has been created and there's no need to wait.
396 if (id != 0 && time_state() < 1.0f)
399 /* Spin or skip depending on how fast the demo wants to run. */
401 if (!demo_replay_step(dt))
404 goto_state(&st_demo_end);
410 static int demo_play_keybd(int c, int d)
414 if (config_tst_d(CONFIG_KEY_PAUSE, c))
417 return goto_state(&st_demo_end);
421 show_hud = !show_hud;
426 static int demo_play_buttn(int b, int d)
430 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
432 if (config_tst_d(CONFIG_KEY_PAUSE, SDLK_ESCAPE))
435 return goto_state(&st_demo_end);
441 /*---------------------------------------------------------------------------*/
446 #define DEMO_REPLAY 3
447 #define DEMO_CONTINUE 4
449 static int demo_end_action(int i)
451 audio_play(AUD_MENU, 1.0f);
457 return goto_state(&st_demo_del);
461 return goto_state(&st_demo);
467 progress_replay(curr_demo_replay()->filename);
468 return goto_state(&st_demo_play);
470 return goto_state(&st_demo_play);
475 static int demo_end_enter(void)
479 if ((id = gui_vstack(0)))
482 kd = gui_label(id, _("Replay Paused"), GUI_LRG, GUI_ALL,
485 kd = gui_label(id, _("Replay Ends"), GUI_LRG, GUI_ALL,
488 if ((jd = gui_harray(id)))
494 start_id = gui_start(jd, _("Quit"), GUI_SML, DEMO_QUIT, 1);
498 start_id = gui_start(jd, _("Keep"), GUI_SML, DEMO_KEEP, 1);
499 gui_state(jd, _("Delete"), GUI_SML, DEMO_DEL, 0);
504 gui_start(jd, _("Continue"), GUI_SML, DEMO_CONTINUE, 1);
505 gui_toggle(start_id);
508 gui_state(jd, _("Repeat"), GUI_SML, DEMO_REPLAY, 0);
512 gui_layout(id, 0, 0);
515 audio_music_fade_out(demo_paused ? 0.2f : 2.0f);
520 static void demo_end_paint(int id, float t)
529 static int demo_end_keybd(int c, int d)
533 if (demo_paused && config_tst_d(CONFIG_KEY_PAUSE, c))
534 return demo_end_action(DEMO_CONTINUE);
539 static int demo_end_buttn(int b, int d)
543 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
544 return demo_end_action(gui_token(gui_click()));
546 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
549 return demo_end_action(DEMO_CONTINUE);
551 return demo_end_action(standalone ? DEMO_QUIT : DEMO_KEEP);
557 /*---------------------------------------------------------------------------*/
559 static int demo_del_action(int i)
561 audio_play(AUD_MENU, 1.0f);
563 demo_replay_stop(i == DEMO_DEL);
564 return goto_state(&st_demo);
567 static int demo_del_enter(void)
571 if ((id = gui_vstack(0)))
573 kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
575 if ((jd = gui_harray(id)))
577 gui_start(jd, _("No"), GUI_SML, DEMO_KEEP, 1);
578 gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL, 0);
582 gui_layout(id, 0, 0);
584 audio_music_fade_out(2.0f);
589 static int demo_del_buttn(int b, int d)
593 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
594 return demo_del_action(gui_token(gui_click()));
595 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
596 return demo_del_action(DEMO_KEEP);
601 /*---------------------------------------------------------------------------*/
603 static int demo_compat_enter(void)
609 if ((id = gui_vstack(0)))
611 gui_label(id, _("Warning!"), GUI_MED, GUI_ALL, 0, 0);
613 gui_multi(id, _("The replay you're about to view was\\"
614 "recorded with a different (or unknown)\\"
615 "version of this map. Be prepared to\\"
616 "encounter visual errors.\\"),
617 GUI_SML, GUI_ALL, gui_wht, gui_wht);
619 gui_layout(id, 0, 0);
622 game_set_fly(0.f, game_client_file());
623 game_client_step(NULL);
628 static void demo_compat_timer(int id, float dt)
634 static int demo_compat_buttn(int b, int d)
638 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
639 return goto_state(&st_demo_play);
640 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
641 return goto_state(&st_demo_end);
646 /*---------------------------------------------------------------------------*/
648 struct state st_demo = {
662 struct state st_demo_play = {
676 struct state st_demo_end = {
690 struct state st_demo_del = {
704 struct state st_demo_compat = {