From 186f8a1742a4fc4a5e1af0a566e6e3ffba22afac Mon Sep 17 00:00:00 2001 From: jacksonmj Date: Thu, 7 Nov 2013 21:36:36 +0000 Subject: [PATCH] No more red DEUT Prevent some integer overflows in DEUT graphics and interactions. --- src/simulation/elements/DEUT.cpp | 16 +++++++++++----- src/simulation/elements/NEUT.cpp | 4 ++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/simulation/elements/DEUT.cpp b/src/simulation/elements/DEUT.cpp index 8fb80bdf0..aaee8548d 100644 --- a/src/simulation/elements/DEUT.cpp +++ b/src/simulation/elements/DEUT.cpp @@ -68,7 +68,9 @@ int Element_DEUT::update(UPDATE_FUNC_ARGS) continue; if ((r&0xFF)==PT_DEUT&& !(rand()%3)) { - if ((parts[i].life + parts[r>>8].life + 1) <= maxlife) + // If neighbour life+1 fits in the free capacity for this particle, absorb neighbour + // Condition is written in this way so that large neighbour life values don't cause integer overflow + if (parts[r>>8].life <= maxlife - parts[i].life - 1) { parts[i].life += parts[r>>8].life + 1; sim->kill_part(r>>8); @@ -130,18 +132,22 @@ int Element_DEUT::graphics(GRAPHICS_FUNC_ARGS) if(cpart->life>=700) { *firea = 60; - *firer = *colr += cpart->life*1; - *fireg = *colg += cpart->life*2; - *fireb = *colb += cpart->life*3; + *firer = *colr += 255; + *fireg = *colg += 255; + *fireb = *colb += 255; *pixel_mode |= PMODE_GLOW | FIRE_ADD; } - else + else if(cpart->life>0) { *colr += cpart->life*1; *colg += cpart->life*2; *colb += cpart->life*3; *pixel_mode |= PMODE_BLUR; } + else + { + *pixel_mode |= PMODE_BLUR; + } return 0; } diff --git a/src/simulation/elements/NEUT.cpp b/src/simulation/elements/NEUT.cpp index d61466c05..098732f03 100644 --- a/src/simulation/elements/NEUT.cpp +++ b/src/simulation/elements/NEUT.cpp @@ -92,7 +92,7 @@ int Element_NEUT::update(UPDATE_FUNC_ARGS) case PT_DEUT: if ((pressureFactor+1+(parts[r>>8].life/100))>(rand()%1000)) { - DeutExplosion(sim, parts[r>>8].life, x+rx, y+ry, restrict_flt(parts[r>>8].temp + parts[r>>8].life*500, MIN_TEMP, MAX_TEMP), PT_NEUT); + DeutExplosion(sim, parts[r>>8].life, x+rx, y+ry, restrict_flt(parts[r>>8].temp + parts[r>>8].life*500.0f, MIN_TEMP, MAX_TEMP), PT_NEUT); sim->kill_part(r>>8); } break; @@ -104,7 +104,7 @@ int Element_NEUT::update(UPDATE_FUNC_ARGS) parts[r>>8].vx = 0.25f*parts[r>>8].vx + parts[i].vx; parts[r>>8].vy = 0.25f*parts[r>>8].vy + parts[i].vy; parts[r>>8].life --; - parts[r>>8].temp = restrict_flt(parts[r>>8].temp + parts[r>>8].life*17, MIN_TEMP, MAX_TEMP); + parts[r>>8].temp = restrict_flt(parts[r>>8].temp + parts[r>>8].life*17.0f, MIN_TEMP, MAX_TEMP); pv[y/CELL][x/CELL] += 6.0f * CFDS; }