DEADSOFTWARE

Cleanup includes
[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"
32 #define MAXSW 100
34 extern map_block_t blk;
36 #pragma pack(1)
37 typedef struct{
38 byte x,y;
39 byte t,tm;
40 byte a,b,c,d;
41 byte f;
42 }sw_t;
43 #pragma pack()
45 static sw_t sw[MAXSW];
47 static void *sndswn,*sndswx,*sndnoway,*sndbdo,*sndbdc,*sndnotele;
48 static int swsnd;
50 int sw_secrets;
52 void SW_savegame(FILE* h) {
53 int n;
55 for(n=MAXSW;--n;) if(sw[n].t) break;
56 ++n;myfwrite(&n,1,4,h);myfwrite(sw,1,n*sizeof(sw[0]),h);
57 myfwrite(&sw_secrets,1,4,h);
58 }
60 void SW_loadgame(FILE* h) {
61 int n;
63 myfread(&n,1,4,h);myfread(sw,1,n*sizeof(sw[0]),h);
64 myfread(&sw_secrets,1,4,h);
65 }
67 int SW_load(FILE* h) {
68 int i;
70 switch(blk.t) {
71 case MB_SWITCH2:
72 sw_secrets=0;
73 for(i=0;i<MAXSW && blk.sz>0;++i,blk.sz-=sizeof(sw_t)) {
74 myfread(sw+i,1,sizeof(sw_t),h);
75 sw[i].tm=0;sw[i].d=0;
76 sw[i].f|=0x80;
77 if(sw[i].t==SW_SECRET) ++sw_secrets;
78 }
79 return 1;
80 }return 0;
81 }
83 void SW_alloc(void) {
84 sndswn=Z_getsnd("SWTCHN");
85 sndswx=Z_getsnd("SWTCHX");
86 sndnoway=Z_getsnd("NOWAY");
87 sndbdo=Z_getsnd("BDOPN");
88 sndbdc=Z_getsnd("BDCLS");
89 sndnotele=Z_getsnd("NOTELE");
90 }
92 void SW_init(void) {
93 int i;
95 for(i=0;i<MAXSW;++i) sw[i].t=0;
96 swsnd=0;
97 }
99 static byte cht,chto,chf,f_ch;
101 static void door(byte x,byte y) {
102 byte ex;
104 if(x>=FLDW || y>=FLDH) return;
105 if(fld[y][x]!=cht) return;
106 ex=x+1;
107 for(;x && fld[y][x-1]==cht;--x);
108 for(;ex<FLDW && fld[y][ex]==cht;++ex);
109 memset(fld[y]+x,chto,ex-x);
110 if(f_ch) memset(fldf[y]+x,chf,ex-x);
111 for(;x<ex;++x) {
112 door(x,y-1);
113 door(x,y+1);
117 void Z_water_trap(obj_t *o) {
118 int i,j,sx,sy,x,y;
120 if((y=o->y)>=FLDH*CELH+o->h) return;
121 if((x=o->x)<0 || o->x>FLDW*CELW) return;
122 sx=(x-o->r)/CELW;
123 sy=(y-o->h+1)/CELH;
124 x=(x+o->r)/CELW;
125 y=(y-o->h/2)/CELH;
126 for(i=sx;i<=x;++i)
127 for(j=sy;j<=y;++j)
128 if(fld[j][i]==5) {
129 cht=5;chto=255;f_ch=0;
130 door(i,j);
134 void Z_untrap(byte t) {
135 byte *p;
136 word n;
138 for(p=(byte*)fld,n=FLDW*FLDH;n;--n,++p)
139 if(*p==255) *p=t;
142 static void opendoor(int i) {
143 int j;
145 swsnd=Z_sound(sndbdo,128);
146 j=fldf[sw[i].b][sw[i].a];
147 cht=2;chto=3;chf=0;f_ch=1;
148 door(sw[i].a,sw[i].b);
149 fldf[sw[i].b][sw[i].a]=j;
150 fld_need_remap=1;
153 static int shutdoor(int i) {
154 int j;
156 cht=3;chto=255;chf=fldf[sw[i].b][sw[i].a];f_ch=1;
157 door(sw[i].a,sw[i].b);
158 cht=255;
159 if(Z_chktrap(0,0,-3,HIT_SOME)) {
160 j=fldf[sw[i].b][sw[i].a];
161 chto=3;chf=0;f_ch=1;
162 door(sw[i].a,sw[i].b);
163 fldf[sw[i].b][sw[i].a]=j;
164 return 0;
166 chto=2;
167 door(sw[i].a,sw[i].b);
168 fld_need_remap=1;
169 swsnd=Z_sound(sndbdc,128);
170 return 1;
173 void SW_act(void) {
174 int i;
176 if(swsnd) --swsnd;
177 for(i=0;i<MAXSW;++i) if(sw[i].t) {
178 if(sw[i].tm) --sw[i].tm;
179 switch(sw[i].t) {
180 case SW_DOOR5: case SW_DOOR: case SW_SHUTDOOR:
181 if(!sw[i].d) break;
182 if(fld[sw[i].b][sw[i].a]!=3) {sw[i].d=0;break;}
183 if(--sw[i].d==0) if(!shutdoor(i)) sw[i].d=9;
184 break;
185 case SW_TRAP:
186 if(!sw[i].d) break;
187 if(fld[sw[i].b][sw[i].a]!=2) {sw[i].d=0;break;}
188 if(--sw[i].d==0) {opendoor(i);sw[i].tm=18;}
189 break;
194 static int doortime(int t) {
195 switch(t) {
196 case SW_DOOR5: return 90;
198 return 0;
201 void SW_cheat_open(void) {
202 int i;
204 for(i=0;i<MAXSW;++i) if(sw[i].t && !sw[i].tm) switch(sw[i].t) {
205 case SW_DOOR: case SW_DOOR5:
206 case SW_OPENDOOR:
207 if(fld[sw[i].b][sw[i].a]!=2) break;
208 SW_press(sw[i].x*CELW+4,sw[i].y*CELH+4,1,1,0xFF,-3);
209 break;
213 int SW_press(int x,int y,int r,int h,byte t,int o) {
214 int sx,sy,i,p;
216 sx=(x-r)/CELW;sy=(y-h+1)/CELH;
217 x=(x+r)/CELW;y/=CELH;
218 for(i=p=0;i<MAXSW;++i) if(sw[i].t && !sw[i].tm) {
219 if(sw[i].x>=sx && sw[i].x<=x && sw[i].y>=sy && sw[i].y<=y && ((sw[i].f&0x8F)&t)) {
220 if(sw[i].f&0x70) if((sw[i].f&(t&0x70))!=(sw[i].f&0x70)) continue;
221 switch(sw[i].t) {
222 case SW_EXIT:
223 g_exit=1;sw[i].tm=9;swsnd=Z_sound(sndswx,128);break;
224 case SW_EXITS:
225 g_exit=2;sw[i].tm=9;swsnd=Z_sound(sndswx,128);break;
226 case SW_DOOR: case SW_DOOR5:
227 switch(fld[sw[i].b][sw[i].a]) {
228 case 2:
229 opendoor(i);sw[i].tm=9;sw[i].d=doortime(sw[i].t);break;
230 case 3:
231 if(shutdoor(i)) {sw[i].tm=9;sw[i].d=0;}
232 else {
233 if(!swsnd) swsnd=Z_sound(sndnoway,128);
234 sw[i].d=2;
235 }break;
236 }break;
237 case SW_PRESS:
238 sw[i].tm=9;
239 SW_press((dword)sw[i].a*8+4,(dword)sw[i].b*8+12,8,16,(t&0x70)|0x80,o);
240 break;
241 case SW_TELE:
242 if(o < -2) break;
243 if(!Z_canfit((dword)sw[i].a*8+4,(dword)sw[i].b*8+7,r,h)) {
244 if(!swsnd) swsnd=Z_sound(sndnotele,128);
245 break;
246 }Z_teleobj(o,(dword)sw[i].a*8+4,(dword)sw[i].b*8+7);
247 sw[i].tm=1;
248 break;
249 case SW_OPENDOOR:
250 if(fld[sw[i].b][sw[i].a]!=2) break;
251 opendoor(i);
252 sw[i].tm=1;
253 break;
254 case SW_SHUTDOOR:
255 if(fld[sw[i].b][sw[i].a]!=3) break;
256 if(shutdoor(i)) {sw[i].tm=1;sw[i].d=0;}
257 else {
258 if(!swsnd) swsnd=Z_sound(sndnoway,128);
259 sw[i].d=2;
260 }break;
261 case SW_SHUTTRAP: case SW_TRAP:
262 if(fld[sw[i].b][sw[i].a]!=3) break;
263 cht=3;chto=255;chf=fldf[sw[i].b][sw[i].a];f_ch=1;
264 door(sw[i].a,sw[i].b);
265 Z_chktrap(1,100,-3,HIT_TRAP);
266 cht=255;chto=2;
267 door(sw[i].a,sw[i].b);
268 fld_need_remap=1;
269 swsnd=Z_sound(sndswn,128);
270 sw[i].tm=1;sw[i].d=20;
271 break;
272 case SW_LIFT:
273 if(fld[sw[i].b][sw[i].a]==10) {
274 cht=10;chto=9;f_ch=0;
275 }else if(fld[sw[i].b][sw[i].a]==9) {
276 cht=9;chto=10;f_ch=0;
277 }else break;
278 door(sw[i].a,sw[i].b);
279 fld_need_remap=1;
280 swsnd=Z_sound(sndswx,128);
281 sw[i].tm=9;
282 break;
283 case SW_LIFTUP:
284 if(fld[sw[i].b][sw[i].a]!=10) break;
285 cht=10;chto=9;f_ch=0;
286 door(sw[i].a,sw[i].b);
287 fld_need_remap=1;
288 swsnd=Z_sound(sndswx,128);
289 sw[i].tm=1;
290 break;
291 case SW_LIFTDOWN:
292 if(fld[sw[i].b][sw[i].a]!=9) break;
293 cht=9;chto=10;f_ch=0;
294 door(sw[i].a,sw[i].b);
295 fld_need_remap=1;
296 swsnd=Z_sound(sndswx,128);
297 sw[i].tm=1;
298 break;
299 case SW_SECRET:
300 if(o!=-1 && o!=-2) break;
301 if(o==-1) ++pl1.secrets;
302 else ++pl2.secrets;
303 sw[i].tm=1;sw[i].t=0;break;
305 if(sw[i].tm)
306 {fldb[sw[i].y][sw[i].x]=walswp[fldb[sw[i].y][sw[i].x]];p=1;}
307 if(sw[i].tm==1) sw[i].tm=0;
310 return p;