static void dig_level(void) { int new_ry=0; int new_rx=0; int radius=1+rand()%ROOM_RADIUS; //radial or square room bool radial=(bool)rand()%2; while (true) { //continue digging from the last new room or //dig the first room in the middle of the level int ry=(new_ry?new_ry:Y_/2); int rx=(new_rx?new_rx:X_/2); if ((new_rx&&new_ry) || dig_room(ry,rx,radius,radial)) { int paths=1+rand()%PATHS; for (int p=0;p<paths;p++) { int tries=0; //try to find an empty space and dig a room there while (tries++<10000 && !dig_room(new_ry=ry+rand()%(8*radius)-4*radius, new_rx=rx+rand()%(8*radius)-4*radius, radius=1+rand()%ROOM_RADIUS, radial=rand()%2) ); if (tries>10000) return; //connect the old room to the new room dig_path(ry,rx,new_ry,new_rx); } } } }
static void dig_room(GameState *game, int x, int y) { int i, j; int c = game->map[y][x]; if (x > 0 && x < SIZEX - 1 && y > 0 && y < SIZEY - 1 && c == TILE_WALL) { game->map[y][x] = TILE_EMPTY; for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) if (rand() % 9 < 2) dig_room(game, x + i - 1, y + j - 1); } }
void game_init(GameState *game, char *magic_word) { int x, y; int target_empty = SIZEX * SIZEY / 3; int current_empty; for (y = 0; y < SIZEY; y++) for (x = 0; x < SIZEX; x++) { game->map[y][x] = TILE_WALL; game->seen[y][x] = false; } do { dig_room(game, rand() % SIZEX, rand() % SIZEY); current_empty = 0; for (y = 0; y < SIZEY; y++) { for (x = 0; x < SIZEX; x++) { if (game->map[y][x] == TILE_EMPTY) current_empty++; } } } while (current_empty < target_empty); game->player.hp = 5; game->player.pos = rand_pos(game); game->magic_word = magic_word; game->num_letters = strlen(magic_word); game->letters = (Letter *) malloc(game->num_letters * sizeof(Letter)); if (game->letters != NULL) for (size_t i = 0; i < game->num_letters; i++) game->letters[i] = (Letter) { game->magic_word[i], rand_pos(game), false }; }