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