proof of concept, direct play level with the --level option (I hope that only replays...
[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 #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 high;
45 static int time_i;
46 static int coin_i;
47     
48 static int balls_id;
49 static int coins_id;
50 static int score_id;
51
52 extern struct state st_goal_bis;
53
54 static int goal_action(int i)
55 {
56     audio_play(AUD_MENU, 1.0f);
57
58     switch (i)
59     {
60     case GOAL_BACK:
61         return goto_end_level();
62
63     case GOAL_SAVE:
64         return goto_save(&st_goal_bis, &st_goal_bis);
65
66     case GOAL_NAME:
67         return goto_name(&st_goal_bis, &st_goal_bis);
68         
69     case GOAL_DONE:
70         return goto_state(&st_done);
71         
72     case GOAL_NEXT:
73         level_next();
74         return goto_state(&st_level);
75
76     case GOAL_SAME:
77         return goto_state(&st_level);
78     }
79
80     return 1;
81 }
82
83 static int goal_init(int * gidp)
84 {
85     const char *s1 = _("New Record");
86     const char *s2 = _("GOAL");
87
88     int id, jd, kd;
89
90     if ((id = gui_vstack(0)))
91     {
92         int gid;
93
94         if (high)
95             gid = gui_label(id, s1, GUI_MED, GUI_ALL, gui_grn, gui_grn);
96         else
97             gid = gui_label(id, s2, GUI_LRG, GUI_ALL, gui_blu, gui_grn);
98
99         gui_space(id);
100
101         if (level_mode() == MODE_CHALLENGE)
102         {
103             int coins = curr_coins();
104             int score = curr_score() - coins;
105             int balls = curr_balls() - count_extra_balls(score, coins);
106             if ((jd = gui_hstack(id)))
107             {
108                 if ((kd = gui_harray(jd)))
109                 {
110                     balls_id = gui_count(kd,  100, GUI_MED, GUI_RGT);
111                     gui_label(kd, _("Balls"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
112                 }
113                 if ((kd = gui_harray(jd)))
114                 {
115                     score_id = gui_count(kd, 1000, GUI_MED, GUI_RGT);
116                     gui_label(kd, _("Score"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
117                 }
118                 if ((kd = gui_harray(jd)))
119                 {
120                     coins_id = gui_count(kd, 100, GUI_MED, GUI_RGT);
121                     gui_label(kd, _("Coins"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
122                 }
123
124                 gui_set_count(balls_id, balls);
125                 gui_set_count(score_id, score);
126                 gui_set_count(coins_id, coins);
127             }
128         }
129         
130         gui_space(id);
131
132         if ((jd = gui_harray(id)))
133         {
134             gui_most_coins(jd, 3, 1);
135             gui_best_times(jd, 3, 1);
136         }
137
138         gui_space(id);
139
140         if ((jd = gui_harray(id)))
141         {
142             if (demo_play_saved())
143                 gui_state(jd, _("Save Replay"), GUI_SML, GOAL_SAVE, 0);
144             else
145                 gui_label(jd, _("Save Replay"), GUI_SML, GUI_ALL, gui_blk, gui_blk);
146             
147             if (level_mode() != MODE_CHALLENGE)
148                 gui_start(jd, _("Retry Level"), GUI_SML, GOAL_SAME, 0);
149             
150             if (level_mode() == MODE_CHALLENGE && level_last())
151                 gui_start(jd, _("Finish"),      GUI_SML, GOAL_DONE, 0);
152             else if (level_opened(curr_level()+1))
153                 gui_state(jd, _("Next Level"),  GUI_SML, GOAL_NEXT, 0);
154             else if (level_mode() != MODE_SINGLE)
155                 gui_label(jd, _("Next Level"),  GUI_SML, GUI_ALL, gui_blk, gui_blk);
156         }
157
158         if (high)
159             gui_state(id, _("Change Player Name"),  GUI_SML, GOAL_NAME, 0);
160
161         gui_layout(id, 0, 0);
162         if (gidp) *gidp = gid;
163     }
164
165     set_most_coins(curr_level(), coin_i);
166     set_best_times(curr_level(), time_i);
167
168     config_clr_grab();
169
170     return id;
171 }
172
173 static int goal_enter(void)
174 {
175     int gid;
176     int r;
177     
178     time_i = 3;
179     coin_i = 3;
180     high   = level_sort(&time_i, &coin_i);
181
182     r = goal_init(&gid);
183     
184     gui_pulse(gid, 1.2f);
185     audio_music_fade_out(2.0f);
186     return r; 
187 }
188
189 static int goal_bis_enter(void)
190 {
191     char player[MAXNAM];
192     config_get_s(CONFIG_PLAYER, player, MAXNAM);
193     level_name(curr_level(), player, time_i, coin_i);
194     return goal_init(NULL);
195 }
196
197 static void goal_timer(int id, float dt)
198 {
199     static float DT = 0.0f;
200
201     float g[3] = { 0.0f, 9.8f, 0.0f };
202
203     DT += dt;
204
205     if (time_state() < 1.f)
206         game_step(g, dt, 0);
207     else if (DT > 0.05f && level_mode() == MODE_CHALLENGE)
208     {
209         int coins = gui_value(coins_id);
210         if (coins > 0)
211         {
212             int score = gui_value(score_id);
213             int balls = gui_value(balls_id);
214
215             gui_set_count(coins_id, coins - 1);
216             gui_pulse(coins_id, 1.1f);
217             
218             gui_set_count(score_id, score + 1);
219             gui_pulse(score_id, 1.1f);
220             if ((score+1) % 100 == 0)
221             {
222                 gui_set_count(balls_id, balls + 1);
223                 gui_pulse(balls_id, 2.0f);
224                 audio_play(AUD_BALL, 1.0f);
225             }
226         }
227
228         DT = 0.0f;
229     }
230
231     gui_timer(id, dt);
232     audio_timer(dt);
233 }
234
235 static int goal_buttn(int b, int d)
236 {
237     if (d)
238     {
239         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
240             return goal_action(gui_token(gui_click()));
241         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
242             return goal_action(GOAL_BACK);
243     }
244     return 1;
245 }
246
247 /*---------------------------------------------------------------------------*/
248
249 struct state st_goal = {
250     goal_enter,
251     shared_leave,
252     shared_paint,
253     goal_timer,
254     shared_point,
255     shared_stick,
256     shared_click,
257     NULL,
258     goal_buttn,
259     1, 0
260 };
261
262 struct state st_goal_bis = {
263     goal_bis_enter,
264     shared_leave,
265     shared_paint,
266     shared_timer,
267     shared_point,
268     shared_stick,
269     shared_click,
270     NULL,
271     goal_buttn,
272     1, 0
273 };