mirror of
https://github.com/danbulant/flying-squid
synced 2026-07-05 03:00:46 +00:00
use an es6 class for diamond_square generation
This commit is contained in:
parent
1a58da904c
commit
53c2bc776d
1 changed files with 39 additions and 46 deletions
|
|
@ -2,58 +2,51 @@ var Chunk = require('prismarine-chunk')(require("../version"));
|
||||||
var Vec3 = require('vec3');
|
var Vec3 = require('vec3');
|
||||||
var rand = require('random-seed');
|
var rand = require('random-seed');
|
||||||
|
|
||||||
function DiamondSquare(size, roughness, seed) {
|
class DiamondSquare {
|
||||||
// public fields
|
constructor(size, roughness, seed) {
|
||||||
this.size = size;
|
// public fields
|
||||||
this.roughness = roughness;
|
this.size = size;
|
||||||
this.seed = seed;
|
this.roughness = roughness;
|
||||||
var opCount = 0;
|
this.seed = seed;
|
||||||
|
this.opCountN = 0;
|
||||||
|
|
||||||
// private field
|
// private field
|
||||||
var data = [];
|
this.data = [];
|
||||||
|
}
|
||||||
|
|
||||||
// public methods
|
// public methods
|
||||||
this.value = function(x, y, v) {
|
value(x, y, v) {
|
||||||
x = parseInt(x);
|
x = parseInt(x);
|
||||||
y = parseInt(y);
|
y = parseInt(y);
|
||||||
if (typeof(v) != 'undefined')
|
if (typeof(v) != 'undefined')
|
||||||
val(x, y, v);
|
this.val(x, y, v);
|
||||||
else
|
else
|
||||||
return val(x, y);
|
return this.val(x, y);
|
||||||
};
|
|
||||||
this.clear = function() {
|
|
||||||
data = [];
|
|
||||||
};
|
|
||||||
this.opCount = function(v) {
|
|
||||||
if (typeof(v) != 'undefined')
|
|
||||||
opCount = v;
|
|
||||||
else
|
|
||||||
return opCount;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// private methods
|
// private methods
|
||||||
function val(x, y, v) {
|
val(x, y, v) {
|
||||||
if (typeof(v) != 'undefined')
|
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 {
|
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) {
|
if (this.data[x + '_' + y] == null) {
|
||||||
opCount++;
|
this.opCountN++;
|
||||||
var base = 1;
|
var base = 1;
|
||||||
while (((x & base) == 0) && ((y & base) == 0))
|
while (((x & base) == 0) && ((y & base) == 0))
|
||||||
base <<= 1;
|
base <<= 1;
|
||||||
|
|
||||||
if (((x & base) != 0) && ((y & base) != 0))
|
if (((x & base) != 0) && ((y & base) != 0))
|
||||||
squareStep(x, y, base);
|
this.squareStep(x, y, base);
|
||||||
else
|
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++) {
|
for (var i = 0; i < 80; i++) {
|
||||||
var xm7 = x % 7;
|
var xm7 = x % 7;
|
||||||
var xm13 = x % 13;
|
var xm13 = x % 13;
|
||||||
|
|
@ -62,34 +55,34 @@ function DiamondSquare(size, roughness, seed) {
|
||||||
var ym105467 = y % 105467;
|
var ym105467 = y % 105467;
|
||||||
var ym105943 = y % 105943;
|
var ym105943 = y % 105943;
|
||||||
//y = (i < 40 ? seed : x);
|
//y = (i < 40 ? seed : x);
|
||||||
y = x + seed;
|
y = x + this.seed;
|
||||||
x += (xm7 + xm13 + xm1301081 + ym8461 + ym105467 + ym105943);
|
x += (xm7 + xm13 + xm1301081 + ym8461 + ym105467 + ym105943);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (xm7 + xm13 + xm1301081 + ym8461 + ym105467 + ym105943) / 1520972.0;
|
return (xm7 + xm13 + xm1301081 + ym8461 + ym105467 + ym105943) / 1520972.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function displace(v, blockSize, x, y) {
|
displace(v, blockSize, x, y) {
|
||||||
return (v + (randFromPair(x, y, seed) - 0.5) * blockSize * 2 / size * roughness);
|
return (v + (this.randFromPair(x, y, this.seed) - 0.5) * blockSize * 2 / this.size * this.roughness);
|
||||||
}
|
}
|
||||||
|
|
||||||
function squareStep(x, y, blockSize) {
|
squareStep(x, y, blockSize) {
|
||||||
if (data[x + '_' + y] == null) {
|
if (this.data[x + '_' + y] == null) {
|
||||||
val(x, y,
|
this.val(x, y,
|
||||||
displace((val(x - blockSize, y - blockSize) +
|
this.displace((this.val(x - blockSize, y - blockSize) +
|
||||||
val(x + blockSize, y - blockSize) +
|
this.val(x + blockSize, y - blockSize) +
|
||||||
val(x - blockSize, y + blockSize) +
|
this.val(x - blockSize, y + blockSize) +
|
||||||
val(x + blockSize, y + blockSize)) / 4, blockSize, x, y));
|
this.val(x + blockSize, y + blockSize)) / 4, blockSize, x, y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function diamondStep(x, y, blockSize) {
|
diamondStep(x, y, blockSize) {
|
||||||
if (data[x + '_' + y] == null) {
|
if (this.data[x + '_' + y] == null) {
|
||||||
val(x, y,
|
this.val(x, y,
|
||||||
displace((val(x - blockSize, y) +
|
this.displace((this.val(x - blockSize, y) +
|
||||||
val(x + blockSize, y) +
|
this.val(x + blockSize, y) +
|
||||||
val(x, y - blockSize) +
|
this.val(x, y - blockSize) +
|
||||||
val(x, y + blockSize)) / 4, blockSize, x, y));
|
this.val(x, y + blockSize)) / 4, blockSize, x, y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue