void maze_set_init_pos (Pos *pos) { int i, j, k; int incx[4] = {0, 0, 1, -1}; int incy[4] = {1, -1, 0, 0}; do { mazegen(); for (i=0; i<board_wid; i++) for (j=0; j<board_heit; j++) pos->board [j * board_wid + i] = maze_maze[i][j]; for (i=0; i<CORNER_SIZE; i++) for (j=0; j<CORNER_SIZE; j++) pos->board [j * board_wid + i] = 0; for (i=board_wid-CORNER_SIZE; i<board_wid; i++) for (j=board_heit-CORNER_SIZE; j<board_heit; j++) pos->board [j * board_wid + i] = 0; recursive_pathgen (pos->board, 0, 0, -1); } while (pos->board [board_wid * board_heit - 1] != -1); for (i=0; i<board_wid; i++) for (j=0; j<board_heit; j++) if (pos->board [j * board_wid + i] == -1) pos->board [j * board_wid + i] = 0; pos->board[0] = MAZE_CUR; /*while(1) { i = random()%board_wid; j = random()%board_heit; if (pos->board [j*board_wid+i] == 0) { pos->board [j * board_wid + i] = 1; break; } }*/ }
/* Modified and commented by me, Kevin Turner. */ void mazegen(gint pos, gchar *maz, gint x, gint y, gint rnd) { gchar d, i; gint c=0, j=1; /* Punch a hole here... */ maz[pos] = IN; /* If there is a wall two rows above us, bit 1 is 1. */ while((d= (pos <= (x * 2) ? 0 : (maz[pos - x - x ] ? 0 : 1)) /* If there is a wall two rows below us, bit 2 is 1. */ | (pos >= x * (y - 2) ? 0 : (maz[pos + x + x] ? 0 : 2)) /* If there is a wall two columns to the right, bit 3 is 1. */ | (pos % x == x - 2 ? 0 : (maz[pos + 2] ? 0 : 4)) /* If there is a wall two colums to the left, bit 4 is 1. */ | ((pos % x == 1 ) ? 0 : (maz[pos-2] ? 0 : 8)))) { /* Note if all bits are 0, d is false, we don't do this while loop, we don't call ourselves again, so this branch is done. */ /* I see what this loop does (more or less), but I don't know _why_ it does it this way... I also haven't figured out exactly which values of multiple will work and which won't. */ do { rnd = (rnd * mvals.multiple + mvals.offset); i = 3 & (rnd / d); if (++c > 100) { /* Break and try to salvage something */ i=99; /* if it looks like we're going to be */ break; /* here forever... */ } } while ( !(d & ( 1 << i) ) ); /* ...While there's *not* a wall in direction i. */ /* (stop looping when there is) */ switch (i) { /* This is simple enough. */ case 0: /* Go in the direction we just figured . . . */ j= -x; break; case 1: j = x; break; case 2: j=1; break; case 3: j= -1; break; case 99: return; /* Hey neat, broken mazes! */ break; /* (Umm... Wow... Yeah, neat.) */ default: g_warning("maze: mazegen: Going in unknown direction.\n" "i: %d, d: %d, seed: %d, mw: %d, mh: %d, mult: %d, offset: %d\n", i, d,mvals.seed, x, y, mvals.multiple, mvals.offset); break; } /* And punch a hole there. */ maz[pos + j] = 1; /* Now, start again just past where we punched the hole... */ mazegen(pos + 2 * j, maz, x, y, rnd); } /* End while(d=...) Loop */ /* This routine is quite quick, even for rather large mazes. For that reason, it doesn't need a progress bar. */ return; }