Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,67 @@
"use strict"
const _ = require('lodash');
const WIDTH_ARGUMENT_POSITION = 2;
const HEIGHT_ARGUMENT_POSITION = 3;
const TREE_PROBABILITY = 0.5;
const NEW_TREE_PROBABILITY = 0.01;
const BURN_PROBABILITY = 0.0001;
const CONSOLE_RED = '\x1b[31m';
const CONSOLE_GREEN = '\x1b[32m';
const CONSOLE_COLOR_CLOSE = '\x1b[91m';
const CONSOLE_CLEAR = '\u001B[2J\u001B[0;0f';
const NEIGHBOURS = [
[-1, -1],
[-1, 0],
[-1, 1],
[ 0, -1],
[ 0, 1],
[ 1, -1],
[ 1, 0],
[ 1, 1]
];
const PRINT_DECODE = {
' ': ' ',
'T': `${CONSOLE_GREEN}T${CONSOLE_COLOR_CLOSE}`,
'B': `${CONSOLE_RED}T${CONSOLE_COLOR_CLOSE}`,
};
const CONDITIONS = {
'T': (forest, y, x) => Math.random() < BURN_PROBABILITY || burningNeighbour(forest, y, x) ? 'B' : 'T',
' ': () => Math.random() < NEW_TREE_PROBABILITY ? 'T' : ' ',
'B': () => ' '
};
const WIDTH = process.argv[WIDTH_ARGUMENT_POSITION] || 20;
const HEIGHT = process.argv[HEIGHT_ARGUMENT_POSITION] || 10;
const update = forest => {
return _.map(forest, (c, ci) => {
return _.map(c, (r, ri) => {
return CONDITIONS[r](forest, ci, ri);
});
});
}
const printForest = forest => {
process.stdout.write(CONSOLE_CLEAR);
_.each(forest, c => {
_.each(c, r => {
process.stdout.write(PRINT_DECODE[r]);
});
process.stdout.write('\n');
})
}
const burningNeighbour = (forest, y, x) => {
return _(NEIGHBOURS)
.map(n => _.isUndefined(forest[y + n[0]]) ? null : forest[y + n[0]][x + n[1]])
.any(_.partial(_.isEqual, 'B'));
};
let forest = _.times(HEIGHT, () => _.times(WIDTH, () => Math.random() < TREE_PROBABILITY ? 'T' : ' '));
setInterval(() => {
forest = update(forest);
printForest(forest)
}, 20);

View file

@ -0,0 +1,63 @@
var forest = {
X: 50,
Y: 50,
propTree: 0.5,
propTree2: 0.01,
propBurn: 0.0001,
t: [],
c: ['rgb(255,255,255)', 'rgb(0,255,0)', 'rgb(255,0,0)']
};
for(var i = 0; i < forest.Y; i++) {
forest.t[i] = [];
for(var j = 0; j < forest.Y; j++) {
forest.t[i][j] = Math.random() < forest.propTree ? 1 : 0;
}
}
function afterLoad(forest) {
var canvas = document.getElementById('canvas');
var c = canvas.getContext('2d');
for(var i = 0; i < forest.X; i++) {
for(var j = 0; j < forest.Y; j++) {
c.fillStyle = forest.c[forest.t[i][j]];
c.fillRect(10*j, 10*i, 10*j+9, 10*i+9);
}
}
}
function doStep(forest) {
var to = [];
for(var i = 0; i < forest.Y; i++) {
to[i] = forest.t[i].slice(0);
}
//indices outside the array are undefined; which converts to 0=empty on forced typecast
for(var i = 0; i < forest.Y; i++) {
for(var j = 0; j < forest.Y; j++) {
if(0 == to[i][j]) {
forest.t[i][j] = Math.random() < forest.propTree2 ? 1 : 0;
} else if(1 == to[i][j]) {
if(
((i>0) && (2 == to[i-1][j])) ||
((i<forest.Y-1) && (2 == to[i+1][j])) ||
((j>0) && (2 == to[i][j-1])) ||
((j<forest.X-1) && (2 == to[i][j+1]))
) {
forest.t[i][j] = 2;
} else {
forest.t[i][j] = Math.random() < forest.propBurn ? 2 : 1;
}
} else if(2 == to[i][j]) {
//If it burns, it gets empty ...
forest.t[i][j] = 0;
}
}
}
}
window.setInterval(function(){
doStep(forest);
afterLoad(forest);
}, 100);

View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Forest Fire</title>
</head>
<body>
<canvas id="canvas" width="500" height="500">
Your browser doesn't support HTML5 Canvas.
</canvas>
<script language="JavaScript">//<![CDATA[<!--
// --> HERE COMES THE SCRIPT FROM ABOVE <--
//-->]]></script>
</body>
</html>