Skip to content

Commit

Permalink
FloodWalls and FloodParts from tpt++ a06202c
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob1 committed Jul 12, 2013
1 parent 7d6c434 commit 95fca18
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 41 deletions.
3 changes: 2 additions & 1 deletion includes/powder.h
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,8 @@ void clear_area(int area_x, int area_y, int area_w, int area_h);

void create_box(int x1, int y1, int x2, int y2, int c, int flags);

int flood_parts(int x, int y, int c, int cm, int bm, int flags);
int FloodParts(int x, int y, int fullc, int cm, int flags);
int FloodWalls(int x, int y, int wall, int bm, int flags);

int flood_INST(int x, int y, int fullc, int cm);

Expand Down
8 changes: 5 additions & 3 deletions src/luaconsole.c
Original file line number Diff line number Diff line change
Expand Up @@ -2917,14 +2917,16 @@ int luatpt_floodfill(lua_State* l)
int y = luaL_optint(l,2,-1);
int c = luaL_optint(l,3,sl);
int cm = luaL_optint(l,4,-1);
int bm = luaL_optint(l,5,-1);
int flags = luaL_optint(l,6,get_brush_flags());
int flags = luaL_optint(l,5,get_brush_flags());
int ret;
if (x < 0 || x > XRES || y < 0 || y > YRES)
return luaL_error(l, "coordinates out of range (%d,%d)", x, y);
if (c < 0 || c >= PT_NUM && !ptypes[c].enabled)
return luaL_error(l, "Unrecognised element number '%d'", c);
ret = flood_parts(x, y, c, cm, bm, flags);
if (c >= UI_WALLSTART && c < UI_WALLSTART+UI_WALLCOUNT)
ret = FloodWalls(x, y, c, cm, flags);
else
ret = FloodParts(x, y, c, cm, flags);
lua_pushinteger(l, ret);
return 1;
}
Expand Down
11 changes: 8 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2867,7 +2867,7 @@ int main(int argc, char *argv[])
{
nfvx = (line_x-lx)*0.005f;
nfvy = (line_y-ly)*0.005f;
flood_parts(lx, ly, WL_FANHELPER, -1, WL_FAN, 0);
FloodWalls(lx, ly, WL_FANHELPER, WL_FAN, 0);
for (j=0; j<YRES/CELL; j++)
for (i=0; i<XRES/CELL; i++)
if (bmap[j][i] == WL_FANHELPER)
Expand Down Expand Up @@ -2953,8 +2953,13 @@ int main(int argc, char *argv[])
unsigned int col = (b != 4 && c != DECO_ERASE) ? decocolor : PIXRGB(0,0,0);
flood_prop(x, y, offsetof(particle,dcolour), &col, 0);
}
else if (c != WL_STREAM+100 && c != DECO_DRAW && c != DECO_ERASE)
flood_parts(x, y, c, -1, -1, get_brush_flags());
else if (c >= UI_WALLSTART && c < UI_WALLSTART+UI_WALLCOUNT)
{
if (c != WL_STREAM+100)
FloodWalls(x, y, c, -1, get_brush_flags());
}
else if (c != DECO_DRAW && c != DECO_ERASE)
FloodParts(x, y, c, -1, get_brush_flags());
lx = x;
ly = y;
lb = 0;
Expand Down
113 changes: 79 additions & 34 deletions src/powder.c
Original file line number Diff line number Diff line change
Expand Up @@ -3759,8 +3759,7 @@ int flood_prop(int x, int y, size_t propoffset, void * propvalue, int proptype)
int flood_INST(int x, int y, int fullc, int cm)
{
int c = fullc&0xFF;
int x1, x2, dy = (c<PT_NUM)?1:CELL;
int co = c;
int x1, x2;
int coord_stack_limit = XRES*YRES;
unsigned short (*coord_stack)[2];
int coord_stack_size = 0;
Expand Down Expand Up @@ -3905,8 +3904,7 @@ int flood_INST(int x, int y, int fullc, int cm)
return created_something;
}


