DEADSOFTWARE

portability: avoid errors on some compilers
[flatwaifu.git] / src / weapons.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 "view.h"
21 #include "bmap.h"
22 #include "dots.h"
23 #include "smoke.h"
24 #include "weapons.h"
25 #include "misc.h"
26 #include "files.h"
27 #include "game.h"
28 #include "player.h"
29 #include "monster.h"
30 #include "switch.h"
32 enum{NONE=0,ROCKET,PLASMA,APLASMA,BALL1,BALL2,BALL7,BFGBALL,BFGHIT,
33 MANF,REVF,FIRE};
35 weapon_t wp[MAXWPN];
37 static void *snd[14];
38 static void throw(int,int,int,int,int,int,int,int);
40 void WP_alloc (void) {
41 int i;
42 static char nm[14][6]={
43 "PISTOL",
44 "SHOTGN",
45 "DSHTGN",
46 "RLAUNC",
47 "RXPLOD",
48 "PLASMA",
49 "FIRSHT",
50 "FIRXPL",
51 "BAREXP",
52 "PUNCH",
53 "SAWHIT",
54 "MGUN",
55 "SPARK1",
56 "SPARK2"
57 };
58 for(i=0;i<14;++i) snd[i]=Z_getsnd(nm[i]);
59 }
61 void WP_init (void) {
62 int i;
64 for(i=0;i<MAXWPN;++i) wp[i].t=NONE;
65 }
67 void WP_act (void) {
68 int i,st;
69 static obj_t o;
71 for(i=0;i<MAXWPN;++i) if(wp[i].t) {
72 if(wp[i].t==ROCKET || wp[i].t==REVF)
73 SMK_gas(wp[i].o.x+Z_sign(wp[i].o.xv)*2,
74 wp[i].o.y-wp[i].o.h/2,3,3,
75 wp[i].o.xv+wp[i].o.vx,wp[i].o.yv+wp[i].o.vy,64
76 );
77 --wp[i].o.yv;st=Z_moveobj(&wp[i].o);
78 if(st&Z_FALLOUT) {wp[i].t=0;continue;}
79 if(st&Z_HITWATER) switch(wp[i].t) {
80 case PLASMA: case APLASMA:
81 case BFGBALL:
82 break;
83 default:
84 Z_splash(&wp[i].o,wp[i].o.r+wp[i].o.h);break;
85 }
86 switch(wp[i].t) {
87 case REVF:
88 if(Z_getobjpos(wp[i].target,&o))
89 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);
90 case ROCKET:
91 if(wp[i].s>=2) {if(++wp[i].s>=8) wp[i].t=0; break;}
92 if(st&Z_HITAIR) Z_set_speed(&wp[i].o,12);
93 if(st&(Z_HITWALL|Z_HITCEIL|Z_HITLAND)) {
94 wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[4],128);
95 Z_explode(wp[i].o.x,wp[i].o.y,30,wp[i].own);break;}
96 else if(Z_hit(&wp[i].o,10,wp[i].own,HIT_SOME)) {
97 wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[4],128);
98 Z_explode(wp[i].o.x,wp[i].o.y-wp[i].o.h/2,30,wp[i].own);break;}
99 bfg_fly(wp[i].o.x,wp[i].o.y-wp[i].o.h/2,wp[i].own);
100 break;
101 case PLASMA:
102 case APLASMA:
103 if(st&Z_INWATER) {
104 Z_sound(snd[12],128);
105 Z_water_trap(&wp[i].o);
106 Z_chktrap(1,10,wp[i].own,HIT_ELECTRO);
107 Z_untrap(5);
108 wp[i].t=0;break;
110 case BALL1:
111 case BALL7:
112 case BALL2:
113 case MANF:
114 if(wp[i].s>=2)
115 {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;}
116 if(st&Z_HITAIR) Z_set_speed(&wp[i].o,16);
117 if(st&(Z_HITWALL|Z_HITCEIL|Z_HITLAND))
118 {wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[7],128);break;}
119 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))
120 {wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[7],128);break;}
121 wp[i].s^=1;break;
122 case BFGBALL:
123 if(st&Z_INWATER) {
124 Z_sound(snd[8],40);Z_sound(snd[13],128);
125 Z_water_trap(&wp[i].o);
126 Z_chktrap(1,1000,wp[i].own,HIT_ELECTRO);
127 Z_untrap(5);
128 wp[i].t=0;break;
130 if(wp[i].s>=2) {if(++wp[i].s>=14) wp[i].t=0; break;}
131 else if(st&(Z_HITWALL|Z_HITCEIL|Z_HITLAND)) {
132 Z_bfg9000(wp[i].o.x,wp[i].o.y,wp[i].own);
133 wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[8],128);break;}
134 else if(Z_hit(&wp[i].o,100,wp[i].own,HIT_BFG)) {
135 Z_bfg9000(wp[i].o.x,wp[i].o.y,wp[i].own);
136 wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[8],128);break;}
137 bfg_fly(wp[i].o.x,wp[i].o.y-wp[i].o.h/2,wp[i].own);
138 wp[i].s^=1;break;
139 case BFGHIT:
140 if(++wp[i].s>=8) wp[i].t=0;
141 break;
142 default: break;
147 void WP_gun (int x, int y, int xd, int yd, int o, int v) {
148 register dword d,m;
149 int sx,sy,lx,ly;
150 dword xe,ye,s;
151 byte f;
153 f=BM_MONSTER|BM_WALL;
154 if(o!=-1) f|=BM_PLR1;
155 if(o!=-2) f|=BM_PLR2;
156 if((xd-=x)>0) sx=1;
157 else if(xd<0) sx=-1;
158 else sx=0;
159 if((yd-=y)>0) sy=1;
160 else if(yd<0) sy=-1;
161 else sy=0;
162 if(!xd && !yd) return;
163 if((xd=abs(xd)) > (yd=abs(yd))) d=xd; else d=yd;
164 hit_xv=xd*10/d*sx;
165 hit_yv=yd*10/d*sy;
166 xe=ye=0;
167 lx=x;ly=y;
168 for(;;) {
169 if(x<0 || x>=FLDW*8 || y<0 || y>=FLDH*8) break;
170 if(((m=bmap[y>>5][x>>5])&f)) {
171 if(m&BM_WALL) if(fld[y>>3][x>>3]==1 || fld[y>>3][x>>3]==2) {
172 for(x=lx,y=ly,xe=ye=0;fld[y>>3][x>>3]!=1 && fld[y>>3][x>>3]!=2;) {
173 lx=x;ly=y;
174 if((xe+=xd)>=d) {xe-=d;x+=sx;}
175 if((ye+=yd)>=d) {ye-=d;y+=sy;}
177 DOT_spark(lx,ly,sx*10,sy*10,1);
178 break;
180 if(m&(BM_MONSTER|BM_PLR1|BM_PLR2)) if(Z_gunhit(x,y,o,sx*v,sy*v)) break;
181 lx=x;ly=y;
182 if((xe+=(xd<<3))>=d) {
183 x+=xe/d*sx;xe=xe%d;
185 if((ye+=(yd<<3))>=d) {
186 y+=ye/d*sy;ye=ye%d;
188 }else{
189 if(sx==0) m=0;
190 else{m=x&31;if(sx>0) m^=31; ++m;}
191 if(sy==0) s=0;
192 else{s=y&31;if(sy>0) s^=31; ++s;}
193 if((s<m && s!=0) || m==0) m=s;
194 lx=x;ly=y;
195 x+=(xd*m+xe)/d*sx;xe=(xd*m+xe)%d;
196 y+=(yd*m+ye)/d*sy;ye=(yd*m+ye)%d;
201 void WP_punch (int x, int y, int d, int own) {
202 obj_t o;
204 o.x=x;o.y=y;o.r=12;o.h=26;
205 o.xv=o.yv=o.vx=o.vy=0;
206 if(Z_hit(&o,d,own,HIT_SOME)) Z_sound(snd[9],128);
209 int WP_chainsaw (int x, int y, int d, int own) {
210 obj_t o;
212 o.x=x;o.y=y;o.r=12;o.h=26;
213 o.xv=o.yv=o.vx=o.vy=0;
214 if(Z_hit(&o,d,own,HIT_SOME)) return 1;
215 return 0;
218 static void throw(int i,int x,int y,int xd,int yd,int r,int h,int s) {
219 int m;
221 wp[i].o.x=x;wp[i].o.y=y+h/2;
222 yd-=y;xd-=x;
223 if(!(m=max(abs(xd),abs(yd)))) m=1;
224 wp[i].o.xv=xd*s/m;wp[i].o.yv=yd*s/m;
225 wp[i].o.r=r;wp[i].o.h=h;
226 wp[i].o.vx=wp[i].o.vy=0;
229 void WP_rocket (int x, int y, int xd, int yd, int o) {
230 int i;
232 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
233 Z_sound(snd[3],128);
234 wp[i].t=ROCKET;wp[i].s=(xd>=x)?1:0;
235 wp[i].own=o;
236 throw(i,x,y,xd,yd,2,5,12);
237 return;
241 void WP_revf (int x, int y, int xd, int yd, int o, int t) {
242 int i;
244 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
245 Z_sound(snd[3],128);
246 wp[i].t=REVF;wp[i].s=(xd>=x)?1:0;
247 wp[i].own=o;wp[i].target=t;
248 throw(i,x,y,xd,yd,2,5,12);
249 return;
253 void WP_plasma (int x, int y, int xd, int yd, int o) {
254 int i;
256 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
257 Z_sound(snd[5],64);
258 wp[i].t=PLASMA;wp[i].s=0;
259 wp[i].own=o;
260 throw(i,x,y,xd,yd,2,5,16);
261 return;
265 void WP_ball1 (int x, int y, int xd, int yd, int o) {
266 int i;
268 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
269 wp[i].t=BALL1;wp[i].s=0;
270 wp[i].own=o;
271 throw(i,x,y,xd,yd,2,5,16);
272 return;
276 void WP_ball2 (int x, int y, int xd, int yd, int o) {
277 int i;
279 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
280 wp[i].t=BALL2;wp[i].s=0;
281 wp[i].own=o;
282 throw(i,x,y,xd,yd,2,5,16);
283 return;
287 void WP_ball7 (int x, int y, int xd, int yd, int o) {
288 int i;
290 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
291 wp[i].t=BALL7;wp[i].s=0;
292 wp[i].own=o;
293 throw(i,x,y,xd,yd,2,5,16);
294 return;
298 void WP_aplasma (int x, int y, int xd, int yd, int o) {
299 int i;
301 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
302 Z_sound(snd[5],64);
303 wp[i].t=APLASMA;wp[i].s=0;
304 wp[i].own=o;
305 throw(i,x,y,xd,yd,2,5,16);
306 return;
310 void WP_manfire (int x, int y, int xd, int yd, int o) {
311 int i;
313 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
314 Z_sound(snd[6],128);
315 wp[i].t=MANF;wp[i].s=0;
316 wp[i].own=o;
317 throw(i,x,y,xd,yd,5,11,16);
318 return;
322 void WP_bfgshot (int x, int y, int xd, int yd, int o) {
323 int i;
325 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
326 wp[i].t=BFGBALL;wp[i].s=0;
327 wp[i].own=o;
328 throw(i,x,y,xd,yd,5,12,16);
329 return;
333 void WP_bfghit (int x, int y, int o) {
334 int i;
336 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
337 wp[i].t=BFGHIT;wp[i].s=0;
338 wp[i].o.x=x;wp[i].o.y=y;
339 wp[i].o.xv=wp[i].o.yv=0;
340 wp[i].o.r=0;wp[i].o.h=1;
341 wp[i].o.vx=wp[i].o.vy=0;
342 wp[i].own=o;
343 return;
347 void WP_pistol (int x,int y,int xd,int yd,int o) {
348 Z_sound(snd[0],96);
349 WP_gun(x,y,xd,yd,o,1);
350 if(g_dm) {
351 WP_gun(x,y+1,xd,yd+1,o,1);
352 WP_gun(x,y-1,xd,yd-1,o,1);
356 void WP_mgun (int x, int y, int xd, int yd, int o) {
357 Z_sound(snd[11],128);
358 WP_gun(x,y,xd,yd,o,1);
361 void WP_shotgun (int x, int y, int xd, int yd, int o) {
362 int i,j;
364 Z_sound(snd[1],128);
365 for(i=0;i<10;++i) {
366 j=myrand(4*2+1)-4;
367 WP_gun(x,y+j,xd,yd+j,o,i&1);
371 void WP_dshotgun (int x, int y, int xd, int yd, int o) {
372 int i,j;
374 Z_sound(snd[2],128);
375 for(i=(g_dm)?25:20;i>=0;--i) {
376 j=myrand(10*2+1)-10;
377 WP_gun(x,y+j,xd,yd+j,o,(i%3)?0:1);
381 void WP_ognemet (int x, int y, int xd, int yd, int xv, int yv, int o) {
382 int m;
384 m=abs(xd-x);if(!m) m=abs(yd-y);
385 SMK_flame(x,y,xv,yv,2,2,(xd-x)*3000/m,(yd-y)*3000/m,1,o);