DEADSOFTWARE

b778a0ec96c484a448264ae979454c9a9e0b70a7
[flatwaifu.git] / src / weapons.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 "view.h"
26 #include "bmap.h"
27 #include "dots.h"
28 #include "smoke.h"
29 #include "weapons.h"
30 #include "misc.h"
32 extern int hit_xv,hit_yv;
34 void bfg_fly(int x,int y,int own);
36 enum{NONE=0,ROCKET,PLASMA,APLASMA,BALL1,BALL2,BALL7,BFGBALL,BFGHIT,
37 MANF,REVF,FIRE};
39 #pragma pack(1)
40 typedef struct{
41 obj_t o;
42 byte t,s;
43 int own;
44 short target;
45 }weapon_t;
46 #pragma pack()
48 static void *snd[14],*spr[49*2];
49 static char sprd[49*2];
50 static weapon_t wp[MAXWPN];
52 static void throw(int,int,int,int,int,int,int,int);
54 void WP_savegame(FILE* h) {
55 int n;
57 for(n=MAXWPN;--n;) if(wp[n].t) break;
58 ++n;myfwrite(&n,1,4,h);myfwrite(wp,1,n*sizeof(wp[0]),h);
59 }
61 void WP_loadgame(FILE* h) {
62 int n;
64 myfread(&n,1,4,h);myfread(wp,1,n*sizeof(wp[0]),h);
65 }
67 void WP_alloc(void) {
68 int i;
69 static char nm[14][6]={
70 "PISTOL",
71 "SHOTGN",
72 "DSHTGN",
73 "RLAUNC",
74 "RXPLOD",
75 "PLASMA",
76 "FIRSHT",
77 "FIRXPL",
78 "BAREXP",
79 "PUNCH",
80 "SAWHIT",
81 "MGUN",
82 "SPARK1",
83 "SPARK2"
84 };
86 for(i=0;i<4;++i) {
87 spr[i*2]=Z_getspr("MISL",i,1,sprd+i*2);
88 spr[i*2+1]=Z_getspr("MISL",i,2,sprd+i*2+1);
89 }
90 for(;i<6;++i) {
91 spr[i*2]=Z_getspr("PLSS",i-4,1,sprd+i*2);
92 spr[i*2+1]=Z_getspr("PLSS",i-4,2,sprd+i*2+1);
93 }
94 for(;i<11;++i) {
95 spr[i*2]=Z_getspr("PLSE",i-6,1,sprd+i*2);
96 spr[i*2+1]=Z_getspr("PLSE",i-6,2,sprd+i*2+1);
97 }
98 for(;i<13;++i) {
99 spr[i*2]=Z_getspr("APLS",i-11,1,sprd+i*2);
100 spr[i*2+1]=Z_getspr("APLS",i-11,2,sprd+i*2+1);
102 for(;i<18;++i) {
103 spr[i*2]=Z_getspr("APBX",i-13,1,sprd+i*2);
104 spr[i*2+1]=Z_getspr("APBX",i-13,2,sprd+i*2+1);
106 for(;i<20;++i) {
107 spr[i*2]=Z_getspr("BFS1",i-18,1,sprd+i*2);
108 spr[i*2+1]=Z_getspr("BFS1",i-18,2,sprd+i*2+1);
110 for(;i<26;++i) {
111 spr[i*2]=Z_getspr("BFE1",i-20,1,sprd+i*2);
112 spr[i*2+1]=Z_getspr("BFE1",i-20,2,sprd+i*2+1);
114 for(;i<30;++i) {
115 spr[i*2]=Z_getspr("BFE2",i-26,1,sprd+i*2);
116 spr[i*2+1]=Z_getspr("BFE2",i-26,2,sprd+i*2+1);
118 for(;i<32;++i) {
119 spr[i*2]=Z_getspr("MISL",i-30+4,1,sprd+i*2);
120 spr[i*2+1]=Z_getspr("MISL",i-30+4,2,sprd+i*2+1);
122 for(;i<37;++i) {
123 spr[i*2]=Z_getspr("BAL1",i-32,1,sprd+i*2);
124 spr[i*2+1]=Z_getspr("BAL1",i-32,2,sprd+i*2+1);
126 for(;i<42;++i) {
127 spr[i*2]=Z_getspr("BAL7",i-37,1,sprd+i*2);
128 spr[i*2+1]=Z_getspr("BAL7",i-37,2,sprd+i*2+1);
130 for(;i<47;++i) {
131 spr[i*2]=Z_getspr("BAL2",i-42,1,sprd+i*2);
132 spr[i*2+1]=Z_getspr("BAL2",i-42,2,sprd+i*2+1);
134 for(;i<49;++i) {
135 spr[i*2]=Z_getspr("MANF",i-47,1,sprd+i*2);
136 spr[i*2+1]=Z_getspr("MANF",i-47,2,sprd+i*2+1);
138 for(i=0;i<14;++i) snd[i]=Z_getsnd(nm[i]);
141 void WP_init(void) {
142 int i;
144 for(i=0;i<MAXWPN;++i) wp[i].t=NONE;
147 void WP_act(void) {
148 int i,st;
149 static obj_t o;
151 for(i=0;i<MAXWPN;++i) if(wp[i].t) {
152 if(wp[i].t==ROCKET || wp[i].t==REVF)
153 SMK_gas(wp[i].o.x+Z_sign(wp[i].o.xv)*2,
154 wp[i].o.y-wp[i].o.h/2,3,3,
155 wp[i].o.xv+wp[i].o.vx,wp[i].o.yv+wp[i].o.vy,64
156 );
157 --wp[i].o.yv;st=Z_moveobj(&wp[i].o);
158 if(st&Z_FALLOUT) {wp[i].t=0;continue;}
159 if(st&Z_HITWATER) switch(wp[i].t) {
160 case PLASMA: case APLASMA:
161 case BFGBALL:
162 break;
163 default:
164 Z_splash(&wp[i].o,wp[i].o.r+wp[i].o.h);break;
166 switch(wp[i].t) {
167 case REVF:
168 if(Z_getobjpos(wp[i].target,&o))
169 throw(i,wp[i].o.x,wp[i].o.y-2,o.x+o.xv+o.vx,o.y+o.yv+o.vy,2,5,12);
170 case ROCKET:
171 if(wp[i].s>=2) {if(++wp[i].s>=8) wp[i].t=0; break;}
172 if(st&Z_HITAIR) Z_set_speed(&wp[i].o,12);
173 if(st&(Z_HITWALL|Z_HITCEIL|Z_HITLAND)) {
174 wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[4],128);
175 Z_explode(wp[i].o.x,wp[i].o.y,30,wp[i].own);break;}
176 else if(Z_hit(&wp[i].o,10,wp[i].own,HIT_SOME)) {
177 wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[4],128);
178 Z_explode(wp[i].o.x,wp[i].o.y-wp[i].o.h/2,30,wp[i].own);break;}
179 bfg_fly(wp[i].o.x,wp[i].o.y-wp[i].o.h/2,wp[i].own);
180 break;
181 case PLASMA:
182 case APLASMA:
183 if(st&Z_INWATER) {
184 Z_sound(snd[12],128);
185 Z_water_trap(&wp[i].o);
186 Z_chktrap(1,10,wp[i].own,HIT_ELECTRO);
187 Z_untrap(5);
188 wp[i].t=0;break;
190 case BALL1:
191 case BALL7:
192 case BALL2:
193 case MANF:
194 if(wp[i].s>=2)
195 {if(++wp[i].s>=((wp[i].t==BALL1 || wp[i].t==BALL7 || wp[i].t==BALL2 || wp[i].t==MANF)?8:12)) wp[i].t=0; break;}
196 if(st&Z_HITAIR) Z_set_speed(&wp[i].o,16);
197 if(st&(Z_HITWALL|Z_HITCEIL|Z_HITLAND))
198 {wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[7],128);break;}
199 else if(Z_hit(&wp[i].o,(wp[i].t==BALL7 || wp[i].t==MANF)?40:((wp[i].t==BALL2)?20:5),wp[i].own,HIT_SOME))
200 {wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[7],128);break;}
201 wp[i].s^=1;break;
202 case BFGBALL:
203 if(st&Z_INWATER) {
204 Z_sound(snd[8],40);Z_sound(snd[13],128);
205 Z_water_trap(&wp[i].o);
206 Z_chktrap(1,1000,wp[i].own,HIT_ELECTRO);
207 Z_untrap(5);
208 wp[i].t=0;break;
210 if(wp[i].s>=2) {if(++wp[i].s>=14) wp[i].t=0; break;}
211 else if(st&(Z_HITWALL|Z_HITCEIL|Z_HITLAND)) {
212 Z_bfg9000(wp[i].o.x,wp[i].o.y,wp[i].own);
213 wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[8],128);break;}
214 else if(Z_hit(&wp[i].o,100,wp[i].own,HIT_BFG)) {
215 Z_bfg9000(wp[i].o.x,wp[i].o.y,wp[i].own);
216 wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[8],128);break;}
217 bfg_fly(wp[i].o.x,wp[i].o.y-wp[i].o.h/2,wp[i].own);
218 wp[i].s^=1;break;
219 case BFGHIT:
220 if(++wp[i].s>=8) wp[i].t=0;
221 break;
222 default: break;
227 void WP_draw(void) {
228 int i,s,d,x,y;
230 for(i=0;i<MAXWPN;++i) {
231 s=-1;d=0;
232 switch(wp[i].t) {
233 case NONE: default: break;
234 case REVF:
235 case ROCKET:
236 if((d=wp[i].s)<2) {
237 d=(wp[i].o.xv>0)?1:0;
238 x=abs(wp[i].o.xv);y=wp[i].o.yv;s=0;
239 if(y<0) {if(-y>=x) s=30;}
240 else if(y>0) if(y>=x/2) s=31;
241 }else {s=(d-2)/2+1;d=0;}
242 break;
243 case MANF:
244 if((s=wp[i].s)>=2) {s/=2;break;}
245 case PLASMA:
246 case APLASMA:
247 case BALL1:
248 case BALL7:
249 case BALL2:
250 if((s=wp[i].s)>=2) s=s/2+1;
251 switch(wp[i].t) {
252 case PLASMA: s+=4;break;
253 case APLASMA: s+=11;break;
254 case BALL1: s+=32;break;
255 case BALL2: s+=42;break;
256 case BALL7: s+=37;d=(wp[i].o.xv>=0)?1:0;break;
257 case MANF: s+=47;d=(wp[i].o.xv>=0)?1:0;break;
258 }break;
259 case BFGBALL:
260 if((s=wp[i].s)>=2) s=s/2+1;
261 s+=18;break;
262 case BFGHIT:
263 s=wp[i].s/2+26;break;
265 if(s>=0) Z_drawspr(wp[i].o.x,wp[i].o.y,spr[s*2+d],sprd[s*2+d]);
269 void WP_gun(int x,int y,int xd,int yd,int o,int v) {
270 register dword d,m;
271 int sx,sy,lx,ly;
272 dword xe,ye,s;
273 byte f;
275 f=BM_MONSTER|BM_WALL;
276 if(o!=-1) f|=BM_PLR1;
277 if(o!=-2) f|=BM_PLR2;
278 if((xd-=x)>0) sx=1;
279 else if(xd<0) sx=-1;
280 else sx=0;
281 if((yd-=y)>0) sy=1;
282 else if(yd<0) sy=-1;
283 else sy=0;
284 if(!xd && !yd) return;
285 if((xd=abs(xd)) > (yd=abs(yd))) d=xd; else d=yd;
286 hit_xv=xd*10/d*sx;
287 hit_yv=yd*10/d*sy;
288 xe=ye=0;
289 lx=x;ly=y;
290 for(;;) {
291 if(x<0 || x>=FLDW*8 || y<0 || y>=FLDH*8) break;
292 if(((m=bmap[y>>5][x>>5])&f)) {
293 if(m&BM_WALL) if(fld[y>>3][x>>3]==1 || fld[y>>3][x>>3]==2) {
294 for(x=lx,y=ly,xe=ye=0;fld[y>>3][x>>3]!=1 && fld[y>>3][x>>3]!=2;) {
295 lx=x;ly=y;
296 if((xe+=xd)>=d) {xe-=d;x+=sx;}
297 if((ye+=yd)>=d) {ye-=d;y+=sy;}
299 DOT_spark(lx,ly,sx*10,sy*10,1);
300 break;
302 if(m&(BM_MONSTER|BM_PLR1|BM_PLR2)) if(Z_gunhit(x,y,o,sx*v,sy*v)) break;
303 lx=x;ly=y;
304 if((xe+=(xd<<3))>=d) {
305 x+=xe/d*sx;xe=xe%d;
307 if((ye+=(yd<<3))>=d) {
308 y+=ye/d*sy;ye=ye%d;
310 }else{
311 if(sx==0) m=0;
312 else{m=x&31;if(sx>0) m^=31; ++m;}
313 if(sy==0) s=0;
314 else{s=y&31;if(sy>0) s^=31; ++s;}
315 if((s<m && s!=0) || m==0) m=s;
316 lx=x;ly=y;
317 x+=(xd*m+xe)/d*sx;xe=(xd*m+xe)%d;
318 y+=(yd*m+ye)/d*sy;ye=(yd*m+ye)%d;
323 void WP_punch(int x,int y,int d,int own) {
324 obj_t o;
326 o.x=x;o.y=y;o.r=12;o.h=26;
327 o.xv=o.yv=o.vx=o.vy=0;
328 if(Z_hit(&o,d,own,HIT_SOME)) Z_sound(snd[9],128);
331 int WP_chainsaw(int x,int y,int d,int own) {
332 obj_t o;
334 o.x=x;o.y=y;o.r=12;o.h=26;
335 o.xv=o.yv=o.vx=o.vy=0;
336 if(Z_hit(&o,d,own,HIT_SOME)) return 1;
337 return 0;
340 static void throw(int i,int x,int y,int xd,int yd,int r,int h,int s) {
341 int m;
343 wp[i].o.x=x;wp[i].o.y=y+h/2;
344 yd-=y;xd-=x;
345 if(!(m=max(abs(xd),abs(yd)))) m=1;
346 wp[i].o.xv=xd*s/m;wp[i].o.yv=yd*s/m;
347 wp[i].o.r=r;wp[i].o.h=h;
348 wp[i].o.vx=wp[i].o.vy=0;
351 void WP_rocket(int x,int y,int xd,int yd,int o) {
352 int i;
354 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
355 Z_sound(snd[3],128);
356 wp[i].t=ROCKET;wp[i].s=(xd>=x)?1:0;
357 wp[i].own=o;
358 throw(i,x,y,xd,yd,2,5,12);
359 return;
363 void WP_revf(int x,int y,int xd,int yd,int o,int t) {
364 int i;
366 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
367 Z_sound(snd[3],128);
368 wp[i].t=REVF;wp[i].s=(xd>=x)?1:0;
369 wp[i].own=o;wp[i].target=t;
370 throw(i,x,y,xd,yd,2,5,12);
371 return;
375 void WP_plasma(int x,int y,int xd,int yd,int o) {
376 int i;
378 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
379 Z_sound(snd[5],64);
380 wp[i].t=PLASMA;wp[i].s=0;
381 wp[i].own=o;
382 throw(i,x,y,xd,yd,2,5,16);
383 return;
387 void WP_ball1(int x,int y,int xd,int yd,int o) {
388 int i;
390 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
391 wp[i].t=BALL1;wp[i].s=0;
392 wp[i].own=o;
393 throw(i,x,y,xd,yd,2,5,16);
394 return;
398 void WP_ball2(int x,int y,int xd,int yd,int o) {
399 int i;
401 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
402 wp[i].t=BALL2;wp[i].s=0;
403 wp[i].own=o;
404 throw(i,x,y,xd,yd,2,5,16);
405 return;
409 void WP_ball7(int x,int y,int xd,int yd,int o) {
410 int i;
412 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
413 wp[i].t=BALL7;wp[i].s=0;
414 wp[i].own=o;
415 throw(i,x,y,xd,yd,2,5,16);
416 return;
420 void WP_aplasma(int x,int y,int xd,int yd,int o) {
421 int i;
423 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
424 Z_sound(snd[5],64);
425 wp[i].t=APLASMA;wp[i].s=0;
426 wp[i].own=o;
427 throw(i,x,y,xd,yd,2,5,16);
428 return;
432 void WP_manfire(int x,int y,int xd,int yd,int o) {
433 int i;
435 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
436 Z_sound(snd[6],128);
437 wp[i].t=MANF;wp[i].s=0;
438 wp[i].own=o;
439 throw(i,x,y,xd,yd,5,11,16);
440 return;
444 void WP_bfgshot(int x,int y,int xd,int yd,int o) {
445 int i;
447 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
448 wp[i].t=BFGBALL;wp[i].s=0;
449 wp[i].own=o;
450 throw(i,x,y,xd,yd,5,12,16);
451 return;
455 void WP_bfghit(int x,int y,int o) {
456 int i;
458 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
459 wp[i].t=BFGHIT;wp[i].s=0;
460 wp[i].o.x=x;wp[i].o.y=y;
461 wp[i].o.xv=wp[i].o.yv=0;
462 wp[i].o.r=0;wp[i].o.h=1;
463 wp[i].o.vx=wp[i].o.vy=0;
464 wp[i].own=o;
465 return;
469 void WP_pistol(int x,int y,int xd,int yd,int o) {
470 Z_sound(snd[0],96);
471 WP_gun(x,y,xd,yd,o,1);
472 if(g_dm) {
473 WP_gun(x,y+1,xd,yd+1,o,1);
474 WP_gun(x,y-1,xd,yd-1,o,1);
478 void WP_mgun(int x,int y,int xd,int yd,int o) {
479 Z_sound(snd[11],128);
480 WP_gun(x,y,xd,yd,o,1);
483 void WP_shotgun(int x,int y,int xd,int yd,int o) {
484 int i,j;
486 Z_sound(snd[1],128);
487 for(i=0;i<10;++i) {
488 j=myrand(4*2+1)-4;
489 WP_gun(x,y+j,xd,yd+j,o,i&1);
493 void WP_dshotgun(int x,int y,int xd,int yd,int o) {
494 int i,j;
496 Z_sound(snd[2],128);
497 for(i=(g_dm)?25:20;i>=0;--i) {
498 j=myrand(10*2+1)-10;
499 WP_gun(x,y+j,xd,yd+j,o,(i%3)?0:1);
503 void WP_ognemet(int x,int y,int xd,int yd,int xv,int yv,int o) {
504 int m;
506 m=abs(xd-x);if(!m) m=abs(yd-y);
507 SMK_flame(x,y,xv,yv,2,2,(xd-x)*3000/m,(yd-y)*3000/m,1,o);