int flood_parts(int x, int y, int fullc, int cm, int bm, int flags)
int FloodParts(int x, int y, int fullc, int cm, int flags)
{
int c = fullc&0xFF;
int x1, x2, dy = (c<PT_NUM)?1:CELL;
Expand All @@ -3915,43 +3913,31 @@ int flood_parts(int x, int y, int fullc, int cm, int bm, int flags)
int coord_stack_size = 0;
int created_something = 0;

if (fullc == SPC_PROP)
return flood_prop(x, y, prop_offset, prop_value, prop_format);

if (cm==-1)
{
//if initial flood point is out of bounds, do nothing
if (c != 0 && (x < CELL || x >= XRES-CELL || y < CELL || y >= YRES-CELL))
return 1;
if (c==0)
{
cm = pmap[y][x]&0xFF;
if (!cm)
cm = photons[y][x]&0xFF;
if (!cm)
{
c = fullc = WL_ERASE+100; //Try to erase walls if there is no particle in this spot
dy = 4;
}
if (!cm && bmap[y/CELL][x/CELL])
FloodWalls(x, y, WL_ERASE+100, -1, flags);
if ((flags&BRUSH_REPLACEMODE) && cm!=SLALT)
return 0;
}
else
cm = 0;
}
if (bm==-1)
{
if (c-UI_WALLSTART+UI_ACTUALSTART==WL_ERASE || c-UI_WALLSTART+UI_ACTUALSTART==WL_ERASEALL)
{
bm = bmap[y/CELL][x/CELL];
if (!bm)
return 0;
}
else
bm = 0;
}
if (IsWallBlocking(x, y, c))
return 1;

if ((!FloodFillPmapCheck(x, y, cm) || bmap[y/CELL][x/CELL] != bm) || ((flags&BRUSH_SPECIFIC_DELETE) && cm!=SLALT))
if (!FloodFillPmapCheck(x, y, cm) || ((flags&BRUSH_SPECIFIC_DELETE) && cm!=SLALT))
return 1;

coord_stack = (unsigned short(*)[2])malloc(sizeof(unsigned short)*2*coord_stack_limit);
coord_stack = (short unsigned int (*)[2])malloc(sizeof(unsigned short)*2*coord_stack_limit);
coord_stack[coord_stack_size][0] = x;
coord_stack[coord_stack_size][1] = y;
coord_stack_size++;
Expand All @@ -3963,18 +3949,18 @@ int flood_parts(int x, int y, int fullc, int cm, int bm, int flags)
y = coord_stack[coord_stack_size][1];
x1 = x2 = x;
// go left as far as possible
while (x1>=CELL)
while (c?x1>CELL:x1>0)
{
if (!FloodFillPmapCheck(x1-1, y, cm) || bmap[y/CELL][(x1-1)/CELL]!=bm)
if (!FloodFillPmapCheck(x1-1, y, cm) || (c != 0 && IsWallBlocking(x1-1, y, c)))
{
break;
}
x1--;
}
// go right as far as possible
while (x2<XRES-CELL)
while (c?x2<XRES-CELL-1:x2<XRES-1)
{
if (!FloodFillPmapCheck(x2+1, y, cm) || bmap[y/CELL][(x2+1)/CELL]!=bm)
if (!FloodFillPmapCheck(x2+1, y, cm) || (c != 0 && IsWallBlocking(x2+1, y, c)))
{
break;
}
Expand All @@ -3987,10 +3973,9 @@ int flood_parts(int x, int y, int fullc, int cm, int bm, int flags)
created_something = 1;
}

// add vertically adjacent pixels to stack
if (y>=CELL+dy)
if (c?y>=CELL+dy:y>=dy)
for (x=x1; x<=x2; x++)
if (FloodFillPmapCheck(x, y-dy, cm) && bmap[(y-dy)/CELL][x/CELL]==bm)
if (FloodFillPmapCheck(x, y-dy, cm) && (c == 0 || !IsWallBlocking(x, y-dy, c)))
{
coord_stack[coord_stack_size][0] = x;
coord_stack[coord_stack_size][1] = y-dy;
Expand All @@ -4001,9 +3986,10 @@ int flood_parts(int x, int y, int fullc, int cm, int bm, int flags)
return -1;
}
}
if (y<YRES-CELL-dy)

if (c?y<YRES-CELL-dy:y<YRES-dy)
for (x=x1; x<=x2; x++)
if (FloodFillPmapCheck(x, y+dy, cm) && bmap[(y+dy)/CELL][x/CELL]==bm)
if (FloodFillPmapCheck(x, y+dy, cm) && (c == 0 || !IsWallBlocking(x, y+dy, c)))
{
coord_stack[coord_stack_size][0] = x;
coord_stack[coord_stack_size][1] = y+dy;
Expand All @@ -4019,6 +4005,65 @@ int flood_parts(int x, int y, int fullc, int cm, int bm, int flags)
return created_something;
}

int FloodWalls(int x, int y, int wall, int bm, int flags)
{
int x1, x2, dy = CELL;
if (wall == SPC_PROP)
return flood_prop(x, y, prop_offset, prop_value, prop_format);
if (bm==-1)
{
if (wall-UI_WALLSTART+UI_ACTUALSTART==WL_ERASE || wall-UI_WALLSTART+UI_ACTUALSTART==WL_ERASEALL)
{
bm = bmap[y/CELL][x/CELL];
if (!bm)
return 0;
}
else
bm = 0;
}

if (bmap[y/CELL][x/CELL] != bm || ((flags&BRUSH_SPECIFIC_DELETE) && bm!=SLALT))
return 1;

// go left as far as possible
x1 = x2 = x;
while (x1>=CELL)
{
if (bmap[y/CELL][(x1-1)/CELL]!=bm)
{
break;
}
x1--;
}
while (x2<XRES-CELL)
{
if (bmap[y/CELL][(x2+1)/CELL]!=bm)
{
break;
}
x2++;
}

// fill span
for (x=x1; x<=x2; x++)
{
if (!create_parts(x, y, 0, 0, wall, flags, 1))
return 0;
}
// fill children
if (y>=CELL)
for (x=x1; x<=x2; x++)
if (bmap[(y-dy)/CELL][x/CELL]==bm)
if (!FloodWalls(x, y-dy, wall, bm, flags))
return 0;
if (y<YRES-CELL)
for (x=x1; x<=x2; x++)
if (bmap[(y+dy)/CELL][x/CELL]==bm)
if (!FloodWalls(x, y+dy, wall, bm, flags))
return 0;
return 1;
}

int flood_water(int x, int y, int i, int originaly, int check)
{
int x1 = 0,x2 = 0;
Expand Down

0 comments on commit 95fca18

Please sign in to comment.