no crash in single level when fall out, time out or special goal anymore
[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     char states[MAXLVL+1];
53     
54     if ((fout = fopen(config_user(s->user_scores), "w")))
55     {
56         for (i = 0 ; i < s->count ; i++)
57         {
58             if (level_v[i].is_locked)
59                 states[i] = 'L';
60             else if (level_v[i].is_completed)
61                 states[i] = 'C';
62             else
63                 states[i] = 'O';
64         }
65         states[s->count] = '\0';
66         fprintf(fout, "%s\n",states);
67         
68         put_score(fout, &s->time_score);
69         put_score(fout, &s->coin_score);
70
71         for (i = 0 ; i < s->count ; i++)
72         {
73             l = &level_v[i];
74             put_score(fout, &l->time_score);
75             put_score(fout, &l->goal_score);
76             put_score(fout, &l->coin_score);
77         }
78
79         fclose(fout);
80     }
81 }
82
83 static int get_score(FILE *fp, struct score *s)
84 {
85     int j;
86     int res = 1;
87     for (j = 0; j < NSCORE && res; j++)
88     {
89        res = (fscanf(fp, "%d %d %s\n",
90                &s->timer[j], &s->coins[j], s->player[j])) == 3;
91     }
92     return res;
93 }
94
95 static void set_load_hs(void)
96 /* Get the score of the set */
97 {
98     struct set *s = current_set;
99     FILE *fin;
100     int i;
101     int res = 0;
102     struct level *l;
103     const char *fn = config_user(s->user_scores);
104     char states[MAXLVL+1];
105
106     if ((fin = fopen(fn, "r")))
107     {
108         res = ((fscanf(fin, "%s\n", states) == 1) && 
109                 (strlen(states) == s->count));
110         for (i = 0 ; i < s->count && res ; i++)
111         {
112             if (states[i] == 'L')
113             {
114                 level_v[i].is_locked = 1;
115                 level_v[i].is_completed = 0;
116             }
117             else if (states[i] == 'C')
118             {
119                 level_v[i].is_locked = 0;
120                 level_v[i].is_completed = 1;
121             } 
122             else if (states[i] == 'O')
123             {
124                 level_v[i].is_locked = 0;
125                 level_v[i].is_completed = 0;
126             }
127             else
128                 res = 0;
129         }
130         
131         res = res &&
132             get_score(fin, &s->time_score) &&
133             get_score(fin, &s->coin_score);
134         
135         for (i = 0; i < s->count && res; i++)
136         {
137             l = &level_v[i];
138             res = get_score(fin, &l->time_score) &&
139                   get_score(fin, &l->goal_score) &&
140                   get_score(fin, &l->coin_score);
141         }
142
143         fclose(fin);
144     }
145     
146     if (!res && errno != ENOENT)
147     {
148         fprintf(stderr, _("Error while loading user high-score file '%s': "), fn);
149         if (errno)
150             perror(NULL);
151         else
152             fprintf(stderr, _("Incorrect format\n"));
153     }
154 }
155
156 static char* chomp(char *str)
157 /* Remove trailing \n if any */
158 {
159     char *p = str + strlen(str) - 1;
160     if (p >= str && *p == '\n') *p = 0;
161     return str;
162 }
163
164 static int set_load(struct set *s, const char *filename)
165 /* Count levels */
166 {
167     FILE *fin;
168     char buf[MAXSTR];
169     int res = 0;
170     
171     /* Open the datafile */
172
173     fin = fopen(filename, "r");
174     if (fin == NULL)
175     {
176         fprintf(stderr, _("Cannot load the set file '%s':"), filename);
177         perror(NULL);
178         return 0;
179     }
180
181     /* Raz the set structure */
182
183     memset(s, 0, sizeof(struct set));
184
185     /* Set some sane values in case the scores hs is missing. */
186     
187     score_init_hs(&s->time_score, 359999, 0);
188     score_init_hs(&s->coin_score, 359999, 0);
189     
190     /* Load set metadata */
191     
192     strcpy(s->file, filename);
193     if ((res = fgets(buf, MAXSTR, fin) != NULL))
194         strcpy(s->name, chomp(buf));
195     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
196         strcpy(s->desc, chomp(buf));
197     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
198         strcpy(s->setname, chomp(buf));
199     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
200         strcpy(s->shot, chomp(buf));
201     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
202         sscanf(buf, "%d %d %d %d %d %d", 
203                 &s->time_score.timer[0],
204                 &s->time_score.timer[1],
205                 &s->time_score.timer[2],
206                 &s->coin_score.coins[0],
207                 &s->coin_score.coins[1],
208                 &s->coin_score.coins[2]);
209     strcpy(s->user_scores, "neverballhs-");
210     strcat(s->user_scores, s->setname);
211
212     /* Count levels levels. */
213     
214     s->count = 0;
215
216     while (s->count < MAXLVL && (fscanf(fin, "%s", buf) == 1))
217         s->count++;
218     
219     /* Close the file, since it's no more needed */
220     
221     fclose(fin);
222
223     /* Load the levels states (stored in the user highscore file) */
224     s->locked = s->count;
225     s->completed = 0;
226     if ((fin = fopen(config_user(s->user_scores), "r")))
227     {
228         char states[MAXLVL + 1];
229         int i;
230         if ((fscanf(fin, "%s\n", states) == 1) && (strlen(states) == s->count))
231         {
232             for (i=0; i<s->count; i++)
233             {
234                 if (states[i] == 'O')
235                     s->locked -= 1;
236                 else if (states[i] == 'C')
237                 {
238                     s->completed += 1;
239                     s->locked -= 1;
240                 }
241             }
242         }
243         fclose(fin);
244     }
245     if (s->locked == s->count)
246         s->locked = s->count-1;
247
248     return 1;
249 }
250
251 /*---------------------------------------------------------------------------*/
252
253 void set_init()
254 {
255     FILE *fin;
256     struct set * set;
257     char filename[MAXSTR];
258     int res;
259
260     current_set = NULL;
261     
262     count = 0;
263
264     if ((fin = fopen(config_data(SET_FILE), "r")))
265     {
266         res = 1;
267         while (count < MAXSET && res)
268         {
269             set = &(set_v[count]);
270
271             /* clean the set data */
272             
273             res = (fgets(filename, MAXSTR, fin) != NULL);
274             if (res)
275             {
276                 chomp(filename);
277
278                 res = set_load(set, config_data(filename));
279                 if (res)
280                 {
281                     set->number = count;
282                     count++;
283                 }
284             }
285         }
286
287         fclose(fin);
288     }
289 }
290
291 /*---------------------------------------------------------------------------*/
292
293 int  set_exists(int i)
294 {
295     return (0 <= i && i < count);
296 }
297
298 const struct set *get_set(int i)
299 {
300     return set_exists(i) ? &set_v[i] : NULL;
301 }
302
303 /*---------------------------------------------------------------------------*/
304
305 int  set_unlocked(const struct set *s)
306 /* Are all levels (even extra bonus) unlocked? */
307 {
308     return s->locked == 0;
309 }
310
311 int  set_completed(const struct set *s)
312 /* Are all levels (even extra bonus) completed? */
313 {
314     return s->completed == s->count;
315 }
316
317 int  set_level_exists(const struct set *s, int i)
318 /* Is the level i of the set exists */
319 {
320     return (i >= 0) && (i < s->count);
321 }
322
323 /*---------------------------------------------------------------------------*/
324
325 static void set_load_levels(void)
326 /* Load more the levels of the current set */
327 {
328     FILE *fin;
329     char buf[MAXSTR];
330     char name[MAXSTR];
331     struct level * l;
332
333     int i=0, res;
334     int nb=1, bnb=1;
335     
336     fin = fopen(current_set->file, "r");
337     assert(fin != NULL);
338     
339     res = 1;
340     /* Skip the five first lines */
341     for(i=0; i<5; i++)
342         fgets(buf, MAXSTR, fin);
343     for(i=0; i<current_set->count && res; i++)
344     {
345         l = &level_v[i];
346         res = (fscanf(fin, "%s", name) == 1);
347         assert(res);
348
349         level_load(config_data(name), l);
350
351         /* Initialize set related info */
352         l->set        = current_set;
353         l->number     = i;
354         if (l->is_bonus)
355             sprintf(l->numbername, _("B%d"), bnb++);
356         else
357             sprintf(l->numbername, "%02d", nb++);
358         l->is_locked    = 1;
359         l->is_completed = 0;
360     }
361     level_v[0].is_locked = 0; /* unlock the first level */
362     fclose(fin);
363     assert(i == current_set->count);
364 }
365
366 void set_goto(int i)
367 {
368     assert(set_exists(i));
369     current_set = &set_v[i];
370     set_load_levels();
371     set_load_hs();
372 }
373
374 const struct set *curr_set(void)
375 {
376     return current_set;
377 }
378
379 const struct level *get_level(int i)
380 {
381     return (i>=0 && i<current_set->count) ? &level_v[i] : NULL;
382 }
383
384 /*---------------------------------------------------------------------------*/
385
386 static int score_time_comp(const struct score *S, int i, int j)
387 {
388     if (S->timer[i] <  S->timer[j])
389         return 1;
390
391     if (S->timer[i] == S->timer[j] &&
392         S->coins[i] >  S->coins[j])
393         return 1;
394
395     return 0;
396 }
397
398 static int score_coin_comp(const struct score *S, int i, int j)
399 {
400     if (S->coins[i] >  S->coins[j])
401         return 1;
402
403     if (S->coins[i] == S->coins[j] &&
404         S->timer[i] <  S->timer[j])
405         return 1;
406
407     return 0;
408 }
409
410 static void score_swap(struct score *S, int i, int j)
411 {
412     char player[MAXNAM];
413     int  tmp;
414
415     strncpy(player,       S->player[i], MAXNAM);
416     strncpy(S->player[i], S->player[j], MAXNAM);
417     strncpy(S->player[j], player,       MAXNAM);
418
419     tmp         = S->timer[i];
420     S->timer[i] = S->timer[j];
421     S->timer[j] = tmp;
422
423     tmp         = S->coins[i];
424     S->coins[i] = S->coins[j];
425     S->coins[j] = tmp;
426 }
427
428 static int score_time_insert(struct score *s, const char* player, int timer, int coins)
429 {
430     int i;
431     
432     strncpy(s->player[3], player, MAXNAM);
433     s->timer[3] = timer;
434     s->coins[3] = coins;
435
436     for (i = 2; i >= 0 && score_time_comp(s, i + 1, i); i--)
437         score_swap(s, i + 1, i);
438     return i+1;
439 }
440
441 static int score_coin_insert(struct score *s, const char* player, int timer, int coins)
442 {
443     int i;
444     
445     strncpy(s->player[3], player, MAXNAM);
446     s->timer[3] = timer;
447     s->coins[3] = coins;
448
449     for (i = 2; i >= 0 && score_coin_comp(s, i + 1, i); i--)
450         score_swap(s, i + 1, i);
451     return i+1;
452 }
453
454 static int level_score_update(struct level_game *lg, const char *player)
455 /* Update the level score rank according to coins and timer */
456 {
457     int timer = lg->timer;
458     int coins = lg->coins;
459     struct level * l = &level_v[lg->level->number];
460
461     lg->time_rank = score_time_insert(&l->time_score, player, timer, coins);
462         
463     if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
464         lg->goal_rank = score_time_insert(&l->goal_score, player, timer, coins);
465     else
466         lg->goal_rank = 3;
467
468     lg->coin_rank = score_coin_insert(&l->coin_score, player, timer, coins);
469
470     return (lg->time_rank < 3 || lg->goal_rank < 3 || lg->coin_rank < 3);
471 }
472
473 static int set_score_update(struct level_game *lg, const char *player)
474 /* Update the set score rank according to score and times */
475 {
476     int timer = lg->times;
477     int coins = lg->score;
478     struct set * s = current_set;
479
480     lg->score_rank = score_time_insert(&s->time_score, player, timer, coins);
481     lg->times_rank = score_time_insert(&s->coin_score, player, timer, coins);
482     return (lg->score_rank < 3 || lg->times_rank < 3);
483 }
484
485
486 void score_change_name(struct level_game *lg, const char *player)
487 /* Update the player name for set and level high-score */
488 {
489 #define UPDATE(i, x) (strncpy((x).player[(i)], player, MAXNAM))
490     struct set * s = current_set;
491     struct level *l = &level_v[lg->level->number];
492     UPDATE(lg->time_rank, l->time_score);
493     UPDATE(lg->goal_rank, l->goal_score);
494     UPDATE(lg->coin_rank, l->coin_score);
495     UPDATE(lg->score_rank, s->coin_score);
496     UPDATE(lg->times_rank, s->time_score);
497     set_store_hs();
498 }
499
500 static struct level *next_level(int i)
501 {
502 /* Return the ith level, or NULL */
503     return set_level_exists(current_set, i+1) ? &level_v[i+1] : NULL;
504 }
505
506 static struct level *next_normal_level(int i)
507 /* Return the next notmal level (starting for i) 
508  * Return NULL if there is not a such level */
509 {
510     for (i++; i < current_set->count; i++)
511         if (!level_v[i].is_bonus)
512             return &level_v[i];
513     return NULL;
514 }
515
516 void set_finish_level(struct level_game *lg, const char *player)
517 /* Inform the set that a level is finished. 
518  * Update next_level and score rank fields */
519 {
520     struct set *s = current_set;
521     int ln = lg->level->number;     /* curent level number */
522     struct level *cl = &level_v[ln]; /* current level */
523     struct level *nl = NULL;        /* next level*/
524     int dirty = 0;                  /* HS should be saved? */
525
526     assert(s == cl->set);
527
528         /* if no set, no next level */    
529         if (s == NULL)
530         {
531             /* if no set, return */
532             lg->next_level = NULL;
533             return;
534         }
535
536     /* On level completed */
537     if (lg->state == GAME_GOAL)
538     {       
539         /* Update level scores */
540         dirty = level_score_update(lg, player);
541         
542         /* Complete the level */
543         if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
544         {
545             /* Complete the level */
546             if (!cl->is_completed)
547             {
548                 cl->is_completed = 1;
549                 s->completed += 1;
550                 dirty = 1;
551             }
552         }
553     }
554
555     /* On goal reached */
556     if (lg->state == GAME_GOAL || lg->state == GAME_SPEC)
557     {
558         /* Identify the following level */
559         nl = next_level(ln + lg->state_value);
560         if (nl != NULL)
561         {
562             /* skip bonuses if unlocked in non challenge mode*/
563             if(nl->is_bonus && nl->is_locked && lg->mode != MODE_CHALLENGE)
564                 nl = next_normal_level(nl->number);
565         }
566         else if (lg->mode == MODE_CHALLENGE)
567             lg->win = 1;
568     }
569     else if (cl->is_bonus || lg->mode != MODE_CHALLENGE)
570     {
571         /* On fail, identify the next level (only in bonus for challenge) */
572         nl = next_normal_level(ln);
573         /* Next level may be unavailable */
574         if (!cl->is_bonus && nl != NULL && nl->is_locked)
575             nl = NULL;
576         /* Fail a bonus level but win the set! */
577         else if (nl == NULL && lg->mode == MODE_CHALLENGE)
578             lg->win = 1;
579     }
580
581     /* Win ! */
582     if (lg->win)
583     {
584         /* update set score */
585         set_score_update(lg, player);
586         /* unlock all levels */
587         set_cheat();
588         dirty = 1;
589     }
590    
591     /* unlock the next level if needed */
592     if(nl != NULL && nl->is_locked)
593     {
594         if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
595         {
596             lg->unlock = 1;
597             nl->is_locked = 0;
598             s->locked -= 1;
599             dirty = 1;
600         }
601         else
602             nl = NULL;
603     }
604     
605     /* got the next level */ 
606     lg->next_level = nl;
607
608     /* Update file */
609     if (dirty)
610         set_store_hs();
611 }
612
613 /*---------------------------------------------------------------------------*/
614
615 void level_snap(int i)
616 {
617     char filename[MAXSTR];
618
619     /* Convert the level name to a BMP filename. */
620
621     memset(filename, 0, MAXSTR);
622     strncpy(filename, level_v[i].file, strcspn(level_v[i].file, "."));
623     strcat(filename, ".bmp");
624
625     /* Initialize the game for a snapshot. */
626
627     if (game_init(&level_v[i], 0, 0))
628     {
629         /* Render the level and grab the screen. */
630
631         config_clear();
632         game_set_fly(1.f);
633         game_kill_fade();
634         game_draw(1, 0);
635         SDL_GL_SwapBuffers();
636
637         image_snap(filename, config_get_d(CONFIG_WIDTH), config_get_d(CONFIG_HEIGHT));
638     }
639 }
640
641 void set_cheat(void)
642 /* Open each level of the current set */
643 {
644     int i;
645     current_set->locked = 0;
646     for (i=0; i < current_set->count; i++)
647         level_v[i].is_locked = 0;
648 }
649
650
651 /*---------------------------------------------------------------------------*/
652