xxxxxxxxxx
75
const MAP_GEN = new Dusa(`
# Inputs must be given
#demand width is _.
# Create a grid of regions, connected Manhattan-style
#builtin INT_MINUS minus
dim width.
dim (minus N 1) :- dim N, N > 1.
region (coord X Y) :- dim X, dim Y.
next_to (coord X Y) (coord (minus X 1) Y) :- dim X, dim Y, X > 1.
next_to (coord X Y) (coord X (minus Y 1)) :- dim X, dim Y, Y > 1.
next_to R1 R2 :- next_to R2 R1.
# Each region can be mountains, forest, or ocean
terrain R is { mountain, forest, ocean } :-
region R.
# Mountains can't be next to the ocean
terrain R1 is { forest, ocean } :-
next_to R1 R2,
terrain R2 is ocean.
# Spanning tree rooted at the start
parent (coord 1 1) is (coord 1 1).
parent R2 is? R1 :-
can_reach R1,
terrain R1 is forest,
next_to R1 R2.
can_reach R :- parent R is _.
# Connected components for oceans to ensure only one ocean
ocean_rep R is? R :- terrain R is ocean.
ocean_rep R2 is R :- ocean_rep R1 is R, next_to R1 R2, terrain R2 is ocean.
#forbid ocean_rep R1 is R, ocean_rep R2 is R', R != R'.
# You must be able to reach the lower-right, and ocean, and mountans
#demand can_reach (coord width width).
#demand can_reach R, terrain R is forest.
#demand can_reach R, terrain R is ocean.`);
const WIDTH = 6;
const CELL_SIZE = 50;
const REGIONS = {
forest: [71,191,107],
ocean: [77,176,209],
mountain: [152,157,158],
}
MAP_GEN.assert({name: 'width', value: WIDTH });
function setup() {
createCanvas(WIDTH * CELL_SIZE, WIDTH * CELL_SIZE);
}
function draw() {
const solution = MAP_GEN.sample();
text("🏠")
noStroke();
for (let x = 1; x <= WIDTH; x++) {
for (let y = 1; y <= WIDTH; y++) {
const { name: terrain } = solution.get("terrain", ({name: 'coord', args:[x, y]}));
fill(REGIONS[terrain]);
square((x-1)*CELL_SIZE, (y-1)*CELL_SIZE, CELL_SIZE);
}
}
stroke(0);
let next;
for (let coord = [WIDTH, WIDTH]; coord[0] > 1 || coord[1] > 1; coord = next) {
next = solution.get("parent", {name: 'coord', args: coord}).args;
line((coord[0]-0.5)*CELL_SIZE, (coord[1]-0.5)*CELL_SIZE, (next[0]-0.5)*CELL_SIZE, (next[1]-0.5)*CELL_SIZE);
}
}