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