Skip to content

Commit

Permalink
Rewrite flood_water to use CoordStack and fix stack overflow, fixes #646
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob1 committed Aug 11, 2019
1 parent d61db0f commit 2e154e5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 53 deletions.
2 changes: 1 addition & 1 deletion src/simulation/ElementDefs.h
Expand Up @@ -34,7 +34,7 @@

#define FLAG_STAGNANT 0x1
#define FLAG_SKIPMOVE 0x2 // skip movement for one frame, only implemented for PHOT
#define FLAG_WATEREQUAL 0x4 //if a liquid was already checked during equalization
//#define FLAG_WATEREQUAL 0x4 //if a liquid was already checked during equalization
#define FLAG_MOVABLE 0x8 // compatibility with old saves (moving SPNG), only applies to SPNG
#define FLAG_PHOTDECO 0x8 // compatibility with old saves (decorated photons), only applies to PHOT. Having the same value as FLAG_MOVABLE is fine because they apply to different elements, and this saves space for future flags,

Expand Down
109 changes: 58 additions & 51 deletions src/simulation/Simulation.cpp
Expand Up @@ -866,63 +866,70 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
return created_something;
}

int Simulation::flood_water(int x, int y, int i, int originaly, int check)
bool Simulation::flood_water(int x, int y, int i)
{
int x1 = 0,x2 = 0;
// go left as far as possible
x1 = x2 = x;
if (!pmap[y][x])
return 1;
int x1, x2, originalY = y;
int r = pmap[y][x];
if (!r)
return false;

while (x1>=CELL)
{
if ((elements[TYP(pmap[y][x1-1])].Falldown) != 2)
{
break;
}
x1--;
}
while (x2<XRES-CELL)
// Bitmap for checking where we've already looked
auto bitmapPtr = std::unique_ptr<char[]>(new char[XRES * YRES]);
char *bitmap = bitmapPtr.get();
std::fill(&bitmap[0], &bitmap[XRES * YRES], 0);

try
{
if ((elements[TYP(pmap[y][x2+1])].Falldown) != 2)
CoordStack cs;
cs.push(x, y);
do
{
break;
}
x2++;
}
cs.pop(x, y);
x1 = x2 = x;
while (x1 >= CELL)
{
if (elements[TYP(pmap[y][x1 - 1])].Falldown != 2 || bitmap[(y * XRES) + x1 - 1])
break;
x1--;
}
while (x2 < XRES-CELL)
{
if (elements[TYP(pmap[y][x2 + 1])].Falldown != 2 || bitmap[(y * XRES) + x1 - 1])
break;
x2++;
}
for (int x = x1; x <= x2; x++)
{
// Check above, maybe around other sides too?
if (((y - 1) > originalY) && !pmap[y - 1][x] && eval_move(parts[i].type, x, y - 1, nullptr))
{
int oldx = (int)(parts[i].x + 0.5f);
int oldy = (int)(parts[i].y + 0.5f);
pmap[y - 1][x] = pmap[oldy][oldx];
pmap[oldy][oldx] = 0;
parts[i].x = x;
parts[i].y = y - 1;
return true;
}

// fill span
for (x=x1; x<=x2; x++)
bitmap[(y * XRES) + x] = 1;
}
if (y >= CELL + 1)
for (int x = x1; x <= x2; x++)
if (elements[TYP(pmap[y - 1][x])].Falldown == 2 && !bitmap[((y - 1) * XRES) + x])
cs.push(x, y - 1);
if (y < YRES - CELL - 1)
for (int x = x1; x <= x2; x++)
if (elements[TYP(pmap[y + 1][x])].Falldown == 2 && !bitmap[((y + 1) * XRES) + x])
cs.push(x, y + 1);
} while (cs.getSize() > 0);
}
catch (std::exception &e)
{
if (check)
parts[ID(pmap[y][x])].flags &= ~FLAG_WATEREQUAL;//flag it as checked (different from the original particle's checked flag)
else
parts[ID(pmap[y][x])].flags |= FLAG_WATEREQUAL;
//check above, maybe around other sides too?
if ( ((y-1) > originaly) && !pmap[y-1][x] && eval_move(parts[i].type, x, y-1, NULL))
{
int oldx = (int)(parts[i].x + 0.5f);
int oldy = (int)(parts[i].y + 0.5f);
pmap[y-1][x] = pmap[oldy][oldx];
pmap[oldy][oldx] = 0;
parts[i].x = x;
parts[i].y = y-1;
return 0;
}
std::cerr << e.what() << std::endl;
return false;
}
// fill children

if (y>=CELL+1)
for (x=x1; x<=x2; x++)
if ((elements[TYP(pmap[y-1][x])].Falldown)==2 && (parts[ID(pmap[y-1][x])].flags & FLAG_WATEREQUAL) == check)
if (!flood_water(x, y-1, i, originaly, check))
return 0;
if (y<YRES-CELL-1)
for (x=x1; x<=x2; x++)
if ((elements[TYP(pmap[y+1][x])].Falldown)==2 && (parts[ID(pmap[y+1][x])].flags & FLAG_WATEREQUAL) == check)
if (!flood_water(x, y+1, i, originaly, check))
return 0;
return 1;
return false;
}

void Simulation::SetEdgeMode(int newEdgeMode)
Expand Down Expand Up @@ -4651,7 +4658,7 @@ void Simulation::UpdateParticles(int start, int end)
// Checking stagnant is cool, but then it doesn't update when you change it later.
if (water_equal_test && elements[t].Falldown == 2 && RNG::Ref().chance(1, 200))
{
if (!flood_water(x,y,i,y, parts[i].flags&FLAG_WATEREQUAL))
if (!flood_water(x, y, i))
goto movedone;
}
// liquids and powders
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/Simulation.h
Expand Up @@ -142,7 +142,7 @@ class Simulation
void kill_part(int i);
bool FloodFillPmapCheck(int x, int y, int type);
int flood_prop(int x, int y, size_t propoffset, PropertyValue propvalue, StructProperty::PropertyType proptype);
int flood_water(int x, int y, int i, int originaly, int check);
bool flood_water(int x, int y, int i);
int FloodINST(int x, int y, int fullc, int cm);
void detach(int i);
bool part_change_type(int i, int x, int y, int t);
Expand Down

0 comments on commit 2e154e5

Please sign in to comment.