DEADSOFTWARE

ppc: fix saves and more map loading improvements
[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 i, n;
56 for (n = MAXWPN - 1; n >= 0 && wp[n].t == 0; n--) {
57 // empty
58 }
59 n += 1;
60 myfwrite32(n, h);
61 for (i = 0; i < n; i++) {
62 myfwrite32(wp[i].o.x, h);
63 myfwrite32(wp[i].o.y, h);
64 myfwrite32(wp[i].o.xv, h);
65 myfwrite32(wp[i].o.yv, h);
66 myfwrite32(wp[i].o.vx, h);
67 myfwrite32(wp[i].o.vy, h);
68 myfwrite32(wp[i].o.r, h);
69 myfwrite32(wp[i].o.h, h);
70 myfwrite8(wp[i].t, h);
71 myfwrite8(wp[i].s, h);
72 myfwrite32(wp[i].own, h);
73 myfwrite16(wp[i].target, h);
74 }
75 }
77 void WP_loadgame (FILE *h) {
78 int i, n;
79 myfread32(&n, h);
80 for (i = 0; i < n; i++) {
81 myfread32(&wp[i].o.x, h);
82 myfread32(&wp[i].o.y, h);
83 myfread32(&wp[i].o.xv, h);
84 myfread32(&wp[i].o.yv, h);
85 myfread32(&wp[i].o.vx, h);
86 myfread32(&wp[i].o.vy, h);
87 myfread32(&wp[i].o.r, h);
88 myfread32(&wp[i].o.h, h);
89 myfread8(&wp[i].t, h);
90 myfread8(&wp[i].s, h);
91 myfread32(&wp[i].own, h);
92 myfread16(&wp[i].target, h);
93 }
94 }
96 void WP_alloc(void) {
97 int i;
98 static char nm[14][6]={
99 "PISTOL",
100 "SHOTGN",
101 "DSHTGN",
102 "RLAUNC",
103 "RXPLOD",
104 "PLASMA",
105 "FIRSHT",
106 "FIRXPL",
107 "BAREXP",
108 "PUNCH",
109 "SAWHIT",
110 "MGUN",
111 "SPARK1",
112 "SPARK2"
113 };
115 for(i=0;i<4;++i) {
116 spr[i*2]=Z_getspr("MISL",i,1,sprd+i*2);
117 spr[i*2+1]=Z_getspr("MISL",i,2,sprd+i*2+1);
119 for(;i<6;++i) {
120 spr[i*2]=Z_getspr("PLSS",i-4,1,sprd+i*2);
121 spr[i*2+1]=Z_getspr("PLSS",i-4,2,sprd+i*2+1);
123 for(;i<11;++i) {
124 spr[i*2]=Z_getspr("PLSE",i-6,1,sprd+i*2);
125 spr[i*2+1]=Z_getspr("PLSE",i-6,2,sprd+i*2+1);
127 for(;i<13;++i) {
128 spr[i*2]=Z_getspr("APLS",i-11,1,sprd+i*2);
129 spr[i*2+1]=Z_getspr("APLS",i-11,2,sprd+i*2+1);
131 for(;i<18;++i) {
132 spr[i*2]=Z_getspr("APBX",i-13,1,sprd+i*2);
133 spr[i*2+1]=Z_getspr("APBX",i-13,2,sprd+i*2+1);
135 for(;i<20;++i) {
136 spr[i*2]=Z_getspr("BFS1",i-18,1,sprd+i*2);
137 spr[i*2+1]=Z_getspr("BFS1",i-18,2,sprd+i*2+1);
139 for(;i<26;++i) {
140 spr[i*2]=Z_getspr("BFE1",i-20,1,sprd+i*2);
141 spr[i*2+1]=Z_getspr("BFE1",i-20,2,sprd+i*2+1);
143 for(;i<30;++i) {
144 spr[i*2]=Z_getspr("BFE2",i-26,1,sprd+i*2);
145 spr[i*2+1]=Z_getspr("BFE2",i-26,2,sprd+i*2+1);
147 for(;i<32;++i) {
148 spr[i*2]=Z_getspr("MISL",i-30+4,1,sprd+i*2);
149 spr[i*2+1]=Z_getspr("MISL",i-30+4,2,sprd+i*2+1);
151 for(;i<37;++i) {
152 spr[i*2]=Z_getspr("BAL1",i-32,1,sprd+i*2);
153 spr[i*2+1]=Z_getspr("BAL1",i-32,2,sprd+i*2+1);
155 for(;i<42;++i) {
156 spr[i*2]=Z_getspr("BAL7",i-37,1,sprd+i*2);
157 spr[i*2+1]=Z_getspr("BAL7",i-37,2,sprd+i*2+1);
159 for(;i<47;++i) {
160 spr[i*2]=Z_getspr("BAL2",i-42,1,sprd+i*2);
161 spr[i*2+1]=Z_getspr("BAL2",i-42,2,sprd+i*2+1);
163 for(;i<49;++i) {
164 spr[i*2]=Z_getspr("MANF",i-47,1,sprd+i*2);
165 spr[i*2+1]=Z_getspr("MANF",i-47,2,sprd+i*2+1);
167 for(i=0;i<14;++i) snd[i]=Z_getsnd(nm[i]);
170 void WP_init(void) {
171 int i;
173 for(i=0;i<MAXWPN;++i) wp[i].t=NONE;
176 void WP_act(void) {
177 int i,st;
178 static obj_t o;
180 for(i=0;i<MAXWPN;++i) if(wp[i].t) {
181 if(wp[i].t==ROCKET || wp[i].t==REVF)
182 SMK_gas(wp[i].o.x+Z_sign(wp[i].o.xv)*2,
183 wp[i].o.y-wp[i].o.h/2,3,3,
184 wp[i].o.xv+wp[i].o.vx,wp[i].o.yv+wp[i].o.vy,64
185 );
186 --wp[i].o.yv;st=Z_moveobj(&wp[i].o);
187 if(st&Z_FALLOUT) {wp[i].t=0;continue;}
188 if(st&Z_HITWATER) switch(wp[i].t) {
189 case PLASMA: case APLASMA:
190 case BFGBALL:
191 break;
192 default:
193 Z_splash(&wp[i].o,wp[i].o.r+wp[i].o.h);break;
195 switch(wp[i].t) {
196 case REVF:
197 if(Z_getobjpos(wp[i].target,&o))
198 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);
199 case ROCKET:
200 if(wp[i].s>=2) {if(++wp[i].s>=8) wp[i].t=0; break;}
201 if(st&Z_HITAIR) Z_set_speed(&wp[i].o,12);
202 if(st&(Z_HITWALL|Z_HITCEIL|Z_HITLAND)) {
203 wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[4],128);
204 Z_explode(wp[i].o.x,wp[i].o.y,30,wp[i].own);break;}
205 else if(Z_hit(&wp[i].o,10,wp[i].own,HIT_SOME)) {
206 wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[4],128);
207 Z_explode(wp[i].o.x,wp[i].o.y-wp[i].o.h/2,30,wp[i].own);break;}
208 bfg_fly(wp[i].o.x,wp[i].o.y-wp[i].o.h/2,wp[i].own);
209 break;
210 case PLASMA:
211 case APLASMA:
212 if(st&Z_INWATER) {
213 Z_sound(snd[12],128);
214 Z_water_trap(&wp[i].o);
215 Z_chktrap(1,10,wp[i].own,HIT_ELECTRO);
216 Z_untrap(5);
217 wp[i].t=0;break;
219 case BALL1:
220 case BALL7:
221 case BALL2:
222 case MANF:
223 if(wp[i].s>=2)
224 {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;}
225 if(st&Z_HITAIR) Z_set_speed(&wp[i].o,16);
226 if(st&(Z_HITWALL|Z_HITCEIL|Z_HITLAND))
227 {wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[7],128);break;}
228 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))
229 {wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[7],128);break;}
230 wp[i].s^=1;break;
231 case BFGBALL:
232 if(st&Z_INWATER) {
233 Z_sound(snd[8],40);Z_sound(snd[13],128);
234 Z_water_trap(&wp[i].o);
235 Z_chktrap(1,1000,wp[i].own,HIT_ELECTRO);
236 Z_untrap(5);
237 wp[i].t=0;break;
239 if(wp[i].s>=2) {if(++wp[i].s>=14) wp[i].t=0; break;}
240 else if(st&(Z_HITWALL|Z_HITCEIL|Z_HITLAND)) {
241 Z_bfg9000(wp[i].o.x,wp[i].o.y,wp[i].own);
242 wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[8],128);break;}
243 else if(Z_hit(&wp[i].o,100,wp[i].own,HIT_BFG)) {
244 Z_bfg9000(wp[i].o.x,wp[i].o.y,wp[i].own);
245 wp[i].s=2;wp[i].o.xv=wp[i].o.yv=0;Z_sound(snd[8],128);break;}
246 bfg_fly(wp[i].o.x,wp[i].o.y-wp[i].o.h/2,wp[i].own);
247 wp[i].s^=1;break;
248 case BFGHIT:
249 if(++wp[i].s>=8) wp[i].t=0;
250 break;
251 default: break;
256 void WP_draw(void) {
257 int i,s,d,x,y;
259 for(i=0;i<MAXWPN;++i) {
260 s=-1;d=0;
261 switch(wp[i].t) {
262 case NONE: default: break;
263 case REVF:
264 case ROCKET:
265 if((d=wp[i].s)<2) {
266 d=(wp[i].o.xv>0)?1:0;
267 x=abs(wp[i].o.xv);y=wp[i].o.yv;s=0;
268 if(y<0) {if(-y>=x) s=30;}
269 else if(y>0) if(y>=x/2) s=31;
270 }else {s=(d-2)/2+1;d=0;}
271 break;
272 case MANF:
273 if((s=wp[i].s)>=2) {s/=2;break;}
274 case PLASMA:
275 case APLASMA:
276 case BALL1:
277 case BALL7:
278 case BALL2:
279 if((s=wp[i].s)>=2) s=s/2+1;
280 switch(wp[i].t) {
281 case PLASMA: s+=4;break;
282 case APLASMA: s+=11;break;
283 case BALL1: s+=32;break;
284 case BALL2: s+=42;break;
285 case BALL7: s+=37;d=(wp[i].o.xv>=0)?1:0;break;
286 case MANF: s+=47;d=(wp[i].o.xv>=0)?1:0;break;
287 }break;
288 case BFGBALL:
289 if((s=wp[i].s)>=2) s=s/2+1;
290 s+=18;break;
291 case BFGHIT:
292 s=wp[i].s/2+26;break;
294 if(s>=0) Z_drawspr(wp[i].o.x,wp[i].o.y,spr[s*2+d],sprd[s*2+d]);
298 void WP_gun(int x,int y,int xd,int yd,int o,int v) {
299 register dword d,m;
300 int sx,sy,lx,ly;
301 dword xe,ye,s;
302 byte f;
304 f=BM_MONSTER|BM_WALL;
305 if(o!=-1) f|=BM_PLR1;
306 if(o!=-2) f|=BM_PLR2;
307 if((xd-=x)>0) sx=1;
308 else if(xd<0) sx=-1;
309 else sx=0;
310 if((yd-=y)>0) sy=1;
311 else if(yd<0) sy=-1;
312 else sy=0;
313 if(!xd && !yd) return;
314 if((xd=abs(xd)) > (yd=abs(yd))) d=xd; else d=yd;
315 hit_xv=xd*10/d*sx;
316 hit_yv=yd*10/d*sy;
317 xe=ye=0;
318 lx=x;ly=y;
319 for(;;) {
320 if(x<0 || x>=FLDW*8 || y<0 || y>=FLDH*8) break;
321 if(((m=bmap[y>>5][x>>5])&f)) {
322 if(m&BM_WALL) if(fld[y>>3][x>>3]==1 || fld[y>>3][x>>3]==2) {
323 for(x=lx,y=ly,xe=ye=0;fld[y>>3][x>>3]!=1 && fld[y>>3][x>>3]!=2;) {
324 lx=x;ly=y;
325 if((xe+=xd)>=d) {xe-=d;x+=sx;}
326 if((ye+=yd)>=d) {ye-=d;y+=sy;}
328 DOT_spark(lx,ly,sx*10,sy*10,1);
329 break;
331 if(m&(BM_MONSTER|BM_PLR1|BM_PLR2)) if(Z_gunhit(x,y,o,sx*v,sy*v)) break;
332 lx=x;ly=y;
333 if((xe+=(xd<<3))>=d) {
334 x+=xe/d*sx;xe=xe%d;
336 if((ye+=(yd<<3))>=d) {
337 y+=ye/d*sy;ye=ye%d;
339 }else{
340 if(sx==0) m=0;
341 else{m=x&31;if(sx>0) m^=31; ++m;}
342 if(sy==0) s=0;
343 else{s=y&31;if(sy>0) s^=31; ++s;}
344 if((s<m && s!=0) || m==0) m=s;
345 lx=x;ly=y;
346 x+=(xd*m+xe)/d*sx;xe=(xd*m+xe)%d;
347 y+=(yd*m+ye)/d*sy;ye=(yd*m+ye)%d;
352 void WP_punch(int x,int y,int d,int own) {
353 obj_t o;
355 o.x=x;o.y=y;o.r=12;o.h=26;
356 o.xv=o.yv=o.vx=o.vy=0;
357 if(Z_hit(&o,d,own,HIT_SOME)) Z_sound(snd[9],128);
360 int WP_chainsaw(int x,int y,int d,int own) {
361 obj_t o;
363 o.x=x;o.y=y;o.r=12;o.h=26;
364 o.xv=o.yv=o.vx=o.vy=0;
365 if(Z_hit(&o,d,own,HIT_SOME)) return 1;
366 return 0;
369 static void throw(int i,int x,int y,int xd,int yd,int r,int h,int s) {
370 int m;
372 wp[i].o.x=x;wp[i].o.y=y+h/2;
373 yd-=y;xd-=x;
374 if(!(m=max(abs(xd),abs(yd)))) m=1;
375 wp[i].o.xv=xd*s/m;wp[i].o.yv=yd*s/m;
376 wp[i].o.r=r;wp[i].o.h=h;
377 wp[i].o.vx=wp[i].o.vy=0;
380 void WP_rocket(int x,int y,int xd,int yd,int o) {
381 int i;
383 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
384 Z_sound(snd[3],128);
385 wp[i].t=ROCKET;wp[i].s=(xd>=x)?1:0;
386 wp[i].own=o;
387 throw(i,x,y,xd,yd,2,5,12);
388 return;
392 void WP_revf(int x,int y,int xd,int yd,int o,int t) {
393 int i;
395 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
396 Z_sound(snd[3],128);
397 wp[i].t=REVF;wp[i].s=(xd>=x)?1:0;
398 wp[i].own=o;wp[i].target=t;
399 throw(i,x,y,xd,yd,2,5,12);
400 return;
404 void WP_plasma(int x,int y,int xd,int yd,int o) {
405 int i;
407 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
408 Z_sound(snd[5],64);
409 wp[i].t=PLASMA;wp[i].s=0;
410 wp[i].own=o;
411 throw(i,x,y,xd,yd,2,5,16);
412 return;
416 void WP_ball1(int x,int y,int xd,int yd,int o) {
417 int i;
419 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
420 wp[i].t=BALL1;wp[i].s=0;
421 wp[i].own=o;
422 throw(i,x,y,xd,yd,2,5,16);
423 return;
427 void WP_ball2(int x,int y,int xd,int yd,int o) {
428 int i;
430 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
431 wp[i].t=BALL2;wp[i].s=0;
432 wp[i].own=o;
433 throw(i,x,y,xd,yd,2,5,16);
434 return;
438 void WP_ball7(int x,int y,int xd,int yd,int o) {
439 int i;
441 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
442 wp[i].t=BALL7;wp[i].s=0;
443 wp[i].own=o;
444 throw(i,x,y,xd,yd,2,5,16);
445 return;
449 void WP_aplasma(int x,int y,int xd,int yd,int o) {
450 int i;
452 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
453 Z_sound(snd[5],64);
454 wp[i].t=APLASMA;wp[i].s=0;
455 wp[i].own=o;
456 throw(i,x,y,xd,yd,2,5,16);
457 return;
461 void WP_manfire(int x,int y,int xd,int yd,int o) {
462 int i;
464 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
465 Z_sound(snd[6],128);
466 wp[i].t=MANF;wp[i].s=0;
467 wp[i].own=o;
468 throw(i,x,y,xd,yd,5,11,16);
469 return;
473 void WP_bfgshot(int x,int y,int xd,int yd,int o) {
474 int i;
476 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
477 wp[i].t=BFGBALL;wp[i].s=0;
478 wp[i].own=o;
479 throw(i,x,y,xd,yd,5,12,16);
480 return;
484 void WP_bfghit(int x,int y,int o) {
485 int i;
487 for(i=0;i<MAXWPN;++i) if(!wp[i].t) {
488 wp[i].t=BFGHIT;wp[i].s=0;
489 wp[i].o.x=x;wp[i].o.y=y;
490 wp[i].o.xv=wp[i].o.yv=0;
491 wp[i].o.r=0;wp[i].o.h=1;
492 wp[i].o.vx=wp[i].o.vy=0;
493 wp[i].own=o;
494 return;
498 void WP_pistol(int x,int y,int xd,int yd,int o) {
499 Z_sound(snd[0],96);
500 WP_gun(x,y,xd,yd,o,1);
501 if(g_dm) {
502 WP_gun(x,y+1,xd,yd+1,o,1);
503 WP_gun(x,y-1,xd,yd-1,o,1);
507 void WP_mgun(int x,int y,int xd,int yd,int o) {
508 Z_sound(snd[11],128);
509 WP_gun(x,y,xd,yd,o,1);
512 void WP_shotgun(int x,int y,int xd,int yd,int o) {
513 int i,j;
515 Z_sound(snd[1],128);
516 for(i=0;i<10;++i) {
517 j=myrand(4*2+1)-4;
518 WP_gun(x,y+j,xd,yd+j,o,i&1);
522 void WP_dshotgun(int x,int y,int xd,int yd,int o) {
523 int i,j;
525 Z_sound(snd[2],128);
526 for(i=(g_dm)?25:20;i>=0;--i) {
527 j=myrand(10*2+1)-10;
528 WP_gun(x,y+j,xd,yd+j,o,(i%3)?0:1);
532 void WP_ognemet(int x,int y,int xd,int yd,int xv,int yv,int o) {
533 int m;
535 m=abs(xd-x);if(!m) m=abs(yd-y);
536 SMK_flame(x,y,xv,yv,2,2,(xd-x)*3000/m,(yd-y)*3000/m,1,o);