Skip to content

Commit 95fca18

Browse files
committed
FloodWalls and FloodParts from tpt++ a06202c
1 parent 7d6c434 commit 95fca18

4 files changed

Lines changed: 94 additions & 41 deletions

File tree

includes/powder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,8 @@ void clear_area(int area_x, int area_y, int area_w, int area_h);
998998

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

1001-
int flood_parts(int x, int y, int c, int cm, int bm, int flags);
1001+
int FloodParts(int x, int y, int fullc, int cm, int flags);
1002+
int FloodWalls(int x, int y, int wall, int bm, int flags);
10021003

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

src/luaconsole.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2917,14 +2917,16 @@ int luatpt_floodfill(lua_State* l)
29172917
int y = luaL_optint(l,2,-1);
29182918
int c = luaL_optint(l,3,sl);
29192919
int cm = luaL_optint(l,4,-1);
2920-
int bm = luaL_optint(l,5,-1);
2921-
int flags = luaL_optint(l,6,get_brush_flags());
2920+
int flags = luaL_optint(l,5,get_brush_flags());
29222921
int ret;
29232922
if (x < 0 || x > XRES || y < 0 || y > YRES)
29242923
return luaL_error(l, "coordinates out of range (%d,%d)", x, y);
29252924
if (c < 0 || c >= PT_NUM && !ptypes[c].enabled)
29262925
return luaL_error(l, "Unrecognised element number '%d'", c);
2927-
ret = flood_parts(x, y, c, cm, bm, flags);
2926+
if (c >= UI_WALLSTART && c < UI_WALLSTART+UI_WALLCOUNT)
2927+
ret = FloodWalls(x, y, c, cm, flags);
2928+
else
2929+
ret = FloodParts(x, y, c, cm, flags);
29282930
lua_pushinteger(l, ret);
29292931
return 1;
29302932
}

src/main.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2867,7 +2867,7 @@ int main(int argc, char *argv[])
28672867
{
28682868
nfvx = (line_x-lx)*0.005f;
28692869
nfvy = (line_y-ly)*0.005f;
2870-
flood_parts(lx, ly, WL_FANHELPER, -1, WL_FAN, 0);
2870+
FloodWalls(lx, ly, WL_FANHELPER, WL_FAN, 0);
28712871
for (j=0; j<YRES/CELL; j++)
28722872
for (i=0; i<XRES/CELL; i++)
28732873
if (bmap[j][i] == WL_FANHELPER)
@@ -2953,8 +2953,13 @@ int main(int argc, char *argv[])
29532953
unsigned int col = (b != 4 && c != DECO_ERASE) ? decocolor : PIXRGB(0,0,0);
29542954
flood_prop(x, y, offsetof(particle,dcolour), &col, 0);
29552955
}
2956-
else if (c != WL_STREAM+100 && c != DECO_DRAW && c != DECO_ERASE)
2957-
flood_parts(x, y, c, -1, -1, get_brush_flags());
2956+
else if (c >= UI_WALLSTART && c < UI_WALLSTART+UI_WALLCOUNT)
2957+
{
2958+
if (c != WL_STREAM+100)
2959+
FloodWalls(x, y, c, -1, get_brush_flags());
2960+
}
2961+
else if (c != DECO_DRAW && c != DECO_ERASE)
2962+
FloodParts(x, y, c, -1, get_brush_flags());
29582963
lx = x;
29592964
ly = y;
29602965
lb = 0;

src/powder.c

Lines changed: 79 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3759,8 +3759,7 @@ int flood_prop(int x, int y, size_t propoffset, void * propvalue, int proptype)
37593759
int flood_INST(int x, int y, int fullc, int cm)
37603760
{
37613761
int c = fullc&0xFF;
3762-
int x1, x2, dy = (c<PT_NUM)?1:CELL;
3763-
int co = c;
3762+
int x1, x2;
37643763
int coord_stack_limit = XRES*YRES;
37653764
unsigned short (*coord_stack)[2];
37663765
int coord_stack_size = 0;
@@ -3905,8 +3904,7 @@ int flood_INST(int x, int y, int fullc, int cm)
39053904
return created_something;
39063905
}
39073906

