Add map beautifying (still has some minor bugs)
authormarkus schnalke <meillo@marmaro.de>
Sun, 19 Dec 2021 20:55:36 +0000 (21:55 +0100)
committermarkus schnalke <meillo@marmaro.de>
Sun, 19 Dec 2021 20:55:36 +0000 (21:55 +0100)
main.c
main.h
map.c

diff --git a/main.c b/main.c
index 2d80a81..bd5c248 100644 (file)
--- a/main.c
+++ b/main.c
@@ -27,7 +27,7 @@ main(void)
 
        fow = getblackmap();
        showmap(fow);
-       map = readmap("map2");
+       map = readmap("map1");
 
        findchar(map, '>', &y, &x);
        move(y, x);
diff --git a/main.h b/main.h
index b5e7959..7cce2ec 100644 (file)
--- a/main.h
+++ b/main.h
@@ -5,7 +5,8 @@
 #include <ncurses.h>
 
 #define MAPDIR "maps"
-#define BLANKCHAR ':'
+#define BLANKCHAR ' '
+#define WALLCHARS "#-|.'+"
 
 enum {
        H = 24,
diff --git a/map.c b/map.c
index 2dedb2d..92a8d92 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,7 +20,6 @@ readmap(char *fname)
                return NULL;
        }
 
-       x = y = 0;
        for (y=0; y<H; y++) {
                for (x=0; x<W; x++) {
                        map->map[y][x] = fgetc(fp);
@@ -28,9 +29,101 @@ readmap(char *fname)
                        return NULL;
                }
        }
+       beautifymap(map);
        return map;
 }
 
+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];
+}
+
 struct map *
 getblackmap(void)
 {
@@ -48,11 +141,11 @@ see(struct map *map, int ypos, int xpos)
        int y, x;
 
        for (y=ypos-SEEDIST; y <= ypos+SEEDIST; y++) {
-               if (y<T || y>H) {
+               if (y<T || y>=H) {
                        continue;
                }
                for (x=xpos-SEEDIST; x <= xpos+SEEDIST; x++) {
-                       if (x<0 || x>W) {
+                       if (x<0 || x>=W) {
                                continue;
                        }
                        mvaddch(y, x, map->map[y][x]);