db15dde94b1e7c1307af7f0c136396472395e627
[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 TapeStartRecording()
294 {
295   time_t zeit1 = time(NULL);
296   struct tm *zeit2 = localtime(&zeit1);
297   int i;
298
299   if (!TAPE_IS_STOPPED(tape))
300     TapeStop();
301
302   tape.level_nr = level_nr;
303   tape.length = 0;
304   tape.counter = 0;
305   tape.pos[tape.counter].delay = 0;
306   tape.recording = TRUE;
307   tape.playing = FALSE;
308   tape.pausing = FALSE;
309   tape.changed = TRUE;
310   tape.date = 10000*(zeit2->tm_year%100) + 100*zeit2->tm_mon + zeit2->tm_mday;
311   tape.random_seed = InitRND(NEW_RANDOMIZE);
312   tape.game_version = GAME_VERSION_ACTUAL;
313
314   for(i=0; i<MAX_PLAYERS; i++)
315     tape.player_participates[i] = FALSE;
316
317   DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
318   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
319   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
320   MapTapeIndexButton();
321
322   SetDrawDeactivationMask(REDRAW_NONE);
323 }
324
325 static void TapeStartGameRecording()
326 {
327   TapeStartRecording();
328
329 #if defined(PLATFORM_UNIX)
330   if (options.network)
331     SendToServer_StartPlaying();
332   else
333 #endif
334   {
335     game_status = PLAYING;
336     StopAnimation();
337     InitGame();
338   }
339 }
340
341 static void TapeAppendRecording()
342 {
343   if (!tape.playing || !tape.pausing)
344     return;
345
346   if (tape.game_version != GAME_VERSION_ACTUAL &&
347       !Request("This may break old version tape ! Append anyway ?",
348                REQ_ASK))
349     return;
350
351   tape.pos[tape.counter].delay = tape.delay_played;
352   tape.playing = FALSE;
353   tape.recording = TRUE;
354   tape.changed = TRUE;
355   tape.game_version = GAME_VERSION_ACTUAL;
356
357   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON,0);
358 }
359
360 void TapeHaltRecording()
361 {
362   if (!tape.recording)
363     return;
364
365   tape.counter++;
366   tape.length = tape.counter;
367   tape.length_seconds = GetTapeLength();
368 }
369
370 void TapeStopRecording()
371 {
372   if (!tape.recording)
373     return;
374
375   TapeHaltRecording();
376
377   tape.recording = FALSE;
378   tape.pausing = FALSE;
379
380   DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
381   MapTapeEjectButton();
382 }
383
384 void TapeRecordAction(byte action[MAX_PLAYERS])
385 {
386   int i;
387
388   if (!tape.recording || tape.pausing)
389     return;
390
391   if (tape.counter >= MAX_TAPELEN - 1)
392   {
393     TapeStopRecording();
394     return;
395   }
396
397   if (tape.pos[tape.counter].delay > 0)         /* already stored action */
398   {
399     boolean changed_events = FALSE;
400
401     for(i=0; i<MAX_PLAYERS; i++)
402       if (tape.pos[tape.counter].action[i] != action[i])
403         changed_events = TRUE;
404
405     if (changed_events || tape.pos[tape.counter].delay >= 255)
406     {
407       tape.counter++;
408       tape.pos[tape.counter].delay = 0;
409     }
410     else
411       tape.pos[tape.counter].delay++;
412   }
413
414   if (tape.pos[tape.counter].delay == 0)        /* store new action */
415   {
416     for(i=0; i<MAX_PLAYERS; i++)
417       tape.pos[tape.counter].action[i] = action[i];
418
419     tape.pos[tape.counter].delay++;
420   }
421 }
422
423 void TapeTogglePause()
424 {
425   unsigned long state;
426
427   if (!tape.recording && !tape.playing)
428     return;
429
430   tape.pausing = !tape.pausing;
431   tape.fast_forward = FALSE;
432   tape.pause_before_death = FALSE;
433
434   state = (tape.pausing ? VIDEO_STATE_PAUSE_ON : VIDEO_STATE_PAUSE_OFF);
435   if (tape.playing)
436     state |= VIDEO_STATE_PBEND_OFF;
437
438   DrawVideoDisplay(state, 0);
439
440   if (tape.index_search)
441   {
442     SetDrawDeactivationMask(REDRAW_NONE);
443     RedrawPlayfield(TRUE, 0,0,0,0);
444   }
445
446   if (tape.index_search)
447   {
448     tape.index_search = FALSE;
449
450     SetDrawDeactivationMask(REDRAW_NONE);
451     RedrawPlayfield(TRUE, 0,0,0,0);
452
453     if (tape.quick_resume)
454     {
455       tape.quick_resume = FALSE;
456
457       TapeAppendRecording();
458       TapeTogglePause();
459     }
460   }
461 }
462
463 void TapeStartPlaying()
464 {
465   if (TAPE_IS_EMPTY(tape))
466     return;
467
468   if (!TAPE_IS_STOPPED(tape))
469     TapeStop();
470
471   tape.counter = 0;
472   tape.delay_played = 0;
473   tape.pause_before_death = FALSE;
474   tape.recording = FALSE;
475   tape.playing = TRUE;
476   tape.pausing = FALSE;
477   tape.fast_forward = FALSE;
478   tape.index_search = FALSE;
479   tape.quick_resume = FALSE;
480
481   InitRND(tape.random_seed);
482
483   DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
484   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
485   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
486   MapTapeIndexButton();
487
488   SetDrawDeactivationMask(REDRAW_NONE);
489 }
490
491 static void TapeStartGamePlaying()
492 {
493   TapeStartPlaying();
494
495   game_status = PLAYING;
496   StopAnimation();
497   InitGame();
498 }
499
500 void TapeStopPlaying()
501 {
502   if (!tape.playing)
503     return;
504
505   tape.playing = FALSE;
506   tape.pausing = FALSE;
507
508   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
509   MapTapeEjectButton();
510 }
511
512 byte *TapePlayAction()
513 {
514   static byte action[MAX_PLAYERS];
515   int i;
516
517   if (!tape.playing || tape.pausing)
518     return NULL;
519
520   if (tape.pause_before_death)  /* STOP 10s BEFORE PLAYER GETS KILLED... */
521   {
522     if (!(FrameCounter % 20))
523     {
524       if ((FrameCounter / 20) % 2)
525         DrawVideoDisplay(VIDEO_STATE_PBEND_ON, VIDEO_DISPLAY_LABEL_ONLY);
526       else
527         DrawVideoDisplay(VIDEO_STATE_PBEND_OFF, VIDEO_DISPLAY_LABEL_ONLY);
528     }
529
530     if (TimePlayed > tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
531     {
532       TapeTogglePause();
533       return NULL;
534     }
535   }
536
537   if (tape.counter >= tape.length)      /* end of tape reached */
538   {
539     if (tape.index_search)
540       TapeTogglePause();
541     else
542       TapeStop();
543
544     return NULL;
545   }
546
547   for(i=0; i<MAX_PLAYERS; i++)
548     action[i] = tape.pos[tape.counter].action[i];
549
550   tape.delay_played++;
551   if (tape.delay_played >= tape.pos[tape.counter].delay)
552   {
553     tape.counter++;
554     tape.delay_played = 0;
555   }
556
557   return action;
558 }
559
560 void TapeStop()
561 {
562   TapeStopRecording();
563   TapeStopPlaying();
564
565   DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0);
566   if (tape.date && tape.length)
567   {
568     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
569     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
570   }
571 }
572
573 void TapeErase()
574 {
575   tape.length = 0;
576 }
577
578 unsigned int GetTapeLength()
579 {
580   unsigned int tape_length = 0;
581   int i;
582
583   if (TAPE_IS_EMPTY(tape))
584     return(0);
585
586   for(i=0;i<tape.length;i++)
587     tape_length += tape.pos[i].delay;
588
589   return(tape_length * GAME_FRAME_DELAY / 1000);
590 }
591
592 void TapeIndexSearch()
593 {
594   tape.index_search = TRUE;
595
596   if (!tape.fast_forward || tape.pause_before_death)
597     SetDrawDeactivationMask(REDRAW_FIELD);
598 }
599
600 void TapeQuickSave()
601 {
602   if (game_status == PLAYING)
603   {
604     if (tape.recording)
605       TapeHaltRecording();      /* prepare tape for saving on-the-fly */
606
607     if (TAPE_IS_EMPTY(tape))
608       Request("No tape that can be saved !", REQ_CONFIRM);
609     else
610       SaveTape(tape.level_nr);
611   }
612   else if (game_status == MAINMENU)
613     Request("No game that can be saved !", REQ_CONFIRM);
614 }
615
616 void TapeQuickLoad()
617 {
618   if (game_status == PLAYING || game_status == MAINMENU)
619   {
620     TapeStop();
621     TapeErase();
622
623     LoadTape(level_nr);
624     if (!TAPE_IS_EMPTY(tape))
625     {
626       TapeStartGamePlaying();
627       TapeIndexSearch();
628
629       tape.quick_resume = TRUE;
630     }
631     else
632       Request("No tape for this level !", REQ_CONFIRM);
633   }
634 }
635
636
637 /* ---------- new tape button stuff ---------------------------------------- */
638
639 /* graphic position values for tape buttons */
640 #define TAPE_BUTTON_XSIZE       18
641 #define TAPE_BUTTON_YSIZE       18
642 #define TAPE_BUTTON_XPOS        5
643 #define TAPE_BUTTON_YPOS        77
644
645 #define TAPE_BUTTON_EJECT_XPOS  (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
646 #define TAPE_BUTTON_INDEX_XPOS  (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
647 #define TAPE_BUTTON_STOP_XPOS   (TAPE_BUTTON_XPOS + 1 * TAPE_BUTTON_XSIZE)
648 #define TAPE_BUTTON_PAUSE_XPOS  (TAPE_BUTTON_XPOS + 2 * TAPE_BUTTON_XSIZE)
649 #define TAPE_BUTTON_RECORD_XPOS (TAPE_BUTTON_XPOS + 3 * TAPE_BUTTON_XSIZE)
650 #define TAPE_BUTTON_PLAY_XPOS   (TAPE_BUTTON_XPOS + 4 * TAPE_BUTTON_XSIZE)
651
652 static struct
653 {
654   int x, y;
655   int gadget_id;
656   char *infotext;
657 } tapebutton_info[NUM_TAPE_BUTTONS] =
658 {
659   {
660     TAPE_BUTTON_EJECT_XPOS,     TAPE_BUTTON_YPOS,
661     TAPE_CTRL_ID_EJECT,
662     "eject tape"
663   },
664   {
665     TAPE_BUTTON_INDEX_XPOS,     TAPE_BUTTON_YPOS,
666     TAPE_CTRL_ID_INDEX,
667     "index mark"
668   },
669   {
670     TAPE_BUTTON_STOP_XPOS,      TAPE_BUTTON_YPOS,
671     TAPE_CTRL_ID_STOP,
672     "stop tape"
673   },
674   {
675     TAPE_BUTTON_PAUSE_XPOS,     TAPE_BUTTON_YPOS,
676     TAPE_CTRL_ID_PAUSE,
677     "pause tape"
678   },
679   {
680     TAPE_BUTTON_RECORD_XPOS,    TAPE_BUTTON_YPOS,
681     TAPE_CTRL_ID_RECORD,
682     "record tape"
683   },
684   {
685     TAPE_BUTTON_PLAY_XPOS,      TAPE_BUTTON_YPOS,
686     TAPE_CTRL_ID_PLAY,
687     "play tape"
688   }
689 };
690
691 void CreateTapeButtons()
692 {
693   int i;
694
695   for (i=0; i<NUM_TAPE_BUTTONS; i++)
696   {
697     Bitmap *gd_bitmap = pix[PIX_DOOR];
698     struct GadgetInfo *gi;
699     int gd_xoffset, gd_yoffset;
700     int gd_x1, gd_x2, gd_y;
701     int id = i;
702
703     gd_xoffset = tapebutton_info[i].x;
704     gd_yoffset = tapebutton_info[i].y;
705     gd_x1 = DOOR_GFX_PAGEX4 + gd_xoffset;
706     gd_x2 = DOOR_GFX_PAGEX3 + gd_xoffset;
707     gd_y  = DOOR_GFX_PAGEY2 + gd_yoffset;
708
709     if (i == TAPE_CTRL_ID_INDEX)
710     {
711       gd_x1 = DOOR_GFX_PAGEX6 + gd_xoffset;
712       gd_x2 = DOOR_GFX_PAGEX5 + gd_xoffset;
713     }
714
715     gi = CreateGadget(GDI_CUSTOM_ID, id,
716                       GDI_INFO_TEXT, tapebutton_info[i].infotext,
717                       GDI_X, VX + gd_xoffset,
718                       GDI_Y, VY + gd_yoffset,
719                       GDI_WIDTH, TAPE_BUTTON_XSIZE,
720                       GDI_HEIGHT, TAPE_BUTTON_YSIZE,
721                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
722                       GDI_STATE, GD_BUTTON_UNPRESSED,
723                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y,
724                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y,
725                       GDI_EVENT_MASK, GD_EVENT_RELEASED,
726                       GDI_CALLBACK_ACTION, HandleTapeButtons,
727                       GDI_END);
728
729     if (gi == NULL)
730       Error(ERR_EXIT, "cannot create gadget");
731
732     tape_gadget[id] = gi;
733   }
734 }
735
736 void MapTapeEjectButton()
737 {
738   UnmapGadget(tape_gadget[TAPE_CTRL_ID_INDEX]);
739   MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
740 }
741
742 void MapTapeIndexButton()
743 {
744   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
745   MapGadget(tape_gadget[TAPE_CTRL_ID_INDEX]);
746 }
747
748 void MapTapeButtons()
749 {
750   int i;
751
752   for (i=0; i<NUM_TAPE_BUTTONS; i++)
753     if (i != TAPE_CTRL_ID_INDEX)
754       MapGadget(tape_gadget[i]);
755
756   if (tape.recording || tape.playing)
757     MapTapeIndexButton();
758 }
759
760 void UnmapTapeButtons()
761 {
762   int i;
763
764   for (i=0; i<NUM_TAPE_BUTTONS; i++)
765     UnmapGadget(tape_gadget[i]);
766 }
767
768 static void HandleTapeButtons(struct GadgetInfo *gi)
769 {
770   int id = gi->custom_id;
771
772   if (game_status != MAINMENU && game_status != PLAYING)
773     return;
774
775   switch (id)
776   {
777     case TAPE_CTRL_ID_EJECT:
778       TapeStop();
779       if (TAPE_IS_EMPTY(tape))
780       {
781         LoadTape(level_nr);
782         if (TAPE_IS_EMPTY(tape))
783           Request("No tape for this level !", REQ_CONFIRM);
784       }
785       else
786       {
787         if (tape.changed)
788           SaveTape(tape.level_nr);
789         TapeErase();
790       }
791       DrawCompleteVideoDisplay();
792       break;
793
794     case TAPE_CTRL_ID_INDEX:
795       if (tape.playing)
796         TapeIndexSearch();
797       else if (tape.recording)
798         ;       /* setting index mark -- not yet implemented */
799       break;
800
801     case TAPE_CTRL_ID_STOP:
802       TapeStop();
803       break;
804
805     case TAPE_CTRL_ID_PAUSE:
806       TapeTogglePause();
807       break;
808
809     case TAPE_CTRL_ID_RECORD:
810       if (TAPE_IS_STOPPED(tape))
811         TapeStartGameRecording();
812       else if (tape.pausing)
813       {
814         if (tape.playing)       /* PLAYING -> PAUSING -> RECORDING */
815           TapeAppendRecording();
816         else
817           TapeTogglePause();
818       }
819       break;
820
821     case TAPE_CTRL_ID_PLAY:
822       if (TAPE_IS_EMPTY(tape))
823         break;
824
825       if (TAPE_IS_STOPPED(tape))
826       {
827         TapeStartGamePlaying();
828       }
829       else if (tape.playing)
830       {
831         if (tape.pausing)                       /* PAUSE -> PLAY */
832         {
833           TapeTogglePause();
834         }
835         else if (!tape.fast_forward)            /* PLAY -> FAST FORWARD PLAY */
836         {
837           tape.fast_forward = TRUE;
838           DrawVideoDisplay(VIDEO_STATE_FFWD_ON, 0);
839         }
840         else if (!tape.pause_before_death)      /* FFWD PLAY -> AUTO PAUSE */
841         {
842           tape.pause_before_death = TRUE;
843           DrawVideoDisplay(VIDEO_STATE_PBEND_ON, VIDEO_DISPLAY_LABEL_ONLY);
844         }
845         else                                    /* AUTO PAUSE -> NORMAL PLAY */
846         {
847           tape.fast_forward = FALSE;
848           tape.pause_before_death = FALSE;
849           DrawVideoDisplay(VIDEO_STATE_FFWD_OFF | VIDEO_STATE_PBEND_OFF, 0);
850         }
851       }
852       break;
853
854     default:
855       break;
856   }
857 }