Implement a server/client-like game/replay architecture
[neverball] / ball / st_fall_out.c
1 /*
2  * Copyright (C) 2007 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 "gui.h"
16 #include "util.h"
17 #include "progress.h"
18 #include "audio.h"
19 #include "config.h"
20 #include "demo.h"
21
22 #include "game_common.h"
23 #include "game_server.h"
24 #include "game_client.h"
25
26 #include "st_fall_out.h"
27 #include "st_save.h"
28 #include "st_over.h"
29 #include "st_start.h"
30 #include "st_level.h"
31 #include "st_shared.h"
32 #include "st_play.h"
33
34 /*---------------------------------------------------------------------------*/
35
36 #define FALL_OUT_NEXT 1
37 #define FALL_OUT_SAME 2
38 #define FALL_OUT_SAVE 3
39 #define FALL_OUT_BACK 4
40 #define FALL_OUT_OVER 5
41
42 static int resume;
43
44 static int fall_out_action(int i)
45 {
46     audio_play(AUD_MENU, 1.0f);
47
48     switch (i)
49     {
50     case FALL_OUT_BACK:
51         /* Fall through. */
52
53     case FALL_OUT_OVER:
54         progress_stop();
55         return goto_state(&st_over);
56
57     case FALL_OUT_SAVE:
58         resume = 1;
59
60         progress_stop();
61         return goto_save(&st_fall_out, &st_fall_out);
62
63     case FALL_OUT_NEXT:
64         if (progress_next())
65             return goto_state(&st_level);
66         break;
67
68     case FALL_OUT_SAME:
69         if (progress_same())
70             return goto_state(&st_level);
71         break;
72     }
73
74     return 1;
75 }
76
77 static int fall_out_enter(void)
78 {
79     int id, jd, kd;
80
81     /* Reset hack. */
82     resume = 0;
83
84     if ((id = gui_vstack(0)))
85     {
86         kd = gui_label(id, _("Fall-out!"), GUI_LRG, GUI_ALL, gui_gry, gui_red);
87
88         gui_space(id);
89
90         if ((jd = gui_harray(id)))
91         {
92             if (progress_dead())
93                 gui_start(jd, _("Exit"), GUI_SML, FALL_OUT_OVER, 0);
94
95             if (progress_next_avail())
96                 gui_start(jd, _("Next Level"),  GUI_SML, FALL_OUT_NEXT, 0);
97
98             if (progress_same_avail())
99                 gui_start(jd, _("Retry Level"), GUI_SML, FALL_OUT_SAME, 0);
100
101             if (demo_saved())
102                 gui_state(jd, _("Save Replay"), GUI_SML, FALL_OUT_SAVE, 0);
103         }
104
105         gui_space(id);
106
107         gui_pulse(kd, 1.2f);
108         gui_layout(id, 0, 0);
109     }
110
111     audio_music_fade_out(2.0f);
112     /* audio_play(AUD_FALL, 1.0f); */
113
114     config_clr_grab();
115
116     return id;
117 }
118
119 static void fall_out_timer(int id, float dt)
120 {
121     if (time_state() < 2.f)
122     {
123         game_server_step(dt);
124         game_client_step(demo_file());
125     }
126
127     gui_timer(id, dt);
128 }
129
130 static int fall_out_keybd(int c, int d)
131 {
132     if (d)
133     {
134         if (config_tst_d(CONFIG_KEY_RESTART, c) && progress_same_avail())
135         {
136             if (progress_same())
137                 goto_state(&st_play_ready);
138         }
139     }
140     return 1;
141 }
142
143 static int fall_out_buttn(int b, int d)
144 {
145     if (d)
146     {
147         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
148             return fall_out_action(gui_token(gui_click()));
149         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
150             return fall_out_action(FALL_OUT_BACK);
151     }
152     return 1;
153 }
154
155 static void fall_out_leave(int id)
156 {
157     /* HACK:  don't run animation if only "visiting" a state. */
158     st_fall_out.timer = resume ? shared_timer : fall_out_timer;
159
160     gui_delete(id);
161 }
162
163 /*---------------------------------------------------------------------------*/
164
165 struct state st_fall_out = {
166     fall_out_enter,
167     fall_out_leave,
168     shared_paint,
169     fall_out_timer,
170     shared_point,
171     shared_stick,
172     NULL,
173     shared_click,
174     fall_out_keybd,
175     fall_out_buttn,
176     1, 0
177 };
178