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