Add fog of war
[dungeon1] / map.c
1 #include "main.h"
2
3
4 struct map *
5 readmap(char *fname)
6 {
7         struct map *map;
8         char buf[BUFSIZ];
9         FILE *fp;
10         int x, y;
11
12         map = calloc(1, sizeof(struct map));
13         map->name = strdup(fname);
14
15         snprintf(buf, sizeof(buf), "%s/%s", MAPDIR, fname);
16         if (!(fp = fopen(buf, "r"))) {
17                 fprintf(stderr, "error fopen() %s\n", buf);
18                 return NULL;
19         }
20
21         x = y = 0;
22         for (y=0; y<H; y++) {
23                 for (x=0; x<W; x++) {
24                         map->map[y][x] = fgetc(fp);
25                 }
26                 if (fgetc(fp) != '\n') {
27                         fprintf(stderr, "error file format: no NL at y=%d x=%d\n", y, x);
28                         return NULL;
29                 }
30         }
31         return map;
32 }
33
34 struct map *
35 getblackmap(void)
36 {
37         struct map *map;
38
39         map = calloc(1, sizeof(struct map));
40         map->name = strdup("(BLACK)");
41         memset(map->map, BLANKCHAR, sizeof(map->map));
42         return map;
43 }
44
45 void
46 see(struct map *map, int ypos, int xpos)
47 {
48         int y, x;
49
50         for (y=ypos-SEEDIST; y <= ypos+SEEDIST; y++) {
51                 if (y<T || y>H) {
52                         continue;
53                 }
54                 for (x=xpos-SEEDIST; x <= xpos+SEEDIST; x++) {
55                         if (x<0 || x>W) {
56                                 continue;
57                         }
58                         mvaddch(y, x, map->map[y][x]);
59                 }
60         }
61         move(ypos, xpos);
62 }
63
64 void
65 freemap(struct map *map)
66 {
67         if (!map) {
68                 return;
69         }
70         free(map->name);
71         free(map);
72 }
73
74 void
75 findchar(struct map *map, char c, int *yret, int *xret)
76 {
77         int y, x;
78
79         for (y=0; y<H; y++) {
80                 for (x=0; x<W; x++) {
81                         if (map->map[y][x] == c) {
82                                 *yret = y;
83                                 *xret = x;
84                                 return;
85                         }
86                 }
87         }
88 }
89
90 void
91 showmap(struct map *map)
92 {
93         int x, y;
94
95         mvprintw(0, 60, "map: %s", map->name);
96         for (y=T; y<H; y++) {
97                 for (x=0; x<W; x++) {
98                         mvaddch(y, x, map->map[y][x]);
99                 }
100         }
101 }
102