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