diff --git a/src/lib/worldGenerations/diamond_square.js b/src/lib/worldGenerations/diamond_square.js index aa5c4e3..7b5ed18 100644 --- a/src/lib/worldGenerations/diamond_square.js +++ b/src/lib/worldGenerations/diamond_square.js @@ -2,58 +2,51 @@ var Chunk = require('prismarine-chunk')(require("../version")); var Vec3 = require('vec3'); var rand = require('random-seed'); -function DiamondSquare(size, roughness, seed) { - // public fields - this.size = size; - this.roughness = roughness; - this.seed = seed; - var opCount = 0; +class DiamondSquare { + constructor(size, roughness, seed) { + // public fields + this.size = size; + this.roughness = roughness; + this.seed = seed; + this.opCountN = 0; - // private field - var data = []; + // private field + this.data = []; + } // public methods - this.value = function(x, y, v) { + value(x, y, v) { x = parseInt(x); y = parseInt(y); if (typeof(v) != 'undefined') - val(x, y, v); + this.val(x, y, v); else - return val(x, y); - }; - this.clear = function() { - data = []; - }; - this.opCount = function(v) { - if (typeof(v) != 'undefined') - opCount = v; - else - return opCount; + return this.val(x, y); }; // private methods - function val(x, y, v) { + val(x, y, v) { if (typeof(v) != 'undefined') - data[x + '_' + y] = Math.max(0.0, Math.min(1.0, v)); + this.data[x + '_' + y] = Math.max(0.0, Math.min(1.0, v)); else { - if (x <= 0 || x >= size || y <= 0 || y >= size) return 0.0; + if (x <= 0 || x >= this.size || y <= 0 || y >= this.size) return 0.0; - if (data[x + '_' + y] == null) { - opCount++; + if (this.data[x + '_' + y] == null) { + this.opCountN++; var base = 1; while (((x & base) == 0) && ((y & base) == 0)) base <<= 1; if (((x & base) != 0) && ((y & base) != 0)) - squareStep(x, y, base); + this.squareStep(x, y, base); else - diamondStep(x, y, base); + this.diamondStep(x, y, base); } - return data[x + '_' + y]; + return this.data[x + '_' + y]; } } - function randFromPair(x, y) { + randFromPair(x, y) { for (var i = 0; i < 80; i++) { var xm7 = x % 7; var xm13 = x % 13; @@ -62,34 +55,34 @@ function DiamondSquare(size, roughness, seed) { var ym105467 = y % 105467; var ym105943 = y % 105943; //y = (i < 40 ? seed : x); - y = x + seed; + y = x + this.seed; x += (xm7 + xm13 + xm1301081 + ym8461 + ym105467 + ym105943); } return (xm7 + xm13 + xm1301081 + ym8461 + ym105467 + ym105943) / 1520972.0; } - function displace(v, blockSize, x, y) { - return (v + (randFromPair(x, y, seed) - 0.5) * blockSize * 2 / size * roughness); + displace(v, blockSize, x, y) { + return (v + (this.randFromPair(x, y, this.seed) - 0.5) * blockSize * 2 / this.size * this.roughness); } - function squareStep(x, y, blockSize) { - if (data[x + '_' + y] == null) { - val(x, y, - displace((val(x - blockSize, y - blockSize) + - val(x + blockSize, y - blockSize) + - val(x - blockSize, y + blockSize) + - val(x + blockSize, y + blockSize)) / 4, blockSize, x, y)); + squareStep(x, y, blockSize) { + if (this.data[x + '_' + y] == null) { + this.val(x, y, + this.displace((this.val(x - blockSize, y - blockSize) + + this.val(x + blockSize, y - blockSize) + + this.val(x - blockSize, y + blockSize) + + this.val(x + blockSize, y + blockSize)) / 4, blockSize, x, y)); } } - function diamondStep(x, y, blockSize) { - if (data[x + '_' + y] == null) { - val(x, y, - displace((val(x - blockSize, y) + - val(x + blockSize, y) + - val(x, y - blockSize) + - val(x, y + blockSize)) / 4, blockSize, x, y)); + diamondStep(x, y, blockSize) { + if (this.data[x + '_' + y] == null) { + this.val(x, y, + this.displace((this.val(x - blockSize, y) + + this.val(x + blockSize, y) + + this.val(x, y - blockSize) + + this.val(x, y + blockSize)) / 4, blockSize, x, y)); } } }