From 53f018de3c6db90ed120e9993c656e23836874ae Mon Sep 17 00:00:00 2001 From: Jacob Janzen Date: Sun, 11 Feb 2024 23:52:18 -0600 Subject: functioning mvp --- Makefile | 9 ++++--- cavegen.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cavegen.h | 18 +++++++++++++ cavegen.o | Bin 0 -> 2248 bytes drunkard.c | 77 ------------------------------------------------------ main.c | 86 ++++++++++++++++++++++++++++++++++++++++++++----------------- 6 files changed, 172 insertions(+), 103 deletions(-) create mode 100644 cavegen.c create mode 100644 cavegen.h create mode 100644 cavegen.o delete mode 100644 drunkard.c diff --git a/Makefile b/Makefile index da8a861..35c15e1 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,7 @@ -all: drunkard main +all: main -main: main.c - $(CC) main.c -lcurses -o main +main: main.c cavegen.o + $(CC) main.c cavegen.o -lcurses -o main -D_XOPEN_SOURCE_EXTENDED + +cavegen.o: cavegen.c + $(CC) cavegen.c -c -o cavegen.o diff --git a/cavegen.c b/cavegen.c new file mode 100644 index 0000000..11a88cb --- /dev/null +++ b/cavegen.c @@ -0,0 +1,85 @@ +#include "cavegen.h" + +#include +#include +#include +#include + +#define HEIGHT 100 +#define WIDTH 180 +#define MAX_STEPS 200 +#define NUM_WALKERS 100 + +enum direction { + UP, + LEFT, + RIGHT, + DOWN, + NUM_DIRS, +}; + +void create_cave(bool **map, struct point **open, int *open_tiles) +{ + srand(time(NULL)); + *map = malloc(sizeof(bool) * HEIGHT * WIDTH); + for (int i = 0; i < HEIGHT; ++i) { + for (int j = 0; j < WIDTH; ++j) { + (*map)[i * WIDTH + j] = false; + } + } + + int start_x = WIDTH / 2; + int start_y = HEIGHT / 2; + *open_tiles = 1; + *open = malloc(sizeof(struct point) * HEIGHT * WIDTH); + struct point p = {.x = start_x, .y = start_y}; + (*open)[0] = p; + + for (int i = 0; i < NUM_WALKERS; ++i) { + struct point curr_point = (*open)[rand() % *open_tiles]; + int x_pos = curr_point.x; + int y_pos = curr_point.y; + + for (int j = 1; j < MAX_STEPS - 1 && x_pos < WIDTH - 1 && x_pos >= 1 && + y_pos < HEIGHT - 1 && y_pos >= 1; + ++j) { + if (!((*map)[y_pos * WIDTH + x_pos])) { + (*map)[y_pos * WIDTH + x_pos] = true; + struct point p = {.x = x_pos, .y = y_pos}; + (*open)[(*open_tiles)++] = p; + } + enum direction dir = rand() % NUM_DIRS; + switch (dir) { + case UP : --y_pos; break; + case LEFT : --x_pos; break; + case RIGHT : ++x_pos; break; + case DOWN : ++y_pos; break; + default : exit(EXIT_FAILURE); // should not occur + } + } + } + + for (int i = 0; i < NUM_WALKERS; ++i) { + struct point curr_point = (*open)[rand() % *open_tiles]; + int x_pos = curr_point.x; + int y_pos = curr_point.y; + + for (int j = 1; j < MAX_STEPS - 1 && x_pos < WIDTH - 1 && x_pos >= 1 && + y_pos < HEIGHT - 1 && y_pos >= 1; + ++j) { + if (!((*map)[y_pos * WIDTH + x_pos])) { + (*map)[y_pos * WIDTH + x_pos] = true; + struct point p = {.x = x_pos, .y = y_pos}; + (*open)[(*open_tiles)++] = p; + } + enum direction dir = rand() % NUM_DIRS; + switch (dir) { + case UP : --y_pos; break; + case LEFT : --x_pos; break; + case RIGHT : ++x_pos; break; + case DOWN : ++y_pos; break; + default : exit(EXIT_FAILURE); // should not occur + } + } + } +} diff --git a/cavegen.h b/cavegen.h new file mode 100644 index 0000000..829c7a6 --- /dev/null +++ b/cavegen.h @@ -0,0 +1,18 @@ +#ifndef CAVEGEN_H_ +#define CAVEGEN_H_ + +#include + +#define HEIGHT 100 +#define WIDTH 180 +#define MAX_STEPS 200 +#define NUM_WALKERS 100 + +struct point { + int x; + int y; +}; + +void create_cave(bool **map, struct point **open, int *open_tiles); + +#endif // CAVEGEN_H_ diff --git a/cavegen.o b/cavegen.o new file mode 100644 index 0000000..60822c3 Binary files /dev/null and b/cavegen.o differ diff --git a/drunkard.c b/drunkard.c deleted file mode 100644 index 2427249..0000000 --- a/drunkard.c +++ /dev/null @@ -1,77 +0,0 @@ -#include -#include -#include -#include - -#define HEIGHT 50 -#define WIDTH 50 -#define MAX_STEPS 50 -#define NUM_WALKERS 100 - -enum direction { - UP, - LEFT, - RIGHT, - DOWN, - NUM_DIRS, -}; - -struct point { - int x; - int y; -}; - -int main(void) -{ - bool map[HEIGHT][WIDTH]; - for (int i = 0; i < HEIGHT; ++i) { - for (int j = 0; j < WIDTH; ++j) { - map[i][j] = false; - } - } - - int start_x = WIDTH / 2; - int start_y = HEIGHT / 2; - - int open_tiles = 1; - struct point open[HEIGHT * WIDTH]; - struct point p = {.x = start_x, .y = start_y}; - open[0] = p; - - srand(time(NULL)); - - for (int i = 0; i < NUM_WALKERS; ++i) { - struct point curr_point = open[rand() % open_tiles]; - int x_pos = curr_point.x; - int y_pos = curr_point.y; - - for (int j = 0; j < MAX_STEPS && x_pos < WIDTH && x_pos >= 0 && - y_pos < HEIGHT && y_pos >= 0; - ++j) { - if (!map[y_pos][x_pos]) { - map[y_pos][x_pos] = true; - struct point p = {.x = x_pos, .y = y_pos}; - open[open_tiles++] = p; - } - enum direction dir = rand() % NUM_DIRS; - switch (dir) { - case UP : --y_pos; break; - case LEFT : --x_pos; break; - case RIGHT : ++x_pos; break; - case DOWN : ++y_pos; break; - default : exit(EXIT_FAILURE); // should not occur - } - } - } - - for (int i = 0; i < HEIGHT; ++i) { - for (int j = 0; j < WIDTH; ++j) { - if (map[i][j]) { - printf("#"); - } else { - printf("."); - } - } - printf("\n"); - } -} diff --git a/main.c b/main.c index 6f646ec..d422b30 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,9 @@ #include +#include #include +#include "cavegen.h" + #define MAIN_PANEL_WIDTH 100 #define MAIN_PANEL_HEIGHT 41 #define INSTRUCTION_PANEL_WIDTH 32 @@ -17,15 +20,10 @@ WINDOW *create_newwin(int height, int width, int starty, int startx) return local_win; } -void destroy_win(WINDOW *local_win) +void initialize(void) { - wborder(local_win, ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '); - wrefresh(local_win); - delwin(local_win); -} + setlocale(LC_ALL, ""); // allow extended ASCII -int main(void) -{ initscr(); // initialize curses // exit on unsupported consoles @@ -47,11 +45,45 @@ int main(void) curs_set(0); // disable the cursor start_color(); // enable colours - int starty = 12; - int startx = 40; + // setup colours init_pair(1, COLOR_WHITE, COLOR_BLACK); wattron(stdscr, COLOR_PAIR(1)); refresh(); +} + +void display_map(WINDOW *win, bool *map, int cam_y, int cam_x) +{ + for (int i = 1; i < MAIN_PANEL_HEIGHT - 1; ++i) { + for (int j = 1; j < MAIN_PANEL_WIDTH - 1; ++j) { + int i1 = i - 1 + cam_y; + int j1 = j - 1 + cam_x; + if (i1 > HEIGHT || j1 > WIDTH || i1 < 0 || j1 < 0) { + mvwaddch(win, i, j, ' '); + } else if (map[i1 * WIDTH + j1]) { + mvwaddch(win, i, j, '.'); + } else if (i1 > 0 && map[(i1 - 1) * WIDTH + j1]) { + mvwprintw(win, i, j, "█"); + } else if (i1 < HEIGHT - 1 && map[(i1 + 1) * WIDTH + j1]) { + mvwprintw(win, i, j, "█"); + } else if (j1 > 0 && map[i1 * WIDTH + j1 - 1]) { + mvwprintw(win, i, j, "█"); + } else if (j1 < WIDTH - 1 && map[i1 * WIDTH + j1 + 1]) { + mvwprintw(win, i, j, "█"); + } else { + mvwaddch(win, i, j, ' '); + } + } + } + mvwaddch(win, MAIN_PANEL_HEIGHT / 2, MAIN_PANEL_WIDTH / 2, '@'); + wrefresh(win); +} + +int main(void) +{ + int starty = 12; + int startx = 40; + + initialize(); // create the windows WINDOW *inst = create_newwin( @@ -82,38 +114,46 @@ int main(void) wrefresh(msgs); wattron(main_win, COLOR_PAIR(1)); - mvwaddch(main_win, starty, startx, '@'); wrefresh(main_win); + bool *map; + struct point *open_tiles; + int num_open_tiles; + create_cave(&map, &open_tiles, &num_open_tiles); + + display_map(main_win, map, starty, startx); + int ch; while ((ch = getch()) != KEY_F(1)) { + int xpos = startx + MAIN_PANEL_WIDTH / 2 - 1; + int ypos = starty + MAIN_PANEL_HEIGHT / 2 - 1; switch (ch) { case 'k' : - if (starty > 1) { - mvwaddch(main_win, starty, startx, ' '); - mvwaddch(main_win, --starty, startx, '@'); + if (ypos > 0) { + if (map[(ypos - 1) * WIDTH + xpos]) + --starty; } break; case 'j' : - if (starty < MAIN_PANEL_HEIGHT - 2) { - mvwaddch(main_win, starty, startx, ' '); - mvwaddch(main_win, ++starty, startx, '@'); + if (ypos < HEIGHT - 1) { + if (map[(ypos + 1) * WIDTH + xpos]) + ++starty; } break; case 'h' : - if (startx > 1) { - mvwaddch(main_win, starty, startx, ' '); - mvwaddch(main_win, starty, --startx, '@'); + if (xpos > 0) { + if (map[ypos * WIDTH + xpos - 1]) + --startx; } break; case 'l' : - if (startx < MAIN_PANEL_WIDTH - 2) { - mvwaddch(main_win, starty, startx, ' '); - mvwaddch(main_win, starty, ++startx, '@'); + if (xpos < WIDTH - 1) { + if (map[ypos * WIDTH + xpos + 1]) + ++startx; } break; } - wrefresh(main_win); + display_map(main_win, map, starty, startx); } endwin(); -- cgit v1.2.3