LDFLAGS=-lncurses
PROG=dungeon1
-OBJS=main.o
+OBJS=main.o map.o
HDRS=main.h
all: dungeon1
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();
intrflush(stdscr, FALSE);
*/
+ map = readmap("map1");
+ showmap(map);
+
mvprintw(0, 20, "%d:%d", W, H);
move(H/2, W/2);
refresh();
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;
}
#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*);
--- /dev/null
+#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]);
+ }
+ }
+}
+
--- /dev/null
+################################################################################
+################################################################################
+################################################################################
+################################################################################
+################################################################################
+################################################################################
+################################################################################
+################################################################################
+################################################################################
+################################################################################
+########################### ############################
+##################### H F S L C ############ : O= #########
+> ##### #### * ############ >
+########## ####### $ ######## () [=]#########
+########################### ######## ###################
+########################### ######## ###################
+########################################### ############## ###################
+############################################# ########## ###################
+############################################# ########## #####################
+############################################# ######################
+################################################################################
+################################################################################
+################################################################################
+################################################################################