From 22805e14f19517896e60ca8e0b443ce96b3e64ac Mon Sep 17 00:00:00 2001 From: savask Date: Sat, 13 Aug 2022 23:40:26 +0700 Subject: [PATCH] Tidy up some conditionals in simulation code. (#854) --- src/simulation/Simulation.cpp | 147 ++++++++++++---------------------- 1 file changed, 49 insertions(+), 98 deletions(-) diff --git a/src/simulation/Simulation.cpp b/src/simulation/Simulation.cpp index a14a66b74..6321897bf 100644 --- a/src/simulation/Simulation.cpp +++ b/src/simulation/Simulation.cpp @@ -1252,10 +1252,7 @@ void Simulation::ApplyDecorationLine(int x1, int y1, int x2, int y2, int colR, i } dx = x2 - x1; dy = abs(y2 - y1); - if (dx) - de = dy/(float)dx; - else - de = 0.0f; + de = dx ? dy/(float)dx : 0.0f; y = y1; sy = (y1 0) - pressureResistance = (float)parts[ID(r)].tmp; - else - pressureResistance = 4.0f; + pressureResistance = (parts[ID(r)].tmp > 0) ? (float)parts[ID(r)].tmp : 4.0f; + if (pv[ny/CELL][nx/CELL] >= -pressureResistance && pv[ny/CELL][nx/CELL] <= pressureResistance) { part_change_type(i,x,y,PT_NEUT); @@ -3381,15 +3360,10 @@ void Simulation::create_gain_photon(int pp)//photons from PHOT going through GLO return; i = pfree; - lr = RNG::Ref().between(0, 1); + lr = 2*RNG::Ref().between(0, 1) - 1; // -1 or 1 - if (lr) { - xx = parts[pp].x - 0.3*parts[pp].vy; - yy = parts[pp].y + 0.3*parts[pp].vx; - } else { - xx = parts[pp].x + 0.3*parts[pp].vy; - yy = parts[pp].y - 0.3*parts[pp].vx; - } + xx = parts[pp].x - lr*0.3*parts[pp].vy; + yy = parts[pp].y + lr*0.3*parts[pp].vx; nx = (int)(xx + 0.5f); ny = (int)(yy + 0.5f); @@ -3474,11 +3448,8 @@ void Simulation::delete_part(int x, int y)//calls kill_part with the particle lo if (x<0 || y<0 || x>=XRES || y>=YRES) return; - if (photons[y][x]) { - i = photons[y][x]; - } else { - i = pmap[y][x]; - } + + i = photons[y][x] ? photons[y][x] : pmap[y][x]; if (!i) return; @@ -3508,18 +3479,24 @@ void Simulation::UpdateParticles(int start, int end) x = (int)(parts[i].x+0.5f); y = (int)(parts[i].y+0.5f); - //this kills any particle out of the screen, or in a wall where it isn't supposed to go - if (x=XRES-CELL || y>=YRES-CELL || - (bmap[y/CELL][x/CELL] && - (bmap[y/CELL][x/CELL]==WL_WALL || - bmap[y/CELL][x/CELL]==WL_WALLELEC || - bmap[y/CELL][x/CELL]==WL_ALLOWAIR || - (bmap[y/CELL][x/CELL]==WL_DESTROYALL) || - (bmap[y/CELL][x/CELL]==WL_ALLOWLIQUID && !(elements[t].Properties&TYPE_LIQUID)) || - (bmap[y/CELL][x/CELL]==WL_ALLOWPOWDER && !(elements[t].Properties&TYPE_PART)) || - (bmap[y/CELL][x/CELL]==WL_ALLOWGAS && !(elements[t].Properties&TYPE_GAS)) || //&& elements[t].Falldown!=0 && parts[i].type!=PT_FIRE && parts[i].type!=PT_SMKE && parts[i].type!=PT_CFLM) || - (bmap[y/CELL][x/CELL]==WL_ALLOWENERGY && !(elements[t].Properties&TYPE_ENERGY)) || - (bmap[y/CELL][x/CELL]==WL_EWALL && !emap[y/CELL][x/CELL])) && (t!=PT_STKM) && (t!=PT_STKM2) && (t!=PT_FIGH))) + // Kill a particle off screen + if (x=XRES-CELL || y>=YRES-CELL) + { + kill_part(i); + continue; + } + + // Kill a particle in a wall where it isn't supposed to go + if (bmap[y/CELL][x/CELL] && + (bmap[y/CELL][x/CELL]==WL_WALL || + bmap[y/CELL][x/CELL]==WL_WALLELEC || + bmap[y/CELL][x/CELL]==WL_ALLOWAIR || + (bmap[y/CELL][x/CELL]==WL_DESTROYALL) || + (bmap[y/CELL][x/CELL]==WL_ALLOWLIQUID && !(elements[t].Properties&TYPE_LIQUID)) || + (bmap[y/CELL][x/CELL]==WL_ALLOWPOWDER && !(elements[t].Properties&TYPE_PART)) || + (bmap[y/CELL][x/CELL]==WL_ALLOWGAS && !(elements[t].Properties&TYPE_GAS)) || //&& elements[t].Falldown!=0 && parts[i].type!=PT_FIRE && parts[i].type!=PT_SMKE && parts[i].type!=PT_CFLM) || + (bmap[y/CELL][x/CELL]==WL_ALLOWENERGY && !(elements[t].Properties&TYPE_ENERGY)) || + (bmap[y/CELL][x/CELL]==WL_EWALL && !emap[y/CELL][x/CELL])) && (t!=PT_STKM) && (t!=PT_STKM2) && (t!=PT_FIGH)) { kill_part(i); continue; @@ -3604,10 +3581,8 @@ void Simulation::UpdateParticles(int start, int end) if (nx||ny) { surround[j] = r = pmap[y+ny][x+nx]; j++; - if (!TYP(r)) - surround_space++;//there is empty space - if (TYP(r)!=t) - nt++;//there is nothing or a different particle + surround_space += (!TYP(r)); // count empty space + nt += (TYP(r)!=t); // count empty space and particles of different type } } @@ -3797,10 +3772,7 @@ void Simulation::UpdateParticles(int start, int end) { pt = (c_heat - platent[t])/c_Cm; - if (RNG::Ref().chance(1, 4)) - t = PT_SALT; - else - t = PT_WTRV; + t = RNG::Ref().chance(1, 4) ? PT_SALT : PT_WTRV; } else { @@ -3808,10 +3780,7 @@ void Simulation::UpdateParticles(int start, int end) s = 0; } #else - if (RNG::Ref().chance(1, 4)) - t = PT_SALT; - else - t = PT_WTRV; + t = RNG::Ref().chance(1, 4) ? PT_SALT : PT_WTRV; #endif } else if (t == PT_BRMT) @@ -3866,10 +3835,7 @@ void Simulation::UpdateParticles(int start, int end) #endif else if (t == PT_WTRV) { - if (pt < 273.0f) - t = PT_RIME; - else - t = PT_DSTW; + t = (pt < 273.0f) ? PT_RIME : PT_DSTW; } else if (t == PT_LAVA) { @@ -4472,10 +4438,8 @@ killed: // but no point trying this if particle is stuck in a block of identical particles dx = parts[i].vx - parts[i].vy*r; dy = parts[i].vy + parts[i].vx*r; - if (fabsf(dy)>fabsf(dx)) - mv = fabsf(dy); - else - mv = fabsf(dx); + + mv = std::max(fabsf(dx), fabsf(dy)); dx /= mv; dy /= mv; if (do_move(i, x, y, clear_xf+dx, clear_yf+dy)) @@ -4525,10 +4489,9 @@ killed: if (TYP(pmap[clear_y][j])!=t || (bmap[clear_y/CELL][j/CELL] && bmap[clear_y/CELL][j/CELL]!=WL_STREAM)) break; } - if (parts[i].vy>0) - r = 1; - else - r = -1; + + r = (parts[i].vy>0) ? 1 : -1; + if (s==1) for (j=ny+r; j>=0 && j=ny-rt && jfabsf(pGravX)) - mv = fabsf(pGravY); - else - mv = fabsf(pGravX); + mv = std::max(fabsf(pGravX), fabsf(pGravY)); if (mv<0.0001f) break; pGravX /= mv; pGravY /= mv; @@ -4618,10 +4578,7 @@ killed: // Calculate overall gravity direction GetGravityField(nx, ny, ptGrav, 1.0f, pGravX, pGravY); // Scale gravity vector so that the largest component is 1 pixel - if (fabsf(pGravY)>fabsf(pGravX)) - mv = fabsf(pGravY); - else - mv = fabsf(pGravX); + mv = std::max(fabsf(pGravX), fabsf(pGravY)); if (mv<0.0001f) break; pGravX /= mv; pGravY /= mv; @@ -4768,17 +4725,11 @@ void Simulation::RecalcFreeParticles(bool do_life_dec) } if (lastPartUnused == -1) { - if (parts_lastActiveIndex>=NPART-1) - pfree = -1; - else - pfree = parts_lastActiveIndex+1; + pfree = (parts_lastActiveIndex>=(NPART-1)) ? -1 : parts_lastActiveIndex+1; } else { - if (parts_lastActiveIndex>=NPART-1) - parts[lastPartUnused].life = -1; - else - parts[lastPartUnused].life = parts_lastActiveIndex+1; + parts[lastPartUnused].life = (parts_lastActiveIndex>=(NPART-1)) ? -1 : parts_lastActiveIndex+1; } parts_lastActiveIndex = lastPartUsed; if (elementRecount)