9bafe55b6e5d0a6a9ec31ad50d36e739e98d7918
[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 "main_em.h"
7
8
9 unsigned int RandomEM;
10
11 struct LEVEL lev;
12 struct PLAYER ply[MAX_PLAYERS];
13
14 short **Boom;
15 short **Cave;
16 short **Next;
17 short **Draw;
18
19 static short *Index[4][HEIGHT];
20 static short Array[4][HEIGHT][WIDTH];
21
22 extern int screen_x;
23 extern int screen_y;
24
25 struct EngineSnapshotInfo_EM engine_snapshot_em;
26
27 void game_init_vars(void)
28 {
29   int x, y;
30
31   RandomEM = 1684108901;
32
33   for (y = 0; y < HEIGHT; y++)
34     for (x = 0; x < WIDTH; x++)
35       Array[0][y][x] = ZBORDER;
36   for (y = 0; y < HEIGHT; y++)
37     for (x = 0; x < WIDTH; x++)
38       Array[1][y][x] = ZBORDER;
39   for (y = 0; y < HEIGHT; y++)
40     for (x = 0; x < WIDTH; x++)
41       Array[2][y][x] = ZBORDER;
42   for (y = 0; y < HEIGHT; y++)
43     for (x = 0; x < WIDTH; x++)
44       Array[3][y][x] = Xblank;
45
46   for (y = 0; y < HEIGHT; y++)
47     Index[0][y] = Array[0][y];
48   for (y = 0; y < HEIGHT; y++)
49     Index[1][y] = Array[1][y];
50   for (y = 0; y < HEIGHT; y++)
51     Index[2][y] = Array[2][y];
52   for (y = 0; y < HEIGHT; y++)
53     Index[3][y] = Array[3][y];
54
55   Cave = Index[0];
56   Next = Index[1];
57   Draw = Index[2];
58   Boom = Index[3];
59 }
60
61 void InitGameEngine_EM(void)
62 {
63   prepare_em_level();
64
65   game_initscreen();
66
67   RedrawPlayfield_EM(FALSE);
68 }
69
70 static void UpdateGameDoorValues_EM(void)
71 {
72 }
73
74 void GameActions_EM(byte action[MAX_PLAYERS], boolean warp_mode)
75 {
76   int i;
77   boolean any_player_dropping = FALSE;
78
79   RandomEM = RandomEM * 129 + 1;
80
81   frame = (frame - 1) & 7;
82
83   for (i = 0; i < MAX_PLAYERS; i++)
84     readjoy(action[i], &ply[i]);
85
86   UpdateEngineValues(screen_x / TILEX, screen_y / TILEY, ply[0].x, ply[0].y);
87
88   if (frame == 7)
89   {
90     synchro_1();
91     synchro_2();
92   }
93
94   if (frame == 6)
95   {
96     synchro_3();
97
98     UpdateGameDoorValues_EM();
99   }
100
101   for (i = 0; i < MAX_PLAYERS; i++)
102     if (ply[i].joy_drop &&
103         ply[i].dynamite &&
104         ply[i].dynamite_cnt > 0 &&
105         ply[i].dynamite_cnt < 5)
106       any_player_dropping = TRUE;
107
108   CheckSingleStepMode_EM(action, frame, game_em.any_player_moving,
109                          game_em.any_player_snapping, any_player_dropping);
110
111   RedrawPlayfield_EM(FALSE);
112 }
113
114 /* read input device for players */
115
116 void readjoy(byte action, struct PLAYER *ply)
117 {
118   int north = 0, east = 0, south = 0, west = 0;
119   int snap = 0, drop = 0;
120
121   if (game_em.use_single_button && action & (JOY_BUTTON_1 | JOY_BUTTON_2))
122     action |= JOY_BUTTON_1 | JOY_BUTTON_2;
123
124   if (action & JOY_LEFT)
125     west = 1;
126
127   if (action & JOY_RIGHT)
128     east = 1;
129
130   if (action & JOY_UP)
131     north = 1;
132
133   if (action & JOY_DOWN)
134     south = 1;
135
136   if (action & JOY_BUTTON_1)
137     snap = 1;
138
139   if (action & JOY_BUTTON_2)
140     drop = 1;
141
142   /* always update drop action */
143   ply->joy_drop = drop;
144
145   if (ply->joy_stick || (north | east | south | west))  /* (no "| snap"!) */
146   {
147     ply->joy_n = north;
148     ply->joy_e = east;
149     ply->joy_s = south;
150     ply->joy_w = west;
151
152     /* when storing last action, only update snap action with direction */
153     /* (prevents clearing direction if snapping stopped before frame 7) */
154     ply->joy_snap = snap;
155   }
156
157   /* if no direction was stored before, allow setting snap to current state */
158   if (!ply->joy_n &&
159       !ply->joy_e &&
160       !ply->joy_s &&
161       !ply->joy_w)
162     ply->joy_snap = snap;
163
164   /* use bug with snap key (mainly TAS keys) sometimes moving the player */
165   if (game_em.use_snap_key_bug)
166     ply->joy_snap = snap;
167 }
168
169 void SaveEngineSnapshotValues_EM(void)
170 {
171   int i, j, k;
172
173   engine_snapshot_em.game_em = game_em;
174   engine_snapshot_em.lev = lev;
175
176   engine_snapshot_em.RandomEM = RandomEM;
177   engine_snapshot_em.frame = frame;
178
179   engine_snapshot_em.screen_x = screen_x;
180   engine_snapshot_em.screen_y = screen_y;
181
182   engine_snapshot_em.Boom = Boom;
183   engine_snapshot_em.Cave = Cave;
184   engine_snapshot_em.Next = Next;
185   engine_snapshot_em.Draw = Draw;
186
187   for (i = 0; i < 4; i++)
188     engine_snapshot_em.ply[i] = ply[i];
189
190   for (i = 0; i < 4; i++)
191     for (j = 0; j < HEIGHT; j++)
192       for (k = 0; k < WIDTH; k++)
193         engine_snapshot_em.Array[i][j][k] = Array[i][j][k];
194 }
195
196 void LoadEngineSnapshotValues_EM(void)
197 {
198   int i, j, k;
199
200   game_em = engine_snapshot_em.game_em;
201   lev = engine_snapshot_em.lev;
202
203   RandomEM = engine_snapshot_em.RandomEM;
204   frame = engine_snapshot_em.frame;
205
206   screen_x = engine_snapshot_em.screen_x;
207   screen_y = engine_snapshot_em.screen_y;
208
209   Boom = engine_snapshot_em.Boom;
210   Cave = engine_snapshot_em.Cave;
211   Next = engine_snapshot_em.Next;
212   Draw = engine_snapshot_em.Draw;
213
214   for (i = 0; i < 4; i++)
215     ply[i] = engine_snapshot_em.ply[i];
216
217   for (i = 0; i < 4; i++)
218     for (j = 0; j < HEIGHT; j++)
219       for (k = 0; k < WIDTH; k++)
220         Array[i][j][k] = engine_snapshot_em.Array[i][j][k];
221 }