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