DEADSOFTWARE

577e402b58c794f8356eca0871e44d22b9d1ad0a
[flatwaifu.git] / src / smoke.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 "files.h"
26 #include "memory.h"
27 #include "vga.h"
28 #include "error.h"
29 #include "sound.h"
30 #include "view.h"
31 #include "smoke.h"
32 #include "fx.h"
33 #include "misc.h"
35 #define MAXSR 20
37 #define SMSN 10
38 #define FLSN 8
40 #pragma pack(1)
41 typedef struct{
42 int x,y,xv,yv;
43 byte t,s;
44 short o;
45 }smoke_t;
46 #pragma pack()
48 static smoke_t sm[MAXSMOK];
49 static int sr_r,sxr[MAXSR],syr[MAXSR];
50 static int lsm;
52 static void *spr[SMSN],*fspr[FLSN];
53 static void *burnsnd;
54 static int burntm=0;
56 byte flametab[16]={
57 0xBC,0xBA,0xB8,0xB6,0xB4,0xB2,0xB0,0xD5,0xD6,0xD7,0xA1,0xA0,0xE3,0xE2,0xE1,0xE0
58 };
60 void SMK_savegame(FILE* h) {
61 int i,n;
63 for(i=n=0;i<MAXSMOK;++i) if(sm[i].t) ++n;
64 myfwrite(&n,1,4,h);
65 for(i=0;i<MAXSMOK;++i) if(sm[i].t) myfwrite(&sm[i],1,sizeof(sm[0]),h);
66 }
68 void SMK_loadgame(FILE* h) {
69 int n;
71 myfread(&n,1,4,h);
72 myfread(sm,1,n*sizeof(sm[0]),h);
73 }
75 void SMK_init(void) {
76 int i;
78 for(i=0;i<MAXSMOK;++i) {sm[i].t=0;}
79 lsm=0;
80 burntm=0;
81 }
83 void SMK_alloc(void) {
84 int i;
86 burnsnd=Z_getsnd("BURN");
87 for(i=0;i<SMSN;++i) spr[i]=Z_getspr("SMOK",i,0,NULL);
88 for(i=0;i<FLSN;++i) fspr[i]=Z_getspr("FLAM",i,0,NULL);
89 for(i=0;i<MAXSR;++i) {
90 sxr[i]=myrand(256*2+1)-256;
91 syr[i]=myrand(256*2+1)-256;
92 }
93 sr_r=0;
94 }
96 static void inclast(void) {
97 if(++lsm>=MAXSMOK) lsm=0;
98 }
100 void SMK_act(void) {
101 int i,ox,oy;
102 static obj_t o;
104 if(burntm) --burntm;
105 for(i=0;i<MAXSMOK;++i) if(sm[i].t) {
106 if(sm[i].s) {
107 ox=sm[i].x;oy=sm[i].y;
108 sm[i].xv=Z_dec(sm[i].xv,20);
109 sm[i].yv=Z_dec(sm[i].yv,20);
110 sm[i].x+=sm[i].xv/2;sm[i].y+=sm[i].yv/2;
111 if(!Z_canfit(sm[i].x>>8,(sm[i].y>>8)+3,3,7)) {
112 sm[i].x=ox;sm[i].y=oy;
113 }else if(Z_inwater(sm[i].x>>8,(sm[i].y>>8)+3,3,7)) {
114 sm[i].x=ox;sm[i].y=oy;
116 ox=sm[i].x;oy=sm[i].y;
117 sm[i].x+=sm[i].xv/2;sm[i].y+=sm[i].yv/2;
118 if(!Z_canfit(sm[i].x>>8,(sm[i].y>>8)+3,3,7)) {
119 sm[i].x=ox;sm[i].y=oy;
120 }else if(Z_inwater(sm[i].x>>8,(sm[i].y>>8)+3,3,7)) {
121 sm[i].x=ox;sm[i].y=oy;
123 if(sm[i].o!=-3) {
124 o.x=sm[i].x>>8;o.y=sm[i].y>>8;
125 o.xv=sm[i].xv>>10;o.yv=sm[i].yv>>10;
126 o.vx=o.vy=0;
127 if(!(g_time&3)) Z_hit(&o,1,sm[i].o,HIT_FLAME);
129 }else{
130 ox=sm[i].x;oy=sm[i].y;
131 sm[i].xv=Z_dec(sm[i].xv,20);
132 sm[i].yv=Z_dec(sm[i].yv,20);
133 sm[i].x+=sm[i].xv;sm[i].y+=sm[i].yv;
134 if(!Z_canfit(sm[i].x>>8,(sm[i].y>>8)+3,3,7)) {
135 sm[i].x=ox;sm[i].y=oy;
136 }else if(Z_inwater(sm[i].x>>8,(sm[i].y>>8)+3,3,7)) {
137 sm[i].x=ox;sm[i].y=oy;
140 --sm[i].t;
144 void SMK_draw(void) {
145 int i,s;
147 for(i=0;i<MAXSMOK;++i) if(sm[i].t) switch(sm[i].s) {
148 case 0:
149 if((s=sm[i].t)>=(SMSN-1)*3) s=0; else s=SMSN-1-s/3;
150 V_sprf((sm[i].x>>8)-w_x+WD/2,(sm[i].y>>8)-w_y+HT/2+1+w_o,spr[s],&smoke_sprf);//V_sprf((sm[i].x>>8)-w_x+100,(sm[i].y>>8)-w_y+50+w_o,spr[s],&smoke_sprf);
151 break;
152 case 1:
153 if((s=sm[i].t)>=(FLSN-1)) s=0; else s=FLSN-1-s;
154 V_sprf((sm[i].x>>8)-w_x+WD/2,(sm[i].y>>8)-w_y+HT/2+1+w_o,fspr[s],&flame_sprf);//V_sprf((sm[i].x>>8)-w_x+100,(sm[i].y>>8)-w_y+50+w_o,fspr[s],&flame_sprf);
155 break;
159 void SMK_add(int x,int y,int xv,int yv,byte t,byte s,short o) {
160 int i;
162 if(!Z_canfit(x>>8,(y>>8)+3,3,7)) return;
163 if(Z_inwater(x>>8,(y>>8)+3,3,7)) {FX_bubble(x>>8,y>>8,xv,yv,1);return;}
164 i=lsm;
165 sm[i].x=x;sm[i].y=y;
166 sm[i].xv=xv;sm[i].yv=yv;
167 sm[i].t=t;sm[i].s=s;
168 sm[i].o=o;
169 inclast();
172 void SMK_gas(int x0,int y0,int xr,int yr,int xv,int yv,int k) {
173 int i,x,y;
174 static int sxv,syv;
176 xv=-xv;yv=-yv;
177 sxv=xv*k;syv=yv*k;
178 k=max(abs(xv),abs(yv));
179 if(!k) return;
180 for(i=0;i<=k;i+=3) {
181 x=((xv*i/k+x0)<<8)+sxr[sr_r]*xr;
182 y=((yv*i/k+y0)<<8)+syr[sr_r]*yr;
183 if(++sr_r>=MAXSR) sr_r=0;
184 SMK_add(x,y,sxv,syv,SMSN*3,0,-3);
188 void SMK_flame(int x0,int y0,int ox,int oy,int xr,int yr,int xv,int yv,int k,int o) {
189 int i,x,y;
190 static int sxv,syv;
192 sxv=xv*k;syv=yv*k;
193 xv=xv-(ox<<8);yv=yv-(oy<<8);
194 k=max(abs(xv),abs(yv));
195 if(!k) return;
196 if(!burntm) burntm=Z_sound(burnsnd,128);
197 for(i=0;i<=k;i+=200) {
198 x=xv*i/k+(x0<<8)+sxr[sr_r]*xr;
199 y=yv*i/k+(y0<<8)+syr[sr_r]*yr;
200 if(++sr_r>=MAXSR) sr_r=0;
201 SMK_add(x,y,sxv,syv,FLSN,1,o);