can save replay only one time
[neverball] / ball / st_goal.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 "set.h"
19 #include "game.h"
20 #include "util.h"
21 #include "demo.h"
22 #include "level.h"
23 #include "audio.h"
24 #include "config.h"
25
26 #include "st_goal.h"
27 #include "st_save.h"
28 #include "st_over.h"
29 #include "st_done.h"
30 #include "st_start.h"
31 #include "st_level.h"
32
33 /*---------------------------------------------------------------------------*/
34
35 #define GOAL_NEXT 2
36 #define GOAL_SAME 3
37 #define GOAL_SAVE 4
38 #define GOAL_BACK 5
39 #define GOAL_DONE 6
40
41 static int high;
42 static int time_i;
43 static int coin_i;
44     
45 static int balls_id;
46 static int coins_id;
47 static int score_id;
48
49 extern struct state st_goal_bis;
50
51 static int goal_action(int i)
52 {
53     char player[MAXNAM];
54     size_t l;
55
56     audio_play(AUD_MENU, 1.0f);
57
58     config_get_s(CONFIG_PLAYER, player, MAXNAM);
59     l = strlen(player);
60
61     switch (i)
62     {
63     case GOAL_BACK:
64         if (level_mode() == MODE_CHALLENGE)
65             return goto_state(&st_over);
66         else
67             return goto_state(&st_start);
68
69     case GOAL_SAVE:
70         while (level_count())
71             ;
72         return goto_save(&st_goal_bis);
73
74     case GOAL_DONE:
75         while (level_count())
76             ;
77         goto_state(&st_done);
78         
79     case GOAL_NEXT:
80         while (level_count())
81             ;
82         level_next();
83         return goto_state(&st_level);
84
85     case GOAL_SAME:
86         while (level_count())
87             ;
88         return goto_state(&st_level);
89
90     case GUI_CL:
91         gui_keyboard_lock();
92         break;
93
94     case GUI_BS:
95         if (l > 0)
96         {
97             player[l - 1] = 0;
98
99             config_set_s(CONFIG_PLAYER, player);
100             level_name(curr_level(), player, time_i, coin_i);
101             set_most_coins(curr_level(), coin_i);
102             set_best_times(curr_level(), time_i);
103         }
104         break;
105
106     default:
107         if (l < MAXNAM - 1)
108         {
109             player[l + 0] = gui_keyboard_char((char) i);
110             player[l + 1] = 0;
111
112             config_set_s(CONFIG_PLAYER, player);
113             level_name(curr_level(), player, time_i, coin_i);
114             set_most_coins(curr_level(), coin_i);
115             set_best_times(curr_level(), time_i);
116         }
117     }
118     return 1;
119 }
120
121 static int goal_init(int * gidp, int save)
122 {
123     const char *s1 = _("New Record");
124     const char *s2 = _("GOAL");
125
126     int id, jd, kd;
127
128     if ((id = gui_vstack(0)))
129     {
130         int gid;
131
132         if (high)
133             gid = gui_label(id, s1, GUI_MED, GUI_ALL, gui_grn, gui_grn);
134         else
135             gid = gui_label(id, s2, GUI_LRG, GUI_ALL, gui_blu, gui_grn);
136
137         gui_space(id);
138
139         if (level_mode() == MODE_CHALLENGE)
140         {
141             if ((jd = gui_hstack(id)))
142             {
143                 if ((kd = gui_harray(jd)))
144                 {
145                     balls_id = gui_count(kd,  100, GUI_MED, GUI_RGT);
146                     gui_label(kd, _("Balls"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
147                 }
148                 if ((kd = gui_harray(jd)))
149                 {
150                     score_id = gui_count(kd, 1000, GUI_MED, GUI_RGT);
151                     gui_label(kd, _("Score"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
152                 }
153                 if ((kd = gui_harray(jd)))
154                 {
155                     coins_id = gui_count(kd, 100, GUI_MED, GUI_RGT);
156                     gui_label(kd, _("Coins"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
157                 }
158
159                 gui_set_count(balls_id, curr_balls());
160                 gui_set_count(score_id, curr_score());
161                 gui_set_count(coins_id, curr_coins());
162             }
163         }
164         
165         gui_space(id);
166
167         if ((jd = gui_harray(id)))
168         {
169             gui_most_coins(jd, 3, 1);
170             gui_best_times(jd, 3, 1);
171         }
172
173         gui_space(id);
174
175         if ((jd = gui_harray(id)))
176         {
177             if (save)
178                 gui_state(jd, _("Save Replay"), GUI_SML, GOAL_SAVE, 0);
179             else
180                 gui_label(jd, _("Save Replay"), GUI_SML, GUI_ALL, gui_blk, gui_blk);
181             
182             if (level_mode() != MODE_CHALLENGE)
183                 gui_state(jd, _("Retry Level"), GUI_SML, GOAL_SAME, 0);
184             
185             if (level_mode() == MODE_CHALLENGE && level_last())
186                 gui_start(jd, _("Finish"),      GUI_SML, GOAL_DONE, 0);
187             else if (level_opened(curr_level()+1))
188                 gui_start(jd, _("Next Level"),  GUI_SML, GOAL_NEXT, 0);
189             else
190                 gui_label(jd, _("Next Level"),  GUI_SML, GUI_ALL, gui_blk, gui_blk);
191         }
192
193         if (high) gui_keyboard(id);
194
195         gui_layout(id, 0, 0);
196         if (gidp) *gidp = gid;
197     }
198
199     set_most_coins(curr_level(), coin_i);
200     set_best_times(curr_level(), time_i);
201
202     config_clr_grab();
203
204     return id;
205 }
206
207 static int goal_enter(void)
208 {
209     int gid;
210     int r;
211     
212     time_i = 3;
213     coin_i = 3;
214     high   = level_sort(&time_i, &coin_i);
215
216     r = goal_init(&gid, 1);
217     
218     gui_pulse(gid, 1.2f);
219     audio_music_fade_out(2.0f);
220     return r; 
221 }
222
223 static int goal_bis_enter(void)
224 {
225     return goal_init(NULL, 0);
226 }
227
228 static void goal_leave(int id)
229 {
230     gui_delete(id);
231 }
232
233 static void goal_paint(int id, float st)
234 {
235     game_draw(0, st);
236     gui_paint(id);
237 }
238
239 static void goal_timer(int id, float dt)
240 {
241     static float DT = 0.0f;
242
243     float g[3] = { 0.0f, 9.8f, 0.0f };
244
245     DT += dt;
246
247     if (time_state() < 1.f)
248     {
249         game_step(g, dt, 0);
250         demo_play_step(dt);
251     }
252     else if (DT > 0.05f)
253     {
254         if (level_count())
255         {
256             int coins = curr_coins();
257             int score = curr_score();
258             int balls = curr_balls();
259
260             if (gui_value(coins_id) != coins)
261             {
262                 gui_set_count(coins_id, coins);
263                 gui_pulse(coins_id, 1.1f);
264             }
265             if (gui_value(score_id) != score)
266             {
267                 gui_set_count(score_id, score);
268                 gui_pulse(score_id, 1.1f);
269             }
270             if (gui_value(balls_id) != balls)
271             {
272                 gui_set_count(balls_id, balls);
273                 gui_pulse(balls_id, 2.0f);
274             }
275         }
276
277         DT = 0.0f;
278     }
279
280     gui_timer(id, dt);
281     audio_timer(dt);
282 }
283
284 static void goal_bis_timer(int id, float dt)
285 {
286     gui_timer(id, dt);
287     audio_timer(dt);
288 }
289
290 static void goal_point(int id, int x, int y, int dx, int dy)
291 {
292     gui_pulse(gui_point(id, x, y), 1.2f);
293 }
294
295 static void goal_stick(int id, int a, int v)
296 {
297     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
298         gui_pulse(gui_stick(id, v, 0), 1.2f);
299     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
300         gui_pulse(gui_stick(id, 0, v), 1.2f);
301 }
302
303 static int goal_click(int b, int d)
304 {
305     if (b <= 0 && d == 1)
306         return goal_action(gui_token(gui_click()));
307     return 1;
308 }
309
310 static int goal_keybd(int c, int d)
311 {
312     if (d && c == SDLK_ESCAPE)
313         goal_action(GOAL_BACK);
314     return 1;
315 }
316
317 static int goal_buttn(int b, int d)
318 {
319     if (d)
320     {
321         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
322             return goal_click(0, 1);
323         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
324             goal_action(GOAL_BACK);
325     }
326     return 1;
327 }
328
329 /*---------------------------------------------------------------------------*/
330
331 struct state st_goal = {
332     goal_enter,
333     goal_leave,
334     goal_paint,
335     goal_timer,
336     goal_point,
337     goal_stick,
338     goal_click,
339     goal_keybd,
340     goal_buttn,
341     1, 0
342 };
343
344 struct state st_goal_bis = {
345     goal_bis_enter,
346     goal_leave,
347     goal_paint,
348     goal_bis_timer,
349     goal_point,
350     goal_stick,
351     goal_click,
352     goal_keybd,
353     goal_buttn,
354     1, 0
355 };