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 void demo_header_write(FILE *fp, struct demo *d)
112 int version = DEMO_VERSION;
115 put_index(fp, &magic);
116 put_index(fp, &version);
117 put_index(fp, &zero);
118 put_index(fp, &zero);
119 put_index(fp, &zero);
120 put_index(fp, &d->mode);
121 put_index(fp, (int *) &d->date);
123 fwrite(d->player, 1, MAXNAM, fp);
125 fwrite(d->shot, 1, PATHMAX, fp);
126 fwrite(d->file, 1, PATHMAX, fp);
127 fwrite(d->back, 1, PATHMAX, fp);
128 fwrite(d->grad, 1, PATHMAX, fp);
129 fwrite(d->song, 1, PATHMAX, fp);
131 put_index(fp, &d->time);
132 put_index(fp, &d->goal);
133 put_index(fp, &d->score);
134 put_index(fp, &d->balls);
135 put_index(fp, &d->times);
137 fwrite(d->nb_version, 1, 20, fp);
140 /* Update the demo header using the final level state. */
142 void demo_header_stop(FILE *fp, int coins, int timer, int state)
144 long pos = ftell(fp);
146 fseek(fp, 8, SEEK_SET);
147 put_index(fp, &timer);
148 put_index(fp, &coins);
149 put_index(fp, &state);
150 fseek(fp, pos, SEEK_SET);
153 /*---------------------------------------------------------------------------*/
155 /* Scan another file (used by demo_scan). */
157 static void demo_scan_file(const char *filename)
160 struct demo *d = &demos[count];
162 if ((fp = fopen(config_user(filename), FMODE_RB)))
164 if (demo_header_read(fp, d))
169 strncpy(d->filename, config_user(filename), MAXSTR);
171 strncpy(buf, filename, PATHMAX);
172 l = strlen(buf) - strlen(REPLAY_EXT);
174 if ((l > 1) && (strcmp(buf + l, REPLAY_EXT) == 0))
177 strncpy(d->name, buf, PATHMAX);
178 d->name[PATHMAX - 1] = '\0';
195 /* Scan the user directory for files. */
197 if ((h = FindFirstFile(config_user("*"), &d)) != INVALID_HANDLE_VALUE)
200 demo_scan_file(d.cFileName);
201 while (count < MAXDEMO && FindNextFile(h, &d));
218 /* Scan the user directory for files. */
220 if ((dp = opendir(config_user(""))))
222 while (count < MAXDEMO && (ent = readdir(dp)))
223 demo_scan_file(ent->d_name);
231 const char *demo_pick(void)
235 return (n > 0) ? demos[(rand() >> 4) % n].filename : NULL;
238 const struct demo *demo_get(int i)
240 return (0 <= i && i < count) ? &demos[i] : NULL;
243 const char *date_to_str(time_t i)
245 static char str[MAXSTR];
246 struct tm *tm = localtime(&i);
247 strftime(str, MAXSTR, "%c", tm);
251 /*---------------------------------------------------------------------------*/
253 int demo_exists(char *name)
258 strcpy(buf, config_user(name));
259 strcat(buf, REPLAY_EXT);
260 if ((fp = fopen(buf, "r")))
268 void demo_unique(char *name)
272 /* Generate a unique name for a new replay save. */
274 for (i = 1; i < 100; i++)
276 sprintf(name, _("replay%02d"), i);
278 if (!demo_exists(name))
283 /*---------------------------------------------------------------------------*/
285 int demo_play_init(const char *name,
286 const struct level *level,
287 const struct level_game *lg)
291 memset(&demo, 0, sizeof (demo));
293 strncpy(demo.filename, config_user(name), MAXSTR);
294 strcat(demo.filename, REPLAY_EXT);
296 demo.mode = lg->mode;
297 demo.date = time(NULL);
299 config_get_s(CONFIG_PLAYER, demo.player, MAXNAM);
301 strncpy(demo.shot, level->shot, PATHMAX);
302 strncpy(demo.file, level->file, PATHMAX);
303 strncpy(demo.back, level->back, PATHMAX);
304 strncpy(demo.grad, level->grad, PATHMAX);
305 strncpy(demo.song, level->song, PATHMAX);
307 demo.time = lg->time;
308 demo.goal = lg->goal;
309 demo.score = lg->score;
310 demo.balls = lg->balls;
311 demo.times = lg->times;
313 strncpy(demo.nb_version, VERSION, 20);
315 if (demo.filename && (demo_fp = fopen(demo.filename, FMODE_WB)))
317 demo_header_write(demo_fp, &demo);
318 audio_music_fade_to(2.0f, level->song);
319 return game_init(level, lg->time, lg->goal);
324 void demo_play_step(float dt)
328 put_float(demo_fp, &dt);
329 put_game_state(demo_fp);
333 /* Update the demo header using the final level state. */
335 void demo_play_stop(const struct level_game *lg)
339 demo_header_stop(demo_fp, lg->coins, lg->timer, lg->state);
345 int demo_play_saved(void)
347 return demo_exists(USER_REPLAY_FILE);
350 void demo_play_save(const char *name)
355 if (name && demo_exists(USER_REPLAY_FILE)
356 && strcmp(name, USER_REPLAY_FILE) != 0)
358 strncpy(src, config_user(USER_REPLAY_FILE), PATHMAX);
359 strcat(src, REPLAY_EXT);
360 strncpy(dst, config_user(name), PATHMAX);
361 strcat(dst, REPLAY_EXT);
367 /*---------------------------------------------------------------------------*/
369 static int demo_load_level(const struct demo *demo, struct level *level)
371 if (level_load(demo->file, level))
373 level->time = demo->time;
374 level->goal = demo->goal;
380 static struct demo demo_replay; /* The current demo */
381 static struct level demo_level_replay; /* The current level demo-ed*/
383 const struct demo *curr_demo_replay(void)
388 /* Internally load a replay and fill the lg structure (if not NULL) */
390 int demo_replay_init(const char *name, struct level_game *lg)
392 demo_fp = fopen(name, FMODE_RB);
394 if (demo_fp && demo_header_read(demo_fp, &demo_replay))
396 strncpy(demo_replay.filename, name, MAXSTR);
398 if (!demo_load_level(&demo_replay, &demo_level_replay))
403 lg->mode = demo_replay.mode;
404 lg->score = demo_replay.score;
405 lg->times = demo_replay.times;
406 lg->time = demo_replay.time;
407 lg->goal = demo_replay.goal;
409 /* A normal replay demo */
410 audio_music_fade_to(0.5f, demo_replay.song);
411 return game_init(&demo_level_replay, demo_replay.time,
414 else /* A title screen demo */
415 return game_init(&demo_level_replay, demo_replay.time, 0);
420 int demo_replay_step(float *dt)
422 const float g[3] = { 0.0f, -9.8f, 0.0f };
427 get_float(demo_fp, dt);
429 if (feof(demo_fp) == 0)
431 /* Play out current game state for particles, clock, etc. */
433 game_step(g, *dt, &sv);
435 /* Load real current game state from file. */
437 if (get_game_state(demo_fp))
444 void demo_replay_stop(int d)
451 if (d) remove(demo_replay.filename);
455 void demo_replay_dump_info(void)
457 demo_dump_info(&demo_replay);
460 /*---------------------------------------------------------------------------*/