rnd-20060203-1-src
[rocksndiamonds.git] / src / game_em / input.c
1 /* 2000-08-13T15:29:40Z
2  *
3  * handle input from x11 and keyboard and joystick
4  */
5
6 #include "global.h"
7 #include "display.h"
8 #include "level.h"
9
10
11 unsigned long RandomEM;
12
13 struct PLAYER ply1;
14 struct PLAYER ply2;
15 struct LEVEL lev;
16
17 unsigned short **Boom;
18 unsigned short **Cave;
19 unsigned short **Next;
20 unsigned short **Draw;
21
22 static unsigned short *Index[4][HEIGHT];
23 static unsigned short Array[4][HEIGHT][WIDTH];
24
25 extern unsigned int screen_x;
26 extern unsigned int screen_y;
27
28 void game_init_vars(void)
29 {
30   int x, y;
31
32   RandomEM = 1684108901;
33
34   for (y = 0; y < HEIGHT; y++)
35     for (x = 0; x < WIDTH; x++)
36       Array[0][y][x] = ZBORDER;
37   for (y = 0; y < HEIGHT; y++)
38     for (x = 0; x < WIDTH; x++)
39       Array[1][y][x] = ZBORDER;
40   for (y = 0; y < HEIGHT; y++)
41     for (x = 0; x < WIDTH; x++)
42       Array[2][y][x] = ZBORDER;
43   for (y = 0; y < HEIGHT; y++)
44     for (x = 0; x < WIDTH; x++)
45       Array[3][y][x] = Xblank;
46
47   for (y = 0; y < HEIGHT; y++)
48     Index[0][y] = Array[0][y];
49   for (y = 0; y < HEIGHT; y++)
50     Index[1][y] = Array[1][y];
51   for (y = 0; y < HEIGHT; y++)
52     Index[2][y] = Array[2][y];
53   for (y = 0; y < HEIGHT; y++)
54     Index[3][y] = Array[3][y];
55
56   Cave = Index[0];
57   Next = Index[1];
58   Draw = Index[2];
59   Boom = Index[3];
60 }
61
62 void InitGameEngine_EM()
63 {
64   prepare_em_level();
65
66   game_initscreen();
67   game_animscreen();
68 }
69
70 #if 1
71
72 void GameActions_EM(byte action[MAX_PLAYERS])
73 {
74   static unsigned long game_frame_delay = 0;
75 #if 1
76   unsigned long game_frame_delay_value = getGameFrameDelay_EM(20);
77 #else
78   unsigned long game_frame_delay_value = getGameFrameDelay_EM(25);
79 #endif
80   int i;
81
82 #if 0
83   /* this is done in screens.c/HandleGameActions() by calling BackToFront() */
84   XSync(display, False);        /* block until all graphics are drawn */
85 #endif
86
87   WaitUntilDelayReached(&game_frame_delay, game_frame_delay_value);
88
89   game_animscreen();
90
91   RandomEM = RandomEM * 129 + 1;
92
93   frame = (frame - 1) & 7;
94
95   for (i = 0; i < 2; i++)
96     readjoy(action[i], i);
97
98   UpdateEngineValues(screen_x / TILEX, screen_y / TILEY);
99
100   if (frame == 7)
101   {
102     synchro_1();
103     synchro_2();
104   }
105
106   if (frame == 6)
107   {
108     synchro_3();
109     sound_play();
110
111     if (game_frame_delay_value > 0)     /* do not redraw values in warp mode */
112       DrawGameDoorValues_EM();
113   }
114
115 #if 0
116   if (lev.time_initial == 0)
117     lev.time++;
118   else if (lev.time > 0)
119     lev.time--;
120 #endif
121
122 #if 0
123   if (lev.time_initial > 0 &&
124       lev.time > 0 && lev.time <= 50 && lev.time % 5 == 0 && setup.time_limit)
125     play_sound(-1, -1, SAMPLE_time);
126 #endif
127 }
128
129 #else
130
131 void GameActions_EM(byte action)
132 {
133   static unsigned long game_frame_delay = 0;
134 #if 1
135   unsigned long game_frame_delay_value = getGameFrameDelay_EM(20);
136 #else
137   unsigned long game_frame_delay_value = getGameFrameDelay_EM(25);
138 #endif
139
140 #if 0
141   /* this is done in screens.c/HandleGameActions() by calling BackToFront() */
142   XSync(display, False);        /* block until all graphics are drawn */
143 #endif
144
145   WaitUntilDelayReached(&game_frame_delay, game_frame_delay_value);
146
147   game_animscreen();
148
149   RandomEM = RandomEM * 129 + 1;
150
151   frame = (frame - 1) & 7;
152
153   readjoy(action);
154
155   UpdateEngineValues(screen_x / TILEX, screen_y / TILEY);
156
157   if (frame == 7)
158   {
159     synchro_1();
160     synchro_2();
161   }
162
163   if (frame == 6)
164   {
165     synchro_3();
166     sound_play();
167
168     if (game_frame_delay_value > 0)     /* do not redraw values in warp mode */
169       DrawGameDoorValues_EM();
170   }
171
172 #if 0
173   if (lev.time_initial == 0)
174     lev.time++;
175   else if (lev.time > 0)
176     lev.time--;
177 #endif
178
179 #if 0
180   if (lev.time_initial > 0 &&
181       lev.time > 0 && lev.time <= 50 && lev.time % 5 == 0 && setup.time_limit)
182     play_sound(-1, -1, SAMPLE_time);
183 #endif
184 }
185
186 #endif
187
188
189 /* read input device for players */
190
191 #if 1
192
193 void readjoy(byte action, int player_nr)
194 {
195   unsigned int north = 0, east = 0, south = 0, west = 0;
196   unsigned int snap = 0, drop = 0;
197
198   if (action & JOY_LEFT)
199     west = 1;
200
201   if (action & JOY_RIGHT)
202     east = 1;
203
204   if (action & JOY_UP)
205     north = 1;
206
207   if (action & JOY_DOWN)
208     south = 1;
209
210   if (action & JOY_BUTTON_1)
211     snap = 1;
212
213   if (action & JOY_BUTTON_2)
214     drop = 1;
215
216   if (player_nr == 0)
217   {
218     ply1.joy_snap = snap;
219     ply1.joy_drop = drop;
220     if (ply1.joy_stick || (north | east | south | west))
221     {
222       ply1.joy_n = north;
223       ply1.joy_e = east;
224       ply1.joy_s = south;
225       ply1.joy_w = west;
226     }
227   }
228   else
229   {
230     ply2.joy_snap = snap;
231     ply2.joy_drop = drop;
232     if (ply2.joy_stick || (north | east | south | west))
233     {
234       ply2.joy_n = north;
235       ply2.joy_e = east;
236       ply2.joy_s = south;
237       ply2.joy_w = west;
238     }
239   }
240 }
241
242 #else
243
244 void readjoy(byte action)
245 {
246   unsigned int north = 0, east = 0, south = 0, west = 0;
247   unsigned int snap = 0, drop = 0;
248
249   if (action & JOY_LEFT)
250     west = 1;
251
252   if (action & JOY_RIGHT)
253     east = 1;
254
255   if (action & JOY_UP)
256     north = 1;
257
258   if (action & JOY_DOWN)
259     south = 1;
260
261   if (action & JOY_BUTTON_1)
262     snap = 1;
263
264   if (action & JOY_BUTTON_2)
265     drop = 1;
266
267 #if 1
268   ply1.joy_snap = snap;
269   ply1.joy_drop = drop;
270   if (ply1.joy_stick || (north | east | south | west))
271   {
272     ply1.joy_n = north;
273     ply1.joy_e = east;
274     ply1.joy_s = south;
275     ply1.joy_w = west;
276   }
277
278 #else
279
280   ply2.joy_snap = snap;
281   ply2.joy_drop = drop;
282   if (ply2.joy_stick || (north | east | south | west))
283   {
284     ply2.joy_n = north;
285     ply2.joy_e = east;
286     ply2.joy_s = south;
287     ply2.joy_w = west;
288   }
289 #endif
290 }
291
292 #endif