Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@derjanb
Created April 1, 2015 22:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save derjanb/540f04b9b2304452e6f8 to your computer and use it in GitHub Desktop.
Save derjanb/540f04b9b2304452e6f8 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name LE
// @namespace http://your.homepage/
// @version 0.1
// @description LE
// @author You
// @match http://tampermonkey.net/empty.html
// @grant unsafeWindow
// ==/UserScript==
/* global console: true*/
'use strict';
var V = false;
var D = true;
var slow = true;
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
document.body.appendChild(canvas);
var sceene = { x: 250, y: 250, w: 500, h: 500, scale: 1 };
canvas.height = sceene.h * sceene.scale;
canvas.width = sceene.w * sceene.scale;
var world = {};
var past = {};
var render = function() {
context.clearRect ( 0 , 0 , canvas.width, canvas.height );
var id = context.getImageData(0, 0, canvas.width, canvas.height);
var dP = function(x, y, z, r, g, b) {
var index = (x + y * canvas.width) * 4;
id.data[index + 0] = r; //r;
id.data[index + 1] = g; //g;
id.data[index + 2] = b; //b;
id.data[index + 3] = 255; //a;
};
Object.getOwnPropertyNames(world).forEach(function(k) {
var l = world[k];
var p = past[k];
dP(l.x, l.y, l.z, p ? 185 : 100, p ? 185 : 0, p ? 185 : 0);
});
context.putImageData(id, 0, 0);
};
var key = function(_x, _y, _z) {
return [_x, _y, _z].join(':');
};
var find = function(k) {
return world[k];
};
var new_life = function(x, y, z, e ) {
if (!e) e = 1;
return { x: x, y: y, z: z, e: e };
};
var neighbors = function(_x,_y,_z, s) {
var c = { a: [], d: {} };
for (var x=-1;x<=1;x++) {
for (var y=-1;y<=1;y++) {
var z = 0;
//for (var z=-1;z<=1;z++) {
if (!x && !y && !z) continue;
var nx = _x + x;
var ny = _y + y;
var nz = _z + z;
var k = key(nx, ny, nz);
var o = find(k);
if (V) console.log('search @ ', k, o);
if (o) {
c.a.push(o);
} else if (s) {
c.d[k] = new_life(nx, ny, nz);
}
//}
}
}
return c;
};
var cont = 0;
var run = function() {
var del = [];
var add = {};
var seed = {};
var new_past = {};
Object.getOwnPropertyNames(world).forEach(function(k) {
var l = world[k];
new_past[k] = true;
var n = neighbors(l.x, l.y, l.z, true);
if (V) console.log('check world', k, n);
if (n.a.length < 2 || n.a.length > 3) {
del.push(k);
}
Object.getOwnPropertyNames(n.d).forEach(function(k) {
seed[k] = n.d[k];
});
});
var p = !!del.length;
Object.getOwnPropertyNames(seed).forEach(function(k) {
var s = seed[k];
if (s.x == 35 && s.y == 3) debugger;
var n = neighbors(s.x, s.y, s.z);
if (V) console.log('check seed', k, n);
if (n.a.length === 3) {
add[k] = s;
}
});
del.forEach(function (d) {
if (V) console.log('delete', d);
delete world[d];
});
Object.getOwnPropertyNames(add).forEach(function(k) {
var s = add[k];
world[k] = s;
if (V) console.log('create', k, s);
p = true;
});
if (slow || cont++ > 100) {
cont = 0;
render();
past = new_past;
if (p) {
window.setTimeout(run, 1);
// random_fill(10);
} else {
window.alert('The end!');
}
} else {
run();
}
};
var random_fill = function(c, f, v) {
if (f === undefined) f = 1000;
if (v === undefined) v = 100;
var g = c / f;
while (g-- > 0) {
var i = f;
var gx = Math.floor(Math.random() * canvas.width) + 1;
var gy = Math.floor(Math.random() * canvas.height) + 1;
var gz = 1;
while (i-- > 0) {
var x = gx + Math.floor(Math.random() * v) + 1;
var y = gy + Math.floor(Math.random() * v) + 1;
var z = gz + 0;
world[key(x, y ,z)] = new_life(x, y, z);
}
}
};
if (1) {
world = {};
[
[
{ x: 2, y: 3 },
{ x: 3, y: 3 },
{ x: 4, y: 3 }
],
[
/* gosper_glider */
{ x: 2 , y: 6 },
{ x: 2 , y: 7 },
{ x: 3 , y: 6 },
{ x: 3 , y: 7 },
{ x: 12, y: 6 },
{ x: 12, y: 7 },
{ x: 12, y: 8 },
{ x: 13, y: 5 },
{ x: 13, y: 9 },
{ x: 14, y: 4 },
{ x: 14, y: 10 },
{ x: 15, y: 4 },
{ x: 15, y: 10 },
{ x: 16, y: 7 },
{ x: 17, y: 5 },
{ x: 17, y: 9 },
{ x: 18, y: 6 },
{ x: 18, y: 7 },
{ x: 18, y: 8 },
{ x: 19, y: 7 },
{ x: 22, y: 4 },
{ x: 22, y: 5 },
{ x: 22, y: 6 },
{ x: 23, y: 4 },
{ x: 23, y: 5 },
{ x: 23, y: 6 },
{ x: 24, y: 3 },
{ x: 24, y: 7 },
{ x: 26, y: 2 },
{ x: 26, y: 3 },
{ x: 26, y: 7 },
{ x: 26, y: 8 },
{ x: 36, y: 4 },
{ x: 36, y: 5 },
{ x: 37, y: 4 },
{ x: 37, y: 5 }
],
[
/*infinite 1 */
{ x: 2 , y: 7 },
{ x: 4 , y: 6 },
{ x: 4 , y: 7 },
{ x: 6 , y: 3 },
{ x: 6 , y: 4 },
{ x: 6 , y: 5 },
{ x: 8 , y: 2 },
{ x: 8 , y: 3 },
{ x: 8 , y: 4 },
{ x: 9 , y: 3 },
]
][1].forEach(function(e) {
e.x += 100;
e.y += 100;
e.z = e.z || 1;
world[key(e.x, e.y, e.z)] = e;
});
//} else {
random_fill(sceene.w * sceene.h / 100);
}
if (D) console.log('start!');
render();
window.setTimeout(run, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment