DEADSOFTWARE

linux: fix build
[flatwaifu.git] / src / monster.c
1 /* Copyright (C) 1996-1997 Aleksey Volynskov
2 * Copyright (C) 2011 Rambo
3 * Copyright (C) 2020 SovietPony
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
18 #include "glob.h"
19 #include <stdlib.h>
20 #include <string.h>
21 #include "files.h"
22 #include "view.h"
23 #include "bmap.h"
24 #include "dots.h"
25 #include "weapons.h"
26 #include "player.h"
27 #include "monster.h"
28 #include "items.h"
29 #include "switch.h"
30 #include "misc.h"
31 #include "fx.h"
32 #include "smoke.h"
33 #include "my.h"
34 #include "player.h"
35 #include "error.h"
36 #include "game.h"
38 #define MAX_ATM 90
40 enum{
41 SLEEP,GO,RUN,CLIMB,DIE,DEAD,ATTACK,SHOOT,PAIN,WAIT,REVIVE,RUNOUT
42 };
44 typedef struct {
45 int r, h, l, mp, rv, jv, sp, minp;
46 } mnsz_t;
48 byte nomon = 1;
50 static char *sleepanim[MN_TN]={
51 "AAABBB","AAABBB","AAABBB","AAABBB","AAABBB","AAABBB","AAABBB","AAABBB",
52 "A","AAABBB","AAABBB","AAABBB","AAABBB","AAABBB","AAABBB","AAABBB","AAABBB",
53 "A","A","AAABBB"
54 }, *goanim[MN_TN]={
55 "AABBCCDD","AABBCCDD","AABBDDAACCDD","AABBDDAACCDD","AABBDDCCDDBB",
56 "AABBDDAACCDD","AABBCCDD","AABBCCDD","A","AABB","AABBCCBB",
57 "AABBCCDDEEFF","AABBCCDDEEFF","AABBCCDDEEFF","AABBCCDDEEFF","AABBCCDDEEFF",
58 "AABB","A","DDEEFFGGHHIIJJKKLLAABBCC","ACDABD"
59 }, *painanim[MN_TN]={
60 "H","H","G","G","G","G","H","H","F","E","G","I","I","J","L","Q","EECCDDCC",
61 "A","D","G"
62 }, *waitanim[MN_TN]={
63 "A","A","A","A","A","A","A","A","A","AABB","A","A","A","I","K","A","A",
64 "A","D","E"
65 }, *attackanim[MN_TN]={
66 "EEFFGG","EEFFGG","EEEEEF","EEEEEF","EEEEEF","EF","EEFFGG","EEFFGG",
67 "BBCCDD","CCDD","DDEEFF","GH","GH","GGGGHH","GGHHII",
68 "QQGGGHHHIIJJKKLLMMNNOOPP","BBFFAA","A","OOPPQQ","EEEEFF"
69 }, *dieanim[MN_TN]={
70 "IIIJJJKKKLLLMMM","IIIJJJKKKLLL","HHHIIIJJJKKK","HHHIIIJJJKKK",
71 "HHHIIIJJJKKKLLLMMMNNNOOO","HHHIIIJJJKKKLLLMMM",
72 "IIIJJJKKKLLLMMMNNN","IIIJJJKKKLLLMMMNNN","GGGHHHIIIJJJKKK",
73 "FFFGGGHHHIIIJJJKKK","HHHIIIJJJKKKLLLMMM",
74 "JJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRR","JJJKKKLLLMMMNNNOOO",
75 "KKKLLLMMMNNNOOOPPPRRRSSS","MMMNNNOOOPPP","RRRSSSTTTUUUVVVWWWXXXYYY",
76 "DDDD","CCCDDDEEEFFFGGG","D","HHHHIIIIJJJJKKKKLLLLMMMM"
77 }, *slopanim[MN_TN]={
78 "","NNNOOOPPPRRRSSSTTT","MMMNNNOOOPPPRRRSSSTTT","MMMNNNOOOPPPRRRSSSTTT","",
79 "OOOPPPQQQRRRSSS","","","","","","","","","","","","","","OOPPQQRRSSTTUUVV"
80 }, *deadanim[MN_TN]={
81 "N","M","L","L","P","N","O","O","L","","","S","P","T","Q","Z","C","","D","N"
82 }, *messanim[MN_TN]={
83 "","U","U","U","","T","","","","","","","","","","","","","","W"
84 };
86 int hit_xv, hit_yv;
87 mn_t mn[MAXMN];
88 int mnum, gsndt;
90 static void *fsnd,*pauksnd,*trupsnd;
91 static void *snd[MN_TN][5],*impsitsnd[2],*impdthsnd[2],*firsnd,*slopsnd,*gsnd[4];
92 static void *swgsnd,*pchsnd,*telesnd;
93 static void *positsnd[3],*podthsnd[3];
94 static mnsz_t mnsz[MN_TN+1]={
95 //rad ht life pain rv jv slop min_pn
96 0, 0, 0, 0, 0, 0, 0, 0, // none
97 15, 28, 60, 20, 7,10, 0, 10, // demon
98 10, 28, 25, 15, 3,10, 30, 0, // imp
99 10, 28, 15, 10, 3,10, 30, 0, // zomby
100 10, 28, 20, 10, 3,10, 30, 0, // sergeant
101 20, 55, 500, 70, 5,10, 0, 50, // cyberdemon
102 12, 28, 60, 20, 3,10, 30, 10, // chaingunner
103 12, 32, 150, 40, 3,10, 0, 30, // baron of hell
104 12, 32, 75, 40, 3,10, 0, 30, // hell knight
105 15, 28, 100, 10, 4, 4, 0, 0, // cacodemon
106 8, 18, 60, 10, 4, 4, 0, 0, // lost soul
107 15, 28, 100, 10, 4, 4, 0, 0, // pain elemental
108 64, 50, 500, 70, 4,10, 0, 50, // spider mastermind
109 25, 27, 150, 20, 4,10, 0, 0, // arachnotron
110 18, 30, 200, 40, 3, 7, 0, 20, // mancubus
111 17, 36, 200, 40, 6,11, 0, 20, // revenant
112 17, 36, 150, 30, 7,12, 0, 10, // archvile
113 5, 5, 35, 20,14, 6, 0, 10, // fish
114 5, 17, 20, 0, 7, 6, 0, 0, // barrel
115 17, 38, 20, 40, 3, 6, 0, 20, // robot
116 8, 26, 400, 70, 8,10, 30, 50 // man
117 };
119 void setst (int i, int st) {
120 char *a;
121 int t;
123 switch(mn[i].st) {
124 case DIE: case DEAD:
125 if(st!=DEAD && st!=REVIVE) return;
127 mn[i].ac=0;
128 t=mn[i].t-1;
129 switch(mn[i].st=st) {
130 case SLEEP: a=sleepanim[t];break;
131 case PAIN: a=painanim[t];break;
132 case WAIT: a=waitanim[t];break;
133 case CLIMB:
134 case RUN: case RUNOUT:
135 case GO: a=goanim[t];break;
136 case SHOOT:
137 if(t==MN_SKEL-1) {a="KKKKJJ";break;}
138 if(t==MN_ROBO-1) {a="MN";break;}
139 case ATTACK: a=attackanim[t];
140 if(st==ATTACK && t==MN_VILE-1) a="[[\\\\]]";
141 break;
142 case DIE:
143 if(g_map==9 && t==MN_BSP-1) Z_sound(pauksnd,128);
144 a=dieanim[t];break;
145 case DEAD:
146 a=deadanim[t];
147 if(mn[i].ap==slopanim[t]) a=messanim[t];
148 if(t==MN_BARREL-1) {mn[i].t=0;}
149 break;
150 case REVIVE:
151 a=(mn[i].ap==messanim[t])?slopanim[t]:dieanim[t];
152 mn[i].ac=strlen(a)-1;
153 mn[i].o.r=mnsz[t+1].r;mn[i].o.h=mnsz[t+1].h;
154 mn[i].life=mnsz[t+1].l;mn[i].ammo=mn[i].pain=0;
155 ++mnum;
156 break;
158 mn[i].ap=a;
161 #define GGAS_TOTAL (MN__LAST-MN_DEMON+16+10)
163 void MN_alloc (void) {
164 int i,j;
165 static char sn[MN_TN][5][6]={
166 {"DMACT","DMPAIN","SGTATK","SGTSIT","SGTDTH"},
167 {"BGACT","POPAIN","CLAW","",""},
168 {"POSACT","POPAIN","","",""},
169 {"POSACT","POPAIN","","",""},
170 {"","DMPAIN","HOOF","CYBSIT","CYBDTH"},
171 {"POSACT","POPAIN","","",""},
172 {"","DMPAIN","","BRSSIT","BRSDTH"},
173 {"","DMPAIN","","KNTSIT","KNTDTH"},
174 {"DMACT","DMPAIN","","CACSIT","CACDTH"},
175 {"DMACT","DMPAIN","SKLATK","SKLATK","FIRXPL"},
176 {"DMACT","PEPAIN","","PESIT","PEDTH"},
177 {"","DMPAIN","METAL","SPISIT","SPIDTH"},
178 {"BSPACT","DMPAIN","BSPWLK","BSPSIT","BSPDTH"},
179 {"DMACT","MNPAIN","MANATK","MANSIT","MANDTH"},
180 {"SKEACT","POPAIN","SKEATK","SKESIT","SKEDTH"},
181 {"VILACT","VIPAIN","VILATK","VILSIT","VILDTH"},
182 {"","","BITE1","",""},
183 {"","","","","BAREXP"},
184 {"BSPACT","","BSPWLK","BSPSIT","BSPDTH"},
185 {"HAHA1","PLPAIN","","STOP1","PDIEHI"}
186 };
187 static char gsn[6]="GOOD0";
188 for(j=0;j<MN_TN;++j) {
189 for(i=0;i<5;++i)
190 if(sn[j][i][0])
191 snd[j][i]=Z_getsnd(sn[j][i]);
192 else
193 snd[j][i]=NULL;
194 logo_gas(j+5,GGAS_TOTAL);
196 impsitsnd[0]=Z_getsnd("BGSIT1");
197 impsitsnd[1]=Z_getsnd("BGSIT2");
198 impdthsnd[0]=Z_getsnd("BGDTH1");
199 impdthsnd[1]=Z_getsnd("BGDTH2");
200 positsnd[0]=Z_getsnd("POSIT1");
201 positsnd[1]=Z_getsnd("POSIT2");
202 positsnd[2]=Z_getsnd("POSIT3");
203 podthsnd[0]=Z_getsnd("PODTH1");
204 podthsnd[1]=Z_getsnd("PODTH2");
205 podthsnd[2]=Z_getsnd("PODTH3");
206 fsnd=Z_getsnd("FLAME");
207 firsnd=Z_getsnd("FIRSHT");
208 slopsnd=Z_getsnd("SLOP");
209 swgsnd=Z_getsnd("SKESWG");
210 pchsnd=Z_getsnd("SKEPCH");
211 telesnd=Z_getsnd("TELEPT");
212 pauksnd=Z_getsnd("PAUK1");
213 trupsnd=Z_getsnd("UTRUP");
214 for(i=0;i<4;++i) {gsn[4]=i+'1';gsnd[i]=Z_getsnd(gsn);}
217 void MN_init (void) {
218 int i;
219 for(i=0;i<MAXMN;++i) {mn[i].t=0;mn[i].st=SLEEP;}
220 gsndt=mnum=0;
223 int MN_spawn (int x, int y, byte d, int t) {
224 int i;
226 if(g_dm && nomon && t<MN_PL_DEAD) return -1;
227 for(i=0;i<MAXMN;++i) if(!mn[i].t) goto ok;
228 for(i=0;i<MAXMN;++i) if(mn[i].t>=MN_PL_DEAD) goto ok;
229 return -1;
230 ok:
231 mn[i].o.x=x;mn[i].o.y=y;
232 mn[i].o.xv=mn[i].o.yv=mn[i].o.vx=mn[i].o.vy=0;
233 mn[i].d=d;mn[i].t=t;
234 mn[i].st=SLEEP;
235 if(t<MN_PL_DEAD) {
236 mn[i].o.r=mnsz[t].r;mn[i].o.h=mnsz[t].h;
237 mn[i].life=mnsz[t].l;
238 setst(i,SLEEP);mn[i].s=myrand(18);
239 ++mnum;
240 }else {mn[i].o.r=8;mn[i].o.h=6;mn[i].life=0;mn[i].st=DEAD;}
241 mn[i].aim=-3;mn[i].atm=0;
242 mn[i].pain=0;
243 mn[i].ammo=0;
244 mn[i].ftime=0;
245 return i;
248 int MN_spawn_deadpl (obj_t *o, byte c, int t) {
249 int i;
251 if((i=MN_spawn(o->x,o->y,c,t+MN_PL_DEAD))==-1) return -1;
252 mn[i].o=*o;return i;
255 static int isfriend(int a,int b) {
256 if(a==MN_BARREL || b==MN_BARREL) return 1;
257 if(a==b) switch(a) {
258 case MN_IMP: case MN_DEMON:
259 case MN_BARON: case MN_KNIGHT:
260 case MN_CACO: case MN_SOUL:
261 case MN_MANCUB: case MN_SKEL:
262 case MN_FISH:
263 return 1;
265 if(a==MN_SOUL && b==MN_PAIN) return 1;
266 if(b==MN_SOUL && a==MN_PAIN) return 1;
267 return 0;
270 static int MN_findnewprey(int i) {
271 int a,b,l;
273 a=!PL_isdead(&pl1);
274 if(_2pl) b=!PL_isdead(&pl2); else b=0;
275 if(a) {
276 if(b) mn[i].aim=(abs(mn[i].o.x-pl1.o.x)+abs(mn[i].o.y-pl1.o.y)
277 <= abs(mn[i].o.x-pl2.o.x)+abs(mn[i].o.y-pl2.o.y))?-1:-2;
278 else mn[i].aim=-1;
279 }else{
280 if(b) mn[i].aim=-2;
281 else{
282 for(a=0,b=32000,mn[i].aim=-3;a<MAXMN;++a)
283 if(mn[a].t && mn[a].st!=DEAD && a!=i && !isfriend(mn[a].t,mn[i].t))
284 if((l=abs(mn[i].o.x-mn[a].o.x)+abs(mn[i].o.y-mn[a].o.y))<b)
285 {mn[i].aim=a;b=l;}
286 if(mn[i].aim<0) {mn[i].atm=MAX_ATM;return 0;} else mn[i].atm=0;
289 return 1;
292 int Z_getobjpos (int i, obj_t *o) {
293 if(i==-1) {*o=pl1.o;return !PL_isdead(&pl1);}
294 if(_2pl) if(i==-2) {*o=pl2.o;return !PL_isdead(&pl2);}
295 if(i>=0 && i<MAXMN) if(mn[i].t && mn[i].st!=DEAD)
296 {*o=mn[i].o;return 1;}
297 return 0;
300 static void *wakeupsnd(int t) {
301 switch(t) {
302 case MN_IMP: return impsitsnd[myrand(2)];
303 case MN_ZOMBY: case MN_SERG: case MN_CGUN:
304 return positsnd[myrand(3)];
306 return snd[t-1][3];
309 static void *dthsnd(int t) {
310 switch(t) {
311 case MN_IMP: return impdthsnd[myrand(2)];
312 case MN_ZOMBY: case MN_SERG: case MN_CGUN:
313 return podthsnd[myrand(3)];
315 return snd[t-1][4];
318 static int canshoot(int t) {
319 switch(t) {
320 case MN_DEMON: case MN_FISH: case MN_BARREL:
321 return 0;
323 return 1;
326 static int shoot(int i,obj_t *o,int n) {
327 int xd,yd,m;
329 if(mn[i].ammo<0) return 0;
330 if(!n) switch(mn[i].t) {
331 case MN_FISH: case MN_BARREL:
332 case MN_DEMON: return 0;
333 case MN_CGUN:
334 case MN_BSP:
335 case MN_ROBO:
336 if(++mn[i].ammo>=50) mn[i].ammo=(mn[i].t==MN_ROBO)?-200:-50;
337 break;
338 case MN_MAN:
339 break;
340 case MN_MANCUB:
341 if(++mn[i].ammo>=5) mn[i].ammo=-50;
342 break;
343 case MN_SPIDER:
344 if(++mn[i].ammo>=100) mn[i].ammo=-50;
345 break;
346 case MN_CYBER:
347 if(rand()&1) return 0;
348 if(++mn[i].ammo>=10) mn[i].ammo=-50;
349 break;
350 case MN_BARON: case MN_KNIGHT:
351 if(rand()&7) return 0;
352 break;
353 case MN_SKEL:
354 if(rand()&31) return 0;
355 break;
356 case MN_VILE:
357 if(rand()&7) return 0;
358 break;
359 case MN_PAIN:
360 if(rand()&7) return 0;
361 break;
362 default:
363 if(rand()&15) return 0;
365 if(!Z_look(&mn[i].o,o,mn[i].d)) return 0;
366 mn[i].atm=0;
367 mn[i].tx=o->x+(o->xv+o->vx)*6;mn[i].ty=o->y-o->h/2+(o->yv+o->vy)*6;
368 if(abs(mn[i].tx-mn[i].o.x)<abs(mn[i].ty-mn[i].o.y+mn[i].o.h/2)) return 0;
369 switch(mn[i].t) {
370 case MN_IMP: case MN_BARON: case MN_KNIGHT: case MN_CACO:
371 setst(i,SHOOT);Z_sound(firsnd,128);break;
372 case MN_SKEL:
373 setst(i,SHOOT);Z_sound(snd[MN_SKEL-1][2],128);break;
374 case MN_VILE:
375 mn[i].tx=o->x;mn[i].ty=o->y;
376 setst(i,SHOOT);Z_sound(fsnd,128);
377 Z_sound(snd[MN_VILE-1][2],128);break;
378 case MN_SOUL:
379 setst(i,ATTACK);Z_sound(snd[MN_SOUL-1][2],128);
380 yd=mn[i].ty-mn[i].o.y+mn[i].o.h/2;xd=mn[i].tx-mn[i].o.x;
381 if(!(m=max(abs(xd),abs(yd)))) m=1;
382 mn[i].o.xv=xd*16/m;mn[i].o.yv=yd*16/m;
383 break;
384 case MN_MANCUB: if(mn[i].ammo==1) Z_sound(snd[MN_MANCUB-1][2],128);
385 case MN_ZOMBY: case MN_SERG: case MN_BSP: case MN_ROBO:
386 case MN_CYBER: case MN_CGUN: case MN_SPIDER:
387 case MN_PAIN: case MN_MAN:
388 setst(i,SHOOT);break;
389 default:
390 return 0;
392 return 1;
395 static int kick(int i,obj_t *o) {
396 switch(mn[i].t) {
397 case MN_FISH:
398 setst(i,ATTACK);return 1;
399 case MN_DEMON:
400 setst(i,ATTACK);Z_sound(snd[0][2],128);return 1;
401 case MN_IMP:
402 setst(i,ATTACK);Z_sound(snd[1][2],128);return 1;
403 case MN_SKEL:
404 setst(i,ATTACK);Z_sound(swgsnd,128);return 1;
405 case MN_ROBO:
406 setst(i,ATTACK);Z_sound(swgsnd,128);return 1;
407 case MN_BARON: case MN_KNIGHT: case MN_CACO: case MN_MANCUB:
408 return shoot(i,o,1);
410 return 0;
413 static int iscorpse(obj_t *o,int n) {
414 int i;
416 if(!n) if(rand()&7) return -3;
417 for(i=0;i<MAXMN;++i) if(mn[i].t) if(mn[i].st==DEAD)
418 if(Z_overlap(o,&mn[i].o)) switch(mn[i].t) {
419 case MN_SOUL: case MN_PAIN:
420 case MN_CYBER: case MN_SPIDER:
421 case MN_PL_DEAD: case MN_PL_MESS:
422 case MN_VILE: case MN_BARREL:
423 continue;
424 default:
425 return i;
427 return -3;
430 static int Z_hitobj (int obj, int d, int own, int t) {
431 hit_xv=hit_yv=0;
432 if(obj==-1) return PL_hit(&pl1,d,own,t);
433 else if(obj==-2 && _2pl) return PL_hit(&pl2,d,own,t);
434 else if(obj<0 || obj>=MAXMN) return 0;
435 if(mn[obj].t) return MN_hit(obj,d,own,t);
436 return 0;
439 void MN_act (void) {
440 int i,st,sx,sy,t;
441 static obj_t o;
442 static int pt_x=0,pt_xs=1,pt_y=0,pt_ys=1;
444 if(abs(pt_x+=pt_xs) > 123) pt_xs=-pt_xs;
445 if(abs(pt_y+=pt_ys) > 50) pt_ys=-pt_ys;
446 if(gsndt>0) if(--gsndt==0) {
447 Z_sound(gsnd[myrand(4)],128);
449 for(i=0;i<MAXMN;++i) if((t=mn[i].t)!=0) {
450 switch(t) {
451 case MN_FISH:
452 if(!Z_inwater(mn[i].o.x,mn[i].o.y,mn[i].o.r,mn[i].o.h)) break;
453 case MN_SOUL: case MN_PAIN: case MN_CACO:
454 if(mn[i].st!=DIE && mn[i].st!=DEAD) --mn[i].o.yv;
455 break;
456 }z_mon=1;st=Z_moveobj(&mn[i].o);z_mon=0;
457 BM_mark(&mn[i].o,BM_MONSTER);
458 if(st&Z_FALLOUT) {
459 if(t==MN_ROBO) g_exit=1;
460 mn[i].t=0;--mnum;continue;
462 if(st&Z_HITWATER) Z_splash(&mn[i].o,mn[i].o.r+mn[i].o.h);
463 SW_press(mn[i].o.x,mn[i].o.y,mn[i].o.r,mn[i].o.h,8,i);
464 if(mn[i].ftime) {
465 --mn[i].ftime;
466 SMK_flame(mn[i].o.x,mn[i].o.y-mn[i].o.h/2,
467 mn[i].o.xv+mn[i].o.vx,mn[i].o.yv+mn[i].o.vy,
468 mn[i].o.r/2,mn[i].o.h/2,rand()%(200*2+1)-200,-500,1,mn[i].fobj);
470 if(st&Z_INWATER) mn[i].ftime=0;
471 if(mn[i].st==DEAD) continue;
472 if(st&Z_INWATER) if(!(rand()&31)) switch(t) {
473 case MN_FISH:
474 if(rand()&3) break;
475 case MN_ROBO: case MN_BARREL:
476 case MN_PL_DEAD: case MN_PL_MESS:
477 FX_bubble(mn[i].o.x+((rand()&1)*2-1)*myrand(mn[i].o.r+1),
478 mn[i].o.y-myrand(mn[i].o.h+1),0,0,1
479 );
480 break;
481 default:
482 FX_bubble(mn[i].o.x,mn[i].o.y-mn[i].o.h*3/4,0,0,5);
485 if(t==MN_BARREL) {
487 if(!mn[i].ap[++mn[i].ac]) {
488 mn[i].ac=0;if(mn[i].st==DIE || mn[i].st==DEAD) {mn[i].t=0;}
489 }else if(mn[i].st==DIE && mn[i].ac==2) Z_explode(mn[i].o.x,mn[i].o.y-8,30,mn[i].aim);
490 continue;
492 if(t==MN_SOUL) if(st&Z_HITAIR) Z_set_speed(&mn[i].o,16);
493 if(mn[i].ammo<0) ++mn[i].ammo;
494 if(mn[i].o.yv<0)
495 if(st&Z_INWATER) mn[i].o.yv=-4;
496 ++mn[i].atm;
497 switch(mn[i].st) {
498 case PAIN:
499 if(mn[i].pain>=mnsz[t].mp)
500 {mn[i].pain=mnsz[t].mp;Z_sound(snd[t-1][1],128);}
501 if((mn[i].pain-=5)<=mnsz[t].minp)
502 {setst(i,GO);mn[i].pain=0;mn[i].ammo=-9;}
503 break;
504 case SLEEP:
505 if(++mn[i].s>=18) mn[i].s=0; else break;
506 if(Z_look(&mn[i].o,&pl1.o,mn[i].d))
507 {setst(i,GO);mn[i].aim=-1;mn[i].atm=0;Z_sound(wakeupsnd(t),128);}
508 if(_2pl) if(Z_look(&mn[i].o,&pl2.o,mn[i].d))
509 {setst(i,GO);mn[i].aim=-2;mn[i].atm=0;Z_sound(wakeupsnd(t),128);}
510 break;
511 case WAIT:
512 if(--mn[i].s<0) setst(i,GO);
513 break;
514 case GO:
515 if(st&Z_BLOCK) {mn[i].d^=1;setst(i,RUNOUT);mn[i].s=40;break;}
516 if(t==MN_VILE) if(iscorpse(&mn[i].o,0)>=0) {
517 setst(i,ATTACK);mn[i].o.xv=0;break;
519 if(!Z_getobjpos(mn[i].aim,&o) || mn[i].atm>MAX_ATM)
520 if(!MN_findnewprey(i)) {
521 mn[i].aim=-3;
522 o.x=mn[i].o.x+pt_x;o.y=mn[i].o.y+pt_y;
523 o.xv=o.vx=o.yv=o.vy=o.r=0;o.h=1;
524 }else Z_getobjpos(mn[i].aim,&o);
525 if(Z_overlap(&mn[i].o,&o)) {
526 mn[i].atm=0;
527 if(kick(i,&o)) break;
529 sx=o.x-mn[i].o.x;
530 sy=o.y-o.h/2-mn[i].o.y+mn[i].o.h/2;
531 if(!(st&Z_BLOCK)) if(abs(sx)<20)
532 if(t!=MN_FISH) {setst(i,RUN);mn[i].s=15;mn[i].d=rand()&1;break;}
533 if(st&Z_HITWALL) {
534 if(SW_press(mn[i].o.x,mn[i].o.y,mn[i].o.r,mn[i].o.h,2,i))
535 {setst(i,WAIT);mn[i].s=4;break;}
536 switch(t) {
537 case MN_CACO: case MN_SOUL: case MN_PAIN: case MN_FISH:
538 break;
539 default:
540 if(Z_canstand(mn[i].o.x,mn[i].o.y,mn[i].o.r))
541 {mn[i].o.yv=-mnsz[t].jv;setst(i,CLIMB);break;}
542 }break;
544 mn[i].d=(sx>0)?1:0;
545 if(canshoot(t))
546 if(abs(sx)>abs(sy)) if(shoot(i,&o,0)) break;
548 switch(t) {
549 case MN_FISH:
550 if(!(st&Z_INWATER)) {
551 if(Z_canstand(mn[i].o.x,mn[i].o.y,mn[i].o.r)) {
552 mn[i].o.yv=-6;
553 mn[i].o.vx+=rand()%17-8;
554 }setst(i,PAIN);mn[i].pain+=50;break;
556 case MN_CACO: case MN_SOUL: case MN_PAIN:
557 if(abs(sy)>4) mn[i].o.yv=(sy<0)?-4:4; else mn[i].o.yv=0;
558 if(t==MN_FISH) if(mn[i].o.yv<0)
559 if(!Z_inwater(mn[i].o.x,mn[i].o.y-8,mn[i].o.r,mn[i].o.h))
560 {mn[i].o.yv=0;setst(i,RUN);mn[i].d=rand()&1;mn[i].s=20;}
561 break;
562 default:
563 if(sy<-20) if(Z_canstand(mn[i].o.x,mn[i].o.y,mn[i].o.r))
564 if(!(rand()&3)) mn[i].o.yv=-mnsz[t].jv;
566 if(++mn[i].s>=8) {
567 mn[i].s=0;
568 if(!(rand()&7)) Z_sound(snd[t-1][0],128);
570 mn[i].o.xv=((mn[i].d)?1:-1)*mnsz[t].rv;
571 if(st&Z_INWATER) mn[i].o.xv/=2;
572 else if(t==MN_FISH) mn[i].o.xv=0;
573 break;
574 case RUN:
575 if(st&Z_BLOCK) {setst(i,RUNOUT);mn[i].d^=1;mn[i].s=40;break;}
576 if(--mn[i].s<=0 || ((st&Z_HITWALL) && mn[i].o.yv+mn[i].o.vy==0)) {
577 setst(i,GO);mn[i].s=0;if(st&(Z_HITWALL|Z_BLOCK)) mn[i].d^=1;
578 if(!(rand()&7)) Z_sound(snd[t-1][0],128);
579 }mn[i].o.xv=((mn[i].d)?1:-1)*mnsz[t].rv;
580 if(st&Z_INWATER) mn[i].o.xv/=2;
581 else if(t==MN_FISH) mn[i].o.xv=0;
582 break;
583 case RUNOUT:
584 if(!(st&Z_BLOCK) && mn[i].s>0) mn[i].s=0;
585 if(--mn[i].s<=-18) {
586 setst(i,GO);mn[i].s=0;if(st&(Z_HITWALL|Z_BLOCK)) mn[i].d^=1;
587 if(!(rand()&7)) Z_sound(snd[t-1][0],128);
588 }mn[i].o.xv=((mn[i].d)?1:-1)*mnsz[t].rv;
589 if(st&Z_INWATER) mn[i].o.xv/=2;
590 else if(t==MN_FISH) mn[i].o.xv=0;
591 break;
592 case CLIMB:
593 if(mn[i].o.yv+mn[i].o.vy>=0 || !(st&Z_HITWALL)) {
594 setst(i,GO);mn[i].s=0;
595 if(st&(Z_HITWALL|Z_BLOCK)) {mn[i].d^=1;setst(i,RUN);mn[i].s=15;}
596 }mn[i].o.xv=((mn[i].d)?1:-1)*mnsz[t].rv;
597 if(st&Z_INWATER) mn[i].o.xv/=2;
598 else if(t==MN_FISH) mn[i].o.xv=0;
599 break;
600 case ATTACK:
601 case SHOOT:
602 if(t==MN_SOUL) {if(st&(Z_HITWALL|Z_HITCEIL|Z_HITLAND)) setst(i,GO); break;}
603 if(t!=MN_FISH) mn[i].o.xv=Z_dec(mn[i].o.xv,1);
604 if(t==MN_VILE && mn[i].st==SHOOT) {
605 if(!Z_getobjpos(mn[i].aim,&o)) {setst(i,GO);break;}
606 if(!Z_look(&mn[i].o,&o,mn[i].d)) {setst(i,GO);break;}
607 if(Z_inwater(o.x,o.y,o.r,o.h)) {setst(i,GO);break;}
608 mn[i].tx=o.x;mn[i].ty=o.y;
609 Z_hitobj(mn[i].aim,2,i,HIT_SOME);
610 }break;
612 if(mn[i].st==REVIVE) {
613 if(--mn[i].ac==0) setst(i,GO);
614 }else ++mn[i].ac;
615 if(!mn[i].ap[mn[i].ac]) switch(mn[i].st) {
616 case ATTACK:
617 switch(t) {
618 case MN_SOUL: mn[i].ac=0;
619 case MN_IMP:
620 case MN_DEMON:
621 if(Z_hit(&mn[i].o,15,i,HIT_SOME)) if(t==MN_SOUL) setst(i,GO);
622 break;
623 case MN_FISH:
624 if(Z_hit(&mn[i].o,10,i,HIT_SOME))
625 Z_sound(snd[MN_FISH-1][2],128);
626 break;
627 case MN_SKEL: case MN_ROBO:
628 o=mn[i].o;o.xv=mn[i].d?50:-50;
629 if(Z_hit(&o,50,i,HIT_SOME)) Z_sound(pchsnd,128);
630 break;
631 case MN_VILE:
632 sx=iscorpse(&mn[i].o,1);
633 if(sx==-3) break;
634 if(!mn[sx].t || mn[sx].st!=DEAD) break;
635 setst(sx,REVIVE);Z_sound(slopsnd,128);
636 hit_xv=hit_yv=0;MN_hit(i,5,-3,HIT_SOME);
637 break;
638 }if(t!=MN_SOUL && mn[i].st!=DIE) setst(i,GO);
639 break;
640 case SHOOT:
641 switch(t) {
642 case MN_IMP:
643 WP_ball1(mn[i].o.x+(mn[i].d*2-1)*mn[i].o.r,mn[i].o.y-mn[i].o.h/2,mn[i].tx,mn[i].ty,i);
644 break;
645 case MN_ZOMBY:
646 WP_pistol(mn[i].o.x+(mn[i].d*2-1)*mn[i].o.r,mn[i].o.y-mn[i].o.h/2,mn[i].tx,mn[i].ty,i);
647 break;
648 case MN_SERG:
649 WP_shotgun(mn[i].o.x+(mn[i].d*2-1)*mn[i].o.r,mn[i].o.y-mn[i].o.h/2,mn[i].tx,mn[i].ty,i);
650 break;
651 case MN_MAN:
652 WP_dshotgun(mn[i].o.x+(mn[i].d*2-1)*mn[i].o.r,mn[i].o.y-mn[i].o.h/2,mn[i].tx,mn[i].ty,i);
653 mn[i].ammo=-36;break;
654 case MN_CYBER:
655 WP_rocket(mn[i].o.x+(mn[i].d*2-1)*mn[i].o.r,mn[i].o.y-mn[i].o.h/2,mn[i].tx,mn[i].ty,i);
656 break;
657 case MN_SKEL:
658 WP_revf(mn[i].o.x+(mn[i].d*2-1)*mn[i].o.r,mn[i].o.y-mn[i].o.h/2,mn[i].tx,mn[i].ty,i,mn[i].aim);
659 break;
660 case MN_CGUN:
661 case MN_SPIDER:
662 WP_mgun(mn[i].o.x+(mn[i].d*2-1)*mn[i].o.r,mn[i].o.y-mn[i].o.h/2,mn[i].tx,mn[i].ty,i);
663 break;
664 case MN_BSP:
665 WP_aplasma(mn[i].o.x+(mn[i].d*2-1)*mn[i].o.r,mn[i].o.y-mn[i].o.h/2,mn[i].tx,mn[i].ty,i);
666 break;
667 case MN_ROBO:
668 WP_plasma(mn[i].o.x+(mn[i].d*2-1)*15,mn[i].o.y-30,mn[i].tx,mn[i].ty,i);
669 break;
670 case MN_MANCUB:
671 WP_manfire(mn[i].o.x+(mn[i].d*2-1)*mn[i].o.r,mn[i].o.y-mn[i].o.h/2,mn[i].tx,mn[i].ty,i);
672 break;
673 case MN_BARON: case MN_KNIGHT:
674 WP_ball7(mn[i].o.x,mn[i].o.y-mn[i].o.h/2,mn[i].tx,mn[i].ty,i);
675 break;
676 case MN_CACO:
677 WP_ball2(mn[i].o.x,mn[i].o.y-mn[i].o.h/2,mn[i].tx,mn[i].ty,i);
678 break;
679 case MN_PAIN:
680 if((sx=MN_spawn(mn[i].o.x,mn[i].o.y,mn[i].d,MN_SOUL))==-1) break;
681 Z_getobjpos(mn[sx].aim=mn[i].aim,&o);mn[sx].atm=0;
682 shoot(sx,&o,1);
683 break;
685 if(t==MN_CGUN || t==MN_SPIDER || t==MN_BSP || t==MN_MANCUB || t==MN_ROBO)
686 if(!Z_getobjpos(mn[i].aim,&o)) MN_findnewprey(i);
687 else if(shoot(i,&o,0)) break;
688 setst(i,GO);break;
689 case DIE:
690 setst(i,DEAD);
691 if(t==MN_PAIN || t==MN_SOUL) mn[i].ftime=0;
692 if(t==MN_PAIN) {
693 if((sx=MN_spawn(mn[i].o.x-15,mn[i].o.y,0,MN_SOUL))==-1) break;
694 setst(sx,GO);
695 if((sx=MN_spawn(mn[i].o.x+15,mn[i].o.y,1,MN_SOUL))==-1) break;
696 setst(sx,GO);
697 if((sx=MN_spawn(mn[i].o.x,mn[i].o.y-10,1,MN_SOUL))==-1) break;
698 setst(sx,GO);
699 }break;
700 default: mn[i].ac=0;
702 switch(mn[i].st) {
703 case GO: case RUN: case CLIMB: case RUNOUT:
704 if(t==MN_CYBER || t==MN_SPIDER || t==MN_BSP) {
705 if(mn[i].ac==0 || mn[i].ac==6) Z_sound(snd[t-1][2],128);
706 }else if(t==MN_ROBO)
707 if(mn[i].ac==0 || mn[i].ac==12) Z_sound(snd[t-1][2],128);
712 void MN_mark (void) {
713 int i;
714 for(i=0;i<MAXMN;++i) if(mn[i].t!=0) BM_mark(&mn[i].o,BM_MONSTER);
717 int MN_hit(int n,int d,int o,int t) {
718 int i;
720 if(mn[n].st==DEAD || mn[n].st==DIE) return 0;
721 if(o==n) {
722 if(t!=HIT_ROCKET && t!=HIT_ELECTRO) return 0;
723 if(mn[n].t==MN_CYBER || mn[n].t==MN_BARREL) return 1;
725 if(o>=0) {
726 if(mn[o].t==MN_SOUL && mn[n].t==MN_PAIN) return 0;
727 if(mn[o].t==mn[n].t) switch(mn[n].t) {
728 case MN_IMP: case MN_DEMON:
729 case MN_BARON: case MN_KNIGHT:
730 case MN_CACO: case MN_SOUL:
731 case MN_MANCUB: case MN_SKEL:
732 case MN_FISH:
733 return 0;
736 if(t==HIT_FLAME) if(mn[n].ftime && mn[n].fobj==o) {if(g_time&31) return 1;}
737 else {mn[n].ftime=255;mn[n].fobj=o;}
738 if(t==HIT_ELECTRO) if(mn[n].t==MN_FISH)
739 {setst(n,RUN);mn[n].s=20;mn[n].d=rand()&1;return 1;}
740 if(t==HIT_TRAP) mn[n].life=-100;
741 if(mn[n].t==MN_ROBO) d=0;
742 if((mn[n].life-=d)<=0) --mnum;
743 if(!mn[n].pain) mn[n].pain=3;
744 mn[n].pain+=d;
745 if(mn[n].st!=PAIN) {
746 if(mn[n].pain>=mnsz[mn[n].t].minp) setst(n,PAIN);
748 if(mn[n].t!=MN_BARREL)
749 DOT_blood(mn[n].o.x,mn[n].o.y-mn[n].o.h/2,hit_xv,hit_yv,d*2);
750 mn[n].aim=o;mn[n].atm=0;
751 if(mn[n].life<=0) {
752 if(mn[n].t!=MN_BARREL)
753 if(o==-1) ++pl1.kills;
754 else if(o==-2) ++pl2.kills;
755 setst(n,DIE);
756 switch(mn[n].t) {
757 case MN_ZOMBY: i=I_CLIP;break;
758 case MN_SERG: i=I_SGUN;break;
759 case MN_CGUN: i=I_MGUN;break;
760 case MN_MAN: i=I_KEYR;break;
761 default: i=0;
762 }if(i) IT_spawn(mn[n].o.x,mn[n].o.y,i);
763 mn[n].o.xv=0;mn[n].o.h=6;
764 if(mn[n].life<=-mnsz[mn[n].t].sp)
765 switch(mn[n].t) {
766 case MN_IMP: case MN_ZOMBY: case MN_SERG: case MN_CGUN:
767 case MN_MAN:
768 mn[n].ap=slopanim[mn[n].t-1];
769 Z_sound(slopsnd,128);
770 break;
771 case MN_BSP: if(g_map==9) break;
772 default:
773 Z_sound(dthsnd(mn[n].t),128);
775 else if(mn[n].t!=MN_BSP || g_map!=9) Z_sound(dthsnd(mn[n].t),128);
776 mn[n].life=0;
777 }else if(mn[n].st==SLEEP) {setst(n,GO);mn[n].pain=mnsz[mn[n].t].mp;}
778 return 1;
781 #define hit(o,x,y) (y<=o.y && y>o.y-o.h && x>=o.x-o.r && x<=o.x+o.r)
783 int Z_gunhit (int x, int y, int o, int xv, int yv) {
784 int i;
786 if(o!=-1) if(hit(pl1.o,x,y)) if(PL_hit(&pl1,3,o,HIT_SOME))
787 {pl1.o.vx+=xv;pl1.o.vy+=yv;return -1;}
788 if(_2pl && o!=-2) if(hit(pl2.o,x,y)) if(PL_hit(&pl2,3,o,HIT_SOME))
789 {pl2.o.vx+=xv;pl2.o.vy+=yv;return -2;}
791 for(i=0;i<MAXMN;++i) if(mn[i].t && o!=i)
792 if(hit(mn[i].o,x,y)) if(MN_hit(i,3,o,HIT_SOME))
793 {mn[i].o.vx+=xv;mn[i].o.vy+=yv;return 1;}
794 return 0;
797 static void goodsnd(void) {
798 if(!g_dm) return;
799 gsndt=18;
802 int Z_hit (obj_t *o, int d, int own, int t) {
803 int i;
805 hit_xv=o->xv+o->vx;
806 hit_yv=o->yv+o->vy;
807 if(Z_overlap(o,&pl1.o)) if(PL_hit(&pl1,d,own,t)) {
808 pl1.o.vx+=(o->xv+o->vx)*((t==HIT_BFG)?8:1)/4;
809 pl1.o.vy+=(o->yv+o->vy)*((t==HIT_BFG)?8:1)/4;
810 if(t==HIT_BFG) goodsnd();
811 return -1;
813 if(_2pl) if(Z_overlap(o,&pl2.o)) if(PL_hit(&pl2,d,own,t)) {
814 pl2.o.vx+=(o->xv+o->vx)*((t==HIT_BFG)?8:1)/4;
815 pl2.o.vy+=(o->yv+o->vy)*((t==HIT_BFG)?8:1)/4;
816 if(t==HIT_BFG) goodsnd();
817 return -2;
820 for(i=0;i<MAXMN;++i) if(mn[i].t)
821 if(Z_overlap(o,&mn[i].o)) if(MN_hit(i,d,own,t)) {
822 mn[i].o.vx+=(o->xv+o->vx)*((t==HIT_BFG)?8:1)/4;
823 mn[i].o.vy+=(o->yv+o->vy)*((t==HIT_BFG)?8:1)/4;
824 return 1;
826 return 0;
829 void MN_killedp (void) {
830 int i;
831 for(i=0;i<MAXMN;++i) if(mn[i].t==MN_MAN)
832 if(mn[i].st!=DEAD && mn[i].st!=DIE && mn[i].st!=SLEEP)
833 Z_sound(trupsnd,128);
836 void Z_explode (int x,int y,int rad,int own) {
837 long r;
838 int dx,dy,m,i;
840 if(x<-100 || x>FLDW*CELW+100) return;
841 if(y<-100 || y>FLDH*CELH+100) return;
842 r=(long)rad*rad;
843 dx=pl1.o.x-x;dy=pl1.o.y-pl1.o.h/2-y;
844 if((long)dx*dx+(long)dy*dy<r) {
845 if(!(m=max(abs(dx),abs(dy)))) m=1;
846 pl1.o.vx+=hit_xv=dx*10/m;
847 pl1.o.vy+=hit_yv=dy*10/m;
848 PL_hit(&pl1,100*(rad-m)/rad,own,HIT_ROCKET);
850 if(_2pl) {
851 dx=pl2.o.x-x;dy=pl2.o.y-pl2.o.h/2-y;
852 if((long)dx*dx+(long)dy*dy<r) {
853 if(!(m=max(abs(dx),abs(dy)))) m=1;
854 pl2.o.vx+=hit_xv=dx*10/m;
855 pl2.o.vy+=hit_yv=dy*10/m;
856 PL_hit(&pl2,100*(rad-m)/rad,own,HIT_ROCKET);
859 for(i=0;i<MAXMN;++i) if(mn[i].t) {
860 dx=mn[i].o.x-x;dy=mn[i].o.y-mn[i].o.h/2-y;
861 if((long)dx*dx+(long)dy*dy<r) {
862 if(!(m=max(abs(dx),abs(dy)))) m=1;
863 mn[i].o.vx+=hit_xv=dx*10/m;
864 mn[i].o.vy+=hit_yv=dy*10/m;
865 MN_hit(i,mn[i].o.r*10*(rad-m)/rad,own,HIT_ROCKET);
870 void Z_bfg9000 (int x,int y,int own) {
871 int dx,dy,i;
873 hit_xv=hit_yv=0;
874 if(x<-100 || x>FLDW*CELW+100) return;
875 if(y<-100 || y>FLDH*CELH+100) return;
876 dx=pl1.o.x-x;dy=pl1.o.y-pl1.o.h/2-y;
877 if(own!=-1) if((long)dx*dx+(long)dy*dy<16000)
878 if(Z_cansee(x,y,pl1.o.x,pl1.o.y-pl1.o.h/2)) {
879 if(PL_hit(&pl1,50,own,HIT_SOME))
880 WP_bfghit(pl1.o.x,pl1.o.y-pl1.o.h/2,own);
882 if(_2pl) {
883 dx=pl2.o.x-x;dy=pl2.o.y-pl2.o.h/2-y;
884 if(own!=-2) if((long)dx*dx+(long)dy*dy<16000)
885 if(Z_cansee(x,y,pl2.o.x,pl2.o.y-pl2.o.h/2)) {
886 if(PL_hit(&pl2,50,own,HIT_SOME))
887 WP_bfghit(pl2.o.x,pl2.o.y-pl2.o.h/2,own);
890 for(i=0;i<MAXMN;++i) if(mn[i].t && own!=i) {
891 dx=mn[i].o.x-x;dy=mn[i].o.y-mn[i].o.h/2-y;
892 if((long)dx*dx+(long)dy*dy<16000)
893 if(Z_cansee(x,y,mn[i].o.x,mn[i].o.y-mn[i].o.h/2)) {
894 if(MN_hit(i,50,own,HIT_SOME))
895 WP_bfghit(mn[i].o.x,mn[i].o.y-mn[i].o.h/2,own);
900 int Z_chktrap (int t, int d, int o, int ht) {
901 int i,s;
903 hit_xv=hit_yv=0;
904 s=0;
905 if(Z_istrapped(pl1.o.x,pl1.o.y,pl1.o.r,pl1.o.h)) {
906 s=1;
907 if(t) PL_hit(&pl1,d,o,ht);
909 if(_2pl) if(Z_istrapped(pl2.o.x,pl2.o.y,pl2.o.r,pl2.o.h)) {
910 s=1;
911 if(t) PL_hit(&pl2,d,o,ht);
913 for(i=0;i<MAXMN;++i) if(mn[i].t && mn[i].st!=DEAD)
914 if(Z_istrapped(mn[i].o.x,mn[i].o.y,mn[i].o.r,mn[i].o.h)) {
915 s=1;
916 if(t) MN_hit(i,d,o,ht);
918 return s;
921 void Z_teleobj (int o, int x, int y) {
922 obj_t *p;
924 if(o==-1) p=&pl1.o;
925 else if(o==-2) p=&pl2.o;
926 else if(o>=0 && o<MAXMN) p=&mn[o].o;
927 else return;
928 FX_tfog(p->x,p->y);FX_tfog(p->x=x,p->y=y);
929 Z_sound(telesnd,128);
932 void MN_warning (int l,int t,int r,int b) {
933 int i;
935 for(i=0;i<MAXMN;++i) if(mn[i].t && mn[i].t!=MN_CACO && mn[i].t!=MN_SOUL
936 && mn[i].t!=MN_PAIN && mn[i].t!=MN_FISH)
937 if(mn[i].st!=DIE && mn[i].st!=DEAD && mn[i].st!=SLEEP)
938 if(mn[i].o.x+mn[i].o.r>=l && mn[i].o.x-mn[i].o.r<=r
939 && mn[i].o.y>=t && mn[i].o.y-mn[i].o.h<=b)
940 if(Z_canstand(mn[i].o.x,mn[i].o.y,mn[i].o.r))
941 mn[i].o.yv=-mnsz[mn[i].t].jv;