From: markus schnalke Date: Sun, 19 Dec 2021 17:39:56 +0000 (+0100) Subject: Add map readings and movements based on map stuff X-Git-Url: http://git.marmaro.de/?a=commitdiff_plain;h=36829587f6a6c375aa2912d955f84871a098c254;p=dungeon1 Add map readings and movements based on map stuff --- diff --git a/Makefile b/Makefile index a4bb22a..d90816b 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CFLAGS=-Wall -Wextra -pedantic -g LDFLAGS=-lncurses PROG=dungeon1 -OBJS=main.o +OBJS=main.o map.o HDRS=main.h all: dungeon1 diff --git a/main.c b/main.c index e9c5604..af24677 100644 --- a/main.c +++ b/main.c @@ -9,14 +9,12 @@ int main(void) { - int x, y; + int x, y, x2, y2; WINDOW *win; int c; - enum { - W = 80, - H = 24, - }; char c2; + struct map *map; + int gold=0; win = initscr(); noecho(); @@ -27,6 +25,9 @@ main(void) intrflush(stdscr, FALSE); */ + map = readmap("map1"); + showmap(map); + mvprintw(0, 20, "%d:%d", W, H); move(H/2, W/2); refresh(); @@ -36,44 +37,53 @@ main(void) break; } getyx(win, y, x); - addch('.'); + x2 = x; + y2 = y; switch (c) { case KEY_LEFT: case 'h': - if (x > 0) { - x--; - } + x2 = x - 1; break; case KEY_RIGHT: case 'l': - if (x < W) { - x++; - } + x2 = x + 1; break; case KEY_UP: case 'k': - if (y > 1) { - y--; - } + y2 = y - 1; break; case KEY_DOWN: case 'j': - if (y < H) { - y++; - } + y2 = y + 1; break; } - c2 = mvinch(y, x) & 255; - switch (c2) { + if (y2 < 0 || y2 > H || x2 < 0 || x2 > W) { + continue; } - mvprintw(0, 40, "<%c>", c2); - mvprintw(0, 0, "%d:%d", x, y); + c2 = mvinch(y2, x2) & 255; + mvprintw(0, 40, "<%c> gold:%d", c2, gold); move(y, x); + switch (c2) { + case '$': + gold++; + mvaddch(y2, x2, ' '); + /* FALL */ + case ' ': + mvprintw(0, 0, "%d:%d", x2, y2); + move(y2, x2); + break; + case '>': + mvprintw(H/2, 10, "AUSGANG gefunden!"); + goto exit; + } + refresh(); napms(10); } +exit: + getch(); endwin(); return 0; } diff --git a/main.h b/main.h index 603e59b..011b80c 100644 --- a/main.h +++ b/main.h @@ -1,4 +1,22 @@ #include -#include #include +#include +#include +#include + +#define MAPDIR "maps" + +enum { + H = 24, + W = 80, + T = 1, +}; + +struct map { + char *name; + char map[24][80]; +}; + +struct map *readmap(char *); +void showmap(struct map*); diff --git a/map.c b/map.c new file mode 100644 index 0000000..4b948ff --- /dev/null +++ b/map.c @@ -0,0 +1,46 @@ +#include "main.h" + + +struct map * +readmap(char *fname) +{ + struct map *map; + char buf[BUFSIZ]; + FILE *fp; + int x, y; + + map = calloc(1, sizeof(struct map)); + map->name = strdup(fname); + + snprintf(buf, sizeof(buf), "%s/%s", MAPDIR, fname); + if (!(fp = fopen(buf, "r"))) { + fprintf(stderr, "error fopen() %s\n", buf); + return NULL; + } + + x = y = 0; + for (y=0; ymap[y][x] = fgetc(fp); + } + if (fgetc(fp) != '\n') { + fprintf(stderr, "error file format: no NL at y=%d x=%d\n", y, x); + return NULL; + } + } + return map; +} + +void +showmap(struct map* map) +{ + int x, y; + + mvprintw(0, 50, "map: %s", map->name); + for (y=T; ymap[y][x]); + } + } +} + diff --git a/maps/map1 b/maps/map1 new file mode 100644 index 0000000..5b3caff --- /dev/null +++ b/maps/map1 @@ -0,0 +1,24 @@ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +################################################################################ +########################### ############################ +##################### H F S L C ############ : O= ######### +> ##### #### * ############ > +########## ####### $ ######## () [=]######### +########################### ######## ################### +########################### ######## ################### +########################################### ############## ################### +############################################# ########## ################### +############################################# ########## ##################### +############################################# ###################### +################################################################################ +################################################################################ +################################################################################ +################################################################################