@@ -3759,8 +3759,7 @@ int flood_prop(int x, int y, size_t propoffset, void * propvalue, int proptype)
37593759int 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+
40224067int flood_water (int x , int y , int i , int originaly , int check )
40234068{
40244069 int x1 = 0 ,x2 = 0 ;
0 commit comments