From cccecbe0c0861ecef1cb511378132f9e981056e6 Mon Sep 17 00:00:00 2001 From: markus schnalke Date: Sun, 19 Dec 2021 20:21:21 +0100 Subject: [PATCH] Add fog of war --- main.c | 26 +++++++++++++++++--------- main.h | 8 +++++++- map.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- maps/map1 | 14 +++++++------- maps/map2 | 24 ++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 19 deletions(-) create mode 100644 maps/map2 diff --git a/main.c b/main.c index af24677..2d80a81 100644 --- a/main.c +++ b/main.c @@ -13,7 +13,7 @@ main(void) WINDOW *win; int c; char c2; - struct map *map; + struct map *map, *fow; int gold=0; win = initscr(); @@ -25,11 +25,13 @@ main(void) intrflush(stdscr, FALSE); */ - map = readmap("map1"); - showmap(map); + fow = getblackmap(); + showmap(fow); + map = readmap("map2"); - mvprintw(0, 20, "%d:%d", W, H); - move(H/2, W/2); + findchar(map, '>', &y, &x); + move(y, x); + see(map, y, x); refresh(); while ((c = getch()) != ERR) { @@ -60,29 +62,35 @@ main(void) if (y2 < 0 || y2 > H || x2 < 0 || x2 > W) { continue; } - c2 = mvinch(y2, x2) & 255; - mvprintw(0, 40, "<%c> gold:%d", c2, gold); - move(y, x); + c2 = map->map[y2][x2]; switch (c2) { case '$': gold++; + map->map[y2][x2] = ' '; mvaddch(y2, x2, ' '); /* FALL */ case ' ': mvprintw(0, 0, "%d:%d", x2, y2); move(y2, x2); + see(map, y2, x2); break; - case '>': + case '*': mvprintw(H/2, 10, "AUSGANG gefunden!"); goto exit; + default: + continue; } + mvprintw(0, 40, "<%c> gold:%d", c2, gold); + move(y2, x2); + refresh(); napms(10); } exit: + mvprintw(H, 0, "press key to exit..."); getch(); endwin(); return 0; diff --git a/main.h b/main.h index 011b80c..b5e7959 100644 --- a/main.h +++ b/main.h @@ -5,11 +5,13 @@ #include #define MAPDIR "maps" +#define BLANKCHAR ':' enum { H = 24, W = 80, T = 1, + SEEDIST = 4, }; struct map { @@ -18,5 +20,9 @@ struct map { }; struct map *readmap(char *); -void showmap(struct map*); +struct map *getblackmap(void); +void freemap(struct map *); +void showmap(struct map *); +void findchar(struct map *, char, int *, int *); +void see(struct map *, int, int); diff --git a/map.c b/map.c index 4b948ff..2dedb2d 100644 --- a/map.c +++ b/map.c @@ -31,12 +31,68 @@ readmap(char *fname) return map; } +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 (yH) { + continue; + } + for (x=xpos-SEEDIST; x <= xpos+SEEDIST; x++) { + if (x<0 || x>W) { + continue; + } + mvaddch(y, x, map->map[y][x]); + } + } + move(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; ymap[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); + mvprintw(0, 60, "map: %s", map->name); for (y=T; ymap[y][x]); diff --git a/maps/map1 b/maps/map1 index 5b3caff..e28fc8e 100644 --- a/maps/map1 +++ b/maps/map1 @@ -9,15 +9,15 @@ ################################################################################ ################################################################################ ########################### ############################ -##################### H F S L C ############ : O= ######### -> ##### #### * ############ > -########## ####### $ ######## () [=]######### +##################### H F S L C ############ ######### +> ##### #### * ############ * +########## ####### $ ######## ######### ########################### ######## ################### ########################### ######## ################### -########################################### ############## ################### -############################################# ########## ################### -############################################# ########## ##################### -############################################# ###################### +########################################### ############## ########## +############################################# ########## ######## ########## +############################################# ########## ########## ########## +############################################# ####### $ ########## ################################################################################ ################################################################################ ################################################################################ diff --git a/maps/map2 b/maps/map2 new file mode 100644 index 0000000..30238fc --- /dev/null +++ b/maps/map2 @@ -0,0 +1,24 @@ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +####################|++++++ ############################ +-------------.###.++' H F S L C ############ ######### +> '+++' .--. * ############ * +---------. .--'##| $ ######## ######### +#########'----------'#####| ######## ################### +##########################| ######## ################### +##########################+-----########### ############## ########## +############################################# ########## ######## ########## +############################################# ########## ########## ########## +############################################# ####### $ ########## +################################################################################ +################################################################################ +################################################################################ +################################################################################ -- 1.7.10.4