More docstring clean-up. Renamed REPLAY_VERSION to DEMO_VERSION.
[neverball] / ball / demo.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
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.
8  *
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.
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <time.h>
19
20 #ifndef _WIN32
21 #include <unistd.h>
22 #endif
23
24 #include "demo.h"
25 #include "game.h"
26 #include "audio.h"
27 #include "solid.h"
28 #include "config.h"
29 #include "binary.h"
30
31 /*---------------------------------------------------------------------------*/
32
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 DEMO_VERSION  1    /* Replay file format version (can change)      */
36
37 #define DEMO_FPS_CAP 200 /* FPS replay limit, keeps size down on monster systems */
38
39 static FILE *demo_fp;
40
41 static struct demo demos[MAXDEMO]; /* Array of scanned demos */
42
43 static int count; /* number of scanned demos */
44
45 /*---------------------------------------------------------------------------*/
46
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 */
50 {
51     printf("Name:         %s\n"
52            "File:         %s\n"
53            "NB Version:   %s\n"
54            "Time:         %d\n"
55            "Coins:        %d\n"
56            "Mode:         %d\n"
57            "State:        %d\n"
58            "Date:         %s"
59            "Player:       %s\n"
60            "Shot:         %s\n"
61            "Level:        %s\n"
62            "  Back:       %s\n"
63            "  Grad:       %s\n"
64            "  Song:       %s\n"
65            "Time:         %d\n"
66            "Goal:         %d\n"
67            "Score:        %d\n"
68            "Balls:        %d\n"
69            "Total Time:   %d\n",
70            d->name, d->filename,
71            d->nb_version,
72            d->timer, d->coins, d->mode, d->state, ctime(&d->date),
73            d->player,
74            d->shot, d->file, d->back, d->grad, d->song,
75            d->time, d->goal, d->score, d->balls, d->times);
76 }
77
78 /* Open a demo file, fill the demo information structure.  If success, return
79  * the file pointer positioned after the header.  If fail, return NULL. */
80
81 FILE *demo_header_read(const char *filename, struct demo *d)
82 {
83     FILE *fp;
84
85     char *basename;
86     char buf[MAXSTR];
87
88     if ((fp = fopen(filename, FMODE_RB)))
89     {
90         int magic;
91         int version;
92         int t;
93
94         get_index(fp, &magic);
95         get_index(fp, &version);
96
97         get_index(fp, &t);
98
99         if (magic == MAGIC && version == DEMO_VERSION && t)
100         {
101             d->timer = t;
102             strncpy(d->filename, filename, PATHMAX);
103
104             /* Remove the directory delimiter */
105
106             basename = strrchr(filename, '/');
107
108 #ifdef _WIN32
109             if (!basename)
110                 basename = strrchr(filename, '\\');
111             else
112             {
113                 char *tmp;
114                 if ((tmp = strrchr(basename, '\\')))
115                     basename = tmp;
116             }
117 #endif
118             strncpy(buf, basename ? basename + 1 : filename, MAXSTR);
119
120             /* Remove the extension */
121             t = strlen(buf) - strlen(REPLAY_EXT);
122             if ((t > 1) && (strcmp(buf + t, REPLAY_EXT) == 0))
123                 buf[t] = '\0';
124             strncpy(d->name, buf, PATHMAX);
125             d->name[PATHMAX - 1] = '\0';
126
127             get_index (fp, &d->coins);
128             get_index (fp, &d->state);
129             get_index (fp, &d->mode);
130             get_index (fp, (int *)&d->date);
131
132             fread(d->player, 1, MAXNAM, fp);
133
134             fread(d->shot, 1, PATHMAX, fp);
135             fread(d->file, 1, PATHMAX, fp);
136             fread(d->back, 1, PATHMAX, fp);
137             fread(d->grad, 1, PATHMAX, fp);
138             fread(d->song, 1, PATHMAX, fp);
139
140             get_index (fp, &d->time);
141             get_index (fp, &d->goal);
142             get_index (fp, &d->score);
143             get_index (fp, &d->balls);
144             get_index (fp, &d->times);
145
146             fread(d->nb_version, 1, 20, fp);
147
148             return fp;
149         }
150         fclose(fp);
151     }
152     return NULL;
153 }
154
155 /* Create a new demo file, write the demo information structure.  If success,
156  * return the file pointer positioned after the header.  If fail, return NULL.
157  * */
158
159 static FILE *demo_header_write(struct demo *d)
160 {
161     int magic = MAGIC;
162     int version = DEMO_VERSION;
163     int zero  = 0;
164
165     FILE *fp;
166
167     if (d->filename && (fp = fopen(d->filename, FMODE_WB)))
168     {
169         put_index(fp, &magic);
170         put_index(fp, &version);
171         put_index(fp, &zero);
172         put_index(fp, &zero);
173         put_index(fp, &zero);
174         put_index(fp, &d->mode);
175         put_index(fp, (int *) &d->date);
176
177         fwrite(d->player, 1, MAXNAM, fp);
178
179         fwrite(d->shot, 1, PATHMAX, fp);
180         fwrite(d->file, 1, PATHMAX, fp);
181         fwrite(d->back, 1, PATHMAX, fp);
182         fwrite(d->grad, 1, PATHMAX, fp);
183         fwrite(d->song, 1, PATHMAX, fp);
184
185         put_index(fp, &d->time);
186         put_index(fp, &d->goal);
187         put_index(fp, &d->score);
188         put_index(fp, &d->balls);
189         put_index(fp, &d->times);
190
191         fwrite(d->nb_version, 1, 20, fp);
192
193         return fp;
194     }
195     return NULL;
196 }
197
198 /* Update the demo header using the final level state. */
199
200 void demo_header_stop(FILE *fp, int coins, int timer, int state)
201 {
202     long pos = ftell(fp);
203
204     fseek(fp, 8, SEEK_SET);
205     put_index(fp, &timer);
206     put_index(fp, &coins);
207     put_index(fp, &state);
208     fseek(fp, pos, SEEK_SET);
209 }
210
211 /*---------------------------------------------------------------------------*/
212
213 /* Scan another file (used by demo_scan). */
214
215 static void demo_scan_file(const char *filename)
216 {
217     FILE *fp;
218     if ((fp = demo_header_read(config_user(filename), &demos[count])))
219     {
220         count++;
221         fclose(fp);
222     }
223 }
224
225 #ifdef _WIN32
226
227 int demo_scan(void)
228 {
229     WIN32_FIND_DATA d;
230     HANDLE h;
231
232     count = 0;
233
234     /* Scan the user directory for files. */
235
236     if ((h = FindFirstFile(config_user("*"), &d)) != INVALID_HANDLE_VALUE)
237     {
238         do
239             demo_scan_file(d.cFileName);
240         while (count < MAXDEMO && FindNextFile(h, &d));
241
242         FindClose(h);
243     }
244     return count;
245 }
246
247 #else /* _WIN32 */
248 #include <dirent.h>
249
250 int demo_scan(void)
251 {
252     struct dirent *ent;
253     DIR  *dp;
254
255     count = 0;
256
257     /* Scan the user directory for files. */
258
259     if ((dp = opendir(config_user(""))))
260     {
261         while (count < MAXDEMO && (ent = readdir(dp)))
262             demo_scan_file(ent->d_name);
263
264         closedir(dp);
265     }
266     return count;
267 }
268 #endif /* _WIN32 */
269
270 const char *demo_pick(void)
271 {
272     int n = demo_scan();
273
274     return (n > 0) ? demos[(rand() >> 4) % n].filename : NULL;
275 }
276
277 const struct demo *get_demo(int i)
278 {
279     return (0 <= i && i < count) ? &demos[i] : NULL;
280 }
281
282 const char *date_to_str(time_t i)
283 {
284     static char str[MAXSTR];
285     struct tm *tm = localtime(&i);
286     strftime (str, MAXSTR, "%c", tm);
287     return str;
288 }
289
290 /*---------------------------------------------------------------------------*/
291
292 int demo_exists(char *name)
293 {
294     FILE *fp;
295     char buf[MAXSTR];
296
297     strcpy(buf, config_user(name));
298     strcat(buf, REPLAY_EXT);
299     if ((fp = fopen(buf, "r")))
300     {
301         fclose(fp);
302         return 1;
303     }
304     return 0;
305 }
306
307 void demo_unique(char *name)
308 {
309     int i;
310
311     /* Generate a unique name for a new replay save. */
312
313     for (i = 1; i < 100; i++)
314     {
315         sprintf(name, _("replay%02d"), i);
316
317         if (!demo_exists(name))
318             return;
319     }
320 }
321
322 /*---------------------------------------------------------------------------*/
323
324 int demo_play_init(const char *name,
325                    const struct level *level,
326                    const struct level_game *lg)
327 {
328     struct demo demo;
329
330     memset(&demo, 0, sizeof(demo));
331
332     strncpy(demo.filename, config_user(name), PATHMAX);
333     strcat(demo.filename, REPLAY_EXT);
334
335     demo.mode = lg->mode;
336     demo.date = time(NULL);
337
338     config_get_s(CONFIG_PLAYER, demo.player, MAXNAM);
339
340     strncpy(demo.shot, level->shot, PATHMAX);
341     strncpy(demo.file, level->file, PATHMAX);
342     strncpy(demo.back, level->back, PATHMAX);
343     strncpy(demo.grad, level->grad, PATHMAX);
344     strncpy(demo.song, level->song, PATHMAX);
345
346     demo.time  = lg->time;
347     demo.goal  = lg->goal;
348     demo.score = lg->score;
349     demo.balls = lg->balls;
350     demo.times = lg->times;
351
352     strncpy(demo.nb_version, VERSION, 20);
353
354     demo_fp = demo_header_write(&demo);
355
356     if (demo_fp == NULL)
357         return 0;
358     else
359     {
360         audio_music_fade_to(2.0f, level->song);
361         return game_init(level, lg->time, lg->goal);
362     }
363 }
364
365 void demo_play_step(float dt)
366 {
367     static float fps_track = 0.0f;
368     static float fps_cap   = 1.0f / (float) DEMO_FPS_CAP;
369
370     if (demo_fp)
371     {
372         fps_track += dt;
373         if (fps_track > fps_cap)
374         {
375             put_float(demo_fp, &fps_track);
376             put_game_state(demo_fp);
377             fps_track = 0.0f;
378         }
379     }
380 }
381
382 void demo_play_stop(const struct level_game *lg)
383 /* Update the demo header using the final level state. */
384 {
385     if (demo_fp)
386     {
387         demo_header_stop(demo_fp, lg->coins, lg->timer, lg->state);
388         fclose(demo_fp);
389         demo_fp = NULL;
390     }
391 }
392
393 int demo_play_saved(void)
394 {
395     return demo_exists(USER_REPLAY_FILE);
396 }
397
398 void demo_play_save(const char *name)
399 {
400     char src[PATHMAX];
401     char dst[PATHMAX];
402
403     if (name && demo_exists(USER_REPLAY_FILE)
404         && strcmp(name, USER_REPLAY_FILE) != 0)
405     {
406         strncpy(src, config_user(USER_REPLAY_FILE), PATHMAX);
407         strcat(src, REPLAY_EXT);
408         strncpy(dst, config_user(name), PATHMAX);
409         strcat(dst, REPLAY_EXT);
410
411         rename(src, dst);
412     }
413 }
414
415 /*---------------------------------------------------------------------------*/
416
417 static int demo_load_level(const struct demo *demo, struct level *level)
418 /* Load the level of the demo and fill the level structure */
419 {
420     if (level_load(demo->file, level))
421     {
422         level->time = demo->time;
423         level->goal = demo->goal;
424         return 1;
425     }
426     else
427         return 0;
428 }
429
430 static struct demo  demo_replay;       /* The current demo */
431 static struct level demo_level_replay; /* The current level demo-ed*/
432
433 const struct demo *curr_demo_replay(void)
434 {
435     return &demo_replay;
436 }
437
438 /* Internally load a replay and fill the lg structure (if not NULL) */
439
440 int demo_replay_init(const char *name, struct level_game *lg)
441 {
442     if ((demo_fp = demo_header_read(name, &demo_replay)))
443     {
444         if (!demo_load_level(&demo_replay, &demo_level_replay))
445             return 0;
446
447         if (lg)
448         {
449             lg->mode = demo_replay.mode;
450             lg->score = demo_replay.score;
451             lg->times = demo_replay.times;
452             lg->time = demo_replay.time;
453             lg->goal = demo_replay.goal;
454
455             /* A normal replay demo */
456             audio_music_fade_to(0.5f, demo_replay.song);
457             return game_init(&demo_level_replay, demo_replay.time,
458                              demo_replay.goal);
459         }
460         else                    /* A title screen demo */
461             return game_init(&demo_level_replay, demo_replay.time, 0);
462     }
463
464     return 0;
465 }
466
467 int demo_replay_step(float *dt)
468 {
469     const float g[3] = { 0.0f, -9.8f, 0.0f };
470     int sv;
471
472     if (demo_fp)
473     {
474         get_float(demo_fp, dt);
475
476         if (feof(demo_fp) == 0)
477         {
478             /* Play out current game state for particles, clock, etc. */
479
480             game_step(g, *dt, &sv);
481
482             /* Load real current game state from file. */
483
484             if (get_game_state(demo_fp))
485                 return 1;
486         }
487     }
488     return 0;
489 }
490
491 void demo_replay_stop(int d)
492 {
493     if (demo_fp)
494     {
495         fclose(demo_fp);
496         demo_fp = NULL;
497
498         if (d) unlink(demo_replay.filename);
499     }
500 }
501
502 void demo_replay_dump_info(void)
503 {
504     demo_dump_info(&demo_replay);
505 }
506
507 /*---------------------------------------------------------------------------*/