+ if(st->bridge_p && logoh>=3 && logow>=3)
+ {
+ bridge_dir = 1+random()%2;
+ if(bridge_dir==1)
+ {
+ bridge_c = st->logo_y+random()%(logoh-2)+1;
+ }
+ else
+ {
+ bridge_c = st->logo_x+random()%(logow-2)+1;
+ }
+ }
+ else
+ {
+ bridge_dir = 0;
+ bridge_c = -1;
+ }
+ for(i = st->logo_x; i <= st->logo_x + logow; i++)
+ {
+ for(j = st->logo_y; j <= st->logo_y + logoh; j++)
+ {
+ corners[i+width*j] = 1;
+ }
+ }
+ for(xx = st->logo_x; xx < st->logo_x+logow; xx++)
+ {
+ if(!(bridge_dir==2 && xx==bridge_c))
+ {
+ build_wall(st, xx, st->logo_y, 0);
+ build_wall(st, xx, st->logo_y+logoh, 0);
+ }
+ if(bridge_dir==1)
+ {
+ build_wall(st, xx, bridge_c, 0);
+ build_wall(st, xx, bridge_c, 2);
+ }
+ }
+ for(yy = st->logo_y; yy < st->logo_y+logoh; yy++)
+ {
+ if(!(bridge_dir==1 && yy==bridge_c))
+ {
+ build_wall(st, st->logo_x, yy, 3);
+ build_wall(st, st->logo_x+logow, yy, 3);
+ }
+ if(bridge_dir==2)
+ {
+ build_wall(st, bridge_c, yy, 1);
+ build_wall(st, bridge_c, yy, 3);
+ }
+ }
+ /* Connect one wall of the logo with an outside wall. */
+ if(st->bridge_p)
+ dir = (bridge_dir+1)%4;
+ else
+ dir = random()%4;
+ switch(dir)
+ {
+ case 0:
+ xx = st->logo_x+(random()%(logow+1));
+ yy = st->logo_y;
+ break;
+ case 1:
+ xx = st->logo_x+logow;
+ yy = st->logo_y+(random()%(logoh+1));
+ break;
+ case 2:
+ xx = st->logo_x+(random()%(logow+1));
+ yy = st->logo_y+logoh;
+ break;
+ case 3:
+ xx = st->logo_x;
+ yy = st->logo_y+(random()%(logoh+1));
+ break;
+ }
+ do
+ {
+ corners[xx+width*yy] = 1;
+ switch(dir)
+ {
+ case 0:
+ build_wall(st, xx-1, yy-1, 1);
+ yy--;
+ break;
+ case 1:
+ build_wall(st, xx, yy, 0);
+ xx++;
+ break;
+ case 2:
+ build_wall(st, xx, yy, 3);
+ yy++;
+ break;
+ case 3:
+ build_wall(st, xx-1, yy-1, 2);
+ xx--;
+ break;
+ }
+ }
+ while(!corners[xx+width*yy]);
+ if(st->bridge_p)
+ {
+ dir = (dir+2)%4;
+ switch(dir)
+ {
+ case 0:
+ xx = st->logo_x+(random()%(logow+1));
+ yy = st->logo_y;
+ break;
+ case 1:
+ xx = st->logo_x+logow;
+ yy = st->logo_y+(random()%(logoh+1));
+ break;
+ case 2:
+ xx = st->logo_x+(random()%(logow+1));
+ yy = st->logo_y+logoh;
+ break;
+ case 3:
+ xx = st->logo_x;
+ yy = st->logo_y+(random()%(logoh+1));
+ break;
+ }
+ do
+ {
+ corners[xx+width*yy] = 1;
+ switch(dir)
+ {
+ case 0:
+ build_wall(st, xx-1, yy-1, 1);
+ yy--;
+ break;
+ case 1:
+ build_wall(st, xx, yy, 0);
+ xx++;
+ break;
+ case 2:
+ build_wall(st, xx, yy, 3);
+ yy++;
+ break;
+ case 3:
+ build_wall(st, xx-1, yy-1, 2);
+ xx--;
+ break;
+ }
+ }
+ while(!corners[xx+width*yy]);
+ }
+ }
+
+ /* Count open gridpoints. */
+ open_corners = 0;
+ for(i = 0; i < width; i++)
+ for(j = 0; j < height; j++)
+ if(!corners[i+width*j])
+ open_corners++;
+
+ /* Now do actual maze generation. */
+ while(open_corners>0)
+ {
+ for(i = 0; i < width*height; i++)
+ {
+ if(!corners[c_idx[i]])
+ {
+ xx = c_idx[i]%width;
+ yy = c_idx[i]/width;
+ /* Choose a random direction. */
+ dir = random()%4;
+
+ k = 0;
+ /* Measure the length of the wall we'd draw. */
+ while(!corners[xx+width*yy])
+ {
+ k++;
+ switch(dir)
+ {
+ case 0:
+ yy--;
+ break;
+ case 1:
+ xx++;
+ break;
+ case 2:
+ yy++;
+ break;
+ case 3:
+ xx--;
+ break;
+ }
+ }
+
+ if(k<=st->max_length)
+ {
+ xx = c_idx[i]%width;
+ yy = c_idx[i]/width;
+
+ /* Draw a wall until we hit something. */
+ while(!corners[xx+width*yy])
+ {
+ open_corners--;
+ corners[xx+width*yy] = 1;
+ switch(dir)
+ {
+ case 0:
+ build_wall(st, xx-1, yy-1, 1);
+ yy--;
+ break;
+ case 1:
+ build_wall(st, xx, yy, 0);
+ xx++;
+ break;
+ case 2:
+ build_wall(st, xx, yy, 3);
+ yy++;
+ break;
+ case 3:
+ build_wall(st, xx-1, yy-1, 2);
+ xx--;
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ /* Free some memory we used. */
+ free(corners);
+ free(c_idx);
+}
+
+/* The original maze creator. Start somewhere. Take a step in a random
+ * direction. Keep doing this until we hit a wall. Then, backtrack until
+ * we find a point where we can go in another direction.
+ */
+static void
+create_maze (struct state *st) /* create a maze layout given the initialized maze */