2 * Copyright (C) Prikol Software 1996-1997
3 * Copyright (C) Aleksey Volynskov 1996-1997
4 * Copyright (C) <ARembo@gmail.com> 2011
5 * Copyright (C) fgsfds 2019
7 * This file is part of the Doom2D PSX project.
9 * Doom2D PSX is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
13 * Doom2D PSX is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/> or
20 * write to the Free Software Foundation, Inc.,
21 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
47 extern dword walf
[256];
51 extern void * walp
[256];
53 static void * sth
[22], * bfh
[160 - '!'], * sfh
[160 - '!'], * bulsnd
[2], * stone
, * stone2
, * keys
[3], *hudmed
, *hudarm
[2];
54 static int prx
= 0, pry
= 0;
56 char vabuf
[80] = { 0 };
68 int Z_dec(int a
, int b
) {
81 void * Z_getspr(char n
[4], int s
, int d
, char * dir
) {
84 h
= F_getsprid(n
, s
, d
);
86 *dir
= (h
& 0x8000) ? 1 : 0;
87 return V_cachepic(M_lock(h
));
90 void * Z_getsnd(char n
[6]) {
95 return S_cachesound(M_lock(F_getresid(s
)));
98 #define GAS_START (MN__LAST - MN_DEMON + 5)
99 #define GAS_TOTAL (MN__LAST - MN_DEMON + 16 + 10)
101 void Z_initst(void) {
104 static char nm
[21][8] = {
105 "STTNUM0", "STTNUM1", "STTNUM2", "STTNUM3", "STTNUM4",
106 "STTNUM5", "STTNUM6", "STTNUM7", "STTNUM8", "STTNUM9",
108 "FISTA0", "CSAWA0", "PISTA0", "SHOTA0", "SGN2A0", "MGUNA0","LAUNA0",
109 "PLASA0", "BFUGA0", "GUN2A0"
112 stone
= V_cachepic(M_lock(F_getresid("STONE")));
113 stone2
= V_cachepic(M_lock(F_getresid("STONE2")));
114 keys
[0] = V_cachepic(M_lock(F_getresid("KEYRA0")));
115 keys
[1] = V_cachepic(M_lock(F_getresid("KEYGA0")));
116 keys
[2] = V_cachepic(M_lock(F_getresid("KEYBA0")));
117 hudmed
= V_cachepic(M_lock(F_getresid("MEDIA0")));
118 hudarm
[0] = V_cachepic(M_lock(F_getresid("ARM1A0")));
119 hudarm
[1] = V_cachepic(M_lock(F_getresid("ARM2A0")));
121 for (i
= 0; i
< 21; ++i
)
122 sth
[i
] = V_cachepic(M_lock(F_getresid(nm
[i
])));
124 for (i
= '!'; i
< 160; ++i
) {
126 bfh
[i
- '!'] = V_cachepic(M_lock(F_findres(s
)));
128 for (i
= '!'; i
< 160; ++i
) {
129 sprintf(s
, "STCFN%03d", i
);
130 sfh
[i
- '!'] = V_cachepic(M_lock(F_findres(s
)));
133 for (i
= '0'; i
<= '9'; ++i
) {
135 bfh
[i
- '!'] = V_cachepic(M_lock(F_getresid(s
)));
137 bfh
[':' - '!'] = V_cachepic(M_lock(F_getresid("WICOLON")));
138 bulsnd
[0] = Z_getsnd("BUL1");
139 bulsnd
[1] = Z_getsnd("BUL2");
142 void Z_putbfch(int c
) {
145 if ((c
> 32) && (c
< 160))
151 prx
+= p
->img
->w
- 1;
157 void Z_putsfch(int c
) {
160 if ((c
> 32) && (c
< 160))
166 prx
+= p
->img
->w
- 1;
172 void Z_gotoxy(int x
, int y
) {
177 void Z_printb(char * buf
) {
179 for (i
= 0; buf
[i
]; ++i
) {
189 Z_putbfch((byte
) buf
[i
]);
194 int Z_linelens(char *buf
) {
196 for (i
= 0, x
= 0; buf
[i
]; ++i
) {
198 if ((c
> 32) && (c
< 160) && sfh
[c
- '!'])
199 x
+= ((cacheimg
*)sfh
[c
- '!'])->img
->w
- 1;
206 int Z_linelenb(char *buf
) {
208 for (i
= 0, x
= 0; buf
[i
]; ++i
) {
210 if ((c
> 32) && (c
< 160) && bfh
[c
- '!'])
211 x
+= ((cacheimg
*)bfh
[c
- '!'])->img
->w
- 1;
219 void Z_printsf(char * s, ...) {
225 vsprintf(buf, s, ap);
227 for (i = 0; buf[i]; ++i) {
237 Z_putsfch((byte) buf[i]);
243 void Z_prints(char *buf
) {
245 for (i
= 0; buf
[i
]; ++i
) {
255 Z_putsfch((byte
) buf
[i
]);
260 void Z_drawspr(int x
, int y
, void * p
, char d
) {
262 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);
264 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);
268 V_pic(SCRW
- 120, w_o
, stone
); // V_pic(200,w_o,stone);
270 V_pic(SCRW
- 120, w_o
+ 120, stone2
);
273 void Z_drawstkeys(byte k
) {
276 //V_setrect(SCRW - 120, 70, w_o + 77, 23);
277 //Z_clrst(); // V_setrect(200,70,w_o+77,23);Z_clrst();
278 for (k
>>= 4, n
= 0, x
= SCRW
- 69; n
< 3; ++n
, k
>>= 1, x
+= 9) { // for(k>>=4,n=0,x=245;n<3;++n,k>>=1,x+=9)
280 V_spr(x
, w_o
+ 111, keys
[n
]);
284 void Z_drawstair(int a
) {
285 //V_setrect(SCRW - 120, 120, w_o + 49, 2);
286 //Z_clrst(); // V_setrect(200,120,w_o+49,2);Z_clrst();
292 a
= a
* 100 / MAXAIR
;
296 V_clr(SCRW
- 110, a
, w_o
+ 58, 2, 0xC8); // V_clr(210,a,w_o+49,2,0xC8);
299 void Z_drawstprcnt(int y
, int n
) {
303 //V_setrect(SCRW - 120, 70, y * 19 + 7 + w_o, 19);
304 // Z_clrst(); // V_setrect(200,70,y*19+7+w_o,19);Z_clrst();
305 sprintf(s
, "%3d", n
);
307 x
= SCRW
- 10 - l
* 14; // l=strlen(s);x=210;
308 for (i
= 0; i
< l
; ++i
, x
+= 14) {
309 if ((s
[i
] >= '0') && (s
[i
] <= '9'))
311 else if (s
[i
] == '-')
316 V_spr(x
, y
* 19 + 12 + w_o
, sth
[c
]);
320 void Z_drawstnum(int n
) {
324 //V_setrect(SCRW - 50, 50, w_o + 77, 23);
325 // Z_clrst(); // V_setrect(270,50,w_o+77,23);Z_clrst();
331 x
= SCRW
- 121 - l
* 14; // l=strlen(s);x=(115-l*14)+200;
332 for (i
= 0; i
< l
; ++i
, x
+= 14) {
333 if ((s
[i
] >= '0') && (s
[i
] <= '9'))
335 else if (s
[i
] == '-')
340 V_spr(x
, w_o
+ 2, sth
[c
]);
344 void Z_drawstwpn(int n
, int a
) {
349 //V_setrect(SCRW - 120, 120, w_o + 58, 23);
350 // Z_clrst(); // V_setrect(200,120,w_o+58,23);Z_clrst();
352 V_spr(SCRW
- 88, w_o
+ 73 + 12, sth
[i
+ 11]); // if(i>=0) V_spr(232,w_o+58+19,sth[i+12]);
356 x
= SCRW
- 10 - l
* 14; // l=strlen(s);x=310-l*14;
357 for (i
= 0; i
< l
; ++i
, x
+= 14) {
358 if ((s
[i
] >= '0') && (s
[i
] <= '9'))
360 else if (s
[i
] == '-')
365 V_spr(x
, w_o
+ 73, sth
[c
]);
370 void Z_drawmanspr(int x
, int y
, void * p
, char d
, byte color
) {
372 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);
374 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);
377 int Z_canstand(int x
, int y
, int r
) {
383 if ((y
>= FLDH
) || (y
< 0))
390 for (; i
<= x
; ++i
) {
391 if ((fld
[y
][i
] == 1) || (fld
[y
][i
] == 2) || (fld
[y
][i
] == 4)) {
394 else if (!((walf
[fldf
[y
][i
]] | walf
[fldb
[y
][i
]]) & 2))
401 int Z_hitceil(int x
, int y
, int r
, int h
) {
406 y
= (y
- h
+ 1) / CELH
;
407 if ((y
>= FLDH
) || (y
< 0))
414 for (; i
<= x
; ++i
) {
415 if ((fld
[y
][i
] == 1) || (fld
[y
][i
] == 2)) {
418 else if (!((walf
[fldf
[y
][i
]] | walf
[fldb
[y
][i
]]) & 2))
425 int Z_canfit(int x
, int y
, int r
, int h
) {
429 sy
= (y
- h
+ 1) / CELH
;
440 for (i
= sx
; i
<= x
; ++i
) {
441 for (j
= sy
; j
<= y
; ++j
) {
442 if ((fld
[j
][i
] == 1) || (fld
[j
][i
] == 2)) {
445 else if (!((walf
[fldf
[j
][i
]] | walf
[fldb
[j
][i
]]) & 2))
453 int Z_inlift(int x
, int y
, int r
, int h
) {
457 sy
= (y
- h
+ 1) / CELH
;
468 for (i
= sx
; i
<= x
; ++i
) {
469 for (j
= sy
; j
<= y
; ++j
) {
470 if ((fld
[j
][i
] == 9) || (fld
[j
][i
] == 10))
471 return fld
[j
][i
] - 8;
477 int Z_isblocked(int x
, int y
, int r
, int h
, int xv
) {
481 sy
= (y
- h
+ 1) / CELH
;
496 for (i
= sx
; i
<= x
; ++i
) {
497 for (j
= sy
; j
<= y
; ++j
) {
505 int Z_istrapped(int x
, int y
, int r
, int h
) {
509 sy
= (y
- h
+ 1) / CELH
;
520 for (i
= sx
; i
<= x
; ++i
) {
521 for (j
= sy
; j
<= y
; ++j
) {
522 if (fld
[j
][i
] == 255)
529 void Z_set_speed(obj_t
* o
, int s
) {
532 if (!(m
= max(abs(o
->xv
), abs(o
->yv
))))
534 o
->xv
= o
->xv
* s
/ m
;
535 o
->yv
= o
->yv
* s
/ m
;
540 int Z_inwater(int x
, int y
, int r
, int h
) {
544 sy
= (y
- h
+ 1) / CELH
;
550 y
= (y
- h
/ 2) / CELH
;
555 for (i
= sx
; i
<= x
; ++i
) {
556 for (j
= sy
; j
<= y
; ++j
) {
557 if ((fld
[j
][i
] >= 5) && (fld
[j
][i
] <= 7)) {
566 int Z_getacid(int x
, int y
, int r
, int h
) {
568 static byte tab
[4] = { 0, 5, 10, 20 };
572 sy
= (y
- h
+ 1) / CELH
;
583 for (i
= sx
; i
<= x
; ++i
) {
584 for (j
= sy
; j
<= y
; ++j
) {
587 else if (fld
[j
][i
] == 7)
594 int Z_canbreathe(int x
, int y
, int r
, int h
) {
598 sy
= (y
- h
+ 1) / CELH
;
604 y
= (y
- h
/ 2) / CELH
;
609 if ((sx
> x
) || (sy
> y
))
612 for (i
= sx
; i
<= x
; ++i
) {
613 for (j
= sy
; j
<= y
; ++j
) {
614 if ((fld
[j
][i
] == 0) || (fld
[j
][i
] == 3) || (fld
[j
][i
] == 9) || (fld
[j
][i
] == 10))
621 int Z_overlap(obj_t
* a
, obj_t
* b
) {
622 if (a
->x
- a
->r
> b
->x
+ b
->r
)
625 if (a
->x
+ a
->r
< b
->x
- b
->r
)
628 if (a
->y
<= b
->y
- b
->h
)
631 if (a
->y
- a
->h
>= b
->y
)
637 void Z_kickobj(obj_t
* o
, int x
, int y
, int pwr
) {
641 dy
= o
->y
- o
->h
/ 2 - y
;
642 if (!(m
= max(abs(dx
), abs(dy
))))
644 o
->vx
+= (long) dx
* pwr
/ m
;
645 o
->vy
+= (long) dy
* pwr
/ m
;
648 int Z_cansee(int x
, int y
, int xd
, int yd
) {
668 if ((xd
= abs(xd
)) > (yd
= abs(yd
)))
673 for (i
= 0; i
<= d
;) {
674 if ((x
< 0) || (x
>= FLDW
* 8) || (y
< 0) || (y
>= FLDH
* 8))
677 if ((bmap
[y
>> 5][x
>> 5] & BM_WALL
)) {
678 if ((fld
[y
>> 3][x
>> 3] == 1) || (fld
[y
>> 3][x
>> 3] == 2))
681 if ((xe
+= (xd
<< 3)) >= d
) {
685 if ((ye
+= (yd
<< 3)) >= d
) {
707 if (((s
< m
) && (s
!= 0)) || (m
== 0))
710 x
+= (xd
* m
+ xe
) / d
* sx
;
711 xe
= (xd
* m
+ xe
) % d
;
712 y
+= (yd
* m
+ ye
) / d
* sy
;
713 ye
= (yd
* m
+ ye
) % d
;
719 int Z_look(obj_t
* a
, obj_t
* b
, int d
) {
720 if (Z_sign(b
->x
- a
->x
) != d
* 2 - 1)
723 return Z_cansee(a
->x
, a
->y
- a
->h
/ 2, b
->x
, b
->y
- b
->h
/ 2);
726 #define wvel(v) if ((xv = abs(v) + 1) > 5) v = Z_dec (v, xv / 2 - 2)
730 int Z_moveobj(obj_t
* p
) {
731 static int x
, y
, xv
, yv
, r
, h
, lx
, ly
, st
;
735 switch (Z_inlift(x
= p
->x
, y
= p
->y
, r
= p
->r
, h
= p
->h
)) {
737 if (++p
->yv
> MAX_YV
)
754 if ((inw
= Z_inwater(x
, y
, r
, h
)) != 0) {
761 p
->vx
= Z_dec(p
->vx
, 1);
762 p
->vy
= Z_dec(p
->vy
, 1);
766 if ((x
< -100) || (x
>= FLDW
* 8 + 100) || (y
< -100) || (y
>= FLDH
* 8 + 100))
770 x
+= (abs(xv
) <= 7) ? xv
: ((xv
> 0) ? 7 : -7);
772 if (Z_isblocked(x
, y
, r
, h
, xv
))
775 if (!Z_canfit(x
, y
, r
, h
)) {
779 x
= ((lx
- r
) & 0xFFF8) + r
;
781 x
= ((lx
+ r
) & 0xFFF8) - r
+ 7;
782 xv
= p
->xv
= p
->vx
= 0;
785 xv
-= (abs(xv
) <= 7) ? xv
: ((xv
> 0) ? 7 : -7);
788 y
+= (abs(yv
) <= 7) ? yv
: ((yv
> 0) ? 7 : -7);
791 if ((yv
< 0) && Z_hitceil(x
, y
, r
, h
)) {
792 y
= ((ly
- h
+ 1) & 0xFFF8) + h
- 1;
797 if ((yv
> 0) && Z_canstand(x
, y
, r
)) {
798 y
= ((y
+ 1) & 0xFFF8) - 1;
799 yv
= p
->yv
= p
->vy
= 0;
802 yv
-= (abs(yv
) <= 7) ? yv
: ((yv
> 0) ? 7 : -7);
806 if (Z_inwater(x
, y
, r
, h
)) {
816 void Z_splash(obj_t
* p
, int n
) {
817 Z_sound(bulsnd
[0], 128);
818 DOT_water(p
->x
, p
->y
- p
->h
/ 2, p
->xv
+ p
->vx
, p
->yv
+ p
->vy
, n
,
819 (int) walp
[wfront
] - 1);
822 void Z_calc_time(dword t
, word
* h
, word
* m
, word
* s
) {