1 #include "Air.h"
2 
3 #include <cmath>
4 #include <algorithm>
5 
6 #include "Simulation.h"
7 #include "ElementClasses.h"
8 #include "common/tpt-rand.h"
9 
10 /*float kernel[9];
11 
12 float vx[YRES/CELL][XRES/CELL], ovx[YRES/CELL][XRES/CELL];
13 float vy[YRES/CELL][XRES/CELL], ovy[YRES/CELL][XRES/CELL];
14 float pv[YRES/CELL][XRES/CELL], opv[YRES/CELL][XRES/CELL];
15 unsigned char bmap_blockair[YRES/CELL][XRES/CELL];
16 
17 float cb_vx[YRES/CELL][XRES/CELL];
18 float cb_vy[YRES/CELL][XRES/CELL];
19 float cb_pv[YRES/CELL][XRES/CELL];
20 float cb_hv[YRES/CELL][XRES/CELL];
21 
22 float fvx[YRES/CELL][XRES/CELL], fvy[YRES/CELL][XRES/CELL];
23 
24 float hv[YRES/CELL][XRES/CELL], ohv[YRES/CELL][XRES/CELL]; // For Ambient Heat */
25 
make_kernel(void)26 void Air::make_kernel(void) //used for velocity
27 {
28 	int i, j;
29 	float s = 0.0f;
30 	for (j=-1; j<2; j++)
31 		for (i=-1; i<2; i++)
32 		{
33 			kernel[(i+1)+3*(j+1)] = expf(-2.0f*(i*i+j*j));
34 			s += kernel[(i+1)+3*(j+1)];
35 		}
36 	s = 1.0f / s;
37 	for (j=-1; j<2; j++)
38 		for (i=-1; i<2; i++)
39 			kernel[(i+1)+3*(j+1)] *= s;
40 }
41 
Clear()42 void Air::Clear()
43 {
44 	std::fill(&pv[0][0], &pv[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f);
45 	std::fill(&vy[0][0], &vy[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f);
46 	std::fill(&vx[0][0], &vx[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f);
47 }
48 
ClearAirH()49 void Air::ClearAirH()
50 {
51 	std::fill(&hv[0][0], &hv[0][0]+((XRES/CELL)*(YRES/CELL)), ambientAirTemp);
52 }
53 
update_airh(void)54 void Air::update_airh(void)
55 {
56 	int x, y, i, j;
57 	float odh, dh, dx, dy, f, tx, ty;
58 	for (i=0; i<YRES/CELL; i++) //reduces pressure/velocity on the edges every frame
59 	{
60 		hv[i][0] = ambientAirTemp;
61 		hv[i][1] = ambientAirTemp;
62 		hv[i][XRES/CELL-3] = ambientAirTemp;
63 		hv[i][XRES/CELL-2] = ambientAirTemp;
64 		hv[i][XRES/CELL-1] = ambientAirTemp;
65 	}
66 	for (i=0; i<XRES/CELL; i++) //reduces pressure/velocity on the edges every frame
67 	{
68 		hv[0][i] = ambientAirTemp;
69 		hv[1][i] = ambientAirTemp;
70 		hv[YRES/CELL-3][i] = ambientAirTemp;
71 		hv[YRES/CELL-2][i] = ambientAirTemp;
72 		hv[YRES/CELL-1][i] = ambientAirTemp;
73 	}
74 	for (y=0; y<YRES/CELL; y++) //update velocity and pressure
75 	{
76 		for (x=0; x<XRES/CELL; x++)
77 		{
78 			dh = 0.0f;
79 			dx = 0.0f;
80 			dy = 0.0f;
81 			for (j=-1; j<2; j++)
82 			{
83 				for (i=-1; i<2; i++)
84 				{
85 					if (y+j>0 && y+j<YRES/CELL-2 &&
86 					        x+i>0 && x+i<XRES/CELL-2 &&
87 					        !(bmap_blockairh[y+j][x+i]&0x8))
88 						{
89 						f = kernel[i+1+(j+1)*3];
90 						dh += hv[y+j][x+i]*f;
91 						dx += vx[y+j][x+i]*f;
92 						dy += vy[y+j][x+i]*f;
93 					}
94 					else
95 					{
96 						f = kernel[i+1+(j+1)*3];
97 						dh += hv[y][x]*f;
98 						dx += vx[y][x]*f;
99 						dy += vy[y][x]*f;
100 					}
101 				}
102 			}
103 			tx = x - dx*0.7f;
104 			ty = y - dy*0.7f;
105 			i = (int)tx;
106 			j = (int)ty;
107 			tx -= i;
108 			ty -= j;
109 			if (i>=2 && i<XRES/CELL-3 && j>=2 && j<YRES/CELL-3)
110 			{
111 				odh = dh;
112 				dh *= 1.0f - AIR_VADV;
113 				dh += AIR_VADV*(1.0f-tx)*(1.0f-ty)*((bmap_blockairh[j][i]&0x8) ? odh : hv[j][i]);
114 				dh += AIR_VADV*tx*(1.0f-ty)*((bmap_blockairh[j][i+1]&0x8) ? odh : hv[j][i+1]);
115 				dh += AIR_VADV*(1.0f-tx)*ty*((bmap_blockairh[j+1][i]&0x8) ? odh : hv[j+1][i]);
116 				dh += AIR_VADV*tx*ty*((bmap_blockairh[j+1][i+1]&0x8) ? odh : hv[j+1][i+1]);
117 			}
118 			if(!sim.gravityMode)
119 			{ //Vertical gravity only for the time being
120 				float airdiff = hv[y-1][x]-hv[y][x];
121 				if(airdiff>0 && !(bmap_blockairh[y-1][x]&0x8))
122 					vy[y][x] -= airdiff/5000.0f;
123 			}
124 			ohv[y][x] = dh;
125 		}
126 	}
127 	memcpy(hv, ohv, sizeof(hv));
128 }
129 
update_air(void)130 void Air::update_air(void)
131 {
132 	int x = 0, y = 0, i = 0, j = 0;
133 	float dp = 0.0f, dx = 0.0f, dy = 0.0f, f = 0.0f, tx = 0.0f, ty = 0.0f;
134 	const float advDistanceMult = 0.7f;
135 	float stepX, stepY;
136 	int stepLimit, step;
137 
138 	if (airMode != 4) { //airMode 4 is no air/pressure update
139 
140 		for (i=0; i<YRES/CELL; i++) //reduces pressure/velocity on the edges every frame
141 		{
142 			pv[i][0] = pv[i][0]*0.8f;
143 			pv[i][1] = pv[i][1]*0.8f;
144 			pv[i][2] = pv[i][2]*0.8f;
145 			pv[i][XRES/CELL-2] = pv[i][XRES/CELL-2]*0.8f;
146 			pv[i][XRES/CELL-1] = pv[i][XRES/CELL-1]*0.8f;
147 			vx[i][0] = vx[i][0]*0.9f;
148 			vx[i][1] = vx[i][1]*0.9f;
149 			vx[i][XRES/CELL-2] = vx[i][XRES/CELL-2]*0.9f;
150 			vx[i][XRES/CELL-1] = vx[i][XRES/CELL-1]*0.9f;
151 			vy[i][0] = vy[i][0]*0.9f;
152 			vy[i][1] = vy[i][1]*0.9f;
153 			vy[i][XRES/CELL-2] = vy[i][XRES/CELL-2]*0.9f;
154 			vy[i][XRES/CELL-1] = vy[i][XRES/CELL-1]*0.9f;
155 		}
156 		for (i=0; i<XRES/CELL; i++) //reduces pressure/velocity on the edges every frame
157 		{
158 			pv[0][i] = pv[0][i]*0.8f;
159 			pv[1][i] = pv[1][i]*0.8f;
160 			pv[2][i] = pv[2][i]*0.8f;
161 			pv[YRES/CELL-2][i] = pv[YRES/CELL-2][i]*0.8f;
162 			pv[YRES/CELL-1][i] = pv[YRES/CELL-1][i]*0.8f;
163 			vx[0][i] = vx[0][i]*0.9f;
164 			vx[1][i] = vx[1][i]*0.9f;
165 			vx[YRES/CELL-2][i] = vx[YRES/CELL-2][i]*0.9f;
166 			vx[YRES/CELL-1][i] = vx[YRES/CELL-1][i]*0.9f;
167 			vy[0][i] = vy[0][i]*0.9f;
168 			vy[1][i] = vy[1][i]*0.9f;
169 			vy[YRES/CELL-2][i] = vy[YRES/CELL-2][i]*0.9f;
170 			vy[YRES/CELL-1][i] = vy[YRES/CELL-1][i]*0.9f;
171 		}
172 
173 		for (j=1; j<YRES/CELL; j++) //clear some velocities near walls
174 		{
175 			for (i=1; i<XRES/CELL; i++)
176 			{
177 				if (bmap_blockair[j][i])
178 				{
179 					vx[j][i] = 0.0f;
180 					vx[j][i-1] = 0.0f;
181 					vy[j][i] = 0.0f;
182 					vy[j-1][i] = 0.0f;
183 				}
184 			}
185 		}
186 
187 		for (y=1; y<YRES/CELL; y++) //pressure adjustments from velocity
188 			for (x=1; x<XRES/CELL; x++)
189 			{
190 				dp = 0.0f;
191 				dp += vx[y][x-1] - vx[y][x];
192 				dp += vy[y-1][x] - vy[y][x];
193 				pv[y][x] *= AIR_PLOSS;
194 				pv[y][x] += dp*AIR_TSTEPP;
195 			}
196 
197 		for (y=0; y<YRES/CELL-1; y++) //velocity adjustments from pressure
198 			for (x=0; x<XRES/CELL-1; x++)
199 			{
200 				dx = dy = 0.0f;
201 				dx += pv[y][x] - pv[y][x+1];
202 				dy += pv[y][x] - pv[y+1][x];
203 				vx[y][x] *= AIR_VLOSS;
204 				vy[y][x] *= AIR_VLOSS;
205 				vx[y][x] += dx*AIR_TSTEPV;
206 				vy[y][x] += dy*AIR_TSTEPV;
207 				if (bmap_blockair[y][x] || bmap_blockair[y][x+1])
208 					vx[y][x] = 0;
209 				if (bmap_blockair[y][x] || bmap_blockair[y+1][x])
210 					vy[y][x] = 0;
211 			}
212 
213 		for (y=0; y<YRES/CELL; y++) //update velocity and pressure
214 			for (x=0; x<XRES/CELL; x++)
215 			{
216 				dx = 0.0f;
217 				dy = 0.0f;
218 				dp = 0.0f;
219 				for (j=-1; j<2; j++)
220 					for (i=-1; i<2; i++)
221 						if (y+j>0 && y+j<YRES/CELL-1 &&
222 						        x+i>0 && x+i<XRES/CELL-1 &&
223 						        !bmap_blockair[y+j][x+i])
224 						{
225 							f = kernel[i+1+(j+1)*3];
226 							dx += vx[y+j][x+i]*f;
227 							dy += vy[y+j][x+i]*f;
228 							dp += pv[y+j][x+i]*f;
229 						}
230 						else
231 						{
232 							f = kernel[i+1+(j+1)*3];
233 							dx += vx[y][x]*f;
234 							dy += vy[y][x]*f;
235 							dp += pv[y][x]*f;
236 						}
237 
238 				tx = x - dx*advDistanceMult;
239 				ty = y - dy*advDistanceMult;
240 				if ((dx*advDistanceMult>1.0f || dy*advDistanceMult>1.0f) && (tx>=2 && tx<XRES/CELL-2 && ty>=2 && ty<YRES/CELL-2))
241 				{
242 					// Trying to take velocity from far away, check whether there is an intervening wall. Step from current position to desired source location, looking for walls, with either the x or y step size being 1 cell
243 					if (std::abs(dx)>std::abs(dy))
244 					{
245 						stepX = (dx<0.0f) ? 1 : -1;
246 						stepY = -dy/fabsf(dx);
247 						stepLimit = (int)(fabsf(dx*advDistanceMult));
248 					}
249 					else
250 					{
251 						stepY = (dy<0.0f) ? 1 : -1;
252 						stepX = -dx/fabsf(dy);
253 						stepLimit = (int)(fabsf(dy*advDistanceMult));
254 					}
255 					tx = x;
256 					ty = y;
257 					for (step=0; step<stepLimit; ++step)
258 					{
259 						tx += stepX;
260 						ty += stepY;
261 						if (bmap_blockair[(int)(ty+0.5f)][(int)(tx+0.5f)])
262 						{
263 							tx -= stepX;
264 							ty -= stepY;
265 							break;
266 						}
267 					}
268 					if (step==stepLimit)
269 					{
270 						// No wall found
271 						tx = x - dx*advDistanceMult;
272 						ty = y - dy*advDistanceMult;
273 					}
274 				}
275 				i = (int)tx;
276 				j = (int)ty;
277 				tx -= i;
278 				ty -= j;
279 				if (!bmap_blockair[y][x] && i>=2 && i<=XRES/CELL-3 &&
280 				        j>=2 && j<=YRES/CELL-3)
281 				{
282 					dx *= 1.0f - AIR_VADV;
283 					dy *= 1.0f - AIR_VADV;
284 
285 					dx += AIR_VADV*(1.0f-tx)*(1.0f-ty)*vx[j][i];
286 					dy += AIR_VADV*(1.0f-tx)*(1.0f-ty)*vy[j][i];
287 
288 					dx += AIR_VADV*tx*(1.0f-ty)*vx[j][i+1];
289 					dy += AIR_VADV*tx*(1.0f-ty)*vy[j][i+1];
290 
291 					dx += AIR_VADV*(1.0f-tx)*ty*vx[j+1][i];
292 					dy += AIR_VADV*(1.0f-tx)*ty*vy[j+1][i];
293 
294 					dx += AIR_VADV*tx*ty*vx[j+1][i+1];
295 					dy += AIR_VADV*tx*ty*vy[j+1][i+1];
296 				}
297 
298 				if (bmap[y][x] == WL_FAN)
299 				{
300 					dx += fvx[y][x];
301 					dy += fvy[y][x];
302 				}
303 				// pressure/velocity caps
304 				if (dp > 256.0f) dp = 256.0f;
305 				if (dp < -256.0f) dp = -256.0f;
306 				if (dx > 256.0f) dx = 256.0f;
307 				if (dx < -256.0f) dx = -256.0f;
308 				if (dy > 256.0f) dy = 256.0f;
309 				if (dy < -256.0f) dy = -256.0f;
310 
311 
312 				switch (airMode)
313 				{
314 				default:
315 				case 0:  //Default
316 					break;
317 				case 1:  //0 Pressure
318 					dp = 0.0f;
319 					break;
320 				case 2:  //0 Velocity
321 					dx = 0.0f;
322 					dy = 0.0f;
323 					break;
324 				case 3: //0 Air
325 					dx = 0.0f;
326 					dy = 0.0f;
327 					dp = 0.0f;
328 					break;
329 				case 4: //No Update
330 					break;
331 				}
332 
333 				ovx[y][x] = dx;
334 				ovy[y][x] = dy;
335 				opv[y][x] = dp;
336 			}
337 		memcpy(vx, ovx, sizeof(vx));
338 		memcpy(vy, ovy, sizeof(vy));
339 		memcpy(pv, opv, sizeof(pv));
340 	}
341 }
342 
Invert()343 void Air::Invert()
344 {
345 	int nx, ny;
346 	for (nx = 0; nx<XRES/CELL; nx++)
347 		for (ny = 0; ny<YRES/CELL; ny++)
348 		{
349 			pv[ny][nx] = -pv[ny][nx];
350 			vx[ny][nx] = -vx[ny][nx];
351 			vy[ny][nx] = -vy[ny][nx];
352 		}
353 }
354 
355 // called when loading saves / stamps to ensure nothing "leaks" the first frame
RecalculateBlockAirMaps()356 void Air::RecalculateBlockAirMaps()
357 {
358 	for (int i = 0; i <= sim.parts_lastActiveIndex; i++)
359 	{
360 		int type = sim.parts[i].type;
361 		if (!type)
362 			continue;
363 		// Real TTAN would only block if there was enough TTAN
364 		// but it would be more expensive and complicated to actually check that
365 		// so just block for a frame, if it wasn't supposed to block it will continue allowing air next frame
366 		if (type == PT_TTAN)
367 		{
368 			int x = ((int)(sim.parts[i].x+0.5f))/CELL, y = ((int)(sim.parts[i].y+0.5f))/CELL;
369 			if (sim.InBounds(x, y))
370 			{
371 				bmap_blockair[y][x] = 1;
372 				bmap_blockairh[y][x] = 0x8;
373 			}
374 		}
375 		// mostly accurate insulator blocking, besides checking GEL
376 		else if ((type == PT_HSWC && sim.parts[i].life != 10) || sim.elements[type].HeatConduct <= (random_gen()%250))
377 		{
378 			int x = ((int)(sim.parts[i].x+0.5f))/CELL, y = ((int)(sim.parts[i].y+0.5f))/CELL;
379 			if (sim.InBounds(x, y) && !(bmap_blockairh[y][x]&0x8))
380 				bmap_blockairh[y][x]++;
381 		}
382 	}
383 }
384 
Air(Simulation & simulation)385 Air::Air(Simulation & simulation):
386 	sim(simulation),
387 	airMode(0),
388 	ambientAirTemp(295.15f)
389 {
390 	//Simulation should do this.
391 	make_kernel();
392 	std::fill(&bmap_blockair[0][0], &bmap_blockair[0][0]+((XRES/CELL)*(YRES/CELL)), 0);
393 	std::fill(&bmap_blockairh[0][0], &bmap_blockairh[0][0]+((XRES/CELL)*(YRES/CELL)), 0);
394 	std::fill(&vx[0][0], &vx[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f);
395 	std::fill(&ovx[0][0], &ovx[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f);
396 	std::fill(&vy[0][0], &vy[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f);
397 	std::fill(&ovy[0][0], &ovy[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f);
398 	std::fill(&hv[0][0], &hv[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f);
399 	std::fill(&ohv[0][0], &ohv[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f);
400 	std::fill(&pv[0][0], &pv[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f);
401 	std::fill(&opv[0][0], &opv[0][0]+((XRES/CELL)*(YRES/CELL)), 0.0f);
402 }
403