rnd-20031208-1-src
[rocksndiamonds.git] / src / tape.c
1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back!               *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 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 "init.h"
18 #include "game.h"
19 #include "tools.h"
20 #include "files.h"
21 #include "network.h"
22 #include "cartoons.h"
23
24 /* tape button identifiers */
25 #define TAPE_CTRL_ID_EJECT              0
26 #define TAPE_CTRL_ID_INDEX              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 TapeStopIndexSearch();
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 /* special */
87 #define VIDEO_PBEND_LABEL_XPOS  5
88 #define VIDEO_PBEND_LABEL_YPOS  220
89 #define VIDEO_PBEND_LABEL_XSIZE 35
90 #define VIDEO_PBEND_LABEL_YSIZE 30
91
92 #define VIDEO_STATE_OFF         (VIDEO_STATE_PLAY_OFF   |       \
93                                  VIDEO_STATE_REC_OFF    |       \
94                                  VIDEO_STATE_PAUSE_OFF  |       \
95                                  VIDEO_STATE_FFWD_OFF   |       \
96                                  VIDEO_STATE_PBEND_OFF  |       \
97                                  VIDEO_STATE_DATE_OFF   |       \
98                                  VIDEO_STATE_TIME_OFF)
99 #define VIDEO_PRESS_OFF         (VIDEO_PRESS_PLAY_OFF   |       \
100                                  VIDEO_PRESS_REC_OFF    |       \
101                                  VIDEO_PRESS_PAUSE_OFF  |       \
102                                  VIDEO_PRESS_STOP_OFF   |       \
103                                  VIDEO_PRESS_EJECT_OFF)
104 #define VIDEO_ALL_OFF           (VIDEO_STATE_OFF | VIDEO_PRESS_OFF)
105
106 #define VIDEO_STATE_ON          (VIDEO_STATE_PLAY_ON    |       \
107                                  VIDEO_STATE_REC_ON     |       \
108                                  VIDEO_STATE_PAUSE_ON   |       \
109                                  VIDEO_STATE_FFWD_ON    |       \
110                                  VIDEO_STATE_PBEND_ON   |       \
111                                  VIDEO_STATE_DATE_ON    |       \
112                                  VIDEO_STATE_TIME_ON)
113 #define VIDEO_PRESS_ON          (VIDEO_PRESS_PLAY_ON    |       \
114                                  VIDEO_PRESS_REC_ON     |       \
115                                  VIDEO_PRESS_PAUSE_ON   |       \
116                                  VIDEO_PRESS_STOP_ON    |       \
117                                  VIDEO_PRESS_EJECT_ON)
118 #define VIDEO_ALL_ON            (VIDEO_STATE_ON | VIDEO_PRESS_ON)
119
120 #define VIDEO_STATE             (VIDEO_STATE_ON | VIDEO_STATE_OFF)
121 #define VIDEO_PRESS             (VIDEO_PRESS_ON | VIDEO_PRESS_OFF)
122 #define VIDEO_ALL               (VIDEO_ALL_ON | VIDEO_ALL_OFF)
123
124
125 /* ========================================================================= */
126 /* video display functions                                                   */
127 /* ========================================================================= */
128
129 void DrawVideoDisplay(unsigned long state, unsigned long value)
130 {
131   int i;
132   int part_label = 0, part_symbol = 1;
133   int xpos = 0, ypos = 1, xsize = 2, ysize = 3;
134   static char *monatsname[12] =
135   {
136     "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
137     "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
138   };
139   static int video_pos[5][2][4] =
140   {
141     {{ VIDEO_PLAY_LABEL_XPOS, VIDEO_PLAY_LABEL_YPOS,
142        VIDEO_PLAY_LABEL_XSIZE,VIDEO_PLAY_LABEL_YSIZE },
143      { VIDEO_PLAY_SYMBOL_XPOS, VIDEO_PLAY_SYMBOL_YPOS,
144        VIDEO_PLAY_SYMBOL_XSIZE,VIDEO_PLAY_SYMBOL_YSIZE }},
145
146     {{ VIDEO_REC_LABEL_XPOS, VIDEO_REC_LABEL_YPOS,
147        VIDEO_REC_LABEL_XSIZE,VIDEO_REC_LABEL_YSIZE },
148      { VIDEO_REC_SYMBOL_XPOS, VIDEO_REC_SYMBOL_YPOS,
149        VIDEO_REC_SYMBOL_XSIZE,VIDEO_REC_SYMBOL_YSIZE }},
150
151     {{ VIDEO_PAUSE_LABEL_XPOS, VIDEO_PAUSE_LABEL_YPOS,
152        VIDEO_PAUSE_LABEL_XSIZE,VIDEO_PAUSE_LABEL_YSIZE },
153      { VIDEO_PAUSE_SYMBOL_XPOS, VIDEO_PAUSE_SYMBOL_YPOS,
154        VIDEO_PAUSE_SYMBOL_XSIZE,VIDEO_PAUSE_SYMBOL_YSIZE }},
155
156     {{ VIDEO_DATE_LABEL_XPOS, VIDEO_DATE_LABEL_YPOS,
157        VIDEO_DATE_LABEL_XSIZE,VIDEO_DATE_LABEL_YSIZE },
158      { VIDEO_DATE_XPOS, VIDEO_DATE_YPOS,
159        VIDEO_DATE_XSIZE,VIDEO_DATE_YSIZE }},
160
161     {{ 0,0,
162        0,0 },
163      { VIDEO_TIME_XPOS, VIDEO_TIME_YPOS,
164        VIDEO_TIME_XSIZE,VIDEO_TIME_YSIZE }}
165   };
166
167   if (state & VIDEO_STATE_PBEND_OFF)
168   {
169     int cx = DOOR_GFX_PAGEX3, cy = DOOR_GFX_PAGEY2;
170
171     BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
172                cx + VIDEO_REC_LABEL_XPOS,
173                cy + VIDEO_REC_LABEL_YPOS,
174                VIDEO_PBEND_LABEL_XSIZE,
175                VIDEO_PBEND_LABEL_YSIZE,
176                VX + VIDEO_REC_LABEL_XPOS,
177                VY + VIDEO_REC_LABEL_YPOS);
178   }
179
180   for (i = 0; i < 10; i++)
181   {
182     if (state & (1<<i))
183     {
184       int pos = i/2, cx, cy = DOOR_GFX_PAGEY2;
185
186       if (i%2)                  /* i ungerade => STATE_ON / PRESS_OFF */
187         cx = DOOR_GFX_PAGEX4;
188       else
189         cx = DOOR_GFX_PAGEX3;   /* i gerade => STATE_OFF / PRESS_ON */
190
191       if (video_pos[pos][part_label][0] && value != VIDEO_DISPLAY_SYMBOL_ONLY)
192         BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
193                    cx + video_pos[pos][part_label][xpos],
194                    cy + video_pos[pos][part_label][ypos],
195                    video_pos[pos][part_label][xsize],
196                    video_pos[pos][part_label][ysize],
197                    VX + video_pos[pos][part_label][xpos],
198                    VY + video_pos[pos][part_label][ypos]);
199       if (video_pos[pos][part_symbol][0] && value != VIDEO_DISPLAY_LABEL_ONLY)
200         BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
201                    cx + video_pos[pos][part_symbol][xpos],
202                    cy + video_pos[pos][part_symbol][ypos],
203                    video_pos[pos][part_symbol][xsize],
204                    video_pos[pos][part_symbol][ysize],
205                    VX + video_pos[pos][part_symbol][xpos],
206                    VY + video_pos[pos][part_symbol][ypos]);
207     }
208   }
209
210   if (state & VIDEO_STATE_FFWD_ON)
211   {
212     int cx = DOOR_GFX_PAGEX4, cy = DOOR_GFX_PAGEY2;
213
214     BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
215                cx + VIDEO_PLAY_SYMBOL_XPOS,
216                cy + VIDEO_PLAY_SYMBOL_YPOS,
217                VIDEO_PLAY_SYMBOL_XSIZE - 2,
218                VIDEO_PLAY_SYMBOL_YSIZE,
219                VX + VIDEO_PLAY_SYMBOL_XPOS - 9,
220                VY + VIDEO_PLAY_SYMBOL_YPOS);
221   }
222
223   if (state & VIDEO_STATE_PBEND_ON)
224   {
225     int cx = DOOR_GFX_PAGEX6, cy = DOOR_GFX_PAGEY1;
226
227     BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
228                cx + VIDEO_PBEND_LABEL_XPOS,
229                cy + VIDEO_PBEND_LABEL_YPOS,
230                VIDEO_PBEND_LABEL_XSIZE,
231                VIDEO_PBEND_LABEL_YSIZE,
232                VX + VIDEO_REC_LABEL_XPOS,
233                VY + VIDEO_REC_LABEL_YPOS);
234   }
235
236   if (state & VIDEO_STATE_DATE_ON)
237   {
238     int tag = value % 100;
239     int monat = (value/100) % 100;
240     int jahr = (value/10000);
241
242     DrawText(VX + VIDEO_DATE_XPOS, VY + VIDEO_DATE_YPOS,
243              int2str(tag, 2), FONT_TAPE_RECORDER);
244     DrawText(VX + VIDEO_DATE_XPOS + 27, VY + VIDEO_DATE_YPOS,
245              monatsname[monat], FONT_TAPE_RECORDER);
246     DrawText(VX + VIDEO_DATE_XPOS + 64, VY + VIDEO_DATE_YPOS,
247              int2str(jahr, 2), FONT_TAPE_RECORDER);
248   }
249
250   if (state & VIDEO_STATE_TIME_ON)
251   {
252     int min = value / 60;
253     int sec = value % 60;
254
255     DrawText(VX + VIDEO_TIME_XPOS, VY + VIDEO_TIME_YPOS,
256              int2str(min, 2), FONT_TAPE_RECORDER);
257     DrawText(VX + VIDEO_TIME_XPOS + 27, VY + VIDEO_TIME_YPOS,
258              int2str(sec, 2), FONT_TAPE_RECORDER);
259   }
260
261   if (state & VIDEO_STATE_DATE)
262     redraw_mask |= REDRAW_VIDEO_1;
263   if ((state & ~VIDEO_STATE_DATE) & VIDEO_STATE)
264     redraw_mask |= REDRAW_VIDEO_2;
265   if (state & VIDEO_PRESS)
266     redraw_mask |= REDRAW_VIDEO_3;
267 }
268
269 void DrawCompleteVideoDisplay()
270 {
271   BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
272              DOOR_GFX_PAGEX3, DOOR_GFX_PAGEY2,
273              gfx.vxsize, gfx.vysize, gfx.vx, gfx.vy);
274   BlitBitmap(graphic_info[IMG_GLOBAL_DOOR].bitmap, drawto,
275              DOOR_GFX_PAGEX4 + VIDEO_CONTROL_XPOS,
276              DOOR_GFX_PAGEY2 + VIDEO_CONTROL_YPOS,
277              VIDEO_CONTROL_XSIZE, VIDEO_CONTROL_YSIZE,
278              gfx.vx + VIDEO_CONTROL_XPOS, gfx.vy + VIDEO_CONTROL_YPOS);
279
280   DrawVideoDisplay(VIDEO_ALL_OFF, 0);
281   if (tape.date && tape.length)
282   {
283     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
284     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
285   }
286
287   BlitBitmap(drawto, bitmap_db_door, gfx.vx, gfx.vy, gfx.vxsize, gfx.vysize,
288              DOOR_GFX_PAGEX1, DOOR_GFX_PAGEY2);
289 }
290
291
292 /* ========================================================================= */
293 /* tape control functions                                                    */
294 /* ========================================================================= */
295
296 void TapeErase()
297 {
298   time_t epoch_seconds = time(NULL);
299   struct tm *time = localtime(&epoch_seconds);
300   int i;
301
302   tape.length = 0;
303   tape.counter = 0;
304
305   if (leveldir_current)
306     setString(&tape.level_identifier, leveldir_current->identifier);
307
308   tape.level_nr = level_nr;
309   tape.pos[tape.counter].delay = 0;
310   tape.changed = TRUE;
311
312   tape.date = 10000*(time->tm_year % 100) + 100*time->tm_mon + time->tm_mday;
313   tape.random_seed = InitRND(NEW_RANDOMIZE);
314
315   tape.file_version = FILE_VERSION_ACTUAL;
316   tape.game_version = GAME_VERSION_ACTUAL;
317   tape.engine_version = level.game_version;
318
319 #if 0
320   printf("::: tape.engine_version = level.game_version = %d \n",
321          level.game_version);
322 #endif
323
324   for (i = 0; i < MAX_PLAYERS; i++)
325     tape.player_participates[i] = FALSE;
326 }
327
328 static void TapeRewind()
329 {
330   tape.counter = 0;
331   tape.delay_played = 0;
332   tape.pause_before_death = FALSE;
333   tape.recording = FALSE;
334   tape.playing = FALSE;
335   tape.fast_forward = FALSE;
336   tape.index_search = FALSE;
337   tape.auto_play = (global.autoplay_leveldir != NULL);
338   tape.auto_play_level_solved = FALSE;
339   tape.quick_resume = FALSE;
340   tape.single_step = FALSE;
341
342   InitRND(tape.random_seed);
343 }
344
345 void TapeStartRecording()
346 {
347   if (!TAPE_IS_STOPPED(tape))
348     TapeStop();
349
350   TapeErase();
351   TapeRewind();
352
353   tape.recording = TRUE;
354
355   DrawVideoDisplay(VIDEO_STATE_REC_ON, 0);
356   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
357   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
358   MapTapeIndexButton();
359
360   SetDrawDeactivationMask(REDRAW_NONE);
361   audio.sound_deactivated = FALSE;
362 }
363
364 static void TapeStartGameRecording()
365 {
366   TapeStartRecording();
367
368 #if defined(PLATFORM_UNIX)
369   if (options.network)
370     SendToServer_StartPlaying();
371   else
372 #endif
373   {
374     game_status = GAME_MODE_PLAYING;
375     StopAnimation();
376     InitGame();
377   }
378 }
379
380 static void TapeAppendRecording()
381 {
382   if (!tape.playing || !tape.pausing)
383     return;
384
385   tape.pos[tape.counter].delay = tape.delay_played;
386   tape.playing = FALSE;
387   tape.recording = TRUE;
388   tape.changed = TRUE;
389
390   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF | VIDEO_STATE_REC_ON,0);
391 }
392
393 void TapeHaltRecording()
394 {
395   if (!tape.recording)
396     return;
397
398   tape.counter++;
399   tape.pos[tape.counter].delay = 0;
400
401   tape.length = tape.counter;
402   tape.length_seconds = GetTapeLength();
403 }
404
405 void TapeStopRecording()
406 {
407   if (!tape.recording)
408     return;
409
410   TapeHaltRecording();
411
412   tape.recording = FALSE;
413   tape.pausing = FALSE;
414
415   DrawVideoDisplay(VIDEO_STATE_REC_OFF, 0);
416   MapTapeEjectButton();
417 }
418
419 void TapeRecordAction(byte action[MAX_PLAYERS])
420 {
421   int i;
422
423   if (!tape.recording || tape.pausing)
424     return;
425
426   if (tape.counter >= MAX_TAPELEN - 1)
427   {
428     TapeStopRecording();
429     return;
430   }
431
432   if (tape.pos[tape.counter].delay > 0)         /* already stored action */
433   {
434     boolean changed_events = FALSE;
435
436     for (i = 0; i < MAX_PLAYERS; i++)
437       if (tape.pos[tape.counter].action[i] != action[i])
438         changed_events = TRUE;
439
440     if (changed_events || tape.pos[tape.counter].delay >= 255)
441     {
442       tape.counter++;
443       tape.pos[tape.counter].delay = 0;
444     }
445     else
446       tape.pos[tape.counter].delay++;
447   }
448
449   if (tape.pos[tape.counter].delay == 0)        /* store new action */
450   {
451     for (i = 0; i < MAX_PLAYERS; i++)
452       tape.pos[tape.counter].action[i] = action[i];
453
454     tape.pos[tape.counter].delay++;
455   }
456 }
457
458 void TapeTogglePause(boolean toggle_manual)
459 {
460   unsigned long state;
461
462 #if 0
463   if (!tape.recording && !tape.playing)
464     return;
465 #endif
466
467   tape.pausing = !tape.pausing;
468   tape.fast_forward = FALSE;
469   tape.pause_before_death = FALSE;
470
471   if (tape.single_step && toggle_manual)
472     tape.single_step = FALSE;
473
474   state = (tape.pausing ? VIDEO_STATE_PAUSE_ON : VIDEO_STATE_PAUSE_OFF);
475   if (tape.playing)
476     state |= VIDEO_STATE_PBEND_OFF;
477
478   DrawVideoDisplay(state, 0);
479
480   if (tape.index_search)
481   {
482     TapeStopIndexSearch();
483
484     if (tape.quick_resume)
485     {
486       tape.quick_resume = FALSE;
487
488       TapeAppendRecording();
489       TapeTogglePause(toggle_manual);
490     }
491   }
492 }
493
494 void TapeStartPlaying()
495 {
496   if (TAPE_IS_EMPTY(tape))
497     return;
498
499   if (!TAPE_IS_STOPPED(tape))
500     TapeStop();
501
502   TapeRewind();
503
504   tape.playing = TRUE;
505
506   DrawVideoDisplay(VIDEO_STATE_PLAY_ON, 0);
507   DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
508   DrawVideoDisplay(VIDEO_STATE_TIME_ON, 0);
509   MapTapeIndexButton();
510
511   SetDrawDeactivationMask(REDRAW_NONE);
512   audio.sound_deactivated = FALSE;
513 }
514
515 static void TapeStartGamePlaying()
516 {
517   TapeStartPlaying();
518
519   game_status = GAME_MODE_PLAYING;
520   StopAnimation();
521   InitGame();
522 }
523
524 void TapeStopPlaying()
525 {
526   if (!tape.playing)
527     return;
528
529   tape.playing = FALSE;
530   tape.pausing = FALSE;
531
532   if (tape.index_search)
533     TapeStopIndexSearch();
534
535   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF, 0);
536   MapTapeEjectButton();
537 }
538
539 byte *TapePlayAction()
540 {
541   static byte action[MAX_PLAYERS];
542   int i;
543
544   if (!tape.playing || tape.pausing)
545     return NULL;
546
547   if (tape.pause_before_death)  /* STOP 10s BEFORE PLAYER GETS KILLED... */
548   {
549     if (!(FrameCounter % 20))
550     {
551       if ((FrameCounter / 20) % 2)
552         DrawVideoDisplay(VIDEO_STATE_PBEND_ON, VIDEO_DISPLAY_LABEL_ONLY);
553       else
554         DrawVideoDisplay(VIDEO_STATE_PBEND_OFF, VIDEO_DISPLAY_LABEL_ONLY);
555     }
556
557     if (TimePlayed > tape.length_seconds - TAPE_PAUSE_SECONDS_BEFORE_DEATH)
558     {
559       TapeTogglePause(TAPE_TOGGLE_MANUAL);
560       return NULL;
561     }
562   }
563
564   if (tape.counter >= tape.length)      /* end of tape reached */
565   {
566     if (tape.index_search && !tape.auto_play)
567       TapeTogglePause(TAPE_TOGGLE_MANUAL);
568     else
569       TapeStop();
570
571     return NULL;
572   }
573
574   for (i = 0; i < MAX_PLAYERS; i++)
575     action[i] = tape.pos[tape.counter].action[i];
576
577   tape.delay_played++;
578   if (tape.delay_played >= tape.pos[tape.counter].delay)
579   {
580     tape.counter++;
581     tape.delay_played = 0;
582   }
583
584   return action;
585 }
586
587 void TapeStop()
588 {
589   TapeStopRecording();
590   TapeStopPlaying();
591
592   DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0);
593   if (tape.date && tape.length)
594   {
595     DrawVideoDisplay(VIDEO_STATE_DATE_ON, tape.date);
596     DrawVideoDisplay(VIDEO_STATE_TIME_ON, tape.length_seconds);
597   }
598
599 #if 0
600   if (tape.auto_play)
601     AutoPlayTape();     /* continue automatically playing next tape */
602 #endif
603 }
604
605 unsigned int GetTapeLength()
606 {
607   unsigned int tape_length = 0;
608   int i;
609
610   if (TAPE_IS_EMPTY(tape))
611     return(0);
612
613   for (i = 0; i < tape.length; i++)
614     tape_length += tape.pos[i].delay;
615
616   return(tape_length * GAME_FRAME_DELAY / 1000);
617 }
618
619 static void TapeStartIndexSearch()
620 {
621   tape.index_search = TRUE;
622
623   if (!tape.fast_forward || tape.pause_before_death)
624   {
625     tape.pausing = FALSE;
626
627     SetDrawDeactivationMask(REDRAW_FIELD);
628     audio.sound_deactivated = TRUE;
629   }
630 }
631
632 static void TapeStopIndexSearch()
633 {
634   tape.index_search = FALSE;
635
636   SetDrawDeactivationMask(REDRAW_NONE);
637   audio.sound_deactivated = FALSE;
638
639   RedrawPlayfield(TRUE, 0,0,0,0);
640   DrawGameDoorValues();
641 }
642
643 static void TapeSingleStep()
644 {
645   if (options.network)
646     return;
647
648   if (!tape.pausing)
649     TapeTogglePause(TAPE_TOGGLE_MANUAL);
650
651   tape.single_step = !tape.single_step;
652 }
653
654 void TapeQuickSave()
655 {
656   if (game_status == GAME_MODE_PLAYING)
657   {
658     if (tape.recording)
659       TapeHaltRecording();      /* prepare tape for saving on-the-fly */
660
661     if (TAPE_IS_EMPTY(tape))
662       Request("No tape that can be saved !", REQ_CONFIRM);
663     else
664       SaveTape(tape.level_nr);
665   }
666   else if (game_status == GAME_MODE_MAIN)
667     Request("No game that can be saved !", REQ_CONFIRM);
668 }
669
670 void TapeQuickLoad()
671 {
672   if (game_status == GAME_MODE_PLAYING || game_status == GAME_MODE_MAIN)
673   {
674     TapeStop();
675     TapeErase();
676
677     LoadTape(level_nr);
678     if (!TAPE_IS_EMPTY(tape))
679     {
680       TapeStartGamePlaying();
681       TapeStartIndexSearch();
682
683       tape.quick_resume = TRUE;
684     }
685     else
686       Request("No tape for this level !", REQ_CONFIRM);
687   }
688 }
689
690 void InsertSolutionTape()
691 {
692   if (!TAPE_IS_EMPTY(tape))
693     return;
694
695   LoadSolutionTape(level_nr);
696
697   if (TAPE_IS_EMPTY(tape))
698     Request("No solution tape for this level !", REQ_CONFIRM);
699
700   DrawCompleteVideoDisplay();
701 }
702
703
704 /* ------------------------------------------------------------------------- *
705  * tape autoplay functions
706  * ------------------------------------------------------------------------- */
707
708 #define MAX_NUM_AUTOPLAY_LEVELS         1000
709
710 void AutoPlayTape()
711 {
712   static LevelDirTree *autoplay_leveldir = NULL;
713   static boolean autoplay_initialized = FALSE;
714   static int autoplay_level_nr = -1;
715   static int num_levels_played = 0;
716   static int num_levels_solved = 0;
717   static boolean levels_failed[MAX_NUM_AUTOPLAY_LEVELS];
718   int i;
719
720   if (autoplay_initialized)
721   {
722     /* just finished auto-playing tape */
723     printf("%s.\n", tape.auto_play_level_solved ? "solved" : "NOT SOLVED");
724
725     num_levels_played++;
726     if (tape.auto_play_level_solved)
727       num_levels_solved++;
728     else if (level_nr >= 0 && level_nr < MAX_NUM_AUTOPLAY_LEVELS)
729       levels_failed[level_nr] = TRUE;
730   }
731   else
732   {
733     DrawCompleteVideoDisplay();
734     audio.sound_enabled = FALSE;
735
736     autoplay_leveldir = getTreeInfoFromIdentifier(leveldir_first,
737                                                   global.autoplay_leveldir);
738
739     if (autoplay_leveldir == NULL)
740       Error(ERR_EXIT, "no such level identifier: '%s'",
741             global.autoplay_leveldir);
742
743     leveldir_current = autoplay_leveldir;
744
745     if (global.autoplay_level_nr != -1)
746     {
747       autoplay_leveldir->first_level = global.autoplay_level_nr;
748       autoplay_leveldir->last_level  = global.autoplay_level_nr;
749     }
750
751     autoplay_level_nr = autoplay_leveldir->first_level;
752
753     printf_line("=", 79);
754     printf("Automatically playing level tapes\n");
755     printf_line("-", 79);
756     printf("Level series identifier: '%s'\n", autoplay_leveldir->identifier);
757     printf("Level series name:       '%s'\n", autoplay_leveldir->name);
758     printf("Level series author:     '%s'\n", autoplay_leveldir->author);
759     printf("Number of levels:        %d\n",   autoplay_leveldir->levels);
760     printf_line("=", 79);
761     printf("\n");
762
763     for (i = 0; i < MAX_NUM_AUTOPLAY_LEVELS; i++)
764       levels_failed[i] = FALSE;
765
766     autoplay_initialized = TRUE;
767   }
768
769   while (autoplay_level_nr <= autoplay_leveldir->last_level)
770   {
771     level_nr = autoplay_level_nr++;
772
773     TapeErase();
774
775     printf("Level %03d: ", level_nr);
776
777     LoadLevel(level_nr);
778     if (level.no_level_file)
779     {
780       printf("(no level)\n");
781       continue;
782     }
783
784     LoadSolutionTape(level_nr);
785     if (TAPE_IS_EMPTY(tape))
786     {
787       printf("(no tape)\n");
788       continue;
789     }
790
791     printf("playing tape ... ");
792
793     TapeStartGamePlaying();
794     TapeStartIndexSearch();
795
796     return;
797   }
798
799   printf("\n");
800   printf_line("=", 79);
801   printf("Number of levels played: %d\n", num_levels_played);
802   printf("Number of levels solved: %d (%d%%)\n", num_levels_solved,
803          (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
804   printf_line("-", 79);
805   printf("Summary (for automatic parsing by scripts):\n");
806   printf("LEVELDIR '%s', SOLVED %d/%d (%d%%)",
807          autoplay_leveldir->identifier, num_levels_solved, num_levels_played,
808          (num_levels_played ? num_levels_solved * 100 / num_levels_played :0));
809   if (num_levels_played != num_levels_solved)
810   {
811     printf(", FAILED:");
812     for (i = 0; i < MAX_NUM_AUTOPLAY_LEVELS; i++)
813       if (levels_failed[i])
814         printf(" %03d", i);
815   }
816   printf("\n");
817   printf_line("=", 79);
818
819   CloseAllAndExit(0);
820 }
821
822
823 /* ---------- new tape button stuff ---------------------------------------- */
824
825 /* graphic position values for tape buttons */
826 #define TAPE_BUTTON_XSIZE       18
827 #define TAPE_BUTTON_YSIZE       18
828 #define TAPE_BUTTON_XPOS        5
829 #define TAPE_BUTTON_YPOS        77
830
831 #define TAPE_BUTTON_EJECT_XPOS  (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
832 #define TAPE_BUTTON_INDEX_XPOS  (TAPE_BUTTON_XPOS + 0 * TAPE_BUTTON_XSIZE)
833 #define TAPE_BUTTON_STOP_XPOS   (TAPE_BUTTON_XPOS + 1 * TAPE_BUTTON_XSIZE)
834 #define TAPE_BUTTON_PAUSE_XPOS  (TAPE_BUTTON_XPOS + 2 * TAPE_BUTTON_XSIZE)
835 #define TAPE_BUTTON_RECORD_XPOS (TAPE_BUTTON_XPOS + 3 * TAPE_BUTTON_XSIZE)
836 #define TAPE_BUTTON_PLAY_XPOS   (TAPE_BUTTON_XPOS + 4 * TAPE_BUTTON_XSIZE)
837
838 static struct
839 {
840   int x, y;
841   int gadget_id;
842   char *infotext;
843 } tapebutton_info[NUM_TAPE_BUTTONS] =
844 {
845   {
846     TAPE_BUTTON_EJECT_XPOS,     TAPE_BUTTON_YPOS,
847     TAPE_CTRL_ID_EJECT,
848     "eject tape"
849   },
850   {
851     TAPE_BUTTON_INDEX_XPOS,     TAPE_BUTTON_YPOS,
852     TAPE_CTRL_ID_INDEX,
853     "index mark"
854   },
855   {
856     TAPE_BUTTON_STOP_XPOS,      TAPE_BUTTON_YPOS,
857     TAPE_CTRL_ID_STOP,
858     "stop tape"
859   },
860   {
861     TAPE_BUTTON_PAUSE_XPOS,     TAPE_BUTTON_YPOS,
862     TAPE_CTRL_ID_PAUSE,
863     "pause tape"
864   },
865   {
866     TAPE_BUTTON_RECORD_XPOS,    TAPE_BUTTON_YPOS,
867     TAPE_CTRL_ID_RECORD,
868     "record tape"
869   },
870   {
871     TAPE_BUTTON_PLAY_XPOS,      TAPE_BUTTON_YPOS,
872     TAPE_CTRL_ID_PLAY,
873     "play tape"
874   }
875 };
876
877 void CreateTapeButtons()
878 {
879   int i;
880
881   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
882   {
883     Bitmap *gd_bitmap = graphic_info[IMG_GLOBAL_DOOR].bitmap;
884     struct GadgetInfo *gi;
885     int gd_xoffset, gd_yoffset;
886     int gd_x1, gd_x2, gd_y;
887     int id = i;
888
889     gd_xoffset = tapebutton_info[i].x;
890     gd_yoffset = tapebutton_info[i].y;
891     gd_x1 = DOOR_GFX_PAGEX4 + gd_xoffset;
892     gd_x2 = DOOR_GFX_PAGEX3 + gd_xoffset;
893     gd_y  = DOOR_GFX_PAGEY2 + gd_yoffset;
894
895     if (i == TAPE_CTRL_ID_INDEX)
896     {
897       gd_x1 = DOOR_GFX_PAGEX6 + gd_xoffset;
898       gd_x2 = DOOR_GFX_PAGEX5 + gd_xoffset;
899     }
900
901     gi = CreateGadget(GDI_CUSTOM_ID, id,
902                       GDI_INFO_TEXT, tapebutton_info[i].infotext,
903                       GDI_X, VX + gd_xoffset,
904                       GDI_Y, VY + gd_yoffset,
905                       GDI_WIDTH, TAPE_BUTTON_XSIZE,
906                       GDI_HEIGHT, TAPE_BUTTON_YSIZE,
907                       GDI_TYPE, GD_TYPE_NORMAL_BUTTON,
908                       GDI_STATE, GD_BUTTON_UNPRESSED,
909                       GDI_DESIGN_UNPRESSED, gd_bitmap, gd_x1, gd_y,
910                       GDI_DESIGN_PRESSED, gd_bitmap, gd_x2, gd_y,
911                       GDI_EVENT_MASK, GD_EVENT_RELEASED,
912                       GDI_CALLBACK_ACTION, HandleTapeButtons,
913                       GDI_END);
914
915     if (gi == NULL)
916       Error(ERR_EXIT, "cannot create gadget");
917
918     tape_gadget[id] = gi;
919   }
920 }
921
922 void FreeTapeButtons()
923 {
924   int i;
925
926   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
927     FreeGadget(tape_gadget[i]);
928 }
929
930 void MapTapeEjectButton()
931 {
932   UnmapGadget(tape_gadget[TAPE_CTRL_ID_INDEX]);
933   MapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
934 }
935
936 void MapTapeIndexButton()
937 {
938   UnmapGadget(tape_gadget[TAPE_CTRL_ID_EJECT]);
939   MapGadget(tape_gadget[TAPE_CTRL_ID_INDEX]);
940 }
941
942 void MapTapeButtons()
943 {
944   int i;
945
946   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
947     if (i != TAPE_CTRL_ID_INDEX)
948       MapGadget(tape_gadget[i]);
949
950   if (tape.recording || tape.playing)
951     MapTapeIndexButton();
952 }
953
954 void UnmapTapeButtons()
955 {
956   int i;
957
958   for (i = 0; i < NUM_TAPE_BUTTONS; i++)
959     UnmapGadget(tape_gadget[i]);
960 }
961
962 static void HandleTapeButtons(struct GadgetInfo *gi)
963 {
964   int id = gi->custom_id;
965
966   if (game_status != GAME_MODE_MAIN && game_status != GAME_MODE_PLAYING)
967     return;
968
969   switch (id)
970   {
971     case TAPE_CTRL_ID_EJECT:
972       TapeStop();
973       if (TAPE_IS_EMPTY(tape))
974       {
975         LoadTape(level_nr);
976         if (TAPE_IS_EMPTY(tape))
977           Request("No tape for this level !", REQ_CONFIRM);
978       }
979       else
980       {
981         if (tape.changed)
982           SaveTape(tape.level_nr);
983         TapeErase();
984       }
985       DrawCompleteVideoDisplay();
986       break;
987
988     case TAPE_CTRL_ID_INDEX:
989       if (tape.playing)
990         TapeStartIndexSearch();
991       else if (tape.recording)
992         TapeSingleStep();
993       break;
994
995     case TAPE_CTRL_ID_STOP:
996       TapeStop();
997       break;
998
999     case TAPE_CTRL_ID_PAUSE:
1000       TapeTogglePause(TAPE_TOGGLE_MANUAL);
1001       break;
1002
1003     case TAPE_CTRL_ID_RECORD:
1004       if (TAPE_IS_STOPPED(tape))
1005         TapeStartGameRecording();
1006       else if (tape.pausing)
1007       {
1008         if (tape.playing)       /* PLAYING -> PAUSING -> RECORDING */
1009           TapeAppendRecording();
1010         else
1011           TapeTogglePause(TAPE_TOGGLE_MANUAL);
1012       }
1013       break;
1014
1015     case TAPE_CTRL_ID_PLAY:
1016       if (TAPE_IS_EMPTY(tape))
1017         break;
1018
1019       if (TAPE_IS_STOPPED(tape))
1020       {
1021         TapeStartGamePlaying();
1022       }
1023       else if (tape.playing)
1024       {
1025         if (tape.pausing)                       /* PAUSE -> PLAY */
1026         {
1027           TapeTogglePause(TAPE_TOGGLE_MANUAL);
1028         }
1029         else if (!tape.fast_forward)            /* PLAY -> FAST FORWARD PLAY */
1030         {
1031           tape.fast_forward = TRUE;
1032           DrawVideoDisplay(VIDEO_STATE_FFWD_ON, 0);
1033         }
1034         else if (!tape.pause_before_death)      /* FFWD PLAY -> AUTO PAUSE */
1035         {
1036           tape.pause_before_death = TRUE;
1037           DrawVideoDisplay(VIDEO_STATE_PBEND_ON, VIDEO_DISPLAY_LABEL_ONLY);
1038         }
1039         else                                    /* AUTO PAUSE -> NORMAL PLAY */
1040         {
1041           tape.fast_forward = FALSE;
1042           tape.pause_before_death = FALSE;
1043           DrawVideoDisplay(VIDEO_STATE_FFWD_OFF | VIDEO_STATE_PBEND_OFF, 0);
1044         }
1045       }
1046       break;
1047
1048     default:
1049       break;
1050   }
1051 }