A new inline help system.
[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 "levels.h"
22 #include "audio.h"
23 #include "config.h"
24 #include "demo.h"
25 #include "st_shared.h"
26
27 #include "st_goal.h"
28 #include "st_save.h"
29 #include "st_over.h"
30 #include "st_done.h"
31 #include "st_start.h"
32 #include "st_level.h"
33 #include "st_name.h"
34
35 /*---------------------------------------------------------------------------*/
36
37 #define GOAL_NEXT 2
38 #define GOAL_SAME 3
39 #define GOAL_SAVE 4
40 #define GOAL_BACK 5
41 #define GOAL_DONE 6
42 #define GOAL_NAME 7
43
44 static int balls_id;
45 static int coins_id;
46 static int score_id;
47
48 extern struct state st_goal_bis;
49
50 static int goal_action(int i)
51 {
52     audio_play(AUD_MENU, 1.0f);
53
54     switch (i)
55     {
56     case GOAL_BACK:
57         return goto_end_level();
58
59     case GOAL_SAVE:
60         return goto_save(&st_goal_bis, &st_goal_bis);
61
62     case GOAL_NAME:
63         return goto_name(&st_goal_bis, &st_goal_bis);
64         
65     case GOAL_DONE:
66         return goto_state(&st_done);
67         
68     case GOAL_NEXT:
69         level_next();
70         return goto_state(&st_level);
71
72     case GOAL_SAME:
73         return goto_state(&st_level);
74     }
75
76     return 1;
77 }
78
79 static int goal_init(int * gidp)
80 {
81     const struct level_game *lg = curr_lg();
82     int mode  = lg->mode;
83     int state = lg->state;
84     const struct level *l = lg->level;
85
86     int id, jd, kd;
87     int high;
88
89     high = (lg->time_rank < 3) || (lg->goal_rank < 3) || (lg->coin_rank < 3);
90
91     if ((id = gui_vstack(0)))
92     {
93         int gid;
94
95         if      (state == GAME_SPEC)
96             gid = gui_label(id, _("Special Goal"), GUI_MED, GUI_ALL, gui_yel, gui_yel);
97         else if (state == GAME_FALL)
98             gid = gui_label(id, _("Fall-out!"),    GUI_MED, GUI_ALL, gui_yel, gui_yel);
99         else if (state == GAME_TIME)
100             gid = gui_label(id, _("Time's Up!"),   GUI_MED, GUI_ALL, gui_yel, gui_yel);
101         else if (high)
102             gid = gui_label(id, _("New Record"),   GUI_MED, GUI_ALL, gui_grn, gui_grn);
103         else
104             gid = gui_label(id, _("GOAL"),         GUI_LRG, GUI_ALL, gui_blu, gui_grn);
105
106         gui_space(id);
107
108         if (mode == MODE_CHALLENGE && 
109                 (lg->state == GAME_GOAL || lg->state == GAME_SPEC || l->is_bonus))
110         {
111             int coins = lg->coins;
112             int score = lg->score - coins;
113             int balls = lg->balls - count_extra_balls(score, coins);
114             if ((jd = gui_hstack(id)))
115             {
116                 if ((kd = gui_harray(jd)))
117                 {
118                     balls_id = gui_count(kd,  100, GUI_MED, GUI_RGT);
119                     gui_label(kd, _("Balls"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
120                 }
121                 if ((kd = gui_harray(jd)))
122                 {
123                     score_id = gui_count(kd, 1000, GUI_MED, GUI_RGT);
124                     gui_label(kd, _("Score"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
125                 }
126                 if ((kd = gui_harray(jd)))
127                 {
128                     coins_id = gui_count(kd, 100, GUI_MED, GUI_RGT);
129                     gui_label(kd, _("Coins"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
130                 }
131
132                 gui_set_count(balls_id, balls);
133                 gui_set_count(score_id, score);
134                 gui_set_count(coins_id, coins);
135             }
136         }
137         else
138         {
139             balls_id = score_id = coins_id = 0;
140         }
141         
142         if (state == GAME_GOAL)
143         {
144             gui_space(id);
145             if ((jd = gui_harray(id)))
146             {
147                 gui_most_coins(jd, 1);
148                 gui_best_times(jd, 1);
149             }
150         }
151
152         gui_space(id);
153
154         if ((jd = gui_harray(id)))
155         {
156             int nlid = 0, rlid = 0;
157             int b = 0;
158             if (lg->win)
159                 gui_start(jd, _("Finish"),      GUI_SML, GOAL_DONE, 0);
160             else
161                 nlid = gui_maybe(jd, _("Next Level"),  GOAL_NEXT, lg->next_level != NULL);
162             
163             b = mode != MODE_CHALLENGE || 
164                     ((state == GAME_FALL || state == GAME_TIME) && !lg->dead && !l->is_bonus);
165             rlid = gui_maybe(jd, _("Retry Level"), GOAL_SAME, b);
166            
167             gui_maybe(jd, _("Save Replay"), GOAL_SAVE, demo_play_saved());
168
169             /* default is next if the next level is newly unkocked */
170             if (nlid != 0 && lg->unlock)
171                  gui_focus(nlid);
172             else if(b)
173                  gui_focus(rlid);
174         }
175
176         if (high)
177             gui_state(id, _("Change Player Name"),  GUI_SML, GOAL_NAME, 0);
178
179         gui_layout(id, 0, 0);
180         if (gidp) *gidp = gid;
181     }
182
183     if (state == GAME_GOAL)
184     {
185         set_most_coins(&l->coin_score, lg->coin_rank);
186         if (mode == MODE_CHALLENGE || mode == MODE_NORMAL)
187             set_best_times(&l->goal_score, lg->goal_rank, 1);
188         else
189             set_best_times(&l->time_score, lg->time_rank, 0);
190     }
191
192     config_clr_grab();
193
194     return id;
195 }
196
197 static int goal_enter(void)
198 {
199     int gid;
200     int r;
201     
202     r = goal_init(&gid);
203     
204     gui_pulse(gid, 1.2f);
205     audio_music_fade_out(2.0f);
206     return r; 
207 }
208
209 static int goal_bis_enter(void)
210 {
211     level_update_player_name();
212     return goal_init(NULL);
213 }
214
215 static void goal_timer(int id, float dt)
216 {
217     static float DT = 0.0f;
218
219     float gg[3] = { 0.0f, 9.8f, 0.0f };
220     float gf[3] = { 0.0f, -9.8f, 0.0f };
221     int state = curr_lg()->state;
222
223     DT += dt;
224
225     if (time_state() < 2.f)
226         game_step((state == GAME_GOAL || state == GAME_SPEC) ? gg : gf, dt, NULL);
227     
228     if (time_state() > 1.f && DT > 0.05f && coins_id != 0)
229     {
230         int coins = gui_value(coins_id);
231         if (coins > 0)
232         {
233             int score = gui_value(score_id);
234             int balls = gui_value(balls_id);
235
236             gui_set_count(coins_id, coins - 1);
237             gui_pulse(coins_id, 1.1f);
238             
239             gui_set_count(score_id, score + 1);
240             gui_pulse(score_id, 1.1f);
241             if ((score+1) % 100 == 0)
242             {
243                 gui_set_count(balls_id, balls + 1);
244                 gui_pulse(balls_id, 2.0f);
245                 audio_play(AUD_BALL, 1.0f);
246             }
247         }
248
249         DT = 0.0f;
250     }
251
252     gui_timer(id, dt);
253     audio_timer(dt);
254 }
255
256 static int goal_buttn(int b, int d)
257 {
258     if (d)
259     {
260         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
261             return goal_action(gui_token(gui_click()));
262         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
263             return goal_action(GOAL_BACK);
264     }
265     return 1;
266 }
267
268 /*---------------------------------------------------------------------------*/
269
270 struct state st_goal = {
271     goal_enter,
272     shared_leave,
273     shared_paint,
274     goal_timer,
275     shared_point,
276     shared_stick,
277     shared_click,
278     NULL,
279     goal_buttn,
280     1, 0
281 };
282
283 struct state st_goal_bis = {
284     goal_bis_enter,
285     shared_leave,
286     shared_paint,
287     shared_timer,
288     shared_point,
289     shared_stick,
290     shared_click,
291     NULL,
292     goal_buttn,
293     1, 0
294 };