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 */
35 static int count; /* Number of scanned demos */
37 /*---------------------------------------------------------------------------*/
39 void demo_dump_info(const struct demo *d)
62 d->timer, d->coins, d->mode, d->state, ctime(&d->date),
64 d->shot, d->file, d->back, d->grad, d->song,
65 d->time, d->goal, d->score, d->balls, d->times);
68 static int demo_header_read(FILE *fp, struct demo *d)
74 get_index(fp, &magic);
75 get_index(fp, &version);
79 if (magic == MAGIC && version == DEMO_VERSION && t)
83 get_index(fp, &d->coins);
84 get_index(fp, &d->state);
85 get_index(fp, &d->mode);
86 get_index(fp, (int *) &d->date);
88 fread(d->player, 1, MAXNAM, fp);
90 fread(d->shot, 1, PATHMAX, fp);
91 fread(d->file, 1, PATHMAX, fp);
92 fread(d->back, 1, PATHMAX, fp);
93 fread(d->grad, 1, PATHMAX, fp);
94 fread(d->song, 1, PATHMAX, fp);
96 get_index(fp, &d->time);
97 get_index(fp, &d->goal);
98 get_index(fp, &d->score);
99 get_index(fp, &d->balls);
100 get_index(fp, &d->times);
102 fread(d->nb_version, 1, 20, fp);
109 static FILE *demo_header_open(const char *filename, struct demo *d)
113 if ((fp = fopen(filename, FMODE_RB)))
115 if (demo_header_read(fp, d))
121 strncpy(d->filename, filename, MAXSTR);
123 /* Remove the directory delimiter */
125 basename = strrchr(filename, '/');
128 basename = strrchr(filename, '\\');
132 if ((tmp = strrchr(basename, '\\')))
136 strncpy(buf, basename ? basename + 1 : filename, MAXSTR);
138 /* Remove the extension */
140 l = strlen(buf) - strlen(REPLAY_EXT);
141 if ((l > 1) && (strcmp(buf + l, REPLAY_EXT) == 0))
144 strncpy(d->name, buf, PATHMAX);
145 d->name[PATHMAX - 1] = '\0';
154 static void demo_header_write(FILE *fp, struct demo *d)
157 int version = DEMO_VERSION;
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);
185 /* Update the demo header using the final level state. */
187 void demo_header_stop(FILE *fp, int coins, int timer, int state)
189 long pos = ftell(fp);
191 fseek(fp, 8, SEEK_SET);
192 put_index(fp, &timer);
193 put_index(fp, &coins);
194 put_index(fp, &state);
195 fseek(fp, pos, SEEK_SET);
198 /*---------------------------------------------------------------------------*/
200 /* Scan another file (used by demo_scan). */
202 static void demo_scan_file(const char *filename)
205 struct demo *d = &demos[count];
207 if ((fp = demo_header_open(config_user(filename), d)))
223 /* Scan the user directory for files. */
225 if ((h = FindFirstFile(config_user("*"), &d)) != INVALID_HANDLE_VALUE)
228 demo_scan_file(d.cFileName);
229 while (count < MAXDEMO && FindNextFile(h, &d));
246 /* Scan the user directory for files. */
248 if ((dp = opendir(config_user(""))))
250 while (count < MAXDEMO && (ent = readdir(dp)))
251 demo_scan_file(ent->d_name);
259 const char *demo_pick(void)
263 return (n > 0) ? demos[(rand() >> 4) % n].filename : NULL;
266 const struct demo *demo_get(int i)
268 return (0 <= i && i < count) ? &demos[i] : NULL;
271 const char *date_to_str(time_t i)
273 static char str[MAXSTR];
274 struct tm *tm = localtime(&i);
275 strftime(str, MAXSTR, "%c", tm);
279 /*---------------------------------------------------------------------------*/
281 int demo_exists(char *name)
286 strcpy(buf, config_user(name));
287 strcat(buf, REPLAY_EXT);
288 if ((fp = fopen(buf, "r")))
296 void demo_unique(char *name)
300 /* Generate a unique name for a new replay save. */
302 for (i = 1; i < 100; i++)
304 sprintf(name, _("replay%02d"), i);
306 if (!demo_exists(name))
311 /*---------------------------------------------------------------------------*/
313 int demo_play_init(const char *name,
314 const struct level *level,
315 const struct level_game *lg)
319 memset(&demo, 0, sizeof (demo));
321 strncpy(demo.filename, config_user(name), MAXSTR);
322 strcat(demo.filename, REPLAY_EXT);
324 demo.mode = lg->mode;
325 demo.date = time(NULL);
327 config_get_s(CONFIG_PLAYER, demo.player, MAXNAM);
329 strncpy(demo.shot, level->shot, PATHMAX);
330 strncpy(demo.file, level->file, PATHMAX);
331 strncpy(demo.back, level->back, PATHMAX);
332 strncpy(demo.grad, level->grad, PATHMAX);
333 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 strncpy(demo.nb_version, VERSION, 20);
343 if (demo.filename && (demo_fp = fopen(demo.filename, FMODE_WB)))
345 demo_header_write(demo_fp, &demo);
346 audio_music_fade_to(2.0f, level->song);
347 return game_init(level, lg->time, lg->goal);
352 void demo_play_step(float dt)
356 put_float(demo_fp, &dt);
357 put_game_state(demo_fp);
361 /* Update the demo header using the final level state. */
363 void demo_play_stop(const struct level_game *lg)
367 demo_header_stop(demo_fp, lg->coins, lg->timer, lg->state);
373 int demo_play_saved(void)
375 return demo_exists(USER_REPLAY_FILE);
378 void demo_play_save(const char *name)
383 if (name && demo_exists(USER_REPLAY_FILE)
384 && strcmp(name, USER_REPLAY_FILE) != 0)
386 strncpy(src, config_user(USER_REPLAY_FILE), PATHMAX);
387 strcat(src, REPLAY_EXT);
388 strncpy(dst, config_user(name), PATHMAX);
389 strcat(dst, REPLAY_EXT);
395 /*---------------------------------------------------------------------------*/
397 static int demo_load_level(const struct demo *demo, struct level *level)
399 if (level_load(demo->file, level))
401 level->time = demo->time;
402 level->goal = demo->goal;
408 static struct demo demo_replay; /* The current demo */
409 static struct level demo_level_replay; /* The current level demo-ed*/
411 const struct demo *curr_demo_replay(void)
416 /* Internally load a replay and fill the lg structure (if not NULL) */
418 int demo_replay_init(const char *name, struct level_game *lg)
420 demo_fp = demo_header_open(name, &demo_replay);
424 if (!demo_load_level(&demo_replay, &demo_level_replay))
429 lg->mode = demo_replay.mode;
430 lg->score = demo_replay.score;
431 lg->times = demo_replay.times;
432 lg->time = demo_replay.time;
433 lg->goal = demo_replay.goal;
435 /* A normal replay demo */
436 audio_music_fade_to(0.5f, demo_replay.song);
437 return game_init(&demo_level_replay, demo_replay.time,
440 else /* A title screen demo */
441 return game_init(&demo_level_replay, demo_replay.time, 0);
446 int demo_replay_step(float *dt)
448 const float g[3] = { 0.0f, -9.8f, 0.0f };
453 get_float(demo_fp, dt);
455 if (feof(demo_fp) == 0)
457 /* Play out current game state for particles, clock, etc. */
459 game_step(g, *dt, &sv);
461 /* Load real current game state from file. */
463 if (get_game_state(demo_fp))
470 void demo_replay_stop(int d)
477 if (d) remove(demo_replay.filename);
481 void demo_replay_dump_info(void)
483 demo_dump_info(&demo_replay);
486 /*---------------------------------------------------------------------------*/