rnd-20030426-2-src
[rocksndiamonds.git] / src / tape.c
1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back!               *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * tape.c                                                   *
12 ***********************************************************/
13
14 #include "libgame/libgame.h"
15
16 #include "tape.h"
17 #include "init.h"
18 #include "game.h"
19 #include "tools.h"
20 #include "files.h"
21 #include "network.h"
22 #include "cartoons.h"
23
24 /* tape button identifiers */
25 #define TAPE_CTRL_ID_EJECT              0
26 #define TAPE_CTRL_ID_INDEX              1
27 #define TAPE_CTRL_ID_STOP               2
28 #define TAPE_CTRL_ID_PAUSE              3
29 #define TAPE_CTRL_ID_RECORD             4
30 #define TAPE_CTRL_ID_PLAY               5
31
32 #define NUM_TAPE_BUTTONS                6
33
34 /* values for tape handling */
35 #define TAPE_PAUSE_SECONDS_BEFORE_DEATH         5
36
37 /* forward declaration for internal use */
38 static void HandleTapeButtons(struct GadgetInfo *);
39 static void TapeStopIndexSearch();
40
41 static struct GadgetInfo *tape_gadget[NUM_TAPE_BUTTONS];
42
43
44 /* ========================================================================= */
45 /* video tape definitions                                                    */
46 /* ========================================================================= */
47
48 /* some positions in the video tape control window */
49 #define VIDEO_DATE_LABEL_XPOS   (VIDEO_DISPLAY1_XPOS)
50 #define VIDEO_DATE_LABEL_YPOS   (VIDEO_DISPLAY1_YPOS)
51 #define VIDEO_DATE_LABEL_XSIZE  (VIDEO_DISPLAY_XSIZE)
52 #define VIDEO_DATE_LABEL_YSIZE  (VIDEO_DISPLAY_YSIZE)
53 #define VIDEO_DATE_XPOS         (VIDEO_DISPLAY1_XPOS + 2)
54 #define VIDEO_DATE_YPOS         (VIDEO_DISPLAY1_YPOS + 14)
55 #define VIDEO_DATE_XSIZE        (VIDEO_DISPLAY_XSIZE)
56 #define VIDEO_DATE_YSIZE        16
57 #define VIDEO_REC_LABEL_XPOS    (VIDEO_DISPLAY2_XPOS)
58 #define VIDEO_REC_LABEL_YPOS    (VIDEO_DISPLAY2_YPOS)
59 #define VIDEO_REC_LABEL_XSIZE   20
60 #define VIDEO_REC_LABEL_YSIZE   12
61 #define VIDEO_REC_SYMBOL_XPOS   (VIDEO_DISPLAY2_XPOS + 20)
62 #define VIDEO_REC_SYMBOL_YPOS   (VIDEO_DISPLAY2_YPOS)
63 #define VIDEO_REC_SYMBOL_XSIZE  16
64 #define VIDEO_REC_SYMBOL_YSIZE  16
65 #define VIDEO_PLAY_LABEL_XPOS   (VIDEO_DISPLAY2_XPOS + 65)
66 #define VIDEO_PLAY_LABEL_YPOS   (VIDEO_DISPLAY2_YPOS)
67 #define VIDEO_PLAY_LABEL_XSIZE  22
68 #define VIDEO_PLAY_LABEL_YSIZE  12
69 #define VIDEO_PLAY_SYMBOL_XPOS  (VIDEO_DISPLAY2_XPOS + 52)
70 #define VIDEO_PLAY_SYMBOL_YPOS  (VIDEO_DISPLAY2_YPOS)
71 #define VIDEO_PLAY_SYMBOL_XSIZE 11
72 #define VIDEO_PLAY_SYMBOL_YSIZE 13
73 #define VIDEO_PAUSE_LABEL_XPOS  (VIDEO_DISPLAY2_XPOS)
74 #define VIDEO_PAUSE_LABEL_YPOS  (VIDEO_DISPLAY2_YPOS + 20)
75 #define VIDEO_PAUSE_LABEL_XSIZE 35
76 #define VIDEO_PAUSE_LABEL_YSIZE 8
77 #define VIDEO_PAUSE_SYMBOL_XPOS (VIDEO_DISPLAY2_XPOS + 35)
78 #define VIDEO_PAUSE_SYMBOL_YPOS (VIDEO_DISPLAY2_YPOS)
79 #define VIDEO_PAUSE_SYMBOL_XSIZE 17
80 #define VIDEO_PAUSE_SYMBOL_YSIZE 13
81 #define VIDEO_TIME_XPOS         (VIDEO_DISPLAY2_XPOS + 39)
82 #define VIDEO_TIME_YPOS         (VIDEO_DISPLAY2_YPOS + 14)
83 #define VIDEO_TIME_XSIZE        50
84 #define VIDEO_TIME_YSIZE        16
85
86 /* special */
87 #define VIDEO_PBEND_LABEL_XPOS  5
88 #define VIDEO_PBEND_LABEL_YPOS  220
89 #define VIDEO_PBEND_LABEL_XSIZE 35
90 #define VIDEO_PBEND_LABEL_YSIZE 30
91
92 #define VIDEO_STATE_OFF         (VIDEO_STATE_PLAY_OFF   |       \
93                                  VIDEO_STATE_REC_OFF    |       \
94                                  VIDEO_STATE_PAUSE_OFF  |       \
95                                  VIDEO_STATE_FFWD_OFF   |       \
96                                  VIDEO_STATE_PBEND_OFF  |       \
97                                  VIDEO_STATE_DATE_OFF   |       \
98                                  VIDEO_STATE_TIME_OFF)
99 #define VIDEO_PRESS_OFF         (VIDEO_PRESS_PLAY_OFF   |       \
100                                  VIDEO_PRESS_REC_OFF    |       \
101                                  VIDEO_PRESS_PAUSE_OFF  |       \
102                                  VIDEO_PRESS_STOP_OFF   |       \
103                                  VIDEO_PRESS_EJECT_OFF)
104 #define VIDEO_ALL_OFF           (VIDEO_STATE_OFF | VIDEO_PRESS_OFF)
105
106 #define VIDEO_STATE_ON          (VIDEO_STATE_PLAY_ON    |       \
107                                  VIDEO_STATE_REC_ON     |       \
108                                  VIDEO_STATE_PAUSE_ON   |       \
109                                  VIDEO_STATE_FFWD_ON    |       \
110                                  VIDEO_STATE_PBEND_ON   |       \
111                                  VIDEO_STATE_DATE_ON    |       \
112                                  VIDEO_STATE_TIME_ON)
113 #define VIDEO_PRESS_ON          (VIDEO_PRESS_PLAY_ON    |       \
114                                  VIDEO_PRESS_REC_ON     |       \
115                                  VIDEO_PRESS_PAUSE_ON   |       \
116                                  VIDEO_PRESS_STOP_ON    |       \
117                                  VIDEO_PRESS_EJECT_ON)
118 #define VIDEO_ALL_ON            (VIDEO_STATE_ON | VIDEO_PRESS_ON)
119
120 #define VIDEO_STATE             (VIDEO_STATE_ON | VIDEO_STATE_OFF)
121 #define VIDEO_PRESS             (VIDEO_PRESS_ON | VIDEO_PRESS_OFF)
122 #define VIDEO_ALL               (VIDEO_ALL_ON | VIDEO_ALL_OFF)
123
124
125 /* ========================================================================= */
126 /* video display functions                                                   */
127 /* ========================================================================= */
128
129 void DrawVideoDisplay(unsigned long state, unsigned long value)
130 {
131   int i;
132   int part_label = 0, part_symbol = 1;
133   int xpos = 0, ypos = 1, xsize = 2, ysize = 3;
134   static char *monatsname[12] =
135   {
136     "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
137     "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
138   };
139   static int video_pos[5][2][4] =
140   {
141     {{ VIDEO_PLAY_LABEL_XPOS, VIDEO_PLAY_LABEL_YPOS,
142        VIDEO_PLAY_LABEL_XSIZE,VIDEO_PLAY_LABEL_YSIZE },
143      { VIDEO_PLAY_SYMBOL_XPOS, VIDEO_PLAY_SYMBOL_YPOS,
144        VIDEO_PLAY_SYMBOL_XSIZE,VIDEO_PLAY_SYMBOL_YSIZE }},
145
146     {{ VIDEO_REC_LABEL_XPOS, VIDEO_REC_LABEL_YPOS,
147        VIDEO_REC_LABEL_XSIZE,VIDEO_REC_LABEL_YSIZE },
148      { VIDEO_REC_SYMBOL_XPOS, VIDEO_REC_SYMBOL_YPOS,
149        VIDEO_REC_SYMBOL_XSIZE,VIDEO_REC_SYMBOL_YSIZE }},
150
151     {{ VIDEO_PAUSE_LABEL_XPOS, VIDEO_PAUSE_LABEL_YPOS,
152        VIDEO_PAUSE_LABEL_XSIZE,VIDEO_PAUSE_LABEL_YSIZE },
153      { VIDEO_PAUSE_SYMBOL_XPOS, VIDEO_PAUSE_SYMBOL_YPOS,
154        VIDEO_PAUSE_SYMBOL_XSIZE,VIDEO_PAUSE_SYMBOL_YSIZE }},
155
156     {{ VIDEO_DATE_LABEL_XPOS, VIDEO_DATE_LABEL_YPOS,
157        VIDEO_DATE_LABEL_XSIZE,VIDEO_DATE_LABEL_YSIZE },
158      { VIDEO_DATE_XPOS, VIDEO_DATE_YPOS,
159        VIDEO_DATE_XSIZE,VIDEO_DATE_YSIZE }},
160
161     {{ 0,0,
162        0,0 },
163      { VIDEO_TIME_XPOS, VIDEO_TIME_YPOS,
164        VIDEO_TIME_XSIZE,VIDEO_TIME_YSIZE }}
165   };
166
167   if (state & VIDEO_STATE_PBEND_OFF)
168   {
169     int cx = DOOR_GFX_PAGEX3, cy = DOOR_GFX_PAGEY2;
170
171     BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
172                cx + VIDEO_REC_LABEL_XPOS,
173                cy + VIDEO_REC_LABEL_YPOS,
174                VIDEO_PBEND_LABEL_XSIZE,
175                VIDEO_PBEND_LABEL_YSIZE,
176                VX + VIDEO_REC_LABEL_XPOS,
177                VY + VIDEO_REC_LABEL_YPOS);
178   }
179
180   for(i=0;i<10;i++)
181   {
182     if (state & (1<<i))
183     {
184       int pos = i/2, cx, cy = DOOR_GFX_PAGEY2;
185
186       if (i%2)                  /* i ungerade => STATE_ON / PRESS_OFF */
187         cx = DOOR_GFX_PAGEX4;
188       else
189         cx = DOOR_GFX_PAGEX3;   /* i gerade => STATE_OFF / PRESS_ON */
190
191       if (video_pos[pos][part_label][0] && value != VIDEO_DISPLAY_SYMBOL_ONLY)
192         BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
193                    cx + video_pos[pos][part_label][xpos],
194                    cy + video_pos[pos][part_label][ypos],
195                    video_pos[pos][part_label][xsize],
196                    video_pos[pos][part_label][ysize],
197                    VX + video_pos[pos][part_label][xpos],
198                    VY + video_pos[pos][part_label][ypos]);
199       if (video_pos[pos][part_symbol][0] && value != VIDEO_DISPLAY_LABEL_ONLY)
200         BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
201                    cx + video_pos[pos][part_symbol][xpos],
202                    cy + video_pos[pos][part_symbol][ypos],
203                    video_pos[pos][part_symbol][xsize],
204                    video_pos[pos][part_symbol][ysize],
205                    VX + video_pos[pos][part_symbol][xpos],
206                    VY + video_pos[pos][part_symbol][ypos]);
207     }
208   }
209
210   if (state & VIDEO_STATE_FFWD_ON)
211   {
212     int cx = DOOR_GFX_PAGEX4, cy = DOOR_GFX_PAGEY2;
213
214     BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
215                cx + VIDEO_PLAY_SYMBOL_XPOS,
216                cy + VIDEO_PLAY_SYMBOL_YPOS,
217                VIDEO_PLAY_SYMBOL_XSIZE - 2,
218                VIDEO_PLAY_SYMBOL_YSIZE,
219                VX + VIDEO_PLAY_SYMBOL_XPOS - 9,
220                VY + VIDEO_PLAY_SYMBOL_YPOS);
221   }
222
223   if (state & VIDEO_STATE_PBEND_ON)
224   {
225     int cx = DOOR_GFX_PAGEX6, cy = DOOR_GFX_PAGEY1;
226
227     BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
228                cx + VIDEO_PBEND_LABEL_XPOS,
229                cy + VIDEO_PBEND_LABEL_YPOS,
230                VIDEO_PBEND_LABEL_XSIZE,
231                VIDEO_PBEND_LABEL_YSIZE,
232                VX + VIDEO_REC_LABEL_XPOS,
233                VY + VIDEO_REC_LABEL_YPOS);
234   }
235
236   if (state & VIDEO_STATE_DATE_ON)
237   {
238     int tag = value % 100;
239     int monat = (value/100) % 100;
240     int jahr = (value/10000);
241
242     DrawText(VX + VIDEO_DATE_XPOS, VY + VIDEO_DATE_YPOS,
243              int2str(tag, 2), FONT_TAPE_RECORDER);
244     DrawText(VX + VIDEO_DATE_XPOS + 27, VY + VIDEO_DATE_YPOS,
245              monatsname[monat], FONT_TAPE_RECORDER);
246     DrawText(VX + VIDEO_DATE_XPOS + 64, VY + VIDEO_DATE_YPOS,
247              int2str(jahr, 2), FONT_TAPE_RECORDER);
248   }
249
250   if (state & VIDEO_STATE_TIME_ON)
251   {
252     int min = value / 60;
253     int sec = value % 60;
254
255     DrawText(VX + VIDEO_TIME_XPOS, VY + VIDEO_TIME_YPOS,
256              int2str(min, 2), FONT_TAPE_RECORDER);
257     DrawText(VX + VIDEO_TIME_XPOS + 27, VY + VIDEO_TIME_YPOS,
258              int2str(sec, 2), FONT_TAPE_RECORDER);
259   }
260
261   if (state & VIDEO_STATE_DATE)
262     redraw_mask |= REDRAW_VIDEO_1;
263   if ((state & ~VIDEO_STATE_DATE) & VIDEO_STATE)
264     redraw_mask |= REDRAW_VIDEO_2;
265   if (state & VIDEO_PRESS)
266     redraw_mask |= REDRAW_VIDEO_3;
267 }
268
269 void DrawCompleteVideoDisplay()
270 {
271   BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
272              DOOR_GFX_PAGEX3, DOOR_GFX_PAGEY2,
273              gfx.vxsize, gfx.vysize, gfx.vx, gfx.vy);
274   BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
275              DOOR_GFX_PAGEX4 + VIDEO_CONTROL_XPOS,
276              DOOR_GFX_PAGEY2 + VIDEO_CONTROL_YPOS,
277              VIDEO_CONTROL_XSIZE, VIDEO_CONTROL_YSIZE,
278              gfx.vx + VIDEO_CONTROL_XPOS, gfx.vy + VIDEO_CONTROL_YPOS);
279
280   DrawVideoDisplay(VIDEO_ALL_OFF, 0);
281   if (tape.date && tape.length)
282   {
283     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
284     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
285   }
286
287   BlitBitmap(drawto, bitmap_db_door, gfx.vx, gfx.vy, gfx.vxsize, gfx.vysize,
288              DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY2);
289 }
290
291
292 /* ========================================================================= */
293 /* tape control functions                                                    */
294 /* ========================================================================= */
295
296 void TapeErase()
297 {
298   time_t epoch_seconds = time(NULL);
299   struct tm *time = localtime(&epoch_seconds);
300   int i;
301
302   tape.length = 0;
303   tape.counter = 0;
304
305   if (leveldir_current)
306     setString(&tape.level_identifier, leveldir_current->identifier);
307
308   tape.level_nr = level_nr;
309   tape.pos[tape.counter].delay = 0;
310   tape.changed = TRUE;
311
312   tape.date = 10000*(time->tm_year % 100) + 100*time->tm_mon + time->tm_mday;
313   tape.random_seed = InitRND(NEW_RANDOMIZE);
314
315   tape.file_version = FILE_VERSION_ACTUAL;
316   tape.game_version = GAME_VERSION_ACTUAL;
317   tape.engine_version = level.game_version;
318
319   for(i=0; i<MAX_PLAYERS; i++)
320     tape.player_participates[i] = FALSE;
321 }
322
323 static void TapeRewind()
324 {
325   tape.counter = 0;
326   tape.delay_played = 0;
327   tape.pause_before_death = FALSE;
328   tape.recording = FALSE;
329   tape.playing = FALSE;
330   tape.fast_forward = FALSE;
331   tape.index_search = FALSE;
332   tape.auto_play = (global.autoplay_leveldir != NULL);
333   tape.auto_play_level_solved = FALSE;
334   tape.quick_resume = FALSE;
335   tape.single_step = FALSE;
336
337   InitRND(tape.random_seed);
338 }
339
340 void TapeStartRecording()
341 {
342   if (!TAPE_IS_STOPPED(tape))
343     TapeStop();
344
345   TapeErase();
346   TapeRewind();
347
348   tape.recording = TRUE;
349
350   DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
351   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
352   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
353   MapTapeIndexButton();
354
355   SetDrawDeactivationMask(REDRAW_NONE);
356   audio.sound_deactivated = FALSE;
357 }
358
359 static void TapeStartGameRecording()
360 {
361   TapeStartRecording();
362
363 #if defined(PLATFORM_UNIX)
364   if (options.network)
365     SendToServer_StartPlaying();
366   else
367 #endif
368   {
369     game_status = GAME_MODE_PLAYING;
370     StopAnimation();
371     InitGame();
372   }
373 }
374
375 static void TapeAppendRecording()
376 {
377   if (!tape.playing || !tape.pausing)
378     return;
379
380   tape.pos[tape.counter].delay = tape.delay_played;
381   tape.playing = FALSE;
382   tape.recording = TRUE;
383   tape.changed = TRUE;
384
385   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON,0);
386 }
387
388 void TapeHaltRecording()
389 {
390   if (!tape.recording)
391     return;
392
393   tape.counter++;
394   tape.pos[tape.counter].delay = 0;
395
396   tape.length = tape.counter;
397   tape.length_seconds = GetTapeLength();
398 }
399
400 void TapeStopRecording()
401 {
402   if (!tape.recording)
403     return;
404
405   TapeHaltRecording();
406
407   tape.recording = FALSE;
408   tape.pausing = FALSE;
409
410   DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
411   MapTapeEjectButton();
412 }
413
414 void TapeRecordAction(byte action[MAX_PLAYERS])
415 {
416   int i;
417
418   if (!tape.recording || tape.pausing)
419     return;
420
421   if (tape.counter >= MAX_TAPELEN - 1)
422   {
423     TapeStopRecording();
424     return;
425   }
426
427   if (tape.pos[tape.counter].delay > 0)         /* already stored action */
428   {
429     boolean changed_events = FALSE;
430
431     for(i=0; i<MAX_PLAYERS; i++)
432       if (tape.pos[tape.counter].action[i] != action[i])
433         changed_events = TRUE;
434
435     if (changed_events || tape.pos[tape.counter].delay >= 255)
436     {
437       tape.counter++;
438       tape.pos[tape.counter].delay = 0;
439     }
440     else
441       tape.pos[tape.counter].delay++;
442   }
443
444   if (tape.pos[tape.counter].delay == 0)        /* store new action */
445   {
446     for(i=0; i<MAX_PLAYERS; i++)
447       tape.pos[tape.counter].action[i] = action[i];
448
449     tape.pos[tape.counter].delay++;
450   }
451 }
452
453 void TapeTogglePause(boolean toggle_manual)
454 {
455   unsigned long state;
456
457 #if 0
458   if (!tape.recording && !tape.playing)
459     return;
460 #endif
461
462   tape.pausing = !tape.pausing;
463   tape.fast_forward = FALSE;
464   tape.pause_before_death = FALSE;
465
466   if (tape.single_step && toggle_manual)
467     tape.single_step = FALSE;
468
469   state = (tape.pausing ? VIDEO_STATE_PAUSE_ON : VIDEO_STATE_PAUSE_OFF);
470   if (tape.playing)
471     state |= VIDEO_STATE_PBEND_OFF;
472
473   DrawVideoDisplay(state, 0);
474
475   if (tape.index_search)
476   {
477     TapeStopIndexSearch();
478
479     if (tape.quick_resume)
480     {
481       tape.quick_resume = FALSE;
482
483       TapeAppendRecording();
484       TapeTogglePause(toggle_manual);
485     }
486   }
487 }
488
489 void TapeStartPlaying()
490 {
491   if (TAPE_IS_EMPTY(tape))
492     return;
493
494   if (!TAPE_IS_STOPPED(tape))
495     TapeStop();
496
497   TapeRewind();
498
499   tape.playing = TRUE;
500
501   DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
502   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
503   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
504   MapTapeIndexButton();
505
506   SetDrawDeactivationMask(REDRAW_NONE);
507   audio.sound_deactivated = FALSE;
508 }
509
510 static void TapeStartGamePlaying()
511 {
512   TapeStartPlaying();
513
514   game_status = GAME_MODE_PLAYING;
515   StopAnimation();
516   InitGame();
517 }
518
519 void TapeStopPlaying()
520 {
521   if (!tape.playing)
522     return;
523
524   tape.playing = FALSE;
525   tape.pausing = FALSE;
526
527   if (tape.index_search)
528     TapeStopIndexSearch();
529
530   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
531   MapTapeEjectButton();
532 }
533
534 byte *TapePlayAction()
535 {
536   static byte action[MAX_PLAYERS];
537   int i;
538
539   if (!tape.playing || tape.pausing)
540     return NULL;
541
542   if (tape.pause_before_death)  /* STOP 10s BEFORE PLAYER GETS KILLED... */
543   {
544     if (!(FrameCounter % 20))
545     {
546       if ((FrameCounter / 20) % 2)
547         DrawVideoDisplay(VIDEO_STATE_PBEND_ON, VIDEO_DISPLAY_LABEL_ONLY);
548       else
549         DrawVideoDisplay(VIDEO_STATE_PBEND_OFF, VIDEO_DISPLAY_LABEL_ONLY);
550     }
551
552     if (TimePlayed > tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
553     {
554       TapeTogglePause(TAPE_TOGGLE_MANUAL);
555       return NULL;
556     }
557   }
558
559   if (tape.counter >= tape.length)      /* end of tape reached */
560   {
561     if (tape.index_search && !tape.auto_play)
562       TapeTogglePause(TAPE_TOGGLE_MANUAL);
563     else
564       TapeStop();
565
566     return NULL;
567   }
568
569   for(i=0; i<MAX_PLAYERS; i++)
570     action[i] = tape.pos[tape.counter].action[i];
571
572   tape.delay_played++;
573   if (tape.delay_played >= tape.pos[tape.counter].delay)
574   {
575     tape.counter++;
576     tape.delay_played = 0;
577   }
578
579   return action;
580 }
581
582 void TapeStop()
583 {
584   TapeStopRecording();
585   TapeStopPlaying();
586
587   DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0);
588   if (tape.date && tape.length)
589   {
590     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
591     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
592   }
593
594   if (tape.auto_play)
595     AutoPlayTape();     /* continue automatically playing next tape */
596 }
597
598 unsigned int GetTapeLength()
599 {
600   unsigned int tape_length = 0;
601   int i;
602
603   if (TAPE_IS_EMPTY(tape))
604     return(0);
605
606   for(i=0;i<tape.length;i++)
607     tape_length += tape.pos[i].delay;
608
609   return(tape_length * GAME_FRAME_DELAY / 1000);
610 }
611
612 static void TapeStartIndexSearch()
613 {
614   tape.index_search = TRUE;
615
616   if (!tape.fast_forward || tape.pause_before_death)
617   {
618     tape.pausing = FALSE;
619
620     SetDrawDeactivationMask(REDRAW_FIELD);
621     audio.sound_deactivated = TRUE;
622   }
623 }
624
625 static void TapeStopIndexSearch()
626 {
627   tape.index_search = FALSE;
628
629   SetDrawDeactivationMask(REDRAW_NONE);
630   audio.sound_deactivated = FALSE;
631
632   RedrawPlayfield(TRUE, 0,0,0,0);
633   DrawGameDoorValues();
634 }
635
636 static void TapeSingleStep()
637 {
638   if (options.network)
639     return;
640
641   if (!tape.pausing)
642     TapeTogglePause(TAPE_TOGGLE_MANUAL);
643
644   tape.single_step = !tape.single_step;
645 }
646
647 void TapeQuickSave()
648 {
649   if (game_status == GAME_MODE_PLAYING)
650   {
651     if (tape.recording)
652       TapeHaltRecording();      /* prepare tape for saving on-the-fly */
653
654     if (TAPE_IS_EMPTY(tape))
655       Request("No tape that can be saved !", REQ_CONFIRM);
656     else
657       SaveTape(tape.level_nr);
658   }
659   else if (game_status == GAME_MODE_MAIN)
660     Request("No game that can be saved !", REQ_CONFIRM);
661 }
662
663 void TapeQuickLoad()
664 {
665   if (game_status == GAME_MODE_PLAYING || game_status == GAME_MODE_MAIN)
666   {
667     TapeStop();
668     TapeErase();
669
670     LoadTape(level_nr);
671     if (!TAPE_IS_EMPTY(tape))
672     {
673       TapeStartGamePlaying();
674       TapeStartIndexSearch();
675
676       tape.quick_resume = TRUE;
677     }
678     else
679       Request("No tape for this level !", REQ_CONFIRM);
680   }
681 }
682
683
684 /* ------------------------------------------------------------------------- *
685  * tape autoplay functions
686  * ------------------------------------------------------------------------- */
687
688 #define MAX_NUM_AUTOPLAY_LEVELS         1000
689
690 void AutoPlayTape()
691 {
692   static LevelDirTree *autoplay_leveldir = NULL;
693   static boolean autoplay_initialized = FALSE;
694   static int autoplay_level_nr = -1;
695   static int num_levels_played = 0;
696   static int num_levels_solved = 0;
697   static boolean levels_failed[MAX_NUM_AUTOPLAY_LEVELS];
698   int i;
699
700   if (autoplay_initialized)
701   {
702     /* just finished auto-playing tape */
703     printf("%s.\n", tape.auto_play_level_solved ? "solved" : "NOT SOLVED");
704
705     num_levels_played++;
706     if (tape.auto_play_level_solved)
707       num_levels_solved++;
708     else if (level_nr >= 0 && level_nr < MAX_NUM_AUTOPLAY_LEVELS)
709       levels_failed[level_nr] = TRUE;
710   }
711   else
712   {
713     DrawCompleteVideoDisplay();
714     audio.sound_enabled = FALSE;
715
716     autoplay_leveldir = getTreeInfoFromIdentifier(leveldir_first,
717                                                   global.autoplay_leveldir);
718
719     if (autoplay_leveldir == NULL)
720       Error(ERR_EXIT, "no such level identifier: '%s'",
721             global.autoplay_leveldir);
722
723     leveldir_current = autoplay_leveldir;
724
725     if (global.autoplay_level_nr != -1)
726     {
727       autoplay_leveldir->first_level = global.autoplay_level_nr;
728       autoplay_leveldir->last_level  = global.autoplay_level_nr;
729     }
730
731     autoplay_level_nr = autoplay_leveldir->first_level;
732
733     printf_line("=", 79);
734     printf("Automatically playing level tapes\n");
735     printf_line("-", 79);
736     printf("Level series identifier: '%s'\n", autoplay_leveldir->identifier);
737     printf("Level series name:       '%s'\n", autoplay_leveldir->name);
738     printf("Level series author:     '%s'\n", autoplay_leveldir->author);
739     printf("Number of levels:        %d\n",   autoplay_leveldir->levels);
740     printf_line("=", 79);
741     printf("\n");
742
743     for (i=0; i<MAX_NUM_AUTOPLAY_LEVELS; i++)
744       levels_failed[i] = FALSE;
745
746     autoplay_initialized = TRUE;
747   }
748
749   while (autoplay_level_nr <= autoplay_leveldir->last_level)
750   {
751     level_nr = autoplay_level_nr++;
752
753     TapeErase();
754
755     printf("Level %03d: ", level_nr);
756
757     LoadLevel(level_nr);
758     if (level.no_level_file)
759     {
760       printf("(no level)\n");
761       continue;
762     }
763
764     LoadTape(level_nr);
765     if (TAPE_IS_EMPTY(tape))
766     {
767       printf("(no tape)\n");
768       continue;
769     }
770
771     printf("playing tape ... ");
772
773     TapeStartGamePlaying();
774     TapeStartIndexSearch();
775
776     return;
777   }
778
779   printf("\n");
780   printf_line("=", 79);
781   printf("Number of levels played: %d\n", num_levels_played);
782   printf("Number of levels solved: %d (%d%%)\n", num_levels_solved,
783          (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
784   printf_line("-", 79);
785   printf("Summary (for automatic parsing by scripts):\n");
786   printf("LEVELDIR '%s', SOLVED %d/%d (%d%%)",
787          autoplay_leveldir->identifier, num_levels_solved, num_levels_played,
788          (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
789   if (num_levels_played != num_levels_solved)
790   {
791     printf(", FAILED:");
792     for (i=0; i<MAX_NUM_AUTOPLAY_LEVELS; i++)
793       if (levels_failed[i])
794         printf(" %03d", i);
795   }
796   printf("\n");
797   printf_line("=", 79);
798
799   CloseAllAndExit(0);
800 }
801
802
803 /* ---------- new tape button stuff ---------------------------------------- */
804
805 /* graphic position values for tape buttons */
806 #define TAPE_BUTTON_XSIZE       18
807 #define TAPE_BUTTON_YSIZE       18
808 #define TAPE_BUTTON_XPOS        5
809 #define TAPE_BUTTON_YPOS        77
810
811 #define TAPE_BUTTON_EJECT_XPOS  (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
812 #define TAPE_BUTTON_INDEX_XPOS  (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
813 #define TAPE_BUTTON_STOP_XPOS   (TAPE_BUTTON_XPOS + 1 * TAPE_BUTTON_XSIZE)
814 #define TAPE_BUTTON_PAUSE_XPOS  (TAPE_BUTTON_XPOS + 2 * TAPE_BUTTON_XSIZE)
815 #define TAPE_BUTTON_RECORD_XPOS (TAPE_BUTTON_XPOS + 3 * TAPE_BUTTON_XSIZE)
816 #define TAPE_BUTTON_PLAY_XPOS   (TAPE_BUTTON_XPOS + 4 * TAPE_BUTTON_XSIZE)
817
818 static struct
819 {
820   int x, y;
821   int gadget_id;
822   char *infotext;
823 } tapebutton_info[NUM_TAPE_BUTTONS] =
824 {
825   {
826     TAPE_BUTTON_EJECT_XPOS,     TAPE_BUTTON_YPOS,
827     TAPE_CTRL_ID_EJECT,
828     "eject tape"
829   },
830   {
831     TAPE_BUTTON_INDEX_XPOS,     TAPE_BUTTON_YPOS,
832     TAPE_CTRL_ID_INDEX,
833     "index mark"
834   },
835   {
836     TAPE_BUTTON_STOP_XPOS,      TAPE_BUTTON_YPOS,
837     TAPE_CTRL_ID_STOP,
838     "stop tape"
839   },
840   {
841     TAPE_BUTTON_PAUSE_XPOS,     TAPE_BUTTON_YPOS,
842     TAPE_CTRL_ID_PAUSE,
843     "pause tape"
844   },
845   {
846     TAPE_BUTTON_RECORD_XPOS,    TAPE_BUTTON_YPOS,
847     TAPE_CTRL_ID_RECORD,
848     "record tape"
849   },
850   {
851     TAPE_BUTTON_PLAY_XPOS,      TAPE_BUTTON_YPOS,
852     TAPE_CTRL_ID_PLAY,
853     "play tape"
854   }
855 };
856
857 void CreateTapeButtons()
858 {
859   int i;
860
861   for (i=0; i<NUM_TAPE_BUTTONS; i++)
862   {
863     Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
864     struct GadgetInfo *gi;
865     int gd_xoffset, gd_yoffset;
866     int gd_x1, gd_x2, gd_y;
867     int id = i;
868
869     gd_xoffset = tapebutton_info[i].x;
870     gd_yoffset = tapebutton_info[i].y;
871     gd_x1 = DOOR_GFX_PAGEX4 + gd_xoffset;
872     gd_x2 = DOOR_GFX_PAGEX3 + gd_xoffset;
873     gd_y  = DOOR_GFX_PAGEY2 + gd_yoffset;
874
875     if (i == TAPE_CTRL_ID_INDEX)
876     {
877       gd_x1 = DOOR_GFX_PAGEX6 + gd_xoffset;
878       gd_x2 = DOOR_GFX_PAGEX5 + gd_xoffset;
879     }
880
881     gi = CreateGadget(GDI_CUSTOM_ID, id,
882                       GDI_INFO_TEXT, tapebutton_info[i].infotext,
883                       GDI_X, VX + gd_xoffset,
884                       GDI_Y, VY + gd_yoffset,
885                       GDI_WIDTH, TAPE_BUTTON_XSIZE,
886                       GDI_HEIGHT, TAPE_BUTTON_YSIZE,
887                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
888                       GDI_STATE, GD_BUTTON_UNPRESSED,
889                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y,
890                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y,
891                       GDI_EVENT_MASK, GD_EVENT_RELEASED,
892                       GDI_CALLBACK_ACTION, HandleTapeButtons,
893                       GDI_END);
894
895     if (gi == NULL)
896       Error(ERR_EXIT, "cannot create gadget");
897
898     tape_gadget[id] = gi;
899   }
900 }
901
902 void FreeTapeButtons()
903 {
904   int i;
905
906   for (i=0; i<NUM_TAPE_BUTTONS; i++)
907     FreeGadget(tape_gadget[i]);
908 }
909
910 void MapTapeEjectButton()
911 {
912   UnmapGadget(tape_gadget[TAPE_CTRL_ID_INDEX]);
913   MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
914 }
915
916 void MapTapeIndexButton()
917 {
918   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
919   MapGadget(tape_gadget[TAPE_CTRL_ID_INDEX]);
920 }
921
922 void MapTapeButtons()
923 {
924   int i;
925
926   for (i=0; i<NUM_TAPE_BUTTONS; i++)
927     if (i != TAPE_CTRL_ID_INDEX)
928       MapGadget(tape_gadget[i]);
929
930   if (tape.recording || tape.playing)
931     MapTapeIndexButton();
932 }
933
934 void UnmapTapeButtons()
935 {
936   int i;
937
938   for (i=0; i<NUM_TAPE_BUTTONS; i++)
939     UnmapGadget(tape_gadget[i]);
940 }
941
942 static void HandleTapeButtons(struct GadgetInfo *gi)
943 {
944   int id = gi->custom_id;
945
946   if (game_status != GAME_MODE_MAIN && game_status != GAME_MODE_PLAYING)
947     return;
948
949   switch (id)
950   {
951     case TAPE_CTRL_ID_EJECT:
952       TapeStop();
953       if (TAPE_IS_EMPTY(tape))
954       {
955         LoadTape(level_nr);
956         if (TAPE_IS_EMPTY(tape))
957           Request("No tape for this level !", REQ_CONFIRM);
958       }
959       else
960       {
961         if (tape.changed)
962           SaveTape(tape.level_nr);
963         TapeErase();
964       }
965       DrawCompleteVideoDisplay();
966       break;
967
968     case TAPE_CTRL_ID_INDEX:
969       if (tape.playing)
970         TapeStartIndexSearch();
971       else if (tape.recording)
972         TapeSingleStep();
973       break;
974
975     case TAPE_CTRL_ID_STOP:
976       TapeStop();
977       break;
978
979     case TAPE_CTRL_ID_PAUSE:
980       TapeTogglePause(TAPE_TOGGLE_MANUAL);
981       break;
982
983     case TAPE_CTRL_ID_RECORD:
984       if (TAPE_IS_STOPPED(tape))
985         TapeStartGameRecording();
986       else if (tape.pausing)
987       {
988         if (tape.playing)       /* PLAYING -> PAUSING -> RECORDING */
989           TapeAppendRecording();
990         else
991           TapeTogglePause(TAPE_TOGGLE_MANUAL);
992       }
993       break;
994
995     case TAPE_CTRL_ID_PLAY:
996       if (TAPE_IS_EMPTY(tape))
997         break;
998
999       if (TAPE_IS_STOPPED(tape))
1000       {
1001         TapeStartGamePlaying();
1002       }
1003       else if (tape.playing)
1004       {
1005         if (tape.pausing)                       /* PAUSE -> PLAY */
1006         {
1007           TapeTogglePause(TAPE_TOGGLE_MANUAL);
1008         }
1009         else if (!tape.fast_forward)            /* PLAY -> FAST FORWARD PLAY */
1010         {
1011           tape.fast_forward = TRUE;
1012           DrawVideoDisplay(VIDEO_STATE_FFWD_ON, 0);
1013         }
1014         else if (!tape.pause_before_death)      /* FFWD PLAY -> AUTO PAUSE */
1015         {
1016           tape.pause_before_death = TRUE;
1017           DrawVideoDisplay(VIDEO_STATE_PBEND_ON, VIDEO_DISPLAY_LABEL_ONLY);
1018         }
1019         else                                    /* AUTO PAUSE -> NORMAL PLAY */
1020         {
1021           tape.fast_forward = FALSE;
1022           tape.pause_before_death = FALSE;
1023           DrawVideoDisplay(VIDEO_STATE_FFWD_OFF | VIDEO_STATE_PBEND_OFF, 0);
1024         }
1025       }
1026       break;
1027
1028     default:
1029       break;
1030   }
1031 }