rnd-20020418-1-src
[rocksndiamonds.git] / src / tape.c
1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back!               *
3 *----------------------------------------------------------*
4 * (c) 1995-2001 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
39 static struct GadgetInfo *tape_gadget[NUM_TAPE_BUTTONS];
40
41
42 /* ========================================================================= */
43 /* video tape definitions                                                    */
44 /* ========================================================================= */
45
46 /* some positions in the video tape control window */
47 #define VIDEO_DATE_LABEL_XPOS   (VIDEO_DISPLAY1_XPOS)
48 #define VIDEO_DATE_LABEL_YPOS   (VIDEO_DISPLAY1_YPOS)
49 #define VIDEO_DATE_LABEL_XSIZE  (VIDEO_DISPLAY_XSIZE)
50 #define VIDEO_DATE_LABEL_YSIZE  (VIDEO_DISPLAY_YSIZE)
51 #define VIDEO_DATE_XPOS         (VIDEO_DISPLAY1_XPOS + 1)
52 #define VIDEO_DATE_YPOS         (VIDEO_DISPLAY1_YPOS + 14)
53 #define VIDEO_DATE_XSIZE        (VIDEO_DISPLAY_XSIZE)
54 #define VIDEO_DATE_YSIZE        16
55 #define VIDEO_REC_LABEL_XPOS    (VIDEO_DISPLAY2_XPOS)
56 #define VIDEO_REC_LABEL_YPOS    (VIDEO_DISPLAY2_YPOS)
57 #define VIDEO_REC_LABEL_XSIZE   20
58 #define VIDEO_REC_LABEL_YSIZE   12
59 #define VIDEO_REC_SYMBOL_XPOS   (VIDEO_DISPLAY2_XPOS + 20)
60 #define VIDEO_REC_SYMBOL_YPOS   (VIDEO_DISPLAY2_YPOS)
61 #define VIDEO_REC_SYMBOL_XSIZE  16
62 #define VIDEO_REC_SYMBOL_YSIZE  16
63 #define VIDEO_PLAY_LABEL_XPOS   (VIDEO_DISPLAY2_XPOS + 65)
64 #define VIDEO_PLAY_LABEL_YPOS   (VIDEO_DISPLAY2_YPOS)
65 #define VIDEO_PLAY_LABEL_XSIZE  22
66 #define VIDEO_PLAY_LABEL_YSIZE  12
67 #define VIDEO_PLAY_SYMBOL_XPOS  (VIDEO_DISPLAY2_XPOS + 52)
68 #define VIDEO_PLAY_SYMBOL_YPOS  (VIDEO_DISPLAY2_YPOS)
69 #define VIDEO_PLAY_SYMBOL_XSIZE 11
70 #define VIDEO_PLAY_SYMBOL_YSIZE 13
71 #define VIDEO_PAUSE_LABEL_XPOS  (VIDEO_DISPLAY2_XPOS)
72 #define VIDEO_PAUSE_LABEL_YPOS  (VIDEO_DISPLAY2_YPOS + 20)
73 #define VIDEO_PAUSE_LABEL_XSIZE 35
74 #define VIDEO_PAUSE_LABEL_YSIZE 8
75 #define VIDEO_PAUSE_SYMBOL_XPOS (VIDEO_DISPLAY2_XPOS + 35)
76 #define VIDEO_PAUSE_SYMBOL_YPOS (VIDEO_DISPLAY2_YPOS)
77 #define VIDEO_PAUSE_SYMBOL_XSIZE 17
78 #define VIDEO_PAUSE_SYMBOL_YSIZE 13
79 #define VIDEO_TIME_XPOS         (VIDEO_DISPLAY2_XPOS + 38)
80 #define VIDEO_TIME_YPOS         (VIDEO_DISPLAY2_YPOS + 14)
81 #define VIDEO_TIME_XSIZE        50
82 #define VIDEO_TIME_YSIZE        16
83
84 /* special */
85 #define VIDEO_PBEND_LABEL_XPOS  5
86 #define VIDEO_PBEND_LABEL_YPOS  220
87 #define VIDEO_PBEND_LABEL_XSIZE 35
88 #define VIDEO_PBEND_LABEL_YSIZE 30
89
90 #define VIDEO_STATE_OFF         (VIDEO_STATE_PLAY_OFF   |       \
91                                  VIDEO_STATE_REC_OFF    |       \
92                                  VIDEO_STATE_PAUSE_OFF  |       \
93                                  VIDEO_STATE_FFWD_OFF   |       \
94                                  VIDEO_STATE_PBEND_OFF  |       \
95                                  VIDEO_STATE_DATE_OFF   |       \
96                                  VIDEO_STATE_TIME_OFF)
97 #define VIDEO_PRESS_OFF         (VIDEO_PRESS_PLAY_OFF   |       \
98                                  VIDEO_PRESS_REC_OFF    |       \
99                                  VIDEO_PRESS_PAUSE_OFF  |       \
100                                  VIDEO_PRESS_STOP_OFF   |       \
101                                  VIDEO_PRESS_EJECT_OFF)
102 #define VIDEO_ALL_OFF           (VIDEO_STATE_OFF | VIDEO_PRESS_OFF)
103
104 #define VIDEO_STATE_ON          (VIDEO_STATE_PLAY_ON    |       \
105                                  VIDEO_STATE_REC_ON     |       \
106                                  VIDEO_STATE_PAUSE_ON   |       \
107                                  VIDEO_STATE_FFWD_ON    |       \
108                                  VIDEO_STATE_PBEND_ON   |       \
109                                  VIDEO_STATE_DATE_ON    |       \
110                                  VIDEO_STATE_TIME_ON)
111 #define VIDEO_PRESS_ON          (VIDEO_PRESS_PLAY_ON    |       \
112                                  VIDEO_PRESS_REC_ON     |       \
113                                  VIDEO_PRESS_PAUSE_ON   |       \
114                                  VIDEO_PRESS_STOP_ON    |       \
115                                  VIDEO_PRESS_EJECT_ON)
116 #define VIDEO_ALL_ON            (VIDEO_STATE_ON | VIDEO_PRESS_ON)
117
118 #define VIDEO_STATE             (VIDEO_STATE_ON | VIDEO_STATE_OFF)
119 #define VIDEO_PRESS             (VIDEO_PRESS_ON | VIDEO_PRESS_OFF)
120 #define VIDEO_ALL               (VIDEO_ALL_ON | VIDEO_ALL_OFF)
121
122
123 /* ========================================================================= */
124 /* video display functions                                                   */
125 /* ========================================================================= */
126
127 void DrawVideoDisplay(unsigned long state, unsigned long value)
128 {
129   int i;
130   int part_label = 0, part_symbol = 1;
131   int xpos = 0, ypos = 1, xsize = 2, ysize = 3;
132   static char *monatsname[12] =
133   {
134     "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
135     "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
136   };
137   static int video_pos[5][2][4] =
138   {
139     {{ VIDEO_PLAY_LABEL_XPOS, VIDEO_PLAY_LABEL_YPOS,
140        VIDEO_PLAY_LABEL_XSIZE,VIDEO_PLAY_LABEL_YSIZE },
141      { VIDEO_PLAY_SYMBOL_XPOS, VIDEO_PLAY_SYMBOL_YPOS,
142        VIDEO_PLAY_SYMBOL_XSIZE,VIDEO_PLAY_SYMBOL_YSIZE }},
143
144     {{ VIDEO_REC_LABEL_XPOS, VIDEO_REC_LABEL_YPOS,
145        VIDEO_REC_LABEL_XSIZE,VIDEO_REC_LABEL_YSIZE },
146      { VIDEO_REC_SYMBOL_XPOS, VIDEO_REC_SYMBOL_YPOS,
147        VIDEO_REC_SYMBOL_XSIZE,VIDEO_REC_SYMBOL_YSIZE }},
148
149     {{ VIDEO_PAUSE_LABEL_XPOS, VIDEO_PAUSE_LABEL_YPOS,
150        VIDEO_PAUSE_LABEL_XSIZE,VIDEO_PAUSE_LABEL_YSIZE },
151      { VIDEO_PAUSE_SYMBOL_XPOS, VIDEO_PAUSE_SYMBOL_YPOS,
152        VIDEO_PAUSE_SYMBOL_XSIZE,VIDEO_PAUSE_SYMBOL_YSIZE }},
153
154     {{ VIDEO_DATE_LABEL_XPOS, VIDEO_DATE_LABEL_YPOS,
155        VIDEO_DATE_LABEL_XSIZE,VIDEO_DATE_LABEL_YSIZE },
156      { VIDEO_DATE_XPOS, VIDEO_DATE_YPOS,
157        VIDEO_DATE_XSIZE,VIDEO_DATE_YSIZE }},
158
159     {{ 0,0,
160        0,0 },
161      { VIDEO_TIME_XPOS, VIDEO_TIME_YPOS,
162        VIDEO_TIME_XSIZE,VIDEO_TIME_YSIZE }}
163   };
164
165   if (state & VIDEO_STATE_PBEND_OFF)
166   {
167     int cx = DOOR_GFX_PAGEX3, cy = DOOR_GFX_PAGEY2;
168
169     BlitBitmap(pix[PIX_DOOR], drawto,
170                cx + VIDEO_REC_LABEL_XPOS,
171                cy + VIDEO_REC_LABEL_YPOS,
172                VIDEO_PBEND_LABEL_XSIZE,
173                VIDEO_PBEND_LABEL_YSIZE,
174                VX + VIDEO_REC_LABEL_XPOS,
175                VY + VIDEO_REC_LABEL_YPOS);
176   }
177
178   for(i=0;i<10;i++)
179   {
180     if (state & (1<<i))
181     {
182       int pos = i/2, cx, cy = DOOR_GFX_PAGEY2;
183
184       if (i%2)                  /* i ungerade => STATE_ON / PRESS_OFF */
185         cx = DOOR_GFX_PAGEX4;
186       else
187         cx = DOOR_GFX_PAGEX3;   /* i gerade => STATE_OFF / PRESS_ON */
188
189       if (video_pos[pos][part_label][0] && value != VIDEO_DISPLAY_SYMBOL_ONLY)
190         BlitBitmap(pix[PIX_DOOR], drawto,
191                    cx + video_pos[pos][part_label][xpos],
192                    cy + video_pos[pos][part_label][ypos],
193                    video_pos[pos][part_label][xsize],
194                    video_pos[pos][part_label][ysize],
195                    VX + video_pos[pos][part_label][xpos],
196                    VY + video_pos[pos][part_label][ypos]);
197       if (video_pos[pos][part_symbol][0] && value != VIDEO_DISPLAY_LABEL_ONLY)
198         BlitBitmap(pix[PIX_DOOR], drawto,
199                    cx + video_pos[pos][part_symbol][xpos],
200                    cy + video_pos[pos][part_symbol][ypos],
201                    video_pos[pos][part_symbol][xsize],
202                    video_pos[pos][part_symbol][ysize],
203                    VX + video_pos[pos][part_symbol][xpos],
204                    VY + video_pos[pos][part_symbol][ypos]);
205     }
206   }
207
208   if (state & VIDEO_STATE_FFWD_ON)
209   {
210     int cx = DOOR_GFX_PAGEX4, cy = DOOR_GFX_PAGEY2;
211
212     BlitBitmap(pix[PIX_DOOR], drawto,
213                cx + VIDEO_PLAY_SYMBOL_XPOS,
214                cy + VIDEO_PLAY_SYMBOL_YPOS,
215                VIDEO_PLAY_SYMBOL_XSIZE - 2,
216                VIDEO_PLAY_SYMBOL_YSIZE,
217                VX + VIDEO_PLAY_SYMBOL_XPOS - 9,
218                VY + VIDEO_PLAY_SYMBOL_YPOS);
219   }
220
221   if (state & VIDEO_STATE_PBEND_ON)
222   {
223     int cx = DOOR_GFX_PAGEX6, cy = DOOR_GFX_PAGEY1;
224
225     BlitBitmap(pix[PIX_DOOR], drawto,
226                cx + VIDEO_PBEND_LABEL_XPOS,
227                cy + VIDEO_PBEND_LABEL_YPOS,
228                VIDEO_PBEND_LABEL_XSIZE,
229                VIDEO_PBEND_LABEL_YSIZE,
230                VX + VIDEO_REC_LABEL_XPOS,
231                VY + VIDEO_REC_LABEL_YPOS);
232   }
233
234   if (state & VIDEO_STATE_DATE_ON)
235   {
236     int tag = value % 100;
237     int monat = (value/100) % 100;
238     int jahr = (value/10000);
239
240     DrawText(VX+VIDEO_DATE_XPOS,VY+VIDEO_DATE_YPOS,
241              int2str(tag,2),FS_SMALL,FC_SPECIAL1);
242     DrawText(VX+VIDEO_DATE_XPOS+27,VY+VIDEO_DATE_YPOS,
243              monatsname[monat],FS_SMALL,FC_SPECIAL1);
244     DrawText(VX+VIDEO_DATE_XPOS+64,VY+VIDEO_DATE_YPOS,
245              int2str(jahr,2),FS_SMALL,FC_SPECIAL1);
246   }
247
248   if (state & VIDEO_STATE_TIME_ON)
249   {
250     int min = value / 60;
251     int sec = value % 60;
252
253     DrawText(VX+VIDEO_TIME_XPOS,VY+VIDEO_TIME_YPOS,
254              int2str(min,2),FS_SMALL,FC_SPECIAL1);
255     DrawText(VX+VIDEO_TIME_XPOS+27,VY+VIDEO_TIME_YPOS,
256              int2str(sec,2),FS_SMALL,FC_SPECIAL1);
257   }
258
259   if (state & VIDEO_STATE_DATE)
260     redraw_mask |= REDRAW_VIDEO_1;
261   if ((state & ~VIDEO_STATE_DATE) & VIDEO_STATE)
262     redraw_mask |= REDRAW_VIDEO_2;
263   if (state & VIDEO_PRESS)
264     redraw_mask |= REDRAW_VIDEO_3;
265 }
266
267 void DrawCompleteVideoDisplay()
268 {
269   BlitBitmap(pix[PIX_DOOR], drawto, DOOR_GFX_PAGEX3, DOOR_GFX_PAGEY2,
270              gfx.vxsize, gfx.vysize, gfx.vx, gfx.vy);
271   BlitBitmap(pix[PIX_DOOR], drawto,
272              DOOR_GFX_PAGEX4 + VIDEO_CONTROL_XPOS,
273              DOOR_GFX_PAGEY2 + VIDEO_CONTROL_YPOS,
274              VIDEO_CONTROL_XSIZE, VIDEO_CONTROL_YSIZE,
275              gfx.vx + VIDEO_CONTROL_XPOS, gfx.vy + VIDEO_CONTROL_YPOS);
276
277   DrawVideoDisplay(VIDEO_ALL_OFF, 0);
278   if (tape.date && tape.length)
279   {
280     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
281     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
282   }
283
284   BlitBitmap(drawto, pix[PIX_DB_DOOR], gfx.vx, gfx.vy, gfx.vxsize, gfx.vysize,
285              DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY2);
286 }
287
288
289 /* ========================================================================= */
290 /* tape control functions                                                    */
291 /* ========================================================================= */
292
293 void TapeErase()
294 {
295   time_t epoch_seconds = time(NULL);
296   struct tm *time = localtime(&epoch_seconds);
297   int i;
298
299   tape.length = 0;
300   tape.counter = 0;
301
302   tape.level_nr = level_nr;
303   tape.pos[tape.counter].delay = 0;
304   tape.changed = TRUE;
305   tape.date = 10000*(time->tm_year % 100) + 100*time->tm_mon + time->tm_mday;
306   tape.game_version = GAME_VERSION_ACTUAL;
307   tape.random_seed = InitRND(NEW_RANDOMIZE);
308
309   for(i=0; i<MAX_PLAYERS; i++)
310     tape.player_participates[i] = FALSE;
311 }
312
313 static void TapeRewind()
314 {
315   tape.counter = 0;
316   tape.delay_played = 0;
317   tape.pause_before_death = FALSE;
318   tape.recording = FALSE;
319   tape.playing = FALSE;
320   tape.fast_forward = FALSE;
321   tape.index_search = FALSE;
322   tape.quick_resume = FALSE;
323   tape.single_step = FALSE;
324
325   InitRND(tape.random_seed);
326 }
327
328 void TapeStartRecording()
329 {
330   if (!TAPE_IS_STOPPED(tape))
331     TapeStop();
332
333   TapeErase();
334   TapeRewind();
335
336   tape.recording = TRUE;
337
338   DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
339   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
340   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
341   MapTapeIndexButton();
342
343   SetDrawDeactivationMask(REDRAW_NONE);
344   audio.sound_deactivated = FALSE;
345 }
346
347 static void TapeStartGameRecording()
348 {
349   TapeStartRecording();
350
351 #if defined(PLATFORM_UNIX)
352   if (options.network)
353     SendToServer_StartPlaying();
354   else
355 #endif
356   {
357     game_status = PLAYING;
358     StopAnimation();
359     InitGame();
360   }
361 }
362
363 static void TapeAppendRecording()
364 {
365   if (!tape.playing || !tape.pausing)
366     return;
367
368   if (tape.game_version != GAME_VERSION_ACTUAL &&
369       !Request("This may break old version tape ! Append anyway ?",
370                REQ_ASK))
371     return;
372
373   tape.pos[tape.counter].delay = tape.delay_played;
374   tape.playing = FALSE;
375   tape.recording = TRUE;
376   tape.changed = TRUE;
377   tape.game_version = GAME_VERSION_ACTUAL;
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     tape.index_search = FALSE;
472
473     SetDrawDeactivationMask(REDRAW_NONE);
474     audio.sound_deactivated = FALSE;
475
476     RedrawPlayfield(TRUE, 0,0,0,0);
477     DrawGameDoorValues();
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 = 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   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)
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
592 unsigned int GetTapeLength()
593 {
594   unsigned int tape_length = 0;
595   int i;
596
597   if (TAPE_IS_EMPTY(tape))
598     return(0);
599
600   for(i=0;i<tape.length;i++)
601     tape_length += tape.pos[i].delay;
602
603   return(tape_length * GAME_FRAME_DELAY / 1000);
604 }
605
606 void TapeIndexSearch()
607 {
608   tape.index_search = TRUE;
609
610   if (!tape.fast_forward || tape.pause_before_death)
611   {
612     tape.pausing = FALSE;
613
614     SetDrawDeactivationMask(REDRAW_FIELD | REDRAW_DOOR_1);
615     audio.sound_deactivated = TRUE;
616   }
617 }
618
619 void TapeSingleStep()
620 {
621   if (options.network)
622     return;
623
624   if (!tape.pausing)
625     TapeTogglePause(TAPE_TOGGLE_MANUAL);
626
627   tape.single_step = !tape.single_step;
628 }
629
630 void TapeQuickSave()
631 {
632   if (game_status == PLAYING)
633   {
634     if (tape.recording)
635       TapeHaltRecording();      /* prepare tape for saving on-the-fly */
636
637     if (TAPE_IS_EMPTY(tape))
638       Request("No tape that can be saved !", REQ_CONFIRM);
639     else
640       SaveTape(tape.level_nr);
641   }
642   else if (game_status == MAINMENU)
643     Request("No game that can be saved !", REQ_CONFIRM);
644 }
645
646 void TapeQuickLoad()
647 {
648   if (game_status == PLAYING || game_status == MAINMENU)
649   {
650     TapeStop();
651     TapeErase();
652
653     LoadTape(level_nr);
654     if (!TAPE_IS_EMPTY(tape))
655     {
656       TapeStartGamePlaying();
657       TapeIndexSearch();
658
659       tape.quick_resume = TRUE;
660     }
661     else
662       Request("No tape for this level !", REQ_CONFIRM);
663   }
664 }
665
666
667 /* ---------- new tape button stuff ---------------------------------------- */
668
669 /* graphic position values for tape buttons */
670 #define TAPE_BUTTON_XSIZE       18
671 #define TAPE_BUTTON_YSIZE       18
672 #define TAPE_BUTTON_XPOS        5
673 #define TAPE_BUTTON_YPOS        77
674
675 #define TAPE_BUTTON_EJECT_XPOS  (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
676 #define TAPE_BUTTON_INDEX_XPOS  (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
677 #define TAPE_BUTTON_STOP_XPOS   (TAPE_BUTTON_XPOS + 1 * TAPE_BUTTON_XSIZE)
678 #define TAPE_BUTTON_PAUSE_XPOS  (TAPE_BUTTON_XPOS + 2 * TAPE_BUTTON_XSIZE)
679 #define TAPE_BUTTON_RECORD_XPOS (TAPE_BUTTON_XPOS + 3 * TAPE_BUTTON_XSIZE)
680 #define TAPE_BUTTON_PLAY_XPOS   (TAPE_BUTTON_XPOS + 4 * TAPE_BUTTON_XSIZE)
681
682 static struct
683 {
684   int x, y;
685   int gadget_id;
686   char *infotext;
687 } tapebutton_info[NUM_TAPE_BUTTONS] =
688 {
689   {
690     TAPE_BUTTON_EJECT_XPOS,     TAPE_BUTTON_YPOS,
691     TAPE_CTRL_ID_EJECT,
692     "eject tape"
693   },
694   {
695     TAPE_BUTTON_INDEX_XPOS,     TAPE_BUTTON_YPOS,
696     TAPE_CTRL_ID_INDEX,
697     "index mark"
698   },
699   {
700     TAPE_BUTTON_STOP_XPOS,      TAPE_BUTTON_YPOS,
701     TAPE_CTRL_ID_STOP,
702     "stop tape"
703   },
704   {
705     TAPE_BUTTON_PAUSE_XPOS,     TAPE_BUTTON_YPOS,
706     TAPE_CTRL_ID_PAUSE,
707     "pause tape"
708   },
709   {
710     TAPE_BUTTON_RECORD_XPOS,    TAPE_BUTTON_YPOS,
711     TAPE_CTRL_ID_RECORD,
712     "record tape"
713   },
714   {
715     TAPE_BUTTON_PLAY_XPOS,      TAPE_BUTTON_YPOS,
716     TAPE_CTRL_ID_PLAY,
717     "play tape"
718   }
719 };
720
721 void CreateTapeButtons()
722 {
723   int i;
724
725   for (i=0; i<NUM_TAPE_BUTTONS; i++)
726   {
727     Bitmap *gd_bitmap = pix[PIX_DOOR];
728     struct GadgetInfo *gi;
729     int gd_xoffset, gd_yoffset;
730     int gd_x1, gd_x2, gd_y;
731     int id = i;
732
733     gd_xoffset = tapebutton_info[i].x;
734     gd_yoffset = tapebutton_info[i].y;
735     gd_x1 = DOOR_GFX_PAGEX4 + gd_xoffset;
736     gd_x2 = DOOR_GFX_PAGEX3 + gd_xoffset;
737     gd_y  = DOOR_GFX_PAGEY2 + gd_yoffset;
738
739     if (i == TAPE_CTRL_ID_INDEX)
740     {
741       gd_x1 = DOOR_GFX_PAGEX6 + gd_xoffset;
742       gd_x2 = DOOR_GFX_PAGEX5 + gd_xoffset;
743     }
744
745     gi = CreateGadget(GDI_CUSTOM_ID, id,
746                       GDI_INFO_TEXT, tapebutton_info[i].infotext,
747                       GDI_X, VX + gd_xoffset,
748                       GDI_Y, VY + gd_yoffset,
749                       GDI_WIDTH, TAPE_BUTTON_XSIZE,
750                       GDI_HEIGHT, TAPE_BUTTON_YSIZE,
751                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
752                       GDI_STATE, GD_BUTTON_UNPRESSED,
753                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y,
754                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y,
755                       GDI_EVENT_MASK, GD_EVENT_RELEASED,
756                       GDI_CALLBACK_ACTION, HandleTapeButtons,
757                       GDI_END);
758
759     if (gi == NULL)
760       Error(ERR_EXIT, "cannot create gadget");
761
762     tape_gadget[id] = gi;
763   }
764 }
765
766 void MapTapeEjectButton()
767 {
768   UnmapGadget(tape_gadget[TAPE_CTRL_ID_INDEX]);
769   MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
770 }
771
772 void MapTapeIndexButton()
773 {
774   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
775   MapGadget(tape_gadget[TAPE_CTRL_ID_INDEX]);
776 }
777
778 void MapTapeButtons()
779 {
780   int i;
781
782   for (i=0; i<NUM_TAPE_BUTTONS; i++)
783     if (i != TAPE_CTRL_ID_INDEX)
784       MapGadget(tape_gadget[i]);
785
786   if (tape.recording || tape.playing)
787     MapTapeIndexButton();
788 }
789
790 void UnmapTapeButtons()
791 {
792   int i;
793
794   for (i=0; i<NUM_TAPE_BUTTONS; i++)
795     UnmapGadget(tape_gadget[i]);
796 }
797
798 static void HandleTapeButtons(struct GadgetInfo *gi)
799 {
800   int id = gi->custom_id;
801
802   if (game_status != MAINMENU && game_status != PLAYING)
803     return;
804
805   switch (id)
806   {
807     case TAPE_CTRL_ID_EJECT:
808       TapeStop();
809       if (TAPE_IS_EMPTY(tape))
810       {
811         LoadTape(level_nr);
812         if (TAPE_IS_EMPTY(tape))
813           Request("No tape for this level !", REQ_CONFIRM);
814       }
815       else
816       {
817         if (tape.changed)
818           SaveTape(tape.level_nr);
819         TapeErase();
820       }
821       DrawCompleteVideoDisplay();
822       break;
823
824     case TAPE_CTRL_ID_INDEX:
825       if (tape.playing)
826         TapeIndexSearch();
827       else if (tape.recording)
828         TapeSingleStep();
829       break;
830
831     case TAPE_CTRL_ID_STOP:
832       TapeStop();
833       break;
834
835     case TAPE_CTRL_ID_PAUSE:
836       TapeTogglePause(TAPE_TOGGLE_MANUAL);
837       break;
838
839     case TAPE_CTRL_ID_RECORD:
840       if (TAPE_IS_STOPPED(tape))
841         TapeStartGameRecording();
842       else if (tape.pausing)
843       {
844         if (tape.playing)       /* PLAYING -> PAUSING -> RECORDING */
845           TapeAppendRecording();
846         else
847           TapeTogglePause(TAPE_TOGGLE_MANUAL);
848       }
849       break;
850
851     case TAPE_CTRL_ID_PLAY:
852       if (TAPE_IS_EMPTY(tape))
853         break;
854
855       if (TAPE_IS_STOPPED(tape))
856       {
857         TapeStartGamePlaying();
858       }
859       else if (tape.playing)
860       {
861         if (tape.pausing)                       /* PAUSE -> PLAY */
862         {
863           TapeTogglePause(TAPE_TOGGLE_MANUAL);
864         }
865         else if (!tape.fast_forward)            /* PLAY -> FAST FORWARD PLAY */
866         {
867           tape.fast_forward = TRUE;
868           DrawVideoDisplay(VIDEO_STATE_FFWD_ON, 0);
869         }
870         else if (!tape.pause_before_death)      /* FFWD PLAY -> AUTO PAUSE */
871         {
872           tape.pause_before_death = TRUE;
873           DrawVideoDisplay(VIDEO_STATE_PBEND_ON, VIDEO_DISPLAY_LABEL_ONLY);
874         }
875         else                                    /* AUTO PAUSE -> NORMAL PLAY */
876         {
877           tape.fast_forward = FALSE;
878           tape.pause_before_death = FALSE;
879           DrawVideoDisplay(VIDEO_STATE_FFWD_OFF | VIDEO_STATE_PBEND_OFF, 0);
880         }
881       }
882       break;
883
884     default:
885       break;
886   }
887 }