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