aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Janzen <jacob.a.s.janzen@gmail.com>2024-02-11 23:52:18 -0600
committerJacob Janzen <jacob.a.s.janzen@gmail.com>2024-02-11 23:52:18 -0600
commit53f018de3c6db90ed120e9993c656e23836874ae (patch)
treec15ae974a6dd66895f54d5d8f46ff4f255056684
parent063f4f854b1274d306c75f5737cccccfc51d8b52 (diff)
functioning mvp
-rw-r--r--Makefile9
-rw-r--r--cavegen.c85
-rw-r--r--cavegen.h18
-rw-r--r--cavegen.obin0 -> 2248 bytes
-rw-r--r--drunkard.c77
-rw-r--r--main.c86
6 files changed, 172 insertions, 103 deletions
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 <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#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 <stdbool.h>
+
+#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
--- /dev/null
+++ b/cavegen.o
Binary files 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 <stdbool.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <time.h>
-
-#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 <curses.h>
+#include <locale.h>
#include <stdlib.h>
+#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();