Fixed the problem that in a rare case, a set is downloaded and the neverballrc is...
[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 #include "text.h"
27 #include "common.h"
28
29 /*---------------------------------------------------------------------------*/
30
31 #define MAGIC           0x52424EAF
32 #define DEMO_VERSION    5
33
34 #define DATELEN 20
35
36 static FILE *demo_fp;
37
38 static struct demo demos[MAXDEMO]; /* Array of scanned demos  */
39 static int         count;          /* Number of scanned demos */
40
41 /*---------------------------------------------------------------------------*/
42
43 void demo_dump_info(const struct demo *d)
44 {
45     printf("Name:         %s\n"
46            "File:         %s\n"
47            "Time:         %d\n"
48            "Coins:        %d\n"
49            "Mode:         %d\n"
50            "State:        %d\n"
51            "Date:         %s"
52            "Player:       %s\n"
53            "Shot:         %s\n"
54            "Level:        %s\n"
55            "  Back:       %s\n"
56            "  Grad:       %s\n"
57            "  Song:       %s\n"
58            "Time:         %d\n"
59            "Goal:         %d\n"
60            "Score:        %d\n"
61            "Balls:        %d\n"
62            "Total Time:   %d\n",
63            d->name, d->filename,
64            d->timer, d->coins, d->mode, d->status, ctime(&d->date),
65            d->player,
66            d->shot, d->file, d->back, d->grad, d->song,
67            d->time, d->goal, d->score, d->balls, d->times);
68 }
69
70 static int demo_header_read(FILE *fp, struct demo *d)
71 {
72     int magic;
73     int version;
74     int t;
75
76     struct tm date;
77     char datestr[DATELEN];
78
79     get_index(fp, &magic);
80     get_index(fp, &version);
81
82     get_index(fp, &t);
83
84     if (magic == MAGIC && version == DEMO_VERSION && t)
85     {
86         d->timer = t;
87
88         get_index(fp, &d->coins);
89         get_index(fp, &d->status);
90         get_index(fp, &d->mode);
91
92         get_string(fp, d->player, MAXNAM);
93         get_string(fp, datestr, DATELEN);
94
95         sscanf(datestr,
96                "%d-%d-%dT%d:%d:%d",
97                &date.tm_year,
98                &date.tm_mon,
99                &date.tm_mday,
100                &date.tm_hour,
101                &date.tm_min,
102                &date.tm_sec);
103
104         date.tm_year -= 1900;
105         date.tm_mon  -= 1;
106         date.tm_isdst = 0;
107
108         d->date = make_time_from_utc(&date);
109
110         get_string(fp, d->shot, PATHMAX);
111         get_string(fp, d->file, PATHMAX);
112
113         get_index(fp, &d->time);
114         get_index(fp, &d->goal);
115         get_index(fp, &d->score);
116         get_index(fp, &d->balls);
117         get_index(fp, &d->times);
118
119         return 1;
120     }
121     return 0;
122 }
123
124 static void demo_header_write(FILE *fp, struct demo *d)
125 {
126     int magic = MAGIC;
127     int version = DEMO_VERSION;
128     int zero  = 0;
129
130     char datestr[DATELEN];
131
132     strftime(datestr, DATELEN, "%Y-%m-%dT%H:%M:%S", gmtime(&d->date));
133
134     put_index(fp, &magic);
135     put_index(fp, &version);
136     put_index(fp, &zero);
137     put_index(fp, &zero);
138     put_index(fp, &zero);
139     put_index(fp, &d->mode);
140
141     put_string(fp, d->player);
142     put_string(fp, datestr);
143
144     put_string(fp, d->shot);
145     put_string(fp, d->file);
146
147     put_index(fp, &d->time);
148     put_index(fp, &d->goal);
149     put_index(fp, &d->score);
150     put_index(fp, &d->balls);
151     put_index(fp, &d->times);
152 }
153
154 /*---------------------------------------------------------------------------*/
155
156 /* Scan another file (used by demo_scan). */
157
158 static void demo_scan_file(const char *filename)
159 {
160     FILE *fp;
161     struct demo *d = &demos[count];
162
163     if ((fp = fopen(config_user(filename), FMODE_RB)))
164     {
165         if (demo_header_read(fp, d))
166         {
167             strncpy(d->filename, config_user(filename), MAXSTR);
168             strncpy(d->name,
169                     base_name(text_from_locale(d->filename), REPLAY_EXT),
170                     PATHMAX);
171             d->name[PATHMAX - 1] = '\0';
172
173             count++;
174         }
175         fclose(fp);
176     }
177 }
178
179 #ifdef _WIN32
180
181 int demo_scan(void)
182 {
183     WIN32_FIND_DATA d;
184     HANDLE h;
185
186     count = 0;
187
188     /* Scan the user directory for files. */
189
190     if ((h = FindFirstFile(config_user("*"), &d)) != INVALID_HANDLE_VALUE)
191     {
192         do
193             demo_scan_file(d.cFileName);
194         while (count < MAXDEMO && FindNextFile(h, &d));
195
196         FindClose(h);
197     }
198     return count;
199 }
200
201 #else /* _WIN32 */
202 #include <dirent.h>
203
204 int demo_scan(void)
205 {
206     struct dirent *ent;
207     DIR  *dp;
208
209     count = 0;
210
211     /* Scan the user directory for files. */
212
213     if ((dp = opendir(config_user(""))))
214     {
215         while (count < MAXDEMO && (ent = readdir(dp)))
216             demo_scan_file(ent->d_name);
217
218         closedir(dp);
219     }
220     return count;
221 }
222 #endif /* _WIN32 */
223
224 const char *demo_pick(void)
225 {
226     int n = demo_scan();
227
228     return (n > 0) ? demos[(rand() >> 4) % n].filename : NULL;
229 }
230
231 const struct demo *demo_get(int i)
232 {
233     return (0 <= i && i < count) ? &demos[i] : NULL;
234 }
235
236 /*---------------------------------------------------------------------------*/
237
238 int demo_exists(const char *name)
239 {
240     char buf[MAXSTR];
241
242     strcpy(buf, config_user(name));
243     strcat(buf, REPLAY_EXT);
244
245     return file_exists(buf);
246 }
247
248 void demo_unique(char *name)
249 {
250     int i;
251
252     /* Generate a unique name for a new replay save. */
253
254     for (i = 1; i < 100; i++)
255     {
256         sprintf(name, "replay%02d", i);
257
258         if (!demo_exists(name))
259             return;
260     }
261 }
262
263 /*---------------------------------------------------------------------------*/
264
265 int demo_play_init(const char *name,
266                    const struct level *level,
267                    const struct level_game *lg)
268 {
269     struct demo demo;
270
271     memset(&demo, 0, sizeof (demo));
272
273     strncpy(demo.filename, config_user(name), MAXSTR);
274     strcat(demo.filename, REPLAY_EXT);
275
276     demo.mode = lg->mode;
277     demo.date = time(NULL);
278
279     config_get_s(CONFIG_PLAYER, demo.player, MAXNAM);
280
281     strncpy(demo.shot, level->shot, PATHMAX);
282     strncpy(demo.file, level->file, PATHMAX);
283     strncpy(demo.back, level->back, PATHMAX);
284     strncpy(demo.grad, level->grad, PATHMAX);
285     strncpy(demo.song, level->song, PATHMAX);
286
287     demo.time  = lg->time;
288     demo.goal  = lg->goal;
289     demo.score = lg->score;
290     demo.balls = lg->balls;
291     demo.times = lg->times;
292
293     if ((demo_fp = fopen(demo.filename, FMODE_WB)))
294     {
295         demo_header_write(demo_fp, &demo);
296         audio_music_fade_to(2.0f, level->song);
297         return game_init(level, lg->time, lg->goal);
298     }
299     return 0;
300 }
301
302 void demo_play_step()
303 {
304     if (demo_fp)
305         input_put(demo_fp);
306 }
307
308 void demo_play_stat(const struct level_game *lg)
309 {
310     if (demo_fp)
311     {
312         long pos = ftell(demo_fp);
313
314         fseek(demo_fp, 8, SEEK_SET);
315
316         put_index(demo_fp, &lg->timer);
317         put_index(demo_fp, &lg->coins);
318         put_index(demo_fp, &lg->status);
319
320         fseek(demo_fp, pos, SEEK_SET);
321     }
322 }
323
324 void demo_play_stop(void)
325 {
326     if (demo_fp)
327     {
328         fclose(demo_fp);
329         demo_fp = NULL;
330     }
331 }
332
333 int demo_saved(void)
334 {
335     return demo_exists(USER_REPLAY_FILE);
336 }
337
338 void demo_rename(const char *name)
339 {
340     char src[MAXSTR];
341     char dst[MAXSTR];
342
343     if (name &&
344         demo_exists(USER_REPLAY_FILE) &&
345         strcmp(name, USER_REPLAY_FILE) != 0)
346     {
347         strcpy(src, config_user(USER_REPLAY_FILE));
348         strcat(src, REPLAY_EXT);
349
350         strcpy(dst, config_user(name));
351         strcat(dst, REPLAY_EXT);
352
353         file_rename(src, dst);
354     }
355 }
356
357 /*---------------------------------------------------------------------------*/
358
359 static int demo_load_level(const struct demo *demo, struct level *level)
360 {
361     if (level_load(demo->file, level))
362     {
363         level->time = demo->time;
364         level->goal = demo->goal;
365         return 1;
366     }
367     return 0;
368 }
369
370 static struct demo  demo_replay;       /* The current demo */
371 static struct level demo_level_replay; /* The current level demo-ed*/
372
373 const struct demo *curr_demo_replay(void)
374 {
375     return &demo_replay;
376 }
377
378 static int demo_status = GAME_NONE;
379
380 int demo_replay_init(const char *name, struct level_game *lg)
381 {
382     demo_status = GAME_NONE;
383     demo_fp     = fopen(name, FMODE_RB);
384
385     if (demo_fp && demo_header_read(demo_fp, &demo_replay))
386     {
387         strncpy(demo_replay.filename, name, MAXSTR);
388         strncpy(demo_replay.name,
389                 base_name(text_from_locale(demo_replay.filename), REPLAY_EXT),
390                 PATHMAX);
391
392         if (!demo_load_level(&demo_replay, &demo_level_replay))
393             return 0;
394
395         if (lg)
396         {
397             lg->mode  = demo_replay.mode;
398             lg->score = demo_replay.score;
399             lg->times = demo_replay.times;
400             lg->time  = demo_replay.time;
401             lg->goal  = demo_replay.goal;
402
403             /* A normal replay demo */
404             audio_music_fade_to(0.5f, demo_level_replay.song);
405             return game_init(&demo_level_replay, demo_replay.time,
406                              demo_replay.goal);
407         }
408         else /* A title screen demo */
409             return game_init(&demo_level_replay, demo_replay.time, 0);
410     }
411     return 0;
412 }
413
414 int demo_replay_step(float dt)
415 {
416     const float gdn[3] = { 0.0f, -9.8f, 0.0f };
417     const float gup[3] = { 0.0f, +9.8f, 0.0f };
418
419     if (demo_fp)
420     {
421         if (input_get(demo_fp))
422         {
423             /* Play out current game state. */
424
425             switch (demo_status)
426             {
427             case GAME_NONE:
428                 demo_status = game_step(gdn, dt, 1); break;
429             case GAME_GOAL:
430                 (void)        game_step(gup, dt, 0); break;
431             default:
432                 (void)        game_step(gdn, dt, 0); break;
433             }
434
435             return 1;
436         }
437     }
438     return 0;
439 }
440
441 void demo_replay_stop(int d)
442 {
443     if (demo_fp)
444     {
445         fclose(demo_fp);
446         demo_fp = NULL;
447
448         if (d) remove(demo_replay.filename);
449     }
450 }
451
452 void demo_replay_dump_info(void)
453 {
454     demo_dump_info(&demo_replay);
455 }
456
457 /*---------------------------------------------------------------------------*/