Add map readings and movements based on map stuff
authormarkus schnalke <meillo@marmaro.de>
Sun, 19 Dec 2021 17:39:56 +0000 (18:39 +0100)
committermarkus schnalke <meillo@marmaro.de>
Sun, 19 Dec 2021 17:39:56 +0000 (18:39 +0100)
Makefile
main.c
main.h
map.c [new file with mode: 0644]
maps/map1 [new file with mode: 0644]

index a4bb22a..d90816b 100644 (file)
--- 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 (file)
--- 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 (file)
--- a/main.h
+++ b/main.h
@@ -1,4 +1,22 @@
 #include <stdio.h>
-#include <ncurses.h>
 #include <unistd.h>
+#include <string.h>
+#include <stdlib.h>
+#include <ncurses.h>
+
+#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 (file)
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; y<H; y++) {
+               for (x=0; x<W; x++) {
+                       map->map[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; y<H; y++) {
+               for (x=0; x<W; x++) {
+                       mvaddch(y, x, map->map[y][x]);
+               }
+       }
+}
+
diff --git a/maps/map1 b/maps/map1
new file mode 100644 (file)
index 0000000..5b3caff
--- /dev/null
+++ b/maps/map1
@@ -0,0 +1,24 @@
+################################################################################
+################################################################################
+################################################################################
+################################################################################
+################################################################################
+################################################################################
+################################################################################
+################################################################################
+################################################################################
+################################################################################
+###########################                         ############################
+#####################         H  F  S  L  C         ############  : O= #########
+>            #####     ####                     *   ############               >
+##########          #######        $                ########     () [=]#########
+###########################                         ######## ###################
+###########################                         ######## ###################
+###########################################   ############## ###################
+############################################# ##########     ###################
+############################################# ##########   #####################
+#############################################             ######################
+################################################################################
+################################################################################
+################################################################################
+################################################################################