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.
31 /*---------------------------------------------------------------------------*/
33 #define MAGIC 0x4E425251 /* Replay file magic number (should not change) */
34 #define OLD_MAGIC 0x4E425250 /* Replay file magic number for neverball 1.4.0 */
35 #define REPLAY_VERSION 1 /* Replay file format version (can change) */
37 #define DEMO_FPS_CAP 200 /* FPS replay limit, keeps size down on monster systems */
41 static struct demo demos[MAXDEMO]; /* Array of scanned demos */
43 static int count; /* number of scanned demos */
45 /*---------------------------------------------------------------------------*/
47 void demo_dump_info(const struct demo *d)
48 /* This function dump the info of a demo structure
49 * It's only a function for debugging, no need of I18N */
72 d->timer, d->coins, d->mode, d->state, ctime(&d->date),
74 d->shot, d->file, d->back, d->grad, d->song,
75 d->time, d->goal, d->score, d->balls, d->times);
78 FILE *demo_header_read(const char *filename, struct demo *d)
79 /* Open a demo file, fill the demo information structure
80 * If success, return the file pointer positioned after the header
81 * If fail, return null
88 if ((fp = fopen(filename, FMODE_RB)))
94 get_index(fp, &magic);
95 get_index(fp, &version);
97 /* if time is 0, it means the replay was not finished */
100 if (magic == MAGIC && version == REPLAY_VERSION && t)
103 strncpy(d->filename, filename, PATHMAX);
105 /* Remove the directory delimiter */
107 basename = strrchr(filename, '/');
110 basename = strrchr(filename, '\\');
114 if ((tmp = strrchr(basename, '\\')))
119 if (basename != NULL)
120 strncpy(buf, basename + 1, MAXSTR);
122 strncpy(buf, filename, MAXSTR);
124 /* Remove the extension */
125 t = strlen(buf) - strlen(REPLAY_EXT);
126 if ((t > 1) && (strcmp(buf + t, REPLAY_EXT) == 0))
128 strncpy(d->name, buf, PATHMAX);
129 d->name[PATHMAX - 1] = '\0';
131 get_index (fp, &d->coins);
132 get_index (fp, &d->state);
133 get_index (fp, &d->mode);
134 get_index (fp, (int *)&d->date);
135 get_string(fp, d->player, MAXNAM);
136 get_string(fp, d->shot, PATHMAX);
137 get_string(fp, d->file, PATHMAX);
138 get_string(fp, d->back, PATHMAX);
139 get_string(fp, d->grad, PATHMAX);
140 get_string(fp, d->song, PATHMAX);
141 get_index (fp, &d->time);
142 get_index (fp, &d->goal);
143 get_index (fp, &d->score);
144 get_index (fp, &d->balls);
145 get_index (fp, &d->times);
146 get_string(fp, d->nb_version, 20);
155 static FILE *demo_header_write(struct demo *d)
156 /* Create a new demo file, write the demo information structure
157 * If success, return the file pointer positioned after the header
158 * If fail, return null
162 int version = REPLAY_VERSION;
166 if (d->filename && (fp = fopen(d->filename, FMODE_WB)))
168 put_index (fp, &magic);
169 put_index (fp, &version);
170 put_index (fp, &zero);
171 put_index (fp, &zero);
172 put_index (fp, &zero);
173 put_index (fp, &d->mode);
174 put_index (fp, (int *)&d->date);
175 put_string(fp, d->player);
176 put_string(fp, d->shot);
177 put_string(fp, d->file);
178 put_string(fp, d->back);
179 put_string(fp, d->grad);
180 put_string(fp, d->song);
181 put_index (fp, &d->time);
182 put_index (fp, &d->goal);
183 put_index (fp, &d->score);
184 put_index (fp, &d->balls);
185 put_index (fp, &d->times);
186 put_string(fp, VERSION);
193 void demo_header_stop(FILE *fp, int coins, int timer, int state)
194 /* Update the demo header using the final level state. */
196 long pos = ftell(fp);
197 fseek(fp, 8, SEEK_SET);
198 put_index(fp, &timer);
199 put_index(fp, &coins);
200 put_index(fp, &state);
201 fseek(fp, pos, SEEK_SET);
204 /*---------------------------------------------------------------------------*/
206 static void demo_scan_file(const char *filename)
207 /* Scan another file (used by demo_scan */
210 if ((fp = demo_header_read(config_user(filename), &demos[count])))
226 /* Scan the user directory for files. */
228 if ((h = FindFirstFile(config_user("*"), &d)) != INVALID_HANDLE_VALUE)
231 demo_scan_file(d.cFileName);
232 while (count < MAXDEMO && FindNextFile(h, &d));
249 /* Scan the user directory for files. */
251 if ((dp = opendir(config_user(""))))
253 while (count < MAXDEMO && (ent = readdir(dp)))
254 demo_scan_file(ent->d_name);
262 const char *demo_pick(void)
266 return (n > 0) ? demos[(rand() >> 4) % n].filename : NULL;
269 const struct demo *get_demo(int i)
271 return (0 <= i && i < count) ? &demos[i] : NULL;
274 const char *date_to_str(time_t i)
276 static char str[MAXSTR];
277 struct tm *tm = localtime(&i);
278 strftime (str, MAXSTR, "%c", tm);
282 /*---------------------------------------------------------------------------*/
284 int demo_exists(char *name)
289 strcpy(buf, config_user(name));
290 strcat(buf, REPLAY_EXT);
291 if ((fp = fopen(buf, "r")))
299 void demo_unique(char *name)
303 /* Generate a unique name for a new replay save. */
305 for (i = 1; i < 100; i++)
307 sprintf(name, _("replay%02d"), i);
309 if (!demo_exists(name))
314 /*---------------------------------------------------------------------------*/
316 int demo_play_init(const char *name,
317 const struct level *level,
318 const struct level_game *lg)
323 strncpy(demo.name, name, MAXNAM);
324 strncpy(demo.filename, config_user(name), PATHMAX);
325 strcat(demo.filename, REPLAY_EXT);
326 demo.time = demo.coins = demo.state = 0;
327 demo.mode = lg->mode;
328 demo.date = time(NULL);
329 config_get_s(CONFIG_PLAYER, demo.player, MAXNAM);
330 strncpy(demo.shot, level->shot, PATHMAX);
331 strncpy(demo.file, level->file, PATHMAX);
332 strncpy(demo.back, level->back, PATHMAX);
333 strncpy(demo.grad, level->grad, PATHMAX);
334 strncpy(demo.song, level->song, PATHMAX);
335 demo.time = lg->time;
336 demo.goal = lg->goal;
337 demo.score = lg->score;
338 demo.balls = lg->balls;
339 demo.times = lg->times;
341 demo_fp = demo_header_write(&demo);
346 audio_music_fade_to(2.0f, level->song);
347 return game_init(level, lg->time, lg->goal);
351 void demo_play_step(float dt)
353 static float fps_track = 0.0f;
354 static float fps_cap = 1.0f / (float) DEMO_FPS_CAP;
359 if (fps_track > fps_cap)
361 put_float(demo_fp, &fps_track);
362 put_game_state(demo_fp);
368 void demo_play_stop(const struct level_game *lg)
369 /* Update the demo header using the final level state. */
373 demo_header_stop(demo_fp, lg->coins, lg->timer, lg->state);
379 int demo_play_saved(void)
381 return demo_exists(USER_REPLAY_FILE);
384 void demo_play_save(const char *name)
389 if (name && demo_exists(USER_REPLAY_FILE)
390 && strcmp(name, USER_REPLAY_FILE) != 0)
392 strncpy(src, config_user(USER_REPLAY_FILE), PATHMAX);
393 strcat(src, REPLAY_EXT);
394 strncpy(dst, config_user(name), PATHMAX);
395 strcat(dst, REPLAY_EXT);
401 /*---------------------------------------------------------------------------*/
403 static int demo_load_level(const struct demo *demo, struct level *level)
404 /* Load the level of the demo and fill the level structure */
406 if (level_load(demo->file, level))
408 level->time = demo->time;
409 level->goal = demo->goal;
416 static struct demo demo_replay; /* The current demo */
417 static struct level demo_level_replay; /* The current level demo-ed*/
419 const struct demo *curr_demo_replay(void)
425 int demo_replay_init(const char *name, struct level_game *lg)
426 /* Internally load a replay an fill the lg structure (if not NULL) */
428 if ((demo_fp = demo_header_read(name, &demo_replay)))
430 if (!demo_load_level(&demo_replay, &demo_level_replay))
435 lg->mode = demo_replay.mode;
436 lg->score = demo_replay.score;
437 lg->times = demo_replay.times;
438 lg->time = demo_replay.time;
439 lg->goal = demo_replay.goal;
441 /* A normal replay demo */
442 audio_music_fade_to(0.5f, demo_replay.song);
443 return game_init(&demo_level_replay, demo_replay.time,
446 else /* A title screen demo */
447 return game_init(&demo_level_replay, demo_replay.time, 0);
453 int demo_replay_step(float *dt)
455 const float g[3] = { 0.0f, -9.8f, 0.0f };
460 get_float(demo_fp, dt);
462 if (feof(demo_fp) == 0)
464 /* Play out current game state for particles, clock, etc. */
466 game_step(g, *dt, &sv);
468 /* Load real current game state from file. */
470 if (get_game_state(demo_fp))
477 void demo_replay_stop(int d)
484 if (d) unlink(demo_replay.filename);
488 void demo_replay_dump_info(void)
490 demo_dump_info(&demo_replay);
493 /*---------------------------------------------------------------------------*/