2 * Copyright (C) 2003 Robert Kooima
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.
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.
29 /*---------------------------------------------------------------------------*/
33 char time_n[4][MAXNAM];
37 char coin_n[4][MAXNAM];
55 static int score; /* Current coin total */
56 static int coins; /* Current coin count */
57 static int balls; /* Current life count */
58 static int goal; /* Current goal count */
60 static int level; /* Current level number */
61 static int count; /* Number of levels */
62 static int limit; /* Last opened (locked) level */
63 static int status; /* Status of current level */
65 static int mode; /* Current play mode */
67 static int level_total;
68 static int coins_total;
69 static int times_total;
71 static struct level level_v[MAXLVL];
72 static struct score score_v[MAXLVL];
74 static char scores_file[MAXSTR];
76 /*---------------------------------------------------------------------------*/
78 static void level_store_hs(const char *filename)
82 if ((fout = fopen(config_user(filename), "w")))
87 for (i = 0; i < limit; i++)
88 for (j = 0; j < 3; j++)
90 if (strlen(score_v[i].time_n[j]) == 0)
91 strcpy(score_v[i].time_n[j], DEFAULT_PLAYER);
92 if (strlen(score_v[i].coin_n[j]) == 0)
93 strcpy(score_v[i].coin_n[j], DEFAULT_PLAYER);
95 fprintf(fout, "%d %d %s\n",
98 score_v[i].time_n[j]);
99 fprintf(fout, "%d %d %s\n",
100 score_v[i].coin_t[j],
101 score_v[i].coin_c[j],
102 score_v[i].coin_n[j]);
109 static void level_load_hs(const char *filename)
115 if ((fin = fopen(config_user(filename), "r")))
119 for (i = 0; i < count; i++)
121 if (fscanf(fin, "%d %d %s",
122 &score_v[i].time_t[0],
123 &score_v[i].time_c[0],
124 score_v[i].time_n[0]) == 3 &&
125 fscanf(fin, "%d %d %s",
126 &score_v[i].coin_t[0],
127 &score_v[i].coin_c[0],
128 score_v[i].coin_n[0]) == 3 &&
129 fscanf(fin, "%d %d %s",
130 &score_v[i].time_t[1],
131 &score_v[i].time_c[1],
132 score_v[i].time_n[1]) == 3 &&
133 fscanf(fin, "%d %d %s",
134 &score_v[i].coin_t[1],
135 &score_v[i].coin_c[1],
136 score_v[i].coin_n[1]) == 3 &&
137 fscanf(fin, "%d %d %s",
138 &score_v[i].time_t[2],
139 &score_v[i].time_c[2],
140 score_v[i].time_n[2]) == 3 &&
141 fscanf(fin, "%d %d %s",
142 &score_v[i].coin_t[2],
143 &score_v[i].coin_c[2],
144 score_v[i].coin_n[2]) == 3)
152 /*---------------------------------------------------------------------------*/
154 static void level_init_rc(const char *filename)
165 /* Load the levels list. */
167 if ((fin = fopen(config_data(filename), "r")))
169 while (count < MAXLVL && fgets(buf, MAXSTR, fin))
171 sscanf(buf, "%s %s %s %s %d %d %s",
176 &level_v[count].time,
177 &level_v[count].goal,
178 level_v[count].song);
185 static void level_init_hs(const char *filename)
191 /* Set some sane values in case the scores file is missing. */
193 for (i = 0; i < MAXLVL; i++)
195 strcpy(score_v[i].time_n[0], "Hard");
196 strcpy(score_v[i].time_n[1], "Medium");
197 strcpy(score_v[i].time_n[2], "Easy");
199 score_v[i].time_t[0] = i ? 59999 : 359999;
200 score_v[i].time_t[1] = i ? 59999 : 359999;
201 score_v[i].time_t[2] = i ? 59999 : 359999;
203 score_v[i].time_c[0] = 0;
204 score_v[i].time_c[1] = 0;
205 score_v[i].time_c[2] = 0;
207 strcpy(score_v[i].coin_n[0], "Hard");
208 strcpy(score_v[i].coin_n[1], "Medium");
209 strcpy(score_v[i].coin_n[2], "Easy");
211 score_v[i].coin_t[0] = i ? 59999 : 359999;
212 score_v[i].coin_t[1] = i ? 59999 : 359999;
213 score_v[i].coin_t[2] = i ? 59999 : 359999;
215 score_v[i].coin_c[0] = 0;
216 score_v[i].coin_c[1] = 0;
217 score_v[i].coin_c[2] = 0;
220 /* Load the default high scores file. */
222 if ((fin = fopen(config_data(filename), "r")))
224 for (i = 0; i < MAXLVL && fgets(buf, MAXSTR, fin); i++)
225 sscanf(buf, "%d %d %d %d %d %d",
226 &score_v[i].time_t[0], &score_v[i].coin_c[0],
227 &score_v[i].time_t[1], &score_v[i].coin_c[1],
228 &score_v[i].time_t[2], &score_v[i].coin_c[2]);
234 /*---------------------------------------------------------------------------*/
236 const char *level_shot(int i)
238 return level_v[i].shot;
241 const char *level_time_n(int i, int j)
243 return score_v[i].time_n[j];
246 const char *level_coin_n(int i, int j)
248 return score_v[i].coin_n[j];
251 /*---------------------------------------------------------------------------*/
252 /* Return the coin count for the Most Coins or Best Time score. */
254 int level_coin_c(int i, int j)
259 return score_v[i].coin_c[j];
262 int level_time_c(int i, int j)
264 return score_v[i].time_c[j];
267 /*---------------------------------------------------------------------------*/
268 /* Return the time for the Most Coins or Best Time score. */
270 int level_coin_t(int i, int j)
272 return score_v[i].coin_t[j];
275 int level_time_t(int i, int j)
278 return level_v[i].time - curr_clock();
280 return score_v[i].time_t[j];
283 /*---------------------------------------------------------------------------*/
285 void level_init(const char *init_levels,
286 const char *init_scores,
287 const char *user_scores)
289 memset(level_v, 0, sizeof (struct level) * MAXLVL);
290 memset(score_v, 0, sizeof (struct score) * MAXLVL);
292 level_init_rc(init_levels);
293 level_init_hs(init_scores);
294 level_load_hs(user_scores);
296 strncpy(scores_file, user_scores, MAXSTR);
308 void level_cheat(void)
309 /* Open each level of the set */
314 void level_free(void)
318 level_store_hs(scores_file);
320 for (i = 0; i < count; i++)
321 if (glIsTexture(level_v[i].text))
322 glDeleteTextures(1, &level_v[i].text);
327 int level_exists(int i)
329 return (0 < i && i < count);
332 int level_opened(int i)
334 return level_exists(i) && (0 < i && i < count && i <= limit);
337 int level_locked(int i)
339 return level_opened(i) && (i == limit) && (level_v[i].goal > 0);
342 /*---------------------------------------------------------------------------*/
344 int curr_times_total(void) { return times_total; }
345 int curr_coins_total(void) { return coins_total; }
347 int curr_count(void) { return count; }
348 int curr_score(void) { return score; }
349 int curr_coins(void) { return coins; }
350 int curr_balls(void) { return balls; }
351 int curr_level(void) { return level; }
352 int curr_goal (void) { return goal; }
354 /*---------------------------------------------------------------------------*/
356 static int score_time_comp(const struct score *S, int i, int j)
358 if (S->time_t[i] < S->time_t[j])
361 if (S->time_t[i] == S->time_t[j] &&
362 S->time_c[i] > S->time_c[j])
368 static int score_coin_comp(const struct score *S, int i, int j)
370 if (S->coin_c[i] > S->coin_c[j])
373 if (S->coin_c[i] == S->coin_c[j] &&
374 S->coin_t[i] < S->coin_t[j])
380 /*---------------------------------------------------------------------------*/
382 static void score_time_swap(struct score *S, int i, int j)
388 strncpy(n, S->time_n[i], MAXNAM);
389 strncpy(S->time_n[i], S->time_n[j], MAXNAM);
390 strncpy(S->time_n[j], n, MAXNAM);
393 S->time_t[i] = S->time_t[j];
397 S->time_c[i] = S->time_c[j];
401 static void score_coin_swap(struct score *S, int i, int j)
407 strncpy(n, S->coin_n[i], MAXNAM);
408 strncpy(S->coin_n[i], S->coin_n[j], MAXNAM);
409 strncpy(S->coin_n[j], n, MAXNAM);
412 S->coin_t[i] = S->coin_t[j];
416 S->coin_c[i] = S->coin_c[j];
420 /*---------------------------------------------------------------------------*/
422 int level_replay(const char *filename)
426 return demo_replay_init(filename, &score, &coins, &balls, &goal);
429 int level_play(const char *filename, int i, int m)
437 if (m == MODE_CHALLENGE)
438 goal = level_v[level].goal;
440 goal = (level == limit) ? level_v[level].goal : 0;
442 return demo_play_init(USER_REPLAY_FILE,
449 goal, score, coins, balls);
452 /*---------------------------------------------------------------------------*/
454 void level_stat(int s)
456 if ((status = s) == GAME_GOAL)
458 coins_total += coins;
462 demo_play_stat(curr_coins(), level_v[level].time - curr_clock());
472 return (level + 1 == count);
475 int level_exit(const char *filename, int next)
477 times_total += level_v[level].time - curr_clock();
479 demo_play_stop(filename);
488 level_store_hs(scores_file);
493 if (mode == MODE_CHALLENGE)
498 /* Load the next level. */
500 if (status && level < count && balls >= 0)
502 if (mode == MODE_CHALLENGE)
503 goal = level_v[level].goal;
505 goal = (level == limit) ? level_v[level].goal : 0;
509 return demo_play_init(USER_REPLAY_FILE,
516 goal, score, coins, balls);
522 int level_sort(int *time_i, int *coin_i)
524 int i, clock = level_v[level].time - curr_clock();
527 config_get_s(CONFIG_PLAYER, player, MAXNAM);
529 /* Insert the time record into the high score list. */
531 strncpy(score_v[level].time_n[3], player, MAXNAM);
532 score_v[level].time_c[3] = coins;
533 score_v[level].time_t[3] = clock;
535 for (i = 2; i >= 0 && score_time_comp(score_v + level, i + 1, i); i--)
537 score_time_swap(score_v + level, i + 1, i);
541 /* Insert the coin record into the high score list. */
543 strncpy(score_v[level].coin_n[3], player, MAXNAM);
544 score_v[level].coin_c[3] = coins;
545 score_v[level].coin_t[3] = clock;
547 for (i = 2; i >= 0 && score_coin_comp(score_v + level, i + 1, i); i--)
549 score_coin_swap(score_v + level, i + 1, i);
553 return (*time_i < 3 || *coin_i < 3);
556 int level_done(int *time_i, int *coin_i)
561 config_get_s(CONFIG_PLAYER, player, MAXNAM);
563 /* Note a global high score. */
565 strncpy(score_v[0].time_n[3], player, MAXNAM);
566 score_v[0].time_c[3] = coins_total;
567 score_v[0].time_t[3] = times_total;
569 strncpy(score_v[0].coin_n[3], player, MAXNAM);
570 score_v[0].coin_c[3] = coins_total;
571 score_v[0].coin_t[3] = times_total;
573 if (level == count && level_total == count - 1)
575 /* Insert the time record into the global high score list. */
577 for (i = 2; i >= 0 && score_time_comp(score_v, i + 1, i); i--)
579 score_time_swap(score_v, i + 1, i);
583 /* Insert the coin record into the global high score list. */
585 for (i = 2; i >= 0 && score_coin_comp(score_v, i + 1, i); i--)
587 score_coin_swap(score_v, i + 1, i);
592 return (*time_i < 3 || *coin_i < 3);
595 int level_score(int n)
597 int sound = AUD_COIN;
602 /* Pulse the coin counter based on the value of the grabbed coin. */
604 if (n >= 10) hud_coin_pulse(2.00f);
605 else if (n >= 5) hud_coin_pulse(1.50f);
606 else hud_coin_pulse(1.25f);
608 /* Check for goal open. */
612 if (n >= 10) hud_goal_pulse(2.00f);
613 else if (n >= 5) hud_goal_pulse(1.50f);
614 else hud_goal_pulse(1.25f);
620 hud_goal_pulse(2.0f);
623 goal = (goal > n) ? (goal - n) : 0;
626 audio_play(sound, 1.f);
630 int level_count(void)
632 if (mode != MODE_CHALLENGE)
639 if (score % 100 == 0)
642 audio_play(AUD_BALL, 1.0f);
649 /*---------------------------------------------------------------------------*/
651 void level_name(int i, const char *name, int time_i, int coin_i)
653 strncpy(score_v[i].time_n[time_i], name, MAXNAM);
654 strncpy(score_v[i].coin_n[coin_i], name, MAXNAM);
657 void level_snap(int i)
659 char filename[MAXSTR];
661 /* Convert the level name to a BMP filename. */
663 memset(filename, 0, MAXSTR);
664 strncpy(filename, level_v[i].file, strcspn(level_v[i].file, "."));
665 strcat(filename, ".bmp");
667 /* Initialize the game for a snapshot. */
669 if (game_init(level_v[i].file, level_v[i].back, level_v[i].grad, 0, 1))
671 /* Render the level and grab the screen. */
677 SDL_GL_SwapBuffers();
679 image_snap(filename);
683 /*---------------------------------------------------------------------------*/