Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rewriten library cellauto to make it work on diagrams generated by Raymond Hill voronoi library( https://github.com/gorhill/Javascript-Voronoi )
// rewriten from http://sanojian.github.io/cellauto/
function CellAutoVoronoiCell(index) {
this.index = index;
this.delays = [];
}
CellAutoVoronoiCell.prototype.process = function(neighbors) {
return;
};
CellAutoVoronoiCell.prototype.countSurroundingCellsWithValue = function(neighbors, value) {
var surrounding = 0;
for (var i = 0; i < neighbors.length; i++) {
if (neighbors[i].ca && (neighbors[i].ca[value] || neighbors[i].ca[value] === 0)) {
surrounding++;
}
}
return surrounding;
};
CellAutoVoronoiCell.prototype.getSurroundingCellsAverageValue = function(neighbors, value) {
var summed = 0.0;
for (var i = 0; i < neighbors.length; i++) {
if (neighbors[i].ca && neighbors[i].ca[value]) {
summed += neighbors[i].ca[value];
}
}
return summed / neighbors.length;//cnt;
};
CellAutoVoronoiCell.prototype.delay = function(numSteps, fn) {
this.delays.push({ steps: numSteps, action: fn });
};
CellAutoVoronoiCell.prototype.reset = function(neighbors) {
return;
};
function CAVWorld(voronoiCells, options) {
this.options = options;
this.voronoiCells = voronoiCells;
this.cellsLength = voronoiCells.length;
this.randomGenerator = Math.random;
this.step = function() {
for (var x=0; x<this.cellsLength; x++) {
this.voronoiCells[x].ca.reset();
}
for (var x=0; x<this.cellsLength; x++) {
var cell = this.voronoiCells[x];
cell.ca.process(cell);
// perform any delays
for (var i=0; i<cell.ca.delays.length; i++) {
if (!cell.ca.delays[i].steps--) {
// perform action and remove delay
cell.ca.delays[i].action(cell.ca);
cell.ca.delays.splice(i, 1);
i--;
}
}
}
};
this.initialize = function(arrayTypeDist) {
// sort the cell types by distribution
arrayTypeDist.sort(function(a, b) {
return a.distribution > b.distribution ? 1 : -1;
});
var totalDist = 0;
// add all distributions together
for (var i=0; i<arrayTypeDist.length; i++) {
totalDist += arrayTypeDist[i].distribution;
arrayTypeDist[i].distribution = totalDist;
}
for (var x=0; x<this.cellsLength; x++) {
var random = this.randomGenerator() * 100;
for (i=0; i<arrayTypeDist.length; i++) {
if (random <= arrayTypeDist[i].distribution) {
this.voronoiCells[x].ca = new this.cellTypes[arrayTypeDist[i].name](x);
break;
}
}
}
};
this.cellTypes = {};
this.registerCellType = function(name, cellOptions, init) {
this.cellTypes[name] = function(index) {
CellAutoVoronoiCell.call(this, index);
if (init) {
init.call(this);
}
if (cellOptions) {
for (var key in cellOptions) {
if (typeof cellOptions[key] !== 'function') {
// properties get instance
if (typeof cellOptions[key] === 'object') {
// objects must be cloned
this[key] = JSON.parse(JSON.stringify(cellOptions[key]));
}
else {
// primitive
this[key] = cellOptions[key];
}
}
}
}
};
this.cellTypes[name].prototype = Object.create(CellAutoVoronoiCell.prototype);
this.cellTypes[name].prototype.constructor = this.cellTypes[name];
this.cellTypes[name].prototype.cellType = name;
if (cellOptions) {
for (var key in cellOptions) {
if (typeof cellOptions[key] === 'function') {
// functions get prototype
this.cellTypes[name].prototype[key] = cellOptions[key];
}
}
}
};
// apply options
if (options) {
for (var key in options) {
this[key] = options[key];
}
}
}
(function() {
var CellAutoVoronoi = {
World: CAVWorld,
Cell: CellAutoVoronoiCell
};
if (typeof define === 'function' && define.amd) {
define('CellAutoVoronoi', function () {
return CellAutoVoronoi;
});
} else if (typeof module !== 'undefined' && module.exports) {
module.exports = CellAutoVoronoi;
} else {
window.CellAutoVoronoi = CellAutoVoronoi;
}
})();
function example_caves() {
var cells = island.diagram.cells,
world = new CAVWorld(cells);
world.palette = [
'255, 255, 255, 1',
'68, 36, 52, 1'
];
world.registerCellType('wall', {
getColor: function () {
return this.open ? 0 : 1;
},
process: function (cellVoronoi) {
var neighbors = [],
neighborsIds = cellVoronoi.getNeighborIds(),
surrounding;
for (var i = neighborsIds.length; i--; ) {
neighbors.push(cells[neighborsIds[i]]);
}
surrounding = this.countSurroundingCellsWithValue(neighbors, 'wasOpen');
this.open = (this.wasOpen && surrounding >= 3) || surrounding >= 5;
},
reset: function () {
this.wasOpen = this.open;
}
}, function () {
//init
this.open = Math.random() > 0.40;
});
world.initialize([
{ name: 'wall', distribution: 100 }
]);
return world;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.