just a little cleanup

origin
stpohle 23 years ago
parent 271d996160
commit 9178c7114a

@ -1,4 +1,4 @@
/* $Id: field.c,v 1.6 2003/05/04 21:44:14 ob1kenewb Exp $ */ /* $Id: field.c,v 1.7 2003/05/05 00:22:21 stpohle Exp $ */
/* field.c - procedures which are needed to control the field */ /* field.c - procedures which are needed to control the field */
#include <stdlib.h> #include <stdlib.h>
@ -13,7 +13,7 @@ draw_stone (int x, int y)
_field *stone = &bman.field[x][y]; _field *stone = &bman.field[x][y];
SDL_Rect dest, SDL_Rect dest,
src; src;
SDL_Surface *srcimg; SDL_Surface *srcimg;
int i, int i,
d; d;
@ -23,34 +23,34 @@ draw_stone (int x, int y)
dest.x = x * gfx.block.x + gfx.offset.x; dest.x = x * gfx.block.x + gfx.offset.x;
dest.y = y * gfx.block.y + gfx.offset.y; dest.y = y * gfx.block.y + gfx.offset.y;
src.x = 0; src.x = 0;
if (stone->frame == 0 || stone->type != FT_stone) { if (stone->frame == 0 || stone->type != FT_stone) {
srcimg = gfx.field[stone->type].image; srcimg = gfx.field[stone->type].image;
src.y = 0; src.y = 0;
} }
else { else {
if (stone->frameto == 0) { if (stone->frameto == 0) {
if (stone->frame < gfx.field[stone->type].frames) { if (stone->frame < gfx.field[stone->type].frames) {
stone->frame++; stone->frame++;
stone->frameto = ANI_STONETIMEOUT; stone->frameto = ANI_STONETIMEOUT;
} }
} }
if (stone->frameto > 0) if (stone->frameto > 0)
stone->frameto--; stone->frameto--;
if (stone->frame < gfx.field[stone->type].frames) { if (stone->frame < gfx.field[stone->type].frames) {
src.y = stone->frame * gfx.block.y; src.y = stone->frame * gfx.block.y;
srcimg = gfx.field[stone->type].image; srcimg = gfx.field[stone->type].image;
} }
else { else {
src.y = 0; src.y = 0;
srcimg = gfx.field[FT_nothing].image; srcimg = gfx.field[FT_nothing].image;
} }
} }
if (stone->frame > 0) if (stone->frame > 0)
SDL_BlitSurface (gfx.field[FT_nothing].image, NULL, gfx.screen, &dest); SDL_BlitSurface (gfx.field[FT_nothing].image, NULL, gfx.screen, &dest);
SDL_BlitSurface (srcimg, &src, gfx.screen, &dest); SDL_BlitSurface (srcimg, &src, gfx.screen, &dest);
@ -85,56 +85,51 @@ draw_field ()
an espace is nothing ' ' an espace is nothing ' '
% are commentary at the beginning of the map */ % are commentary at the beginning of the map */
void field_load(FILE *map) void
field_load (FILE * map)
{ {
size_t length; size_t length;
char *currentline; char *currentline;
char tmp[MAX_FIELDSIZE_X]; char tmp[MAX_FIELDSIZE_X];
int sizex=0; int sizex = 0;
int sizey=0; int sizey = 0;
int i; int i;
int d; int d;
while((currentline=fgets(tmp,MAX_FIELDSIZE_X,map))) while ((currentline = fgets (tmp, MAX_FIELDSIZE_X, map))) {
/* fgetln is only implemented on *BSD */ length = strlen (currentline);
/* while((currentline=fgetln(map,&length))) */ if (currentline[0] == '%')
{ continue;
length=strlen(currentline); /* now each line correspond to the field */
if(currentline[0]=='%') else {
continue; for (i = 0; i < length; i++) {
/* now each line correspond to the field */ switch (currentline[i]) {
else case '#':
{ bman.field[i][sizey].type = FT_block;
for(i=0;i<length;i++) break;
{ case '@':
switch(currentline[i]) bman.field[i][sizey].type = FT_stone;
{ break;
case '#': case ' ':
bman.field[i][sizey].type=FT_block; bman.field[i][sizey].type = FT_nothing;
break; default:
case '@': break;
bman.field[i][sizey].type=FT_stone; }
break; for (d = 0; d < 4; d++)
case ' ': bman.field[i][sizey].ex[d] = 0;
bman.field[i][sizey].type=FT_nothing; bman.field[i][sizey].ex_nr = -1;
default: bman.field[i][sizey].frame = 0;
break; bman.field[i][sizey].frameto = 0;
} bman.field[i][sizey].special = FT_nothing;
for (d = 0; d < 4; d++) }
bman.field[i][sizey].ex[d] = 0; sizey++;
bman.field[i][sizey].ex_nr = -1; if (sizex < length)
bman.field[i][sizey].frame = 0; sizex = length;
bman.field[i][sizey].frameto = 0; }
bman.field[i][sizey].special = FT_nothing; }
}
sizey++; bman.fieldsize.x = sizex - 1;
if(sizex<length) bman.fieldsize.y = sizey;
sizex=length;
}
}
bman.fieldsize.x=sizex-1;
bman.fieldsize.y=sizey;
} }
@ -147,116 +142,114 @@ field_new (char *filename)
FILE *fmap; FILE *fmap;
float fkt; float fkt;
int nb_try; int nb_try;
fmap=fopen(filename,"r"); fmap = fopen (filename, "r");
/* if we can't open the given filename for any reason, reverting /* if we can't open the given filename for any reason, reverting
to default value else, load the file*/ to default value else, load the file */
if(fmap) if (fmap)
field_load(fmap); field_load (fmap);
// fkt = ((float)(bman.fieldsize.x * bman.fieldsize.y))/(25.0 * 17.0); fkt = ((float) (bman.fieldsize.x * bman.fieldsize.y)) / (bman.fieldsize.x * bman.fieldsize.y);
fkt = ((float)(bman.fieldsize.x * bman.fieldsize.y))/(bman.fieldsize.x * bman.fieldsize.y);
// Clean the field // // Clean the field //
if(fmap==NULL) if (fmap == NULL) {
{ for (x = 0; x < bman.fieldsize.x; x++)
for (x = 0; x < bman.fieldsize.x; x++) for (y = 0; y < bman.fieldsize.y; y++) {
for (y = 0; y < bman.fieldsize.y; y++) { if ((y == 0) || (y == bman.fieldsize.y - 1))
if ((y == 0) || (y == bman.fieldsize.y - 1)) bman.field[x][y].type = FT_block;
bman.field[x][y].type = FT_block; else if ((x == 0) || (x == bman.fieldsize.x - 1))
else if ((x == 0) || (x == bman.fieldsize.x - 1)) bman.field[x][y].type = FT_block;
bman.field[x][y].type = FT_block; else if (((x & 1) == 0) && ((y & 1) == 0))
else if (((x & 1) == 0) && ((y & 1) == 0)) bman.field[x][y].type = FT_block;
bman.field[x][y].type = FT_block; else {
else { // create random field
// create random field if ((s_random (256) & 3) == 0)
if ((s_random (256) & 3) == 0) bman.field[x][y].type = FT_nothing;
bman.field[x][y].type = FT_nothing; else
else bman.field[x][y].type = FT_stone;
bman.field[x][y].type = FT_stone; }
for (d = 0; d < 4; d++)
bman.field[x][y].ex[d] = 0;
bman.field[x][y].ex_nr = -1;
bman.field[x][y].frame = 0;
bman.field[x][y].frameto = 0;
bman.field[x][y].special = FT_nothing;
} }
}
/* get some free space for the playerstart position */
for (d = 0; d < MAX_PLAYERS; d++)
if ((PS_IS_alife (bman.players[d].state)) != 0) {
x = bman.players[d].pos.x >> 8;
y = bman.players[d].pos.y >> 8;
bman.field[x][y].type = FT_nothing;
if (x > 1)
bman.field[x - 1][y].type = FT_nothing;
if (x < bman.fieldsize.x - 2)
bman.field[x + 1][y].type = FT_nothing;
if (y > 1)
bman.field[x][y - 1].type = FT_nothing;
if (y < bman.fieldsize.y - 2)
bman.field[x][y + 1].type = FT_nothing;
}
for (d = 0; d < 4; d++) nb_try = 100; // to prevent infinite loops (maybe there are no stones)
bman.field[x][y].ex[d] = 0; /* put the fire powerups in the field */
bman.field[x][y].ex_nr = -1; for (d = 0, x = 0, y = 0; d < GAME_SPECIAL_ITEMFIRE * fkt; d++) {
bman.field[x][y].frame = 0; while (bman.field[x][y].type != FT_stone || bman.field[x][y].special != FT_nothing) {
bman.field[x][y].frameto = 0; x = ((float) rand () / (float) RAND_MAX) * (bman.fieldsize.x - 1);
bman.field[x][y].special = FT_nothing; 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;
/* get some free space for the playerstart position */ }
for (d = 0; d < MAX_PLAYERS; d++)
if ((PS_IS_alife (bman.players[d].state)) != 0) {
x = bman.players[d].pos.x >> 8;
y = bman.players[d].pos.y >> 8;
bman.field[x][y].type = FT_nothing;
if (x > 1)
bman.field[x-1][y].type = FT_nothing;
if (x < bman.fieldsize.x-2)
bman.field[x+1][y].type = FT_nothing;
if (y > 1)
bman.field[x][y-1].type = FT_nothing;
if (y < bman.fieldsize.y -2)
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;
}
} }

Loading…
Cancel
Save