From 9c575777833404ccf3dfb6f8cfa36bfbc8862c53 Mon Sep 17 00:00:00 2001 From: ob1kenewb Date: Sun, 4 May 2003 20:26:16 +0000 Subject: [PATCH] Prevent an infinit loop if there is not enough stone block on the map or if the map is too little --- src/field.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/field.c b/src/field.c index 8fe5746..b9f335e 100644 --- a/src/field.c +++ b/src/field.c @@ -1,4 +1,4 @@ -/* $Id: field.c,v 1.4 2003/05/04 19:23:00 ob1kenewb Exp $ */ +/* $Id: field.c,v 1.5 2003/05/04 20:26:16 ob1kenewb Exp $ */ /* field.c - procedures which are needed to control the field */ #include @@ -146,7 +146,7 @@ field_new (char *filename) d; FILE *fmap; float fkt; - + int nb_try; fmap=fopen(filename,"r"); /* if we can't open the given filename for any reason, reverting @@ -204,41 +204,57 @@ field_new (char *filename) bman.field[x][y+1].type = FT_nothing; } + nb_try=100; /* put the fire powerups in the field */ for (d = 0, x = 0, y = 0; d < GAME_SPECIAL_ITEMFIRE * fkt; d++) { while (bman.field[x][y].type != FT_stone || bman.field[x][y].special != FT_nothing) { x = ((float) rand() / (float) RAND_MAX) * (bman.fieldsize.x-1); y = ((float) rand() / (float) RAND_MAX) * (bman.fieldsize.y-1); + nb_try--; + if(nb_try<0) + break; } bman.field[x][y].special = FT_fire; x = y = 0; } + nb_try=100; /* put the bomb powerups in the field */ for (d = 0, x = 0, y = 0; d < GAME_SPECIAL_ITEMBOMB * fkt; d++) { while (bman.field[x][y].type != FT_stone || bman.field[x][y].special != FT_nothing) { x = ((float) rand() / (float) RAND_MAX) * (bman.fieldsize.x-1); y = ((float) rand() / (float) RAND_MAX) * (bman.fieldsize.y-1); + nb_try--; + if(nb_try<0) + break; } bman.field[x][y].special = FT_bomb; x = y = 0; } + nb_try=100; /* put the shoe powerup in the field */ for (d = 0, x = 0, y = 0; d < GAME_SPECIAL_ITEMSHOE * fkt; d++) { while (bman.field[x][y].type != FT_stone || bman.field[x][y].special != FT_nothing) { x = ((float) rand() / (float) RAND_MAX) * (bman.fieldsize.x-1); y = ((float) rand() / (float) RAND_MAX) * (bman.fieldsize.y-1); + nb_try--; + if(nb_try<0) + break; } bman.field[x][y].special = FT_shoe; x = y = 0; } + nb_try=100; /* put the death ?powerups? in the field */ for (d = 0, x = 0, y = 0; d < GAME_SPECIAL_ITEMDEATH * fkt; d++) { while (bman.field[x][y].type != FT_stone || bman.field[x][y].special != FT_nothing) { x = ((float) rand() / (float) RAND_MAX) * (bman.fieldsize.x-1); y = ((float) rand() / (float) RAND_MAX) * (bman.fieldsize.y-1); + nb_try--; + if(nb_try<0) + break; } bman.field[x][y].special = FT_death; x = y = 0;