Revert revision [536] to avoid making further changes on an unstable
[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 /*---------------------------------------------------------------------------*/
335
336 static int score_time_comp(const struct score *S, int i, int j)
337 {
338     if (S->timer[i] < S->timer[j])
339         return 1;
340
341     if (S->timer[i] == S->timer[j] && S->coins[i] > S->coins[j])
342         return 1;
343
344     return 0;
345 }
346
347 static int score_coin_comp(const struct score *S, int i, int j)
348 {
349     if (S->coins[i] > S->coins[j])
350         return 1;
351
352     if (S->coins[i] == S->coins[j] && S->timer[i] < S->timer[j])
353         return 1;
354
355     return 0;
356 }
357
358 static void score_swap(struct score *S, int i, int j)
359 {
360     char player[MAXNAM];
361     int  tmp;
362
363     strncpy(player,       S->player[i], MAXNAM);
364     strncpy(S->player[i], S->player[j], MAXNAM);
365     strncpy(S->player[j], player,       MAXNAM);
366
367     tmp         = S->timer[i];
368     S->timer[i] = S->timer[j];
369     S->timer[j] = tmp;
370
371     tmp         = S->coins[i];
372     S->coins[i] = S->coins[j];
373     S->coins[j] = tmp;
374 }
375
376 static int score_time_insert(struct score *s, const char *player, int timer,
377                              int coins)
378 {
379     int i;
380
381     strncpy(s->player[3], player, MAXNAM);
382     s->timer[3] = timer;
383     s->coins[3] = coins;
384
385     for (i = 2; i >= 0 && score_time_comp(s, i + 1, i); i--)
386         score_swap(s, i + 1, i);
387
388     return i + 1;
389 }
390
391 static int score_coin_insert(struct score *s, const char *player, int timer,
392                              int coins)
393 {
394     int i;
395
396     strncpy(s->player[3], player, MAXNAM);
397     s->timer[3] = timer;
398     s->coins[3] = coins;
399
400     for (i = 2; i >= 0 && score_coin_comp(s, i + 1, i); i--)
401         score_swap(s, i + 1, i);
402
403     return i + 1;
404 }
405
406 static int level_score_update(struct level_game *lg, const char *player)
407 /* Update the level score rank according to coins and timer */
408 {
409     int timer = lg->timer;
410     int coins = lg->coins;
411     struct level *l = &current_set->level_v[lg->level->number];
412
413     lg->time_rank = score_time_insert(&l->score.best_times,
414                                       player, timer, coins);
415
416     if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
417         lg->goal_rank = score_time_insert(&l->score.unlock_goal,
418                                           player, timer, coins);
419     else
420         lg->goal_rank = 3;
421
422     lg->coin_rank = score_coin_insert(&l->score.most_coins,
423                                       player, timer, coins);
424
425     return (lg->time_rank < 3 || lg->goal_rank < 3 || lg->coin_rank < 3);
426 }
427
428 static int set_score_update(struct level_game *lg, const char *player)
429 /* Update the set score rank according to score and times */
430 {
431     int timer = lg->times;
432     int coins = lg->score;
433     struct set *s = current_set;
434
435     lg->score_rank = score_time_insert(&s->time_score, player, timer, coins);
436     lg->times_rank = score_time_insert(&s->coin_score, player, timer, coins);
437     return (lg->score_rank < 3 || lg->times_rank < 3);
438 }
439
440
441 void score_change_name(struct level_game *lg, const char *player)
442 /* Update the player name for set and level high-score */
443 {
444 #define UPDATE(i, x) (strncpy((x).player[(i)], player, MAXNAM))
445     struct set *s = current_set;
446     struct level *l = &s->level_v[lg->level->number];
447     UPDATE(lg->time_rank, l->score.best_times);
448     UPDATE(lg->goal_rank, l->score.unlock_goal);
449     UPDATE(lg->coin_rank, l->score.most_coins);
450     UPDATE(lg->score_rank, s->coin_score);
451     UPDATE(lg->times_rank, s->time_score);
452     set_store_hs(s);
453 }
454
455 static struct level *next_level(int i)
456 {
457 /* Return the ith level, or NULL */
458     return set_level_exists(current_set, i + 1) ? &current_set->level_v[i + 1] : NULL;
459 }
460
461 static struct level *next_normal_level(int i)
462 /* Return the next normal level (starting for i)
463  * Return NULL if there is not a such level */
464 {
465     for (i++; i < current_set->count; i++)
466         if (!current_set->level_v[i].is_bonus)
467             return &current_set->level_v[i];
468     return NULL;
469 }
470
471 void set_finish_level(struct level_game *lg, const char *player)
472 /* Inform the set that a level is finished.
473  * Update next_level and score rank fields */
474 {
475     struct set *s = current_set;
476     int ln = lg->level->number; /* curent level number */
477     struct level *cl = &s->level_v[ln];    /* current level */
478     struct level *nl = NULL;    /* next level */
479     int dirty = 0;              /* HS should be saved? */
480
481     assert(s == cl->set);
482
483     /* if no set, no next level */
484     if (s == NULL)
485     {
486         /* if no set, return */
487         lg->next_level = NULL;
488         return;
489     }
490
491     /* On level completed */
492     if (lg->state == GAME_GOAL)
493     {
494         /* Update level scores */
495         dirty = level_score_update(lg, player);
496
497         /* Complete the level */
498         if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
499         {
500             /* Complete the level */
501             if (!cl->is_completed)
502             {
503                 cl->is_completed = 1;
504                 s->completed += 1;
505                 dirty = 1;
506             }
507         }
508     }
509
510     /* On goal reached */
511     if (lg->state == GAME_GOAL || lg->state == GAME_SPEC)
512     {
513         /* Identify the following level */
514         nl = next_level(ln + lg->state_value);
515         if (nl != NULL)
516         {
517             /* skip bonuses if unlocked in non challenge mode */
518             if (nl->is_bonus && nl->is_locked && lg->mode != MODE_CHALLENGE)
519                 nl = next_normal_level(nl->number);
520         }
521         else if (lg->mode == MODE_CHALLENGE)
522             lg->win = 1;
523     }
524     else if (cl->is_bonus || lg->mode != MODE_CHALLENGE)
525     {
526         /* On fail, identify the next level (only in bonus for challenge) */
527         nl = next_normal_level(ln);
528         /* Next level may be unavailable */
529         if (!cl->is_bonus && nl != NULL && nl->is_locked)
530             nl = NULL;
531         /* Fail a bonus level but win the set! */
532         else if (nl == NULL && lg->mode == MODE_CHALLENGE)
533             lg->win = 1;
534     }
535
536     /* Win ! */
537     if (lg->win)
538     {
539         /* update set score */
540         set_score_update(lg, player);
541         /* unlock all levels */
542         set_cheat();
543         dirty = 1;
544     }
545
546     /* unlock the next level if needed */
547     if (nl != NULL && nl->is_locked)
548     {
549         if (lg->mode == MODE_CHALLENGE || lg->mode == MODE_NORMAL)
550         {
551             lg->unlock = 1;
552             nl->is_locked = 0;
553             s->locked -= 1;
554             dirty = 1;
555         }
556         else
557             nl = NULL;
558     }
559
560     /* got the next level */
561     lg->next_level = nl;
562
563     /* Update file */
564     if (dirty)
565         set_store_hs(s);
566 }
567
568 /*---------------------------------------------------------------------------*/
569
570 void level_snap(int i)
571 {
572     char filename[MAXSTR];
573     char *ext;
574     struct level *level_v = current_set->level_v;
575
576     /* Convert the level name to a PNG filename. */
577
578     memset(filename, 0, MAXSTR);
579
580     ext = strrchr(level_v[i].file, '.');
581     strncpy(filename, level_v[i].file,
582             ext ? ext - level_v[i].file : strlen(level_v[i].file));
583     strcat(filename, ".png");
584
585     /* Initialize the game for a snapshot. */
586
587     if (game_init(&level_v[i], 0, 0))
588     {
589         int shadow;
590
591         if ((shadow = config_get_d(CONFIG_SHADOW)))
592             config_set_d(CONFIG_SHADOW, 0);
593
594         /* Render the level and grab the screen. */
595
596         config_clear();
597         game_set_fly(1.f);
598         game_kill_fade();
599         game_draw(1, 0);
600         SDL_GL_SwapBuffers();
601
602         image_snap(filename);
603
604         if (shadow)
605             config_set_d(CONFIG_SHADOW, 1);
606     }
607 }
608
609 void set_cheat(void)
610 /* Open each level of the current set */
611 {
612     int i;
613     current_set->locked = 0;
614     for (i = 0; i < current_set->count; i++)
615         current_set->level_v[i].is_locked = 0;
616 }
617
618
619 /*---------------------------------------------------------------------------*/