load_surface: flip image vertically before creating surface
[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 #include <assert.h>
20
21 #include "demo.h"
22 #include "game.h"
23 #include "audio.h"
24 #include "solid.h"
25 #include "config.h"
26 #include "binary.h"
27 #include "text.h"
28 #include "common.h"
29 #include "level.h"
30
31 /*---------------------------------------------------------------------------*/
32
33 #define MAGIC           0x52424EAF
34 #define DEMO_VERSION    6
35
36 #define DATELEN 20
37
38 static FILE *demo_fp;
39
40 static struct demo demos[MAXDEMO]; /* Array of scanned demos  */
41 static int         count;          /* Number of scanned demos */
42
43 /*---------------------------------------------------------------------------*/
44
45 void demo_dump_info(const struct demo *d)
46 {
47     printf("Name:         %s\n"
48            "File:         %s\n"
49            "Time:         %d\n"
50            "Coins:        %d\n"
51            "Mode:         %d\n"
52            "State:        %d\n"
53            "Date:         %s"
54            "Player:       %s\n"
55            "Shot:         %s\n"
56            "Level:        %s\n"
57            "Time:         %d\n"
58            "Goal:         %d\n"
59            "Goal enabled: %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,
67            d->time, d->goal, d->goal_e, 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 = -1;
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->goal_e);
116         get_index(fp, &d->score);
117         get_index(fp, &d->balls);
118         get_index(fp, &d->times);
119
120         return 1;
121     }
122     return 0;
123 }
124
125 static void demo_header_write(FILE *fp, struct demo *d)
126 {
127     int magic = MAGIC;
128     int version = DEMO_VERSION;
129     int zero  = 0;
130
131     char datestr[DATELEN];
132
133     strftime(datestr, DATELEN, "%Y-%m-%dT%H:%M:%S", gmtime(&d->date));
134
135     put_index(fp, &magic);
136     put_index(fp, &version);
137     put_index(fp, &zero);
138     put_index(fp, &zero);
139     put_index(fp, &zero);
140     put_index(fp, &d->mode);
141
142     put_string(fp, d->player);
143     put_string(fp, datestr);
144
145     put_string(fp, d->shot);
146     put_string(fp, d->file);
147
148     put_index(fp, &d->time);
149     put_index(fp, &d->goal);
150     put_index(fp, &d->goal_e);
151     put_index(fp, &d->score);
152     put_index(fp, &d->balls);
153     put_index(fp, &d->times);
154 }
155
156 /*---------------------------------------------------------------------------*/
157
158 /* Scan another file (used by demo_scan). */
159
160 static void demo_scan_file(const char *filename)
161 {
162     FILE *fp;
163     struct demo *d = &demos[count];
164
165     if ((fp = fopen(config_user(filename), FMODE_RB)))
166     {
167         if (demo_header_read(fp, d))
168         {
169             strncpy(d->filename, config_user(filename), MAXSTR);
170             strncpy(d->name,
171                     base_name(text_from_locale(d->filename), REPLAY_EXT),
172                     PATHMAX);
173             d->name[PATHMAX - 1] = '\0';
174
175             count++;
176         }
177         fclose(fp);
178     }
179 }
180
181 #ifdef _WIN32
182
183 int demo_scan(void)
184 {
185     WIN32_FIND_DATA d;
186     HANDLE h;
187
188     count = 0;
189
190     /* Scan the user directory for files. */
191
192     if ((h = FindFirstFile(config_user("*"), &d)) != INVALID_HANDLE_VALUE)
193     {
194         do
195             demo_scan_file(d.cFileName);
196         while (count < MAXDEMO && FindNextFile(h, &d));
197
198         FindClose(h);
199     }
200     return count;
201 }
202
203 #else /* _WIN32 */
204 #include <dirent.h>
205
206 int demo_scan(void)
207 {
208     struct dirent *ent;
209     DIR  *dp;
210
211     count = 0;
212
213     /* Scan the user directory for files. */
214
215     if ((dp = opendir(config_user(""))))
216     {
217         while (count < MAXDEMO && (ent = readdir(dp)))
218             demo_scan_file(ent->d_name);
219
220         closedir(dp);
221     }
222     return count;
223 }
224 #endif /* _WIN32 */
225
226 const char *demo_pick(void)
227 {
228     int n = demo_scan();
229
230     return (n > 0) ? demos[(rand() >> 4) % n].filename : NULL;
231 }
232
233 const struct demo *demo_get(int i)
234 {
235     return (0 <= i && i < count) ? &demos[i] : NULL;
236 }
237
238 /*---------------------------------------------------------------------------*/
239
240 int demo_exists(const char *name)
241 {
242     char buf[MAXSTR];
243
244     strcpy(buf, config_user(name));
245     strcat(buf, REPLAY_EXT);
246
247     return file_exists(buf);
248 }
249
250 void demo_unique(char *name)
251 {
252     int i;
253
254     /* Generate a unique name for a new replay save. */
255
256     for (i = 1; i < 100; i++)
257     {
258         sprintf(name, "replay%02d", i);
259
260         if (!demo_exists(name))
261             return;
262     }
263 }
264
265 /*---------------------------------------------------------------------------*/
266
267 int demo_play_init(const char *name, const struct level *level,
268                    int mode, int t, int g, int e, int s, int b, int tt)
269 {
270     struct demo demo;
271
272     memset(&demo, 0, sizeof (demo));
273
274     strncpy(demo.filename, config_user(name), MAXSTR);
275     strcat(demo.filename, REPLAY_EXT);
276
277     demo.mode = mode;
278     demo.date = time(NULL);
279
280     config_get_s(CONFIG_PLAYER, demo.player, MAXNAM);
281
282     strncpy(demo.shot, level->shot, PATHMAX);
283     strncpy(demo.file, level->file, PATHMAX);
284
285     demo.time   = t;
286     demo.goal   = g;
287     demo.goal_e = e;
288     demo.score  = s;
289     demo.balls  = b;
290     demo.times  = tt;
291
292     if ((demo_fp = fopen(demo.filename, FMODE_WB)))
293     {
294         demo_header_write(demo_fp, &demo);
295         audio_music_fade_to(2.0f, level->song);
296         return game_init(level->file, t, e);
297     }
298     return 0;
299 }
300
301 void demo_play_step()
302 {
303     if (demo_fp)
304         input_put(demo_fp);
305 }
306
307 void demo_play_stat(int status, int coins, int timer)
308 {
309     if (demo_fp)
310     {
311         long pos = ftell(demo_fp);
312
313         fseek(demo_fp, 8, SEEK_SET);
314
315         put_index(demo_fp, &timer);
316         put_index(demo_fp, &coins);
317         put_index(demo_fp, &status);
318
319         fseek(demo_fp, pos, SEEK_SET);
320     }
321 }
322
323 void demo_play_stop(void)
324 {
325     if (demo_fp)
326     {
327         fclose(demo_fp);
328         demo_fp = NULL;
329     }
330 }
331
332 int demo_saved(void)
333 {
334     return demo_exists(USER_REPLAY_FILE);
335 }
336
337 void demo_rename(const char *name)
338 {
339     char src[MAXSTR];
340     char dst[MAXSTR];
341
342     if (name &&
343         demo_exists(USER_REPLAY_FILE) &&
344         strcmp(name, USER_REPLAY_FILE) != 0)
345     {
346         strcpy(src, config_user(USER_REPLAY_FILE));
347         strcat(src, REPLAY_EXT);
348
349         strcpy(dst, config_user(name));
350         strcat(dst, REPLAY_EXT);
351
352         file_rename(src, dst);
353     }
354 }
355
356 void demo_rename_player(const char *name, const char *player)
357 {
358     char filename[MAXSTR];
359     FILE *old_fp, *new_fp;
360     struct demo d;
361
362     assert(name);
363     assert(player);
364
365     /* TODO: make this reusable. */
366
367     filename[sizeof (filename) - 1] = '\0';
368     strncpy(filename, name, sizeof (filename) - 1);
369     strncat(filename, REPLAY_EXT, sizeof (filename) - 1 - strlen(name));
370
371     /*
372      * Write out a temporary file containing the original replay data with a
373      * new player name, then copy the resulting contents back to the original
374      * file.
375      *
376      * (It is believed that the ugliness found here is outweighed by the
377      * benefits of preserving the arbitrary-length property of all strings in
378      * the replay.  In case of doubt, FIXME.)
379      */
380
381     if ((old_fp = fopen(config_user(filename), FMODE_RB)))
382     {
383         if ((new_fp = tmpfile()))
384         {
385             if (demo_header_read(old_fp, &d))
386             {
387                 FILE *save_fp;
388
389                 /* Modify and write the header. */
390
391                 strncpy(d.player, player, sizeof (d.player));
392
393                 demo_header_write(new_fp, &d);
394
395                 /* Restore the last three fields not written by the above call. */
396
397                 /* Hack, hack, hack. */
398
399                 save_fp = demo_fp;
400                 demo_fp = new_fp;
401
402                 demo_play_stat(d.status, d.coins, d.timer);
403
404                 demo_fp = save_fp;
405
406                 /* Copy the remaining data. */
407
408                 file_copy(old_fp, new_fp);
409
410                 /* Then copy everything back. */
411
412                 if (freopen(config_user(filename), FMODE_WB, old_fp))
413                 {
414                     fseek(new_fp, 0L, SEEK_SET);
415                     file_copy(new_fp, old_fp);
416                 }
417             }
418             fclose(new_fp);
419         }
420         fclose(old_fp);
421     }
422 }
423
424 /*---------------------------------------------------------------------------*/
425
426 static struct demo  demo_replay;       /* The current demo */
427 static struct level demo_level_replay; /* The current level demo-ed*/
428
429 const struct demo *curr_demo_replay(void)
430 {
431     return &demo_replay;
432 }
433
434 static int demo_status = GAME_NONE;
435
436 int demo_replay_init(const char *name, int *g, int *m, int *b, int *s, int *tt)
437 {
438     demo_status = GAME_NONE;
439     demo_fp     = fopen(name, FMODE_RB);
440
441     if (demo_fp && demo_header_read(demo_fp, &demo_replay))
442     {
443         strncpy(demo_replay.filename, name, MAXSTR);
444         strncpy(demo_replay.name,
445                 base_name(text_from_locale(demo_replay.filename), REPLAY_EXT),
446                 PATHMAX);
447
448         if (level_load(demo_replay.file, &demo_level_replay))
449         {
450             demo_level_replay.time = demo_replay.time;
451             demo_level_replay.goal = demo_replay.goal;
452         }
453         else
454             return 0;
455
456         if (g)  *g  = demo_replay.goal;
457         if (m)  *m  = demo_replay.mode;
458         if (b)  *b  = demo_replay.balls;
459         if (s)  *s  = demo_replay.score;
460         if (tt) *tt = demo_replay.times;
461
462         if (g)
463         {
464             audio_music_fade_to(0.5f, demo_level_replay.song);
465
466             return game_init(demo_level_replay.file,
467                              demo_level_replay.time,
468                              demo_replay.goal_e);
469         }
470         else
471             return game_init(demo_level_replay.file,
472                              demo_level_replay.time, 1);
473     }
474     return 0;
475 }
476
477 int demo_replay_step(float dt)
478 {
479     const float gdn[3] = { 0.0f, -9.8f, 0.0f };
480     const float gup[3] = { 0.0f, +9.8f, 0.0f };
481
482     if (demo_fp)
483     {
484         if (input_get(demo_fp))
485         {
486             /* Play out current game state. */
487
488             switch (demo_status)
489             {
490             case GAME_NONE:
491                 demo_status = game_step(gdn, dt, 1); break;
492             case GAME_GOAL:
493                 (void)        game_step(gup, dt, 0); break;
494             default:
495                 (void)        game_step(gdn, dt, 0); break;
496             }
497
498             return 1;
499         }
500     }
501     return 0;
502 }
503
504 void demo_replay_stop(int d)
505 {
506     if (demo_fp)
507     {
508         fclose(demo_fp);
509         demo_fp = NULL;
510
511         if (d) remove(demo_replay.filename);
512     }
513 }
514
515 void demo_replay_dump_info(void)
516 {
517     demo_dump_info(&demo_replay);
518 }
519
520 /*---------------------------------------------------------------------------*/