Minor code clean-up.
[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': "),
149                 fn);
150         if (errno)
151             perror(NULL);
152         else
153             fprintf(stderr, _("Incorrect format\n"));
154     }
155 }
156
157 static char *chomp(char *str)
158 /* Remove trailing \n if any */
159 {
160     char *p = str + strlen(str) - 1;
161     if (p >= str && *p == '\n') *p = 0;
162     return str;
163 }
164
165 static int set_load(struct set *s, const char *filename)
166 {
167     FILE *fin;
168
169     char buf[MAXSTR];
170     int res;
171
172     fin = fopen(config_data(filename), "r");
173     if (!fin)
174     {
175         fprintf(stderr, _("Cannot load the set file '%s':"), filename);
176         perror(NULL);
177         return 0;
178     }
179
180     memset(s, 0, sizeof (struct set));
181
182     /* Set some sane values in case the scores hs is missing. */
183
184     score_init_hs(&s->time_score, 359999, 0);
185     score_init_hs(&s->coin_score, 359999, 0);
186
187     /* Load set metadata. */
188
189     strcpy(s->file, config_data(filename));
190
191     if ((res = fgets(buf, MAXSTR, fin) != NULL))
192         strcpy(s->name, chomp(buf));
193     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
194         strcpy(s->desc, chomp(buf));
195     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
196         strcpy(s->setname, chomp(buf));
197     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
198         strcpy(s->shot, chomp(buf));
199     if (res && (res = fgets(buf, MAXSTR, fin) != NULL))
200         sscanf(buf, "%d %d %d %d %d %d",
201                 &s->time_score.timer[0],
202                 &s->time_score.timer[1],
203                 &s->time_score.timer[2],
204                 &s->coin_score.coins[0],
205                 &s->coin_score.coins[1],
206                 &s->coin_score.coins[2]);
207
208     strcpy(s->user_scores, "neverballhs-");
209     strcat(s->user_scores, s->setname);
210
211     /* Count levels. */
212
213     s->count = 0;
214
215     while (s->count < MAXLVL && (fscanf(fin, "%s", buf) == 1))
216         s->count++;
217
218     fclose(fin);
219
220     /* Load the levels states (stored in the user highscore file) */
221
222     s->locked = s->count;
223     s->completed = 0;
224
225     if ((fin = fopen(config_user(s->user_scores), "r")))
226     {
227         char states[MAXLVL + 1];
228         int i;
229         if ((fscanf(fin, "%s\n", states) == 1) && (strlen(states) == s->count))
230         {
231             for (i = 0; i < s->count; i++)
232             {
233                 if (states[i] == 'O')
234                     s->locked -= 1;
235                 else if (states[i] == 'C')
236                 {
237                     s->completed += 1;
238                     s->locked -= 1;
239                 }
240             }
241         }
242         fclose(fin);
243     }
244     if (s->locked == s->count)
245         s->locked = s->count-1;
246
247     return 1;
248 }
249
250 /*---------------------------------------------------------------------------*/
251
252 void set_init()
253 {
254     struct set *set;
255     FILE *fin;
256
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             res = (fgets(filename, MAXSTR, fin) != NULL);
271
272             if (res)
273             {
274                 chomp(filename);
275
276                 res = set_load(set, filename);
277                 if (res)
278                 {
279                     set->number = count;
280                     count++;
281                 }
282             }
283         }
284
285         fclose(fin);
286     }
287 }
288
289 /*---------------------------------------------------------------------------*/
290
291 int  set_exists(int i)
292 {
293     return (0 <= i && i < count);
294 }
295
296 const struct set *get_set(int i)
297 {
298     return set_exists(i) ? &set_v[i] : NULL;
299 }
300
301 /*---------------------------------------------------------------------------*/
302
303 int  set_unlocked(const struct set *s)
304 /* Are all levels (even extra bonus) unlocked? */
305 {
306     return s->locked == 0;
307 }
308
309 int  set_completed(const struct set *s)
310 /* Are all levels (even extra bonus) completed? */
311 {
312     return s->completed == s->count;
313 }
314
315 int  set_level_exists(const struct set *s, int i)
316 /* Does the level i of the set exist? */
317 {
318     return (i >= 0) && (i < s->count);
319 }
320
321 /*---------------------------------------------------------------------------*/
322
323 static void set_load_levels(void)
324 {
325     FILE *fin;
326     struct level *l;
327
328     char buf[MAXSTR];
329     char name[MAXSTR];
330
331     int i = 0, res;
332     int nb = 1, bnb = 1;
333
334     if ((fin = fopen(current_set->file, "r")))
335     {
336         res = 1;
337
338         /* Skip the five first lines */
339         for (i = 0; i < 5; i++)
340             fgets(buf, MAXSTR, fin);
341
342         for (i = 0; i < current_set->count && res; i++)
343         {
344             l = &level_v[i];
345
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     }
364
365     assert(i == current_set->count);
366 }
367
368 void set_goto(int i)
369 {
370     current_set = &set_v[i];
371     set_load_levels();
372     set_load_hs();
373 }
374
375 const struct set *curr_set(void)
376 {
377     return current_set;
378 }
379
380 const struct level *get_level(int i)
381 {
382     return (i >= 0 && i < current_set->count) ? &level_v[i] : NULL;
383 }
384
385 /*---------------------------------------------------------------------------*/
386
387 static int score_time_comp(const struct score *S, int i, int j)
388 {
389     if (S->timer[i] < S->timer[j])
390         return 1;
391
392     if (S->timer[i] == S->timer[j] && 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] && S->timer[i] < S->timer[j])
404         return 1;
405
406     return 0;
407 }
408
409 static void score_swap(struct score *S, int i, int j)
410 {
411     char player[MAXNAM];
412     int  tmp;
413
414     strncpy(player,       S->player[i], MAXNAM);
415     strncpy(S->player[i], S->player[j], MAXNAM);
416     strncpy(S->player[j], player,       MAXNAM);
417
418     tmp         = S->timer[i];
419     S->timer[i] = S->timer[j];
420     S->timer[j] = tmp;
421
422     tmp         = S->coins[i];
423     S->coins[i] = S->coins[j];
424     S->coins[j] = tmp;
425 }
426
427 static int score_time_insert(struct score *s, const char *player, int timer,
428                              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
439     return i + 1;
440 }
441
442 static int score_coin_insert(struct score *s, const char *player, int timer,
443                              int coins)
444 {
445     int i;
446
447     strncpy(s->player[3], player, MAXNAM);
448     s->timer[3] = timer;
449     s->coins[3] = coins;
450
451     for (i = 2; i >= 0 && score_coin_comp(s, i + 1, i); i--)
452         score_swap(s, i + 1, i);
453
454     return i + 1;
455 }
456
457 static int level_score_update(struct level_game *lg, const char *player)
458 /* Update the level score rank according to coins and timer */
459 {
460     int timer = lg->timer;
461     int coins = lg->coins;
462     struct level *l = &level_v[lg->level->number];
463
464     lg->time_rank = score_time_insert(&l->time_score, player, timer, coins);
465
466     if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
467         lg->goal_rank = score_time_insert(&l->goal_score, player, timer, coins);
468     else
469         lg->goal_rank = 3;
470
471     lg->coin_rank = score_coin_insert(&l->coin_score, player, timer, coins);
472
473     return (lg->time_rank < 3 || lg->goal_rank < 3 || lg->coin_rank < 3);
474 }
475
476 static int set_score_update(struct level_game *lg, const char *player)
477 /* Update the set score rank according to score and times */
478 {
479     int timer = lg->times;
480     int coins = lg->score;
481     struct set *s = current_set;
482
483     lg->score_rank = score_time_insert(&s->time_score, player, timer, coins);
484     lg->times_rank = score_time_insert(&s->coin_score, player, timer, coins);
485     return (lg->score_rank < 3 || lg->times_rank < 3);
486 }
487
488
489 void score_change_name(struct level_game *lg, const char *player)
490 /* Update the player name for set and level high-score */
491 {
492 #define UPDATE(i, x) (strncpy((x).player[(i)], player, MAXNAM))
493     struct set *s = current_set;
494     struct level *l = &level_v[lg->level->number];
495     UPDATE(lg->time_rank, l->time_score);
496     UPDATE(lg->goal_rank, l->goal_score);
497     UPDATE(lg->coin_rank, l->coin_score);
498     UPDATE(lg->score_rank, s->coin_score);
499     UPDATE(lg->times_rank, s->time_score);
500     set_store_hs();
501 }
502
503 static struct level *next_level(int i)
504 {
505 /* Return the ith level, or NULL */
506     return set_level_exists(current_set, i + 1) ? &level_v[i + 1] : NULL;
507 }
508
509 static struct level *next_normal_level(int i)
510 /* Return the next normal level (starting for i)
511  * Return NULL if there is not a such level */
512 {
513     for (i++; i < current_set->count; i++)
514         if (!level_v[i].is_bonus)
515             return &level_v[i];
516     return NULL;
517 }
518
519 void set_finish_level(struct level_game *lg, const char *player)
520 /* Inform the set that a level is finished.
521  * Update next_level and score rank fields */
522 {
523     struct set *s = current_set;
524     int ln = lg->level->number; /* curent level number */
525     struct level *cl = &level_v[ln];    /* current level */
526     struct level *nl = NULL;    /* next level */
527     int dirty = 0;              /* HS should be saved? */
528
529     assert(s == cl->set);
530
531     /* if no set, no next level */
532     if (s == NULL)
533     {
534         /* if no set, return */
535         lg->next_level = NULL;
536         return;
537     }
538
539     /* On level completed */
540     if (lg->state == GAME_GOAL)
541     {
542         /* Update level scores */
543         dirty = level_score_update(lg, player);
544
545         /* Complete the level */
546         if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
547         {
548             /* Complete the level */
549             if (!cl->is_completed)
550             {
551                 cl->is_completed = 1;
552                 s->completed += 1;
553                 dirty = 1;
554             }
555         }
556     }
557
558     /* On goal reached */
559     if (lg->state == GAME_GOAL || lg->state == GAME_SPEC)
560     {
561         /* Identify the following level */
562         nl = next_level(ln + lg->state_value);
563         if (nl != NULL)
564         {
565             /* skip bonuses if unlocked in non challenge mode */
566             if (nl->is_bonus && nl->is_locked && lg->mode != MODE_CHALLENGE)
567                 nl = next_normal_level(nl->number);
568         }
569         else if (lg->mode == MODE_CHALLENGE)
570             lg->win = 1;
571     }
572     else if (cl->is_bonus || lg->mode != MODE_CHALLENGE)
573     {
574         /* On fail, identify the next level (only in bonus for challenge) */
575         nl = next_normal_level(ln);
576         /* Next level may be unavailable */
577         if (!cl->is_bonus && nl != NULL && nl->is_locked)
578             nl = NULL;
579         /* Fail a bonus level but win the set! */
580         else if (nl == NULL && lg->mode == MODE_CHALLENGE)
581             lg->win = 1;
582     }
583
584     /* Win ! */
585     if (lg->win)
586     {
587         /* update set score */
588         set_score_update(lg, player);
589         /* unlock all levels */
590         set_cheat();
591         dirty = 1;
592     }
593
594     /* unlock the next level if needed */
595     if (nl != NULL && nl->is_locked)
596     {
597         if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
598         {
599             lg->unlock = 1;
600             nl->is_locked = 0;
601             s->locked -= 1;
602             dirty = 1;
603         }
604         else
605             nl = NULL;
606     }
607
608     /* got the next level */
609     lg->next_level = nl;
610
611     /* Update file */
612     if (dirty)
613         set_store_hs();
614 }
615
616 /*---------------------------------------------------------------------------*/
617
618 void level_snap(int i)
619 {
620     char filename[MAXSTR];
621     char *ext;
622
623     /* Convert the level name to a PNG filename. */
624
625     memset(filename, 0, MAXSTR);
626
627     ext = strrchr(level_v[i].file, '.');
628     strncpy(filename, level_v[i].file,
629             ext ? ext - level_v[i].file : strlen(level_v[i].file));
630     strcat(filename, ".png");
631
632     /* Initialize the game for a snapshot. */
633
634     if (game_init(&level_v[i], 0, 0))
635     {
636         int shadow;
637
638         if ((shadow = config_get_d(CONFIG_SHADOW)))
639             config_set_d(CONFIG_SHADOW, 0);
640
641         /* Render the level and grab the screen. */
642
643         config_clear();
644         game_set_fly(1.f);
645         game_kill_fade();
646         game_draw(1, 0);
647         SDL_GL_SwapBuffers();
648
649         image_snap(filename, config_get_d(CONFIG_WIDTH),
650                    config_get_d(CONFIG_HEIGHT));
651
652         if (shadow)
653             config_set_d(CONFIG_SHADOW, 1);
654     }
655 }
656
657 void set_cheat(void)
658 /* Open each level of the current set */
659 {
660     int i;
661     current_set->locked = 0;
662     for (i = 0; i < current_set->count; i++)
663         level_v[i].is_locked = 0;
664 }
665
666
667 /*---------------------------------------------------------------------------*/