TPT: Fix infinite loop when flood_INST finds some INST with a non zero lif... 9ee316c3e9

This commit is contained in:
Simon Robertshaw 2012-07-25 18:13:09 +01:00
parent 9edf65fb3f
commit aa156fb933

View File

@ -369,7 +369,7 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
int x1, x2, dy = (c<PT_NUM)?1:CELL; int x1, x2, dy = (c<PT_NUM)?1:CELL;
int co = c; int co = c;
int coord_stack_limit = XRES*YRES; int coord_stack_limit = XRES*YRES;
unsigned short (*coord_stack)[2] = (short unsigned int (*)[2])malloc(sizeof(unsigned short)*2*coord_stack_limit); unsigned short (*coord_stack)[2];
int coord_stack_size = 0; int coord_stack_size = 0;
int created_something = 0; int created_something = 0;
@ -388,9 +388,10 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
cm = 0; cm = 0;
} }
if ((pmap[y][x]&0xFF)!=cm) if ((pmap[y][x]&0xFF)!=cm || parts[pmap[y][x]>>8].life!=0)
return 1; return 1;
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][0] = x;
coord_stack[coord_stack_size][1] = y; coord_stack[coord_stack_size][1] = y;
coord_stack_size++; coord_stack_size++;
@ -404,7 +405,7 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
// go left as far as possible // go left as far as possible
while (x1>=CELL) while (x1>=CELL)
{ {
if ((pmap[y][x1-1]&0xFF)!=cm) if ((pmap[y][x1-1]&0xFF)!=cm || parts[pmap[y][x1-1]>>8].life!=0)
{ {
break; break;
} }
@ -413,7 +414,7 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
// go right as far as possible // go right as far as possible
while (x2<XRES-CELL) while (x2<XRES-CELL)
{ {
if ((pmap[y][x2+1]&0xFF)!=cm) if ((pmap[y][x2+1]&0xFF)!=cm || parts[pmap[y][x2+1]>>8].life!=0)
{ {
break; break;
} }
@ -433,20 +434,23 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
!PMAP_CMP_CONDUCTIVE(pmap[y-2][x1-1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y-2][x1], cm) && !PMAP_CMP_CONDUCTIVE(pmap[y-2][x1+1], cm)) !PMAP_CMP_CONDUCTIVE(pmap[y-2][x1-1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y-2][x1], cm) && !PMAP_CMP_CONDUCTIVE(pmap[y-2][x1+1], cm))
{ {
// travelling vertically up, skipping a horizontal line // travelling vertically up, skipping a horizontal line
if ((pmap[y-2][x1]&0xFF)==cm) if ((pmap[y-2][x1]&0xFF)==cm && !parts[pmap[y-2][x1]>>8].life)
{ {
coord_stack[coord_stack_size][0] = x1; coord_stack[coord_stack_size][0] = x1;
coord_stack[coord_stack_size][1] = y-2; coord_stack[coord_stack_size][1] = y-2;
coord_stack_size++; coord_stack_size++;
if (coord_stack_size>=coord_stack_limit) if (coord_stack_size>=coord_stack_limit)
{
free(coord_stack);
return -1; return -1;
}
} }
} }
else if (y>=CELL+1) else if (y>=CELL+1)
{ {
for (x=x1; x<=x2; x++) for (x=x1; x<=x2; x++)
{ {
if ((pmap[y-1][x]&0xFF)==cm) if ((pmap[y-1][x]&0xFF)==cm && !parts[pmap[y-1][x]>>8].life)
{ {
if (x==x1 || x==x2 || y>=YRES-CELL-1 || !PMAP_CMP_CONDUCTIVE(pmap[y+1][x], cm)) if (x==x1 || x==x2 || y>=YRES-CELL-1 || !PMAP_CMP_CONDUCTIVE(pmap[y+1][x], cm))
{ {
@ -455,7 +459,10 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
coord_stack[coord_stack_size][1] = y-1; coord_stack[coord_stack_size][1] = y-1;
coord_stack_size++; coord_stack_size++;
if (coord_stack_size>=coord_stack_limit) if (coord_stack_size>=coord_stack_limit)
{
free(coord_stack);
return -1; return -1;
}
} }
} }
} }
@ -466,20 +473,23 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
!PMAP_CMP_CONDUCTIVE(pmap[y+2][x1-1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y+2][x1], cm) && !PMAP_CMP_CONDUCTIVE(pmap[y+2][x1+1], cm)) !PMAP_CMP_CONDUCTIVE(pmap[y+2][x1-1], cm) && PMAP_CMP_CONDUCTIVE(pmap[y+2][x1], cm) && !PMAP_CMP_CONDUCTIVE(pmap[y+2][x1+1], cm))
{ {
// travelling vertically down, skipping a horizontal line // travelling vertically down, skipping a horizontal line
if ((pmap[y+2][x1]&0xFF)==cm) if ((pmap[y+2][x1]&0xFF)==cm && !parts[pmap[y+2][x1]>>8].life)
{ {
coord_stack[coord_stack_size][0] = x1; coord_stack[coord_stack_size][0] = x1;
coord_stack[coord_stack_size][1] = y+2; coord_stack[coord_stack_size][1] = y+2;
coord_stack_size++; coord_stack_size++;
if (coord_stack_size>=coord_stack_limit) if (coord_stack_size>=coord_stack_limit)
{
free(coord_stack);
return -1; return -1;
}
} }
} }
else if (y<YRES-CELL-1) else if (y<YRES-CELL-1)
{ {
for (x=x1; x<=x2; x++) for (x=x1; x<=x2; x++)
{ {
if ((pmap[y+1][x]&0xFF)==cm) if ((pmap[y+1][x]&0xFF)==cm && !parts[pmap[y+1][x]>>8].life)
{ {
if (x==x1 || x==x2 || y<0 || !PMAP_CMP_CONDUCTIVE(pmap[y-1][x], cm)) if (x==x1 || x==x2 || y<0 || !PMAP_CMP_CONDUCTIVE(pmap[y-1][x], cm))
{ {
@ -488,7 +498,10 @@ int Simulation::FloodINST(int x, int y, int fullc, int cm)
coord_stack[coord_stack_size][1] = y+1; coord_stack[coord_stack_size][1] = y+1;
coord_stack_size++; coord_stack_size++;
if (coord_stack_size>=coord_stack_limit) if (coord_stack_size>=coord_stack_limit)
{
free(coord_stack);
return -1; return -1;
}
} }
} }
@ -506,7 +519,7 @@ int Simulation::FloodParts(int x, int y, int fullc, int cm, int bm, int flags)
int x1, x2, dy = (c<PT_NUM)?1:CELL; int x1, x2, dy = (c<PT_NUM)?1:CELL;
int co = c; int co = c;
int coord_stack_limit = XRES*YRES; int coord_stack_limit = XRES*YRES;
unsigned short (*coord_stack)[2] = (short unsigned int (*)[2])malloc(sizeof(unsigned short)*2*coord_stack_limit); unsigned short (*coord_stack)[2];
int coord_stack_size = 0; int coord_stack_size = 0;
int created_something = 0; int created_something = 0;
@ -540,6 +553,7 @@ int Simulation::FloodParts(int x, int y, int fullc, int cm, int bm, int flags)
if (((pmap[y][x]&0xFF)!=cm || bmap[y/CELL][x/CELL]!=bm )) if (((pmap[y][x]&0xFF)!=cm || bmap[y/CELL][x/CELL]!=bm ))
return 1; return 1;
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][0] = x;
coord_stack[coord_stack_size][1] = y; coord_stack[coord_stack_size][1] = y;
coord_stack_size++; coord_stack_size++;
@ -583,7 +597,10 @@ int Simulation::FloodParts(int x, int y, int fullc, int cm, int bm, int flags)
coord_stack[coord_stack_size][1] = y-dy; coord_stack[coord_stack_size][1] = y-dy;
coord_stack_size++; coord_stack_size++;
if (coord_stack_size>=coord_stack_limit) if (coord_stack_size>=coord_stack_limit)
{
free(coord_stack);
return -1; return -1;
}
} }
if (y<YRES-CELL-dy) if (y<YRES-CELL-dy)
@ -594,7 +611,10 @@ int Simulation::FloodParts(int x, int y, int fullc, int cm, int bm, int flags)
coord_stack[coord_stack_size][1] = y+dy; coord_stack[coord_stack_size][1] = y+dy;
coord_stack_size++; coord_stack_size++;
if (coord_stack_size>=coord_stack_limit) if (coord_stack_size>=coord_stack_limit)
{
free(coord_stack);
return -1; return -1;
}
} }
} while (coord_stack_size>0); } while (coord_stack_size>0);
free(coord_stack); free(coord_stack);