Bonus level info is now in the level (medatada field)
[neverball] / ball / set.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 <string.h>
17 #include <assert.h>
18 #include <errno.h>
19
20 #include "glext.h"
21 #include "config.h"
22 #include "image.h"
23 #include "set.h"
24 #include "game.h"
25
26 /*---------------------------------------------------------------------------*/
27
28 static int count;                    /* number of sets */
29
30 static struct set set_v[MAXSET];     /* array of sets */
31
32 static struct set *current_set;      /* currently selected set */
33
34 static struct level level_v[MAXLVL]; /* levels of the current set  */
35
36 /*---------------------------------------------------------------------------*/
37
38 static void put_score(FILE *fp, const struct score *s)
39 {
40     int j;
41     for (j = 0; j < NSCORE; j++)
42        fprintf(fp, "%d %d %s\n", s->timer[j], s->coins[j], s->player[j]);
43 }
44
45 static void set_store_hs(void)
46 /* Store the score of the set */
47 {
48     const struct set *s = current_set;
49     FILE *fout;
50     int i;
51     const struct level *l;
52     
53     if ((fout = fopen(config_user(s->user_scores), "w")))
54     {
55         int code = 0;
56         for (i = s->count - 1 ; i >=0 ; i--)
57         {
58             code <<= 1;
59             if (!level_v[i].is_locked)
60                  code |= 1;
61         }
62         fprintf(fout, "%d\n", code);
63         
64         put_score(fout, &s->time_score);
65         put_score(fout, &s->coin_score);
66
67         for (i = 0 ; i < s->count ; i++)
68         {
69             l = &level_v[i];
70             put_score(fout, &l->time_score);
71             put_score(fout, &l->goal_score);
72             put_score(fout, &l->coin_score);
73         }
74
75         fclose(fout);
76     }
77 }
78
79 static int get_score(FILE *fp, struct score *s)
80 {
81     int j;
82     int res = 1;
83     for (j = 0; j < NSCORE && res; j++)
84     {
85        res = (fscanf(fp, "%d %d %s\n",
86                &s->timer[j], &s->coins[j], s->player[j])) == 3;
87     }
88     return res;
89 }
90
91 static void set_load_hs(void)
92 /* Get the score of the set */
93 {
94     struct set *s = current_set;
95     FILE *fin;
96     int i;
97     int res = 0;
98     struct level *l;
99     const char *fn = config_user(s->user_scores);
100
101     if ((fin = fopen(fn, "r")))
102     {
103         int code = 0;
104         res = (fscanf(fin, "%d\n", &code) == 1);
105         for (i = 0 ; i < s->count ; i++)
106         {
107             if (code & 1)
108                 level_v[i].is_locked = 0;
109             code >>= 1;
110         }
111         res = res && (code != 0) &&
112             get_score(fin, &s->time_score) &&
113             get_score(fin, &s->coin_score);
114         
115         for (i = 0; i <= s->count && res; i++)
116         {
117             l = &level_v[i];
118             res = get_score(fin, &l->time_score) &&
119                   get_score(fin, &l->goal_score) &&
120                   get_score(fin, &l->coin_score);
121         }
122
123         fclose(fin);
124     }
125     
126     if (!res && errno != ENOENT)
127     {
128         fprintf(stderr, _("Error while loading user high-score file '%s': "), fn);
129         if (errno)
130             perror(NULL);
131         else
132             fprintf(stderr, _("Incorrect format\n"));
133     }
134 }
135
136 static char* chomp(char *str)
137 /* Remove trailing \n if any */
138 {
139     char *p = str + strlen(str) - 1;
140     if (p >= str && *p == '\n') *p = 0;
141     return str;
142 }
143
144 static int set_load(struct set *s, const char *filename)
145 /* Count levels */
146 {
147     FILE *fin;
148     char buf[MAXSTR];
149     int res = 0;
150     
151     /* Open the datafile */
152
153     fin = fopen(filename, "r");
154     if (fin == NULL)
155     {
156         fprintf(stderr, _("Cannot load the set file '%s':"), filename);
157         perror(NULL);
158         return 0;
159     }
160
161     /* Raz the set structure */
162
163     memset(s, 0, sizeof(struct set));
164
165     /* Set some sane values in case the scores hs is missing. */
166     
167     score_init_hs(&s->time_score, 359999, 0);
168     score_init_hs(&s->coin_score, 359999, 0);
169     
170     /* Load set metadata */
171     
172     strcpy(s->file, filename);
173     if ((res = fgets(buf, MAXSTR, fin) != NULL))
174         strcpy(s->name, chomp(buf));
175     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
176         strcpy(s->desc, chomp(buf));
177     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
178         strcpy(s->setname, chomp(buf));
179     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
180         strcpy(s->shot, chomp(buf));
181     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
182         sscanf(buf, "%d %d %d %d %d %d", 
183                 &s->time_score.timer[0],
184                 &s->time_score.timer[1],
185                 &s->time_score.timer[2],
186                 &s->coin_score.coins[0],
187                 &s->coin_score.coins[1],
188                 &s->coin_score.coins[2]);
189     strcpy(s->user_scores, "neverballhs-");
190     strcat(s->user_scores, s->setname);
191
192     /* Count levels levels. */
193     
194     s->count = 0;
195
196     while (s->count < MAXLVL && fgets(buf, MAXSTR, fin))
197         s->count++;
198     
199     /* Close the file, since it's no more needed */
200     
201     fclose(fin);
202
203     /* Load the level limit (stored in the user highscore file) */
204    /* 
205     s->limit = 0;
206     
207     if ((fin = fopen(config_user(s->user_scores), "r")))
208     {
209         fscanf(fin, "%d\n", &s->limit);
210         if (s->limit > s->count)
211             s->limit = 0;
212         fclose(fin);
213     }*/
214
215     return 1;
216 }
217
218 /*---------------------------------------------------------------------------*/
219
220 void set_init()
221 {
222     FILE *fin;
223     struct set * set;
224     char filename[MAXSTR];
225     int res;
226
227     current_set = NULL;
228     
229     count = 0;
230
231     if ((fin = fopen(config_data(SET_FILE), "r")))
232     {
233         res = 1;
234         while (count < MAXSET && res)
235         {
236             set = &(set_v[count]);
237
238             /* clean the set data */
239             
240             res = (fgets(filename, MAXSTR, fin) != NULL);
241             if (res)
242             {
243                 chomp(filename);
244
245                 res = set_load(set, config_data(filename));
246                 if (res)
247                 {
248                     set->number = count;
249                     count++;
250                 }
251             }
252         }
253
254         fclose(fin);
255     }
256 }
257
258 /*---------------------------------------------------------------------------*/
259
260 int  set_exists(int i)
261 {
262     return (0 <= i && i < count);
263 }
264
265 const struct set *get_set(int i)
266 {
267     return set_exists(i) ? &set_v[i] : NULL;
268 }
269
270 /*---------------------------------------------------------------------------*/
271
272 int  set_completed(const struct set *s)
273 /* Are all levels (even extra bonus) completed? */
274 {
275     return 0; /*s->limit >= s->count;*/
276 }
277
278 int  set_level_exists(const struct set *s, int i)
279 /* Is the level i of the set exists */
280 {
281     return (i >= 0) && (i < s->count);
282 }
283
284 /*---------------------------------------------------------------------------*/
285
286 static void set_load_levels(void)
287 /* Load more the levels of the current set */
288 {
289     FILE *fin;
290     char buf[MAXSTR];
291     char name[MAXSTR];
292     struct level * l;
293
294     int i=0, res;
295     int nb=1, bnb=1;
296     
297     fin = fopen(current_set->file, "r");
298     assert(fin != NULL);
299     
300     res = 1;
301     /* Skip the five first lines */
302     for(i=0; i<5; i++)
303         fgets(buf, MAXSTR, fin);
304     for(i=0; i<current_set->count && res; i++)
305     {
306         l = &level_v[i];
307         res = (fgets(buf, MAXSTR, fin) != NULL) &&
308             (sscanf(buf, "%s", name) == 1);
309         assert(res);
310
311         level_load(config_data(name), l);
312
313         /* Initialize set related info */
314         l->set        = current_set;
315         l->number     = i;
316         if (l->is_bonus)
317             sprintf(l->numbername, "B%d", bnb++);
318         else
319             sprintf(l->numbername, "%02d", nb++);
320         l->is_locked  = 1; /*i > current_set->limit;*/
321     }   
322     fclose(fin);
323     assert(i == current_set->count);
324 }
325
326 void set_goto(int i)
327 {
328     assert(set_exists(i));
329     current_set = &set_v[i];
330     set_load_levels();
331     set_load_hs();
332 }
333
334 const struct set *curr_set(void)
335 {
336     return current_set;
337 }
338
339 const struct level *get_level(int i)
340 {
341     return (i>=0 && i<current_set->count) ? &level_v[i] : NULL;
342 }
343
344 /*---------------------------------------------------------------------------*/
345
346 static int score_time_comp(const struct score *S, int i, int j)
347 {
348     if (S->timer[i] <  S->timer[j])
349         return 1;
350
351     if (S->timer[i] == S->timer[j] &&
352         S->coins[i] >  S->coins[j])
353         return 1;
354
355     return 0;
356 }
357
358 static int score_coin_comp(const struct score *S, int i, int j)
359 {
360     if (S->coins[i] >  S->coins[j])
361         return 1;
362
363     if (S->coins[i] == S->coins[j] &&
364         S->timer[i] <  S->timer[j])
365         return 1;
366
367     return 0;
368 }
369
370 static void score_swap(struct score *S, int i, int j)
371 {
372     char player[MAXNAM];
373     int  tmp;
374
375     strncpy(player,       S->player[i], MAXNAM);
376     strncpy(S->player[i], S->player[j], MAXNAM);
377     strncpy(S->player[j], player,       MAXNAM);
378
379     tmp         = S->timer[i];
380     S->timer[i] = S->timer[j];
381     S->timer[j] = tmp;
382
383     tmp         = S->coins[i];
384     S->coins[i] = S->coins[j];
385     S->coins[j] = tmp;
386 }
387
388 static int score_time_insert(struct score *s, const char* player, int timer, int coins)
389 {
390     int i;
391     
392     strncpy(s->player[3], player, MAXNAM);
393     s->timer[3] = timer;
394     s->coins[3] = coins;
395
396     for (i = 2; i >= 0 && score_time_comp(s, i + 1, i); i--)
397         score_swap(s, i + 1, i);
398     return i+1;
399 }
400
401 static int score_coin_insert(struct score *s, const char* player, int timer, int coins)
402 {
403     int i;
404     
405     strncpy(s->player[3], player, MAXNAM);
406     s->timer[3] = timer;
407     s->coins[3] = coins;
408
409     for (i = 2; i >= 0 && score_coin_comp(s, i + 1, i); i--)
410         score_swap(s, i + 1, i);
411     return i+1;
412 }
413
414 static int level_score_update(struct level_game *lg, const char *player)
415 /* Update the level score rank according to coins and timer */
416 {
417     int timer = lg->timer;
418     int coins = lg->coins;
419     struct level * l = &level_v[lg->level->number];
420
421     lg->time_rank = score_time_insert(&l->time_score, player, timer, coins);
422         
423     if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
424         lg->goal_rank = score_time_insert(&l->goal_score, player, timer, coins);
425     else
426         lg->goal_rank = 3;
427
428     lg->coin_rank = score_coin_insert(&l->coin_score, player, timer, coins);
429
430     return (lg->time_rank < 3 || lg->goal_rank < 3 || lg->coin_rank < 3);
431 }
432
433 static int set_score_update(struct level_game *lg, const char *player)
434 /* Update the set score rank according to score and times */
435 {
436     int timer = lg->times;
437     int coins = lg->score;
438     struct set * s = current_set;
439
440     lg->score_rank = score_time_insert(&s->time_score, player, timer, coins);
441     lg->times_rank = score_time_insert(&s->coin_score, player, timer, coins);
442     return (lg->score_rank < 3 || lg->times_rank < 3);
443 }
444
445
446 void score_change_name(struct level_game *lg, const char *player)
447 /* Update the player name for set and level high-score */
448 {
449 #define UPDATE(i, x) (strncpy((x).player[(i)], player, MAXNAM))
450     struct set * s = current_set;
451     struct level *l = &level_v[lg->level->number];
452     UPDATE(lg->time_rank, l->time_score);
453     UPDATE(lg->goal_rank, l->goal_score);
454     UPDATE(lg->coin_rank, l->coin_score);
455     UPDATE(lg->score_rank, s->coin_score);
456     UPDATE(lg->times_rank, s->time_score);
457     set_store_hs();
458 }
459
460 void set_finish_level(struct level_game *lg, const char *player)
461 /* Inform the set that a level is finished. 
462  * Update next_level and score rank fields */
463 {
464     struct set *s = current_set;
465     int level = lg->level->number;
466     int dirty = 0;
467     
468     /* Update scores */
469     dirty = level_score_update(lg, player);
470     dirty = set_score_update(lg, player) || dirty;
471     
472     /* compute the next level */    
473     if (s == NULL)
474     {
475         /* if no set, return */
476         lg->next_level = NULL;
477         return;
478     }
479    
480     level++; /* level is the next level */
481     
482     /* if the next level is not oppened */
483     if (level_v[level].is_locked)
484         if ((lg->mode == MODE_CHALLENGE) ||
485                 (lg->mode == MODE_NORMAL && (level < 20 || level > 20)))
486         {
487             level_v[level].is_locked = 0;
488             s->limit = level;
489             dirty = 1;
490         }      
491    
492     /* got the next level */ 
493     if (lg->mode == MODE_CHALLENGE && level >= 20)
494         lg->next_level = NULL; /* End the challenge */
495     else if (level < s->count && !level_v[level].is_locked)
496         lg->next_level = &level_v[level];
497     else
498         lg->next_level = NULL;
499
500     /* Update file */
501     if (dirty)
502         set_store_hs();
503 }
504
505 /*---------------------------------------------------------------------------*/
506
507 void level_snap(int i)
508 {
509     char filename[MAXSTR];
510
511     /* Convert the level name to a BMP filename. */
512
513     memset(filename, 0, MAXSTR);
514     strncpy(filename, level_v[i].file, strcspn(level_v[i].file, "."));
515     strcat(filename, ".bmp");
516
517     /* Initialize the game for a snapshot. */
518
519     if (game_init(&level_v[i], 0, 0))
520     {
521         /* Render the level and grab the screen. */
522
523         config_clear();
524         game_set_fly(1.f);
525         game_kill_fade();
526         game_draw(1, 0);
527         SDL_GL_SwapBuffers();
528
529         image_snap(filename, config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT));
530     }
531 }
532
533 void set_cheat(void)
534 /* Open each level of the current set */
535 {
536     int i;
537     for (i=0; i < current_set->count; i++)
538         level_v[i].is_locked = 0;
539 }
540
541
542 /*---------------------------------------------------------------------------*/
543