DEADSOFTWARE

headers describes that c-files implements
[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 "sound.h"
31 #include "view.h"
32 #include "bmap.h"
33 #include "dots.h"
34 #include "monster.h"
35 #include "misc.h"
36 #include "render.h"
38 //#define WD 200
39 //#define HT 98
41 #define MAX_YV 30
43 byte z_dot;
44 byte z_mon;
45 static void *bulsnd[2];
46 static byte wfront;
48 int Z_sign(int a) {
49 if(a>0) return 1;
50 if(a<0) return -1;
51 return 0;
52 }
54 int Z_dec(int a,int b) {
55 if(abs(a)<=b) return 0;
56 if(a>0) return a-b;
57 if(a<0) return a+b;
58 return 0;
59 }
61 void *Z_getsnd(char n[6]) {
62 char s[8];
64 //if(snd_type==-1) return NULL;
65 strncpy(s+2,n,6);s[0]='D';
66 s[1]='S';
68 int id = F_getresid(s);
69 int loaded = M_was_locked(id);
70 snd_t *snd = M_lock(id);
71 if (snd != NULL && !loaded) {
72 snd->len = int2host(snd->len);
73 snd->rate = int2host(snd->rate);
74 snd->lstart = int2host(snd->lstart);
75 snd->llen = int2host(snd->llen);
76 }
77 return snd;
78 }
80 int Z_sound(void *s,int v) {
81 //if(snd_type==-1) return 0;
82 if(!s) return 0;
83 S_play(s,-1,1024,v);
84 return F_getreslen(((int*)s)[-1])/605;
85 }
87 #define GAS_START (MN__LAST-MN_DEMON+5)
88 #define GAS_TOTAL (MN__LAST-MN_DEMON+16+10)
90 void Z_initst(void) {
91 bulsnd[0]=Z_getsnd("BUL1");
92 bulsnd[1]=Z_getsnd("BUL2");
93 }
95 int Z_canstand(int x,int y,int r) {
96 int i;
98 i=(x-r)/CELW;
99 x=(x+r)/CELW;
100 y=(y+1)/CELH;
101 if(y>=FLDH || y<0) return 0;
102 if(i<0) i=0;
103 if(x>=FLDW) x=FLDW-1;
104 for(;i<=x;++i)
105 if(fld[y][i]==1 || fld[y][i]==2 || fld[y][i]==4)
106 if(!z_dot) return 1;
107 else if(!((walf[fldf[y][i]]|walf[fldb[y][i]])&2)) return 1;
108 return 0;
111 static int Z_hitceil(int x,int y,int r,int h) {
112 int i;
114 i=(x-r)/CELW;
115 x=(x+r)/CELW;
116 y=(y-h+1)/CELH;
117 if(y>=FLDH || y<0) return 0;
118 if(i<0) i=0;
119 if(x>=FLDW) x=FLDW-1;
120 for(;i<=x;++i)
121 if(fld[y][i]==1 || fld[y][i]==2)
122 if(!z_dot) return 1;
123 else if(!((walf[fldf[y][i]]|walf[fldb[y][i]])&2)) return 1;
124 return 0;
127 int Z_canfit(int x,int y,int r,int h) {
128 int i,j,sx,sy;
130 sx=(x-r)/CELW;
131 sy=(y-h+1)/CELH;
132 if(sx<0) sx=0;
133 if(sy<0) sy=0;
134 x=(x+r)/CELW;
135 y=(y-0)/CELH;
136 if(x>=FLDW) x=FLDW-1;
137 if(y>=FLDH) y=FLDH-1;
138 for(i=sx;i<=x;++i)
139 for(j=sy;j<=y;++j)
140 if(fld[j][i]==1 || fld[j][i]==2)
141 if(!z_dot) return 0;
142 else if(!((walf[fldf[j][i]]|walf[fldb[j][i]])&2)) return 0;
143 return 1;
146 static int Z_inlift(int x,int y,int r,int h) {
147 int i,j,sx,sy;
149 sx=(x-r)/CELW;
150 sy=(y-h+1)/CELH;
151 if(sx<0) sx=0;
152 if(sy<0) sy=0;
153 x=(x+r)/CELW;
154 y=(y-1)/CELH;
155 if(x>=FLDW) x=FLDW-1;
156 if(y>=FLDH) y=FLDH-1;
157 for(i=sx;i<=x;++i)
158 for(j=sy;j<=y;++j)
159 if(fld[j][i]==9 || fld[j][i]==10) return fld[j][i]-8;
160 return 0;
163 static int Z_isblocked(int x,int y,int r,int h,int xv) {
164 int i,j,sx,sy;
166 sx=(x-r)/CELW;
167 sy=(y-h+1)/CELH;
168 if(sx<0) sx=0;
169 if(sy<0) sy=0;
170 x=(x+r)/CELW;
171 y=(y-1)/CELH;
172 if(xv<0) x=sx;
173 else if(xv>0) sx=x;
174 if(x>=FLDW) x=FLDW-1;
175 if(y>=FLDH) y=FLDH-1;
176 for(i=sx;i<=x;++i)
177 for(j=sy;j<=y;++j)
178 if(fld[j][i]==8) return 1;
179 return 0;
182 int Z_istrapped(int x,int y,int r,int h) {
183 int i,j,sx,sy;
185 sx=(x-r)/CELW;
186 sy=(y-h+1)/CELH;
187 if(sx<0) sx=0;
188 if(sy<0) sy=0;
189 x=(x+r)/CELW;
190 y=(y-1)/CELH;
191 if(x>=FLDW) x=FLDW-1;
192 if(y>=FLDH) y=FLDH-1;
193 for(i=sx;i<=x;++i)
194 for(j=sy;j<=y;++j)
195 if(fld[j][i]==255) return 1;
196 return 0;
199 void Z_set_speed(obj_t *o,int s) {
200 int m;
202 if(!(m=max(abs(o->xv),abs(o->yv)))) m=1;
203 o->xv=o->xv*s/m;o->yv=o->yv*s/m;
206 int Z_inwater(int x,int y,int r,int h) {
207 int i,j,sx,sy;
209 sx=(x-r)/CELW;
210 sy=(y-h+1)/CELH;
211 if(sx<0) sx=0;
212 if(sy<0) sy=0;
213 x=(x+r)/CELW;
214 y=(y-h/2)/CELH;
215 if(x>=FLDW) x=FLDW-1;
216 if(y>=FLDH) y=FLDH-1;
217 for(i=sx;i<=x;++i)
218 for(j=sy;j<=y;++j)
219 if(fld[j][i]>=5 && fld[j][i]<=7) {wfront=fldf[j][i];return 1;}
220 return 0;
223 int Z_getacid(int x,int y,int r,int h) {
224 int i,j,sx,sy,a;
225 static byte tab[4]={0,5,10,20};
227 a=0;
228 sx=(x-r)/CELW;
229 sy=(y-h+1)/CELH;
230 if(sx<0) sx=0;
231 if(sy<0) sy=0;
232 x=(x+r)/CELW;
233 y=y/CELH;
234 if(x>=FLDW) x=FLDW-1;
235 if(y>=FLDH) y=FLDH-1;
236 for(i=sx;i<=x;++i)
237 for(j=sy;j<=y;++j)
238 if(fld[j][i]==6) a|=1;
239 else if(fld[j][i]==7) a|=2;
240 return tab[a];
243 int Z_canbreathe(int x,int y,int r,int h) {
244 int i,j,sx,sy;
246 sx=(x-r)/CELW;
247 sy=(y-h+1)/CELH;
248 if(sx<0) sx=0;
249 if(sy<0) sy=0;
250 x=(x+r)/CELW;
251 y=(y-h/2)/CELH;
252 if(x>=FLDW) x=FLDW-1;
253 if(y>=FLDH) y=FLDH-1;
254 if(sx>x || sy>y) return 1;
255 for(i=sx;i<=x;++i)
256 for(j=sy;j<=y;++j)
257 if(fld[j][i]==0 || fld[j][i]==3 || fld[j][i]==9 || fld[j][i]==10) return 1;
258 return 0;
261 int Z_overlap(obj_t *a,obj_t *b) {
262 if(a->x - a->r > b->x + b->r) return 0;
263 if(a->x + a->r < b->x - b->r) return 0;
264 if(a->y <= b->y - b->h) return 0;
265 if(a->y - a->h >= b->y) return 0;
266 return 1;
269 static void Z_kickobj(obj_t *o,int x,int y,int pwr) {
270 int dx,dy,m;
272 dx=o->x-x;dy=o->y-o->h/2-y;
273 if(!(m=max(abs(dx),abs(dy)))) m=1;
274 o->vx+=(long)dx*pwr/m;
275 o->vy+=(long)dy*pwr/m;
278 int Z_cansee(int x,int y,int xd,int yd) {
279 register dword d,m;
280 int sx,sy;
281 dword xe,ye,s,i;
283 if((xd-=x)>0) sx=1;
284 else if(xd<0) sx=-1;
285 else sx=0;
286 if((yd-=y)>0) sy=1;
287 else if(yd<0) sy=-1;
288 else sy=0;
289 if(!xd && !yd) return 1;
290 if((xd=abs(xd)) > (yd=abs(yd))) d=xd; else d=yd;
291 xe=ye=0;
292 for(i=0;i<=d;) {
293 if(x<0 || x>=FLDW*8 || y<0 || y>=FLDH*8) return 0;
294 if((bmap[y>>5][x>>5]&BM_WALL)) {
295 if(fld[y>>3][x>>3]==1 || fld[y>>3][x>>3]==2) return 0;
296 if((xe+=(xd<<3))>=d) {
297 x+=xe/d*sx;xe=xe%d;
299 if((ye+=(yd<<3))>=d) {
300 y+=ye/d*sy;ye=ye%d;
302 i+=8;
303 }else{
304 if(sx==0) m=0;
305 else{m=x&31;if(sx>0) m^=31; ++m;}
306 if(sy==0) s=0;
307 else{s=y&31;if(sy>0) s^=31; ++s;}
308 if((s<m && s!=0) || m==0) m=s;
309 i+=m;
310 x+=(xd*m+xe)/d*sx;xe=(xd*m+xe)%d;
311 y+=(yd*m+ye)/d*sy;ye=(yd*m+ye)%d;
314 return 1;
317 int Z_look(obj_t *a,obj_t *b,int d) {
318 if(Z_sign(b->x-a->x)!=d*2-1) return 0;
319 return Z_cansee(a->x,a->y-a->h/2,b->x,b->y-b->h/2);
322 #define wvel(v) if((xv=abs(v)+1)>5) v=Z_dec(v,xv/2-2)
324 int Z_moveobj(obj_t *p) {
325 static int x,y,xv,yv,r,h,lx,ly,st;
326 static byte inw;
328 st=0;
329 switch(Z_inlift(x=p->x,y=p->y,r=p->r,h=p->h)) {
330 case 0:
331 if(++p->yv>MAX_YV) --p->yv;
332 break;
333 case 1:
334 if(--p->yv < -5) ++p->yv;
335 break;
336 case 2:
337 if(p->yv > 5) {--p->yv;break;}
338 ++p->yv;break;
340 if((inw=Z_inwater(x,y,r,h))!=0) {
341 st|=Z_INWATER;
342 wvel(p->xv);
343 wvel(p->yv);
344 wvel(p->vx);
345 wvel(p->vy);
347 p->vx=Z_dec(p->vx,1);
348 p->vy=Z_dec(p->vy,1);
349 xv=p->xv+p->vx;yv=p->yv+p->vy;
350 while(xv || yv) {
351 if(x<-100 || x>=FLDW*8+100 || y<-100 || y>=FLDH*8+100)
352 {st|=Z_FALLOUT;}
354 lx=x;
355 x+=(abs(xv)<=7)?xv:((xv>0)?7:-7);
356 if(z_mon) if(Z_isblocked(x,y,r,h,xv)) st|=Z_BLOCK;
357 if(!Z_canfit(x,y,r,h)) {
358 if(xv==0) x=lx;
359 else if(xv<0) x=((lx-r)&0xFFF8)+r;
360 else x=((lx+r)&0xFFF8)-r+7;
361 xv=p->xv=p->vx=0;st|=Z_HITWALL;
363 xv-=(abs(xv)<=7)?xv:((xv>0)?7:-7);
365 ly=y;
366 y+=(abs(yv)<=7)?yv:((yv>0)?7:-7);
367 if(yv>=8) --y;
368 if(yv<0 && Z_hitceil(x,y,r,h)) {
369 y=((ly-h+1)&0xFFF8)+h-1;
370 yv=p->vy=1;p->yv=0;st|=Z_HITCEIL;
372 if(yv>0 && Z_canstand(x,y,r)) {
373 y=((y+1)&0xFFF8)-1;
374 yv=p->yv=p->vy=0;st|=Z_HITLAND;
376 yv-=(abs(yv)<=7)?yv:((yv>0)?7:-7);
378 p->x=x;p->y=y;
379 if(Z_inwater(x,y,r,h)) {
380 st|=Z_INWATER;
381 if(!inw) st|=Z_HITWATER;
382 }else if(inw) st|=Z_HITAIR;
383 return st;
386 void Z_splash (obj_t *p, int n) {
387 Z_sound(bulsnd[0], 128);
388 DOT_water(p->x, p->y-p->h / 2, p->xv + p->vx, p->yv + p->vy, n, R_get_special_id(wfront) - 1);
391 void Z_calc_time(dword t,word *h,word *m,word *s)
393 t = t * DELAY;
394 t = t / 1000;
395 *s = t % 60;
396 t = t - *s;
397 t = t / 60;
398 *m = t % 60;
399 t = t - *m;
400 t = t / 60;
401 *h = t;
404 #define SWAP_VAR(a, b) do { unsigned char t = a; a = b; b = t; } while(0)
406 static int16_t short2swap (int16_t x) {
407 union {
408 uint8_t a[2];
409 int16_t x;
410 } y;
411 y.x = x;
412 SWAP_VAR(y.a[0], y.a[1]);
413 return y.x;
416 static int32_t int2swap (int32_t x) {
417 union {
418 uint8_t a[4];
419 int32_t x;
420 } y;
421 y.x = x;
422 SWAP_VAR(y.a[0], y.a[3]);
423 SWAP_VAR(y.a[1], y.a[2]);
424 return y.x;
427 #undef SWAP_VAR
429 int16_t short2host (int16_t x) {
430 #if __BIG_ENDIAN__
431 return short2swap(x);
432 #else
433 return x;
434 #endif
437 int32_t int2host (int32_t x) {
438 #if __BIG_ENDIAN__
439 return int2swap(x);
440 #else
441 return x;
442 #endif