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