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