protomine/src/grid/random_walker.c

58 lines
832 B
C

#include "grid.h"
#include "tiles.h"
#include <gint/std/stdlib.h>
void
grid_random_walker(struct Grid *restrict grid)
{
int x;
int y;
int i;
int roll;
/* fill with walls */
i = grid->width * grid->height;
while (i-- > 0)
grid->data[i] = TILE_SOLID;
/* dig our way to the right */
x = 0;
y = grid->height / 2;
while (x < grid->width) {
roll = rand() % 8;
grid_set(grid, x, y, roll ? TILE_VOID : TILE_OBSTACLE_3);
roll = rand() % 9;
switch (roll) {
case 0:
case 1:
case 2:
/* right */
x += 1;
break;
case 3:
case 4:
/* up */
y -= 1;
if (y < 0)
y = grid->height - 1;
break;
case 5:
case 6:
/* down */
y += 1;
if (y >= grid->height)
y = 0;
break;
case 7:
case 8:
/* left */
x -= 1;
if (x < 0)
x = 0;
break;
default:
break;
}
}
}