DEADSOFTWARE

13359cd39ccb63d1707e2091cc14861a12eaa702
[flatwaifu.git] / src / miscc.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 <stdio.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include "files.h"
29 #include "memory.h"
30 #include "error.h"
31 #include "vga.h"
32 #include "sound.h"
33 #include "view.h"
34 #include "bmap.h"
35 #include "dots.h"
36 #include "monster.h"
37 #include "misc.h"
39 //#define WD 200
40 //#define HT 98
42 #define MAX_YV 30
44 #define MAXAIR 1091
46 extern dword walf[256];
48 byte z_dot=0;
50 extern void *walp[256];
52 static void *sth[22],*bfh[160-'!'],*sfh[160-'!'],*bulsnd[2],*stone, *stone2, *keys[3];
53 static int prx=0,pry=0;
55 int Z_sign(int a) {
56 if(a>0) return 1;
57 if(a<0) return -1;
58 return 0;
59 }
61 int Z_dec(int a,int b) {
62 if(abs(a)<=b) return 0;
63 if(a>0) return a-b;
64 if(a<0) return a+b;
65 return 0;
66 }
68 void *Z_getspr(char n[4],int s,int d,char *dir) {
69 int h;
71 h=F_getsprid(n,s,d);
72 if(dir) *dir=(h&0x8000)?1:0;
73 return M_lock(h);
74 }
76 void *Z_getsnd(char n[6]) {
77 char s[8];
79 //if(snd_type==-1) return NULL;
80 strncpy(s+2,n,6);s[0]='D';
81 s[1]='S';
82 return M_lock(F_getresid(s));
83 }
85 int Z_sound(void *s,int v) {
86 //if(snd_type==-1) return 0;
87 if(!s) return 0;
88 S_play(s,-1,1024,v);
89 return F_getreslen(((int*)s)[-1])/605;
90 }
92 #define GAS_START (MN__LAST-MN_DEMON+5)
93 #define GAS_TOTAL (MN__LAST-MN_DEMON+16+10)
95 void Z_initst(void) {
96 int i;
97 char s[10];
98 static char nm[22][8]={
99 "STTNUM0","STTNUM1","STTNUM2","STTNUM3","STTNUM4",
100 "STTNUM5","STTNUM6","STTNUM7","STTNUM8","STTNUM9",
101 "STTMINUS","STTPRCNT",
102 "FISTA0","CSAWA0","PISTA0","SHOTA0","SGN2A0","MGUNA0","LAUNA0",
103 "PLASA0","BFUGA0","GUN2A0"
104 };
106 stone=M_lock(F_getresid("STONE"));
107 stone2=M_lock(F_getresid("STONE2"));
108 keys[0]=M_lock(F_getresid("KEYRA0"));
109 keys[1]=M_lock(F_getresid("KEYGA0"));
110 keys[2]=M_lock(F_getresid("KEYBA0"));
111 for(i=0;i<22;++i)
112 sth[i]=M_lock(F_getresid(nm[i]));
113 strcpy(s,"STBF_*");
114 for(i='!';i<160;++i) {
115 s[5]=i;
116 bfh[i-'!']=M_lock(F_findres(s));
117 if(!(i&15)) logo_gas(GAS_START+((i-'!')>>4),GAS_TOTAL);
119 for(i='!';i<160;++i) {
120 sprintf(s,"STCFN%03d",i);
121 sfh[i-'!']=M_lock(F_findres(s));
122 if(!(i&15)) logo_gas(GAS_START+8+((i-'!')>>4),GAS_TOTAL);
124 strcpy(s,"WINUM*");
125 for(i='0';i<='9';++i) {
126 s[5]=i;
127 bfh[i-'!']=M_lock(F_getresid(s));
129 bfh[':'-'!']=M_lock(F_getresid("WICOLON"));
130 bulsnd[0]=Z_getsnd("BUL1");
131 bulsnd[1]=Z_getsnd("BUL2");
134 void Z_putbfch(int c) {
135 vgaimg *p;
137 if(c>32 && c<160) p=bfh[c-'!']; else p=NULL;
138 if(p) {
139 V_spr(prx,pry,p);
140 prx+=p->w-1;
141 }else prx+=12;
144 void Z_putsfch(int c) {
145 vgaimg *p;
147 if(c>32 && c<160) p=sfh[c-'!']; else p=NULL;
148 if(p) {
149 V_spr(prx,pry,p);
150 prx+=p->w-1;
151 }else prx+=7;
154 void Z_gotoxy(int x,int y) {prx=x;pry=y;}
156 void Z_printbf(char *s,...) {
157 int i;
158 va_list ap;
159 char buf[80];
161 va_start(ap,s);
162 vsprintf(buf,s,ap);
163 va_end(ap);
164 for(i=0;buf[i];++i) switch(buf[i]) {
165 case '\n':
166 pry+=13;
167 case '\r':
168 prx=0;break;
169 default:
170 Z_putbfch((byte)buf[i]);
174 void Z_printsf(char *s,...) {
175 int i;
176 va_list ap;
177 char buf[80];
179 va_start(ap,s);
180 vsprintf(buf,s,ap);
181 va_end(ap);
182 for(i=0;buf[i];++i) switch(buf[i]) {
183 case '\n':
184 pry+=8;
185 case '\r':
186 prx=0;break;
187 default:
188 Z_putsfch((byte)buf[i]);
192 void Z_drawspr(int x,int y,void *p,char d) {
193 if(d) V_spr2(x-w_x+WD/2,y-w_y+HT/2+1+w_o,p);//if(d) V_spr2(x-w_x+100,y-w_y+HT/2+1+w_o,p);
194 else V_spr(x-w_x+WD/2,y-w_y+HT/2+1+w_o,p);//else V_spr(x-w_x+100,y-w_y+HT/2+1+w_o,p);
197 void Z_clrst(void) {
198 V_pic(SCRW-120,w_o,stone);//V_pic(200,w_o,stone);
199 int y = ((vgaimg*)stone)->h;
200 while (y<HT) {
201 V_pic(SCRW-120,w_o+y,stone2);
202 y+=((vgaimg*)stone)->h;
207 void Z_drawstlives(char n) {
208 V_setrect(SCRW-40,30,w_o,40);Z_clrst();//V_setrect(280,30,w_o,40);Z_clrst();
209 V_spr(SCRW-35,w_o+17,sth[n]);//V_spr(285,w_o+17,sth[n]);
212 void Z_drawstkeys(byte k) {
213 int x,n;
215 V_setrect(SCRW-120,70,w_o+77,23);Z_clrst();//V_setrect(200,70,w_o+77,23);Z_clrst();
216 for(k>>=4,n=0,x=SCRW-75;n<3;++n,k>>=1,x+=9)//for(k>>=4,n=0,x=245;n<3;++n,k>>=1,x+=9)
217 if(k&1) V_spr(x,w_o+91,keys[n]);
220 void Z_drawstair(int a) {
221 V_setrect(SCRW-120,120,w_o+49,2);Z_clrst();//V_setrect(200,120,w_o+49,2);Z_clrst();
222 if(a<=0) return;
223 if(a>MAXAIR) a=MAXAIR;
224 a=a*100/MAXAIR;
225 if(!a) return;
226 V_clr(SCRW-110,a,w_o+49,2,0xC8);//V_clr(210,a,w_o+49,2,0xC8);
229 void Z_drawstprcnt(int y,int n) {
230 char s[20];
231 int l,i,x,c;
233 V_setrect(SCRW-120,70,y*19+7+w_o,19);Z_clrst();//V_setrect(200,70,y*19+7+w_o,19);Z_clrst();
234 sprintf(s,"%3d%%",n);
235 l=strlen(s);x=SCRW-110;//l=strlen(s);x=210;
236 for(i=0;i<l;++i,x+=14) {
237 if(s[i]>='0' && s[i]<='9') c=s[i]-'0';
238 else if(s[i]=='-') c=10;
239 else if(s[i]=='%') c=11;
240 else c=-1;
241 if(c>=0)
242 V_spr(x,y*19+7+w_o,sth[c]);
246 void Z_drawstnum(int n) {
247 char s[20];
248 int l,i,x,c;
250 V_setrect(SCRW-50,50,w_o+77,23);Z_clrst();//V_setrect(270,50,w_o+77,23);Z_clrst();
251 if(!g_dm) return;
252 sprintf(s,"%d",n);
253 l=strlen(s);x=(115-l*14)+SCRW-120;//l=strlen(s);x=(115-l*14)+200;
254 for(i=0;i<l;++i,x+=14) {
255 if(s[i]>='0' && s[i]<='9') c=s[i]-'0';
256 else if(s[i]=='-') c=10;
257 else if(s[i]=='%') c=11;
258 else c=-1;
259 if(c>=0)
260 V_spr(x,w_o+77+5,sth[c]);
264 void Z_drawstwpn(int n,int a) {
265 char s[20];
266 int l,i,x,c;
268 i=n;
269 V_setrect(SCRW-120,120,w_o+58,23);Z_clrst();//V_setrect(200,120,w_o+58,23);Z_clrst();
270 if(i>=0) V_spr(SCRW-88,w_o+58+19,sth[i+12]);//if(i>=0) V_spr(232,w_o+58+19,sth[i+12]);
271 if(n>=2) {
272 sprintf(s,"%d",a);
273 l=strlen(s);x=SCRW-10-l*14;//l=strlen(s);x=310-l*14;
274 for(i=0;i<l;++i,x+=14) {
275 if(s[i]>='0' && s[i]<='9') c=s[i]-'0';
276 else if(s[i]=='-') c=10;
277 else if(s[i]=='%') c=11;
278 else c=-1;
279 if(c>=0)
280 V_spr(x,w_o+58+2,sth[c]);
287 void Z_drawmanspr(int x,int y,void *p,char d,byte color) {
288 if(d) V_manspr2(x-w_x+WD/2,y-w_y+HT/2+1+w_o,p,color);//if(d) V_manspr2(x-w_x+100,y-w_y+HT/2+1+w_o,p,color);
289 else V_manspr(x-w_x+WD/2,y-w_y+HT/2+1+w_o,p,color);//else V_manspr(x-w_x+100,y-w_y+HT/2+1+w_o,p,color);
292 int Z_canstand(int x,int y,int r) {
293 int i;
295 i=(x-r)/CELW;
296 x=(x+r)/CELW;
297 y=(y+1)/CELH;
298 if(y>=FLDH || y<0) return 0;
299 if(i<0) i=0;
300 if(x>=FLDW) x=FLDW-1;
301 for(;i<=x;++i)
302 if(fld[y][i]==1 || fld[y][i]==2 || fld[y][i]==4)
303 if(!z_dot) return 1;
304 else if(!((walf[fldf[y][i]]|walf[fldb[y][i]])&2)) return 1;
305 return 0;
308 int Z_hitceil(int x,int y,int r,int h) {
309 int i;
311 i=(x-r)/CELW;
312 x=(x+r)/CELW;
313 y=(y-h+1)/CELH;
314 if(y>=FLDH || y<0) return 0;
315 if(i<0) i=0;
316 if(x>=FLDW) x=FLDW-1;
317 for(;i<=x;++i)
318 if(fld[y][i]==1 || fld[y][i]==2)
319 if(!z_dot) return 1;
320 else if(!((walf[fldf[y][i]]|walf[fldb[y][i]])&2)) return 1;
321 return 0;
324 int Z_canfit(int x,int y,int r,int h) {
325 int i,j,sx,sy;
327 sx=(x-r)/CELW;
328 sy=(y-h+1)/CELH;
329 if(sx<0) sx=0;
330 if(sy<0) sy=0;
331 x=(x+r)/CELW;
332 y=(y-0)/CELH;
333 if(x>=FLDW) x=FLDW-1;
334 if(y>=FLDH) y=FLDH-1;
335 for(i=sx;i<=x;++i)
336 for(j=sy;j<=y;++j)
337 if(fld[j][i]==1 || fld[j][i]==2)
338 if(!z_dot) return 0;
339 else if(!((walf[fldf[j][i]]|walf[fldb[j][i]])&2)) return 0;
340 return 1;
343 int Z_inlift(int x,int y,int r,int h) {
344 int i,j,sx,sy;
346 sx=(x-r)/CELW;
347 sy=(y-h+1)/CELH;
348 if(sx<0) sx=0;
349 if(sy<0) sy=0;
350 x=(x+r)/CELW;
351 y=(y-1)/CELH;
352 if(x>=FLDW) x=FLDW-1;
353 if(y>=FLDH) y=FLDH-1;
354 for(i=sx;i<=x;++i)
355 for(j=sy;j<=y;++j)
356 if(fld[j][i]==9 || fld[j][i]==10) return fld[j][i]-8;
357 return 0;
360 int Z_isblocked(int x,int y,int r,int h,int xv) {
361 int i,j,sx,sy;
363 sx=(x-r)/CELW;
364 sy=(y-h+1)/CELH;
365 if(sx<0) sx=0;
366 if(sy<0) sy=0;
367 x=(x+r)/CELW;
368 y=(y-1)/CELH;
369 if(xv<0) x=sx;
370 else if(xv>0) sx=x;
371 if(x>=FLDW) x=FLDW-1;
372 if(y>=FLDH) y=FLDH-1;
373 for(i=sx;i<=x;++i)
374 for(j=sy;j<=y;++j)
375 if(fld[j][i]==8) return 1;
376 return 0;
379 int Z_istrapped(int x,int y,int r,int h) {
380 int i,j,sx,sy;
382 sx=(x-r)/CELW;
383 sy=(y-h+1)/CELH;
384 if(sx<0) sx=0;
385 if(sy<0) sy=0;
386 x=(x+r)/CELW;
387 y=(y-1)/CELH;
388 if(x>=FLDW) x=FLDW-1;
389 if(y>=FLDH) y=FLDH-1;
390 for(i=sx;i<=x;++i)
391 for(j=sy;j<=y;++j)
392 if(fld[j][i]==255) return 1;
393 return 0;
396 void Z_set_speed(obj_t *o,int s) {
397 int m;
399 if(!(m=max(abs(o->xv),abs(o->yv)))) m=1;
400 o->xv=o->xv*s/m;o->yv=o->yv*s/m;
403 static byte wfront;
405 int Z_inwater(int x,int y,int r,int h) {
406 int i,j,sx,sy;
408 sx=(x-r)/CELW;
409 sy=(y-h+1)/CELH;
410 if(sx<0) sx=0;
411 if(sy<0) sy=0;
412 x=(x+r)/CELW;
413 y=(y-h/2)/CELH;
414 if(x>=FLDW) x=FLDW-1;
415 if(y>=FLDH) y=FLDH-1;
416 for(i=sx;i<=x;++i)
417 for(j=sy;j<=y;++j)
418 if(fld[j][i]>=5 && fld[j][i]<=7) {wfront=fldf[j][i];return 1;}
419 return 0;
422 int Z_getacid(int x,int y,int r,int h) {
423 int i,j,sx,sy,a;
424 static byte tab[4]={0,5,10,20};
426 a=0;
427 sx=(x-r)/CELW;
428 sy=(y-h+1)/CELH;
429 if(sx<0) sx=0;
430 if(sy<0) sy=0;
431 x=(x+r)/CELW;
432 y=y/CELH;
433 if(x>=FLDW) x=FLDW-1;
434 if(y>=FLDH) y=FLDH-1;
435 for(i=sx;i<=x;++i)
436 for(j=sy;j<=y;++j)
437 if(fld[j][i]==6) a|=1;
438 else if(fld[j][i]==7) a|=2;
439 return tab[a];
442 int Z_canbreathe(int x,int y,int r,int h) {
443 int i,j,sx,sy;
445 sx=(x-r)/CELW;
446 sy=(y-h+1)/CELH;
447 if(sx<0) sx=0;
448 if(sy<0) sy=0;
449 x=(x+r)/CELW;
450 y=(y-h/2)/CELH;
451 if(x>=FLDW) x=FLDW-1;
452 if(y>=FLDH) y=FLDH-1;
453 if(sx>x || sy>y) return 1;
454 for(i=sx;i<=x;++i)
455 for(j=sy;j<=y;++j)
456 if(fld[j][i]==0 || fld[j][i]==3 || fld[j][i]==9 || fld[j][i]==10) return 1;
457 return 0;
460 int Z_overlap(obj_t *a,obj_t *b) {
461 if(a->x - a->r > b->x + b->r) return 0;
462 if(a->x + a->r < b->x - b->r) return 0;
463 if(a->y <= b->y - b->h) return 0;
464 if(a->y - a->h >= b->y) return 0;
465 return 1;
468 void Z_kickobj(obj_t *o,int x,int y,int pwr) {
469 int dx,dy,m;
471 dx=o->x-x;dy=o->y-o->h/2-y;
472 if(!(m=max(abs(dx),abs(dy)))) m=1;
473 o->vx+=(long)dx*pwr/m;
474 o->vy+=(long)dy*pwr/m;
477 int Z_cansee(int x,int y,int xd,int yd) {
478 register dword d,m;
479 int sx,sy;
480 dword xe,ye,s,i;
482 if((xd-=x)>0) sx=1;
483 else if(xd<0) sx=-1;
484 else sx=0;
485 if((yd-=y)>0) sy=1;
486 else if(yd<0) sy=-1;
487 else sy=0;
488 if(!xd && !yd) return 1;
489 if((xd=abs(xd)) > (yd=abs(yd))) d=xd; else d=yd;
490 xe=ye=0;
491 for(i=0;i<=d;) {
492 if(x<0 || x>=FLDW*8 || y<0 || y>=FLDH*8) return 0;
493 if((bmap[y>>5][x>>5]&BM_WALL)) {
494 if(fld[y>>3][x>>3]==1 || fld[y>>3][x>>3]==2) return 0;
495 if((xe+=(xd<<3))>=d) {
496 x+=xe/d*sx;xe=xe%d;
498 if((ye+=(yd<<3))>=d) {
499 y+=ye/d*sy;ye=ye%d;
501 i+=8;
502 }else{
503 if(sx==0) m=0;
504 else{m=x&31;if(sx>0) m^=31; ++m;}
505 if(sy==0) s=0;
506 else{s=y&31;if(sy>0) s^=31; ++s;}
507 if((s<m && s!=0) || m==0) m=s;
508 i+=m;
509 x+=(xd*m+xe)/d*sx;xe=(xd*m+xe)%d;
510 y+=(yd*m+ye)/d*sy;ye=(yd*m+ye)%d;
513 return 1;
516 int Z_look(obj_t *a,obj_t *b,int d) {
517 if(Z_sign(b->x-a->x)!=d*2-1) return 0;
518 return Z_cansee(a->x,a->y-a->h/2,b->x,b->y-b->h/2);
521 #define wvel(v) if((xv=abs(v)+1)>5) v=Z_dec(v,xv/2-2)
523 byte z_mon=0;
525 int Z_moveobj(obj_t *p) {
526 static int x,y,xv,yv,r,h,lx,ly,st;
527 static byte inw;
529 st=0;
530 switch(Z_inlift(x=p->x,y=p->y,r=p->r,h=p->h)) {
531 case 0:
532 if(++p->yv>MAX_YV) --p->yv;
533 break;
534 case 1:
535 if(--p->yv < -5) ++p->yv;
536 break;
537 case 2:
538 if(p->yv > 5) {--p->yv;break;}
539 ++p->yv;break;
541 if((inw=Z_inwater(x,y,r,h))!=0) {
542 st|=Z_INWATER;
543 wvel(p->xv);
544 wvel(p->yv);
545 wvel(p->vx);
546 wvel(p->vy);
548 p->vx=Z_dec(p->vx,1);
549 p->vy=Z_dec(p->vy,1);
550 xv=p->xv+p->vx;yv=p->yv+p->vy;
551 while(xv || yv) {
552 if(x<-100 || x>=FLDW*8+100 || y<-100 || y>=FLDH*8+100)
553 {st|=Z_FALLOUT;}
555 lx=x;
556 x+=(abs(xv)<=7)?xv:((xv>0)?7:-7);
557 if(z_mon) if(Z_isblocked(x,y,r,h,xv)) st|=Z_BLOCK;
558 if(!Z_canfit(x,y,r,h)) {
559 if(xv==0) x=lx;
560 else if(xv<0) x=((lx-r)&0xFFF8)+r;
561 else x=((lx+r)&0xFFF8)-r+7;
562 xv=p->xv=p->vx=0;st|=Z_HITWALL;
564 xv-=(abs(xv)<=7)?xv:((xv>0)?7:-7);
566 ly=y;
567 y+=(abs(yv)<=7)?yv:((yv>0)?7:-7);
568 if(yv>=8) --y;
569 if(yv<0 && Z_hitceil(x,y,r,h)) {
570 y=((ly-h+1)&0xFFF8)+h-1;
571 yv=p->vy=1;p->yv=0;st|=Z_HITCEIL;
573 if(yv>0 && Z_canstand(x,y,r)) {
574 y=((y+1)&0xFFF8)-1;
575 yv=p->yv=p->vy=0;st|=Z_HITLAND;
577 yv-=(abs(yv)<=7)?yv:((yv>0)?7:-7);
579 p->x=x;p->y=y;
580 if(Z_inwater(x,y,r,h)) {
581 st|=Z_INWATER;
582 if(!inw) st|=Z_HITWATER;
583 }else if(inw) st|=Z_HITAIR;
584 return st;
587 void Z_splash(obj_t *p,int n) {
588 Z_sound(bulsnd[0],128);
589 DOT_water(p->x,p->y-p->h/2,p->xv+p->vx,p->yv+p->vy,n,
590 (int)walp[wfront]-1);
593 void Z_calc_time(dword t,word *h,word *m,word *s)
595 t = t * DELAY;
596 t = t / 1000;
597 *s = t % 60;
598 t = t - *s;
599 t = t / 60;
600 *m = t % 60;
601 t = t - *m;
602 t = t / 60;
603 *h = t;