domino-dungeon/domino.c

76 lines
2.5 KiB
C
Raw Normal View History

2025-10-18 17:32:37 +02:00
#include <stddef.h>
#include <stdio.h>
2025-10-14 15:09:17 +02:00
#include <stdlib.h>
#include "domino.h"
2025-10-18 17:32:37 +02:00
#define DIRECTIONS 6
const struct eye direction[DIRECTIONS] = {
{.x = 0, .y = -1},
{.x = -2, .y = 0},
{.x = 1, .y = 0},
{.x = 0, .y = 1},
{.x = -1, .y = -1},
{.x = -1, .y = 1},
};
2025-10-14 15:09:17 +02:00
void bricks_append(struct bricks *bricks, struct brick brick) {
if (bricks->count+1 > bricks->capacity) {
if (bricks->capacity == 0) bricks->capacity = 256;
while (bricks->count+1 > bricks->capacity) bricks->capacity *= 2;
bricks->items.brick = realloc(bricks->items.brick, bricks->capacity * sizeof(*bricks->items.brick));
}
bricks->items.brick[bricks->count++] = brick;
}
2025-10-18 17:32:37 +02:00
void brick_print(const struct brick b) {
struct eye *eyes = (struct eye*) &b;
for (size_t i = 0; i < 2; i++) {
printf("{.x = %d, .y = %d, .val = %d, .vertical = %d}" , eyes[i].x, eyes[i].y, eyes[i].val, eyes[i].vertical); }
printf("\n");
}
void brick_previews(const struct brick active, const struct bricks bricks, struct bricks *preview) {
preview->count = 0;
for (size_t i = 0; i < bricks.count * 2; i++) {
struct eye e = bricks.items.eye[i];
if (e.val != active.front.val && e.val != active.back.val) continue;
struct brick p = active;
p.front.x = e.x;
p.front.y = e.y;
p.back.x = e.x + !active.front.vertical;
p.back.y = e.y + active.front.vertical;
struct brick previews[DIRECTIONS] = {0};
for (size_t ii = 0; ii < DIRECTIONS; ii++) {
int offset_x = active.front.vertical ? direction[ii].y : direction[ii].x;
int offset_y = active.front.vertical ? direction[ii].x : direction[ii].y;
previews[ii] = p;
previews[ii].front.x += offset_x;
previews[ii].front.y += offset_y;
previews[ii].front.val = 0;
previews[ii].back.x += offset_x;
previews[ii].back.y += offset_y;
for (size_t iii = 0; iii < bricks.count * 2; iii++) {
previews[ii].front.val |= (
(bricks.items.eye[iii].x == previews[ii].back.x && bricks.items.eye[iii].y == previews[ii].back.y) ||
(bricks.items.eye[iii].x == previews[ii].front.x && bricks.items.eye[iii].y == previews[ii].front.y)
|| (
(active.front.val != active.back.val) &&
((e.val == active.front.val && direction[ii].x < 0) || (e.val == active.back.val && direction[ii].x >= 0))
)
);
}
2025-10-21 14:57:37 +02:00
if (!previews[ii].front.val) {
previews[ii].front.val = p.front.val;
2025-10-18 17:32:37 +02:00
bricks_append(preview, previews[ii]);
2025-10-21 14:57:37 +02:00
}
2025-10-18 17:32:37 +02:00
}
}
}