shift pistol HUD sprite
[d2d-psx.git] / src / switch.c
blobdcd06b12214ce19f4236c076c432dd6ac90e7b9f
1 /*
2 * Copyright (C) Prikol Software 1996-1997
3 * Copyright (C) Aleksey Volynskov 1996-1997
4 * Copyright (C) <ARembo@gmail.com> 2011
6 * This file is part of the Doom2D PSX project.
8 * Doom2D PSX is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * Doom2D PSX is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/> or
19 * write to the Free Software Foundation, Inc.,
20 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "glob.h"
24 #include <string.h>
25 #include "files.h"
26 #include "view.h"
27 #include "bmap.h"
28 #include "switch.h"
29 #include "player.h"
30 #include "misc.h"
31 #include "map.h"
33 #define MAXSW 100
35 extern map_block_t blk;
37 #pragma pack(1)
38 typedef struct {
39 byte x, y;
40 byte t, tm;
41 byte a, b, c, d;
42 byte f;
43 } sw_t;
44 #pragma pack()
46 static sw_t sw[MAXSW];
48 static void * sndswn, * sndswx, * sndnoway, * sndbdo, * sndbdc, * sndnotele;
49 static int swsnd;
51 int sw_secrets;
53 int SW_load(CDFILE *h) {
54 int i;
56 switch (blk.t) {
57 case MB_SWITCH2:
58 sw_secrets = 0;
59 for (i = 0; i < MAXSW && blk.sz > 0; ++i, blk.sz -= sizeof(sw_t)) {
60 cd_freadordie(sw + i, 1, sizeof(sw_t), h);
61 sw[i].tm = 0;
62 sw[i].d = 0;
63 sw[i].f |= 0x80;
64 if (sw[i].t == SW_SECRET)
65 ++sw_secrets;
67 return 1;
69 return 0;
72 void SW_alloc(void) {
73 sndswn = Z_getsnd("SWTCHN");
74 sndswx = Z_getsnd("SWTCHX");
75 sndnoway = Z_getsnd("NOWAY");
76 sndbdo = Z_getsnd("BDOPN");
77 sndbdc = Z_getsnd("BDCLS");
78 sndnotele = Z_getsnd("NOTELE");
81 void SW_init(void) {
82 int i;
84 for (i = 0; i < MAXSW; ++i)
85 sw[i].t = 0;
86 swsnd = 0;
89 static byte cht, chto, chf, f_ch;
91 static void door(byte x, byte y) {
92 byte ex;
94 if ((x >= FLDW) || (y >= FLDH))
95 return;
97 if (fld[y][x] != cht)
98 return;
100 ex = x + 1;
101 for (; x && fld[y][x - 1] == cht; --x) { }
102 for (; ex < FLDW && fld[y][ex] == cht; ++ex) { }
103 memset(fld[y] + x, chto, ex - x);
104 if (f_ch)
105 memset(fldf[y] + x, chf, ex - x);
106 for (; x < ex; ++x) {
107 door(x, y - 1);
108 door(x, y + 1);
112 void Z_water_trap(obj_t * o) {
113 int i, j, sx, sy, x, y;
115 if ((y = o->y) >= FLDH * CELH + o->h)
116 return;
118 if (((x = o->x) < 0) || (o->x > FLDW * CELW))
119 return;
121 sx = (x - o->r) / CELW;
122 sy = (y - o->h + 1) / CELH;
123 x = (x + o->r) / CELW;
124 y = (y - o->h / 2) / CELH;
125 for (i = sx; i <= x; ++i) {
126 for (j = sy; j <= y; ++j) {
127 if (fld[j][i] == 5) {
128 cht = 5;
129 chto = 255;
130 f_ch = 0;
131 door(i, j);
137 void Z_untrap(byte t) {
138 byte * p;
139 word n;
141 for (p = (byte *) fld, n = FLDW * FLDH; n; --n, ++p) {
142 if (*p == 255)
143 *p = t;
147 static void opendoor(int i) {
148 int j;
150 swsnd = Z_sound(sndbdo, 128);
151 j = fldf[sw[i].b][sw[i].a];
152 cht = 2;
153 chto = 3;
154 chf = 0;
155 f_ch = 1;
156 door(sw[i].a, sw[i].b);
157 fldf[sw[i].b][sw[i].a] = j;
158 fld_need_remap = 1;
161 static int shutdoor(int i) {
162 int j;
164 cht = 3;
165 chto = 255;
166 chf = fldf[sw[i].b][sw[i].a];
167 f_ch = 1;
168 door(sw[i].a, sw[i].b);
169 cht = 255;
170 if (Z_chktrap(0, 0, -3, HIT_SOME)) {
171 j = fldf[sw[i].b][sw[i].a];
172 chto = 3;
173 chf = 0;
174 f_ch = 1;
175 door(sw[i].a, sw[i].b);
176 fldf[sw[i].b][sw[i].a] = j;
177 return 0;
179 chto = 2;
180 door(sw[i].a, sw[i].b);
181 fld_need_remap = 1;
182 swsnd = Z_sound(sndbdc, 128);
183 return 1;
186 void SW_act(void) {
187 int i;
189 if (swsnd)
190 --swsnd;
191 for (i = 0; i < MAXSW; ++i) {
192 if (sw[i].t) {
193 if (sw[i].tm)
194 --sw[i].tm;
195 switch (sw[i].t) {
196 case SW_DOOR5:
197 case SW_DOOR:
198 case SW_SHUTDOOR:
199 if (!sw[i].d)
200 break;
201 if (fld[sw[i].b][sw[i].a] != 3) {
202 sw[i].d = 0;
203 break;
205 if (--sw[i].d == 0) {
206 if (!shutdoor(i))
207 sw[i].d = 9;
209 break;
211 case SW_TRAP:
212 if (!sw[i].d)
213 break;
214 if (fld[sw[i].b][sw[i].a] != 2) {
215 sw[i].d = 0;
216 break;
218 if (--sw[i].d == 0) {
219 opendoor(i);
220 sw[i].tm = 18;
222 break;
226 } /* SW_act */
228 static int doortime(int t) {
229 switch (t) {
230 case SW_DOOR5:
231 return 90;
233 return 0;
236 void SW_cheat_open(void) {
237 int i;
239 for (i = 0; i < MAXSW; ++i) {
240 if (sw[i].t && !sw[i].tm) {
241 switch (sw[i].t) {
242 case SW_DOOR:
243 case SW_DOOR5:
244 case SW_OPENDOOR:
245 if (fld[sw[i].b][sw[i].a] != 2)
246 break;
247 SW_press(sw[i].x * CELW + 4, sw[i].y * CELH + 4, 1, 1, 0xFF, -3);
248 break;
254 int SW_press(int x, int y, int r, int h, byte t, int o) {
255 int sx, sy, i, p;
257 sx = (x - r) / CELW;
258 sy = (y - h + 1) / CELH;
259 x = (x + r) / CELW;
260 y /= CELH;
261 for (i = p = 0; i < MAXSW; ++i) {
262 if (sw[i].t && !sw[i].tm) {
263 if ((sw[i].x >= sx) && (sw[i].x <= x) && (sw[i].y >= sy) && (sw[i].y <= y) && ((sw[i].f & 0x8F) & t)) {
264 if (sw[i].f & 0x70) {
265 if ((sw[i].f & (t & 0x70)) != (sw[i].f & 0x70))
266 continue;
268 switch (sw[i].t) {
269 case SW_EXIT:
270 g_exit = 1;
271 sw[i].tm = 9;
272 swsnd = Z_sound(sndswx, 128);
273 break;
275 case SW_EXITS:
276 g_exit = 2;
277 sw[i].tm = 9;
278 swsnd = Z_sound(sndswx, 128);
279 break;
281 case SW_DOOR:
282 case SW_DOOR5:
283 switch (fld[sw[i].b][sw[i].a]) {
284 case 2:
285 opendoor(i);
286 sw[i].tm = 9;
287 sw[i].d = doortime(sw[i].t);
288 break;
290 case 3:
291 if (shutdoor(i)) {
292 sw[i].tm = 9;
293 sw[i].d = 0;
294 } else {
295 if (!swsnd)
296 swsnd = Z_sound(sndnoway, 128);
297 sw[i].d = 2;
299 break;
301 break;
303 case SW_PRESS:
304 sw[i].tm = 9;
305 SW_press((dword) sw[i].a * 8 + 4, (dword) sw[i].b * 8 + 12, 8, 16, (t & 0x70) | 0x80, o);
306 break;
308 case SW_TELE:
309 if (o < -2)
310 break;
311 if (!Z_canfit((dword) sw[i].a * 8 + 4, (dword) sw[i].b * 8 + 7, r, h)) {
312 if (!swsnd)
313 swsnd = Z_sound(sndnotele, 128);
314 break;
316 Z_teleobj(o, (dword) sw[i].a * 8 + 4, (dword) sw[i].b * 8 + 7);
317 sw[i].tm = 1;
318 break;
320 case SW_OPENDOOR:
321 if (fld[sw[i].b][sw[i].a] != 2)
322 break;
323 opendoor(i);
324 sw[i].tm = 1;
325 break;
327 case SW_SHUTDOOR:
328 if (fld[sw[i].b][sw[i].a] != 3)
329 break;
330 if (shutdoor(i)) {
331 sw[i].tm = 1;
332 sw[i].d = 0;
333 } else {
334 if (!swsnd)
335 swsnd = Z_sound(sndnoway, 128);
336 sw[i].d = 2;
338 break;
340 case SW_SHUTTRAP:
341 case SW_TRAP:
342 if (fld[sw[i].b][sw[i].a] != 3)
343 break;
344 cht = 3;
345 chto = 255;
346 chf = fldf[sw[i].b][sw[i].a];
347 f_ch = 1;
348 door(sw[i].a, sw[i].b);
349 Z_chktrap(1, 100, -3, HIT_TRAP);
350 cht = 255;
351 chto = 2;
352 door(sw[i].a, sw[i].b);
353 fld_need_remap = 1;
354 swsnd = Z_sound(sndswn, 128);
355 sw[i].tm = 1;
356 sw[i].d = 20;
357 break;
359 case SW_LIFT:
360 if (fld[sw[i].b][sw[i].a] == 10) {
361 cht = 10;
362 chto = 9;
363 f_ch = 0;
364 } else if (fld[sw[i].b][sw[i].a] == 9) {
365 cht = 9;
366 chto = 10;
367 f_ch = 0;
368 } else {
369 break;
371 door(sw[i].a, sw[i].b);
372 fld_need_remap = 1;
373 swsnd = Z_sound(sndswx, 128);
374 sw[i].tm = 9;
375 break;
377 case SW_LIFTUP:
378 if (fld[sw[i].b][sw[i].a] != 10)
379 break;
380 cht = 10;
381 chto = 9;
382 f_ch = 0;
383 door(sw[i].a, sw[i].b);
384 fld_need_remap = 1;
385 swsnd = Z_sound(sndswx, 128);
386 sw[i].tm = 1;
387 break;
389 case SW_LIFTDOWN:
390 if (fld[sw[i].b][sw[i].a] != 9)
391 break;
392 cht = 9;
393 chto = 10;
394 f_ch = 0;
395 door(sw[i].a, sw[i].b);
396 fld_need_remap = 1;
397 swsnd = Z_sound(sndswx, 128);
398 sw[i].tm = 1;
399 break;
401 case SW_SECRET:
402 if ((o != -1) && (o != -2))
403 break;
404 if (o == -1)
405 ++pl1.secrets;
406 else
407 ++pl2.secrets;
408 sw[i].tm = 1;
409 sw[i].t = 0;
410 break;
412 if (sw[i].tm) {
413 fldb[sw[i].y][sw[i].x] = walswp[fldb[sw[i].y][sw[i].x]];
414 p = 1;
416 if (sw[i].tm == 1)
417 sw[i].tm = 0;
421 return p;
422 } /* SW_press */