11 #include <sys/ioctl.h>
17 double damage_overlay = 0.0;
21 struct color black = {0, 0, 0};
25 struct material outside;
27 struct node map[MAP_WIDTH][MAP_HEIGHT];
30 struct list *entities = & (struct list) {
35 struct entity *entity_collision_map[MAP_WIDTH][MAP_HEIGHT] = {{NULL}};
37 struct list *air_functions = NULL;
44 struct color get_color(const char *str)
47 sscanf(str, "#%2x%2x%2x", &r, &g, &b);
48 return (struct color) {r, g, b};
51 bool is_outside(int x, int y)
53 return x >= MAP_WIDTH || x < 0 || y >= MAP_HEIGHT || y < 0;
56 struct node get_node(int x, int y)
58 return is_outside(x, y) ? (struct node) {&outside} : map[x][y];
61 bool is_solid(int x, int y)
63 return get_node(x, y).material->solid;
66 bool move(struct entity *entity, int xoff, int yoff)
74 if (entity->on_collide)
75 entity->on_collide(entity, x, y);
77 } else if (entity->collide_with_entities && entity_collision_map[x][y]) {
78 if (entity->on_collide_with_entity)
79 entity->on_collide_with_entity(entity, entity_collision_map[x][y]);
82 entity_collision_map[entity->x][entity->y] = NULL;
85 entity_collision_map[entity->x][entity->y] = entity;
90 void spawn(struct entity def, int x, int y)
95 if (def.collide_with_entities && entity_collision_map[x][y])
101 struct entity *entity = malloc(sizeof(struct entity));
104 add_element(entities, entity);
106 if (entity->collide_with_entities)
107 entity_collision_map[x][y] = entity;
109 if (entity->on_spawn)
110 entity->on_spawn(entity);
113 void add_health(struct entity *entity, int health)
115 bool was_alive = entity->health > 0;
117 entity->health += health;
119 if (health < 0 && entity->on_damage)
120 entity->on_damage(entity, -health);
122 if (entity->health > entity->max_health)
123 entity->health = entity->max_health;
124 else if (entity->health <= 0 && was_alive && entity->on_death)
125 entity->on_death(entity);
128 void add_score(int s)
135 return player.health <= 0;
138 struct list *add_element(struct list *list, void *element)
142 for (ptr = &list; *ptr != NULL; ptr = &(*ptr)->next)
145 *ptr = malloc(sizeof(struct list));
146 (*ptr)->element = element;
152 void register_air_function(struct generator_function func)
154 struct generator_function *buf = malloc(sizeof(struct generator_function));
157 air_functions = add_element(air_functions, buf);
162 static void player_death(struct entity *self)
164 self->texture = "☠ ";
167 static void player_damage(struct entity *self, int damage)
169 damage_overlay = (double) damage * 0.5;
174 static bool check_direction(int x, int y, int dir)
177 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);
179 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);
182 static void generate_corridor(int lx, int ly, int ldir, bool off)
184 if (is_outside(lx, ly))
188 if (off && rand() % 100 == 0)
192 map[lx][ly] = (struct node) {&air};
194 for (struct list *ptr = air_functions; ptr != NULL; ptr = ptr->next) {
195 struct generator_function *func = ptr->element;
197 if (! func->chance || rand() % func->chance == 0) {
198 func->callback(lx, ly);
203 int ret = (ldir + 2) % 4;
230 } while (dir == ret || (! check_direction(x, y, dir) && --limit));
233 generate_corridor(x, y, dir, off);
235 if (rand() % 20 == 0)
236 generate_corridor(lx, ly, ldir, true);
239 static void generate_corridor_random(int x, int y)
241 int dir = rand() % 4;
243 generate_corridor(x, y, dir, false);
244 generate_corridor(x, y, (dir + 2) % 4, false);
249 void set_color(struct color color, bool bg)
251 printf("\e[%u;2;%u;%u;%um", bg ? 48 : 38, color.r, color.g, color.b);
254 void light_color(struct color *color, double light)
261 void mix_color(struct color *color, struct color other, double ratio)
263 double ratio_total = ratio + 1;
265 color->r = (color->r + other.r * ratio) / ratio_total;
266 color->g = (color->g + other.g * ratio) / ratio_total;
267 color->b = (color->b + other.b * ratio) / ratio_total;
270 void render_color(struct color color, double light, bool bg)
273 set_color(black, bg);
275 if (damage_overlay > 0.0)
276 mix_color(&color, get_color("#F20000"), damage_overlay * 2.0);
278 light_color(&color, light);
280 set_color(color, bg);
284 static void render(render_entity_list entity_list)
286 printf("\e[2J\e[0;0H");
289 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws);
291 int cols = ws.ws_col / 2 - LIGHT * 2;
292 int rows = ws.ws_row / 2 - LIGHT;
294 int cols_left = ws.ws_col - cols - (LIGHT * 2 + 1) * 2;
295 int rows_left = ws.ws_row - rows - (LIGHT * 2 + 1);
297 set_color(black, true);
299 for (int i = 0; i < rows; i++)
300 for (int i = 0; i < ws.ws_col; i++)
303 for (int y = -LIGHT; y <= LIGHT; y++) {
304 for (int i = 0; i < cols; i++)
307 for (int x = -LIGHT; x <= LIGHT; x++) {
310 map_x = x + player.x;
311 map_y = y + player.y;
313 struct node node = get_node(map_x, map_y);
315 double dist = sqrt(x * x + y * y);
316 double light = 1.0 - (double) dist / (double) LIGHT;
318 render_color(node.material->color, light, true);
320 struct entity *entity = entity_list[x + LIGHT][y + LIGHT];
323 render_color(entity->color, light, false);
324 printf("%s", entity->texture);
330 set_color(black, true);
332 for (int i = 0; i < cols_left; i++)
336 for (int i = 0; i < rows_left + 1; i++)
337 for (int i = 0; i < ws.ws_col; i++)
340 printf("\e[0;0H\e[39m");
342 printf("\e[32m\e[3mScore:\e[23m %d\e[39m", score);
346 for (int i = 0; i < rows; i++)
349 printf("\t\e[1mInventory\e[22m\n\n");
350 printf("\t0x\t\e[3mNothing\e[23m\n");
354 for (int i = 0; i < ws.ws_row - 2; i++)
357 int hearts_cols = ws.ws_col / 2 - player.max_health;
359 for (int i = 0; i < hearts_cols; i++)
362 set_color((struct color) {255, 0, 0}, false);
364 for (int i = 0; i < player.max_health; i++) {
365 if (i >= player.health)
366 set_color(get_color("#5A5A5A"), false);
375 static void handle_input(char c)
377 bool dead = player_dead();
384 dead || move(&player, 0, -1);
387 dead || move(&player, -1, 0);
390 dead || move(&player, 0, 1);
393 dead || move(&player, 1, 0);
400 static void handle_interrupt(int signal)
407 static void *input_thread(void *unused)
412 handle_input(tolower(fgetc(stdin)));
419 __attribute__ ((constructor)) static void init()
421 wall = (struct material) {
423 .color = get_color("#5B2F00"),
426 air = (struct material) {
428 .color = get_color("#FFE027"),
431 outside = (struct material) {
436 player = (struct entity) {
440 .color = get_color("#00FFFF"),
446 .collide_with_entities = true,
450 .on_collide_with_entity = NULL,
453 .on_death = &player_death,
454 .on_damage = &player_damage,
457 entity_collision_map[player.x][player.y] = &player;
459 for (int x = 0; x < MAP_WIDTH; x++)
460 for (int y = 0; y < MAP_HEIGHT; y++)
461 map[x][y] = (struct node) {&wall};
469 sa.sa_handler = &handle_interrupt;
470 sigaction(SIGINT, &sa, NULL);
472 generate_corridor_random(player.x, player.y);
474 for (int i = 0; i < 50; i++)
475 generate_corridor_random(rand() % MAP_WIDTH, rand() % MAP_HEIGHT);
477 struct termios oldtio, newtio;
478 tcgetattr(STDIN_FILENO, &oldtio);
480 newtio.c_lflag &= ~(ICANON | ECHO);
481 tcsetattr(STDIN_FILENO, TCSANOW, &newtio);
483 printf("\e[?1049h\e[?25l");
485 pthread_t input_thread_id;
486 pthread_create(&input_thread_id, NULL, &input_thread, NULL);
488 struct timespec ts, ts_old;
489 clock_gettime(CLOCK_REALTIME, &ts_old);
492 clock_gettime(CLOCK_REALTIME, &ts);
493 double dtime = (double) (ts.tv_sec - ts_old.tv_sec) + (double) (ts.tv_nsec - ts_old.tv_nsec) / 1000000000.0;
496 bool dead = player_dead();
498 if (! dead && damage_overlay > 0.0)
499 damage_overlay -= dtime;
501 render_entity_list render_list = {{NULL}};
503 for (struct list **ptr = &entities; *ptr != NULL; ) {
504 struct entity *entity = (*ptr)->element;
506 if (entity->remove) {
507 assert(entity != &player);
508 struct list *next = (*ptr)->next;
510 if (entity->on_remove)
511 entity->on_remove(entity);
525 dx = entity->x - player.x;
526 dy = entity->y - player.y;
528 bool visible = abs(dx) <= LIGHT && abs(dy) <= LIGHT;
531 render_list[dx + LIGHT][dy + LIGHT] = entity;
533 if (! dead && entity->on_step)
534 entity->on_step(entity, (struct entity_step_data) {
546 // there is no such thing as glfwSwapBuffers, so we just wait 1 / 60 seconds to prevent artifacts
547 usleep(1000000 / 60);
550 printf("\e[?1049l\e[?25h");
551 tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
557 get_box_char(is_solid(x, y - 1), is_solid(x, y + 1), is_solid(x - 1, y), is_solid(x + 1, y));
559 const char *get_box_char(bool up, bool down, bool left, bool right)
561 if (left && right && ! up && ! down)
562 return "\u2501\u2501";
563 else if (up && down && ! right && ! left)
565 else if (down && right && ! up && ! left)
566 return "\u250F\u2501";
567 else if (down && left && ! up && ! right)
569 else if (up && right && ! down && ! left)
570 return "\u2517\u2501";
571 else if (up && left && ! down && ! right)
573 else if (up && down && right && ! left)
574 return "\u2523\u2501";
575 else if (up && down && left && ! right)
577 else if (down && left && right && ! up)
578 return "\u2533\u2501";
579 else if (up && left && right && ! down)
580 return "\u253B\u2501";
581 else if (up && down && left && right)
582 return "\u254b\u2501";
583 else if (left && ! up && ! down && ! right)
585 else if (up && ! down && ! left && ! right)
587 else if (right && ! up && ! down && ! left)
588 return "\u257A\u2501";
589 else if (down && ! up && ! left && ! right)
591 else if (! up && ! down && ! left && ! right)