|
|
@ -68,7 +68,7 @@ function setup() {
|
|
|
|
|
|
|
|
|
|
|
|
const startTime = new Date()
|
|
|
|
const startTime = new Date()
|
|
|
|
|
|
|
|
|
|
|
|
const handle = setInterval(() => {
|
|
|
|
const handle = window.setInterval(() => {
|
|
|
|
const time = new Date().getTime() - startTime.getTime()
|
|
|
|
const time = new Date().getTime() - startTime.getTime()
|
|
|
|
|
|
|
|
|
|
|
|
update(state, g.canvas.width, g.canvas.height, time)
|
|
|
|
update(state, g.canvas.width, g.canvas.height, time)
|
|
|
@ -264,16 +264,26 @@ const DIR_AVAILABLE_PREDICATE: Record<WireDirection, (pos: Point2, grid: Grid<Wi
|
|
|
|
!grid.has([x, y + 1]) &&
|
|
|
|
!grid.has([x, y + 1]) &&
|
|
|
|
implies(grid.has([x - 1, y]), () => grid.get([x - 1, y]) !== 'down-right') &&
|
|
|
|
implies(grid.has([x - 1, y]), () => grid.get([x - 1, y]) !== 'down-right') &&
|
|
|
|
implies(grid.has([x + 1, y]), () => grid.get([x + 1, y]) !== 'down-left'),
|
|
|
|
implies(grid.has([x + 1, y]), () => grid.get([x + 1, y]) !== 'down-left'),
|
|
|
|
['down-left']: ([x, y], grid) => !grid.has([x - 1, y + 1]) && implies(grid.has([x - 1, y]), () => grid.get([x - 1, y]) === 'down-left'),
|
|
|
|
['down-left']: ([x, y], grid) =>
|
|
|
|
|
|
|
|
!grid.has([x - 1, y + 1]) &&
|
|
|
|
|
|
|
|
implies(grid.has([x - 1, y]), () => grid.get([x - 1, y]) === 'down-left'),
|
|
|
|
['down-right']: ([x, y], grid) =>
|
|
|
|
['down-right']: ([x, y], grid) =>
|
|
|
|
!grid.has([x + 1, y + 1]) && implies(grid.has([x + 1, y]), () => grid.get([x + 1, y]) === 'down-right'),
|
|
|
|
!grid.has([x + 1, y + 1]) &&
|
|
|
|
|
|
|
|
implies(grid.has([x + 1, y]), () => grid.get([x + 1, y]) === 'down-right'),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function pruneDirections(grid: Grid<WireCell>, position: Point2, directions: WireDirection[]): WireDirection[] {
|
|
|
|
function pruneDirections(
|
|
|
|
|
|
|
|
grid: Grid<WireCell>,
|
|
|
|
|
|
|
|
position: Point2,
|
|
|
|
|
|
|
|
directions: WireDirection[]
|
|
|
|
|
|
|
|
): WireDirection[] {
|
|
|
|
return directions.filter(dir => DIR_AVAILABLE_PREDICATE[dir](position, grid))
|
|
|
|
return directions.filter(dir => DIR_AVAILABLE_PREDICATE[dir](position, grid))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function generateWire(grid: Grid<WireCell>, startingPoint: Point2): { position: Point2; direction: WireCell }[] {
|
|
|
|
function generateWire(
|
|
|
|
|
|
|
|
grid: Grid<WireCell>,
|
|
|
|
|
|
|
|
startingPoint: Point2
|
|
|
|
|
|
|
|
): { position: Point2; direction: WireCell }[] {
|
|
|
|
const segmentLength = Math.floor(1 - Math.random() ** 2) * 10 + 30
|
|
|
|
const segmentLength = Math.floor(1 - Math.random() ** 2) * 10 + 30
|
|
|
|
let currentPosition = startingPoint
|
|
|
|
let currentPosition = startingPoint
|
|
|
|
let currentDirection: WireDirection = randomChoice(['down', 'down', 'down', 'down-left', 'down-right'])
|
|
|
|
let currentDirection: WireDirection = randomChoice(['down', 'down', 'down', 'down-left', 'down-right'])
|
|
|
@ -281,7 +291,11 @@ function generateWire(grid: Grid<WireCell>, startingPoint: Point2): { position:
|
|
|
|
const steps: { position: Point2; direction: WireCell }[] = []
|
|
|
|
const steps: { position: Point2; direction: WireCell }[] = []
|
|
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < segmentLength; i++) {
|
|
|
|
for (let i = 0; i < segmentLength; i++) {
|
|
|
|
const availableDirections = pruneDirections(grid, currentPosition, ['down', 'down-left', 'down-right'])
|
|
|
|
const availableDirections = pruneDirections(grid, currentPosition, [
|
|
|
|
|
|
|
|
'down',
|
|
|
|
|
|
|
|
'down-left',
|
|
|
|
|
|
|
|
'down-right',
|
|
|
|
|
|
|
|
])
|
|
|
|
if (availableDirections.length === 0) {
|
|
|
|
if (availableDirections.length === 0) {
|
|
|
|
break
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -290,7 +304,10 @@ function generateWire(grid: Grid<WireCell>, startingPoint: Point2): { position:
|
|
|
|
? currentDirection
|
|
|
|
? currentDirection
|
|
|
|
: randomChoice(availableDirections)
|
|
|
|
: randomChoice(availableDirections)
|
|
|
|
|
|
|
|
|
|
|
|
if ((currentDirection === 'down-left' && dir === 'down-right') || (currentDirection === 'down-right' && dir === 'down-left')) {
|
|
|
|
if (
|
|
|
|
|
|
|
|
(currentDirection === 'down-left' && dir === 'down-right') ||
|
|
|
|
|
|
|
|
(currentDirection === 'down-right' && dir === 'down-left')
|
|
|
|
|
|
|
|
) {
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|