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