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.
24 /*---------------------------------------------------------------------------*/
26 static void level_scan_metadata(struct level *l, const struct s_file *fp)
30 for (i = 0; i < fp->dc; i++)
32 char *k = fp->av + fp->dv[i].ai;
33 char *v = fp->av + fp->dv[i].aj;
35 if (strcmp(k, "message") == 0)
36 strncpy(l->message, v, MAXSTR);
37 else if (strcmp(k, "back") == 0)
38 strncpy(l->back, v, PATHMAX);
39 else if (strcmp(k, "song") == 0)
40 strncpy(l->song, v, PATHMAX);
41 else if (strcmp(k, "grad") == 0)
42 strncpy(l->grad, v, PATHMAX);
43 else if (strcmp(k, "shot") == 0)
44 strncpy(l->shot, v, PATHMAX);
45 else if (strcmp(k, "goal") == 0)
48 l->score.most_coins.coins[2] = l->goal;
50 else if (strcmp(k, "time") == 0)
53 l->score.best_times.timer[2] = l->time;
54 l->score.unlock_goal.timer[2] = l->time;
56 else if (strcmp(k, "time_hs") == 0)
58 &l->score.best_times.timer[0],
59 &l->score.best_times.timer[1]);
60 else if (strcmp(k, "goal_hs") == 0)
62 &l->score.unlock_goal.timer[0],
63 &l->score.unlock_goal.timer[1]);
64 else if (strcmp(k, "coin_hs") == 0)
66 &l->score.most_coins.coins[0],
67 &l->score.most_coins.coins[1]);
68 else if (strcmp(k, "version") == 0)
69 strncpy(l->version, v, MAXSTR);
70 else if (strcmp(k, "author") == 0)
71 strncpy(l->author, v, MAXSTR);
72 else if (strcmp(k, "bonus") == 0)
73 l->is_bonus = atoi(v);
77 /* Load the sol file 'filename' and fill the 'level' structure. Return 1 on
78 * success, 0 on error. */
80 int level_load(const char *filename, struct level *level)
87 memset(level, 0, sizeof (struct level));
88 memset(&sol, 0, sizeof (sol));
90 /* Try to load the sol file */
91 if (!sol_load_only_file(&sol, config_data(filename)))
94 _("Error while loading level file '%s': %s\n"), filename,
95 errno ? strerror(errno) : _("Not a valid level file"));
99 strcpy(level->file, filename);
101 /* Init hs with default values */
102 score_init_hs(&level->score.best_times, 59999, 0);
103 score_init_hs(&level->score.unlock_goal, 59999, 0);
104 score_init_hs(&level->score.most_coins, 59999, 0);
106 /* Compute money and default max money */
108 for (i = 0; i < sol.hc; i++)
109 if (sol.hv[i].t == ITEM_COIN)
110 money += sol.hv[i].n;
111 level->score.most_coins.coins[0] = money;
113 /* Scan sol metadata */
115 level_scan_metadata(level, &sol);
117 /* Compute initial hs default values */
121 t[0] = t[1] = t[2]; \
122 else if (t[2] c t[1]) \
123 t[1] = (t[0] + t[2]) / 2
125 HOP(level->score.best_times.timer, <=);
126 HOP(level->score.unlock_goal.timer, <=);
127 HOP(level->score.most_coins.coins, >=);
134 /*---------------------------------------------------------------------------*/
136 void level_dump_info(const struct level *l)
138 printf("filename: %s\n"
143 "time hs: %d %d %d\n"
144 "goal hs: %d %d %d\n"
145 "coin hs: %d %d %d\n"
156 l->score.best_times.timer[0],
157 l->score.best_times.timer[1],
158 l->score.best_times.timer[2],
159 l->score.unlock_goal.timer[0],
160 l->score.unlock_goal.timer[1],
161 l->score.unlock_goal.timer[2],
162 l->score.most_coins.coins[0],
163 l->score.most_coins.coins[1],
164 l->score.most_coins.coins[2],
172 /*---------------------------------------------------------------------------*/
174 const char *state_to_str(int m)
178 case GAME_NONE: return _("Aborted");
179 case GAME_TIME: return _("Time-out");
181 case GAME_GOAL: return _("Success");
182 case GAME_FALL: return _("Fall-out");
183 default: return _("Unknown");
187 /*---------------------------------------------------------------------------*/