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