DEADSOFTWARE

7123938b16356714846b4fed3ba705b5ab6c4c19
[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 return V_getvgaimg(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';
83 int id = F_getresid(s);
84 int loaded = M_was_locked(id);
85 snd_t *snd = M_lock(id);
86 if (snd != NULL && !loaded) {
87 snd->len = int2host(snd->len);
88 snd->rate = int2host(snd->rate);
89 snd->lstart = int2host(snd->lstart);
90 snd->llen = int2host(snd->llen);
91 }
92 return snd;
93 }
95 int Z_sound(void *s,int v) {
96 //if(snd_type==-1) return 0;
97 if(!s) return 0;
98 S_play(s,-1,1024,v);
99 return F_getreslen(((int*)s)[-1])/605;
102 #define GAS_START (MN__LAST-MN_DEMON+5)
103 #define GAS_TOTAL (MN__LAST-MN_DEMON+16+10)
105 void Z_initst(void) {
106 int i;
107 char s[10];
108 static char nm[22][8]={
109 "STTNUM0","STTNUM1","STTNUM2","STTNUM3","STTNUM4",
110 "STTNUM5","STTNUM6","STTNUM7","STTNUM8","STTNUM9",
111 "STTMINUS","STTPRCNT",
112 "FISTA0","CSAWA0","PISTA0","SHOTA0","SGN2A0","MGUNA0","LAUNA0",
113 "PLASA0","BFUGA0","GUN2A0"
114 };
116 stone=V_loadvgaimg("STONE");
117 stone2=V_loadvgaimg("STONE2");
118 keys[0]=V_loadvgaimg("KEYRA0");
119 keys[1]=V_loadvgaimg("KEYGA0");
120 keys[2]=V_loadvgaimg("KEYBA0");
121 for(i=0;i<22;++i)
122 sth[i]=V_loadvgaimg(nm[i]);
123 strcpy(s,"STBF_*");
124 for(i='!';i<160;++i) {
125 s[5]=i;
126 bfh[i-'!']=V_getvgaimg(F_findres(s));
127 if(!(i&15)) logo_gas(GAS_START+((i-'!')>>4),GAS_TOTAL);
129 for(i='!';i<160;++i) {
130 sprintf(s,"STCFN%03d",i);
131 sfh[i-'!']=V_getvgaimg(F_findres(s));
132 if(!(i&15)) logo_gas(GAS_START+8+((i-'!')>>4),GAS_TOTAL);
134 strcpy(s,"WINUM*");
135 for(i='0';i<='9';++i) {
136 s[5]=i;
137 bfh[i-'!']=V_loadvgaimg(s);
139 bfh[':'-'!']=V_loadvgaimg("WICOLON");
140 bulsnd[0]=Z_getsnd("BUL1");
141 bulsnd[1]=Z_getsnd("BUL2");
144 void Z_putbfch(int c) {
145 vgaimg *p;
147 if(c>32 && c<160) p=bfh[c-'!']; else p=NULL;
148 if(p) {
149 V_spr(prx,pry,p);
150 prx+=p->w-1;
151 }else prx+=12;
154 void Z_putsfch(int c) {
155 vgaimg *p;
157 if(c>32 && c<160) p=sfh[c-'!']; else p=NULL;
158 if(p) {
159 V_spr(prx,pry,p);
160 prx+=p->w-1;
161 }else prx+=7;
164 void Z_gotoxy(int x,int y) {prx=x;pry=y;}
166 void Z_printbf(char *s,...) {
167 int i;
168 va_list ap;
169 char buf[80];
171 va_start(ap,s);
172 vsprintf(buf,s,ap);
173 va_end(ap);
174 for(i=0;buf[i];++i) switch(buf[i]) {
175 case '\n':
176 pry+=13;
177 case '\r':
178 prx=0;break;
179 default:
180 Z_putbfch((byte)buf[i]);
184 void Z_printsf(char *s,...) {
185 int i;
186 va_list ap;
187 char buf[80];
189 va_start(ap,s);
190 vsprintf(buf,s,ap);
191 va_end(ap);
192 for(i=0;buf[i];++i) switch(buf[i]) {
193 case '\n':
194 pry+=8;
195 case '\r':
196 prx=0;break;
197 default:
198 Z_putsfch((byte)buf[i]);
202 void Z_drawspr(int x,int y,void *p,char d) {
203 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);
204 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);
207 void Z_clrst(void) {
208 V_pic(SCRW-120,w_o,stone);//V_pic(200,w_o,stone);
209 int y = ((vgaimg*)stone)->h;
210 while (y<HT) {
211 V_pic(SCRW-120,w_o+y,stone2);
212 y+=((vgaimg*)stone)->h;
217 void Z_drawstlives(char n) {
218 V_setrect(SCRW-40,30,w_o,40);Z_clrst();//V_setrect(280,30,w_o,40);Z_clrst();
219 V_spr(SCRW-35,w_o+17,sth[n]);//V_spr(285,w_o+17,sth[n]);
222 void Z_drawstkeys(byte k) {
223 int x,n;
225 V_setrect(SCRW-120,70,w_o+77,23);Z_clrst();//V_setrect(200,70,w_o+77,23);Z_clrst();
226 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)
227 if(k&1) V_spr(x,w_o+91,keys[n]);
230 void Z_drawstair(int a) {
231 V_setrect(SCRW-120,120,w_o+49,2);Z_clrst();//V_setrect(200,120,w_o+49,2);Z_clrst();
232 if(a<=0) return;
233 if(a>MAXAIR) a=MAXAIR;
234 a=a*100/MAXAIR;
235 if(!a) return;
236 V_clr(SCRW-110,a,w_o+49,2,0xC8);//V_clr(210,a,w_o+49,2,0xC8);
239 void Z_drawstprcnt(int y,int n) {
240 char s[20];
241 int l,i,x,c;
243 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();
244 sprintf(s,"%3d%%",n);
245 l=strlen(s);x=SCRW-110;//l=strlen(s);x=210;
246 for(i=0;i<l;++i,x+=14) {
247 if(s[i]>='0' && s[i]<='9') c=s[i]-'0';
248 else if(s[i]=='-') c=10;
249 else if(s[i]=='%') c=11;
250 else c=-1;
251 if(c>=0)
252 V_spr(x,y*19+7+w_o,sth[c]);
256 void Z_drawstnum(int n) {
257 char s[20];
258 int l,i,x,c;
260 V_setrect(SCRW-50,50,w_o+77,23);Z_clrst();//V_setrect(270,50,w_o+77,23);Z_clrst();
261 if(!g_dm) return;
262 sprintf(s,"%d",n);
263 l=strlen(s);x=(115-l*14)+SCRW-120;//l=strlen(s);x=(115-l*14)+200;
264 for(i=0;i<l;++i,x+=14) {
265 if(s[i]>='0' && s[i]<='9') c=s[i]-'0';
266 else if(s[i]=='-') c=10;
267 else if(s[i]=='%') c=11;
268 else c=-1;
269 if(c>=0)
270 V_spr(x,w_o+77+5,sth[c]);
274 void Z_drawstwpn(int n,int a) {
275 char s[20];
276 int l,i,x,c;
278 i=n;
279 V_setrect(SCRW-120,120,w_o+58,23);Z_clrst();//V_setrect(200,120,w_o+58,23);Z_clrst();
280 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]);
281 if(n>=2) {
282 sprintf(s,"%d",a);
283 l=strlen(s);x=SCRW-10-l*14;//l=strlen(s);x=310-l*14;
284 for(i=0;i<l;++i,x+=14) {
285 if(s[i]>='0' && s[i]<='9') c=s[i]-'0';
286 else if(s[i]=='-') c=10;
287 else if(s[i]=='%') c=11;
288 else c=-1;
289 if(c>=0)
290 V_spr(x,w_o+58+2,sth[c]);
297 void Z_drawmanspr(int x,int y,void *p,char d,byte color) {
298 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);
299 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);
302 int Z_canstand(int x,int y,int r) {
303 int i;
305 i=(x-r)/CELW;
306 x=(x+r)/CELW;
307 y=(y+1)/CELH;
308 if(y>=FLDH || y<0) return 0;
309 if(i<0) i=0;
310 if(x>=FLDW) x=FLDW-1;
311 for(;i<=x;++i)
312 if(fld[y][i]==1 || fld[y][i]==2 || fld[y][i]==4)
313 if(!z_dot) return 1;
314 else if(!((walf[fldf[y][i]]|walf[fldb[y][i]])&2)) return 1;
315 return 0;
318 int Z_hitceil(int x,int y,int r,int h) {
319 int i;
321 i=(x-r)/CELW;
322 x=(x+r)/CELW;
323 y=(y-h+1)/CELH;
324 if(y>=FLDH || y<0) return 0;
325 if(i<0) i=0;
326 if(x>=FLDW) x=FLDW-1;
327 for(;i<=x;++i)
328 if(fld[y][i]==1 || fld[y][i]==2)
329 if(!z_dot) return 1;
330 else if(!((walf[fldf[y][i]]|walf[fldb[y][i]])&2)) return 1;
331 return 0;
334 int Z_canfit(int x,int y,int r,int h) {
335 int i,j,sx,sy;
337 sx=(x-r)/CELW;
338 sy=(y-h+1)/CELH;
339 if(sx<0) sx=0;
340 if(sy<0) sy=0;
341 x=(x+r)/CELW;
342 y=(y-0)/CELH;
343 if(x>=FLDW) x=FLDW-1;
344 if(y>=FLDH) y=FLDH-1;
345 for(i=sx;i<=x;++i)
346 for(j=sy;j<=y;++j)
347 if(fld[j][i]==1 || fld[j][i]==2)
348 if(!z_dot) return 0;
349 else if(!((walf[fldf[j][i]]|walf[fldb[j][i]])&2)) return 0;
350 return 1;
353 int Z_inlift(int x,int y,int r,int h) {
354 int i,j,sx,sy;
356 sx=(x-r)/CELW;
357 sy=(y-h+1)/CELH;
358 if(sx<0) sx=0;
359 if(sy<0) sy=0;
360 x=(x+r)/CELW;
361 y=(y-1)/CELH;
362 if(x>=FLDW) x=FLDW-1;
363 if(y>=FLDH) y=FLDH-1;
364 for(i=sx;i<=x;++i)
365 for(j=sy;j<=y;++j)
366 if(fld[j][i]==9 || fld[j][i]==10) return fld[j][i]-8;
367 return 0;
370 int Z_isblocked(int x,int y,int r,int h,int xv) {
371 int i,j,sx,sy;
373 sx=(x-r)/CELW;
374 sy=(y-h+1)/CELH;
375 if(sx<0) sx=0;
376 if(sy<0) sy=0;
377 x=(x+r)/CELW;
378 y=(y-1)/CELH;
379 if(xv<0) x=sx;
380 else if(xv>0) sx=x;
381 if(x>=FLDW) x=FLDW-1;
382 if(y>=FLDH) y=FLDH-1;
383 for(i=sx;i<=x;++i)
384 for(j=sy;j<=y;++j)
385 if(fld[j][i]==8) return 1;
386 return 0;
389 int Z_istrapped(int x,int y,int r,int h) {
390 int i,j,sx,sy;
392 sx=(x-r)/CELW;
393 sy=(y-h+1)/CELH;
394 if(sx<0) sx=0;
395 if(sy<0) sy=0;
396 x=(x+r)/CELW;
397 y=(y-1)/CELH;
398 if(x>=FLDW) x=FLDW-1;
399 if(y>=FLDH) y=FLDH-1;
400 for(i=sx;i<=x;++i)
401 for(j=sy;j<=y;++j)
402 if(fld[j][i]==255) return 1;
403 return 0;
406 void Z_set_speed(obj_t *o,int s) {
407 int m;
409 if(!(m=max(abs(o->xv),abs(o->yv)))) m=1;
410 o->xv=o->xv*s/m;o->yv=o->yv*s/m;
413 static byte wfront;
415 int Z_inwater(int x,int y,int r,int h) {
416 int i,j,sx,sy;
418 sx=(x-r)/CELW;
419 sy=(y-h+1)/CELH;
420 if(sx<0) sx=0;
421 if(sy<0) sy=0;
422 x=(x+r)/CELW;
423 y=(y-h/2)/CELH;
424 if(x>=FLDW) x=FLDW-1;
425 if(y>=FLDH) y=FLDH-1;
426 for(i=sx;i<=x;++i)
427 for(j=sy;j<=y;++j)
428 if(fld[j][i]>=5 && fld[j][i]<=7) {wfront=fldf[j][i];return 1;}
429 return 0;
432 int Z_getacid(int x,int y,int r,int h) {
433 int i,j,sx,sy,a;
434 static byte tab[4]={0,5,10,20};
436 a=0;
437 sx=(x-r)/CELW;
438 sy=(y-h+1)/CELH;
439 if(sx<0) sx=0;
440 if(sy<0) sy=0;
441 x=(x+r)/CELW;
442 y=y/CELH;
443 if(x>=FLDW) x=FLDW-1;
444 if(y>=FLDH) y=FLDH-1;
445 for(i=sx;i<=x;++i)
446 for(j=sy;j<=y;++j)
447 if(fld[j][i]==6) a|=1;
448 else if(fld[j][i]==7) a|=2;
449 return tab[a];
452 int Z_canbreathe(int x,int y,int r,int h) {
453 int i,j,sx,sy;
455 sx=(x-r)/CELW;
456 sy=(y-h+1)/CELH;
457 if(sx<0) sx=0;
458 if(sy<0) sy=0;
459 x=(x+r)/CELW;
460 y=(y-h/2)/CELH;
461 if(x>=FLDW) x=FLDW-1;
462 if(y>=FLDH) y=FLDH-1;
463 if(sx>x || sy>y) return 1;
464 for(i=sx;i<=x;++i)
465 for(j=sy;j<=y;++j)
466 if(fld[j][i]==0 || fld[j][i]==3 || fld[j][i]==9 || fld[j][i]==10) return 1;
467 return 0;
470 int Z_overlap(obj_t *a,obj_t *b) {
471 if(a->x - a->r > b->x + b->r) return 0;
472 if(a->x + a->r < b->x - b->r) return 0;
473 if(a->y <= b->y - b->h) return 0;
474 if(a->y - a->h >= b->y) return 0;
475 return 1;
478 void Z_kickobj(obj_t *o,int x,int y,int pwr) {
479 int dx,dy,m;
481 dx=o->x-x;dy=o->y-o->h/2-y;
482 if(!(m=max(abs(dx),abs(dy)))) m=1;
483 o->vx+=(long)dx*pwr/m;
484 o->vy+=(long)dy*pwr/m;
487 int Z_cansee(int x,int y,int xd,int yd) {
488 register dword d,m;
489 int sx,sy;
490 dword xe,ye,s,i;
492 if((xd-=x)>0) sx=1;
493 else if(xd<0) sx=-1;
494 else sx=0;
495 if((yd-=y)>0) sy=1;
496 else if(yd<0) sy=-1;
497 else sy=0;
498 if(!xd && !yd) return 1;
499 if((xd=abs(xd)) > (yd=abs(yd))) d=xd; else d=yd;
500 xe=ye=0;
501 for(i=0;i<=d;) {
502 if(x<0 || x>=FLDW*8 || y<0 || y>=FLDH*8) return 0;
503 if((bmap[y>>5][x>>5]&BM_WALL)) {
504 if(fld[y>>3][x>>3]==1 || fld[y>>3][x>>3]==2) return 0;
505 if((xe+=(xd<<3))>=d) {
506 x+=xe/d*sx;xe=xe%d;
508 if((ye+=(yd<<3))>=d) {
509 y+=ye/d*sy;ye=ye%d;
511 i+=8;
512 }else{
513 if(sx==0) m=0;
514 else{m=x&31;if(sx>0) m^=31; ++m;}
515 if(sy==0) s=0;
516 else{s=y&31;if(sy>0) s^=31; ++s;}
517 if((s<m && s!=0) || m==0) m=s;
518 i+=m;
519 x+=(xd*m+xe)/d*sx;xe=(xd*m+xe)%d;
520 y+=(yd*m+ye)/d*sy;ye=(yd*m+ye)%d;
523 return 1;
526 int Z_look(obj_t *a,obj_t *b,int d) {
527 if(Z_sign(b->x-a->x)!=d*2-1) return 0;
528 return Z_cansee(a->x,a->y-a->h/2,b->x,b->y-b->h/2);
531 #define wvel(v) if((xv=abs(v)+1)>5) v=Z_dec(v,xv/2-2)
533 byte z_mon=0;
535 int Z_moveobj(obj_t *p) {
536 static int x,y,xv,yv,r,h,lx,ly,st;
537 static byte inw;
539 st=0;
540 switch(Z_inlift(x=p->x,y=p->y,r=p->r,h=p->h)) {
541 case 0:
542 if(++p->yv>MAX_YV) --p->yv;
543 break;
544 case 1:
545 if(--p->yv < -5) ++p->yv;
546 break;
547 case 2:
548 if(p->yv > 5) {--p->yv;break;}
549 ++p->yv;break;
551 if((inw=Z_inwater(x,y,r,h))!=0) {
552 st|=Z_INWATER;
553 wvel(p->xv);
554 wvel(p->yv);
555 wvel(p->vx);
556 wvel(p->vy);
558 p->vx=Z_dec(p->vx,1);
559 p->vy=Z_dec(p->vy,1);
560 xv=p->xv+p->vx;yv=p->yv+p->vy;
561 while(xv || yv) {
562 if(x<-100 || x>=FLDW*8+100 || y<-100 || y>=FLDH*8+100)
563 {st|=Z_FALLOUT;}
565 lx=x;
566 x+=(abs(xv)<=7)?xv:((xv>0)?7:-7);
567 if(z_mon) if(Z_isblocked(x,y,r,h,xv)) st|=Z_BLOCK;
568 if(!Z_canfit(x,y,r,h)) {
569 if(xv==0) x=lx;
570 else if(xv<0) x=((lx-r)&0xFFF8)+r;
571 else x=((lx+r)&0xFFF8)-r+7;
572 xv=p->xv=p->vx=0;st|=Z_HITWALL;
574 xv-=(abs(xv)<=7)?xv:((xv>0)?7:-7);
576 ly=y;
577 y+=(abs(yv)<=7)?yv:((yv>0)?7:-7);
578 if(yv>=8) --y;
579 if(yv<0 && Z_hitceil(x,y,r,h)) {
580 y=((ly-h+1)&0xFFF8)+h-1;
581 yv=p->vy=1;p->yv=0;st|=Z_HITCEIL;
583 if(yv>0 && Z_canstand(x,y,r)) {
584 y=((y+1)&0xFFF8)-1;
585 yv=p->yv=p->vy=0;st|=Z_HITLAND;
587 yv-=(abs(yv)<=7)?yv:((yv>0)?7:-7);
589 p->x=x;p->y=y;
590 if(Z_inwater(x,y,r,h)) {
591 st|=Z_INWATER;
592 if(!inw) st|=Z_HITWATER;
593 }else if(inw) st|=Z_HITAIR;
594 return st;
597 void Z_splash(obj_t *p,int n) {
598 Z_sound(bulsnd[0],128);
599 DOT_water(p->x,p->y-p->h/2,p->xv+p->vx,p->yv+p->vy,n,
600 (int)walp[wfront]-1);
603 void Z_calc_time(dword t,word *h,word *m,word *s)
605 t = t * DELAY;
606 t = t / 1000;
607 *s = t % 60;
608 t = t - *s;
609 t = t / 60;
610 *m = t % 60;
611 t = t - *m;
612 t = t / 60;
613 *h = t;
616 unsigned short int short2host (unsigned short int x) {
617 #if __BIG_ENDIAN__
618 union {
619 unsigned char a[2];
620 unsigned short int x;
621 } y;
622 y.x = x;
623 unsigned char t = y.a[0]; y.a[0] = y.a[1]; y.a[1] = t;
624 return y.x;
625 #else
626 return x;
627 #endif
630 unsigned int int2host (unsigned int x) {
631 #if __BIG_ENDIAN__
632 union {
633 unsigned char a[4];
634 unsigned int x;
635 } y;
636 y.x = x;
637 #define SWAP_VAR(a, b) do { unsigned char t = a; a = b; b = t; } while(0)
638 SWAP_VAR(y.a[0], y.a[3]);
639 SWAP_VAR(y.a[1], y.a[2]);
640 #undef SWAP_VAR
641 return y.x;
642 #else
643 return x;
644 #endif