Moved most of ball/levels module functions back to ball/level. Not sure why
[neverball] / ball / level.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 <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <math.h>
19 #include <errno.h>
20
21 #include "config.h"
22 #include "demo.h"
23 #include "level.h"
24 #include "mode.h"
25 #include "set.h"
26 #include "solid.h"
27
28 /*---------------------------------------------------------------------------*/
29
30 static void scan_dict(struct level *l, const struct s_file *fp)
31 {
32     int i;
33
34     for (i = 0; i < fp->dc; i++)
35     {
36         char *k = fp->av + fp->dv[i].ai;
37         char *v = fp->av + fp->dv[i].aj;
38
39         if (strcmp(k, "message") == 0)
40             strncpy(l->message, v, MAXSTR);
41         else if (strcmp(k, "back") == 0)
42             strncpy(l->back, v, PATHMAX);
43         else if (strcmp(k, "song") == 0)
44             strncpy(l->song, v, PATHMAX);
45         else if (strcmp(k, "grad") == 0)
46             strncpy(l->grad, v, PATHMAX);
47         else if (strcmp(k, "shot") == 0)
48             strncpy(l->shot, v, PATHMAX);
49         else if (strcmp(k, "goal") == 0)
50         {
51             l->goal = atoi(v);
52             l->score.most_coins.coins[2] = l->goal;
53         }
54         else if (strcmp(k, "time") == 0)
55         {
56             l->time = atoi(v);
57             l->score.best_times.timer[2] = l->time;
58             l->score.unlock_goal.timer[2] = l->time;
59         }
60         else if (strcmp(k, "time_hs") == 0)
61             sscanf(v, "%d %d",
62                    &l->score.best_times.timer[0],
63                    &l->score.best_times.timer[1]);
64         else if (strcmp(k, "goal_hs") == 0)
65             sscanf(v, "%d %d",
66                    &l->score.unlock_goal.timer[0],
67                    &l->score.unlock_goal.timer[1]);
68         else if (strcmp(k, "coin_hs") == 0)
69             sscanf(v, "%d %d",
70                    &l->score.most_coins.coins[0],
71                    &l->score.most_coins.coins[1]);
72         else if (strcmp(k, "version") == 0)
73             strncpy(l->version, v, MAXSTR);
74         else if (strcmp(k, "author") == 0)
75             strncpy(l->author, v, MAXSTR);
76         else if (strcmp(k, "bonus") == 0)
77             l->is_bonus = atoi(v) ? 1 : 0;
78     }
79 }
80
81 int level_load(const char *filename, struct level *level)
82 {
83     struct s_file sol;
84
85     int money;
86     int i;
87
88     memset(level, 0, sizeof (struct level));
89     memset(&sol,  0, sizeof (sol));
90
91     /* Try to load the sol file */
92     if (!sol_load_only_file(&sol, config_data(filename)))
93     {
94         fprintf(stderr,
95                 _("Error while loading level file '%s': %s\n"), filename,
96                 errno ? strerror(errno) : _("Not a valid level file"));
97         return 0;
98     }
99
100     strcpy(level->file, filename);
101
102     /* Init hs with default values */
103     score_init_hs(&level->score.best_times, 59999, 0);
104     score_init_hs(&level->score.unlock_goal, 59999, 0);
105     score_init_hs(&level->score.most_coins, 59999, 0);
106
107     /* Compute money and default max money */
108     money = 0;
109     for (i = 0; i < sol.hc; i++)
110         if (sol.hv[i].t == ITEM_COIN)
111             money += sol.hv[i].n;
112     level->score.most_coins.coins[0] = money;
113
114     if (sol.dc > 0)
115         scan_dict(level, &sol);
116
117     /* Compute initial hs default values */
118
119 #define HOP(t, c) \
120     if (t[2] c t[0]) \
121         t[0] = t[1] = t[2]; \
122     else if (t[2] c t[1]) \
123         t[1] = (t[0] + t[2]) / 2
124
125     HOP(level->score.best_times.timer, <=);
126     HOP(level->score.unlock_goal.timer, <=);
127     HOP(level->score.most_coins.coins, >=);
128
129     sol_free(&sol);
130
131     return 1;
132 }
133
134 void level_dump(const struct level *l)
135 {
136     printf("filename:        %s\n"
137            "version:         %s\n"
138            "author:          %s\n"
139            "time limit:      %d\n"
140            "goal count:      %d\n"
141            "time hs:         %d %d %d\n"
142            "goal hs:         %d %d %d\n"
143            "coin hs:         %d %d %d\n"
144            "message:         %s\n"
145            "background:      %s\n"
146            "gradient:        %s\n"
147            "screenshot:      %s\n"
148            "song:            %s\n",
149            l->file,
150            l->version,
151            l->author,
152            l->time,
153            l->goal,
154            l->score.best_times.timer[0],
155            l->score.best_times.timer[1],
156            l->score.best_times.timer[2],
157            l->score.unlock_goal.timer[0],
158            l->score.unlock_goal.timer[1],
159            l->score.unlock_goal.timer[2],
160            l->score.most_coins.coins[0],
161            l->score.most_coins.coins[1],
162            l->score.most_coins.coins[2],
163            l->message,
164            l->back,
165            l->grad,
166            l->shot,
167            l->song);
168 }
169
170 /*---------------------------------------------------------------------------*/
171
172 int level_replay(const char *filename)
173 {
174     return demo_replay_init(filename, curr_lg());
175 }
176
177 int level_play(const struct level *l, int m)
178 {
179     struct level_game *lg = curr_lg();
180
181     memset(lg, 0, sizeof (struct level_game));
182
183     lg->mode  = m;
184     lg->level = l;
185     lg->balls = 3;
186
187     lg->goal = (lg->mode == MODE_PRACTICE) ? 0 : lg->level->goal;
188     lg->time = (lg->mode == MODE_PRACTICE) ? 0 : lg->level->time;
189
190     /* Clear other fields. */
191
192     lg->status = GAME_NONE;
193     lg->coins = 0;
194     lg->timer = lg->time;
195     lg->coin_rank = lg->goal_rank = lg->time_rank =
196         lg->score_rank = lg->times_rank = 3;
197
198     lg->win = lg->dead = lg->unlock = 0;
199     lg->next_level = NULL;
200
201     return demo_play_init(USER_REPLAY_FILE, lg->level, lg);
202 }
203
204 void level_stat(int status, int clock, int coins)
205 {
206     struct level_game *lg = curr_lg();
207
208     int mode = lg->mode;
209     int timer = (mode == MODE_PRACTICE) ? clock : lg->time - clock;
210
211     char player[MAXNAM];
212
213     config_get_s(CONFIG_PLAYER, player, MAXNAM);
214
215     lg->status = status;
216     lg->coins = coins;
217     lg->timer = timer;
218
219     if (mode == MODE_CHALLENGE)
220     {
221         /* sum time */
222         lg->times += timer;
223
224         /* sum coins an earn extra balls */
225         if (status == GAME_GOAL || lg->level->is_bonus)
226         {
227             lg->balls += count_extra_balls(lg->score, coins);
228             lg->score += coins;
229         }
230
231         /* lose ball and game */
232         else
233         {
234             lg->dead = (lg->balls <= 0);
235             lg->balls--;
236         }
237     }
238
239     set_finish_level(lg, player);
240
241     demo_play_stat(lg);
242 }
243
244 void level_stop(void)
245 {
246     demo_play_stop();
247 }
248
249 int level_next(void)
250 {
251     struct level_game *lg = curr_lg();
252
253     level_stop();
254     lg->level = lg->next_level;
255     return level_play(lg->level, lg->mode);
256 }
257
258 int level_same(void)
259 {
260     level_stop();
261     return level_play(curr_lg()->level, curr_lg()->mode);
262 }
263
264 /*---------------------------------------------------------------------------*/
265
266 int count_extra_balls(int old_score, int coins)
267 {
268     return ((old_score % 100) + coins) / 100;
269 }
270
271 void level_update_player_name(void)
272 {
273     char player[MAXNAM];
274
275     config_get_s(CONFIG_PLAYER, player, MAXNAM);
276
277     score_change_name(curr_lg(), player);
278 }
279
280 /*---------------------------------------------------------------------------*/
281
282 const char *status_to_str(int m)
283 {
284     switch (m)
285     {
286     case GAME_NONE:    return _("Aborted");
287     case GAME_TIME:    return _("Time-out");
288     case GAME_GOAL:    return _("Success");
289     case GAME_FALL:    return _("Fall-out");
290     default:           return _("Unknown");
291     }
292 }
293
294 /*---------------------------------------------------------------------------*/