3908-
3909-
int flood_parts(int x, int y, int fullc, int cm, int bm, int flags)
3907+
int FloodParts(int x, int y, int fullc, int cm, int flags)
39103908
{
39113909
int c = fullc&0xFF;
39123910
int x1, x2, dy = (c<PT_NUM)?1:CELL;
@@ -3915,43 +3913,31 @@ int flood_parts(int x, int y, int fullc, int cm, int bm, int flags)
39153913
int coord_stack_size = 0;
39163914
int created_something = 0;
39173915

3918-
if (fullc == SPC_PROP)
3919-
return flood_prop(x, y, prop_offset, prop_value, prop_format);
3920-
39213916
if (cm==-1)
39223917
{
3918+
//if initial flood point is out of bounds, do nothing
3919+
if (c != 0 && (x < CELL || x >= XRES-CELL || y < CELL || y >= YRES-CELL))
3920+
return 1;
39233921
if (c==0)
39243922
{
39253923
cm = pmap[y][x]&0xFF;
39263924
if (!cm)
39273925
cm = photons[y][x]&0xFF;
3928-
if (!cm)
3929-
{
3930-
c = fullc = WL_ERASE+100; //Try to erase walls if there is no particle in this spot
3931-
dy = 4;
3932-
}
3926+
if (!cm && bmap[y/CELL][x/CELL])
3927+
FloodWalls(x, y, WL_ERASE+100, -1, flags);
39333928
if ((flags&BRUSH_REPLACEMODE) && cm!=SLALT)
39343929
return 0;
39353930
}
39363931
else
39373932
cm = 0;
39383933
}
3939-
if (bm==-1)
3940-
{
3941-
if (c-UI_WALLSTART+UI_ACTUALSTART==WL_ERASE || c-UI_WALLSTART+UI_ACTUALSTART==WL_ERASEALL)
3942-
{
3943-
bm = bmap[y/CELL][x/CELL];
3944-
if (!bm)
3945-
return 0;
3946-
}
3947-
else
3948-
bm = 0;
3949-
}
3934+
if (IsWallBlocking(x, y, c))
3935+
return 1;
39503936

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

3954-
coord_stack = (unsigned short(*)[2])malloc(sizeof(unsigned short)*2*coord_stack_limit);
3940+
coord_stack = (short unsigned int (*)[2])malloc(sizeof(unsigned short)*2*coord_stack_limit);
39553941
coord_stack[coord_stack_size][0] = x;
39563942
coord_stack[coord_stack_size][1] = y;
39573943
coord_stack_size++;
@@ -3963,18 +3949,18 @@ int flood_parts(int x, int y, int fullc, int cm, int bm, int flags)
39633949
y = coord_stack[coord_stack_size][1];
39643950
x1 = x2 = x;
39653951
// go left as far as possible
3966-
while (x1>=CELL)
3952+
while (c?x1>CELL:x1>0)
39673953
{
3968-
if (!FloodFillPmapCheck(x1-1, y, cm) || bmap[y/CELL][(x1-1)/CELL]!=bm)
3954+
if (!FloodFillPmapCheck(x1-1, y, cm) || (c != 0 && IsWallBlocking(x1-1, y, c)))
39693955
{
39703956
break;
39713957
}
39723958
x1--;
39733959
}
39743960
// go right as far as possible
3975-
while (x2<XRES-CELL)
3961+
while (c?x2<XRES-CELL-1:x2<XRES-1)
39763962
{
3977-
if (!FloodFillPmapCheck(x2+1, y, cm) || bmap[y/CELL][(x2+1)/CELL]!=bm)
3963+
if (!FloodFillPmapCheck(x2+1, y, cm) || (c != 0 && IsWallBlocking(x2+1, y, c)))
39783964
{
39793965
break;
39803966
}
@@ -3987,10 +3973,9 @@ int flood_parts(int x, int y, int fullc, int cm, int bm, int flags)
39873973
created_something = 1;
39883974
}
39893975

3990-
// add vertically adjacent pixels to stack
3991-
if (y>=CELL+dy)
3976+
if (c?y>=CELL+dy:y>=dy)
39923977
for (x=x1; x<=x2; x++)
3993-
if (FloodFillPmapCheck(x, y-dy, cm) && bmap[(y-dy)/CELL][x/CELL]==bm)
3978+
if (FloodFillPmapCheck(x, y-dy, cm) && (c == 0 || !IsWallBlocking(x, y-dy, c)))
39943979
{
39953980
coord_stack[coord_stack_size][0] = x;
39963981
coord_stack[coord_stack_size][1] = y-dy;
@@ -4001,9 +3986,10 @@ int flood_parts(int x, int y, int fullc, int cm, int bm, int flags)
40013986
return -1;
40023987
}
40033988
}
4004-
if (y<YRES-CELL-dy)
3989+
3990+
if (c?y<YRES-CELL-dy:y<YRES-dy)
40053991
for (x=x1; x<=x2; x++)
4006-
if (FloodFillPmapCheck(x, y+dy, cm) && bmap[(y+dy)/CELL][x/CELL]==bm)
3992+
if (FloodFillPmapCheck(x, y+dy, cm) && (c == 0 || !IsWallBlocking(x, y+dy, c)))
40073993
{
40083994
coord_stack[coord_stack_size][0] = x;
40093995
coord_stack[coord_stack_size][1] = y+dy;
@@ -4019,6 +4005,65 @@ int flood_parts(int x, int y, int fullc, int cm, int bm, int flags)
40194005
return created_something;
40204006
}
40214007

