Make sure strncpy always keeps one byte untouched for NUL
[neverball] / ball / st_title.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 #include <assert.h>
17
18 #include "gui.h"
19 #include "vec3.h"
20 #include "demo.h"
21 #include "audio.h"
22 #include "config.h"
23 #include "st_shared.h"
24 #include "cmd.h"
25 #include "demo_dir.h"
26
27 #include "game_common.h"
28 #include "game_server.h"
29 #include "game_client.h"
30 #include "game_proxy.h"
31
32 #include "st_title.h"
33 #include "st_help.h"
34 #include "st_demo.h"
35 #include "st_conf.h"
36 #include "st_set.h"
37 #include "st_name.h"
38
39 /*---------------------------------------------------------------------------*/
40
41 static int init_title_level(void)
42 {
43     if (game_client_init("map-medium/title.sol"))
44     {
45         union cmd cmd;
46
47         cmd.type = CMD_GOAL_OPEN;
48         game_proxy_enq(&cmd);
49
50         game_client_step(NULL);
51
52         return 1;
53     }
54     return 0;
55 }
56
57 static const char *pick_demo(Array items)
58 {
59     struct dir_item *item;
60     return (item = array_rnd(items)) ? item->path : NULL;
61 }
62
63 /*---------------------------------------------------------------------------*/
64
65 static float real_time = 0.0f;
66 static int   mode      = 0;
67
68 static Array items;
69
70 static int play_id = 0;
71
72 #define TITLE_PLAY 1
73 #define TITLE_HELP 2
74 #define TITLE_DEMO 3
75 #define TITLE_CONF 4
76 #define TITLE_EXIT 5
77
78 static int title_action(int i)
79 {
80     static const char keyphrase[] = "CHEAT";
81     static char queue[sizeof (keyphrase)] = "";
82
83     size_t queue_len = strlen(queue);
84
85     audio_play(AUD_MENU, 1.0f);
86
87     switch (i)
88     {
89     case TITLE_PLAY:
90         if (strlen(config_get_s(CONFIG_PLAYER)) == 0)
91             return goto_name(&st_set, &st_title, 0);
92         else
93             return goto_state(&st_set);
94
95         break;
96
97     case TITLE_HELP: return goto_state(&st_help); break;
98     case TITLE_DEMO: return goto_state(&st_demo); break;
99     case TITLE_CONF: return goto_state(&st_conf); break;
100     case TITLE_EXIT: return 0;                    break;
101
102     default:
103
104         /* Let the queue fill up. */
105
106         if (queue_len < sizeof (queue) - 1)
107         {
108             queue[queue_len]     = (char) i;
109             queue[queue_len + 1] = '\0';
110         }
111
112         /* Advance the queue before adding the new element. */
113
114         else
115         {
116             int k;
117
118             for (k = 1; k < queue_len; k++)
119                 queue[k - 1] = queue[k];
120
121             queue[queue_len - 1] = (char) i;
122         }
123
124         if (strcmp(queue, keyphrase) == 0)
125         {
126             config_set_cheat();
127             gui_set_label(play_id, sgettext("menu^Cheat"));
128             gui_pulse(play_id, 1.2f);
129         }
130         else if (config_cheat())
131         {
132             config_clr_cheat();
133             gui_set_label(play_id, sgettext("menu^Play"));
134             gui_pulse(play_id, 1.2f);
135         }
136
137         break;
138     }
139     return 1;
140 }
141
142 static int title_enter(void)
143 {
144     int id, jd, kd;
145
146     /* Build the title GUI. */
147
148     if ((id = gui_vstack(0)))
149     {
150         gui_label(id, "Neverball", GUI_LRG, GUI_ALL, 0, 0);
151
152         gui_space(id);
153
154         if ((jd = gui_harray(id)))
155         {
156             gui_filler(jd);
157
158             if ((kd = gui_varray(jd)))
159             {
160                 if (config_cheat())
161                     play_id = gui_start(kd, sgettext("menu^Cheat"),
162                                         GUI_MED, TITLE_PLAY, 1);
163                 else
164                     play_id = gui_start(kd, sgettext("menu^Play"),
165                                         GUI_MED, TITLE_PLAY, 1);
166
167                 gui_state(kd, sgettext("menu^Replay"),  GUI_MED, TITLE_DEMO, 0);
168                 gui_state(kd, sgettext("menu^Help"),    GUI_MED, TITLE_HELP, 0);
169                 gui_state(kd, sgettext("menu^Options"), GUI_MED, TITLE_CONF, 0);
170                 gui_state(kd, sgettext("menu^Exit"),    GUI_MED, TITLE_EXIT, 0);
171             }
172
173             gui_filler(jd);
174         }
175         gui_layout(id, 0, 0);
176     }
177
178     /* Start the title screen music. */
179
180     audio_music_fade_to(0.5f, "bgm/title.ogg");
181
182     /* Initialize the title level for display. */
183
184     init_title_level();
185
186     real_time = 0.0f;
187     mode = 0;
188
189     SDL_EnableUNICODE(1);
190
191     return id;
192 }
193
194 static void title_leave(int id)
195 {
196     if (items)
197     {
198         demo_dir_free(items);
199         items = NULL;
200     }
201
202     SDL_EnableUNICODE(0);
203     demo_replay_stop(0);
204     gui_delete(id);
205 }
206
207 static void title_timer(int id, float dt)
208 {
209     static const char *demo = NULL;
210
211     real_time += dt;
212
213     switch (mode)
214     {
215     case 0: /* Mode 0: Pan across title level. */
216
217         if (real_time <= 20.0f)
218         {
219             game_set_fly(fcosf(V_PI * real_time / 20.0f),
220                          game_client_file());
221             game_client_step(NULL);
222         }
223         else
224         {
225             game_fade(+1.0f);
226             real_time = 0.0f;
227             mode = 1;
228         }
229         break;
230
231     case 1: /* Mode 1: Fade out.  Load demo level. */
232
233         if (real_time > 1.0f)
234         {
235             if (!items)
236                 items = demo_dir_scan();
237
238             if ((demo = pick_demo(items)))
239             {
240                 demo_replay_init(demo, NULL, NULL, NULL, NULL, NULL);
241                 game_set_fly(0.0f, game_client_file());
242                 game_client_step(NULL);
243                 real_time = 0.0f;
244                 mode = 2;
245             }
246             else
247             {
248                 game_fade(-1.0f);
249                 real_time = 0.0f;
250                 mode = 0;
251             }
252         }
253         break;
254
255     case 2: /* Mode 2: Run demo. */
256
257         if (!demo_replay_step(dt))
258         {
259             demo_replay_stop(0);
260             game_fade(+1.0f);
261             real_time = 0.0f;
262             mode = 3;
263         }
264         break;
265
266     case 3: /* Mode 3: Fade out.  Load title level. */
267
268         if (real_time > 1.0f)
269         {
270             init_title_level();
271
272             real_time = 0.0f;
273             mode = 0;
274         }
275         break;
276     }
277
278     gui_timer(id, dt);
279     game_step_fade(dt);
280 }
281
282 static int title_keybd(int c, int d)
283 {
284     if (d && (c & 0xFF80) == 0 && c > ' ')
285         return title_action(c);
286     return 1;
287 }
288
289 static int title_buttn(int b, int d)
290 {
291     if (d)
292     {
293         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
294             return title_action(gui_token(gui_click()));
295         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
296             return 0;
297     }
298     return 1;
299 }
300
301 /*---------------------------------------------------------------------------*/
302
303 struct state st_title = {
304     title_enter,
305     title_leave,
306     shared_paint,
307     title_timer,
308     shared_point,
309     shared_stick,
310     shared_angle,
311     shared_click,
312     title_keybd,
313     title_buttn,
314     1, 0
315 };
316