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