13 /* Shared variables */
15 struct color black = {0};
19 struct material outside;
21 struct node map[MAP_WIDTH][MAP_HEIGHT];
23 struct list *entities = & (struct list) {
28 /* Private variables */
30 struct entity *entity_collision_map[MAP_WIDTH][MAP_HEIGHT] = {{NULL}};
32 static bool running = true;
33 static double damage_overlay = 0.0;
34 static struct color damage_overlay_color;
36 static struct list *air_functions = NULL;
37 static struct input_handler *input_handlers[256] = {NULL};
38 static struct entity *render_entities[LIGHT * 2 + 1][LIGHT * 2 + 1];
39 static struct list *render_components = NULL;
40 static struct list *globalsteps = NULL;
42 /* Helper functions */
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 void set_color(struct color color, bool bg)
53 printf("\e[%u;2;%u;%u;%um", bg ? 48 : 38, color.r, color.g, color.b);
56 void light_color(struct color *color, double light)
63 void mix_color(struct color *color, struct color other, double ratio)
65 double ratio_total = ratio + 1;
67 color->r = (color->r + other.r * ratio) / ratio_total;
68 color->g = (color->g + other.g * ratio) / ratio_total;
69 color->b = (color->b + other.b * ratio) / ratio_total;
72 void dir_to_xy(enum direction dir, int *x, int *y)
90 struct list *add_element(struct list *list, void *element)
94 for (ptr = &list; *ptr != NULL; ptr = &(*ptr)->next)
97 *ptr = malloc(sizeof(struct list));
98 (*ptr)->element = element;
104 int clamp(int v, int min, int max)
106 return v < min ? min : v > max ? max : v;
109 int max(int a, int b)
111 return a > b ? a : b;
114 int min(int a, int b)
116 return a < b ? a : b;
119 void *make_buffer(void *ptr, size_t size)
121 void *buf = malloc(size);
122 memcpy(buf, ptr, size);
127 double calculate_dtime(struct timespec from, struct timespec to)
129 return (double) (to.tv_sec - from.tv_sec) + (double) (to.tv_nsec - from.tv_nsec) / 1000000000.0;
132 /*struct roman_conversion_rule
158 void get_roman_numeral(int number, char **ptr, size_t *len)
163 for (int i = 0; i < 13; i++) {
164 while (number >= roman_ct[i].number) {
165 number -= roman_ct[i].number;
166 size_t old_len = *len;
168 *ptr = realloc(*ptr, *len + 1);
169 strcpy(*ptr + old_len, roman_ct[i].symbol);
174 /* Game-related utility functions */
183 return player.health <= 0;
188 struct node get_node(int x, int y)
190 return is_outside(x, y) ? (struct node) {&outside} : map[x][y];
193 bool is_outside(int x, int y)
195 return x >= MAP_WIDTH || x < 0 || y >= MAP_HEIGHT || y < 0;
198 bool is_solid(int x, int y)
200 return get_node(x, y).material->solid;
203 /* Entity functions */
205 bool spawn(struct entity def, int x, int y, void *data)
210 if (def.collide_with_entities && entity_collision_map[x][y])
216 struct entity *entity = malloc(sizeof(struct entity));
219 add_element(entities, entity);
221 if (entity->collide_with_entities)
222 entity_collision_map[x][y] = entity;
224 if (entity->on_spawn)
225 entity->on_spawn(entity, data);
230 bool move(struct entity *entity, int xoff, int yoff)
234 x = entity->x + xoff;
235 y = entity->y + yoff;
237 if (is_solid(x, y)) {
238 if (entity->on_collide)
239 entity->on_collide(entity, x, y);
241 } else if (entity->collide_with_entities && entity_collision_map[x][y]) {
242 if (entity->on_collide_with_entity)
243 entity->on_collide_with_entity(entity, entity_collision_map[x][y]);
246 entity_collision_map[entity->x][entity->y] = NULL;
249 entity_collision_map[entity->x][entity->y] = entity;
254 void add_health(struct entity *entity, int health)
256 bool was_alive = entity->health > 0;
258 entity->health += health;
260 if (health < 0 && entity->on_damage)
261 entity->on_damage(entity, -health);
263 if (entity->health > entity->max_health)
264 entity->health = entity->max_health;
265 else if (entity->health <= 0 && was_alive && entity->on_death)
266 entity->on_death(entity);
269 /* Register callback functions */
271 void register_air_function(struct generator_function func)
273 air_functions = add_element(air_functions, make_buffer(&func, sizeof(struct generator_function)));
276 void register_input_handler(unsigned char c, struct input_handler handler)
278 if (input_handlers[c])
281 input_handlers[c] = make_buffer(&handler, sizeof(struct input_handler));
284 void register_render_component(void (*callback)(struct winsize ws))
286 render_components = add_element(render_components, callback);
289 void register_globalstep(struct globalstep step)
291 globalsteps = add_element(globalsteps, make_buffer(&step, sizeof(struct globalstep)));
296 static void player_death(struct entity *self)
298 self->texture = "☠";
301 static void player_damage(struct entity *self, int damage)
303 damage_overlay += (double) damage * 0.5;
306 struct entity player = {
317 .collide_with_entities = true,
321 .on_collide_with_entity = NULL,
324 .on_death = &player_death,
325 .on_damage = &player_damage,
330 static void mapgen_set_air(int x, int y)
332 if (is_outside(x, y))
335 if (map[x][y].material == &air)
338 map[x][y] = (struct node) {&air};
340 for (struct list *ptr = air_functions; ptr != NULL; ptr = ptr->next) {
341 struct generator_function *func = ptr->element;
343 if (rand() % func->chance == 0)
344 func->callback(x, y);
348 static void generate_room(int origin_x, int origin_y)
350 int left = 5 + rand() % 10;
351 int right = 5 + rand() % 10;
356 for (int x = -left; x <= right; x++) {
365 for (int y = -up; y <= down; y++)
366 mapgen_set_air(origin_x + x, origin_y + y);
370 static bool check_direction(int x, int y, enum direction dir)
373 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);
375 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);
378 static void generate_corridor(int lx, int ly, enum direction ldir)
380 if (is_outside(lx, ly))
383 if (rand() % 200 == 0) {
384 generate_room(lx, ly);
388 mapgen_set_air(lx, ly);
393 enum direction ret = (ldir + 2) % 4;
406 dir_to_xy(dir, &x, &y);
407 } while (dir == ret || (! check_direction(x, y, dir) && --limit));
410 generate_corridor(x, y, dir);
412 if (rand() % 20 == 0)
413 generate_corridor(lx, ly, ldir);
416 static void generate_corridor_random(int x, int y)
418 enum direction dir = rand() % 4;
420 generate_corridor(x, y, dir);
421 generate_corridor(x, y, (dir + 2) % 4);
426 static bool render_color(struct color color, double light, bool bg, bool use_color)
429 set_color(black, bg);
431 } else if (use_color) {
432 if (damage_overlay > 0.0)
433 mix_color(&color, damage_overlay_color, damage_overlay * 2.0);
435 light_color(&color, light);
437 set_color(color, bg);
444 static void render_map(struct winsize ws)
446 int cols = ws.ws_col / 2 - LIGHT * 2;
447 int rows = ws.ws_row / 2 - LIGHT;
449 int cols_left = ws.ws_col - cols - (LIGHT * 2 + 1) * 2;
450 int rows_left = ws.ws_row - rows - (LIGHT * 2 + 1);
452 for (int i = 0; i < rows; i++)
453 for (int i = 0; i < ws.ws_col; i++)
456 for (int y = -LIGHT; y <= LIGHT; y++) {
457 for (int i = 0; i < cols; i++)
460 for (int x = -LIGHT; x <= LIGHT; x++) {
463 map_x = x + player.x;
464 map_y = y + player.y;
466 struct node node = get_node(map_x, map_y);
468 double dist = sqrt(x * x + y * y);
469 double light = 1.0 - (double) dist / (double) LIGHT;
471 render_color(node.material->color, light, true, true);
473 struct entity *entity = render_entities[x + LIGHT][y + LIGHT];
474 render_entities[x + LIGHT][y + LIGHT] = NULL;
476 if (entity && render_color(entity->color, light, false, entity->use_color))
477 printf("%s", entity->texture);
482 set_color(black, true);
484 for (int i = 0; i < cols_left; i++)
488 for (int i = 0; i < rows_left; i++)
489 for (int i = 0; i < ws.ws_col; i++)
498 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws);
500 for (struct list *ptr = render_components; ptr != NULL; ptr = ptr->next) {
501 printf("\e[0m\e[0;0H");
502 set_color(black, true);
504 ((void (*)(struct winsize ws)) ptr->element)(ws);
512 static void handle_interrupt(int signal)
519 static void handle_input(unsigned char c)
521 struct input_handler *handler = input_handlers[c];
523 if (handler && (handler->run_if_dead || ! player_dead()))
527 static void *input_thread(void *unused)
532 handle_input(tolower(fgetc(stdin)));
542 sa.sa_handler = &handle_interrupt;
543 sigaction(SIGINT, &sa, NULL);
545 generate_corridor_random(player.x, player.y);
547 for (int i = 0; i < 50; i++)
548 generate_corridor_random(rand() % MAP_WIDTH, rand() % MAP_HEIGHT);
550 struct termios oldtio, newtio;
551 tcgetattr(STDIN_FILENO, &oldtio);
553 newtio.c_lflag &= ~(ICANON | ECHO);
554 tcsetattr(STDIN_FILENO, TCSANOW, &newtio);
556 printf("\e[?1049h\e[?25l");
558 pthread_t input_thread_id;
559 pthread_create(&input_thread_id, NULL, &input_thread, NULL);
561 struct timespec ts, ts_old;
562 clock_gettime(CLOCK_REALTIME, &ts_old);
565 clock_gettime(CLOCK_REALTIME, &ts);
566 double dtime = calculate_dtime(ts_old, ts);
569 bool dead = player_dead();
571 for (struct list *ptr = globalsteps; ptr != NULL; ptr = ptr->next) {
572 struct globalstep *step = ptr->element;
574 if (step->run_if_dead || ! dead)
575 step->callback(dtime);
578 if (! dead && damage_overlay > 0.0) {
579 damage_overlay -= dtime;
581 if (damage_overlay < 0.0)
582 damage_overlay = 0.0;
585 for (struct list **ptr = &entities; *ptr != NULL; ) {
586 struct entity *entity = (*ptr)->element;
588 if (entity->remove) {
589 assert(entity != &player);
590 struct list *next = (*ptr)->next;
592 if (entity->on_remove)
593 entity->on_remove(entity);
598 if (entity->collide_with_entities)
599 entity_collision_map[entity->x][entity->y] = NULL;
610 dx = entity->x - player.x;
611 dy = entity->y - player.y;
613 bool visible = abs(dx) <= LIGHT && abs(dy) <= LIGHT;
616 render_entities[dx + LIGHT][dy + LIGHT] = entity;
618 if (! dead && entity->on_step)
619 entity->on_step(entity, (struct entity_step_data) {
631 // there is no such thing as glfwSwapBuffers, so we just wait 1 / 60 seconds to prevent artifacts
632 usleep(1000000 / 60);
635 printf("\e[?1049l\e[?25h");
636 tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
639 /* Initializer function */
641 __attribute__ ((constructor)) static void init()
645 wall = (struct material) {
647 .color = get_color("#5B2F00"),
650 air = (struct material) {
652 .color = get_color("#FFE027"),
655 outside = (struct material) {
660 entity_collision_map[player.x][player.y] = &player;
662 for (int x = 0; x < MAP_WIDTH; x++)
663 for (int y = 0; y < MAP_HEIGHT; y++)
664 map[x][y] = (struct node) {&wall};
666 register_input_handler('q', (struct input_handler) {
671 register_render_component(&render_map);
673 damage_overlay_color = get_color("#F20000");
679 get_box_char(is_solid(x, y - 1), is_solid(x, y + 1), is_solid(x - 1, y), is_solid(x + 1, y));
681 const char *get_box_char(bool up, bool down, bool left, bool right)
683 if (left && right && ! up && ! down)
684 return "\u2501\u2501";
685 else if (up && down && ! right && ! left)
687 else if (down && right && ! up && ! left)
688 return "\u250F\u2501";
689 else if (down && left && ! up && ! right)
691 else if (up && right && ! down && ! left)
692 return "\u2517\u2501";
693 else if (up && left && ! down && ! right)
695 else if (up && down && right && ! left)
696 return "\u2523\u2501";
697 else if (up && down && left && ! right)
699 else if (down && left && right && ! up)
700 return "\u2533\u2501";
701 else if (up && left && right && ! down)
702 return "\u253B\u2501";
703 else if (up && down && left && right)
704 return "\u254b\u2501";
705 else if (left && ! up && ! down && ! right)
707 else if (up && ! down && ! left && ! right)
709 else if (right && ! up && ! down && ! left)
710 return "\u257A\u2501";
711 else if (down && ! up && ! left && ! right)
713 else if (! up && ! down && ! left && ! right)