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 time_t make_time_from_utc(struct tm *tm)
70 struct tm local, *utc;
75 local = *localtime(&t);
78 local.tm_year += local.tm_year - utc->tm_year;
79 local.tm_mon += local.tm_mon - utc->tm_mon ;
80 local.tm_mday += local.tm_mday - utc->tm_mday;
81 local.tm_hour += local.tm_hour - utc->tm_hour;
82 local.tm_min += local.tm_min - utc->tm_min ;
83 local.tm_sec += local.tm_sec - utc->tm_sec ;
85 return mktime(&local);
88 static int demo_header_read(FILE *fp, struct demo *d)
97 get_index(fp, &magic);
98 get_index(fp, &version);
102 if (magic == MAGIC && version == DEMO_VERSION && t)
106 get_index(fp, &d->coins);
107 get_index(fp, &d->state);
108 get_index(fp, &d->mode);
111 get_index(fp, (int *) &d->date);
113 fread(datestr, 1, 20, fp);
123 /* Convert certain values to valid structure member values. */
125 date.tm_year -= 1900;
128 d->date = make_time_from_utc(&date);
130 fread(d->player, 1, MAXNAM, fp);
132 fread(d->shot, 1, PATHMAX, fp);
133 fread(d->file, 1, PATHMAX, fp);
134 fread(d->back, 1, PATHMAX, fp);
135 fread(d->grad, 1, PATHMAX, fp);
136 fread(d->song, 1, PATHMAX, fp);
138 get_index(fp, &d->time);
139 get_index(fp, &d->goal);
140 get_index(fp, &d->score);
141 get_index(fp, &d->balls);
142 get_index(fp, &d->times);
144 fread(d->nb_version, 1, 20, fp);
151 static FILE *demo_header_open(const char *filename, struct demo *d)
155 if ((fp = fopen(filename, FMODE_RB)))
157 if (demo_header_read(fp, d))
163 strncpy(d->filename, filename, MAXSTR);
165 /* Remove the directory delimiter */
167 basename = strrchr(filename, '/');
170 basename = strrchr(filename, '\\');
174 if ((tmp = strrchr(basename, '\\')))
178 strncpy(buf, basename ? basename + 1 : filename, MAXSTR);
180 /* Remove the extension */
182 l = strlen(buf) - strlen(REPLAY_EXT);
183 if ((l > 1) && (strcmp(buf + l, REPLAY_EXT) == 0))
186 strncpy(d->name, buf, PATHMAX);
187 d->name[PATHMAX - 1] = '\0';
196 static void demo_header_write(FILE *fp, struct demo *d)
199 int version = DEMO_VERSION;
204 strftime(datestr, 20, "%Y-%m-%dT%H:%M:%S", gmtime(&d->date));
206 put_index(fp, &magic);
207 put_index(fp, &version);
208 put_index(fp, &zero);
209 put_index(fp, &zero);
210 put_index(fp, &zero);
211 put_index(fp, &d->mode);
214 put_index(fp, (int *) &d->date);
216 fwrite(datestr, 1, 20, fp);
218 fwrite(d->player, 1, MAXNAM, fp);
220 fwrite(d->shot, 1, PATHMAX, fp);
221 fwrite(d->file, 1, PATHMAX, fp);
222 fwrite(d->back, 1, PATHMAX, fp);
223 fwrite(d->grad, 1, PATHMAX, fp);
224 fwrite(d->song, 1, PATHMAX, fp);
226 put_index(fp, &d->time);
227 put_index(fp, &d->goal);
228 put_index(fp, &d->score);
229 put_index(fp, &d->balls);
230 put_index(fp, &d->times);
232 fwrite(d->nb_version, 1, 20, fp);
235 /* Update the demo header using the final level state. */
237 void demo_header_stop(FILE *fp, int coins, int timer, int state)
239 long pos = ftell(fp);
241 fseek(fp, 8, SEEK_SET);
242 put_index(fp, &timer);
243 put_index(fp, &coins);
244 put_index(fp, &state);
245 fseek(fp, pos, SEEK_SET);
248 /*---------------------------------------------------------------------------*/
250 /* Scan another file (used by demo_scan). */
252 static void demo_scan_file(const char *filename)
255 struct demo *d = &demos[count];
257 if ((fp = demo_header_open(config_user(filename), d)))
273 /* Scan the user directory for files. */
275 if ((h = FindFirstFile(config_user("*"), &d)) != INVALID_HANDLE_VALUE)
278 demo_scan_file(d.cFileName);
279 while (count < MAXDEMO && FindNextFile(h, &d));
296 /* Scan the user directory for files. */
298 if ((dp = opendir(config_user(""))))
300 while (count < MAXDEMO && (ent = readdir(dp)))
301 demo_scan_file(ent->d_name);
309 const char *demo_pick(void)
313 return (n > 0) ? demos[(rand() >> 4) % n].filename : NULL;
316 const struct demo *demo_get(int i)
318 return (0 <= i && i < count) ? &demos[i] : NULL;
321 const char *date_to_str(time_t i)
323 static char str[MAXSTR];
325 strftime(str, MAXSTR, "%c", localtime(&i));
329 /*---------------------------------------------------------------------------*/
331 int demo_exists(char *name)
336 strcpy(buf, config_user(name));
337 strcat(buf, REPLAY_EXT);
338 if ((fp = fopen(buf, "r")))
346 void demo_unique(char *name)
350 /* Generate a unique name for a new replay save. */
352 for (i = 1; i < 100; i++)
354 sprintf(name, _("replay%02d"), i);
356 if (!demo_exists(name))
361 /*---------------------------------------------------------------------------*/
363 int demo_play_init(const char *name,
364 const struct level *level,
365 const struct level_game *lg)
369 memset(&demo, 0, sizeof (demo));
371 strncpy(demo.filename, config_user(name), MAXSTR);
372 strcat(demo.filename, REPLAY_EXT);
374 demo.mode = lg->mode;
375 demo.date = time(NULL);
377 config_get_s(CONFIG_PLAYER, demo.player, MAXNAM);
379 strncpy(demo.shot, level->shot, PATHMAX);
380 strncpy(demo.file, level->file, PATHMAX);
381 strncpy(demo.back, level->back, PATHMAX);
382 strncpy(demo.grad, level->grad, PATHMAX);
383 strncpy(demo.song, level->song, PATHMAX);
385 demo.time = lg->time;
386 demo.goal = lg->goal;
387 demo.score = lg->score;
388 demo.balls = lg->balls;
389 demo.times = lg->times;
391 strncpy(demo.nb_version, VERSION, 20);
393 if (demo.filename && (demo_fp = fopen(demo.filename, FMODE_WB)))
395 demo_header_write(demo_fp, &demo);
396 audio_music_fade_to(2.0f, level->song);
397 return game_init(level, lg->time, lg->goal);
402 void demo_play_step(float dt)
406 put_float(demo_fp, &dt);
407 put_game_state(demo_fp);
411 /* Update the demo header using the final level state. */
413 void demo_play_stop(const struct level_game *lg)
417 demo_header_stop(demo_fp, lg->coins, lg->timer, lg->state);
423 int demo_play_saved(void)
425 return demo_exists(USER_REPLAY_FILE);
428 void demo_play_save(const char *name)
433 if (name && demo_exists(USER_REPLAY_FILE)
434 && strcmp(name, USER_REPLAY_FILE) != 0)
436 strncpy(src, config_user(USER_REPLAY_FILE), PATHMAX);
437 strcat(src, REPLAY_EXT);
438 strncpy(dst, config_user(name), PATHMAX);
439 strcat(dst, REPLAY_EXT);
442 if (demo_exists(name))
449 /*---------------------------------------------------------------------------*/
451 static int demo_load_level(const struct demo *demo, struct level *level)
453 if (level_load(demo->file, level))
455 level->time = demo->time;
456 level->goal = demo->goal;
462 static struct demo demo_replay; /* The current demo */
463 static struct level demo_level_replay; /* The current level demo-ed*/
465 const struct demo *curr_demo_replay(void)
470 /* Internally load a replay and fill the lg structure (if not NULL) */
472 int demo_replay_init(const char *name, struct level_game *lg)
474 demo_fp = demo_header_open(name, &demo_replay);
478 if (!demo_load_level(&demo_replay, &demo_level_replay))
483 lg->mode = demo_replay.mode;
484 lg->score = demo_replay.score;
485 lg->times = demo_replay.times;
486 lg->time = demo_replay.time;
487 lg->goal = demo_replay.goal;
489 /* A normal replay demo */
490 audio_music_fade_to(0.5f, demo_replay.song);
491 return game_init(&demo_level_replay, demo_replay.time,
494 else /* A title screen demo */
495 return game_init(&demo_level_replay, demo_replay.time, 0);
500 int demo_replay_step(float *dt)
502 const float g[3] = { 0.0f, -9.8f, 0.0f };
507 get_float(demo_fp, dt);
509 if (feof(demo_fp) == 0)
511 /* Play out current game state for particles, clock, etc. */
513 game_step(g, *dt, &sv);
515 /* Load real current game state from file. */
517 if (get_game_state(demo_fp))
524 void demo_replay_stop(int d)
531 if (d) remove(demo_replay.filename);
535 void demo_replay_dump_info(void)
537 demo_dump_info(&demo_replay);
540 /*---------------------------------------------------------------------------*/