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:Rembo project.
8 Doom2D:Rembo 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:Rembo 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
40 static void *sndswn
, *sndswx
, *sndnoway
, *sndbdo
, *sndbdc
, *sndnotele
;
42 static byte cht
, chto
, chf
, f_ch
;
44 int SW_load (FILE *h
) {
49 for (i
= 0; i
< MAXSW
&& blk
.sz
> 0; ++i
, blk
.sz
-= 9) {
50 sw
[i
].x
= myfread8(h
);
51 sw
[i
].y
= myfread8(h
);
52 sw
[i
].t
= myfread8(h
);
53 sw
[i
].tm
= myfread8(h
); // unused
54 sw
[i
].a
= myfread8(h
);
55 sw
[i
].b
= myfread8(h
);
56 sw
[i
].c
= myfread8(h
);
57 sw
[i
].d
= myfread8(h
); // unused
58 sw
[i
].f
= myfread8(h
);
62 if (sw
[i
].t
== SW_SECRET
) {
71 void SW_alloc (void) {
72 sndswn
=Z_getsnd("SWTCHN");
73 sndswx
=Z_getsnd("SWTCHX");
74 sndnoway
=Z_getsnd("NOWAY");
75 sndbdo
=Z_getsnd("BDOPN");
76 sndbdc
=Z_getsnd("BDCLS");
77 sndnotele
=Z_getsnd("NOTELE");
82 for (i
= 0; i
< MAXSW
; i
++) {
88 static void door(byte x
,byte y
) {
91 if(x
>=FLDW
|| y
>=FLDH
) return;
92 if(fld
[y
][x
]!=cht
) return;
94 for(;x
&& fld
[y
][x
-1]==cht
;--x
);
95 for(;ex
<FLDW
&& fld
[y
][ex
]==cht
;++ex
);
96 memset(fld
[y
]+x
,chto
,ex
-x
);
97 if(f_ch
) memset(fldf
[y
]+x
,chf
,ex
-x
);
104 void Z_water_trap (obj_t
*o
) {
107 if((y
=o
->y
)>=FLDH
*CELH
+o
->h
) return;
108 if((x
=o
->x
)<0 || o
->x
>FLDW
*CELW
) return;
116 cht
=5;chto
=255;f_ch
=0;
121 void Z_untrap (byte t
) {
125 for(p
=(byte
*)fld
,n
=FLDW
*FLDH
;n
;--n
,++p
)
129 static void opendoor(int i
) {
132 swsnd
=Z_sound(sndbdo
,128);
133 j
=fldf
[sw
[i
].b
][sw
[i
].a
];
134 cht
=2;chto
=3;chf
=0;f_ch
=1;
135 door(sw
[i
].a
,sw
[i
].b
);
136 fldf
[sw
[i
].b
][sw
[i
].a
]=j
;
140 static int shutdoor(int i
) {
143 cht
=3;chto
=255;chf
=fldf
[sw
[i
].b
][sw
[i
].a
];f_ch
=1;
144 door(sw
[i
].a
,sw
[i
].b
);
146 if(Z_chktrap(0,0,-3,HIT_SOME
)) {
147 j
=fldf
[sw
[i
].b
][sw
[i
].a
];
149 door(sw
[i
].a
,sw
[i
].b
);
150 fldf
[sw
[i
].b
][sw
[i
].a
]=j
;
154 door(sw
[i
].a
,sw
[i
].b
);
156 swsnd
=Z_sound(sndbdc
,128);
164 for(i
=0;i
<MAXSW
;++i
) if(sw
[i
].t
) {
165 if(sw
[i
].tm
) --sw
[i
].tm
;
167 case SW_DOOR5
: case SW_DOOR
: case SW_SHUTDOOR
:
169 if(fld
[sw
[i
].b
][sw
[i
].a
]!=3) {sw
[i
].d
=0;break;}
170 if(--sw
[i
].d
==0) if(!shutdoor(i
)) sw
[i
].d
=9;
174 if(fld
[sw
[i
].b
][sw
[i
].a
]!=2) {sw
[i
].d
=0;break;}
175 if(--sw
[i
].d
==0) {opendoor(i
);sw
[i
].tm
=18;}
181 static int doortime(int t
) {
183 case SW_DOOR5
: return 90;
188 void SW_cheat_open (void) {
191 for(i
=0;i
<MAXSW
;++i
) if(sw
[i
].t
&& !sw
[i
].tm
) switch(sw
[i
].t
) {
192 case SW_DOOR
: case SW_DOOR5
:
194 if(fld
[sw
[i
].b
][sw
[i
].a
]!=2) break;
195 SW_press(sw
[i
].x
*CELW
+4,sw
[i
].y
*CELH
+4,1,1,0xFF,-3);
200 int SW_press (int x
, int y
, int r
, int h
, byte t
, int o
) {
203 sx
=(x
-r
)/CELW
;sy
=(y
-h
+1)/CELH
;
204 x
=(x
+r
)/CELW
;y
/=CELH
;
205 for(i
=p
=0;i
<MAXSW
;++i
) if(sw
[i
].t
&& !sw
[i
].tm
) {
206 if(sw
[i
].x
>=sx
&& sw
[i
].x
<=x
&& sw
[i
].y
>=sy
&& sw
[i
].y
<=y
&& ((sw
[i
].f
&0x8F)&t
)) {
207 if(sw
[i
].f
&0x70) if((sw
[i
].f
&(t
&0x70))!=(sw
[i
].f
&0x70)) continue;
210 g_exit
=1;sw
[i
].tm
=9;swsnd
=Z_sound(sndswx
,128);break;
212 g_exit
=2;sw
[i
].tm
=9;swsnd
=Z_sound(sndswx
,128);break;
213 case SW_DOOR
: case SW_DOOR5
:
214 switch(fld
[sw
[i
].b
][sw
[i
].a
]) {
216 opendoor(i
);sw
[i
].tm
=9;sw
[i
].d
=doortime(sw
[i
].t
);break;
218 if(shutdoor(i
)) {sw
[i
].tm
=9;sw
[i
].d
=0;}
220 if(!swsnd
) swsnd
=Z_sound(sndnoway
,128);
226 SW_press((dword
)sw
[i
].a
*8+4,(dword
)sw
[i
].b
*8+12,8,16,(t
&0x70)|0x80,o
);
230 if(!Z_canfit((dword
)sw
[i
].a
*8+4,(dword
)sw
[i
].b
*8+7,r
,h
)) {
231 if(!swsnd
) swsnd
=Z_sound(sndnotele
,128);
233 }Z_teleobj(o
,(dword
)sw
[i
].a
*8+4,(dword
)sw
[i
].b
*8+7);
237 if(fld
[sw
[i
].b
][sw
[i
].a
]!=2) break;
242 if(fld
[sw
[i
].b
][sw
[i
].a
]!=3) break;
243 if(shutdoor(i
)) {sw
[i
].tm
=1;sw
[i
].d
=0;}
245 if(!swsnd
) swsnd
=Z_sound(sndnoway
,128);
248 case SW_SHUTTRAP
: case SW_TRAP
:
249 if(fld
[sw
[i
].b
][sw
[i
].a
]!=3) break;
250 cht
=3;chto
=255;chf
=fldf
[sw
[i
].b
][sw
[i
].a
];f_ch
=1;
251 door(sw
[i
].a
,sw
[i
].b
);
252 Z_chktrap(1,100,-3,HIT_TRAP
);
254 door(sw
[i
].a
,sw
[i
].b
);
256 swsnd
=Z_sound(sndswn
,128);
257 sw
[i
].tm
=1;sw
[i
].d
=20;
260 if(fld
[sw
[i
].b
][sw
[i
].a
]==10) {
261 cht
=10;chto
=9;f_ch
=0;
262 }else if(fld
[sw
[i
].b
][sw
[i
].a
]==9) {
263 cht
=9;chto
=10;f_ch
=0;
265 door(sw
[i
].a
,sw
[i
].b
);
267 swsnd
=Z_sound(sndswx
,128);
271 if(fld
[sw
[i
].b
][sw
[i
].a
]!=10) break;
272 cht
=10;chto
=9;f_ch
=0;
273 door(sw
[i
].a
,sw
[i
].b
);
275 swsnd
=Z_sound(sndswx
,128);
279 if(fld
[sw
[i
].b
][sw
[i
].a
]!=9) break;
280 cht
=9;chto
=10;f_ch
=0;
281 door(sw
[i
].a
,sw
[i
].b
);
283 swsnd
=Z_sound(sndswx
,128);
287 if(o
!=-1 && o
!=-2) break;
288 if(o
==-1) ++pl1
.secrets
;
290 sw
[i
].tm
=1;sw
[i
].t
=0;break;
293 R_switch_texture(sw
[i
].x
, sw
[i
].y
);
296 if(sw
[i
].tm
==1) sw
[i
].tm
=0;