DEADSOFTWARE

3a5efd44a165ff0d6f9c6c59f2ee1f9c8dd571cf
[flatwaifu.git] / src / monster.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 <stdlib.h>
25 #include <string.h>
26 #include "files.h"
27 #include "view.h"
28 #include "bmap.h"
29 #include "dots.h"
30 #include "weapons.h"
31 #include "player.h"
32 #include "monster.h"
33 #include "items.h"
34 #include "switch.h"
35 #include "misc.h"
36 #include "fx.h"
37 #include "smoke.h"
38 #include "my.h"
39 #include "player.h"
40 #include "error.h"
41 #include "game.h"
43 #define MAX_ATM 90
45 enum{
46 SLEEP,GO,RUN,CLIMB,DIE,DEAD,ATTACK,SHOOT,PAIN,WAIT,REVIVE,RUNOUT
47 };
49 typedef struct {
50 int r, h, l, mp, rv, jv, sp, minp;
51 } mnsz_t;
53 byte nomon = 1;
55 static char *sleepanim[MN_TN]={
56 "AAABBB","AAABBB","AAABBB","AAABBB","AAABBB","AAABBB","AAABBB","AAABBB",
57 "A","AAABBB","AAABBB","AAABBB","AAABBB","AAABBB","AAABBB","AAABBB","AAABBB",
58 "A","A","AAABBB"
59 }, *goanim[MN_TN]={
60 "AABBCCDD","AABBCCDD","AABBDDAACCDD","AABBDDAACCDD","AABBDDCCDDBB",
61 "AABBDDAACCDD","AABBCCDD","AABBCCDD","A","AABB","AABBCCBB",
62 "AABBCCDDEEFF","AABBCCDDEEFF","AABBCCDDEEFF","AABBCCDDEEFF","AABBCCDDEEFF",
63 "AABB","A","DDEEFFGGHHIIJJKKLLAABBCC","ACDABD"
64 }, *painanim[MN_TN]={
65 "H","H","G","G","G","G","H","H","F","E","G","I","I","J","L","Q","EECCDDCC",
66 "A","D","G"
67 }, *waitanim[MN_TN]={
68 "A","A","A","A","A","A","A","A","A","AABB","A","A","A","I","K","A","A",
69 "A","D","E"
70 }, *attackanim[MN_TN]={
71 "EEFFGG","EEFFGG","EEEEEF","EEEEEF","EEEEEF","EF","EEFFGG","EEFFGG",
72 "BBCCDD","CCDD","DDEEFF","GH","GH","GGGGHH","GGHHII",
73 "QQGGGHHHIIJJKKLLMMNNOOPP","BBFFAA","A","OOPPQQ","EEEEFF"
74 }, *dieanim[MN_TN]={
75 "IIIJJJKKKLLLMMM","IIIJJJKKKLLL","HHHIIIJJJKKK","HHHIIIJJJKKK",
76 "HHHIIIJJJKKKLLLMMMNNNOOO","HHHIIIJJJKKKLLLMMM",
77 "IIIJJJKKKLLLMMMNNN","IIIJJJKKKLLLMMMNNN","GGGHHHIIIJJJKKK",
78 "FFFGGGHHHIIIJJJKKK","HHHIIIJJJKKKLLLMMM",
79 "JJJJKKKKLLLLMMMMNNNNOOOOPPPPQQQQRRRR","JJJKKKLLLMMMNNNOOO",
80 "KKKLLLMMMNNNOOOPPPRRRSSS","MMMNNNOOOPPP","RRRSSSTTTUUUVVVWWWXXXYYY",
81 "DDDD","CCCDDDEEEFFFGGG","D","HHHHIIIIJJJJKKKKLLLLMMMM"
82 }, *slopanim[MN_TN]={
83 "","NNNOOOPPPRRRSSSTTT","MMMNNNOOOPPPRRRSSSTTT","MMMNNNOOOPPPRRRSSSTTT","",
84 "OOOPPPQQQRRRSSS","","","","","","","","","","","","","","OOPPQQRRSSTTUUVV"
85 }, *deadanim[MN_TN]={
86 "N","M","L","L","P","N","O","O","L","","","S","P","T","Q","Z","C","","D","N"
87 }, *messanim[MN_TN]={
88 "","U","U","U","","T","","","","","","","","","","","","","","W"
89 };
91 int hit_xv, hit_yv;
92 mn_t mn[MAXMN];
93 int mnum, gsndt;
95 static void *fsnd,*pauksnd,*trupsnd;
96 static void *snd[MN_TN][5],*impsitsnd[2],*impdthsnd[2],*firsnd,*slopsnd,*gsnd[4];
97 static void *swgsnd,*pchsnd,*telesnd;
98 static void *positsnd[3],*podthsnd[3];
99 static mnsz_t mnsz[MN_TN+1]={
100 //rad ht life pain rv jv slop min_pn
101 0, 0, 0, 0, 0, 0, 0, 0, // none
102 15, 28, 60, 20, 7,10, 0, 10, // demon
103 10, 28, 25, 15, 3,10, 30, 0, // imp
104 10, 28, 15, 10, 3,10, 30, 0, // zomby
105 10, 28, 20, 10, 3,10, 30, 0, // sergeant
106 20, 55, 500, 70, 5,10, 0, 50, // cyberdemon
107 12, 28, 60, 20, 3,10, 30, 10, // chaingunner
108 12, 32, 150, 40, 3,10, 0, 30, // baron of hell
109 12, 32, 75, 40, 3,10, 0, 30, // hell knight
110 15, 28, 100, 10, 4, 4, 0, 0, // cacodemon
111 8, 18, 60, 10, 4, 4, 0, 0, // lost soul
112 15, 28, 100, 10, 4, 4, 0, 0, // pain elemental
113 64, 50, 500, 70, 4,10, 0, 50, // spider mastermind
114 25, 27, 150, 20, 4,10, 0, 0, // arachnotron
115 18, 30, 200, 40, 3, 7, 0, 20, // mancubus
116 17, 36, 200, 40, 6,11, 0, 20, // revenant
117 17, 36, 150, 30, 7,12, 0, 10, // archvile
118 5, 5, 35, 20,14, 6, 0, 10, // fish
119 5, 17, 20, 0, 7, 6, 0, 0, // barrel
120 17, 38, 20, 40, 3, 6, 0, 20, // robot
121 8, 26, 400, 70, 8,10, 30, 50 // man
122 };
124 void setst (int i, int st) {
125 char *a;
126 int t;
128 switch(mn[i].st) {
129 case DIE: case DEAD:
130 if(st!=DEAD && st!=REVIVE) return;
132 mn[i].ac=0;
133 t=mn[i].t-1;
134 switch(mn[i].st=st) {
135 case SLEEP: a=sleepanim[t];break;
136 case PAIN: a=painanim[t];break;
137 case WAIT: a=waitanim[t];break;
138 case CLIMB:
139 case RUN: case RUNOUT:
140 case GO: a=goanim[t];break;
141 case SHOOT:
142 if(t==MN_SKEL-1) {a="KKKKJJ";break;}
143 if(t==MN_ROBO-1) {a="MN";break;}
144 case ATTACK: a=attackanim[t];
145 if(st==ATTACK && t==MN_VILE-1) a="[[\\\\]]";
146 break;
147 case DIE:
148 if(g_map==9 && t==MN_BSP-1) Z_sound(pauksnd,128);
149 a=dieanim[t];break;
150 case DEAD:
151 a=deadanim[t];
152 if(mn[i].ap==slopanim[t]) a=messanim[t];
153 if(t==MN_BARREL-1) {mn[i].t=0;}
154 break;
155 case REVIVE:
156 a=(mn[i].ap==messanim[t])?slopanim[t]:dieanim[t];
157 mn[i].ac=strlen(a)-1;
158 mn[i].o.r=mnsz[t+1].r;mn[i].o.h=mnsz[t+1].h;
159 mn[i].life=mnsz[t+1].l;mn[i].ammo=mn[i].pain=0;
160 ++mnum;
161 break;
163 mn[i].ap=a;
166 #define GGAS_TOTAL (MN__LAST-MN_DEMON+16+10)
168 void MN_alloc (void) {
169 int i,j;
170 static char sn[MN_TN][5][6]={
171 {"DMACT","DMPAIN","SGTATK","SGTSIT","SGTDTH"},
172 {"BGACT","POPAIN","CLAW","",""},
173 {"POSACT","POPAIN","","",""},
174 {"POSACT","POPAIN","","",""},
175 {"","DMPAIN","HOOF","CYBSIT","CYBDTH"},
176 {"POSACT","POPAIN","","",""},
177 {"","DMPAIN","","BRSSIT","BRSDTH"},
178 {"","DMPAIN","","KNTSIT","KNTDTH"},
179 {"DMACT","DMPAIN","","CACSIT","CACDTH"},
180 {"DMACT","DMPAIN","SKLATK","SKLATK","FIRXPL"},
181 {"DMACT","PEPAIN","","PESIT","PEDTH"},
182 {"","DMPAIN","METAL","SPISIT","SPIDTH"},
183 {"BSPACT","DMPAIN","BSPWLK","BSPSIT","BSPDTH"},
184 {"DMACT","MNPAIN","MANATK","MANSIT","MANDTH"},
185 {"SKEACT","POPAIN","SKEATK","SKESIT","SKEDTH"},
186 {"VILACT","VIPAIN","VILATK","VILSIT","VILDTH"},
187 {"","","BITE1","",""},
188 {"","","","","BAREXP"},
189 {"BSPACT","","BSPWLK","BSPSIT","BSPDTH"},
190 {"HAHA1","PLPAIN","","STOP1","PDIEHI"}
191 };
192 static char gsn[6]="GOOD0";
193 for(j=0;j<MN_TN;++j) {
194 for(i=0;i<5;++i)
195 if(sn[j][i][0])
196 snd[j][i]=Z_getsnd(sn[j][i]);
197 else
198 snd[j][i]=NULL;
199 logo_gas(j+5,GGAS_TOTAL);
201 impsitsnd[0]=Z_getsnd("BGSIT1");
202 impsitsnd[1]=Z_getsnd("BGSIT2");
203 impdthsnd[0]=Z_getsnd("BGDTH1");
204 impdthsnd[1]=Z_getsnd("BGDTH2");
205 positsnd[0]=Z_getsnd("POSIT1");
206 positsnd[1]=Z_getsnd("POSIT2");
207 positsnd[2]=Z_getsnd("POSIT3");
208 podthsnd[0]=Z_getsnd("PODTH1");
209 podthsnd[1]=Z_getsnd("PODTH2");
210 podthsnd[2]=Z_getsnd("PODTH3");
211 fsnd=Z_getsnd("FLAME");
212 firsnd=Z_getsnd("FIRSHT");
213 slopsnd=Z_getsnd("SLOP");
214 swgsnd=Z_getsnd("SKESWG");
215 pchsnd=Z_getsnd("SKEPCH");
216 telesnd=Z_getsnd("TELEPT");
217 pauksnd=Z_getsnd("PAUK1");
218 trupsnd=Z_getsnd("UTRUP");
219 for(i=0;i<4;++i) {gsn[4]=i+'1';gsnd[i]=Z_getsnd(gsn);}
222 void MN_init (void) {
223 int i;
224 for(i=0;i<MAXMN;++i) {mn[i].t=0;mn[i].st=SLEEP;}
225 gsndt=mnum=0;
228 int MN_spawn (int x, int y, byte d, int t) {
229 int i;
231 if(g_dm && nomon && t<MN_PL_DEAD) return -1;
232 for(i=0;i<MAXMN;++i) if(!mn[i].t) goto ok;
233 for(i=0;i<MAXMN;++i) if(mn[i].t>=MN_PL_DEAD) goto ok;
234 return -1;
235 ok:
236 mn[i].o.x=x;mn[i].o.y=y;
237 mn[i].o.xv=mn[i].o.yv=mn[i].o.vx=mn[i].o.vy=0;
238 mn[i].d=d;mn[i].t=t;
239 mn[i].st=SLEEP;
240 if(t<MN_PL_DEAD) {
241 mn[i].o.r=mnsz[t].r;mn[i].o.h=mnsz[t].h;
242 mn[i].life=mnsz[t].l;
243 setst(i,SLEEP);mn[i].s=myrand(18);
244 ++mnum;
245 }else {mn[i].o.r=8;mn[i].o.h=6;mn[i].life=0;mn[i].st=DEAD;}
246 mn[i].aim=-3;mn[i].atm=0;
247 mn[i].pain=0;
248 mn[i].ammo=0;
249 mn[i].ftime=0;
250 return i;
253 int MN_spawn_deadpl (obj_t *o, byte c, int t) {
254 int i;
256 if((i=MN_spawn(o->x,o->y,c,t+MN_PL_DEAD))==-1) return -1;
257 mn[i].o=*o;return i;
260 static int isfriend(int a,int b) {
261 if(a==MN_BARREL || b==MN_BARREL) return 1;
262 if(a==b) switch(a) {
263 case MN_IMP: case MN_DEMON:
264 case MN_BARON: case MN_KNIGHT:
265 case MN_CACO: case MN_SOUL:
266 case MN_MANCUB: case MN_SKEL:
267 case MN_FISH:
268 return 1;
270 if(a==MN_SOUL && b==MN_PAIN) return 1;
271 if(b==MN_SOUL && a==MN_PAIN) return 1;
272 return 0;
275 static int MN_findnewprey(int i) {
276 int a,b,l;
278 a=!PL_isdead(&pl1);
279 if(_2pl) b=!PL_isdead(&pl2); else b=0;
280 if(a) {
281 if(b) mn[i].aim=(abs(mn[i].o.x-pl1.o.x)+abs(mn[i].o.y-pl1.o.y)
282 <= abs(mn[i].o.x-pl2.o.x)+abs(mn[i].o.y-pl2.o.y))?-1:-2;
283 else mn[i].aim=-1;
284 }else{
285 if(b) mn[i].aim=-2;
286 else{
287 for(a=0,b=32000,mn[i].aim=-3;a<MAXMN;++a)
288 if(mn[a].t && mn[a].st!=DEAD && a!=i && !isfriend(mn[a].t,mn[i].t))
289 if((l=abs(mn[i].o.x-mn[a].o.x)+abs(mn[i].o.y-mn[a].o.y))<b)
290 {mn[i].aim=a;b=l;}
291 if(mn[i].aim<0) {mn[i].atm=MAX_ATM;return 0;} else mn[i].atm=0;
294 return 1;
297 int Z_getobjpos (int i, obj_t *o) {
298 if(i==-1) {*o=pl1.o;return !PL_isdead(&pl1);}
299 if(_2pl) if(i==-2) {*o=pl2.o;return !PL_isdead(&pl2);}
300 if(i>=0 && i<MAXMN) if(mn[i].t && mn[i].st!=DEAD)
301 {*o=mn[i].o;return 1;}
302 return 0;
305 static void *wakeupsnd(int t) {
306 switch(t) {
307 case MN_IMP: return impsitsnd[myrand(2)];
308 case MN_ZOMBY: case MN_SERG: case MN_CGUN:
309 return positsnd[myrand(3)];
311 return snd[t-1][3];
314 static void *dthsnd(int t) {
315 switch(t) {
316 case MN_IMP: return impdthsnd[myrand(2)];
317 case MN_ZOMBY: case MN_SERG: case MN_CGUN:
318 return podthsnd[myrand(3)];
320 return snd[t-1][4];
323 static int canshoot(int t) {
324 switch(t) {
325 case MN_DEMON: case MN_FISH: case MN_BARREL:
326 return 0;
328 return 1;
331 static int shoot(int i,obj_t *o,int n) {
332 int xd,yd,m;
334 if(mn[i].ammo<0) return 0;
335 if(!n) switch(mn[i].t) {
336 case MN_FISH: case MN_BARREL:
337 case MN_DEMON: return 0;
338 case MN_CGUN:
339 case MN_BSP:
340 case MN_ROBO:
341 if(++mn[i].ammo>=50) mn[i].ammo=(mn[i].t==MN_ROBO)?-200:-50;
342 break;
343 case MN_MAN:
344 break;
345 case MN_MANCUB:
346 if(++mn[i].ammo>=5) mn[i].ammo=-50;
347 break;
348 case MN_SPIDER:
349 if(++mn[i].ammo>=100) mn[i].ammo=-50;
350 break;
351 case MN_CYBER:
352 if(rand()&1) return 0;
353 if(++mn[i].ammo>=10) mn[i].ammo=-50;
354 break;
355 case MN_BARON: case MN_KNIGHT:
356 if(rand()&7) return 0;
357 break;
358 case MN_SKEL:
359 if(rand()&31) return 0;
360 break;
361 case MN_VILE:
362 if(rand()&7) return 0;
363 break;
364 case MN_PAIN:
365 if(rand()&7) return 0;
366 break;
367 default:
368 if(rand()&15) return 0;
370 if(!Z_look(&mn[i].o,o,mn[i].d)) return 0;
371 mn[i].atm=0;
372 mn[i].tx=o->x+(o->xv+o->vx)*6;mn[i].ty=o->y-o->h/2+(o->yv+o->vy)*6;
373 if(abs(mn[i].tx-mn[i].o.x)<abs(mn[i].ty-mn[i].o.y+mn[i].o.h/2)) return 0;
374 switch(mn[i].t) {
375 case MN_IMP: case MN_BARON: case MN_KNIGHT: case MN_CACO:
376 setst(i,SHOOT);Z_sound(firsnd,128);break;
377 case MN_SKEL:
378 setst(i,SHOOT);Z_sound(snd[MN_SKEL-1][2],128);break;
379 case MN_VILE:
380 mn[i].tx=o->x;mn[i].ty=o->y;
381 setst(i,SHOOT);Z_sound(fsnd,128);
382 Z_sound(snd[MN_VILE-1][2],128);break;
383 case MN_SOUL:
384 setst(i,ATTACK);Z_sound(snd[MN_SOUL-1][2],128);
385 yd=mn[i].ty-mn[i].o.y+mn[i].o.h/2;xd=mn[i].tx-mn[i].o.x;
386 if(!(m=max(abs(xd),abs(yd)))) m=1;
387 mn[i].o.xv=xd*16/m;mn[i].o.yv=yd*16/m;
388 break;
389 case MN_MANCUB: if(mn[i].ammo==1) Z_sound(snd[MN_MANCUB-1][2],128);
390 case MN_ZOMBY: case MN_SERG: case MN_BSP: case MN_ROBO:
391 case MN_CYBER: case MN_CGUN: case MN_SPIDER:
392 case MN_PAIN: case MN_MAN:
393 setst(i,SHOOT);break;
394 default:
395 return 0;
397 return 1;
400 static int kick(int i,obj_t *o) {
401 switch(mn[i].t) {
402 case MN_FISH:
403 setst(i,ATTACK);return 1;
404 case MN_DEMON:
405 setst(i,ATTACK);Z_sound(snd[0][2],128);return 1;
406 case MN_IMP:
407 setst(i,ATTACK);Z_sound(snd[1][2],128);return 1;
408 case MN_SKEL:
409 setst(i,ATTACK);Z_sound(swgsnd,128);return 1;
410 case MN_ROBO:
411 setst(i,ATTACK);Z_sound(swgsnd,128);return 1;
412 case MN_BARON: case MN_KNIGHT: case MN_CACO: case MN_MANCUB:
413 return shoot(i,o,1);
415 return 0;
418 static int iscorpse(obj_t *o,int n) {
419 int i;
421 if(!n) if(rand()&7) return -3;
422 for(i=0;i<MAXMN;++i) if(mn[i].t) if(mn[i].st==DEAD)
423 if(Z_overlap(o,&mn[i].o)) switch(mn[i].t) {
424 case MN_SOUL: case MN_PAIN:
425 case MN_CYBER: case MN_SPIDER:
426 case MN_PL_DEAD: case MN_PL_MESS:
427 case MN_VILE: case MN_BARREL:
428 continue;
429 default:
430 return i;
432 return -3;
435 static int Z_hitobj (int obj, int d, int own, int t) {
436 hit_xv=hit_yv=0;
437 if(obj==-1) return PL_hit(&pl1,d,own,t);
438 else if(obj==-2 && _2pl) return PL_hit(&pl2,d,own,t);
439 else if(obj<0 || obj>=MAXMN) return 0;
440 if(mn[obj].t) return MN_hit(obj,d,own,t);
441 return 0;
444 void MN_act (void) {
445 int i,st,sx,sy,t;
446 static obj_t o;
447 static int pt_x=0,pt_xs=1,pt_y=0,pt_ys=1;
449 if(abs(pt_x+=pt_xs) > 123) pt_xs=-pt_xs;
450 if(abs(pt_y+=pt_ys) > 50) pt_ys=-pt_ys;
451 if(gsndt>0) if(--gsndt==0) {
452 Z_sound(gsnd[myrand(4)],128);
454 for(i=0;i<MAXMN;++i) if((t=mn[i].t)!=0) {
455 switch(t) {
456 case MN_FISH:
457 if(!Z_inwater(mn[i].o.x,mn[i].o.y,mn[i].o.r,mn[i].o.h)) break;
458 case MN_SOUL: case MN_PAIN: case MN_CACO:
459 if(mn[i].st!=DIE && mn[i].st!=DEAD) --mn[i].o.yv;
460 break;
461 }z_mon=1;st=Z_moveobj(&mn[i].o);z_mon=0;
462 BM_mark(&mn[i].o,BM_MONSTER);
463 if(st&Z_FALLOUT) {
464 if(t==MN_ROBO) g_exit=1;
465 mn[i].t=0;--mnum;continue;
467 if(st&Z_HITWATER) Z_splash(&mn[i].o,mn[i].o.r+mn[i].o.h);
468 SW_press(mn[i].o.x,mn[i].o.y,mn[i].o.r,mn[i].o.h,8,i);
469 if(mn[i].ftime) {
470 --mn[i].ftime;
471 SMK_flame(mn[i].o.x,mn[i].o.y-mn[i].o.h/2,
472 mn[i].o.xv+mn[i].o.vx,mn[i].o.yv+mn[i].o.vy,
473 mn[i].o.r/2,mn[i].o.h/2,rand()%(200*2+1)-200,-500,1,mn[i].fobj);
475 if(st&Z_INWATER) mn[i].ftime=0;
476 if(mn[i].st==DEAD) continue;
477 if(st&Z_INWATER) if(!(rand()&31)) switch(t) {
478 case MN_FISH:
479 if(rand()&3) break;
480 case MN_ROBO: case MN_BARREL:
481 case MN_PL_DEAD: case MN_PL_MESS:
482 FX_bubble(mn[i].o.x+((rand()&1)*2-1)*myrand(mn[i].o.r+1),
483 mn[i].o.y-myrand(mn[i].o.h+1),0,0,1
484 );
485 break;
486 default:
487 FX_bubble(mn[i].o.x,mn[i].o.y-mn[i].o.h*3/4,0,0,5);
490 if(t==MN_BARREL) {
492 if(!mn[i].ap[++mn[i].ac]) {
493 mn[i].ac=0;if(mn[i].st==DIE || mn[i].st==DEAD) {mn[i].t=0;}
494 }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);
495 continue;
497 if(t==MN_SOUL) if(st&Z_HITAIR) Z_set_speed(&mn[i].o,16);
498 if(mn[i].ammo<0) ++mn[i].ammo;
499 if(mn[i].o.yv<0)
500 if(st&Z_INWATER) mn[i].o.yv=-4;
501 ++mn[i].atm;
502 switch(mn[i].st) {
503 case PAIN:
504 if(mn[i].pain>=mnsz[t].mp)
505 {mn[i].pain=mnsz[t].mp;Z_sound(snd[t-1][1],128);}
506 if((mn[i].pain-=5)<=mnsz[t].minp)
507 {setst(i,GO);mn[i].pain=0;mn[i].ammo=-9;}
508 break;
509 case SLEEP:
510 if(++mn[i].s>=18) mn[i].s=0; else break;
511 if(Z_look(&mn[i].o,&pl1.o,mn[i].d))
512 {setst(i,GO);mn[i].aim=-1;mn[i].atm=0;Z_sound(wakeupsnd(t),128);}
513 if(_2pl) if(Z_look(&mn[i].o,&pl2.o,mn[i].d))
514 {setst(i,GO);mn[i].aim=-2;mn[i].atm=0;Z_sound(wakeupsnd(t),128);}
515 break;
516 case WAIT:
517 if(--mn[i].s<0) setst(i,GO);
518 break;
519 case GO:
520 if(st&Z_BLOCK) {mn[i].d^=1;setst(i,RUNOUT);mn[i].s=40;break;}
521 if(t==MN_VILE) if(iscorpse(&mn[i].o,0)>=0) {
522 setst(i,ATTACK);mn[i].o.xv=0;break;
524 if(!Z_getobjpos(mn[i].aim,&o) || mn[i].atm>MAX_ATM)
525 if(!MN_findnewprey(i)) {
526 mn[i].aim=-3;
527 o.x=mn[i].o.x+pt_x;o.y=mn[i].o.y+pt_y;
528 o.xv=o.vx=o.yv=o.vy=o.r=0;o.h=1;
529 }else Z_getobjpos(mn[i].aim,&o);
530 if(Z_overlap(&mn[i].o,&o)) {
531 mn[i].atm=0;
532 if(kick(i,&o)) break;
534 sx=o.x-mn[i].o.x;
535 sy=o.y-o.h/2-mn[i].o.y+mn[i].o.h/2;
536 if(!(st&Z_BLOCK)) if(abs(sx)<20)
537 if(t!=MN_FISH) {setst(i,RUN);mn[i].s=15;mn[i].d=rand()&1;break;}
538 if(st&Z_HITWALL) {
539 if(SW_press(mn[i].o.x,mn[i].o.y,mn[i].o.r,mn[i].o.h,2,i))
540 {setst(i,WAIT);mn[i].s=4;break;}
541 switch(t) {
542 case MN_CACO: case MN_SOUL: case MN_PAIN: case MN_FISH:
543 break;
544 default:
545 if(Z_canstand(mn[i].o.x,mn[i].o.y,mn[i].o.r))
546 {mn[i].o.yv=-mnsz[t].jv;setst(i,CLIMB);break;}
547 }break;
549 mn[i].d=(sx>0)?1:0;
550 if(canshoot(t))
551 if(abs(sx)>abs(sy)) if(shoot(i,&o,0)) break;
553 switch(t) {
554 case MN_FISH:
555 if(!(st&Z_INWATER)) {
556 if(Z_canstand(mn[i].o.x,mn[i].o.y,mn[i].o.r)) {
557 mn[i].o.yv=-6;
558 mn[i].o.vx+=rand()%17-8;
559 }setst(i,PAIN);mn[i].pain+=50;break;
561 case MN_CACO: case MN_SOUL: case MN_PAIN:
562 if(abs(sy)>4) mn[i].o.yv=(sy<0)?-4:4; else mn[i].o.yv=0;
563 if(t==MN_FISH) if(mn[i].o.yv<0)
564 if(!Z_inwater(mn[i].o.x,mn[i].o.y-8,mn[i].o.r,mn[i].o.h))
565 {mn[i].o.yv=0;setst(i,RUN);mn[i].d=rand()&1;mn[i].s=20;}
566 break;
567 default:
568 if(sy<-20) if(Z_canstand(mn[i].o.x,mn[i].o.y,mn[i].o.r))
569 if(!(rand()&3)) mn[i].o.yv=-mnsz[t].jv;
571 if(++mn[i].s>=8) {
572 mn[i].s=0;
573 if(!(rand()&7)) Z_sound(snd[t-1][0],128);
575 mn[i].o.xv=((mn[i].d)?1:-1)*mnsz[t].rv;
576 if(st&Z_INWATER) mn[i].o.xv/=2;
577 else if(t==MN_FISH) mn[i].o.xv=0;
578 break;
579 case RUN:
580 if(st&Z_BLOCK) {setst(i,RUNOUT);mn[i].d^=1;mn[i].s=40;break;}
581 if(--mn[i].s<=0 || ((st&Z_HITWALL) && mn[i].o.yv+mn[i].o.vy==0)) {
582 setst(i,GO);mn[i].s=0;if(st&(Z_HITWALL|Z_BLOCK)) mn[i].d^=1;
583 if(!(rand()&7)) Z_sound(snd[t-1][0],128);
584 }mn[i].o.xv=((mn[i].d)?1:-1)*mnsz[t].rv;
585 if(st&Z_INWATER) mn[i].o.xv/=2;
586 else if(t==MN_FISH) mn[i].o.xv=0;
587 break;
588 case RUNOUT:
589 if(!(st&Z_BLOCK) && mn[i].s>0) mn[i].s=0;
590 if(--mn[i].s<=-18) {
591 setst(i,GO);mn[i].s=0;if(st&(Z_HITWALL|Z_BLOCK)) mn[i].d^=1;
592 if(!(rand()&7)) Z_sound(snd[t-1][0],128);
593 }mn[i].o.xv=((mn[i].d)?1:-1)*mnsz[t].rv;
594 if(st&Z_INWATER) mn[i].o.xv/=2;
595 else if(t==MN_FISH) mn[i].o.xv=0;
596 break;
597 case CLIMB:
598 if(mn[i].o.yv+mn[i].o.vy>=0 || !(st&Z_HITWALL)) {
599 setst(i,GO);mn[i].s=0;
600 if(st&(Z_HITWALL|Z_BLOCK)) {mn[i].d^=1;setst(i,RUN);mn[i].s=15;}
601 }mn[i].o.xv=((mn[i].d)?1:-1)*mnsz[t].rv;
602 if(st&Z_INWATER) mn[i].o.xv/=2;
603 else if(t==MN_FISH) mn[i].o.xv=0;
604 break;
605 case ATTACK:
606 case SHOOT:
607 if(t==MN_SOUL) {if(st&(Z_HITWALL|Z_HITCEIL|Z_HITLAND)) setst(i,GO); break;}
608 if(t!=MN_FISH) mn[i].o.xv=Z_dec(mn[i].o.xv,1);
609 if(t==MN_VILE && mn[i].st==SHOOT) {
610 if(!Z_getobjpos(mn[i].aim,&o)) {setst(i,GO);break;}
611 if(!Z_look(&mn[i].o,&o,mn[i].d)) {setst(i,GO);break;}
612 if(Z_inwater(o.x,o.y,o.r,o.h)) {setst(i,GO);break;}
613 mn[i].tx=o.x;mn[i].ty=o.y;
614 Z_hitobj(mn[i].aim,2,i,HIT_SOME);
615 }break;
617 if(mn[i].st==REVIVE) {
618 if(--mn[i].ac==0) setst(i,GO);
619 }else ++mn[i].ac;
620 if(!mn[i].ap[mn[i].ac]) switch(mn[i].st) {
621 case ATTACK:
622 switch(t) {
623 case MN_SOUL: mn[i].ac=0;
624 case MN_IMP:
625 case MN_DEMON:
626 if(Z_hit(&mn[i].o,15,i,HIT_SOME)) if(t==MN_SOUL) setst(i,GO);
627 break;
628 case MN_FISH:
629 if(Z_hit(&mn[i].o,10,i,HIT_SOME))
630 Z_sound(snd[MN_FISH-1][2],128);
631 break;
632 case MN_SKEL: case MN_ROBO:
633 o=mn[i].o;o.xv=mn[i].d?50:-50;
634 if(Z_hit(&o,50,i,HIT_SOME)) Z_sound(pchsnd,128);
635 break;
636 case MN_VILE:
637 sx=iscorpse(&mn[i].o,1);
638 if(sx==-3) break;
639 if(!mn[sx].t || mn[sx].st!=DEAD) break;
640 setst(sx,REVIVE);Z_sound(slopsnd,128);
641 hit_xv=hit_yv=0;MN_hit(i,5,-3,HIT_SOME);
642 break;
643 }if(t!=MN_SOUL && mn[i].st!=DIE) setst(i,GO);
644 break;
645 case SHOOT:
646 switch(t) {
647 case MN_IMP:
648 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);
649 break;
650 case MN_ZOMBY:
651 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);
652 break;
653 case MN_SERG:
654 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);
655 break;
656 case MN_MAN:
657 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);
658 mn[i].ammo=-36;break;
659 case MN_CYBER:
660 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);
661 break;
662 case MN_SKEL:
663 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);
664 break;
665 case MN_CGUN:
666 case MN_SPIDER:
667 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);
668 break;
669 case MN_BSP:
670 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);
671 break;
672 case MN_ROBO:
673 WP_plasma(mn[i].o.x+(mn[i].d*2-1)*15,mn[i].o.y-30,mn[i].tx,mn[i].ty,i);
674 break;
675 case MN_MANCUB:
676 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);
677 break;
678 case MN_BARON: case MN_KNIGHT:
679 WP_ball7(mn[i].o.x,mn[i].o.y-mn[i].o.h/2,mn[i].tx,mn[i].ty,i);
680 break;
681 case MN_CACO:
682 WP_ball2(mn[i].o.x,mn[i].o.y-mn[i].o.h/2,mn[i].tx,mn[i].ty,i);
683 break;
684 case MN_PAIN:
685 if((sx=MN_spawn(mn[i].o.x,mn[i].o.y,mn[i].d,MN_SOUL))==-1) break;
686 Z_getobjpos(mn[sx].aim=mn[i].aim,&o);mn[sx].atm=0;
687 shoot(sx,&o,1);
688 break;
690 if(t==MN_CGUN || t==MN_SPIDER || t==MN_BSP || t==MN_MANCUB || t==MN_ROBO)
691 if(!Z_getobjpos(mn[i].aim,&o)) MN_findnewprey(i);
692 else if(shoot(i,&o,0)) break;
693 setst(i,GO);break;
694 case DIE:
695 setst(i,DEAD);
696 if(t==MN_PAIN || t==MN_SOUL) mn[i].ftime=0;
697 if(t==MN_PAIN) {
698 if((sx=MN_spawn(mn[i].o.x-15,mn[i].o.y,0,MN_SOUL))==-1) break;
699 setst(sx,GO);
700 if((sx=MN_spawn(mn[i].o.x+15,mn[i].o.y,1,MN_SOUL))==-1) break;
701 setst(sx,GO);
702 if((sx=MN_spawn(mn[i].o.x,mn[i].o.y-10,1,MN_SOUL))==-1) break;
703 setst(sx,GO);
704 }break;
705 default: mn[i].ac=0;
707 switch(mn[i].st) {
708 case GO: case RUN: case CLIMB: case RUNOUT:
709 if(t==MN_CYBER || t==MN_SPIDER || t==MN_BSP) {
710 if(mn[i].ac==0 || mn[i].ac==6) Z_sound(snd[t-1][2],128);
711 }else if(t==MN_ROBO)
712 if(mn[i].ac==0 || mn[i].ac==12) Z_sound(snd[t-1][2],128);
717 void MN_mark (void) {
718 int i;
719 for(i=0;i<MAXMN;++i) if(mn[i].t!=0) BM_mark(&mn[i].o,BM_MONSTER);
722 int MN_hit(int n,int d,int o,int t) {
723 int i;
725 if(mn[n].st==DEAD || mn[n].st==DIE) return 0;
726 if(o==n) {
727 if(t!=HIT_ROCKET && t!=HIT_ELECTRO) return 0;
728 if(mn[n].t==MN_CYBER || mn[n].t==MN_BARREL) return 1;
730 if(o>=0) {
731 if(mn[o].t==MN_SOUL && mn[n].t==MN_PAIN) return 0;
732 if(mn[o].t==mn[n].t) switch(mn[n].t) {
733 case MN_IMP: case MN_DEMON:
734 case MN_BARON: case MN_KNIGHT:
735 case MN_CACO: case MN_SOUL:
736 case MN_MANCUB: case MN_SKEL:
737 case MN_FISH:
738 return 0;
741 if(t==HIT_FLAME) if(mn[n].ftime && mn[n].fobj==o) {if(g_time&31) return 1;}
742 else {mn[n].ftime=255;mn[n].fobj=o;}
743 if(t==HIT_ELECTRO) if(mn[n].t==MN_FISH)
744 {setst(n,RUN);mn[n].s=20;mn[n].d=rand()&1;return 1;}
745 if(t==HIT_TRAP) mn[n].life=-100;
746 if(mn[n].t==MN_ROBO) d=0;
747 if((mn[n].life-=d)<=0) --mnum;
748 if(!mn[n].pain) mn[n].pain=3;
749 mn[n].pain+=d;
750 if(mn[n].st!=PAIN) {
751 if(mn[n].pain>=mnsz[mn[n].t].minp) setst(n,PAIN);
753 if(mn[n].t!=MN_BARREL)
754 DOT_blood(mn[n].o.x,mn[n].o.y-mn[n].o.h/2,hit_xv,hit_yv,d*2);
755 mn[n].aim=o;mn[n].atm=0;
756 if(mn[n].life<=0) {
757 if(mn[n].t!=MN_BARREL)
758 if(o==-1) ++pl1.kills;
759 else if(o==-2) ++pl2.kills;
760 setst(n,DIE);
761 switch(mn[n].t) {
762 case MN_ZOMBY: i=I_CLIP;break;
763 case MN_SERG: i=I_SGUN;break;
764 case MN_CGUN: i=I_MGUN;break;
765 case MN_MAN: i=I_KEYR;break;
766 default: i=0;
767 }if(i) IT_spawn(mn[n].o.x,mn[n].o.y,i);
768 mn[n].o.xv=0;mn[n].o.h=6;
769 if(mn[n].life<=-mnsz[mn[n].t].sp)
770 switch(mn[n].t) {
771 case MN_IMP: case MN_ZOMBY: case MN_SERG: case MN_CGUN:
772 case MN_MAN:
773 mn[n].ap=slopanim[mn[n].t-1];
774 Z_sound(slopsnd,128);
775 break;
776 case MN_BSP: if(g_map==9) break;
777 default:
778 Z_sound(dthsnd(mn[n].t),128);
780 else if(mn[n].t!=MN_BSP || g_map!=9) Z_sound(dthsnd(mn[n].t),128);
781 mn[n].life=0;
782 }else if(mn[n].st==SLEEP) {setst(n,GO);mn[n].pain=mnsz[mn[n].t].mp;}
783 return 1;
786 #define hit(o,x,y) (y<=o.y && y>o.y-o.h && x>=o.x-o.r && x<=o.x+o.r)
788 int Z_gunhit (int x, int y, int o, int xv, int yv) {
789 int i;
791 if(o!=-1) if(hit(pl1.o,x,y)) if(PL_hit(&pl1,3,o,HIT_SOME))
792 {pl1.o.vx+=xv;pl1.o.vy+=yv;return -1;}
793 if(_2pl && o!=-2) if(hit(pl2.o,x,y)) if(PL_hit(&pl2,3,o,HIT_SOME))
794 {pl2.o.vx+=xv;pl2.o.vy+=yv;return -2;}
796 for(i=0;i<MAXMN;++i) if(mn[i].t && o!=i)
797 if(hit(mn[i].o,x,y)) if(MN_hit(i,3,o,HIT_SOME))
798 {mn[i].o.vx+=xv;mn[i].o.vy+=yv;return 1;}
799 return 0;
802 static void goodsnd(void) {
803 if(!g_dm) return;
804 gsndt=18;
807 int Z_hit (obj_t *o, int d, int own, int t) {
808 int i;
810 hit_xv=o->xv+o->vx;
811 hit_yv=o->yv+o->vy;
812 if(Z_overlap(o,&pl1.o)) if(PL_hit(&pl1,d,own,t)) {
813 pl1.o.vx+=(o->xv+o->vx)*((t==HIT_BFG)?8:1)/4;
814 pl1.o.vy+=(o->yv+o->vy)*((t==HIT_BFG)?8:1)/4;
815 if(t==HIT_BFG) goodsnd();
816 return -1;
818 if(_2pl) if(Z_overlap(o,&pl2.o)) if(PL_hit(&pl2,d,own,t)) {
819 pl2.o.vx+=(o->xv+o->vx)*((t==HIT_BFG)?8:1)/4;
820 pl2.o.vy+=(o->yv+o->vy)*((t==HIT_BFG)?8:1)/4;
821 if(t==HIT_BFG) goodsnd();
822 return -2;
825 for(i=0;i<MAXMN;++i) if(mn[i].t)
826 if(Z_overlap(o,&mn[i].o)) if(MN_hit(i,d,own,t)) {
827 mn[i].o.vx+=(o->xv+o->vx)*((t==HIT_BFG)?8:1)/4;
828 mn[i].o.vy+=(o->yv+o->vy)*((t==HIT_BFG)?8:1)/4;
829 return 1;
831 return 0;
834 void MN_killedp (void) {
835 int i;
836 for(i=0;i<MAXMN;++i) if(mn[i].t==MN_MAN)
837 if(mn[i].st!=DEAD && mn[i].st!=DIE && mn[i].st!=SLEEP)
838 Z_sound(trupsnd,128);
841 void Z_explode (int x,int y,int rad,int own) {
842 long r;
843 int dx,dy,m,i;
845 if(x<-100 || x>FLDW*CELW+100) return;
846 if(y<-100 || y>FLDH*CELH+100) return;
847 r=(long)rad*rad;
848 dx=pl1.o.x-x;dy=pl1.o.y-pl1.o.h/2-y;
849 if((long)dx*dx+(long)dy*dy<r) {
850 if(!(m=max(abs(dx),abs(dy)))) m=1;
851 pl1.o.vx+=hit_xv=dx*10/m;
852 pl1.o.vy+=hit_yv=dy*10/m;
853 PL_hit(&pl1,100*(rad-m)/rad,own,HIT_ROCKET);
855 if(_2pl) {
856 dx=pl2.o.x-x;dy=pl2.o.y-pl2.o.h/2-y;
857 if((long)dx*dx+(long)dy*dy<r) {
858 if(!(m=max(abs(dx),abs(dy)))) m=1;
859 pl2.o.vx+=hit_xv=dx*10/m;
860 pl2.o.vy+=hit_yv=dy*10/m;
861 PL_hit(&pl2,100*(rad-m)/rad,own,HIT_ROCKET);
864 for(i=0;i<MAXMN;++i) if(mn[i].t) {
865 dx=mn[i].o.x-x;dy=mn[i].o.y-mn[i].o.h/2-y;
866 if((long)dx*dx+(long)dy*dy<r) {
867 if(!(m=max(abs(dx),abs(dy)))) m=1;
868 mn[i].o.vx+=hit_xv=dx*10/m;
869 mn[i].o.vy+=hit_yv=dy*10/m;
870 MN_hit(i,mn[i].o.r*10*(rad-m)/rad,own,HIT_ROCKET);
875 void Z_bfg9000 (int x,int y,int own) {
876 int dx,dy,i;
878 hit_xv=hit_yv=0;
879 if(x<-100 || x>FLDW*CELW+100) return;
880 if(y<-100 || y>FLDH*CELH+100) return;
881 dx=pl1.o.x-x;dy=pl1.o.y-pl1.o.h/2-y;
882 if(own!=-1) if((long)dx*dx+(long)dy*dy<16000)
883 if(Z_cansee(x,y,pl1.o.x,pl1.o.y-pl1.o.h/2)) {
884 if(PL_hit(&pl1,50,own,HIT_SOME))
885 WP_bfghit(pl1.o.x,pl1.o.y-pl1.o.h/2,own);
887 if(_2pl) {
888 dx=pl2.o.x-x;dy=pl2.o.y-pl2.o.h/2-y;
889 if(own!=-2) if((long)dx*dx+(long)dy*dy<16000)
890 if(Z_cansee(x,y,pl2.o.x,pl2.o.y-pl2.o.h/2)) {
891 if(PL_hit(&pl2,50,own,HIT_SOME))
892 WP_bfghit(pl2.o.x,pl2.o.y-pl2.o.h/2,own);
895 for(i=0;i<MAXMN;++i) if(mn[i].t && own!=i) {
896 dx=mn[i].o.x-x;dy=mn[i].o.y-mn[i].o.h/2-y;
897 if((long)dx*dx+(long)dy*dy<16000)
898 if(Z_cansee(x,y,mn[i].o.x,mn[i].o.y-mn[i].o.h/2)) {
899 if(MN_hit(i,50,own,HIT_SOME))
900 WP_bfghit(mn[i].o.x,mn[i].o.y-mn[i].o.h/2,own);
905 int Z_chktrap (int t, int d, int o, int ht) {
906 int i,s;
908 hit_xv=hit_yv=0;
909 s=0;
910 if(Z_istrapped(pl1.o.x,pl1.o.y,pl1.o.r,pl1.o.h)) {
911 s=1;
912 if(t) PL_hit(&pl1,d,o,ht);
914 if(_2pl) if(Z_istrapped(pl2.o.x,pl2.o.y,pl2.o.r,pl2.o.h)) {
915 s=1;
916 if(t) PL_hit(&pl2,d,o,ht);
918 for(i=0;i<MAXMN;++i) if(mn[i].t && mn[i].st!=DEAD)
919 if(Z_istrapped(mn[i].o.x,mn[i].o.y,mn[i].o.r,mn[i].o.h)) {
920 s=1;
921 if(t) MN_hit(i,d,o,ht);
923 return s;
926 void Z_teleobj (int o, int x, int y) {
927 obj_t *p;
929 if(o==-1) p=&pl1.o;
930 else if(o==-2) p=&pl2.o;
931 else if(o>=0 && o<MAXMN) p=&mn[o].o;
932 else return;
933 FX_tfog(p->x,p->y);FX_tfog(p->x=x,p->y=y);
934 Z_sound(telesnd,128);
937 void MN_warning (int l,int t,int r,int b) {
938 int i;
940 for(i=0;i<MAXMN;++i) if(mn[i].t && mn[i].t!=MN_CACO && mn[i].t!=MN_SOUL
941 && mn[i].t!=MN_PAIN && mn[i].t!=MN_FISH)
942 if(mn[i].st!=DIE && mn[i].st!=DEAD && mn[i].st!=SLEEP)
943 if(mn[i].o.x+mn[i].o.r>=l && mn[i].o.x-mn[i].o.r<=r
944 && mn[i].o.y>=t && mn[i].o.y-mn[i].o.h<=b)
945 if(Z_canstand(mn[i].o.x,mn[i].o.y,mn[i].o.r))
946 mn[i].o.yv=-mnsz[mn[i].t].jv;