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