11 #include <sys/ioctl.h>
20 struct color black = {0, 0, 0};
24 struct material outside;
26 struct node map[MAP_WIDTH][MAP_HEIGHT];
29 struct list *entities = & (struct list) {
34 struct entity *entity_collision_map[MAP_WIDTH][MAP_HEIGHT] = {{NULL}};
36 struct list *air_functions = NULL;
43 struct color get_color(const char *str)
46 sscanf(str, "#%2x%2x%2x", &r, &g, &b);
47 return (struct color) {r, g, b};
50 bool is_outside(int x, int y)
52 return x >= MAP_WIDTH || x < 0 || y >= MAP_HEIGHT || y < 0;
55 struct node get_node(int x, int y)
57 return is_outside(x, y) ? (struct node) {&outside} : map[x][y];
60 bool is_solid(int x, int y)
62 return get_node(x, y).material->solid;
65 bool move(struct entity *entity, int xoff, int yoff)
73 if (entity->on_collide)
74 entity->on_collide(entity, x, y);
76 } else if (entity->collide_with_entities && entity_collision_map[x][y]) {
77 if (entity->on_collide_with_entity)
78 entity->on_collide_with_entity(entity, entity_collision_map[x][y]);
81 entity_collision_map[entity->x][entity->y] = NULL;
84 entity_collision_map[entity->x][entity->y] = entity;
89 void spawn(struct entity def, int x, int y)
94 if (def.collide_with_entities && entity_collision_map[x][y])
100 struct entity *entity = malloc(sizeof(struct entity));
103 add_element(entities, entity);
105 if (entity->collide_with_entities)
106 entity_collision_map[x][y] = entity;
108 if (entity->on_spawn)
109 entity->on_spawn(entity);
112 void add_health(struct entity *entity, int health)
114 bool was_alive = entity->health > 0;
116 entity->health += health;
118 if (entity->health > entity->max_health)
119 entity->health = entity->max_health;
120 else if (entity->health <= 0 && was_alive && entity->on_death)
121 entity->on_death(entity);
124 void add_score(int s)
131 return player.health <= 0;
134 struct list *add_element(struct list *list, void *element)
138 for (ptr = &list; *ptr != NULL; ptr = &(*ptr)->next)
141 *ptr = malloc(sizeof(struct list));
142 (*ptr)->element = element;
148 void register_air_function(struct generator_function func)
150 struct generator_function *buf = malloc(sizeof(struct generator_function));
153 air_functions = add_element(air_functions, buf);
158 static void player_death(struct entity *self)
160 self->texture = "☠ ";
165 static bool check_direction(int x, int y, int dir)
168 return is_solid(x, y + 1) && is_solid(x, y - 1) && (is_solid(x + 1, y) || rand() % 3 > 1) && (is_solid(x - 1, y) || rand() % 3 > 1);
170 return is_solid(x + 1, y) && is_solid(x - 1, y) && (is_solid(x, y + 1) || rand() % 3 > 1) && (is_solid(x, y - 1) || rand() % 3 > 1);
173 static void generate_corridor(int lx, int ly, int ldir, bool off)
175 if (is_outside(lx, ly))
179 if (off && rand() % 100 == 0)
183 map[lx][ly] = (struct node) {&air};
185 for (struct list *ptr = air_functions; ptr != NULL; ptr = ptr->next) {
186 struct generator_function *func = ptr->element;
188 if (! func->chance || rand() % func->chance == 0) {
189 func->callback(lx, ly);
194 int ret = (ldir + 2) % 4;
221 } while (dir == ret || (! check_direction(x, y, dir) && --limit));
224 generate_corridor(x, y, dir, off);
226 if (rand() % 20 == 0)
227 generate_corridor(lx, ly, ldir, true);
230 static void generate_corridor_random(int x, int y)
232 int dir = rand() % 4;
234 generate_corridor(x, y, dir, false);
235 generate_corridor(x, y, (dir + 2) % 4, false);
240 void set_color(struct color color, bool bg)
242 printf("\e[%u;2;%u;%u;%um", bg ? 48 : 38, color.r, color.g, color.b);
245 struct color light_color(struct color color, double light)
247 return (struct color) {
254 static void render(render_entity_list entity_list)
256 printf("\e[2J\e[0;0H");
259 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws);
261 int cols = ws.ws_col / 2 - LIGHT * 2;
262 int rows = ws.ws_row / 2 - LIGHT;
264 int cols_left = ws.ws_col - cols - (LIGHT * 2 + 1) * 2;
265 int rows_left = ws.ws_row - rows - (LIGHT * 2 + 1);
267 set_color(black, true);
269 for (int i = 0; i < rows; i++)
270 for (int i = 0; i < ws.ws_col; i++)
273 for (int y = -LIGHT; y <= LIGHT; y++) {
274 for (int i = 0; i < cols; i++)
277 for (int x = -LIGHT; x <= LIGHT; x++) {
280 map_x = x + player.x;
281 map_y = y + player.y;
283 struct node node = get_node(map_x, map_y);
285 double dist = sqrt(x * x + y * y);
286 double light = 1.0 - (double) dist / (double) LIGHT;
291 set_color(light_color(node.material->color, light), true);
293 struct entity *entity = entity_list[x + LIGHT][y + LIGHT];
296 set_color(entity->color, false);
297 printf("%s", entity->texture);
304 set_color(black, true);
306 for (int i = 0; i < cols_left; i++)
310 for (int i = 0; i < rows_left + 1; i++)
311 for (int i = 0; i < ws.ws_col; i++)
314 printf("\e[0;0H\e[39m");
316 printf("\e[32m\e[3mScore:\e[23m %d\e[39m", score);
320 for (int i = 0; i < rows; i++)
323 printf("\t\e[1mInventory\e[22m\n\n");
324 printf("\t0x\t\e[3mNothing\e[23m\n");
328 for (int i = 0; i < ws.ws_row - 2; i++)
331 int hearts_cols = ws.ws_col / 2 - player.max_health;
333 for (int i = 0; i < hearts_cols; i++)
336 set_color((struct color) {255, 0, 0}, false);
338 for (int i = 0; i < player.max_health; i++) {
339 if (i >= player.health)
340 set_color(get_color("#5A5A5A"), false);
349 static void handle_input(char c)
351 bool dead = player_dead();
358 dead || move(&player, 0, -1);
361 dead || move(&player, -1, 0);
364 dead || move(&player, 0, 1);
367 dead || move(&player, 1, 0);
374 static void handle_interrupt(int signal)
381 static void *input_thread(void *unused)
386 handle_input(tolower(fgetc(stdin)));
393 __attribute__ ((constructor)) static void init()
395 wall = (struct material) {
397 .color = get_color("#5B2F00"),
400 air = (struct material) {
402 .color = get_color("#FFE027"),
405 outside = (struct material) {
410 player = (struct entity) {
414 .color = get_color("#00FFFF"),
420 .collide_with_entities = true,
424 .on_collide_with_entity = NULL,
427 .on_death = &player_death,
430 entity_collision_map[player.x][player.y] = &player;
432 for (int x = 0; x < MAP_WIDTH; x++)
433 for (int y = 0; y < MAP_HEIGHT; y++)
434 map[x][y] = (struct node) {&wall};
442 sa.sa_handler = &handle_interrupt;
443 sigaction(SIGINT, &sa, NULL);
445 generate_corridor_random(player.x, player.y);
447 for (int i = 0; i < 50; i++)
448 generate_corridor_random(rand() % MAP_WIDTH, rand() % MAP_HEIGHT);
450 struct termios oldtio, newtio;
451 tcgetattr(STDIN_FILENO, &oldtio);
453 newtio.c_lflag &= ~(ICANON | ECHO);
454 tcsetattr(STDIN_FILENO, TCSANOW, &newtio);
456 printf("\e[?1049h\e[?25l");
458 pthread_t input_thread_id;
459 pthread_create(&input_thread_id, NULL, &input_thread, NULL);
461 struct timespec ts, ts_old;
462 clock_gettime(CLOCK_REALTIME, &ts_old);
465 clock_gettime(CLOCK_REALTIME, &ts);
466 double dtime = (double) (ts.tv_sec - ts_old.tv_sec) + (double) (ts.tv_nsec - ts_old.tv_nsec) / 1000000000.0;
469 bool dead = player_dead();
471 render_entity_list render_list = {{NULL}};
473 for (struct list **ptr = &entities; *ptr != NULL; ) {
474 struct entity *entity = (*ptr)->element;
476 if (entity->remove) {
477 assert(entity != &player);
478 struct list *next = (*ptr)->next;
480 if (entity->on_remove)
481 entity->on_remove(entity);
495 dx = entity->x - player.x;
496 dy = entity->y - player.y;
498 bool visible = abs(dx) <= LIGHT && abs(dy) <= LIGHT;
501 render_list[dx + LIGHT][dy + LIGHT] = entity;
503 if (! dead && entity->on_step)
504 entity->on_step(entity, (struct entity_step_data) {
516 // there is no such thing as glfwSwapBuffers, so we just wait 1 / 60 seconds to prevent artifacts
517 usleep(1000000 / 60);
520 printf("\e[?1049l\e[?25h");
521 tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
527 get_box_char(is_solid(x, y - 1), is_solid(x, y + 1), is_solid(x - 1, y), is_solid(x + 1, y));
529 const char *get_box_char(bool up, bool down, bool left, bool right)
531 if (left && right && ! up && ! down)
532 return "\u2501\u2501";
533 else if (up && down && ! right && ! left)
535 else if (down && right && ! up && ! left)
536 return "\u250F\u2501";
537 else if (down && left && ! up && ! right)
539 else if (up && right && ! down && ! left)
540 return "\u2517\u2501";
541 else if (up && left && ! down && ! right)
543 else if (up && down && right && ! left)
544 return "\u2523\u2501";
545 else if (up && down && left && ! right)
547 else if (down && left && right && ! up)
548 return "\u2533\u2501";
549 else if (up && left && right && ! down)
550 return "\u253B\u2501";
551 else if (up && down && left && right)
552 return "\u254b\u2501";
553 else if (left && ! up && ! down && ! right)
555 else if (up && ! down && ! left && ! right)
557 else if (right && ! up && ! down && ! left)
558 return "\u257A\u2501";
559 else if (down && ! up && ! left && ! right)
561 else if (! up && ! down && ! left && ! right)