DEADSOFTWARE

Cleanup includes
[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 "vga.h"
31 #include "sound.h"
32 #include "view.h"
33 #include "bmap.h"
34 #include "dots.h"
35 #include "monster.h"
36 #include "misc.h"
38 //#define WD 200
39 //#define HT 98
41 #define MAX_YV 30
43 #define MAXAIR 1091
45 extern dword walf[256];
47 byte z_dot=0;
49 extern void *walp[256];
51 static void *sth[22],*bfh[160-'!'],*sfh[160-'!'],*bulsnd[2],*stone, *stone2, *keys[3];
52 static int prx=0,pry=0;
54 int Z_sign(int a) {
55 if(a>0) return 1;
56 if(a<0) return -1;
57 return 0;
58 }
60 int Z_dec(int a,int b) {
61 if(abs(a)<=b) return 0;
62 if(a>0) return a-b;
63 if(a<0) return a+b;
64 return 0;
65 }
67 void *Z_getspr(char n[4],int s,int d,char *dir) {
68 int h;
70 h=F_getsprid(n,s,d);
71 if(dir) *dir=(h&0x8000)?1:0;
72 return M_lock(h);
73 }
75 void *Z_getsnd(char n[6]) {
76 char s[8];
78 //if(snd_type==-1) return NULL;
79 strncpy(s+2,n,6);s[0]='D';
80 s[1]='S';
81 return M_lock(F_getresid(s));
82 }
84 int Z_sound(void *s,int v) {
85 //if(snd_type==-1) return 0;
86 if(!s) return 0;
87 S_play(s,-1,1024,v);
88 return F_getreslen(((int*)s)[-1])/605;
89 }
91 #define GAS_START (MN__LAST-MN_DEMON+5)
92 #define GAS_TOTAL (MN__LAST-MN_DEMON+16+10)
94 void Z_initst(void) {
95 int i;
96 char s[10];
97 static char nm[22][8]={
98 "STTNUM0","STTNUM1","STTNUM2","STTNUM3","STTNUM4",
99 "STTNUM5","STTNUM6","STTNUM7","STTNUM8","STTNUM9",
100 "STTMINUS","STTPRCNT",
101 "FISTA0","CSAWA0","PISTA0","SHOTA0","SGN2A0","MGUNA0","LAUNA0",
102 "PLASA0","BFUGA0","GUN2A0"
103 };
105 stone=M_lock(F_getresid("STONE"));
106 stone2=M_lock(F_getresid("STONE2"));
107 keys[0]=M_lock(F_getresid("KEYRA0"));
108 keys[1]=M_lock(F_getresid("KEYGA0"));
109 keys[2]=M_lock(F_getresid("KEYBA0"));
110 for(i=0;i<22;++i)
111 sth[i]=M_lock(F_getresid(nm[i]));
112 strcpy(s,"STBF_*");
113 for(i='!';i<160;++i) {
114 s[5]=i;
115 bfh[i-'!']=M_lock(F_findres(s));
116 if(!(i&15)) logo_gas(GAS_START+((i-'!')>>4),GAS_TOTAL);
118 for(i='!';i<160;++i) {
119 sprintf(s,"STCFN%03d",i);
120 sfh[i-'!']=M_lock(F_findres(s));
121 if(!(i&15)) logo_gas(GAS_START+8+((i-'!')>>4),GAS_TOTAL);
123 strcpy(s,"WINUM*");
124 for(i='0';i<='9';++i) {
125 s[5]=i;
126 bfh[i-'!']=M_lock(F_getresid(s));
128 bfh[':'-'!']=M_lock(F_getresid("WICOLON"));
129 bulsnd[0]=Z_getsnd("BUL1");
130 bulsnd[1]=Z_getsnd("BUL2");
133 void Z_putbfch(int c) {
134 vgaimg *p;
136 if(c>32 && c<160) p=bfh[c-'!']; else p=NULL;
137 if(p) {
138 V_spr(prx,pry,p);
139 prx+=p->w-1;
140 }else prx+=12;
143 void Z_putsfch(int c) {
144 vgaimg *p;
146 if(c>32 && c<160) p=sfh[c-'!']; else p=NULL;
147 if(p) {
148 V_spr(prx,pry,p);
149 prx+=p->w-1;
150 }else prx+=7;
153 void Z_gotoxy(int x,int y) {prx=x;pry=y;}
155 void Z_printbf(char *s,...) {
156 int i;
157 va_list ap;
158 char buf[80];
160 va_start(ap,s);
161 vsprintf(buf,s,ap);
162 va_end(ap);
163 for(i=0;buf[i];++i) switch(buf[i]) {
164 case '\n':
165 pry+=13;
166 case '\r':
167 prx=0;break;
168 default:
169 Z_putbfch((byte)buf[i]);
173 void Z_printsf(char *s,...) {
174 int i;
175 va_list ap;
176 char buf[80];
178 va_start(ap,s);
179 vsprintf(buf,s,ap);
180 va_end(ap);
181 for(i=0;buf[i];++i) switch(buf[i]) {
182 case '\n':
183 pry+=8;
184 case '\r':
185 prx=0;break;
186 default:
187 Z_putsfch((byte)buf[i]);
191 void Z_drawspr(int x,int y,void *p,char d) {
192 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);
193 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);
196 void Z_clrst(void) {
197 V_pic(SCRW-120,w_o,stone);//V_pic(200,w_o,stone);
198 int y = ((vgaimg*)stone)->h;
199 while (y<HT) {
200 V_pic(SCRW-120,w_o+y,stone2);
201 y+=((vgaimg*)stone)->h;
206 void Z_drawstlives(char n) {
207 V_setrect(SCRW-40,30,w_o,40);Z_clrst();//V_setrect(280,30,w_o,40);Z_clrst();
208 V_spr(SCRW-35,w_o+17,sth[n]);//V_spr(285,w_o+17,sth[n]);
211 void Z_drawstkeys(byte k) {
212 int x,n;
214 V_setrect(SCRW-120,70,w_o+77,23);Z_clrst();//V_setrect(200,70,w_o+77,23);Z_clrst();
215 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)
216 if(k&1) V_spr(x,w_o+91,keys[n]);
219 void Z_drawstair(int a) {
220 V_setrect(SCRW-120,120,w_o+49,2);Z_clrst();//V_setrect(200,120,w_o+49,2);Z_clrst();
221 if(a<=0) return;
222 if(a>MAXAIR) a=MAXAIR;
223 a=a*100/MAXAIR;
224 if(!a) return;
225 V_clr(SCRW-110,a,w_o+49,2,0xC8);//V_clr(210,a,w_o+49,2,0xC8);
228 void Z_drawstprcnt(int y,int n) {
229 char s[20];
230 int l,i,x,c;
232 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();
233 sprintf(s,"%3d%%",n);
234 l=strlen(s);x=SCRW-110;//l=strlen(s);x=210;
235 for(i=0;i<l;++i,x+=14) {
236 if(s[i]>='0' && s[i]<='9') c=s[i]-'0';
237 else if(s[i]=='-') c=10;
238 else if(s[i]=='%') c=11;
239 else c=-1;
240 if(c>=0)
241 V_spr(x,y*19+7+w_o,sth[c]);
245 void Z_drawstnum(int n) {
246 char s[20];
247 int l,i,x,c;
249 V_setrect(SCRW-50,50,w_o+77,23);Z_clrst();//V_setrect(270,50,w_o+77,23);Z_clrst();
250 if(!g_dm) return;
251 sprintf(s,"%d",n);
252 l=strlen(s);x=(115-l*14)+SCRW-120;//l=strlen(s);x=(115-l*14)+200;
253 for(i=0;i<l;++i,x+=14) {
254 if(s[i]>='0' && s[i]<='9') c=s[i]-'0';
255 else if(s[i]=='-') c=10;
256 else if(s[i]=='%') c=11;
257 else c=-1;
258 if(c>=0)
259 V_spr(x,w_o+77+5,sth[c]);
263 void Z_drawstwpn(int n,int a) {
264 char s[20];
265 int l,i,x,c;
267 i=n;
268 V_setrect(SCRW-120,120,w_o+58,23);Z_clrst();//V_setrect(200,120,w_o+58,23);Z_clrst();
269 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]);
270 if(n>=2) {
271 sprintf(s,"%d",a);
272 l=strlen(s);x=SCRW-10-l*14;//l=strlen(s);x=310-l*14;
273 for(i=0;i<l;++i,x+=14) {
274 if(s[i]>='0' && s[i]<='9') c=s[i]-'0';
275 else if(s[i]=='-') c=10;
276 else if(s[i]=='%') c=11;
277 else c=-1;
278 if(c>=0)
279 V_spr(x,w_o+58+2,sth[c]);
286 void Z_drawmanspr(int x,int y,void *p,char d,byte color) {
287 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);
288 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);
291 int Z_canstand(int x,int y,int r) {
292 int i;
294 i=(x-r)/CELW;
295 x=(x+r)/CELW;
296 y=(y+1)/CELH;
297 if(y>=FLDH || y<0) return 0;
298 if(i<0) i=0;
299 if(x>=FLDW) x=FLDW-1;
300 for(;i<=x;++i)
301 if(fld[y][i]==1 || fld[y][i]==2 || fld[y][i]==4)
302 if(!z_dot) return 1;
303 else if(!((walf[fldf[y][i]]|walf[fldb[y][i]])&2)) return 1;
304 return 0;
307 int Z_hitceil(int x,int y,int r,int h) {
308 int i;
310 i=(x-r)/CELW;
311 x=(x+r)/CELW;
312 y=(y-h+1)/CELH;
313 if(y>=FLDH || y<0) return 0;
314 if(i<0) i=0;
315 if(x>=FLDW) x=FLDW-1;
316 for(;i<=x;++i)
317 if(fld[y][i]==1 || fld[y][i]==2)
318 if(!z_dot) return 1;
319 else if(!((walf[fldf[y][i]]|walf[fldb[y][i]])&2)) return 1;
320 return 0;
323 int Z_canfit(int x,int y,int r,int h) {
324 int i,j,sx,sy;
326 sx=(x-r)/CELW;
327 sy=(y-h+1)/CELH;
328 if(sx<0) sx=0;
329 if(sy<0) sy=0;
330 x=(x+r)/CELW;
331 y=(y-0)/CELH;
332 if(x>=FLDW) x=FLDW-1;
333 if(y>=FLDH) y=FLDH-1;
334 for(i=sx;i<=x;++i)
335 for(j=sy;j<=y;++j)
336 if(fld[j][i]==1 || fld[j][i]==2)
337 if(!z_dot) return 0;
338 else if(!((walf[fldf[j][i]]|walf[fldb[j][i]])&2)) return 0;
339 return 1;
342 int Z_inlift(int x,int y,int r,int h) {
343 int i,j,sx,sy;
345 sx=(x-r)/CELW;
346 sy=(y-h+1)/CELH;
347 if(sx<0) sx=0;
348 if(sy<0) sy=0;
349 x=(x+r)/CELW;
350 y=(y-1)/CELH;
351 if(x>=FLDW) x=FLDW-1;
352 if(y>=FLDH) y=FLDH-1;
353 for(i=sx;i<=x;++i)
354 for(j=sy;j<=y;++j)
355 if(fld[j][i]==9 || fld[j][i]==10) return fld[j][i]-8;
356 return 0;
359 int Z_isblocked(int x,int y,int r,int h,int xv) {
360 int i,j,sx,sy;
362 sx=(x-r)/CELW;
363 sy=(y-h+1)/CELH;
364 if(sx<0) sx=0;
365 if(sy<0) sy=0;
366 x=(x+r)/CELW;
367 y=(y-1)/CELH;
368 if(xv<0) x=sx;
369 else if(xv>0) sx=x;
370 if(x>=FLDW) x=FLDW-1;
371 if(y>=FLDH) y=FLDH-1;
372 for(i=sx;i<=x;++i)
373 for(j=sy;j<=y;++j)
374 if(fld[j][i]==8) return 1;
375 return 0;
378 int Z_istrapped(int x,int y,int r,int h) {
379 int i,j,sx,sy;
381 sx=(x-r)/CELW;
382 sy=(y-h+1)/CELH;
383 if(sx<0) sx=0;
384 if(sy<0) sy=0;
385 x=(x+r)/CELW;
386 y=(y-1)/CELH;
387 if(x>=FLDW) x=FLDW-1;
388 if(y>=FLDH) y=FLDH-1;
389 for(i=sx;i<=x;++i)
390 for(j=sy;j<=y;++j)
391 if(fld[j][i]==255) return 1;
392 return 0;
395 void Z_set_speed(obj_t *o,int s) {
396 int m;
398 if(!(m=max(abs(o->xv),abs(o->yv)))) m=1;
399 o->xv=o->xv*s/m;o->yv=o->yv*s/m;
402 static byte wfront;
404 int Z_inwater(int x,int y,int r,int h) {
405 int i,j,sx,sy;
407 sx=(x-r)/CELW;
408 sy=(y-h+1)/CELH;
409 if(sx<0) sx=0;
410 if(sy<0) sy=0;
411 x=(x+r)/CELW;
412 y=(y-h/2)/CELH;
413 if(x>=FLDW) x=FLDW-1;
414 if(y>=FLDH) y=FLDH-1;
415 for(i=sx;i<=x;++i)
416 for(j=sy;j<=y;++j)
417 if(fld[j][i]>=5 && fld[j][i]<=7) {wfront=fldf[j][i];return 1;}
418 return 0;
421 int Z_getacid(int x,int y,int r,int h) {
422 int i,j,sx,sy,a;
423 static byte tab[4]={0,5,10,20};
425 a=0;
426 sx=(x-r)/CELW;
427 sy=(y-h+1)/CELH;
428 if(sx<0) sx=0;
429 if(sy<0) sy=0;
430 x=(x+r)/CELW;
431 y=y/CELH;
432 if(x>=FLDW) x=FLDW-1;
433 if(y>=FLDH) y=FLDH-1;
434 for(i=sx;i<=x;++i)
435 for(j=sy;j<=y;++j)
436 if(fld[j][i]==6) a|=1;
437 else if(fld[j][i]==7) a|=2;
438 return tab[a];
441 int Z_canbreathe(int x,int y,int r,int h) {
442 int i,j,sx,sy;
444 sx=(x-r)/CELW;
445 sy=(y-h+1)/CELH;
446 if(sx<0) sx=0;
447 if(sy<0) sy=0;
448 x=(x+r)/CELW;
449 y=(y-h/2)/CELH;
450 if(x>=FLDW) x=FLDW-1;
451 if(y>=FLDH) y=FLDH-1;
452 if(sx>x || sy>y) return 1;
453 for(i=sx;i<=x;++i)
454 for(j=sy;j<=y;++j)
455 if(fld[j][i]==0 || fld[j][i]==3 || fld[j][i]==9 || fld[j][i]==10) return 1;
456 return 0;
459 int Z_overlap(obj_t *a,obj_t *b) {
460 if(a->x - a->r > b->x + b->r) return 0;
461 if(a->x + a->r < b->x - b->r) return 0;
462 if(a->y <= b->y - b->h) return 0;
463 if(a->y - a->h >= b->y) return 0;
464 return 1;
467 void Z_kickobj(obj_t *o,int x,int y,int pwr) {
468 int dx,dy,m;
470 dx=o->x-x;dy=o->y-o->h/2-y;
471 if(!(m=max(abs(dx),abs(dy)))) m=1;
472 o->vx+=(long)dx*pwr/m;
473 o->vy+=(long)dy*pwr/m;
476 int Z_cansee(int x,int y,int xd,int yd) {
477 register dword d,m;
478 int sx,sy;
479 dword xe,ye,s,i;
481 if((xd-=x)>0) sx=1;
482 else if(xd<0) sx=-1;
483 else sx=0;
484 if((yd-=y)>0) sy=1;
485 else if(yd<0) sy=-1;
486 else sy=0;
487 if(!xd && !yd) return 1;
488 if((xd=abs(xd)) > (yd=abs(yd))) d=xd; else d=yd;
489 xe=ye=0;
490 for(i=0;i<=d;) {
491 if(x<0 || x>=FLDW*8 || y<0 || y>=FLDH*8) return 0;
492 if((bmap[y>>5][x>>5]&BM_WALL)) {
493 if(fld[y>>3][x>>3]==1 || fld[y>>3][x>>3]==2) return 0;
494 if((xe+=(xd<<3))>=d) {
495 x+=xe/d*sx;xe=xe%d;
497 if((ye+=(yd<<3))>=d) {
498 y+=ye/d*sy;ye=ye%d;
500 i+=8;
501 }else{
502 if(sx==0) m=0;
503 else{m=x&31;if(sx>0) m^=31; ++m;}
504 if(sy==0) s=0;
505 else{s=y&31;if(sy>0) s^=31; ++s;}
506 if((s<m && s!=0) || m==0) m=s;
507 i+=m;
508 x+=(xd*m+xe)/d*sx;xe=(xd*m+xe)%d;
509 y+=(yd*m+ye)/d*sy;ye=(yd*m+ye)%d;
512 return 1;
515 int Z_look(obj_t *a,obj_t *b,int d) {
516 if(Z_sign(b->x-a->x)!=d*2-1) return 0;
517 return Z_cansee(a->x,a->y-a->h/2,b->x,b->y-b->h/2);
520 #define wvel(v) if((xv=abs(v)+1)>5) v=Z_dec(v,xv/2-2)
522 byte z_mon=0;
524 int Z_moveobj(obj_t *p) {
525 static int x,y,xv,yv,r,h,lx,ly,st;
526 static byte inw;
528 st=0;
529 switch(Z_inlift(x=p->x,y=p->y,r=p->r,h=p->h)) {
530 case 0:
531 if(++p->yv>MAX_YV) --p->yv;
532 break;
533 case 1:
534 if(--p->yv < -5) ++p->yv;
535 break;
536 case 2:
537 if(p->yv > 5) {--p->yv;break;}
538 ++p->yv;break;
540 if((inw=Z_inwater(x,y,r,h))!=0) {
541 st|=Z_INWATER;
542 wvel(p->xv);
543 wvel(p->yv);
544 wvel(p->vx);
545 wvel(p->vy);
547 p->vx=Z_dec(p->vx,1);
548 p->vy=Z_dec(p->vy,1);
549 xv=p->xv+p->vx;yv=p->yv+p->vy;
550 while(xv || yv) {
551 if(x<-100 || x>=FLDW*8+100 || y<-100 || y>=FLDH*8+100)
552 {st|=Z_FALLOUT;}
554 lx=x;
555 x+=(abs(xv)<=7)?xv:((xv>0)?7:-7);
556 if(z_mon) if(Z_isblocked(x,y,r,h,xv)) st|=Z_BLOCK;
557 if(!Z_canfit(x,y,r,h)) {
558 if(xv==0) x=lx;
559 else if(xv<0) x=((lx-r)&0xFFF8)+r;
560 else x=((lx+r)&0xFFF8)-r+7;
561 xv=p->xv=p->vx=0;st|=Z_HITWALL;
563 xv-=(abs(xv)<=7)?xv:((xv>0)?7:-7);
565 ly=y;
566 y+=(abs(yv)<=7)?yv:((yv>0)?7:-7);
567 if(yv>=8) --y;
568 if(yv<0 && Z_hitceil(x,y,r,h)) {
569 y=((ly-h+1)&0xFFF8)+h-1;
570 yv=p->vy=1;p->yv=0;st|=Z_HITCEIL;
572 if(yv>0 && Z_canstand(x,y,r)) {
573 y=((y+1)&0xFFF8)-1;
574 yv=p->yv=p->vy=0;st|=Z_HITLAND;
576 yv-=(abs(yv)<=7)?yv:((yv>0)?7:-7);
578 p->x=x;p->y=y;
579 if(Z_inwater(x,y,r,h)) {
580 st|=Z_INWATER;
581 if(!inw) st|=Z_HITWATER;
582 }else if(inw) st|=Z_HITAIR;
583 return st;
586 void Z_splash(obj_t *p,int n) {
587 Z_sound(bulsnd[0],128);
588 DOT_water(p->x,p->y-p->h/2,p->xv+p->vx,p->yv+p->vy,n,
589 (int)walp[wfront]-1);
592 void Z_calc_time(dword t,word *h,word *m,word *s)
594 t = t * DELAY;
595 t = t / 1000;
596 *s = t % 60;
597 t = t - *s;
598 t = t / 60;
599 *m = t % 60;
600 t = t - *m;
601 t = t / 60;
602 *h = t;