rocks_n_diamonds-1.0
[rocksndiamonds.git] / src / tape.c
1 /***********************************************************
2 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
3 *----------------------------------------------------------*
4 *  ©1997 Artsoft Development                               *
5 *        Holger Schemel                                    *
6 *        33604 Bielefeld                                   *
7 *        Telefon: (0521) 290471                            *
8 *        eMail: aeglos@valinor.owl.de                      *
9 *               aeglos@uni-paderborn.de                    *
10 *----------------------------------------------------------*
11 *  tape.c                                                  *
12 ***********************************************************/
13
14 #include "tape.h"
15 #include "misc.h"
16 #include "game.h"
17 #include "buttons.h"
18
19 void TapeStartRecording()
20 {
21   time_t zeit1 = time(NULL);
22   struct tm *zeit2 = localtime(&zeit1);
23
24   if (!TAPE_IS_STOPPED(tape))
25     TapeStop();
26
27   tape.level_nr = level_nr;
28   tape.length = 0;
29   tape.counter = 0;
30   tape.pos[tape.counter].delay = 0;
31   tape.changed = TRUE;
32   tape.recording = TRUE;
33   tape.playing = FALSE;
34   tape.pausing = FALSE;
35   tape.date = 10000*(zeit2->tm_year%100) + 100*zeit2->tm_mon + zeit2->tm_mday;
36   tape.random_seed = InitRND(NEW_RANDOMIZE);
37
38   DrawVideoDisplay(VIDEO_STATE_REC_ON,0);
39   DrawVideoDisplay(VIDEO_STATE_DATE_ON,tape.date);
40   DrawVideoDisplay(VIDEO_STATE_TIME_ON,0);
41 }
42
43 void TapeStopRecording()
44 {
45   if (!tape.recording)
46     return;
47
48   tape.pos[tape.counter].joystickdata = 0;
49   tape.counter++;
50   tape.length = tape.counter;
51   tape.length_seconds = GetTapeLength();
52   tape.recording = FALSE;
53   tape.pausing = FALSE;
54   DrawVideoDisplay(VIDEO_STATE_REC_OFF,0);
55 }
56
57 void TapeRecordAction(int joy)
58 {
59   if (!tape.recording || tape.pausing)
60     return;
61
62   if (tape.counter>=MAX_TAPELEN-1)
63   {
64     TapeStopRecording();
65     return;
66   }
67
68   if (joy)
69   {
70     tape.pos[tape.counter].joystickdata = joy;
71     tape.counter++;
72     tape.pos[tape.counter].delay = 0;
73   }
74 }
75
76 void TapeRecordDelay()
77 {
78   if (!tape.recording || tape.pausing)
79     return;
80
81   if (tape.counter>=MAX_TAPELEN)
82   {
83     TapeStopRecording();
84     return;
85   }
86
87   tape.pos[tape.counter].delay++;
88
89   if (tape.pos[tape.counter].delay>=255)
90   {
91     tape.pos[tape.counter].joystickdata = 0;
92     tape.counter++;
93     tape.pos[tape.counter].delay = 0;
94   }
95 }
96
97 void TapeTogglePause()
98 {
99   if (!tape.recording && !tape.playing)
100     return;
101
102   tape.pausing = !tape.pausing;
103   tape.fast_forward = FALSE;
104   tape.pause_before_death = FALSE;
105   DrawVideoDisplay(tape.pausing ?
106                    VIDEO_STATE_PAUSE_ON :
107                    VIDEO_STATE_PAUSE_OFF,0);
108 }
109
110 void TapeStartPlaying()
111 {
112   if (TAPE_IS_EMPTY(tape))
113     return;
114
115   if (!TAPE_IS_STOPPED(tape))
116     TapeStop();
117
118   tape.counter = 0;
119   tape.delay_played = 0;
120   tape.pause_before_death = FALSE;
121   tape.recording = FALSE;
122   tape.playing = TRUE;
123   tape.pausing = FALSE;
124   tape.fast_forward = FALSE;
125   InitRND(tape.random_seed);
126
127   DrawVideoDisplay(VIDEO_STATE_PLAY_ON,0);
128   DrawVideoDisplay(VIDEO_STATE_DATE_ON,tape.date);
129   DrawVideoDisplay(VIDEO_STATE_TIME_ON,0);
130 }
131
132 void TapeStopPlaying()
133 {
134   if (!tape.playing)
135     return;
136
137   tape.playing = FALSE;
138   tape.pausing = FALSE;
139   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF,0);
140 }
141
142 int TapePlayAction()
143 {
144   if (!tape.playing || tape.pausing)
145     return(0);
146
147   if (tape.counter>=tape.length)
148   {
149     TapeStopPlaying();
150     return(0);
151   }
152
153   if (tape.delay_played == tape.pos[tape.counter].delay)
154   {
155     tape.delay_played = 0;
156     tape.counter++;
157     return(tape.pos[tape.counter-1].joystickdata);
158   }
159   else
160     return(0);
161 }
162
163 BOOL TapePlayDelay()
164 {
165   if (!tape.playing || tape.pausing)
166     return(FALSE);
167
168   if (tape.pause_before_death)  /* STOP 10s BEFORE PLAYER GETS KILLED... */
169   {
170     if (!(FrameCounter % 5))
171     {
172       if (2*(FrameCounter/10) == FrameCounter/5)
173         DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, VIDEO_DISPLAY_LABEL_ONLY);
174       else
175         DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF, VIDEO_DISPLAY_LABEL_ONLY);
176     }
177
178     if (level.time-TimeLeft > tape.length_seconds - PAUSE_SECONDS_BEFORE_DEATH)
179     {
180       TapeTogglePause();
181       return(FALSE);
182     }
183   }
184
185   if (tape.counter>=tape.length)
186   {
187     TapeStopPlaying();
188     return(TRUE);
189   }
190
191   if (tape.delay_played < tape.pos[tape.counter].delay)
192   {
193     tape.delay_played++;
194     return(TRUE);
195   }
196   else
197     return(FALSE);
198 }
199
200 void TapeStop()
201 {
202   TapeStopRecording();
203   TapeStopPlaying();
204
205   DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0);
206   if (tape.date && tape.length)
207   {
208     DrawVideoDisplay(VIDEO_STATE_DATE_ON,tape.date);
209     DrawVideoDisplay(VIDEO_STATE_TIME_ON,tape.length_seconds);
210   }
211 }
212
213 void TapeErase()
214 {
215   tape.length = 0;
216 }
217
218 unsigned int GetTapeLength()
219 {
220   unsigned int tape_length = 0;
221   int i;
222
223   if (TAPE_IS_EMPTY(tape))
224     return(0);
225
226   for(i=0;i<tape.length;i++)
227     tape_length += tape.pos[i].delay;
228
229   return(tape_length * GAME_FRAME_DELAY / 100);
230 }