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 /* Game-related utility functions */
141 return player.health <= 0;
146 struct node get_node(int x, int y)
148 return is_outside(x, y) ? (struct node) {&outside} : map[x][y];
151 bool is_outside(int x, int y)
153 return x >= MAP_WIDTH || x < 0 || y >= MAP_HEIGHT || y < 0;
156 bool is_solid(int x, int y)
158 return get_node(x, y).material->solid;
161 /* Entity functions */
163 bool spawn(struct entity def, int x, int y, void *data)
168 if (def.collide_with_entities && entity_collision_map[x][y])
174 struct entity *entity = malloc(sizeof(struct entity));
177 add_element(entities, entity);
179 if (entity->collide_with_entities)
180 entity_collision_map[x][y] = entity;
182 if (entity->on_spawn)
183 entity->on_spawn(entity, data);
188 bool move(struct entity *entity, int xoff, int yoff)
192 x = entity->x + xoff;
193 y = entity->y + yoff;
195 if (is_solid(x, y)) {
196 if (entity->on_collide)
197 entity->on_collide(entity, x, y);
199 } else if (entity->collide_with_entities && entity_collision_map[x][y]) {
200 if (entity->on_collide_with_entity)
201 entity->on_collide_with_entity(entity, entity_collision_map[x][y]);
204 entity_collision_map[entity->x][entity->y] = NULL;
207 entity_collision_map[entity->x][entity->y] = entity;
212 void add_health(struct entity *entity, int health)
214 bool was_alive = entity->health > 0;
216 entity->health += health;
218 if (health < 0 && entity->on_damage)
219 entity->on_damage(entity, -health);
221 if (entity->health > entity->max_health)
222 entity->health = entity->max_health;
223 else if (entity->health <= 0 && was_alive && entity->on_death)
224 entity->on_death(entity);
227 /* Register callback functions */
229 void register_air_function(struct generator_function func)
231 air_functions = add_element(air_functions, make_buffer(&func, sizeof(struct generator_function)));
234 void register_input_handler(unsigned char c, struct input_handler handler)
236 if (input_handlers[c])
239 input_handlers[c] = make_buffer(&handler, sizeof(struct input_handler));
242 void register_render_component(void (*callback)(struct winsize ws))
244 render_components = add_element(render_components, callback);
247 void register_globalstep(struct globalstep step)
249 globalsteps = add_element(globalsteps, make_buffer(&step, sizeof(struct globalstep)));
254 static void player_death(struct entity *self)
256 self->texture = "☠ ";
259 static void player_damage(struct entity *self, int damage)
261 damage_overlay += (double) damage * 0.5;
264 struct entity player = {
275 .collide_with_entities = true,
279 .on_collide_with_entity = NULL,
282 .on_death = &player_death,
283 .on_damage = &player_damage,
288 static void mapgen_set_air(int x, int y)
290 if (is_outside(x, y))
293 if (map[x][y].material == &air)
296 map[x][y] = (struct node) {&air};
298 for (struct list *ptr = air_functions; ptr != NULL; ptr = ptr->next) {
299 struct generator_function *func = ptr->element;
301 if (rand() % func->chance == 0)
302 func->callback(x, y);
306 static void generate_room(int origin_x, int origin_y)
308 int left = 5 + rand() % 10;
309 int right = 5 + rand() % 10;
314 for (int x = -left; x <= right; x++) {
323 for (int y = -up; y <= down; y++)
324 mapgen_set_air(origin_x + x, origin_y + y);
328 static bool check_direction(int x, int y, enum direction dir)
331 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);
333 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);
336 static void generate_corridor(int lx, int ly, enum direction ldir)
338 if (is_outside(lx, ly))
341 if (rand() % 200 == 0) {
342 generate_room(lx, ly);
346 mapgen_set_air(lx, ly);
351 enum direction ret = (ldir + 2) % 4;
364 dir_to_xy(dir, &x, &y);
365 } while (dir == ret || (! check_direction(x, y, dir) && --limit));
368 generate_corridor(x, y, dir);
370 if (rand() % 20 == 0)
371 generate_corridor(lx, ly, ldir);
374 static void generate_corridor_random(int x, int y)
376 enum direction dir = rand() % 4;
378 generate_corridor(x, y, dir);
379 generate_corridor(x, y, (dir + 2) % 4);
384 static bool render_color(struct color color, double light, bool bg, bool use_color)
387 set_color(black, bg);
389 } else if (use_color) {
390 if (damage_overlay > 0.0)
391 mix_color(&color, damage_overlay_color, damage_overlay * 2.0);
393 light_color(&color, light);
395 set_color(color, bg);
402 static void render_map(struct winsize ws)
404 int cols = ws.ws_col / 2 - LIGHT * 2;
405 int rows = ws.ws_row / 2 - LIGHT;
407 int cols_left = ws.ws_col - cols - (LIGHT * 2 + 1) * 2;
408 int rows_left = ws.ws_row - rows - (LIGHT * 2 + 1);
410 for (int i = 0; i < rows; i++)
411 for (int i = 0; i < ws.ws_col; i++)
414 for (int y = -LIGHT; y <= LIGHT; y++) {
415 for (int i = 0; i < cols; i++)
418 for (int x = -LIGHT; x <= LIGHT; x++) {
421 map_x = x + player.x;
422 map_y = y + player.y;
424 struct node node = get_node(map_x, map_y);
426 double dist = sqrt(x * x + y * y);
427 double light = 1.0 - (double) dist / (double) LIGHT;
429 render_color(node.material->color, light, true, true);
431 struct entity *entity = render_entities[x + LIGHT][y + LIGHT];
432 render_entities[x + LIGHT][y + LIGHT] = NULL;
434 if (entity && render_color(entity->color, light, false, entity->use_color))
435 printf("%s", entity->texture);
440 set_color(black, true);
442 for (int i = 0; i < cols_left; i++)
446 for (int i = 0; i < rows_left; i++)
447 for (int i = 0; i < ws.ws_col; i++)
456 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws);
458 for (struct list *ptr = render_components; ptr != NULL; ptr = ptr->next) {
459 printf("\e[0m\e[0;0H");
460 set_color(black, true);
462 ((void (*)(struct winsize ws)) ptr->element)(ws);
470 static void handle_interrupt(int signal)
477 static void handle_input(unsigned char c)
479 struct input_handler *handler = input_handlers[c];
481 if (handler && (handler->run_if_dead || ! player_dead()))
485 static void *input_thread(void *unused)
490 handle_input(tolower(fgetc(stdin)));
500 sa.sa_handler = &handle_interrupt;
501 sigaction(SIGINT, &sa, NULL);
503 generate_corridor_random(player.x, player.y);
505 for (int i = 0; i < 50; i++)
506 generate_corridor_random(rand() % MAP_WIDTH, rand() % MAP_HEIGHT);
508 struct termios oldtio, newtio;
509 tcgetattr(STDIN_FILENO, &oldtio);
511 newtio.c_lflag &= ~(ICANON | ECHO);
512 tcsetattr(STDIN_FILENO, TCSANOW, &newtio);
514 printf("\e[?1049h\e[?25l");
516 pthread_t input_thread_id;
517 pthread_create(&input_thread_id, NULL, &input_thread, NULL);
519 struct timespec ts, ts_old;
520 clock_gettime(CLOCK_REALTIME, &ts_old);
523 clock_gettime(CLOCK_REALTIME, &ts);
524 double dtime = calculate_dtime(ts_old, ts);
527 bool dead = player_dead();
529 for (struct list *ptr = globalsteps; ptr != NULL; ptr = ptr->next) {
530 struct globalstep *step = ptr->element;
532 if (step->run_if_dead || ! dead)
533 step->callback(dtime);
536 if (! dead && damage_overlay > 0.0) {
537 damage_overlay -= dtime;
539 if (damage_overlay < 0.0)
540 damage_overlay = 0.0;
543 for (struct list **ptr = &entities; *ptr != NULL; ) {
544 struct entity *entity = (*ptr)->element;
546 if (entity->remove) {
547 assert(entity != &player);
548 struct list *next = (*ptr)->next;
550 if (entity->on_remove)
551 entity->on_remove(entity);
556 if (entity->collide_with_entities)
557 entity_collision_map[entity->x][entity->y] = NULL;
568 dx = entity->x - player.x;
569 dy = entity->y - player.y;
571 bool visible = abs(dx) <= LIGHT && abs(dy) <= LIGHT;
574 render_entities[dx + LIGHT][dy + LIGHT] = entity;
576 if (! dead && entity->on_step)
577 entity->on_step(entity, (struct entity_step_data) {
589 // there is no such thing as glfwSwapBuffers, so we just wait 1 / 60 seconds to prevent artifacts
590 usleep(1000000 / 60);
593 printf("\e[?1049l\e[?25h");
594 tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
597 /* Initializer function */
599 __attribute__ ((constructor)) static void init()
603 wall = (struct material) {
605 .color = get_color("#5B2F00"),
608 air = (struct material) {
610 .color = get_color("#FFE027"),
613 outside = (struct material) {
618 entity_collision_map[player.x][player.y] = &player;
620 for (int x = 0; x < MAP_WIDTH; x++)
621 for (int y = 0; y < MAP_HEIGHT; y++)
622 map[x][y] = (struct node) {&wall};
624 register_input_handler('q', (struct input_handler) {
629 register_render_component(&render_map);
631 damage_overlay_color = get_color("#F20000");
637 get_box_char(is_solid(x, y - 1), is_solid(x, y + 1), is_solid(x - 1, y), is_solid(x + 1, y));
639 const char *get_box_char(bool up, bool down, bool left, bool right)
641 if (left && right && ! up && ! down)
642 return "\u2501\u2501";
643 else if (up && down && ! right && ! left)
645 else if (down && right && ! up && ! left)
646 return "\u250F\u2501";
647 else if (down && left && ! up && ! right)
649 else if (up && right && ! down && ! left)
650 return "\u2517\u2501";
651 else if (up && left && ! down && ! right)
653 else if (up && down && right && ! left)
654 return "\u2523\u2501";
655 else if (up && down && left && ! right)
657 else if (down && left && right && ! up)
658 return "\u2533\u2501";
659 else if (up && left && right && ! down)
660 return "\u253B\u2501";
661 else if (up && down && left && right)
662 return "\u254b\u2501";
663 else if (left && ! up && ! down && ! right)
665 else if (up && ! down && ! left && ! right)
667 else if (right && ! up && ! down && ! left)
668 return "\u257A\u2501";
669 else if (down && ! up && ! left && ! right)
671 else if (! up && ! down && ! left && ! right)