fixed bug with game buttons in tape area not properly redrawn
[rocksndiamonds.git] / src / tape.c
1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // tape.c
10 // ============================================================================
11
12 #include "libgame/libgame.h"
13
14 #include "tape.h"
15 #include "init.h"
16 #include "game.h"
17 #include "tools.h"
18 #include "files.h"
19 #include "network.h"
20 #include "cartoons.h"
21
22 #define DEBUG_TAPE_WHEN_PLAYING                 FALSE
23
24 /* tape button identifiers */
25 #define TAPE_CTRL_ID_EJECT                      0
26 #define TAPE_CTRL_ID_EXTRA                      1
27 #define TAPE_CTRL_ID_STOP                       2
28 #define TAPE_CTRL_ID_PAUSE                      3
29 #define TAPE_CTRL_ID_RECORD                     4
30 #define TAPE_CTRL_ID_PLAY                       5
31
32 #define NUM_TAPE_BUTTONS                        6
33
34 /* values for tape handling */
35 #define TAPE_PAUSE_SECONDS_BEFORE_DEATH         5
36
37 /* forward declaration for internal use */
38 static void HandleTapeButtons(struct GadgetInfo *);
39 static void TapeStopWarpForward();
40
41 static struct GadgetInfo *tape_gadget[NUM_TAPE_BUTTONS];
42
43
44 /* ========================================================================= */
45 /* video tape definitions                                                    */
46 /* ========================================================================= */
47
48 /* some positions in the video tape control window */
49 #define VIDEO_DATE_LABEL_XPOS           (VIDEO_DISPLAY1_XPOS)
50 #define VIDEO_DATE_LABEL_YPOS           (VIDEO_DISPLAY1_YPOS)
51 #define VIDEO_DATE_LABEL_XSIZE          (VIDEO_DISPLAY_XSIZE)
52 #define VIDEO_DATE_LABEL_YSIZE          (VIDEO_DISPLAY_YSIZE)
53 #define VIDEO_DATE_XPOS                 (VIDEO_DISPLAY1_XPOS + 2)
54 #define VIDEO_DATE_YPOS                 (VIDEO_DISPLAY1_YPOS + 14)
55 #define VIDEO_DATE_XSIZE                (VIDEO_DISPLAY_XSIZE)
56 #define VIDEO_DATE_YSIZE                16
57 #define VIDEO_REC_LABEL_XPOS            (VIDEO_DISPLAY2_XPOS)
58 #define VIDEO_REC_LABEL_YPOS            (VIDEO_DISPLAY2_YPOS)
59 #define VIDEO_REC_LABEL_XSIZE           20
60 #define VIDEO_REC_LABEL_YSIZE           12
61 #define VIDEO_REC_SYMBOL_XPOS           (VIDEO_DISPLAY2_XPOS + 20)
62 #define VIDEO_REC_SYMBOL_YPOS           (VIDEO_DISPLAY2_YPOS)
63 #define VIDEO_REC_SYMBOL_XSIZE          16
64 #define VIDEO_REC_SYMBOL_YSIZE          16
65 #define VIDEO_PLAY_LABEL_XPOS           (VIDEO_DISPLAY2_XPOS + 65)
66 #define VIDEO_PLAY_LABEL_YPOS           (VIDEO_DISPLAY2_YPOS)
67 #define VIDEO_PLAY_LABEL_XSIZE          22
68 #define VIDEO_PLAY_LABEL_YSIZE          12
69 #define VIDEO_PLAY_SYMBOL_XPOS          (VIDEO_DISPLAY2_XPOS + 52)
70 #define VIDEO_PLAY_SYMBOL_YPOS          (VIDEO_DISPLAY2_YPOS)
71 #define VIDEO_PLAY_SYMBOL_XSIZE         11
72 #define VIDEO_PLAY_SYMBOL_YSIZE         13
73 #define VIDEO_PAUSE_LABEL_XPOS          (VIDEO_DISPLAY2_XPOS)
74 #define VIDEO_PAUSE_LABEL_YPOS          (VIDEO_DISPLAY2_YPOS + 20)
75 #define VIDEO_PAUSE_LABEL_XSIZE         35
76 #define VIDEO_PAUSE_LABEL_YSIZE         8
77 #define VIDEO_PAUSE_SYMBOL_XPOS         (VIDEO_DISPLAY2_XPOS + 35)
78 #define VIDEO_PAUSE_SYMBOL_YPOS         (VIDEO_DISPLAY2_YPOS)
79 #define VIDEO_PAUSE_SYMBOL_XSIZE        17
80 #define VIDEO_PAUSE_SYMBOL_YSIZE        13
81 #define VIDEO_TIME_XPOS                 (VIDEO_DISPLAY2_XPOS + 39)
82 #define VIDEO_TIME_YPOS                 (VIDEO_DISPLAY2_YPOS + 14)
83 #define VIDEO_TIME_XSIZE                50
84 #define VIDEO_TIME_YSIZE                16
85
86 /* some default values for tape labels and symbols */
87 #define VIDEO_LABEL_XPOS                5
88 #define VIDEO_LABEL_YPOS                42
89 #define VIDEO_LABEL_XSIZE               40
90 #define VIDEO_LABEL_YSIZE               28
91 #define VIDEO_SYMBOL_XPOS               39
92 #define VIDEO_SYMBOL_YPOS               42
93 #define VIDEO_SYMBOL_XSIZE              56
94 #define VIDEO_SYMBOL_YSIZE              13
95
96 /* values for certain tape labels and symbols */
97 #define VIDEO_FFWD_LABEL_XPOS           VIDEO_LABEL_XPOS
98 #define VIDEO_FFWD_LABEL_YPOS           193
99 #define VIDEO_FFWD_LABEL_XSIZE          VIDEO_LABEL_XSIZE
100 #define VIDEO_FFWD_LABEL_YSIZE          VIDEO_LABEL_YSIZE
101 #define VIDEO_FFWD_SYMBOL_XPOS          VIDEO_SYMBOL_XPOS
102 #define VIDEO_FFWD_SYMBOL_YPOS          193
103 #define VIDEO_FFWD_SYMBOL_XSIZE         27
104 #define VIDEO_FFWD_SYMBOL_YSIZE         VIDEO_SYMBOL_YSIZE
105
106 #define VIDEO_PBEND_LABEL_XPOS          VIDEO_LABEL_XPOS
107 #define VIDEO_PBEND_LABEL_YPOS          221
108 #define VIDEO_PBEND_LABEL_XSIZE         VIDEO_LABEL_XSIZE
109 #define VIDEO_PBEND_LABEL_YSIZE         VIDEO_LABEL_YSIZE
110 #define VIDEO_PBEND_SYMBOL_XPOS         VIDEO_SYMBOL_XPOS
111 #define VIDEO_PBEND_SYMBOL_YPOS         221
112 #define VIDEO_PBEND_SYMBOL_XSIZE        27
113 #define VIDEO_PBEND_SYMBOL_YSIZE        VIDEO_SYMBOL_YSIZE
114
115 #define VIDEO_WARP_LABEL_XPOS           VIDEO_LABEL_XPOS
116 #define VIDEO_WARP_LABEL_YPOS           165
117 #define VIDEO_WARP_LABEL_XSIZE          VIDEO_LABEL_XSIZE
118 #define VIDEO_WARP_LABEL_YSIZE          VIDEO_LABEL_YSIZE
119 #define VIDEO_WARP_SYMBOL_XPOS          VIDEO_SYMBOL_XPOS
120 #define VIDEO_WARP_SYMBOL_YPOS          165
121 #define VIDEO_WARP_SYMBOL_XSIZE         27
122 #define VIDEO_WARP_SYMBOL_YSIZE         VIDEO_SYMBOL_YSIZE
123 #define VIDEO_WARP2_SYMBOL_XPOS         VIDEO_SYMBOL_XPOS
124 #define VIDEO_WARP2_SYMBOL_YPOS         152
125 #define VIDEO_WARP2_SYMBOL_XSIZE        27
126 #define VIDEO_WARP2_SYMBOL_YSIZE        VIDEO_SYMBOL_YSIZE
127
128 #define VIDEO_1STEP_SYMBOL_XPOS         (VIDEO_SYMBOL_XPOS + 18)
129 #define VIDEO_1STEP_SYMBOL_YPOS         139
130 #define VIDEO_1STEP_SYMBOL_XSIZE        (VIDEO_SYMBOL_XSIZE - 18)
131 #define VIDEO_1STEP_SYMBOL_YSIZE        VIDEO_SYMBOL_YSIZE
132
133 #define PG_X3(x)                        (DOOR_GFX_PAGEX3 + (x))
134 #define PG_X4(x)                        (DOOR_GFX_PAGEX4 + (x))
135 #define PG_X5(x)                        (DOOR_GFX_PAGEX5 + (x))
136 #define PG_X6(x)                        (DOOR_GFX_PAGEX6 + (x))
137
138 #define PG_Y1(y)                        (DOOR_GFX_PAGEY1 + (y))
139 #define PG_Y2(y)                        (DOOR_GFX_PAGEY2 + (y))
140
141 #define VIDEO_INFO_OFF                  (VIDEO_STATE_DATE_OFF   |       \
142                                          VIDEO_STATE_TIME_OFF)
143 #define VIDEO_STATE_OFF                 (VIDEO_STATE_PLAY_OFF   |       \
144                                          VIDEO_STATE_REC_OFF    |       \
145                                          VIDEO_STATE_PAUSE_OFF  |       \
146                                          VIDEO_STATE_FFWD_OFF   |       \
147                                          VIDEO_STATE_PBEND_OFF  |       \
148                                          VIDEO_STATE_1STEP_OFF)
149 #define VIDEO_PRESS_OFF                 (VIDEO_PRESS_PLAY_OFF   |       \
150                                          VIDEO_PRESS_REC_OFF    |       \
151                                          VIDEO_PRESS_PAUSE_OFF  |       \
152                                          VIDEO_PRESS_STOP_OFF   |       \
153                                          VIDEO_PRESS_EJECT_OFF)
154 #define VIDEO_ALL_OFF                   (VIDEO_INFO_OFF         |       \
155                                          VIDEO_STATE_OFF        |       \
156                                          VIDEO_PRESS_OFF)
157
158 #define VIDEO_INFO_ON                   (VIDEO_STATE_DATE_ON    |       \
159                                          VIDEO_STATE_TIME_ON)
160 #define VIDEO_STATE_ON                  (VIDEO_STATE_PLAY_ON    |       \
161                                          VIDEO_STATE_REC_ON     |       \
162                                          VIDEO_STATE_PAUSE_ON   |       \
163                                          VIDEO_STATE_FFWD_ON    |       \
164                                          VIDEO_STATE_PBEND_ON   |       \
165                                          VIDEO_STATE_1STEP_ON)
166 #define VIDEO_PRESS_ON                  (VIDEO_PRESS_PLAY_ON    |       \
167                                          VIDEO_PRESS_REC_ON     |       \
168                                          VIDEO_PRESS_PAUSE_ON   |       \
169                                          VIDEO_PRESS_STOP_ON    |       \
170                                          VIDEO_PRESS_EJECT_ON)
171 #define VIDEO_ALL_ON                    (VIDEO_INFO_ON          |       \
172                                          VIDEO_STATE_ON         |       \
173                                          VIDEO_PRESS_ON)
174
175 #define VIDEO_INFO                      (VIDEO_INFO_ON | VIDEO_INFO_OFF)
176 #define VIDEO_STATE                     (VIDEO_STATE_ON | VIDEO_STATE_OFF)
177 #define VIDEO_PRESS                     (VIDEO_PRESS_ON | VIDEO_PRESS_OFF)
178 #define VIDEO_ALL                       (VIDEO_ALL_ON | VIDEO_ALL_OFF)
179
180 #define NUM_TAPE_FUNCTIONS              10
181 #define NUM_TAPE_FUNCTION_PARTS         2
182 #define NUM_TAPE_FUNCTION_STATES        2
183
184
185 /* ========================================================================= */
186 /* video display functions                                                   */
187 /* ========================================================================= */
188
189 static void DrawVideoDisplay_Graphics(unsigned int state, unsigned int value)
190 {
191   int i, j, k;
192
193   static struct
194   {
195     int graphic;
196     struct Rect *pos;
197   }
198   video_pos[NUM_TAPE_FUNCTIONS][NUM_TAPE_FUNCTION_PARTS] =
199   {
200     {
201       { IMG_TAPE_LABEL_GFX_PLAY,                &tape.label.play        },
202       { IMG_TAPE_SYMBOL_GFX_PLAY,               &tape.symbol.play       },
203     },
204     {
205       { IMG_TAPE_LABEL_GFX_RECORD,              &tape.label.record      },
206       { IMG_TAPE_SYMBOL_GFX_RECORD,             &tape.symbol.record     },
207     },
208     {
209       { IMG_TAPE_LABEL_GFX_PAUSE,               &tape.label.pause       },
210       { IMG_TAPE_SYMBOL_GFX_PAUSE,              &tape.symbol.pause      },
211     },
212     {
213       { IMG_TAPE_LABEL_GFX_DATE,                &tape.label.date        },
214       { -1,                                     NULL                    },
215     },
216     {
217       { IMG_TAPE_LABEL_GFX_TIME,                &tape.label.time        },
218       { -1,                                     NULL                    },
219     },
220     {
221       { IMG_TAPE_LABEL_GFX_FAST_FORWARD,        &tape.label.fast_forward  },
222       { IMG_TAPE_SYMBOL_GFX_FAST_FORWARD,       &tape.symbol.fast_forward },
223     },
224     {
225       { IMG_TAPE_LABEL_GFX_PAUSE_BEFORE_END,    &tape.label.pause_before_end  },
226       { IMG_TAPE_SYMBOL_GFX_PAUSE_BEFORE_END,   &tape.symbol.pause_before_end },
227     },
228     {
229       { IMG_TAPE_LABEL_GFX_WARP_FORWARD_BLIND,  &tape.label.warp_forward_blind},
230       { IMG_TAPE_SYMBOL_GFX_WARP_FORWARD_BLIND, &tape.symbol.warp_forward_blind},
231     },
232     {
233       { IMG_TAPE_LABEL_GFX_WARP_FORWARD,        &tape.label.warp_forward  },
234       { IMG_TAPE_SYMBOL_GFX_WARP_FORWARD,       &tape.symbol.warp_forward },
235     },
236     {
237       { IMG_TAPE_LABEL_GFX_SINGLE_STEP,         &tape.label.single_step  },
238       { IMG_TAPE_SYMBOL_GFX_SINGLE_STEP,        &tape.symbol.single_step },
239     },
240   };
241
242   for (k = 0; k < NUM_TAPE_FUNCTION_STATES; k++)        /* on or off states */
243   {
244     for (i = 0; i < NUM_TAPE_FUNCTIONS; i++)            /* record, play, ... */
245     {
246       for (j = 0; j < NUM_TAPE_FUNCTION_PARTS; j++)     /* label or symbol */
247       {
248         if (video_pos[i][j].graphic == -1 ||
249             video_pos[i][j].pos->x < 0 ||
250             video_pos[i][j].pos->y < 0)
251           continue;
252
253         if (state & (1 << (i * 2 + k)))
254         {
255           struct GraphicInfo *gfx_bg = &graphic_info[IMG_BACKGROUND_TAPE];
256           struct GraphicInfo *gfx = &graphic_info[video_pos[i][j].graphic];
257           struct Rect *pos = video_pos[i][j].pos;
258           Bitmap *gd_bitmap;
259           int gd_x, gd_y;
260           int skip_value =
261             (j == 0 ? VIDEO_DISPLAY_SYMBOL_ONLY : VIDEO_DISPLAY_LABEL_ONLY);
262
263           if (value == skip_value)
264             continue;
265
266           if (k == 1)           /* on */
267           {
268             gd_bitmap = gfx->bitmap;
269             gd_x = gfx->src_x;
270             gd_y = gfx->src_y;
271           }
272           else                  /* off */
273           {
274             gd_bitmap = gfx_bg->bitmap;
275             gd_x = gfx_bg->src_x + pos->x;
276             gd_y = gfx_bg->src_y + pos->y;
277           }
278
279           /* some tape graphics may be undefined -- only draw if defined */
280           if (gd_bitmap != NULL)
281             BlitBitmap(gd_bitmap, drawto, gd_x, gd_y, gfx->width, gfx->height,
282                        VX + pos->x, VY + pos->y);
283         }
284       }
285     }
286   }
287 }
288
289
290 #define DATETIME_NONE                   (0)
291
292 #define DATETIME_DATE_YYYY              (1 << 0)
293 #define DATETIME_DATE_YY                (1 << 1)
294 #define DATETIME_DATE_MON               (1 << 2)
295 #define DATETIME_DATE_MM                (1 << 3)
296 #define DATETIME_DATE_DD                (1 << 4)
297
298 #define DATETIME_TIME_HH                (1 << 5)
299 #define DATETIME_TIME_MIN               (1 << 6)
300 #define DATETIME_TIME_MM                (1 << 7)
301 #define DATETIME_TIME_SS                (1 << 8)
302
303 #define DATETIME_XOFFSET_1              (1 << 9)
304 #define DATETIME_XOFFSET_2              (1 << 10)
305
306 #define DATETIME_DATE                   (DATETIME_DATE_YYYY     |       \
307                                          DATETIME_DATE_YY       |       \
308                                          DATETIME_DATE_MON      |       \
309                                          DATETIME_DATE_MM       |       \
310                                          DATETIME_DATE_DD)
311
312 #define DATETIME_TIME                   (DATETIME_TIME_HH       |       \
313                                          DATETIME_TIME_MIN      |       \
314                                          DATETIME_TIME_MM       |       \
315                                          DATETIME_TIME_SS)
316
317 #define MAX_DATETIME_STRING_SIZE        32
318
319 static void DrawVideoDisplay_DateTime(unsigned int state, unsigned int value)
320 {
321   int i;
322
323   static char *month_shortnames[] =
324   {
325     "JAN",
326     "FEB",
327     "MAR",
328     "APR",
329     "MAY",
330     "JUN",
331     "JUL",
332     "AUG",
333     "SEP",
334     "OCT",
335     "NOV",
336     "DEC"
337   };
338
339   static struct
340   {
341     struct TextPosInfo *pos;
342     int type;
343   }
344   datetime_info[] =
345   {
346     { &tape.text.date,          DATETIME_DATE_DD                        },
347     { &tape.text.date,          DATETIME_DATE_MON | DATETIME_XOFFSET_1  },
348     { &tape.text.date,          DATETIME_DATE_YY  | DATETIME_XOFFSET_2  },
349     { &tape.text.date_yyyy,     DATETIME_DATE_YYYY                      },
350     { &tape.text.date_yy,       DATETIME_DATE_YY                        },
351     { &tape.text.date_mon,      DATETIME_DATE_MON                       },
352     { &tape.text.date_mm,       DATETIME_DATE_MM                        },
353     { &tape.text.date_dd,       DATETIME_DATE_DD                        },
354
355     { &tape.text.time,          DATETIME_TIME_MIN                       },
356     { &tape.text.time,          DATETIME_TIME_SS  | DATETIME_XOFFSET_1  },
357     { &tape.text.time_hh,       DATETIME_TIME_HH                        },
358     { &tape.text.time_mm,       DATETIME_TIME_MM                        },
359     { &tape.text.time_ss,       DATETIME_TIME_SS                        },
360
361     { NULL,                     DATETIME_NONE                           },
362   };
363
364   for (i = 0; datetime_info[i].pos != NULL; i++)
365   {
366     struct TextPosInfo *pos = datetime_info[i].pos;
367     int type = datetime_info[i].type;
368     int xpos, ypos;
369
370     if (pos->x == -1 &&
371         pos->y == -1)
372       continue;
373
374     xpos = VX + pos->x + (type & DATETIME_XOFFSET_1 ? pos->xoffset  :
375                           type & DATETIME_XOFFSET_2 ? pos->xoffset2 : 0);
376     ypos = VY + pos->y;
377
378     if ((type & DATETIME_DATE) && (state & VIDEO_STATE_DATE_ON))
379     {
380       char s[MAX_DATETIME_STRING_SIZE];
381       int year2 = value / 10000;
382       int year4 = (year2 < 70 ? 2000 + year2 : 1900 + year2);
383       int month_index = (value / 100) % 100;
384       int month = month_index + 1;
385       int day = value % 100;
386
387       strcpy(s, (type & DATETIME_DATE_YYYY ? int2str(year4, 4) :
388                  type & DATETIME_DATE_YY   ? int2str(year2, 2) :
389                  type & DATETIME_DATE_MON  ? month_shortnames[month_index] :
390                  type & DATETIME_DATE_MM   ? int2str(month, 2) :
391                  type & DATETIME_DATE_DD   ? int2str(day, 2) : ""));
392
393       DrawText(xpos, ypos, s, pos->font);
394     }
395     else if ((type & DATETIME_TIME) && (state & VIDEO_STATE_TIME_ON))
396     {
397       char s[MAX_DATETIME_STRING_SIZE];
398       int hh = (value / 3600) % 100;
399       int min = value / 60;
400       int mm = (value / 60) % 60;
401       int ss = value % 60;
402
403       strcpy(s, (type & DATETIME_TIME_HH  ? int2str(hh, 2) :
404                  type & DATETIME_TIME_MIN ? int2str(min, 2) :
405                  type & DATETIME_TIME_MM  ? int2str(mm, 2) :
406                  type & DATETIME_TIME_SS  ? int2str(ss, 2) : ""));
407
408       DrawText(xpos, ypos, s, pos->font);
409     }
410   }
411 }
412
413 void DrawVideoDisplay(unsigned int state, unsigned int value)
414 {
415   DrawVideoDisplay_Graphics(state, value);
416   DrawVideoDisplay_DateTime(state, value);
417
418   redraw_mask |= REDRAW_DOOR_2;
419 }
420
421 void DrawCompleteVideoDisplay()
422 {
423   struct GraphicInfo *g_tape = &graphic_info[IMG_BACKGROUND_TAPE];
424
425   /* draw tape background */
426   BlitBitmap(g_tape->bitmap, drawto, g_tape->src_x, g_tape->src_y,
427              gfx.vxsize, gfx.vysize, gfx.vx, gfx.vy);
428
429   /* draw tape buttons (forced) */
430   UnmapTapeButtons();
431   MapTapeButtons();
432
433   DrawVideoDisplay(VIDEO_ALL_OFF, 0);
434
435   if (tape.recording)
436   {
437     DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
438     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
439     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
440
441     if (tape.pausing)
442       DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
443   }
444   else if (tape.playing)
445   {
446     DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
447     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
448     DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
449
450     if (tape.pausing)
451       DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, 0);
452   }
453   else if (tape.date && tape.length)
454   {
455     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
456     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
457   }
458
459   BlitBitmap(drawto, bitmap_db_door_2, gfx.vx, gfx.vy, gfx.vxsize, gfx.vysize,
460              0, 0);
461 }
462
463 void TapeDeactivateDisplayOn()
464 {
465   SetDrawDeactivationMask(REDRAW_FIELD);
466   audio.sound_deactivated = TRUE;
467 }
468
469 void TapeDeactivateDisplayOff(boolean redraw_display)
470 {
471   SetDrawDeactivationMask(REDRAW_NONE);
472   audio.sound_deactivated = FALSE;
473
474   if (redraw_display)
475   {
476     RedrawPlayfield(TRUE, 0,0,0,0);
477     DrawGameDoorValues();
478   }
479 }
480
481
482 /* ========================================================================= */
483 /* tape control functions                                                    */
484 /* ========================================================================= */
485
486 void TapeSetDateFromEpochSeconds(time_t epoch_seconds)
487 {
488   struct tm *lt = localtime(&epoch_seconds);
489
490   tape.date = 10000 * (lt->tm_year % 100) + 100 * lt->tm_mon + lt->tm_mday;
491 }
492
493 void TapeSetDateFromNow()
494 {
495   TapeSetDateFromEpochSeconds(time(NULL));
496 }
497
498 void TapeErase()
499 {
500   int i;
501
502   tape.counter = 0;
503   tape.length = 0;
504   tape.length_seconds = 0;
505
506   if (leveldir_current)
507     setString(&tape.level_identifier, leveldir_current->identifier);
508
509   tape.level_nr = level_nr;
510   tape.pos[tape.counter].delay = 0;
511   tape.changed = TRUE;
512
513   tape.random_seed = InitRND(level.random_seed);
514
515   tape.file_version = FILE_VERSION_ACTUAL;
516   tape.game_version = GAME_VERSION_ACTUAL;
517   tape.engine_version = level.game_version;
518
519   TapeSetDateFromNow();
520
521   for (i = 0; i < MAX_PLAYERS; i++)
522     tape.player_participates[i] = FALSE;
523
524   tape.centered_player_nr_next = -1;
525   tape.set_centered_player = FALSE;
526 }
527
528 static void TapeRewind()
529 {
530   tape.counter = 0;
531   tape.delay_played = 0;
532   tape.pause_before_death = FALSE;
533   tape.recording = FALSE;
534   tape.playing = FALSE;
535   tape.fast_forward = FALSE;
536   tape.warp_forward = FALSE;
537   tape.deactivate_display = FALSE;
538   tape.auto_play = (global.autoplay_leveldir != NULL);
539   tape.auto_play_level_solved = FALSE;
540   tape.quick_resume = FALSE;
541   tape.single_step = FALSE;
542
543   tape.centered_player_nr_next = -1;
544   tape.set_centered_player = FALSE;
545
546   InitRND(tape.random_seed);
547 }
548
549 static void TapeSetRandomSeed(int random_seed)
550 {
551   tape.random_seed = InitRND(random_seed);
552 }
553
554 void TapeStartRecording(int random_seed)
555 {
556   if (!TAPE_IS_STOPPED(tape))
557     TapeStop();
558
559   TapeErase();
560   TapeRewind();
561   TapeSetRandomSeed(random_seed);
562
563   tape.recording = TRUE;
564
565   DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
566   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
567   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
568
569   MapTapeWarpButton();
570
571   SetDrawDeactivationMask(REDRAW_NONE);
572   audio.sound_deactivated = FALSE;
573 }
574
575 static void TapeStartGameRecording()
576 {
577   TapeStartRecording(level.random_seed);
578
579 #if defined(NETWORK_AVALIABLE)
580   if (options.network)
581   {
582     SendToServer_StartPlaying();
583
584     return;
585   }
586 #endif
587
588   InitGame();
589 }
590
591 static void TapeAppendRecording()
592 {
593   if (!tape.playing || !tape.pausing)
594     return;
595
596   tape.pos[tape.counter].delay = tape.delay_played;
597   tape.playing = FALSE;
598   tape.recording = TRUE;
599   tape.changed = TRUE;
600
601   TapeSetDateFromNow();
602
603   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
604   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON, 0);
605
606   UpdateAndDisplayGameControlValues();
607 }
608
609 void TapeHaltRecording()
610 {
611   if (!tape.recording)
612     return;
613
614   tape.counter++;
615   tape.pos[tape.counter].delay = 0;
616
617   tape.length = tape.counter;
618   tape.length_seconds = GetTapeLength();
619 }
620
621 void TapeStopRecording()
622 {
623   TapeHaltRecording();
624
625   tape.recording = FALSE;
626   tape.pausing = FALSE;
627
628   DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
629   MapTapeEjectButton();
630 }
631
632 void TapeRecordAction(byte action_raw[MAX_PLAYERS])
633 {
634   byte action[MAX_PLAYERS];
635   int i;
636
637   if (!tape.recording)          /* (record action even when tape is paused) */
638     return;
639
640   if (tape.counter >= MAX_TAPE_LEN - 1)
641   {
642     TapeStopRecording();
643     return;
644   }
645
646   for (i = 0; i < MAX_PLAYERS; i++)
647     action[i] = action_raw[i];
648
649   if (tape.set_centered_player)
650   {
651     for (i = 0; i < MAX_PLAYERS; i++)
652       if (tape.centered_player_nr_next == i ||
653           tape.centered_player_nr_next == -1)
654         action[i] |= KEY_SET_FOCUS;
655
656     tape.set_centered_player = FALSE;
657   }
658
659   if (tape.pos[tape.counter].delay > 0)         /* already stored action */
660   {
661     boolean changed_events = FALSE;
662
663     for (i = 0; i < MAX_PLAYERS; i++)
664       if (tape.pos[tape.counter].action[i] != action[i])
665         changed_events = TRUE;
666
667     if (changed_events || tape.pos[tape.counter].delay >= 255)
668     {
669       tape.counter++;
670       tape.pos[tape.counter].delay = 0;
671     }
672     else
673       tape.pos[tape.counter].delay++;
674   }
675
676   if (tape.pos[tape.counter].delay == 0)        /* store new action */
677   {
678     for (i = 0; i < MAX_PLAYERS; i++)
679       tape.pos[tape.counter].action[i] = action[i];
680
681     tape.pos[tape.counter].delay++;
682   }
683 }
684
685 void TapeTogglePause(boolean toggle_manual)
686 {
687   int state = 0;
688
689   if (tape.pause_before_death)
690     state |= VIDEO_STATE_PBEND_OFF;
691   else if (tape.fast_forward)
692     state |= VIDEO_STATE_FFWD_OFF;
693
694   tape.pausing = !tape.pausing;
695   tape.fast_forward = FALSE;
696   tape.pause_before_death = FALSE;
697
698   if (tape.single_step && toggle_manual)
699     tape.single_step = FALSE;
700
701   state |= (tape.pausing ? VIDEO_STATE_PAUSE_ON : VIDEO_STATE_PAUSE_OFF);
702   if (tape.playing)
703     state |= VIDEO_STATE_PLAY_ON;
704   else
705     state |= (tape.single_step ? VIDEO_STATE_1STEP_ON : VIDEO_STATE_1STEP_OFF);
706
707   DrawVideoDisplay(state, 0);
708
709   if (tape.warp_forward)
710   {
711     TapeStopWarpForward();
712
713     if (tape.quick_resume)
714     {
715       tape.quick_resume = FALSE;
716
717       TapeAppendRecording();
718
719       if (!CheckEngineSnapshot())
720         SaveEngineSnapshot();
721     }
722   }
723 }
724
725 void TapeStartPlaying()
726 {
727   if (TAPE_IS_EMPTY(tape))
728     return;
729
730   if (!TAPE_IS_STOPPED(tape))
731     TapeStop();
732
733   TapeRewind();
734
735   tape.playing = TRUE;
736
737   DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
738   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
739   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
740
741   MapTapeWarpButton();
742
743   SetDrawDeactivationMask(REDRAW_NONE);
744   audio.sound_deactivated = FALSE;
745 }
746
747 static void TapeStartGamePlaying()
748 {
749   TapeStartPlaying();
750
751   InitGame();
752 }
753
754 void TapeStopPlaying()
755 {
756   tape.playing = FALSE;
757   tape.pausing = FALSE;
758
759   if (tape.warp_forward)
760     TapeStopWarpForward();
761
762   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
763   MapTapeEjectButton();
764 }
765
766 byte *TapePlayAction()
767 {
768   static byte action[MAX_PLAYERS];
769   int i;
770
771   if (!tape.playing || tape.pausing)
772     return NULL;
773
774   if (tape.pause_before_death)  /* stop 10 seconds before player gets killed */
775   {
776     if (!(FrameCounter % 20))
777     {
778       if ((FrameCounter / 20) % 2)
779         DrawVideoDisplay(VIDEO_STATE_PBEND_ON, VIDEO_DISPLAY_LABEL_ONLY);
780       else
781         DrawVideoDisplay(VIDEO_STATE_PBEND_OFF, VIDEO_DISPLAY_LABEL_ONLY);
782     }
783
784     if (tape.warp_forward)
785     {
786       if (tape.deactivate_display)
787         DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
788       else
789         DrawVideoDisplay(VIDEO_STATE_WARP2_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
790     }
791
792     if (TapeTime > tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
793     {
794       TapeTogglePause(TAPE_TOGGLE_MANUAL);
795
796       return NULL;
797     }
798   }
799   else if (tape.fast_forward)
800   {
801     if ((FrameCounter / 20) % 2)
802       DrawVideoDisplay(VIDEO_STATE_FFWD_ON, VIDEO_DISPLAY_LABEL_ONLY);
803     else
804       DrawVideoDisplay(VIDEO_STATE_FFWD_OFF, VIDEO_DISPLAY_LABEL_ONLY);
805
806     if (tape.warp_forward)
807     {
808       if (tape.deactivate_display)
809         DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
810       else
811         DrawVideoDisplay(VIDEO_STATE_WARP2_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
812     }
813   }
814
815 #if 0
816   /* !!! this makes things much slower !!! */
817   else if (tape.warp_forward)
818   {
819     if ((FrameCounter / 20) % 2)
820       DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_LABEL_ONLY);
821     else
822       DrawVideoDisplay(VIDEO_STATE_WARP_OFF, VIDEO_DISPLAY_LABEL_ONLY);
823
824     DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
825   }
826 #endif
827
828   if (tape.counter >= tape.length)      /* end of tape reached */
829   {
830     if (tape.warp_forward && !tape.auto_play)
831       TapeTogglePause(TAPE_TOGGLE_MANUAL);
832     else
833       TapeStop();
834
835     return NULL;
836   }
837
838   for (i = 0; i < MAX_PLAYERS; i++)
839     action[i] = tape.pos[tape.counter].action[i];
840
841 #if DEBUG_TAPE_WHEN_PLAYING
842   printf("%05d", FrameCounter);
843   for (i = 0; i < MAX_PLAYERS; i++)
844     printf("   %08x", action[i]);
845   printf("\n");
846 #endif
847
848   tape.set_centered_player = FALSE;
849   tape.centered_player_nr_next = -999;
850
851   for (i = 0; i < MAX_PLAYERS; i++)
852   {
853     if (action[i] & KEY_SET_FOCUS)
854     {
855       tape.set_centered_player = TRUE;
856       tape.centered_player_nr_next =
857         (tape.centered_player_nr_next == -999 ? i : -1);
858     }
859
860     action[i] &= ~KEY_SET_FOCUS;
861   }
862
863   tape.delay_played++;
864   if (tape.delay_played >= tape.pos[tape.counter].delay)
865   {
866     tape.counter++;
867     tape.delay_played = 0;
868   }
869
870   return action;
871 }
872
873 void TapeStop()
874 {
875   TapeStopRecording();
876   TapeStopPlaying();
877
878   DrawVideoDisplay(VIDEO_STATE_OFF, 0);
879
880   if (tape.date && tape.length)
881   {
882     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
883     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
884   }
885 }
886
887 unsigned int GetTapeLength()
888 {
889   unsigned int tape_length = 0;
890   int i;
891
892   if (TAPE_IS_EMPTY(tape))
893     return(0);
894
895   for (i = 0; i < tape.length; i++)
896     tape_length += tape.pos[i].delay;
897
898   return(tape_length * GAME_FRAME_DELAY / 1000);
899 }
900
901 static void TapeStartWarpForward()
902 {
903   tape.warp_forward = TRUE;
904
905   if (!tape.fast_forward && !tape.pause_before_death)
906   {
907     tape.pausing = FALSE;
908     tape.deactivate_display = TRUE;
909
910     TapeDeactivateDisplayOn();
911   }
912
913   if (tape.fast_forward || tape.pause_before_death)
914     DrawVideoDisplay(VIDEO_STATE_WARP_ON, VIDEO_DISPLAY_SYMBOL_ONLY);
915   else
916     DrawVideoDisplay(VIDEO_STATE_WARP_ON, 0);
917 }
918
919 static void TapeStopWarpForward()
920 {
921   int state = (tape.pausing ? VIDEO_STATE_PAUSE_ON : VIDEO_STATE_PAUSE_OFF);
922
923   tape.warp_forward = FALSE;
924   tape.deactivate_display = FALSE;
925
926   TapeDeactivateDisplayOff(game_status == GAME_MODE_PLAYING);
927
928   if (tape.pause_before_death)
929     state |= VIDEO_STATE_WARP_OFF | VIDEO_STATE_PBEND_ON;
930   else if (tape.fast_forward)
931     state |= VIDEO_STATE_WARP_OFF | VIDEO_STATE_FFWD_ON;
932   else
933     state |= VIDEO_STATE_WARP_OFF | VIDEO_STATE_PLAY_ON;
934
935   DrawVideoDisplay(state, 0);
936 }
937
938 static void TapeSingleStep()
939 {
940   if (options.network)
941     return;
942
943   if (!tape.pausing)
944     TapeTogglePause(TAPE_TOGGLE_MANUAL);
945
946   tape.single_step = !tape.single_step;
947
948   DrawVideoDisplay((tape.single_step ? VIDEO_STATE_1STEP_ON :
949                     VIDEO_STATE_1STEP_OFF), 0);
950 }
951
952 void TapeQuickSave()
953 {
954   if (game_status == GAME_MODE_MAIN)
955   {
956     Request("No game that can be saved!", REQ_CONFIRM);
957
958     return;
959   }
960
961   if (game_status != GAME_MODE_PLAYING)
962     return;
963
964   if (tape.recording)
965     TapeHaltRecording();        /* prepare tape for saving on-the-fly */
966
967   if (TAPE_IS_EMPTY(tape))
968   {
969     Request("No tape that can be saved!", REQ_CONFIRM);
970
971     return;
972   }
973
974   if (SaveTapeChecked(tape.level_nr))
975     SaveEngineSnapshot();
976 }
977
978 void TapeQuickLoad()
979 {
980   char *filename = getTapeFilename(level_nr);
981
982   if (!fileExists(filename))
983   {
984     Request("No tape for this level!", REQ_CONFIRM);
985
986     return;
987   }
988
989   if (tape.recording && !Request("Stop recording and load tape?",
990                                  REQ_ASK | REQ_STAY_CLOSED))
991   {
992     OpenDoor(DOOR_OPEN_1 | DOOR_COPY_BACK);
993
994     return;
995   }
996
997   if (game_status != GAME_MODE_PLAYING && game_status != GAME_MODE_MAIN)
998     return;
999
1000   if (CheckEngineSnapshot())
1001   {
1002     TapeStartGamePlaying();
1003
1004     LoadEngineSnapshot();
1005
1006     DrawCompleteVideoDisplay();
1007
1008     tape.playing = TRUE;
1009     tape.pausing = TRUE;
1010
1011     TapeStopWarpForward();
1012     TapeAppendRecording();
1013
1014     if (FrameCounter > 0)
1015       return;
1016   }
1017
1018   TapeStop();
1019   TapeErase();
1020
1021   LoadTape(level_nr);
1022
1023   if (!TAPE_IS_EMPTY(tape))
1024   {
1025     TapeStartGamePlaying();
1026     TapeStartWarpForward();
1027
1028     tape.quick_resume = TRUE;
1029   }
1030   else  /* this should not happen (basically checked above) */
1031   {
1032     int reopen_door = (game_status == GAME_MODE_PLAYING ? REQ_REOPEN : 0);
1033
1034     Request("No tape for this level!", REQ_CONFIRM | reopen_door);
1035   }
1036 }
1037
1038 void InsertSolutionTape()
1039 {
1040   if (!TAPE_IS_EMPTY(tape))
1041     return;
1042
1043   LoadSolutionTape(level_nr);
1044
1045   if (TAPE_IS_EMPTY(tape))
1046     Request("No solution tape for this level!", REQ_CONFIRM);
1047
1048   DrawCompleteVideoDisplay();
1049 }
1050
1051
1052 /* ------------------------------------------------------------------------- *
1053  * tape autoplay functions
1054  * ------------------------------------------------------------------------- */
1055
1056 void AutoPlayTape()
1057 {
1058   static LevelDirTree *autoplay_leveldir = NULL;
1059   static boolean autoplay_initialized = FALSE;
1060   static int autoplay_level_nr = -1;
1061   static int num_levels_played = 0;
1062   static int num_levels_solved = 0;
1063   static int num_tape_missing = 0;
1064   static boolean level_failed[MAX_TAPES_PER_SET];
1065   int i;
1066
1067   if (autoplay_initialized)
1068   {
1069     /* just finished auto-playing tape */
1070     printf("%s.\n", tape.auto_play_level_solved ? "solved" : "NOT SOLVED");
1071
1072     num_levels_played++;
1073
1074     if (tape.auto_play_level_solved)
1075       num_levels_solved++;
1076     else if (level_nr >= 0 && level_nr < MAX_TAPES_PER_SET)
1077       level_failed[level_nr] = TRUE;
1078   }
1079   else
1080   {
1081     DrawCompleteVideoDisplay();
1082     audio.sound_enabled = FALSE;
1083
1084     autoplay_leveldir = getTreeInfoFromIdentifier(leveldir_first,
1085                                                   global.autoplay_leveldir);
1086
1087     if (autoplay_leveldir == NULL)
1088       Error(ERR_EXIT, "no such level identifier: '%s'",
1089             global.autoplay_leveldir);
1090
1091     leveldir_current = autoplay_leveldir;
1092
1093     if (autoplay_leveldir->first_level < 0)
1094       autoplay_leveldir->first_level = 0;
1095     if (autoplay_leveldir->last_level >= MAX_TAPES_PER_SET)
1096       autoplay_leveldir->last_level = MAX_TAPES_PER_SET - 1;
1097
1098     autoplay_level_nr = autoplay_leveldir->first_level;
1099
1100     printf_line("=", 79);
1101     printf("Automatically playing level tapes\n");
1102     printf_line("-", 79);
1103     printf("Level series identifier: '%s'\n", autoplay_leveldir->identifier);
1104     printf("Level series name:       '%s'\n", autoplay_leveldir->name);
1105     printf("Level series author:     '%s'\n", autoplay_leveldir->author);
1106     printf("Number of levels:        %d\n",   autoplay_leveldir->levels);
1107     printf_line("=", 79);
1108     printf("\n");
1109
1110     for (i = 0; i < MAX_TAPES_PER_SET; i++)
1111       level_failed[i] = FALSE;
1112
1113     autoplay_initialized = TRUE;
1114   }
1115
1116   while (autoplay_level_nr <= autoplay_leveldir->last_level)
1117   {
1118     level_nr = autoplay_level_nr++;
1119
1120     if (!global.autoplay_all && !global.autoplay_level[level_nr])
1121       continue;
1122
1123     TapeErase();
1124
1125     printf("Level %03d: ", level_nr);
1126
1127     LoadLevel(level_nr);
1128     if (level.no_valid_file)
1129     {
1130       printf("(no level)\n");
1131       continue;
1132     }
1133
1134 #if 0
1135     /* ACTIVATE THIS FOR LOADING/TESTING OF LEVELS ONLY */
1136     printf("(only testing level)\n");
1137     continue;
1138 #endif
1139
1140     LoadSolutionTape(level_nr);
1141
1142     if (tape.no_valid_file)
1143     {
1144       num_tape_missing++;
1145
1146       printf("(no tape)\n");
1147
1148       continue;
1149     }
1150
1151     printf("playing tape ... ");
1152
1153     TapeStartGamePlaying();
1154     TapeStartWarpForward();
1155
1156     return;
1157   }
1158
1159   printf("\n");
1160   printf_line("=", 79);
1161   printf("Number of levels played: %d\n", num_levels_played);
1162   printf("Number of levels solved: %d (%d%%)\n", num_levels_solved,
1163          (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
1164   printf_line("-", 79);
1165   printf("Summary (for automatic parsing by scripts):\n");
1166   printf("LEVELDIR '%s', SOLVED %d/%d (%d%%)",
1167          autoplay_leveldir->identifier, num_levels_solved, num_levels_played,
1168          (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
1169
1170   if (num_levels_played != num_levels_solved)
1171   {
1172     printf(", FAILED:");
1173     for (i = 0; i < MAX_TAPES_PER_SET; i++)
1174       if (level_failed[i])
1175         printf(" %03d", i);
1176   }
1177
1178   printf("\n");
1179   printf_line("=", 79);
1180
1181   CloseAllAndExit(0);
1182 }
1183
1184
1185 /* ---------- new tape button stuff ---------------------------------------- */
1186
1187 static struct
1188 {
1189   int graphic;
1190   struct Rect *pos;
1191   int gadget_id;
1192   char *infotext;
1193 } tapebutton_info[NUM_TAPE_BUTTONS] =
1194 {
1195   {
1196     IMG_TAPE_BUTTON_GFX_EJECT,          &tape.button.eject,
1197     TAPE_CTRL_ID_EJECT,                 "eject tape"
1198   },
1199   {
1200     /* (same position as "eject" button) */
1201     IMG_TAPE_BUTTON_GFX_EXTRA,          &tape.button.eject,
1202     TAPE_CTRL_ID_EXTRA,                 "extra functions"
1203   },
1204   {
1205     IMG_TAPE_BUTTON_GFX_STOP,           &tape.button.stop,
1206     TAPE_CTRL_ID_STOP,                  "stop tape"
1207   },
1208   {
1209     IMG_TAPE_BUTTON_GFX_PAUSE,          &tape.button.pause,
1210     TAPE_CTRL_ID_PAUSE,                 "pause tape"
1211   },
1212   {
1213     IMG_TAPE_BUTTON_GFX_RECORD,         &tape.button.record,
1214     TAPE_CTRL_ID_RECORD,                "record tape"
1215   },
1216   {
1217     IMG_TAPE_BUTTON_GFX_PLAY,           &tape.button.play,
1218     TAPE_CTRL_ID_PLAY,                  "play tape"
1219   }
1220 };
1221
1222 void CreateTapeButtons()
1223 {
1224   int i;
1225
1226   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1227   {
1228     struct GraphicInfo *gfx = &graphic_info[tapebutton_info[i].graphic];
1229     struct Rect *pos = tapebutton_info[i].pos;
1230     struct GadgetInfo *gi;
1231     int gd_x = gfx->src_x;
1232     int gd_y = gfx->src_y;
1233     int gd_xp = gfx->src_x + gfx->pressed_xoffset;
1234     int gd_yp = gfx->src_y + gfx->pressed_yoffset;
1235     int id = i;
1236
1237     gi = CreateGadget(GDI_CUSTOM_ID, id,
1238                       GDI_INFO_TEXT, tapebutton_info[i].infotext,
1239                       GDI_X, VX + pos->x,
1240                       GDI_Y, VY + pos->y,
1241                       GDI_WIDTH, gfx->width,
1242                       GDI_HEIGHT, gfx->height,
1243                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
1244                       GDI_STATE, GD_BUTTON_UNPRESSED,
1245                       GDI_DESIGN_UNPRESSED, gfx->bitmap, gd_x, gd_y,
1246                       GDI_DESIGN_PRESSED, gfx->bitmap, gd_xp, gd_yp,
1247                       GDI_DIRECT_DRAW, FALSE,
1248                       GDI_EVENT_MASK, GD_EVENT_RELEASED,
1249                       GDI_CALLBACK_ACTION, HandleTapeButtons,
1250                       GDI_END);
1251
1252     if (gi == NULL)
1253       Error(ERR_EXIT, "cannot create gadget");
1254
1255     tape_gadget[id] = gi;
1256   }
1257 }
1258
1259 void FreeTapeButtons()
1260 {
1261   int i;
1262
1263   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1264     FreeGadget(tape_gadget[i]);
1265 }
1266
1267 void MapTapeEjectButton()
1268 {
1269   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1270   MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1271 }
1272
1273 void MapTapeWarpButton()
1274 {
1275   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
1276   MapGadget(tape_gadget[TAPE_CTRL_ID_EXTRA]);
1277 }
1278
1279 void MapTapeButtons()
1280 {
1281   int i;
1282
1283   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1284     if (i != TAPE_CTRL_ID_EXTRA)
1285       MapGadget(tape_gadget[i]);
1286
1287   if (tape.recording || tape.playing)
1288     MapTapeWarpButton();
1289
1290   if (tape.show_game_buttons)
1291     MapGameButtons();
1292 }
1293
1294 void UnmapTapeButtons()
1295 {
1296   int i;
1297
1298   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
1299     UnmapGadget(tape_gadget[i]);
1300
1301   if (tape.show_game_buttons)
1302     UnmapGameButtons();
1303 }
1304
1305 static void HandleTapeButtonsExt(int id)
1306 {
1307   if (game_status != GAME_MODE_MAIN && game_status != GAME_MODE_PLAYING)
1308     return;
1309
1310   switch (id)
1311   {
1312     case TAPE_CTRL_ID_EJECT:
1313       TapeStop();
1314
1315       if (TAPE_IS_EMPTY(tape))
1316       {
1317         LoadTape(level_nr);
1318
1319         if (TAPE_IS_EMPTY(tape))
1320           Request("No tape for this level!", REQ_CONFIRM);
1321       }
1322       else
1323       {
1324         if (tape.changed)
1325           SaveTapeChecked(tape.level_nr);
1326
1327         TapeErase();
1328       }
1329
1330       DrawCompleteVideoDisplay();
1331       break;
1332
1333     case TAPE_CTRL_ID_EXTRA:
1334       if (tape.playing)
1335       {
1336         if (!tape.warp_forward)                 /* PLAY -> WARP FORWARD PLAY */
1337         {
1338           TapeStartWarpForward();
1339         }
1340         else                                    /* WARP FORWARD PLAY -> PLAY */
1341         {
1342           TapeStopWarpForward();
1343         }
1344       }
1345       else if (tape.recording)
1346         TapeSingleStep();
1347
1348       break;
1349
1350     case TAPE_CTRL_ID_STOP:
1351       TapeStop();
1352       break;
1353
1354     case TAPE_CTRL_ID_PAUSE:
1355       TapeTogglePause(TAPE_TOGGLE_MANUAL);
1356       break;
1357
1358     case TAPE_CTRL_ID_RECORD:
1359       if (TAPE_IS_STOPPED(tape))
1360         TapeStartGameRecording();
1361       else if (tape.pausing)
1362       {
1363         if (tape.playing)                       /* PLAY -> PAUSE -> RECORD */
1364           TapeAppendRecording();
1365         else
1366           TapeTogglePause(TAPE_TOGGLE_MANUAL);
1367       }
1368       break;
1369
1370     case TAPE_CTRL_ID_PLAY:
1371       if (TAPE_IS_EMPTY(tape))
1372         break;
1373
1374       if (TAPE_IS_STOPPED(tape))
1375       {
1376         TapeStartGamePlaying();
1377       }
1378       else if (tape.playing)
1379       {
1380         if (tape.pausing)                       /* PAUSE -> PLAY */
1381         {
1382           TapeTogglePause(TAPE_TOGGLE_MANUAL);
1383         }
1384         else if (!tape.fast_forward)            /* PLAY -> FAST FORWARD PLAY */
1385         {
1386           tape.fast_forward = TRUE;
1387           DrawVideoDisplay(VIDEO_STATE_FFWD_ON, 0);
1388         }
1389         else if (!tape.pause_before_death)      /* FFWD PLAY -> AUTO PAUSE */
1390         {
1391           tape.pause_before_death = TRUE;
1392           DrawVideoDisplay(VIDEO_STATE_FFWD_OFF | VIDEO_STATE_PBEND_ON, 0);
1393         }
1394         else                                    /* AUTO PAUSE -> NORMAL PLAY */
1395         {
1396           if (tape.warp_forward)
1397             TapeStopWarpForward();
1398
1399           tape.fast_forward = FALSE;
1400           tape.pause_before_death = FALSE;
1401
1402           DrawVideoDisplay(VIDEO_STATE_PBEND_OFF | VIDEO_STATE_PLAY_ON, 0);
1403         }
1404       }
1405       break;
1406
1407     default:
1408       break;
1409   }
1410 }
1411
1412 static void HandleTapeButtons(struct GadgetInfo *gi)
1413 {
1414   HandleTapeButtonsExt(gi->custom_id);
1415 }
1416
1417 void HandleTapeButtonKeys(Key key)
1418 {
1419   boolean eject_button_is_active = TAPE_IS_STOPPED(tape);
1420   boolean extra_button_is_active = !eject_button_is_active;
1421
1422   if (key == setup.shortcut.tape_eject && eject_button_is_active)
1423     HandleTapeButtonsExt(TAPE_CTRL_ID_EJECT);
1424   else if (key == setup.shortcut.tape_extra && extra_button_is_active)
1425     HandleTapeButtonsExt(TAPE_CTRL_ID_EXTRA);
1426   else if (key == setup.shortcut.tape_stop)
1427     HandleTapeButtonsExt(TAPE_CTRL_ID_STOP);
1428   else if (key == setup.shortcut.tape_pause)
1429     HandleTapeButtonsExt(TAPE_CTRL_ID_PAUSE);
1430   else if (key == setup.shortcut.tape_record)
1431     HandleTapeButtonsExt(TAPE_CTRL_ID_RECORD);
1432   else if (key == setup.shortcut.tape_play)
1433     HandleTapeButtonsExt(TAPE_CTRL_ID_PLAY);
1434 }