DEADSOFTWARE

ppc: fix saves and more map loading improvements
[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 "vga.h"
26 #include "view.h"
27 #include "smoke.h"
28 #include "fx.h"
29 #include "misc.h"
31 #define MAXSR 20
33 #define SMSN 10
34 #define FLSN 8
36 #pragma pack(1)
37 typedef struct{
38 int x,y,xv,yv;
39 byte t,s;
40 short o;
41 }smoke_t;
42 #pragma pack()
44 static smoke_t sm[MAXSMOK];
45 static int sr_r,sxr[MAXSR],syr[MAXSR];
46 static int lsm;
48 static void *spr[SMSN],*fspr[FLSN];
49 static void *burnsnd;
50 static int burntm=0;
52 byte flametab[16]={
53 0xBC,0xBA,0xB8,0xB6,0xB4,0xB2,0xB0,0xD5,0xD6,0xD7,0xA1,0xA0,0xE3,0xE2,0xE1,0xE0
54 };
56 void SMK_savegame (FILE *h) {
57 int i, n;
58 for (i = n = 0; i < MAXSMOK; ++i) {
59 if (sm[i].t) {
60 ++n;
61 }
62 }
63 myfwrite32(n, h);
64 for (i = 0; i < MAXSMOK; ++i) {
65 if (sm[i].t) {
66 myfwrite32(sm[i].x, h);
67 myfwrite32(sm[i].y, h);
68 myfwrite32(sm[i].xv, h);
69 myfwrite32(sm[i].xv, h);
70 myfwrite8(sm[i].t, h);
71 myfwrite8(sm[i].s, h);
72 myfwrite16(sm[i].o, h);
73 }
74 }
75 }
77 void SMK_loadgame (FILE *h) {
78 int i, n;
79 myfread32(&n, h);
80 for (i = 0; i < n; ++i) {
81 myfread32(&sm[i].x, h);
82 myfread32(&sm[i].y, h);
83 myfread32(&sm[i].xv, h);
84 myfread32(&sm[i].xv, h);
85 myfread8(&sm[i].t, h);
86 myfread8(&sm[i].s, h);
87 myfread16(&sm[i].o, h);
88 }
89 }
91 void SMK_init(void) {
92 int i;
94 for(i=0;i<MAXSMOK;++i) {sm[i].t=0;}
95 lsm=0;
96 burntm=0;
97 }
99 void SMK_alloc(void) {
100 int i;
102 burnsnd=Z_getsnd("BURN");
103 for(i=0;i<SMSN;++i) spr[i]=Z_getspr("SMOK",i,0,NULL);
104 for(i=0;i<FLSN;++i) fspr[i]=Z_getspr("FLAM",i,0,NULL);
105 for(i=0;i<MAXSR;++i) {
106 sxr[i]=myrand(256*2+1)-256;
107 syr[i]=myrand(256*2+1)-256;
109 sr_r=0;
112 static void inclast(void) {
113 if(++lsm>=MAXSMOK) lsm=0;
116 void SMK_act(void) {
117 int i,ox,oy;
118 static obj_t o;
120 if(burntm) --burntm;
121 for(i=0;i<MAXSMOK;++i) if(sm[i].t) {
122 if(sm[i].s) {
123 ox=sm[i].x;oy=sm[i].y;
124 sm[i].xv=Z_dec(sm[i].xv,20);
125 sm[i].yv=Z_dec(sm[i].yv,20);
126 sm[i].x+=sm[i].xv/2;sm[i].y+=sm[i].yv/2;
127 if(!Z_canfit(sm[i].x>>8,(sm[i].y>>8)+3,3,7)) {
128 sm[i].x=ox;sm[i].y=oy;
129 }else if(Z_inwater(sm[i].x>>8,(sm[i].y>>8)+3,3,7)) {
130 sm[i].x=ox;sm[i].y=oy;
132 ox=sm[i].x;oy=sm[i].y;
133 sm[i].x+=sm[i].xv/2;sm[i].y+=sm[i].yv/2;
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;
139 if(sm[i].o!=-3) {
140 o.x=sm[i].x>>8;o.y=sm[i].y>>8;
141 o.xv=sm[i].xv>>10;o.yv=sm[i].yv>>10;
142 o.vx=o.vy=0;
143 if(!(g_time&3)) Z_hit(&o,1,sm[i].o,HIT_FLAME);
145 }else{
146 ox=sm[i].x;oy=sm[i].y;
147 sm[i].xv=Z_dec(sm[i].xv,20);
148 sm[i].yv=Z_dec(sm[i].yv,20);
149 sm[i].x+=sm[i].xv;sm[i].y+=sm[i].yv;
150 if(!Z_canfit(sm[i].x>>8,(sm[i].y>>8)+3,3,7)) {
151 sm[i].x=ox;sm[i].y=oy;
152 }else if(Z_inwater(sm[i].x>>8,(sm[i].y>>8)+3,3,7)) {
153 sm[i].x=ox;sm[i].y=oy;
156 --sm[i].t;
160 void SMK_draw(void) {
161 int i,s;
163 for(i=0;i<MAXSMOK;++i) if(sm[i].t) switch(sm[i].s) {
164 case 0:
165 if((s=sm[i].t)>=(SMSN-1)*3) s=0; else s=SMSN-1-s/3;
166 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);
167 break;
168 case 1:
169 if((s=sm[i].t)>=(FLSN-1)) s=0; else s=FLSN-1-s;
170 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);
171 break;
175 void SMK_add(int x,int y,int xv,int yv,byte t,byte s,short o) {
176 int i;
178 if(!Z_canfit(x>>8,(y>>8)+3,3,7)) return;
179 if(Z_inwater(x>>8,(y>>8)+3,3,7)) {FX_bubble(x>>8,y>>8,xv,yv,1);return;}
180 i=lsm;
181 sm[i].x=x;sm[i].y=y;
182 sm[i].xv=xv;sm[i].yv=yv;
183 sm[i].t=t;sm[i].s=s;
184 sm[i].o=o;
185 inclast();
188 void SMK_gas(int x0,int y0,int xr,int yr,int xv,int yv,int k) {
189 int i,x,y;
190 static int sxv,syv;
192 xv=-xv;yv=-yv;
193 sxv=xv*k;syv=yv*k;
194 k=max(abs(xv),abs(yv));
195 if(!k) return;
196 for(i=0;i<=k;i+=3) {
197 x=((xv*i/k+x0)<<8)+sxr[sr_r]*xr;
198 y=((yv*i/k+y0)<<8)+syr[sr_r]*yr;
199 if(++sr_r>=MAXSR) sr_r=0;
200 SMK_add(x,y,sxv,syv,SMSN*3,0,-3);
204 void SMK_flame(int x0,int y0,int ox,int oy,int xr,int yr,int xv,int yv,int k,int o) {
205 int i,x,y;
206 static int sxv,syv;
208 sxv=xv*k;syv=yv*k;
209 xv=xv-(ox<<8);yv=yv-(oy<<8);
210 k=max(abs(xv),abs(yv));
211 if(!k) return;
212 if(!burntm) burntm=Z_sound(burnsnd,128);
213 for(i=0;i<=k;i+=200) {
214 x=xv*i/k+(x0<<8)+sxr[sr_r]*xr;
215 y=yv*i/k+(y0<<8)+syr[sr_r]*yr;
216 if(++sr_r>=MAXSR) sr_r=0;
217 SMK_add(x,y,sxv,syv,FLSN,1,o);