DEADSOFTWARE

719f0dd3635ef9988c621211f3c289096a6d8a0c
[flatwaifu.git] / src / switch.c
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: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
21 */
23 #include "glob.h"
24 #include <string.h>
25 #include "view.h"
26 #include "bmap.h"
27 #include "switch.h"
28 #include "player.h"
29 #include "misc.h"
30 #include "map.h"
31 #include "files.h"
32 #include "game.h"
33 #include "my.h"
34 #include "monster.h"
35 #include "render.h"
37 int sw_secrets;
38 sw_t sw[MAXSW];
40 static void *sndswn, *sndswx, *sndnoway, *sndbdo, *sndbdc, *sndnotele;
41 static int swsnd;
42 static byte cht, chto, chf, f_ch;
44 void SW_alloc (void) {
45 sndswn=Z_getsnd("SWTCHN");
46 sndswx=Z_getsnd("SWTCHX");
47 sndnoway=Z_getsnd("NOWAY");
48 sndbdo=Z_getsnd("BDOPN");
49 sndbdc=Z_getsnd("BDCLS");
50 sndnotele=Z_getsnd("NOTELE");
51 }
53 void SW_init (void) {
54 int i;
55 for (i = 0; i < MAXSW; i++) {
56 sw[i].t = 0;
57 }
58 swsnd = 0;
59 }
61 static void door(byte x,byte y) {
62 byte ex;
64 if(x>=FLDW || y>=FLDH) return;
65 if(fld[y][x]!=cht) return;
66 ex=x+1;
67 for(;x && fld[y][x-1]==cht;--x);
68 for(;ex<FLDW && fld[y][ex]==cht;++ex);
69 memset(fld[y]+x,chto,ex-x);
70 if(f_ch) memset(fldf[y]+x,chf,ex-x);
71 for(;x<ex;++x) {
72 door(x,y-1);
73 door(x,y+1);
74 }
75 }
77 void Z_water_trap (obj_t *o) {
78 int i,j,sx,sy,x,y;
80 if((y=o->y)>=FLDH*CELH+o->h) return;
81 if((x=o->x)<0 || o->x>FLDW*CELW) return;
82 sx=(x-o->r)/CELW;
83 sy=(y-o->h+1)/CELH;
84 x=(x+o->r)/CELW;
85 y=(y-o->h/2)/CELH;
86 for(i=sx;i<=x;++i)
87 for(j=sy;j<=y;++j)
88 if(fld[j][i]==5) {
89 cht=5;chto=255;f_ch=0;
90 door(i,j);
91 }
92 }
94 void Z_untrap (byte t) {
95 byte *p;
96 word n;
98 for(p=(byte*)fld,n=FLDW*FLDH;n;--n,++p)
99 if(*p==255) *p=t;
102 static void opendoor(int i) {
103 int j;
105 swsnd=Z_sound(sndbdo,128);
106 j=fldf[sw[i].b][sw[i].a];
107 cht=2;chto=3;chf=0;f_ch=1;
108 door(sw[i].a,sw[i].b);
109 fldf[sw[i].b][sw[i].a]=j;
110 fld_need_remap=1;
113 static int shutdoor(int i) {
114 int j;
116 cht=3;chto=255;chf=fldf[sw[i].b][sw[i].a];f_ch=1;
117 door(sw[i].a,sw[i].b);
118 cht=255;
119 if(Z_chktrap(0,0,-3,HIT_SOME)) {
120 j=fldf[sw[i].b][sw[i].a];
121 chto=3;chf=0;f_ch=1;
122 door(sw[i].a,sw[i].b);
123 fldf[sw[i].b][sw[i].a]=j;
124 return 0;
126 chto=2;
127 door(sw[i].a,sw[i].b);
128 fld_need_remap=1;
129 swsnd=Z_sound(sndbdc,128);
130 return 1;
133 void SW_act (void) {
134 int i;
136 if(swsnd) --swsnd;
137 for(i=0;i<MAXSW;++i) if(sw[i].t) {
138 if(sw[i].tm) --sw[i].tm;
139 switch(sw[i].t) {
140 case SW_DOOR5: case SW_DOOR: case SW_SHUTDOOR:
141 if(!sw[i].d) break;
142 if(fld[sw[i].b][sw[i].a]!=3) {sw[i].d=0;break;}
143 if(--sw[i].d==0) if(!shutdoor(i)) sw[i].d=9;
144 break;
145 case SW_TRAP:
146 if(!sw[i].d) break;
147 if(fld[sw[i].b][sw[i].a]!=2) {sw[i].d=0;break;}
148 if(--sw[i].d==0) {opendoor(i);sw[i].tm=18;}
149 break;
154 static int doortime(int t) {
155 switch(t) {
156 case SW_DOOR5: return 90;
158 return 0;
161 void SW_cheat_open (void) {
162 int i;
164 for(i=0;i<MAXSW;++i) if(sw[i].t && !sw[i].tm) switch(sw[i].t) {
165 case SW_DOOR: case SW_DOOR5:
166 case SW_OPENDOOR:
167 if(fld[sw[i].b][sw[i].a]!=2) break;
168 SW_press(sw[i].x*CELW+4,sw[i].y*CELH+4,1,1,0xFF,-3);
169 break;
173 int SW_press (int x, int y, int r, int h, byte t, int o) {
174 int sx,sy,i,p;
176 sx=(x-r)/CELW;sy=(y-h+1)/CELH;
177 x=(x+r)/CELW;y/=CELH;
178 for(i=p=0;i<MAXSW;++i) if(sw[i].t && !sw[i].tm) {
179 if(sw[i].x>=sx && sw[i].x<=x && sw[i].y>=sy && sw[i].y<=y && ((sw[i].f&0x8F)&t)) {
180 if(sw[i].f&0x70) if((sw[i].f&(t&0x70))!=(sw[i].f&0x70)) continue;
181 switch(sw[i].t) {
182 case SW_EXIT:
183 g_exit=1;sw[i].tm=9;swsnd=Z_sound(sndswx,128);break;
184 case SW_EXITS:
185 g_exit=2;sw[i].tm=9;swsnd=Z_sound(sndswx,128);break;
186 case SW_DOOR: case SW_DOOR5:
187 switch(fld[sw[i].b][sw[i].a]) {
188 case 2:
189 opendoor(i);sw[i].tm=9;sw[i].d=doortime(sw[i].t);break;
190 case 3:
191 if(shutdoor(i)) {sw[i].tm=9;sw[i].d=0;}
192 else {
193 if(!swsnd) swsnd=Z_sound(sndnoway,128);
194 sw[i].d=2;
195 }break;
196 }break;
197 case SW_PRESS:
198 sw[i].tm=9;
199 SW_press((dword)sw[i].a*8+4,(dword)sw[i].b*8+12,8,16,(t&0x70)|0x80,o);
200 break;
201 case SW_TELE:
202 if(o < -2) break;
203 if(!Z_canfit((dword)sw[i].a*8+4,(dword)sw[i].b*8+7,r,h)) {
204 if(!swsnd) swsnd=Z_sound(sndnotele,128);
205 break;
206 }Z_teleobj(o,(dword)sw[i].a*8+4,(dword)sw[i].b*8+7);
207 sw[i].tm=1;
208 break;
209 case SW_OPENDOOR:
210 if(fld[sw[i].b][sw[i].a]!=2) break;
211 opendoor(i);
212 sw[i].tm=1;
213 break;
214 case SW_SHUTDOOR:
215 if(fld[sw[i].b][sw[i].a]!=3) break;
216 if(shutdoor(i)) {sw[i].tm=1;sw[i].d=0;}
217 else {
218 if(!swsnd) swsnd=Z_sound(sndnoway,128);
219 sw[i].d=2;
220 }break;
221 case SW_SHUTTRAP: case SW_TRAP:
222 if(fld[sw[i].b][sw[i].a]!=3) break;
223 cht=3;chto=255;chf=fldf[sw[i].b][sw[i].a];f_ch=1;
224 door(sw[i].a,sw[i].b);
225 Z_chktrap(1,100,-3,HIT_TRAP);
226 cht=255;chto=2;
227 door(sw[i].a,sw[i].b);
228 fld_need_remap=1;
229 swsnd=Z_sound(sndswn,128);
230 sw[i].tm=1;sw[i].d=20;
231 break;
232 case SW_LIFT:
233 if(fld[sw[i].b][sw[i].a]==10) {
234 cht=10;chto=9;f_ch=0;
235 }else if(fld[sw[i].b][sw[i].a]==9) {
236 cht=9;chto=10;f_ch=0;
237 }else break;
238 door(sw[i].a,sw[i].b);
239 fld_need_remap=1;
240 swsnd=Z_sound(sndswx,128);
241 sw[i].tm=9;
242 break;
243 case SW_LIFTUP:
244 if(fld[sw[i].b][sw[i].a]!=10) break;
245 cht=10;chto=9;f_ch=0;
246 door(sw[i].a,sw[i].b);
247 fld_need_remap=1;
248 swsnd=Z_sound(sndswx,128);
249 sw[i].tm=1;
250 break;
251 case SW_LIFTDOWN:
252 if(fld[sw[i].b][sw[i].a]!=9) break;
253 cht=9;chto=10;f_ch=0;
254 door(sw[i].a,sw[i].b);
255 fld_need_remap=1;
256 swsnd=Z_sound(sndswx,128);
257 sw[i].tm=1;
258 break;
259 case SW_SECRET:
260 if(o!=-1 && o!=-2) break;
261 if(o==-1) ++pl1.secrets;
262 else ++pl2.secrets;
263 sw[i].tm=1;sw[i].t=0;break;
265 if (sw[i].tm != 0) {
266 R_switch_texture(sw[i].x, sw[i].y);
267 p = 1;
269 if(sw[i].tm==1) sw[i].tm=0;
272 return p;