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