14 #define MAKEBUF(type) type *buf = malloc(sizeof(type)); *buf = arg;
16 /* Shared variables */
18 struct color black = {0, 0, 0};
22 struct material outside;
24 struct node map[MAP_WIDTH][MAP_HEIGHT];
27 struct list *entities = & (struct list) {
32 /* Private variables */
34 struct entity *entity_collision_map[MAP_WIDTH][MAP_HEIGHT] = {{NULL}};
36 static bool running = true;
37 static double damage_overlay = 0.0;
38 static struct color damage_overlay_color;
40 static struct list *air_functions = NULL;
41 static struct input_handler *input_handlers[256] = {NULL};
42 static struct entity *render_entities[LIGHT * 2 + 1][LIGHT * 2 + 1];
43 static struct list *render_components = NULL;
45 /* Helper functions */
47 struct color get_color(const char *str)
50 sscanf(str, "#%2x%2x%2x", &r, &g, &b);
51 return (struct color) {r, g, b};
54 void set_color(struct color color, bool bg)
56 printf("\e[%u;2;%u;%u;%um", bg ? 48 : 38, color.r, color.g, color.b);
59 void light_color(struct color *color, double light)
66 void mix_color(struct color *color, struct color other, double ratio)
68 double ratio_total = ratio + 1;
70 color->r = (color->r + other.r * ratio) / ratio_total;
71 color->g = (color->g + other.g * ratio) / ratio_total;
72 color->b = (color->b + other.b * ratio) / ratio_total;
75 void dir_to_xy(enum direction dir, int *x, int *y)
93 struct list *add_element(struct list *list, void *element)
97 for (ptr = &list; *ptr != NULL; ptr = &(*ptr)->next)
100 *ptr = malloc(sizeof(struct list));
101 (*ptr)->element = element;
107 int clamp(int v, int min, int max)
109 return v < min ? min : v > max ? max : v;
112 int max(int a, int b)
114 return a > b ? a : b;
117 int min(int a, int b)
119 return a < b ? a : b;
122 /* Game-related utility functions */
131 return player.health <= 0;
136 struct node get_node(int x, int y)
138 return is_outside(x, y) ? (struct node) {&outside} : map[x][y];
141 bool is_outside(int x, int y)
143 return x >= MAP_WIDTH || x < 0 || y >= MAP_HEIGHT || y < 0;
146 bool is_solid(int x, int y)
148 return get_node(x, y).material->solid;
151 /* Entity functions */
153 bool spawn(struct entity def, int x, int y, void *data)
158 if (def.collide_with_entities && entity_collision_map[x][y])
164 struct entity *entity = malloc(sizeof(struct entity));
167 add_element(entities, entity);
169 if (entity->collide_with_entities)
170 entity_collision_map[x][y] = entity;
172 if (entity->on_spawn)
173 entity->on_spawn(entity, data);
178 bool move(struct entity *entity, int xoff, int yoff)
182 x = entity->x + xoff;
183 y = entity->y + yoff;
185 if (is_solid(x, y)) {
186 if (entity->on_collide)
187 entity->on_collide(entity, x, y);
189 } else if (entity->collide_with_entities && entity_collision_map[x][y]) {
190 if (entity->on_collide_with_entity)
191 entity->on_collide_with_entity(entity, entity_collision_map[x][y]);
194 entity_collision_map[entity->x][entity->y] = NULL;
197 entity_collision_map[entity->x][entity->y] = entity;
202 void add_health(struct entity *entity, int health)
204 bool was_alive = entity->health > 0;
206 entity->health += health;
208 if (health < 0 && entity->on_damage)
209 entity->on_damage(entity, -health);
211 if (entity->health > entity->max_health)
212 entity->health = entity->max_health;
213 else if (entity->health <= 0 && was_alive && entity->on_death)
214 entity->on_death(entity);
217 /* Register callback functions */
219 void register_air_function(struct generator_function arg)
221 MAKEBUF(struct generator_function);
222 air_functions = add_element(air_functions, buf);
225 void register_input_handler(unsigned char c, struct input_handler arg)
227 if (input_handlers[c])
230 MAKEBUF(struct input_handler);
231 input_handlers[c] = buf;
234 void register_render_component(void (*arg)(struct winsize ws))
236 render_components = add_element(render_components, arg);
241 static void player_death(struct entity *self)
243 self->texture = "☠";
246 static void player_damage(struct entity *self, int damage)
248 damage_overlay += (double) damage * 0.5;
253 static bool check_direction(int x, int y, enum direction dir)
256 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);
258 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);
261 static void generate_corridor(int lx, int ly, enum direction ldir)
263 if (is_outside(lx, ly))
266 map[lx][ly] = (struct node) {&air};
268 for (struct list *ptr = air_functions; ptr != NULL; ptr = ptr->next) {
269 struct generator_function *func = ptr->element;
271 if (! func->chance || rand() % func->chance == 0) {
272 func->callback(lx, ly);
279 enum direction ret = (ldir + 2) % 4;
292 dir_to_xy(dir, &x, &y);
293 } while (dir == ret || (! check_direction(x, y, dir) && --limit));
296 generate_corridor(x, y, dir);
298 if (rand() % 20 == 0)
299 generate_corridor(lx, ly, ldir);
302 static void generate_corridor_random(int x, int y)
304 enum direction dir = rand() % 4;
306 generate_corridor(x, y, dir);
307 generate_corridor(x, y, (dir + 2) % 4);
312 static bool render_color(struct color color, double light, bool bg)
315 set_color(black, bg);
318 if (damage_overlay > 0.0)
319 mix_color(&color, damage_overlay_color, damage_overlay * 2.0);
321 light_color(&color, light);
323 set_color(color, bg);
328 static void render_map(struct winsize ws)
330 int cols = ws.ws_col / 2 - LIGHT * 2;
331 int rows = ws.ws_row / 2 - LIGHT;
333 int cols_left = ws.ws_col - cols - (LIGHT * 2 + 1) * 2;
334 int rows_left = ws.ws_row - rows - (LIGHT * 2 + 1);
336 for (int i = 0; i < rows; i++)
337 for (int i = 0; i < ws.ws_col; i++)
340 for (int y = -LIGHT; y <= LIGHT; y++) {
341 for (int i = 0; i < cols; i++)
344 for (int x = -LIGHT; x <= LIGHT; x++) {
347 map_x = x + player.x;
348 map_y = y + player.y;
350 struct node node = get_node(map_x, map_y);
352 double dist = sqrt(x * x + y * y);
353 double light = 1.0 - (double) dist / (double) LIGHT;
355 render_color(node.material->color, light, true);
357 struct entity *entity = render_entities[x + LIGHT][y + LIGHT];
358 render_entities[x + LIGHT][y + LIGHT] = NULL;
360 if (entity && render_color(entity->color, light, false))
361 printf("%s", entity->texture);
366 set_color(black, true);
368 for (int i = 0; i < cols_left; i++)
372 for (int i = 0; i < rows_left; i++)
373 for (int i = 0; i < ws.ws_col; i++)
382 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws);
384 for (struct list *ptr = render_components; ptr != NULL; ptr = ptr->next) {
385 printf("\e[0m\e[0;0H");
386 set_color(black, true);
388 ((void (*)(struct winsize ws)) ptr->element)(ws);
396 static void handle_interrupt(int signal)
403 static void handle_input(unsigned char c)
405 struct input_handler *handler = input_handlers[c];
407 if (handler && (handler->run_if_dead || ! player_dead()))
411 static void *input_thread(void *unused)
416 handle_input(tolower(fgetc(stdin)));
428 sa.sa_handler = &handle_interrupt;
429 sigaction(SIGINT, &sa, NULL);
431 generate_corridor_random(player.x, player.y);
433 for (int i = 0; i < 50; i++)
434 generate_corridor_random(rand() % MAP_WIDTH, rand() % MAP_HEIGHT);
436 struct termios oldtio, newtio;
437 tcgetattr(STDIN_FILENO, &oldtio);
439 newtio.c_lflag &= ~(ICANON | ECHO);
440 tcsetattr(STDIN_FILENO, TCSANOW, &newtio);
442 printf("\e[?1049h\e[?25l");
444 pthread_t input_thread_id;
445 pthread_create(&input_thread_id, NULL, &input_thread, NULL);
447 struct timespec ts, ts_old;
448 clock_gettime(CLOCK_REALTIME, &ts_old);
451 clock_gettime(CLOCK_REALTIME, &ts);
452 double dtime = (double) (ts.tv_sec - ts_old.tv_sec) + (double) (ts.tv_nsec - ts_old.tv_nsec) / 1000000000.0;
455 bool dead = player_dead();
457 if (! dead && damage_overlay > 0.0) {
458 damage_overlay -= dtime;
460 if (damage_overlay < 0.0)
461 damage_overlay = 0.0;
464 for (struct list **ptr = &entities; *ptr != NULL; ) {
465 struct entity *entity = (*ptr)->element;
467 if (entity->remove) {
468 assert(entity != &player);
469 struct list *next = (*ptr)->next;
471 if (entity->on_remove)
472 entity->on_remove(entity);
477 if (entity->collide_with_entities)
478 entity_collision_map[entity->x][entity->y] = NULL;
489 dx = entity->x - player.x;
490 dy = entity->y - player.y;
492 bool visible = abs(dx) <= LIGHT && abs(dy) <= LIGHT;
495 render_entities[dx + LIGHT][dy + LIGHT] = entity;
497 if (! dead && entity->on_step)
498 entity->on_step(entity, (struct entity_step_data) {
510 // there is no such thing as glfwSwapBuffers, so we just wait 1 / 60 seconds to prevent artifacts
511 usleep(1000000 / 60);
514 printf("\e[?1049l\e[?25h");
515 tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
518 /* Initializer function */
520 __attribute__ ((constructor)) static void init()
522 wall = (struct material) {
524 .color = get_color("#5B2F00"),
527 air = (struct material) {
529 .color = get_color("#FFE027"),
532 outside = (struct material) {
537 player = (struct entity) {
541 .color = get_color("#00FFFF"),
547 .collide_with_entities = true,
551 .on_collide_with_entity = NULL,
554 .on_death = &player_death,
555 .on_damage = &player_damage,
558 entity_collision_map[player.x][player.y] = &player;
560 for (int x = 0; x < MAP_WIDTH; x++)
561 for (int y = 0; y < MAP_HEIGHT; y++)
562 map[x][y] = (struct node) {&wall};
564 register_input_handler('q', (struct input_handler) {
569 register_render_component(&render_map);
571 damage_overlay_color = get_color("#F20000");
577 get_box_char(is_solid(x, y - 1), is_solid(x, y + 1), is_solid(x - 1, y), is_solid(x + 1, y));
579 const char *get_box_char(bool up, bool down, bool left, bool right)
581 if (left && right && ! up && ! down)
582 return "\u2501\u2501";
583 else if (up && down && ! right && ! left)
585 else if (down && right && ! up && ! left)
586 return "\u250F\u2501";
587 else if (down && left && ! up && ! right)
589 else if (up && right && ! down && ! left)
590 return "\u2517\u2501";
591 else if (up && left && ! down && ! right)
593 else if (up && down && right && ! left)
594 return "\u2523\u2501";
595 else if (up && down && left && ! right)
597 else if (down && left && right && ! up)
598 return "\u2533\u2501";
599 else if (up && left && right && ! down)
600 return "\u253B\u2501";
601 else if (up && down && left && right)
602 return "\u254b\u2501";
603 else if (left && ! up && ! down && ! right)
605 else if (up && ! down && ! left && ! right)
607 else if (right && ! up && ! down && ! left)
608 return "\u257A\u2501";
609 else if (down && ! up && ! left && ! right)
611 else if (! up && ! down && ! left && ! right)