rnd-19980930-1
[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
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.recording = TRUE;
32   tape.playing = FALSE;
33   tape.pausing = FALSE;
34   tape.changed = TRUE;
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   int i;
46
47   if (!tape.recording)
48     return;
49
50   for(i=0; i<MAX_PLAYERS; i++)
51     tape.pos[tape.counter].joystickdata[i] = 0;
52
53   tape.counter++;
54   tape.length = tape.counter;
55   tape.length_seconds = GetTapeLength();
56   tape.recording = FALSE;
57   tape.pausing = FALSE;
58   DrawVideoDisplay(VIDEO_STATE_REC_OFF,0);
59 }
60
61 void TapeRecordAction(int joy[MAX_PLAYERS])
62 {
63   int i;
64
65   if (!tape.recording || tape.pausing)
66     return;
67
68   if (tape.counter>=MAX_TAPELEN-1)
69   {
70     TapeStopRecording();
71     return;
72   }
73
74   for(i=0; i<MAX_PLAYERS; i++)
75     tape.pos[tape.counter].joystickdata[i] = joy[i];
76
77   tape.counter++;
78   tape.pos[tape.counter].delay = 0;
79 }
80
81 void TapeRecordDelay()
82 {
83   int i;
84
85   if (!tape.recording || tape.pausing)
86     return;
87
88   if (tape.counter>=MAX_TAPELEN)
89   {
90     TapeStopRecording();
91     return;
92   }
93
94   tape.pos[tape.counter].delay++;
95
96   if (tape.pos[tape.counter].delay >= 255)
97   {
98     for(i=0; i<MAX_PLAYERS; i++)
99       tape.pos[tape.counter].joystickdata[i] = 0;
100
101     tape.counter++;
102     tape.pos[tape.counter].delay = 0;
103   }
104 }
105
106 void TapeTogglePause()
107 {
108   if (!tape.recording && !tape.playing)
109     return;
110
111   tape.pausing = !tape.pausing;
112   tape.fast_forward = FALSE;
113   tape.pause_before_death = FALSE;
114   DrawVideoDisplay(tape.pausing ?
115                    VIDEO_STATE_PAUSE_ON :
116                    VIDEO_STATE_PAUSE_OFF,0);
117 }
118
119 void TapeStartPlaying()
120 {
121   if (TAPE_IS_EMPTY(tape))
122     return;
123
124   if (!TAPE_IS_STOPPED(tape))
125     TapeStop();
126
127   tape.counter = 0;
128   tape.delay_played = 0;
129   tape.pause_before_death = FALSE;
130   tape.recording = FALSE;
131   tape.playing = TRUE;
132   tape.pausing = FALSE;
133   tape.fast_forward = FALSE;
134   InitRND(tape.random_seed);
135
136   DrawVideoDisplay(VIDEO_STATE_PLAY_ON,0);
137   DrawVideoDisplay(VIDEO_STATE_DATE_ON,tape.date);
138   DrawVideoDisplay(VIDEO_STATE_TIME_ON,0);
139 }
140
141 void TapeStopPlaying()
142 {
143   if (!tape.playing)
144     return;
145
146   tape.playing = FALSE;
147   tape.pausing = FALSE;
148   DrawVideoDisplay(VIDEO_STATE_PLAY_OFF,0);
149 }
150
151 int *TapePlayAction()
152 {
153   static int joy[MAX_PLAYERS];
154   int i;
155
156   if (!tape.playing || tape.pausing)
157     return(NULL);
158
159   if (tape.counter>=tape.length)
160   {
161     TapeStop();
162     return(NULL);
163   }
164
165   if (tape.delay_played == tape.pos[tape.counter].delay)
166   {
167     tape.delay_played = 0;
168     tape.counter++;
169
170     for(i=0; i<MAX_PLAYERS; i++)
171       joy[i] = tape.pos[tape.counter-1].joystickdata[i];
172
173     return(joy);
174   }
175
176   return(NULL);
177 }
178
179 BOOL TapePlayDelay()
180 {
181   if (!tape.playing || tape.pausing)
182     return(FALSE);
183
184   if (tape.pause_before_death)  /* STOP 10s BEFORE PLAYER GETS KILLED... */
185   {
186     if (!(FrameCounter % 5))
187     {
188       if (2*(FrameCounter/10) == FrameCounter/5)
189         DrawVideoDisplay(VIDEO_STATE_PAUSE_ON, VIDEO_DISPLAY_LABEL_ONLY);
190       else
191         DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF, VIDEO_DISPLAY_LABEL_ONLY);
192     }
193
194     if (level.time-TimeLeft > tape.length_seconds - PAUSE_SECONDS_BEFORE_DEATH)
195     {
196       TapeTogglePause();
197       return(FALSE);
198     }
199   }
200
201   if (tape.counter>=tape.length)
202   {
203     TapeStop();
204     return(TRUE);
205   }
206
207   if (tape.delay_played < tape.pos[tape.counter].delay)
208   {
209     tape.delay_played++;
210     return(TRUE);
211   }
212   else
213     return(FALSE);
214 }
215
216 void TapeStop()
217 {
218   TapeStopRecording();
219   TapeStopPlaying();
220
221   DrawVideoDisplay(VIDEO_STATE_PAUSE_OFF,0);
222   if (tape.date && tape.length)
223   {
224     DrawVideoDisplay(VIDEO_STATE_DATE_ON,tape.date);
225     DrawVideoDisplay(VIDEO_STATE_TIME_ON,tape.length_seconds);
226   }
227 }
228
229 void TapeErase()
230 {
231   tape.length = 0;
232 }
233
234 unsigned int GetTapeLength()
235 {
236   unsigned int tape_length = 0;
237   int i;
238
239   if (TAPE_IS_EMPTY(tape))
240     return(0);
241
242   for(i=0;i<tape.length;i++)
243     tape_length += tape.pos[i].delay;
244
245   return(tape_length * GAME_FRAME_DELAY / 100);
246 }