4008+
int FloodWalls(int x, int y, int wall, int bm, int flags)
4009+
{
4010+
int x1, x2, dy = CELL;
4011+
if (wall == SPC_PROP)
4012+
return flood_prop(x, y, prop_offset, prop_value, prop_format);
4013+
if (bm==-1)
4014+
{
4015+
if (wall-UI_WALLSTART+UI_ACTUALSTART==WL_ERASE || wall-UI_WALLSTART+UI_ACTUALSTART==WL_ERASEALL)
4016+
{
4017+
bm = bmap[y/CELL][x/CELL];
4018+
if (!bm)
4019+
return 0;
4020+
}
4021+
else
4022+
bm = 0;
4023+
}
4024+
4025+
if (bmap[y/CELL][x/CELL] != bm || ((flags&BRUSH_SPECIFIC_DELETE) && bm!=SLALT))
4026+
return 1;
4027+
4028+
// go left as far as possible
4029+
x1 = x2 = x;
4030+
while (x1>=CELL)
4031+
{
4032+
if (bmap[y/CELL][(x1-1)/CELL]!=bm)
4033+
{
4034+
break;
4035+
}
4036+
x1--;
4037+
}
4038+
while (x2<XRES-CELL)
4039+
{
4040+
if (bmap[y/CELL][(x2+1)/CELL]!=bm)
4041+
{
4042+
break;
4043+
}
4044+
x2++;
4045+
}
4046+
4047+
// fill span
4048+
for (x=x1; x<=x2; x++)
4049+
{
4050+
if (!create_parts(x, y, 0, 0, wall, flags, 1))
4051+
return 0;
4052+
}
4053+
// fill children
4054+
if (y>=CELL)
4055+
for (x=x1; x<=x2; x++)
4056+
if (bmap[(y-dy)/CELL][x/CELL]==bm)
4057+
if (!FloodWalls(x, y-dy, wall, bm, flags))
4058+
return 0;
4059+
if (y<YRES-CELL)
4060+
for (x=x1; x<=x2; x++)
4061+
if (bmap[(y+dy)/CELL][x/CELL]==bm)
4062+
if (!FloodWalls(x, y+dy, wall, bm, flags))
4063+
return 0;
4064+
return 1;
4065+
}
4066+
40224067
int flood_water(int x, int y, int i, int originaly, int check)
40234068
{
40244069
int x1 = 0,x2 = 0;

0 commit comments

Comments
 (0)