14 /* Shared variables */
16 struct color black = {0};
20 struct material outside;
22 struct node map[MAP_WIDTH][MAP_HEIGHT];
24 struct list *entities = & (struct list) {
29 /* Private variables */
31 struct entity *entity_collision_map[MAP_WIDTH][MAP_HEIGHT] = {{NULL}};
33 static bool running = true;
34 static double damage_overlay = 0.0;
35 static struct color damage_overlay_color;
37 static struct list *air_functions = NULL;
38 static struct input_handler *input_handlers[256] = {NULL};
39 static struct entity *render_entities[LIGHT * 2 + 1][LIGHT * 2 + 1];
40 static struct list *render_components = NULL;
41 static struct list *globalsteps = NULL;
43 /* Helper functions */
45 struct color get_color(const char *str)
48 sscanf(str, "#%2x%2x%2x", &r, &g, &b);
49 return (struct color) {r, g, b};
52 void set_color(struct color color, bool bg)
54 printf("\e[%u;2;%u;%u;%um", bg ? 48 : 38, color.r, color.g, color.b);
57 void light_color(struct color *color, double light)
64 void mix_color(struct color *color, struct color other, double ratio)
66 double ratio_total = ratio + 1;
68 color->r = (color->r + other.r * ratio) / ratio_total;
69 color->g = (color->g + other.g * ratio) / ratio_total;
70 color->b = (color->b + other.b * ratio) / ratio_total;
73 void dir_to_xy(enum direction dir, int *x, int *y)
91 struct list *add_element(struct list *list, void *element)
95 for (ptr = &list; *ptr != NULL; ptr = &(*ptr)->next)
98 *ptr = malloc(sizeof(struct list));
99 (*ptr)->element = element;
105 int clamp(int v, int min, int max)
107 return v < min ? min : v > max ? max : v;
110 int max(int a, int b)
112 return a > b ? a : b;
115 int min(int a, int b)
117 return a < b ? a : b;
120 void *make_buffer(void *ptr, size_t size)
122 void *buf = malloc(size);
123 memcpy(buf, ptr, size);
128 /* Game-related utility functions */
137 return player.health <= 0;
142 struct node get_node(int x, int y)
144 return is_outside(x, y) ? (struct node) {&outside} : map[x][y];
147 bool is_outside(int x, int y)
149 return x >= MAP_WIDTH || x < 0 || y >= MAP_HEIGHT || y < 0;
152 bool is_solid(int x, int y)
154 return get_node(x, y).material->solid;
157 /* Entity functions */
159 bool spawn(struct entity def, int x, int y, void *data)
164 if (def.collide_with_entities && entity_collision_map[x][y])
170 struct entity *entity = malloc(sizeof(struct entity));
173 add_element(entities, entity);
175 if (entity->collide_with_entities)
176 entity_collision_map[x][y] = entity;
178 if (entity->on_spawn)
179 entity->on_spawn(entity, data);
184 bool move(struct entity *entity, int xoff, int yoff)
188 x = entity->x + xoff;
189 y = entity->y + yoff;
191 if (is_solid(x, y)) {
192 if (entity->on_collide)
193 entity->on_collide(entity, x, y);
195 } else if (entity->collide_with_entities && entity_collision_map[x][y]) {
196 if (entity->on_collide_with_entity)
197 entity->on_collide_with_entity(entity, entity_collision_map[x][y]);
200 entity_collision_map[entity->x][entity->y] = NULL;
203 entity_collision_map[entity->x][entity->y] = entity;
208 void add_health(struct entity *entity, int health)
210 bool was_alive = entity->health > 0;
212 entity->health += health;
214 if (health < 0 && entity->on_damage)
215 entity->on_damage(entity, -health);
217 if (entity->health > entity->max_health)
218 entity->health = entity->max_health;
219 else if (entity->health <= 0 && was_alive && entity->on_death)
220 entity->on_death(entity);
223 /* Register callback functions */
225 void register_air_function(struct generator_function func)
227 air_functions = add_element(air_functions, make_buffer(&func, sizeof(struct generator_function)));
230 void register_input_handler(unsigned char c, struct input_handler handler)
232 if (input_handlers[c])
235 input_handlers[c] = make_buffer(&handler, sizeof(struct input_handler));
238 void register_render_component(void (*callback)(struct winsize ws))
240 render_components = add_element(render_components, callback);
243 void register_globalstep(struct globalstep step)
245 globalsteps = add_element(globalsteps, make_buffer(&step, sizeof(struct globalstep)));
250 static void player_death(struct entity *self)
252 self->texture = "☠ ";
255 static void player_damage(struct entity *self, int damage)
257 damage_overlay += (double) damage * 0.5;
260 struct entity player = {
271 .collide_with_entities = true,
275 .on_collide_with_entity = NULL,
278 .on_death = &player_death,
279 .on_damage = &player_damage,
284 static void mapgen_set_air(int x, int y)
286 if (is_outside(x, y))
289 if (map[x][y].material == &air)
292 map[x][y] = (struct node) {&air};
294 for (struct list *ptr = air_functions; ptr != NULL; ptr = ptr->next) {
295 struct generator_function *func = ptr->element;
297 if (rand() % func->chance == 0)
298 func->callback(x, y);
302 static void generate_room(int origin_x, int origin_y)
304 int left = 5 + rand() % 10;
305 int right = 5 + rand() % 10;
310 for (int x = -left; x <= right; x++) {
319 for (int y = -up; y <= down; y++)
320 mapgen_set_air(origin_x + x, origin_y + y);
324 static bool check_direction(int x, int y, enum direction dir)
327 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);
329 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);
332 static void generate_corridor(int lx, int ly, enum direction ldir)
334 if (is_outside(lx, ly))
337 if (rand() % 200 == 0) {
338 generate_room(lx, ly);
342 mapgen_set_air(lx, ly);
347 enum direction ret = (ldir + 2) % 4;
360 dir_to_xy(dir, &x, &y);
361 } while (dir == ret || (! check_direction(x, y, dir) && --limit));
364 generate_corridor(x, y, dir);
366 if (rand() % 20 == 0)
367 generate_corridor(lx, ly, ldir);
370 static void generate_corridor_random(int x, int y)
372 enum direction dir = rand() % 4;
374 generate_corridor(x, y, dir);
375 generate_corridor(x, y, (dir + 2) % 4);
380 static bool render_color(struct color color, double light, bool bg, bool use_color)
383 set_color(black, bg);
385 } else if (use_color) {
386 if (damage_overlay > 0.0)
387 mix_color(&color, damage_overlay_color, damage_overlay * 2.0);
389 light_color(&color, light);
391 set_color(color, bg);
398 static void render_map(struct winsize ws)
400 int cols = ws.ws_col / 2 - LIGHT * 2;
401 int rows = ws.ws_row / 2 - LIGHT;
403 int cols_left = ws.ws_col - cols - (LIGHT * 2 + 1) * 2;
404 int rows_left = ws.ws_row - rows - (LIGHT * 2 + 1);
406 for (int i = 0; i < rows; i++)
407 for (int i = 0; i < ws.ws_col; i++)
410 for (int y = -LIGHT; y <= LIGHT; y++) {
411 for (int i = 0; i < cols; i++)
414 for (int x = -LIGHT; x <= LIGHT; x++) {
417 map_x = x + player.x;
418 map_y = y + player.y;
420 struct node node = get_node(map_x, map_y);
422 double dist = sqrt(x * x + y * y);
423 double light = 1.0 - (double) dist / (double) LIGHT;
425 render_color(node.material->color, light, true, true);
427 struct entity *entity = render_entities[x + LIGHT][y + LIGHT];
428 render_entities[x + LIGHT][y + LIGHT] = NULL;
430 if (entity && render_color(entity->color, light, false, entity->use_color))
431 printf("%s", entity->texture);
436 set_color(black, true);
438 for (int i = 0; i < cols_left; i++)
442 for (int i = 0; i < rows_left; i++)
443 for (int i = 0; i < ws.ws_col; i++)
452 ioctl(STDIN_FILENO, TIOCGWINSZ, &ws);
454 for (struct list *ptr = render_components; ptr != NULL; ptr = ptr->next) {
455 printf("\e[0m\e[0;0H");
456 set_color(black, true);
458 ((void (*)(struct winsize ws)) ptr->element)(ws);
466 static void handle_interrupt(int signal)
473 static void handle_input(unsigned char c)
475 struct input_handler *handler = input_handlers[c];
477 if (handler && (handler->run_if_dead || ! player_dead()))
481 static void *input_thread(void *unused)
486 handle_input(tolower(fgetc(stdin)));
496 sa.sa_handler = &handle_interrupt;
497 sigaction(SIGINT, &sa, NULL);
499 generate_corridor_random(player.x, player.y);
501 for (int i = 0; i < 50; i++)
502 generate_corridor_random(rand() % MAP_WIDTH, rand() % MAP_HEIGHT);
504 struct termios oldtio, newtio;
505 tcgetattr(STDIN_FILENO, &oldtio);
507 newtio.c_lflag &= ~(ICANON | ECHO);
508 tcsetattr(STDIN_FILENO, TCSANOW, &newtio);
510 printf("\e[?1049h\e[?25l");
512 pthread_t input_thread_id;
513 pthread_create(&input_thread_id, NULL, &input_thread, NULL);
515 struct timespec ts, ts_old;
516 clock_gettime(CLOCK_REALTIME, &ts_old);
519 clock_gettime(CLOCK_REALTIME, &ts);
520 double dtime = (double) (ts.tv_sec - ts_old.tv_sec) + (double) (ts.tv_nsec - ts_old.tv_nsec) / 1000000000.0;
523 bool dead = player_dead();
525 for (struct list *ptr = globalsteps; ptr != NULL; ptr = ptr->next) {
526 struct globalstep *step = ptr->element;
528 if (step->run_if_dead || ! dead)
529 step->callback(dtime);
532 if (! dead && damage_overlay > 0.0) {
533 damage_overlay -= dtime;
535 if (damage_overlay < 0.0)
536 damage_overlay = 0.0;
539 for (struct list **ptr = &entities; *ptr != NULL; ) {
540 struct entity *entity = (*ptr)->element;
542 if (entity->remove) {
543 assert(entity != &player);
544 struct list *next = (*ptr)->next;
546 if (entity->on_remove)
547 entity->on_remove(entity);
552 if (entity->collide_with_entities)
553 entity_collision_map[entity->x][entity->y] = NULL;
564 dx = entity->x - player.x;
565 dy = entity->y - player.y;
567 bool visible = abs(dx) <= LIGHT && abs(dy) <= LIGHT;
570 render_entities[dx + LIGHT][dy + LIGHT] = entity;
572 if (! dead && entity->on_step)
573 entity->on_step(entity, (struct entity_step_data) {
585 // there is no such thing as glfwSwapBuffers, so we just wait 1 / 60 seconds to prevent artifacts
586 usleep(1000000 / 60);
589 printf("\e[?1049l\e[?25h");
590 tcsetattr(STDIN_FILENO, TCSANOW, &oldtio);
593 /* Initializer function */
595 __attribute__ ((constructor)) static void init()
599 wall = (struct material) {
601 .color = get_color("#5B2F00"),
604 air = (struct material) {
606 .color = get_color("#FFE027"),
609 outside = (struct material) {
614 entity_collision_map[player.x][player.y] = &player;
616 for (int x = 0; x < MAP_WIDTH; x++)
617 for (int y = 0; y < MAP_HEIGHT; y++)
618 map[x][y] = (struct node) {&wall};
620 register_input_handler('q', (struct input_handler) {
625 register_render_component(&render_map);
627 damage_overlay_color = get_color("#F20000");
633 get_box_char(is_solid(x, y - 1), is_solid(x, y + 1), is_solid(x - 1, y), is_solid(x + 1, y));
635 const char *get_box_char(bool up, bool down, bool left, bool right)
637 if (left && right && ! up && ! down)
638 return "\u2501\u2501";
639 else if (up && down && ! right && ! left)
641 else if (down && right && ! up && ! left)
642 return "\u250F\u2501";
643 else if (down && left && ! up && ! right)
645 else if (up && right && ! down && ! left)
646 return "\u2517\u2501";
647 else if (up && left && ! down && ! right)
649 else if (up && down && right && ! left)
650 return "\u2523\u2501";
651 else if (up && down && left && ! right)
653 else if (down && left && right && ! up)
654 return "\u2533\u2501";
655 else if (up && left && right && ! down)
656 return "\u253B\u2501";
657 else if (up && down && left && right)
658 return "\u254b\u2501";
659 else if (left && ! up && ! down && ! right)
661 else if (up && ! down && ! left && ! right)
663 else if (right && ! up && ! down && ! left)
664 return "\u257A\u2501";
665 else if (down && ! up && ! left && ! right)
667 else if (! up && ! down && ! left && ! right)