/* $Id: map.c,v 1.6 2003/07/27 21:09:57 stpohle Exp $ */ /* map handling, like generate and load maps. */ #include "bomberclone.h" _map map; // put items into the field void map_fillitems (int fieldtype, int num) { int nb_try = 100, d, x, y; /* this is the item factor we multiply it with this so we know how much items we want in the game */ float fkt = ((float) (map.size.x * map.size.y)) / (25.0 * 17.0); /* put the row special in the field */ for (d = 0; d < num * fkt; d++) { x = y = 0; while (map.field[x][y].type != FT_stone || map.field[x][y].special != FT_nothing) { x = ((float) rand () / (float) RAND_MAX) * (map.size.x - 1); y = ((float) rand () / (float) RAND_MAX) * (map.size.y - 1); nb_try--; if (nb_try < 0) break; } map.field[x][y].special = fieldtype; } } // loads or generate an map void map_new (char *filename) { int x, y; FILE *fmap; if (filename) { fmap = fopen (filename, "r"); /* if we can't open the given filename for any reason, reverting to default value else, load the file */ if (fmap) map_load (fmap); } else fmap = NULL; // Clean and create the field // if (fmap == NULL) map_genrandom (); /* delete the bfield data */ for (x = 0; x < MAX_FIELDSIZE_X; x++) for (y = 0; y < MAX_FIELDSIZE_Y; y++) map.bfield[x][y] = 0; /* Set the Playerinformation */ map_set_playerposition (fmap != NULL); /* put the fire powerups in the field */ map_fillitems (FT_fire, map.fire); /* put the bomb powerups in the field */ map_fillitems (FT_bomb, map.bombs); /* put the shoe powerup in the field */ map_fillitems (FT_shoe, map.shoes); /* put the death ?powerups? in the field */ map_fillitems (FT_death, map.death); /* put the mixed powerrup in the field */ map_fillitems (FT_mixed, map.mixed); /* put the trigger special in the field */ map_fillitems (FT_sp_trigger, map.sp_trigger); /* put the row special in the field */ map_fillitems (FT_sp_row, map.sp_row); /* put the push special in the field */ map_fillitems (FT_sp_push, map.sp_push); /* put the kick special in the field */ // map_fillitems (FT_sp_kick, GAME_SPECIAL_ITEMSKICK); } void map_genrandom () { int x, y, d; /* if we can't load the map check first the fieldsize settings */ if (map.size.x < MIN_FIELDSIZE_X) map.size.x = MIN_FIELDSIZE_X; if (map.size.x > MAX_FIELDSIZE_X) map.size.x = MAX_FIELDSIZE_X; for (x = 0; x < map.size.x; x++) for (y = 0; y < map.size.y; y++) { if ((y == 0) || (y == map.size.y - 1)) map.field[x][y].type = FT_block; else if ((x == 0) || (x == map.size.x - 1)) map.field[x][y].type = FT_block; else if (((x & 1) == 0) && ((y & 1) == 0)) map.field[x][y].type = FT_block; else { // create random field if ((s_random (256) & 3) == 0) map.field[x][y].type = FT_nothing; else map.field[x][y].type = FT_stone; } for (d = 0; d < 4; d++) map.field[x][y].ex[d].frame = map.field[x][y].ex[d].count = 0; map.field[x][y].ex_nr = -1; map.field[x][y].frame = 0; map.field[x][y].frameto = 0; map.field[x][y].special = FT_nothing; } } /* will set the playerposition but in a way that we won't start on a block */ /* i am just too lazy to write this all again and again */ #define PLX bman.players[i].pos.x #define PLY bman.players[i].pos.y void map_set_playerposition (int usermap) { int p, dist, i, j, mx, my, dx = 0, dy = 0; char txt[255]; p = 50; dist = 8; while (p == 50) { p = 0; dist--; for (i = 0; (p < 50 && i < MAX_PLAYERS);) { if (usermap) { int maxloop = 0; while (maxloop < 200 && (PLX == -1 || PLY == -1)) { maxloop++; PLX = s_random (map.size.x - 2) + 1; PLY = s_random (map.size.y - 2) + 1; for (dx = 10, dy = 10, j = 0; (j < i && j < MAX_PLAYERS && (dx > 1 || dy > 1)); j++) { /* is ther any other player */ dx = PLX - bman.players[j].pos.x; if (dx < 0) dx = -dx; dy = PLY - bman.players[j].pos.y; if (dy < 0) dy = -dy; } /* check if there is no block */ if ((dx > 1 || dy > 1) && ((map.field[PLX][PLY].type != FT_block && maxloop > 100) || map.field[PLX][PLY].type == FT_nothing)) { /* get (up or down) dx and (left or right) dy */ dx = s_random (2); if (dx == 0) dx = -1; dy = s_random (2); if (dy == 0) dy = -1; /* first check if there is and free place for us */ if (!((map.field[PLX + dx][PLY].type != FT_block && maxloop > 100) || map.field[PLX + dx][PLY].type == FT_nothing)) dx = -dx; if (!((map.field[PLX + dx][PLY].type != FT_block && maxloop > 100) || map.field[PLX + dx][PLY].type == FT_nothing)) PLX = -1; if (!((map.field[PLX][PLY + dy].type != FT_block && maxloop > 100) || map.field[PLX][PLY + dy].type == FT_nothing)) dy = -dy; if (!((map.field[PLX][PLY + dy].type != FT_block && maxloop > 100) || map.field[PLX][PLY + dy].type == FT_nothing)) PLY = -1; } else { PLX = -1; PLY = -1; } /* make some space */ if (PLX != -1 && PLY != -1) { map.field[PLX][PLY].type = FT_nothing; map.field[PLX + dx][PLY].type = FT_nothing; map.field[PLX][PLY + dy].type = FT_nothing; } } } if (PLX == -1 || PLY == -1) { /* we could not set all fields or we don't run on a usermap */ if (usermap) { sprintf (txt, "Not all players could be set (Pl:%d)", i); menu_displaymessage ("MAP - ERROR", txt); } /* now there will be some fields deleted */ PLX = 2 * (s_random ((map.size.x - 1) / 2)) + 1; PLY = 2 * (s_random ((map.size.y - 1) / 2)) + 1; map.field[PLX][PLY].type = FT_nothing; dx = s_random (4); // bit 1 = up/down bit 2 = left/right /* up and down */ if (((dx & 1) == 0 && PLX > 1) || PLX >= map.size.x - 2) map.field[PLX - 1][PLY].type = FT_nothing; else map.field[PLX + 1][PLY].type = FT_nothing; /* left and right */ if (((dx & 2) == 0 && PLY > 1) || PLY >= map.size.y - 2) map.field[PLX][PLY - 1].type = FT_nothing; else map.field[PLX][PLY + 1].type = FT_nothing; } mx = my = 100; for (j = 0; j <= i; j++) { /* search smalest distance */ dy = PLY - bman.players[j].pos.y; dx = PLX - bman.players[j].pos.x; if (dy < 0) dy = -dy; if (dx < 0) dx = -dx; if (mx > dx && i != j) mx = dx; if (my > dy && i != j) my = dy; } if (mx > dist || my > dist) i++; else p++; } } for (i = 0; i < MAX_PLAYERS; i++) { PLX = PLX << 8; PLY = PLY << 8; } }; #undef PLX #undef PLY /* load a random map */ void map_random () { _direntry *destart, *de, *desel; char path[LEN_PATHFILENAME]; int max, sel; sprintf (path, "%s/maps", bman.datapath); desel = destart = s_getdir (path); for (max = 0, de = destart; de != NULL; de = de->next) if ((de->flags & DF_file) == DF_file) max++; sel = s_random (max); for (max = 0, de = destart; max <= sel && de != NULL; de = de->next) if ((de->flags & DF_file) == DF_file) { desel = de; max++; } d_printf ("Random Map %s (%d on %d)\n", desel->name, sel, max); if (desel != NULL) sprintf (map.map, "%s/maps/%s", bman.datapath, desel->name); } // Init the game according to options void init_map_tileset () { switch (map.map_selection) { case (0): map_new (map.map); break; case (1): map_random (); map_new (map.map); break; case (2): map_new (NULL); break; } if (map.random_tileset) tileset_random (); } /* read from an open file map, determine field.x and field.y and fill the field. (# correspond to a bloc and @ correspond to a stone, an espace is nothing ' ' % are commentary at the beginning of the map */ void map_load (FILE * fmap) { size_t length; char *currentline; char tmp[MAX_FIELDSIZE_X]; int sizex = 0; int sizey = 0; int i; int d; while ((currentline = fgets (tmp, MAX_FIELDSIZE_X, fmap))) { length = strlen (currentline); if (currentline[0] == '%') continue; /* now each line correspond to the field */ else if (strstr (currentline, "bombs") == currentline) // bombs map.bombs = atoi (strchr (currentline, '=') + 1); else if (strstr (currentline, "fire") == currentline) // fire map.fire = atoi (strchr (currentline, '=') + 1); else if (strstr (currentline, "shoes") == currentline) // shoes map.shoes = atoi (strchr (currentline, '=') + 1); else if (strstr (currentline, "mixed") == currentline) // mixed map.mixed = atoi (strchr (currentline, '=') + 1); else if (strstr (currentline, "death") == currentline) // death map.death = atoi (strchr (currentline, '=') + 1); else if (strstr (currentline, "sp_trigger") == currentline) // trigger special map.sp_trigger = atoi (strchr (currentline, '=') + 1); else if (strstr (currentline, "sp_push") == currentline) // push special map.sp_push = atoi (strchr (currentline, '=') + 1); else if (strstr (currentline, "sp_row") == currentline) // row special map.sp_row = atoi (strchr (currentline, '=') + 1); else if (currentline[0] == '#') { /* the map itself */ for (i = 0; i < length; i++) { switch (currentline[i]) { case '#': map.field[i][sizey].type = FT_block; break; case '@': map.field[i][sizey].type = FT_stone; break; case ' ': map.field[i][sizey].type = FT_nothing; default: break; } for (d = 0; d < 4; d++) map.field[i][sizey].ex[d].frame = map.field[i][sizey].ex[d].count = 0; map.field[i][sizey].ex_nr = -1; map.field[i][sizey].frame = 0; map.field[i][sizey].frameto = 0; map.field[i][sizey].special = FT_nothing; } sizey++; if (sizex < length) sizex = length; } } map.size.x = sizex - 1; map.size.y = sizey; /* darw the border so we know everything is right */ for (i = 0; i < map.size.x; i++) map.field[i][0].type = map.field[i][map.size.y - 1].type = FT_block; for (i = 0; i < map.size.y; i++) map.field[0][i].type = map.field[map.size.x - 1][i].type = FT_block; fclose (fmap); }