+static void
+beautifymap(struct map *map)
+{
+ int x, y;
+ char c;
+
+ for (y=0; y<H; y++) {
+ for (x=0; x<W; x++) {
+ c = map->map[y][x];
+ if (c != '#') {
+ continue;
+ }
+ map->map[y][x] = getnewchar(map, y, x);
+ }
+ }
+ for (y=0; y<H; y++) {
+ for (x=0; x<W; x++) {
+ c = map->map[y][x];
+ if (c == '#') {
+ map->map[y][x] = ' ';
+ }
+ }
+ }
+}
+
+static char
+getnewchar(struct map *map, int y, int x)
+{
+ char l, r, a, b;
+ char la, lb, ra, rb;
+ enum {
+ L, R, A, B,
+ };
+ static struct nbors {
+ char n[5];
+ char newc;
+ } nborstable[] = {
+ { "####", '#' },
+ { " ", '+' },
+ { "# ", '+' },
+ { "## ", '-' },
+ { "### ", '-' },
+ { "## #", '-' },
+ { " # ", '+' },
+ { " ##", '|' },
+ { " ###", '|' },
+ { "# ##", '|' },
+ { "# # ", '\'' },
+ { " ## ", '\'' },
+ { " # ", '\'' },
+ { "# #", '.' },
+ { " # #", '.' },
+ { " #", '.' },
+ { "", '\0' },
+ };
+ struct nbors *p;
+
+ l = x-1 < 0 ? '#' : map->map[y][x-1];
+ l = strchr(WALLCHARS, l) ? '#' : ' ';
+ r = x+1 > W ? '#' : map->map[y][x+1];
+ r = strchr(WALLCHARS, r) ? '#' : ' ';
+ a = y-1 < T ? '#' : map->map[y-1][x];
+ a = strchr(WALLCHARS, a) ? '#' : ' ';
+ b = y+1 > H ? '#' : map->map[y+1][x];
+ b = strchr(WALLCHARS, b) ? '#' : ' ';
+
+ la = x-1 < 0 || y-1 < 0 ? '#' : map->map[y-1][x-1];
+ la = strchr(WALLCHARS, la) ? '#' : ' ';
+ ra = x+1 > W || y-1 < 0 ? '#' : map->map[y-1][x+1];
+ ra = strchr(WALLCHARS, ra) ? '#' : ' ';
+ lb = x-1 < 0 || y+1 > H ? '#' : map->map[y+1][x-1];
+ lb = strchr(WALLCHARS, lb) ? '#' : ' ';
+ rb = x+1 > W || y+1 > H ? '#' : map->map[y+1][x+1];
+ rb = strchr(WALLCHARS, rb) ? '#' : ' ';
+
+ for (p=nborstable; *p->n; p++) {
+ if (p->n[L] == l && p->n[R] == r && p->n[A] == a && p->n[B] == b) {
+ if (strcmp(p->n, "####")==0) {
+ if (la==' ' || ra==' ') {
+ return '\'';
+ }
+ if (lb==' ' || rb==' ') {
+ return '.';
+ }
+ }
+ return p->newc;
+ }
+ }
+ return map->map[y][x];
+}
+