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.
27 /*---------------------------------------------------------------------------*/
29 #define MAGIC 0x52424EAF
30 #define DEMO_VERSION 1
34 static struct demo demos[MAXDEMO]; /* Array of scanned demos */
36 static int count; /* number of scanned demos */
38 /*---------------------------------------------------------------------------*/
40 void demo_dump_info(const struct demo *d)
63 d->timer, d->coins, d->mode, d->state, ctime(&d->date),
65 d->shot, d->file, d->back, d->grad, d->song,
66 d->time, d->goal, d->score, d->balls, d->times);
69 /* Open a demo file, fill the demo information structure. If success, return
70 * the file pointer positioned after the header. If fail, return NULL. */
72 FILE *demo_header_read(const char *filename, struct demo *d)
79 if ((fp = fopen(filename, FMODE_RB)))
85 get_index(fp, &magic);
86 get_index(fp, &version);
90 if (magic == MAGIC && version == DEMO_VERSION && t)
93 strncpy(d->filename, filename, PATHMAX);
95 /* Remove the directory delimiter */
97 basename = strrchr(filename, '/');
101 basename = strrchr(filename, '\\');
105 if ((tmp = strrchr(basename, '\\')))
109 strncpy(buf, basename ? basename + 1 : filename, MAXSTR);
111 /* Remove the extension */
112 t = strlen(buf) - strlen(REPLAY_EXT);
113 if ((t > 1) && (strcmp(buf + t, REPLAY_EXT) == 0))
115 strncpy(d->name, buf, PATHMAX);
116 d->name[PATHMAX - 1] = '\0';
118 get_index (fp, &d->coins);
119 get_index (fp, &d->state);
120 get_index (fp, &d->mode);
121 get_index (fp, (int *) &d->date);
123 fread(d->player, 1, MAXNAM, fp);
125 fread(d->shot, 1, PATHMAX, fp);
126 fread(d->file, 1, PATHMAX, fp);
127 fread(d->back, 1, PATHMAX, fp);
128 fread(d->grad, 1, PATHMAX, fp);
129 fread(d->song, 1, PATHMAX, fp);
131 get_index (fp, &d->time);
132 get_index (fp, &d->goal);
133 get_index (fp, &d->score);
134 get_index (fp, &d->balls);
135 get_index (fp, &d->times);
137 fread(d->nb_version, 1, 20, fp);
146 /* Create a new demo file, write the demo information structure. If success,
147 * return the file pointer positioned after the header. If fail, return NULL.
150 static FILE *demo_header_write(struct demo *d)
153 int version = DEMO_VERSION;
158 if (d->filename && (fp = fopen(d->filename, FMODE_WB)))
160 put_index(fp, &magic);
161 put_index(fp, &version);
162 put_index(fp, &zero);
163 put_index(fp, &zero);
164 put_index(fp, &zero);
165 put_index(fp, &d->mode);
166 put_index(fp, (int *) &d->date);
168 fwrite(d->player, 1, MAXNAM, fp);
170 fwrite(d->shot, 1, PATHMAX, fp);
171 fwrite(d->file, 1, PATHMAX, fp);
172 fwrite(d->back, 1, PATHMAX, fp);
173 fwrite(d->grad, 1, PATHMAX, fp);
174 fwrite(d->song, 1, PATHMAX, fp);
176 put_index(fp, &d->time);
177 put_index(fp, &d->goal);
178 put_index(fp, &d->score);
179 put_index(fp, &d->balls);
180 put_index(fp, &d->times);
182 fwrite(d->nb_version, 1, 20, fp);
189 /* Update the demo header using the final level state. */
191 void demo_header_stop(FILE *fp, int coins, int timer, int state)
193 long pos = ftell(fp);
195 fseek(fp, 8, SEEK_SET);
196 put_index(fp, &timer);
197 put_index(fp, &coins);
198 put_index(fp, &state);
199 fseek(fp, pos, SEEK_SET);
202 /*---------------------------------------------------------------------------*/
204 /* Scan another file (used by demo_scan). */
206 static void demo_scan_file(const char *filename)
209 if ((fp = demo_header_read(config_user(filename), &demos[count])))
225 /* Scan the user directory for files. */
227 if ((h = FindFirstFile(config_user("*"), &d)) != INVALID_HANDLE_VALUE)
230 demo_scan_file(d.cFileName);
231 while (count < MAXDEMO && FindNextFile(h, &d));
248 /* Scan the user directory for files. */
250 if ((dp = opendir(config_user(""))))
252 while (count < MAXDEMO && (ent = readdir(dp)))
253 demo_scan_file(ent->d_name);
261 const char *demo_pick(void)
265 return (n > 0) ? demos[(rand() >> 4) % n].filename : NULL;
268 const struct demo *get_demo(int i)
270 return (0 <= i && i < count) ? &demos[i] : NULL;
273 const char *date_to_str(time_t i)
275 static char str[MAXSTR];
276 struct tm *tm = localtime(&i);
277 strftime(str, MAXSTR, "%c", tm);
281 /*---------------------------------------------------------------------------*/
283 int demo_exists(char *name)
288 strcpy(buf, config_user(name));
289 strcat(buf, REPLAY_EXT);
290 if ((fp = fopen(buf, "r")))
298 void demo_unique(char *name)
302 /* Generate a unique name for a new replay save. */
304 for (i = 1; i < 100; i++)
306 sprintf(name, _("replay%02d"), i);
308 if (!demo_exists(name))
313 /*---------------------------------------------------------------------------*/
315 int demo_play_init(const char *name,
316 const struct level *level,
317 const struct level_game *lg)
321 memset(&demo, 0, sizeof (demo));
323 strncpy(demo.filename, config_user(name), PATHMAX);
324 strcat(demo.filename, REPLAY_EXT);
326 demo.mode = lg->mode;
327 demo.date = time(NULL);
329 config_get_s(CONFIG_PLAYER, demo.player, MAXNAM);
331 strncpy(demo.shot, level->shot, PATHMAX);
332 strncpy(demo.file, level->file, PATHMAX);
333 strncpy(demo.back, level->back, PATHMAX);
334 strncpy(demo.grad, level->grad, PATHMAX);
335 strncpy(demo.song, level->song, PATHMAX);
337 demo.time = lg->time;
338 demo.goal = lg->goal;
339 demo.score = lg->score;
340 demo.balls = lg->balls;
341 demo.times = lg->times;
343 strncpy(demo.nb_version, VERSION, 20);
345 demo_fp = demo_header_write(&demo);
349 audio_music_fade_to(2.0f, level->song);
350 return game_init(level, lg->time, lg->goal);
355 void demo_play_step(float dt)
359 put_float(demo_fp, &dt);
360 put_game_state(demo_fp);
364 /* Update the demo header using the final level state. */
366 void demo_play_stop(const struct level_game *lg)
370 demo_header_stop(demo_fp, lg->coins, lg->timer, lg->state);
376 int demo_play_saved(void)
378 return demo_exists(USER_REPLAY_FILE);
381 void demo_play_save(const char *name)
386 if (name && demo_exists(USER_REPLAY_FILE)
387 && strcmp(name, USER_REPLAY_FILE) != 0)
389 strncpy(src, config_user(USER_REPLAY_FILE), PATHMAX);
390 strcat(src, REPLAY_EXT);
391 strncpy(dst, config_user(name), PATHMAX);
392 strcat(dst, REPLAY_EXT);
398 /*---------------------------------------------------------------------------*/
400 static int demo_load_level(const struct demo *demo, struct level *level)
401 /* Load the level of the demo and fill the level structure */
403 if (level_load(demo->file, level))
405 level->time = demo->time;
406 level->goal = demo->goal;
413 static struct demo demo_replay; /* The current demo */
414 static struct level demo_level_replay; /* The current level demo-ed*/
416 const struct demo *curr_demo_replay(void)
421 /* Internally load a replay and fill the lg structure (if not NULL) */
423 int demo_replay_init(const char *name, struct level_game *lg)
425 if ((demo_fp = demo_header_read(name, &demo_replay)))
427 if (!demo_load_level(&demo_replay, &demo_level_replay))
432 lg->mode = demo_replay.mode;
433 lg->score = demo_replay.score;
434 lg->times = demo_replay.times;
435 lg->time = demo_replay.time;
436 lg->goal = demo_replay.goal;
438 /* A normal replay demo */
439 audio_music_fade_to(0.5f, demo_replay.song);
440 return game_init(&demo_level_replay, demo_replay.time,
443 else /* A title screen demo */
444 return game_init(&demo_level_replay, demo_replay.time, 0);
450 int demo_replay_step(float *dt)
452 const float g[3] = { 0.0f, -9.8f, 0.0f };
457 get_float(demo_fp, dt);
459 if (feof(demo_fp) == 0)
461 /* Play out current game state for particles, clock, etc. */
463 game_step(g, *dt, &sv);
465 /* Load real current game state from file. */
467 if (get_game_state(demo_fp))
474 void demo_replay_stop(int d)
481 if (d) remove(demo_replay.filename);
485 void demo_replay_dump_info(void)
487 demo_dump_info(&demo_replay);
490 /*---------------------------------------------------------------------------*/