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