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