Minor changes (found out what overlay() means ...)
[dungeon1] / map.c
1 #include "main.h"
2
3 static void beautifymap(struct map *);
4 static char getnewchar(struct map *, int, int);
5
6 struct map *
7 readmap(char *fname)
8 {
9         struct map *map;
10         char buf[BUFSIZ];
11         FILE *fp;
12         int x, y;
13
14         map = calloc(1, sizeof(struct map));
15         map->name = strdup(fname);
16
17         snprintf(buf, sizeof(buf), "%s/%s", MAPDIR, fname);
18         if (!(fp = fopen(buf, "r"))) {
19                 fprintf(stderr, "error fopen() %s\n", buf);
20                 return NULL;
21         }
22
23         for (y=0; y<MAPH; y++) {
24                 for (x=0; x<MAPW; x++) {
25                         map->map[y][x] = fgetc(fp);
26                 }
27                 if (fgetc(fp) != '\n') {
28                         fprintf(stderr, "error file format: no NL at y=%d x=%d\n", y, x);
29                         return NULL;
30                 }
31         }
32         beautifymap(map);
33         return map;
34 }
35
36 static void
37 beautifymap(struct map *map)
38 {
39         int x, y;
40         char c;
41
42         for (y=0; y<MAPH; y++) {
43                 for (x=0; x<MAPW; x++) {
44                         c = map->map[y][x];
45                         if (c != '#') {
46                                 continue;
47                         }
48                         map->map[y][x] = getnewchar(map, y, x);
49                 }
50         }
51         for (y=0; y<MAPH; y++) {
52                 for (x=0; x<MAPW; x++) {
53                         c = map->map[y][x];
54                         if (c == '#') {
55                                 map->map[y][x] = ' ';
56                         }
57                 }
58         }
59 }
60
61 static char
62 getnewchar(struct map *map, int y, int x)
63 {
64         char l, r, a, b;
65         char la, lb, ra, rb;
66         enum {
67                 L, R, A, B,
68         };
69         static struct nbors {
70                 char n[5];
71                 char newc;
72         } nborstable[] = {
73                 { "####", '#' },
74                 { "    ", '+' },
75                 { "#   ", '+' },
76                 { "##  ", '-' },
77                 { "### ", '-' },
78                 { "## #", '-' },
79                 { " #  ", '+' },
80                 { "  ##", '|' },
81                 { " ###", '|' },
82                 { "# ##", '|' },
83                 { "# # ", '\'' },
84                 { " ## ", '\'' },
85                 { "  # ", '\'' },
86                 { "#  #", '.' },
87                 { " # #", '.' },
88                 { "   #", '.' },
89                 { "", '\0' },
90         };
91         struct nbors *p;
92
93         l = x-1 < 0 ? '#' : map->map[y][x-1];
94         l = strchr(WALLCHARS, l) ? '#' : ' ';
95         r = x+1 > W ? '#' : map->map[y][x+1];
96         r = strchr(WALLCHARS, r) ? '#' : ' ';
97         a = y-1 < 0 ? '#' : map->map[y-1][x];
98         a = strchr(WALLCHARS, a) ? '#' : ' ';
99         b = y+1 > H ? '#' : map->map[y+1][x];
100         b = strchr(WALLCHARS, b) ? '#' : ' ';
101
102         la = x-1 < 0 || y-1 < 0 ? '#' : map->map[y-1][x-1];
103         la = strchr(WALLCHARS, la) ? '#' : ' ';
104         ra = x+1 > W || y-1 < 0 ? '#' : map->map[y-1][x+1];
105         ra = strchr(WALLCHARS, ra) ? '#' : ' ';
106         lb = x-1 < 0 || y+1 > H ? '#' : map->map[y+1][x-1];
107         lb = strchr(WALLCHARS, lb) ? '#' : ' ';
108         rb = x+1 > W || y+1 > H ? '#' : map->map[y+1][x+1];
109         rb = strchr(WALLCHARS, rb) ? '#' : ' ';
110
111         for (p=nborstable; *p->n; p++) {
112                 if (p->n[L] == l && p->n[R] == r && p->n[A] == a && p->n[B] == b) {
113                         if (strcmp(p->n, "####")==0) {
114                                 if (la==' ' || ra==' ') {
115                                         return '\'';
116                                 }
117                                 if (lb==' ' || rb==' ') {
118                                         return '.';
119                                 }
120                         }
121                         return p->newc;
122                 }
123         }
124         return map->map[y][x];
125 }
126
127 struct map *
128 getblackmap(void)
129 {
130         struct map *map;
131
132         map = calloc(1, sizeof(struct map));
133         map->name = strdup("(BLACK)");
134         memset(map->map, BLANKCHAR, sizeof(map->map));
135         return map;
136 }
137
138 void
139 see(struct map *map, int ypos, int xpos)
140 {
141         int y, x;
142
143         for (y=ypos-SEEDIST; y <= ypos+SEEDIST; y++) {
144                 if (y<0 || y>=MAPH) {
145                         continue;
146                 }
147                 for (x=xpos-SEEDIST; x <= xpos+SEEDIST; x++) {
148                         if (x<0 || x>=MAPW) {
149                                 continue;
150                         }
151                         mvwaddch(w_map, y, x, map->map[y][x]);
152                 }
153         }
154         wmove(w_map, ypos, xpos);
155 }
156
157 void
158 freemap(struct map *map)
159 {
160         if (!map) {
161                 return;
162         }
163         free(map->name);
164         free(map);
165 }
166
167 void
168 findchar(struct map *map, char c, int *yret, int *xret)
169 {
170         int y, x;
171
172         for (y=0; y<MAPH; y++) {
173                 for (x=0; x<MAPW; x++) {
174                         if (map->map[y][x] == c) {
175                                 *yret = y;
176                                 *xret = x;
177                                 return;
178                         }
179                 }
180         }
181 }
182
183 void
184 showmap(struct map *map)
185 {
186         int x, y;
187
188         mvwprintw(w_info, 1, 20, "map: %s", map->name);
189         for (y=0; y<MAPH; y++) {
190                 for (x=0; x<MAPW; x++) {
191                         mvwaddch(w_map, y, x, map->map[y][x]);
192                 }
193         }
194 }
195