Minor changes (found out what overlay() means ...)
[dungeon1] / map.c
diff --git a/map.c b/map.c
index 4b948ff..4c548e6 100644 (file)
--- a/map.c
+++ b/map.c
@@ -1,5 +1,7 @@
 #include "main.h"
 
+static void beautifymap(struct map *);
+static char getnewchar(struct map *, int, int);
 
 struct map *
 readmap(char *fname)
@@ -18,9 +20,8 @@ readmap(char *fname)
                return NULL;
        }
 
-       x = y = 0;
-       for (y=0; y<H; y++) {
-               for (x=0; x<W; x++) {
+       for (y=0; y<MAPH; y++) {
+               for (x=0; x<MAPW; x++) {
                        map->map[y][x] = fgetc(fp);
                }
                if (fgetc(fp) != '\n') {
@@ -28,18 +29,166 @@ readmap(char *fname)
                        return NULL;
                }
        }
+       beautifymap(map);
        return map;
 }
 
+static void
+beautifymap(struct map *map)
+{
+       int x, y;
+       char c;
+
+       for (y=0; y<MAPH; y++) {
+               for (x=0; x<MAPW; x++) {
+                       c = map->map[y][x];
+                       if (c != '#') {
+                               continue;
+                       }
+                       map->map[y][x] = getnewchar(map, y, x);
+               }
+       }
+       for (y=0; y<MAPH; y++) {
+               for (x=0; x<MAPW; 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 < 0 ? '#' : 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];
+}
+
+struct map *
+getblackmap(void)
+{
+       struct map *map;
+
+       map = calloc(1, sizeof(struct map));
+       map->name = strdup("(BLACK)");
+       memset(map->map, BLANKCHAR, sizeof(map->map));
+       return map;
+}
+
+void
+see(struct map *map, int ypos, int xpos)
+{
+       int y, x;
+
+       for (y=ypos-SEEDIST; y <= ypos+SEEDIST; y++) {
+               if (y<0 || y>=MAPH) {
+                       continue;
+               }
+               for (x=xpos-SEEDIST; x <= xpos+SEEDIST; x++) {
+                       if (x<0 || x>=MAPW) {
+                               continue;
+                       }
+                       mvwaddch(w_map, y, x, map->map[y][x]);
+               }
+       }
+       wmove(w_map, ypos, xpos);
+}
+
+void
+freemap(struct map *map)
+{
+       if (!map) {
+               return;
+       }
+       free(map->name);
+       free(map);
+}
+
+void
+findchar(struct map *map, char c, int *yret, int *xret)
+{
+       int y, x;
+
+       for (y=0; y<MAPH; y++) {
+               for (x=0; x<MAPW; x++) {
+                       if (map->map[y][x] == c) {
+                               *yret = y;
+                               *xret = x;
+                               return;
+                       }
+               }
+       }
+}
+
 void
-showmap(struct map* map)
+showmap(struct map *map)
 {
        int x, y;
 
-       mvprintw(0, 50, "map: %s", map->name);
-       for (y=T; y<H; y++) {
-               for (x=0; x<W; x++) {
-                       mvaddch(y, x, map->map[y][x]);
+       mvwprintw(w_info, 1, 20, "map: %s", map->name);
+       for (y=0; y<MAPH; y++) {
+               for (x=0; x<MAPW; x++) {
+                       mvwaddch(w_map, y, x, map->map[y][x]);
                }
        }
 }