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