From ef50d796d609354e38b7246a54347a85dcb94220 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Tue, 19 Jan 2016 23:12:30 +0100 Subject: [PATCH] move diamond-square to its own package --- package.json | 1 + src/lib/generations.js | 2 +- src/lib/worldGenerations/diamond_square.js | 138 --------------------- 3 files changed, 2 insertions(+), 139 deletions(-) delete mode 100644 src/lib/worldGenerations/diamond_square.js diff --git a/package.json b/package.json index 187b98d..0969882 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ }, "dependencies": { "babel-runtime": "^5.4.4", + "diamond-square": "0.0.0", "emit-then": "^1.0.2", "event-promise": "^0.0.1", "flatmap": "^0.0.3", diff --git a/src/lib/generations.js b/src/lib/generations.js index bb6572d..3414c32 100644 --- a/src/lib/generations.js +++ b/src/lib/generations.js @@ -1,6 +1,6 @@ module.exports = { 'grass_field':require("./worldGenerations/grass_field"), - 'diamond_square':require("./worldGenerations/diamond_square"), + 'diamond_square':require("diamond-square"), 'superflat':require("./worldGenerations/superflat"), 'all_the_blocks':require("./worldGenerations/all_the_blocks"), 'nether':require("./worldGenerations/nether") diff --git a/src/lib/worldGenerations/diamond_square.js b/src/lib/worldGenerations/diamond_square.js deleted file mode 100644 index 1e1ce27..0000000 --- a/src/lib/worldGenerations/diamond_square.js +++ /dev/null @@ -1,138 +0,0 @@ -const Chunk = require('prismarine-chunk')(require("../version")); -const Vec3 = require('vec3').Vec3; -const rand = require('random-seed'); - -class DiamondSquare { - constructor(size, roughness, seed) { - // public fields - this.size = size; - this.roughness = roughness; - this.seed = seed; - this.opCountN = 0; - - // private field - this.data = []; - } - - // public methods - value(x, y, v) { - x = parseInt(x); - y = parseInt(y); - if (typeof(v) != 'undefined') - this.val(x, y, v); - else - return this.val(x, y); - }; - - // private methods - val(x, y, v) { - if (typeof(v) != 'undefined') - this.data[x + '_' + y] = Math.max(0.0, Math.min(1.0, v)); - else { - if (x <= 0 || x >= this.size || y <= 0 || y >= this.size) return 0.0; - - if (this.data[x + '_' + y] == null) { - this.opCountN++; - let base = 1; - while (((x & base) == 0) && ((y & base) == 0)) - base <<= 1; - - if (((x & base) != 0) && ((y & base) != 0)) - this.squareStep(x, y, base); - else - this.diamondStep(x, y, base); - } - return this.data[x + '_' + y]; - } - } - - randFromPair(x, y) { - let xm7,xm13,xm1301081,ym8461,ym105467,ym105943; - for (let i = 0; i < 80; i++) { - xm7 = x % 7; - xm13 = x % 13; - xm1301081 = x % 1301081; - ym8461 = y % 8461; - ym105467 = y % 105467; - ym105943 = y % 105943; - //y = (i < 40 ? seed : x); - y = x + this.seed; - x += (xm7 + xm13 + xm1301081 + ym8461 + ym105467 + ym105943); - } - - return (xm7 + xm13 + xm1301081 + ym8461 + ym105467 + ym105943) / 1520972.0; - } - - displace(v, blockSize, x, y) { - return (v + (this.randFromPair(x, y, this.seed) - 0.5) * blockSize * 2 / this.size * this.roughness); - } - - 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)); - } - } - - 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)); - } - } -} - -function generation({seed,worldHeight=80,waterline=20}={}) { - // Selected empirically - const size = 10000000; - const space = new DiamondSquare(size, size / 500, seed); - - function generateSimpleChunk(chunkX, chunkZ) { - const chunk = new Chunk(); - const seedRand = rand.create(seed+':'+chunkX+':'+chunkZ); - - const worldX = chunkX * 16 + size / 2; - const worldZ = chunkZ * 16 + size / 2; - - for (let x = 0; x < 16; x++) { - for (let z = 0; z < 16; z++) { - const level = Math.floor(space.value(worldX + x, worldZ + z) * worldHeight); - const dirtheight = level - 4 + seedRand(3); - const bedrockheight = 1 + seedRand(4); - for (let y = 0; y < 256; y++) { - let block; - let data; - - const surfaceblock = level < waterline ? 12 : 2; // Sand below water, grass - const belowblock = level < waterline ? 12 : 3; // 3-5 blocks below surface - - if (y < bedrockheight) block = 7; // Solid bedrock at bottom - else if (y < level && y >= dirtheight) block = belowblock; // Dirt/sand below surface - else if (y < level) block = 1; // Set stone inbetween - else if (y == level) block = surfaceblock; // Set surface sand/grass - else if (y <= waterline) block = 9; // Set the water - else if (y == level+1 && level >= waterline && seedRand(10) ==0) { // 1/10 chance of grass - block = 31; - data = 1; - } - - const pos = new Vec3(x, y, z); - if (block) chunk.setBlockType(pos, block); - if (data) chunk.setBlockData(pos, data); - chunk.setSkyLight(pos, 15); - } - } - } - - return chunk; - } - return generateSimpleChunk; -} - -module.exports=generation;