rnd-20021227-1-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 + 1)
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 + 38)
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(new_graphic_info[IMG_MENU_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(new_graphic_info[IMG_MENU_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(new_graphic_info[IMG_MENU_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(new_graphic_info[IMG_MENU_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(new_graphic_info[IMG_MENU_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),FS_SMALL,FC_SPECIAL1);
244     DrawText(VX+VIDEO_DATE_XPOS+27,VY+VIDEO_DATE_YPOS,
245              monatsname[monat],FS_SMALL,FC_SPECIAL1);
246     DrawText(VX+VIDEO_DATE_XPOS+64,VY+VIDEO_DATE_YPOS,
247              int2str(jahr,2),FS_SMALL,FC_SPECIAL1);
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),FS_SMALL,FC_SPECIAL1);
257     DrawText(VX+VIDEO_TIME_XPOS+27,VY+VIDEO_TIME_YPOS,
258              int2str(sec,2),FS_SMALL,FC_SPECIAL1);
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(new_graphic_info[IMG_MENU_DOOR].bitmap, drawto,
272              DOOR_GFX_PAGEX3, DOOR_GFX_PAGEY2,
273              gfx.vxsize, gfx.vysize, gfx.vx, gfx.vy);
274   BlitBitmap(new_graphic_info[IMG_MENU_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   tape.level_nr = level_nr;
306   tape.pos[tape.counter].delay = 0;
307   tape.changed = TRUE;
308
309   tape.date = 10000*(time->tm_year % 100) + 100*time->tm_mon + time->tm_mday;
310   tape.random_seed = InitRND(NEW_RANDOMIZE);
311
312   tape.file_version = FILE_VERSION_ACTUAL;
313   tape.game_version = GAME_VERSION_ACTUAL;
314   tape.engine_version = level.game_version;
315
316   for(i=0; i<MAX_PLAYERS; i++)
317     tape.player_participates[i] = FALSE;
318 }
319
320 static void TapeRewind()
321 {
322   tape.counter = 0;
323   tape.delay_played = 0;
324   tape.pause_before_death = FALSE;
325   tape.recording = FALSE;
326   tape.playing = FALSE;
327   tape.fast_forward = FALSE;
328   tape.index_search = FALSE;
329   tape.auto_play = (global.autoplay_leveldir != NULL);
330   tape.auto_play_level_solved = FALSE;
331   tape.quick_resume = FALSE;
332   tape.single_step = FALSE;
333
334   InitRND(tape.random_seed);
335 }
336
337 void TapeStartRecording()
338 {
339   if (!TAPE_IS_STOPPED(tape))
340     TapeStop();
341
342   TapeErase();
343   TapeRewind();
344
345   tape.recording = TRUE;
346
347   DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
348   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
349   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
350   MapTapeIndexButton();
351
352   SetDrawDeactivationMask(REDRAW_NONE);
353   audio.sound_deactivated = FALSE;
354 }
355
356 static void TapeStartGameRecording()
357 {
358   TapeStartRecording();
359
360 #if defined(PLATFORM_UNIX)
361   if (options.network)
362     SendToServer_StartPlaying();
363   else
364 #endif
365   {
366     game_status = PLAYING;
367     StopAnimation();
368     InitGame();
369   }
370 }
371
372 static void TapeAppendRecording()
373 {
374   if (!tape.playing || !tape.pausing)
375     return;
376
377   tape.pos[tape.counter].delay = tape.delay_played;
378   tape.playing = FALSE;
379   tape.recording = TRUE;
380   tape.changed = TRUE;
381
382   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON,0);
383 }
384
385 void TapeHaltRecording()
386 {
387   if (!tape.recording)
388     return;
389
390   tape.counter++;
391   tape.pos[tape.counter].delay = 0;
392
393   tape.length = tape.counter;
394   tape.length_seconds = GetTapeLength();
395 }
396
397 void TapeStopRecording()
398 {
399   if (!tape.recording)
400     return;
401
402   TapeHaltRecording();
403
404   tape.recording = FALSE;
405   tape.pausing = FALSE;
406
407   DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
408   MapTapeEjectButton();
409 }
410
411 void TapeRecordAction(byte action[MAX_PLAYERS])
412 {
413   int i;
414
415   if (!tape.recording || tape.pausing)
416     return;
417
418   if (tape.counter >= MAX_TAPELEN - 1)
419   {
420     TapeStopRecording();
421     return;
422   }
423
424   if (tape.pos[tape.counter].delay > 0)         /* already stored action */
425   {
426     boolean changed_events = FALSE;
427
428     for(i=0; i<MAX_PLAYERS; i++)
429       if (tape.pos[tape.counter].action[i] != action[i])
430         changed_events = TRUE;
431
432     if (changed_events || tape.pos[tape.counter].delay >= 255)
433     {
434       tape.counter++;
435       tape.pos[tape.counter].delay = 0;
436     }
437     else
438       tape.pos[tape.counter].delay++;
439   }
440
441   if (tape.pos[tape.counter].delay == 0)        /* store new action */
442   {
443     for(i=0; i<MAX_PLAYERS; i++)
444       tape.pos[tape.counter].action[i] = action[i];
445
446     tape.pos[tape.counter].delay++;
447   }
448 }
449
450 void TapeTogglePause(boolean toggle_manual)
451 {
452   unsigned long state;
453
454 #if 0
455   if (!tape.recording && !tape.playing)
456     return;
457 #endif
458
459   tape.pausing = !tape.pausing;
460   tape.fast_forward = FALSE;
461   tape.pause_before_death = FALSE;
462
463   if (tape.single_step && toggle_manual)
464     tape.single_step = FALSE;
465
466   state = (tape.pausing ? VIDEO_STATE_PAUSE_ON : VIDEO_STATE_PAUSE_OFF);
467   if (tape.playing)
468     state |= VIDEO_STATE_PBEND_OFF;
469
470   DrawVideoDisplay(state, 0);
471
472   if (tape.index_search)
473   {
474     TapeStopIndexSearch();
475
476     if (tape.quick_resume)
477     {
478       tape.quick_resume = FALSE;
479
480       TapeAppendRecording();
481       TapeTogglePause(toggle_manual);
482     }
483   }
484 }
485
486 void TapeStartPlaying()
487 {
488   if (TAPE_IS_EMPTY(tape))
489     return;
490
491   if (!TAPE_IS_STOPPED(tape))
492     TapeStop();
493
494   TapeRewind();
495
496   tape.playing = TRUE;
497
498   DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
499   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
500   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
501   MapTapeIndexButton();
502
503   SetDrawDeactivationMask(REDRAW_NONE);
504   audio.sound_deactivated = FALSE;
505 }
506
507 static void TapeStartGamePlaying()
508 {
509   TapeStartPlaying();
510
511   game_status = PLAYING;
512   StopAnimation();
513   InitGame();
514 }
515
516 void TapeStopPlaying()
517 {
518   if (!tape.playing)
519     return;
520
521   tape.playing = FALSE;
522   tape.pausing = FALSE;
523
524   if (tape.index_search)
525     TapeStopIndexSearch();
526
527   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
528   MapTapeEjectButton();
529 }
530
531 byte *TapePlayAction()
532 {
533   static byte action[MAX_PLAYERS];
534   int i;
535
536   if (!tape.playing || tape.pausing)
537     return NULL;
538
539   if (tape.pause_before_death)  /* STOP 10s BEFORE PLAYER GETS KILLED... */
540   {
541     if (!(FrameCounter % 20))
542     {
543       if ((FrameCounter / 20) % 2)
544         DrawVideoDisplay(VIDEO_STATE_PBEND_ON, VIDEO_DISPLAY_LABEL_ONLY);
545       else
546         DrawVideoDisplay(VIDEO_STATE_PBEND_OFF, VIDEO_DISPLAY_LABEL_ONLY);
547     }
548
549     if (TimePlayed > tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
550     {
551       TapeTogglePause(TAPE_TOGGLE_MANUAL);
552       return NULL;
553     }
554   }
555
556   if (tape.counter >= tape.length)      /* end of tape reached */
557   {
558     if (tape.index_search && !tape.auto_play)
559       TapeTogglePause(TAPE_TOGGLE_MANUAL);
560     else
561       TapeStop();
562
563     return NULL;
564   }
565
566   for(i=0; i<MAX_PLAYERS; i++)
567     action[i] = tape.pos[tape.counter].action[i];
568
569   tape.delay_played++;
570   if (tape.delay_played >= tape.pos[tape.counter].delay)
571   {
572     tape.counter++;
573     tape.delay_played = 0;
574   }
575
576   return action;
577 }
578
579 void TapeStop()
580 {
581   TapeStopRecording();
582   TapeStopPlaying();
583
584   DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0);
585   if (tape.date && tape.length)
586   {
587     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
588     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
589   }
590
591   if (tape.auto_play)
592     AutoPlayTape();     /* continue automatically playing next tape */
593 }
594
595 unsigned int GetTapeLength()
596 {
597   unsigned int tape_length = 0;
598   int i;
599
600   if (TAPE_IS_EMPTY(tape))
601     return(0);
602
603   for(i=0;i<tape.length;i++)
604     tape_length += tape.pos[i].delay;
605
606   return(tape_length * GAME_FRAME_DELAY / 1000);
607 }
608
609 static void TapeStartIndexSearch()
610 {
611   tape.index_search = TRUE;
612
613   if (!tape.fast_forward || tape.pause_before_death)
614   {
615     tape.pausing = FALSE;
616
617     SetDrawDeactivationMask(REDRAW_FIELD);
618     audio.sound_deactivated = TRUE;
619   }
620 }
621
622 static void TapeStopIndexSearch()
623 {
624   tape.index_search = FALSE;
625
626   SetDrawDeactivationMask(REDRAW_NONE);
627   audio.sound_deactivated = FALSE;
628
629   RedrawPlayfield(TRUE, 0,0,0,0);
630   DrawGameDoorValues();
631 }
632
633 static void TapeSingleStep()
634 {
635   if (options.network)
636     return;
637
638   if (!tape.pausing)
639     TapeTogglePause(TAPE_TOGGLE_MANUAL);
640
641   tape.single_step = !tape.single_step;
642 }
643
644 void TapeQuickSave()
645 {
646   if (game_status == PLAYING)
647   {
648     if (tape.recording)
649       TapeHaltRecording();      /* prepare tape for saving on-the-fly */
650
651     if (TAPE_IS_EMPTY(tape))
652       Request("No tape that can be saved !", REQ_CONFIRM);
653     else
654       SaveTape(tape.level_nr);
655   }
656   else if (game_status == MAINMENU)
657     Request("No game that can be saved !", REQ_CONFIRM);
658 }
659
660 void TapeQuickLoad()
661 {
662   if (game_status == PLAYING || game_status == MAINMENU)
663   {
664     TapeStop();
665     TapeErase();
666
667     LoadTape(level_nr);
668     if (!TAPE_IS_EMPTY(tape))
669     {
670       TapeStartGamePlaying();
671       TapeStartIndexSearch();
672
673       tape.quick_resume = TRUE;
674     }
675     else
676       Request("No tape for this level !", REQ_CONFIRM);
677   }
678 }
679
680
681 /* ------------------------------------------------------------------------- *
682  * tape autoplay functions
683  * ------------------------------------------------------------------------- */
684
685 void AutoPlayTape()
686 {
687   static LevelDirTree *autoplay_leveldir = NULL;
688   static boolean autoplay_initialized = FALSE;
689   static int autoplay_level_nr = -1;
690   static int levels_played = 0;
691   static int levels_solved = 0;
692
693   if (autoplay_initialized)
694   {
695     /* just finished auto-playing tape */
696     printf("%s.\n", tape.auto_play_level_solved ? "solved" : "NOT SOLVED");
697
698     levels_played++;
699     if (tape.auto_play_level_solved)
700       levels_solved++;
701   }
702   else
703   {
704     DrawCompleteVideoDisplay();
705     audio.sound_enabled = FALSE;
706
707     autoplay_leveldir = getTreeInfoFromIdentifier(leveldir_first,
708                                                   global.autoplay_leveldir);
709
710     if (autoplay_leveldir == NULL)
711       Error(ERR_EXIT, "no such level identifier: '%s'",
712             global.autoplay_leveldir);
713
714     leveldir_current = autoplay_leveldir;
715
716     if (global.autoplay_level_nr != -1)
717     {
718       autoplay_leveldir->first_level = global.autoplay_level_nr;
719       autoplay_leveldir->last_level  = global.autoplay_level_nr;
720     }
721
722     autoplay_level_nr = autoplay_leveldir->first_level;
723
724     printf_line('=', 79);
725     printf("Automatically playing level tapes\n");
726     printf_line('-', 79);
727     printf("Level series identifier: '%s'\n", autoplay_leveldir->identifier);
728     printf("Level series name:       '%s'\n", autoplay_leveldir->name);
729     printf("Level series author:     '%s'\n", autoplay_leveldir->author);
730     printf("Number of levels:        %d\n",   autoplay_leveldir->levels);
731     printf_line('=', 79);
732     printf("\n");
733
734     autoplay_initialized = TRUE;
735   }
736
737   while (autoplay_level_nr <= autoplay_leveldir->last_level)
738   {
739     level_nr = autoplay_level_nr++;
740
741     TapeErase();
742
743     printf("Level %03d: ", level_nr);
744
745     LoadLevel(level_nr);
746     if (level.no_level_file)
747     {
748       printf("(no level)\n");
749       continue;
750     }
751
752     LoadTape(level_nr);
753     if (TAPE_IS_EMPTY(tape))
754     {
755       printf("(no tape)\n");
756       continue;
757     }
758
759     printf("playing tape ... ");
760
761     TapeStartGamePlaying();
762     TapeStartIndexSearch();
763
764     return;
765   }
766
767   printf("\n");
768   printf_line('=', 79);
769   printf("Summary\n");
770   printf_line('-', 79);
771   printf("Number of levels played: %d\n", levels_played);
772   printf("Number of levels solved: %d (%d%%)\n", levels_solved,
773          levels_solved * 100 / levels_played);
774   printf_line('=', 79);
775
776   CloseAllAndExit(0);
777 }
778
779
780 /* ---------- new tape button stuff ---------------------------------------- */
781
782 /* graphic position values for tape buttons */
783 #define TAPE_BUTTON_XSIZE       18
784 #define TAPE_BUTTON_YSIZE       18
785 #define TAPE_BUTTON_XPOS        5
786 #define TAPE_BUTTON_YPOS        77
787
788 #define TAPE_BUTTON_EJECT_XPOS  (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
789 #define TAPE_BUTTON_INDEX_XPOS  (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
790 #define TAPE_BUTTON_STOP_XPOS   (TAPE_BUTTON_XPOS + 1 * TAPE_BUTTON_XSIZE)
791 #define TAPE_BUTTON_PAUSE_XPOS  (TAPE_BUTTON_XPOS + 2 * TAPE_BUTTON_XSIZE)
792 #define TAPE_BUTTON_RECORD_XPOS (TAPE_BUTTON_XPOS + 3 * TAPE_BUTTON_XSIZE)
793 #define TAPE_BUTTON_PLAY_XPOS   (TAPE_BUTTON_XPOS + 4 * TAPE_BUTTON_XSIZE)
794
795 static struct
796 {
797   int x, y;
798   int gadget_id;
799   char *infotext;
800 } tapebutton_info[NUM_TAPE_BUTTONS] =
801 {
802   {
803     TAPE_BUTTON_EJECT_XPOS,     TAPE_BUTTON_YPOS,
804     TAPE_CTRL_ID_EJECT,
805     "eject tape"
806   },
807   {
808     TAPE_BUTTON_INDEX_XPOS,     TAPE_BUTTON_YPOS,
809     TAPE_CTRL_ID_INDEX,
810     "index mark"
811   },
812   {
813     TAPE_BUTTON_STOP_XPOS,      TAPE_BUTTON_YPOS,
814     TAPE_CTRL_ID_STOP,
815     "stop tape"
816   },
817   {
818     TAPE_BUTTON_PAUSE_XPOS,     TAPE_BUTTON_YPOS,
819     TAPE_CTRL_ID_PAUSE,
820     "pause tape"
821   },
822   {
823     TAPE_BUTTON_RECORD_XPOS,    TAPE_BUTTON_YPOS,
824     TAPE_CTRL_ID_RECORD,
825     "record tape"
826   },
827   {
828     TAPE_BUTTON_PLAY_XPOS,      TAPE_BUTTON_YPOS,
829     TAPE_CTRL_ID_PLAY,
830     "play tape"
831   }
832 };
833
834 void CreateTapeButtons()
835 {
836   int i;
837
838   for (i=0; i<NUM_TAPE_BUTTONS; i++)
839   {
840     Bitmap *gd_bitmap = new_graphic_info[IMG_MENU_DOOR].bitmap;
841     struct GadgetInfo *gi;
842     int gd_xoffset, gd_yoffset;
843     int gd_x1, gd_x2, gd_y;
844     int id = i;
845
846     gd_xoffset = tapebutton_info[i].x;
847     gd_yoffset = tapebutton_info[i].y;
848     gd_x1 = DOOR_GFX_PAGEX4 + gd_xoffset;
849     gd_x2 = DOOR_GFX_PAGEX3 + gd_xoffset;
850     gd_y  = DOOR_GFX_PAGEY2 + gd_yoffset;
851
852     if (i == TAPE_CTRL_ID_INDEX)
853     {
854       gd_x1 = DOOR_GFX_PAGEX6 + gd_xoffset;
855       gd_x2 = DOOR_GFX_PAGEX5 + gd_xoffset;
856     }
857
858     gi = CreateGadget(GDI_CUSTOM_ID, id,
859                       GDI_INFO_TEXT, tapebutton_info[i].infotext,
860                       GDI_X, VX + gd_xoffset,
861                       GDI_Y, VY + gd_yoffset,
862                       GDI_WIDTH, TAPE_BUTTON_XSIZE,
863                       GDI_HEIGHT, TAPE_BUTTON_YSIZE,
864                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
865                       GDI_STATE, GD_BUTTON_UNPRESSED,
866                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y,
867                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y,
868                       GDI_EVENT_MASK, GD_EVENT_RELEASED,
869                       GDI_CALLBACK_ACTION, HandleTapeButtons,
870                       GDI_END);
871
872     if (gi == NULL)
873       Error(ERR_EXIT, "cannot create gadget");
874
875     tape_gadget[id] = gi;
876   }
877 }
878
879 void MapTapeEjectButton()
880 {
881   UnmapGadget(tape_gadget[TAPE_CTRL_ID_INDEX]);
882   MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
883 }
884
885 void MapTapeIndexButton()
886 {
887   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
888   MapGadget(tape_gadget[TAPE_CTRL_ID_INDEX]);
889 }
890
891 void MapTapeButtons()
892 {
893   int i;
894
895   for (i=0; i<NUM_TAPE_BUTTONS; i++)
896     if (i != TAPE_CTRL_ID_INDEX)
897       MapGadget(tape_gadget[i]);
898
899   if (tape.recording || tape.playing)
900     MapTapeIndexButton();
901 }
902
903 void UnmapTapeButtons()
904 {
905   int i;
906
907   for (i=0; i<NUM_TAPE_BUTTONS; i++)
908     UnmapGadget(tape_gadget[i]);
909 }
910
911 static void HandleTapeButtons(struct GadgetInfo *gi)
912 {
913   int id = gi->custom_id;
914
915   if (game_status != MAINMENU && game_status != PLAYING)
916     return;
917
918   switch (id)
919   {
920     case TAPE_CTRL_ID_EJECT:
921       TapeStop();
922       if (TAPE_IS_EMPTY(tape))
923       {
924         LoadTape(level_nr);
925         if (TAPE_IS_EMPTY(tape))
926           Request("No tape for this level !", REQ_CONFIRM);
927       }
928       else
929       {
930         if (tape.changed)
931           SaveTape(tape.level_nr);
932         TapeErase();
933       }
934       DrawCompleteVideoDisplay();
935       break;
936
937     case TAPE_CTRL_ID_INDEX:
938       if (tape.playing)
939         TapeStartIndexSearch();
940       else if (tape.recording)
941         TapeSingleStep();
942       break;
943
944     case TAPE_CTRL_ID_STOP:
945       TapeStop();
946       break;
947
948     case TAPE_CTRL_ID_PAUSE:
949       TapeTogglePause(TAPE_TOGGLE_MANUAL);
950       break;
951
952     case TAPE_CTRL_ID_RECORD:
953       if (TAPE_IS_STOPPED(tape))
954         TapeStartGameRecording();
955       else if (tape.pausing)
956       {
957         if (tape.playing)       /* PLAYING -> PAUSING -> RECORDING */
958           TapeAppendRecording();
959         else
960           TapeTogglePause(TAPE_TOGGLE_MANUAL);
961       }
962       break;
963
964     case TAPE_CTRL_ID_PLAY:
965       if (TAPE_IS_EMPTY(tape))
966         break;
967
968       if (TAPE_IS_STOPPED(tape))
969       {
970         TapeStartGamePlaying();
971       }
972       else if (tape.playing)
973       {
974         if (tape.pausing)                       /* PAUSE -> PLAY */
975         {
976           TapeTogglePause(TAPE_TOGGLE_MANUAL);
977         }
978         else if (!tape.fast_forward)            /* PLAY -> FAST FORWARD PLAY */
979         {
980           tape.fast_forward = TRUE;
981           DrawVideoDisplay(VIDEO_STATE_FFWD_ON, 0);
982         }
983         else if (!tape.pause_before_death)      /* FFWD PLAY -> AUTO PAUSE */
984         {
985           tape.pause_before_death = TRUE;
986           DrawVideoDisplay(VIDEO_STATE_PBEND_ON, VIDEO_DISPLAY_LABEL_ONLY);
987         }
988         else                                    /* AUTO PAUSE -> NORMAL PLAY */
989         {
990           tape.fast_forward = FALSE;
991           tape.pause_before_death = FALSE;
992           DrawVideoDisplay(VIDEO_STATE_FFWD_OFF | VIDEO_STATE_PBEND_OFF, 0);
993         }
994       }
995       break;
996
997     default:
998       break;
999   }
1000 }