support multiple generation : worldGenerations dir + generation option in the settings, move diamond_square in its own file,

This commit is contained in:
Romain Beaumont 2015-10-09 13:08:00 +02:00
parent cc3977e41b
commit df58bea932
5 changed files with 157 additions and 127 deletions

3
app.js
View file

@ -13,7 +13,8 @@ var options = {
gameMode:settings.gameMode,
commands: commands,
logging:settings.logging,
kickTimeout:10*60*1000
kickTimeout:10*60*1000,
generation:settings.generation
};
mcServer.createMCServer(options);

View file

@ -4,5 +4,6 @@
"maxPlayers": 10,
"onlineMode": true,
"logging": true,
"gameMode": 1
"gameMode": 1,
"generation":"diamond_square"
}

View file

@ -2,131 +2,13 @@ var Chunk = require('prismarine-chunk')(require("../version"));
var World = require('prismarine-world');
var Vec3 = require('vec3');
var generations={
'grass_field':require("../worldGenerations/grass_field"),
'diamond_square':require("../worldGenerations/diamond_square")
};
module.exports = inject;
function inject(serv) {
function DiamondSquare(size, roughness, seed) {
// public fields
this.size = size;
this.roughness = roughness;
this.seed = (seed ? seed : Math.random());
var opCount = 0;
// private field
var data = new Array();
// public methods
this.value = function(x, y, v) {
x = parseInt(x);
y = parseInt(y);
if (typeof(v) != 'undefined')
val(x, y, v);
else
return val(x, y);
}
this.clear = function() {
data = new Array();
}
this.opCount = function(v) {
if (typeof(v) != 'undefined')
opCount = v;
else
return opCount;
}
// private methods
function val(x, y, v) {
if (typeof(v) != 'undefined')
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 (data[x + '_' + y] == null) {
opCount++;
var base = 1;
while (((x & base) == 0) && ((y & base) == 0))
base <<= 1;
if (((x & base) != 0) && ((y & base) != 0))
squareStep(x, y, base);
else
diamondStep(x, y, base);
}
return data[x + '_' + y];
}
}
function randFromPair(x, y) {
for (var i = 0; i < 80; i++) {
var xm7 = x % 7;
var xm13 = x % 13;
var xm1301081 = x % 1301081;
var ym8461 = y % 8461;
var ym105467 = y % 105467;
var ym105943 = y % 105943;
//y = (i < 40 ? seed : x);
y = x + 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);
}
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));
}
}
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));
}
}
}
// Selected empirically
var size = 10000000;
var space = new DiamondSquare(size, size/1000, Math.random() * 10000);
function generateSimpleChunk(chunkX, chunkZ) {
var chunk = new Chunk();
var worldX = chunkX * 16 + size/2;
var worldZ = chunkZ * 16 + size/2;
for (var x = 0; x < 16;x++) {
for (var z = 0; z < 16; z++) {
var level = space.value(worldX + x, worldZ + z) * 138;
for (var y = 0; y < 256; y++) {
let block;
if(y == 0) block = 7;
else if(y < level) block = 3;
else if (y == level) block = 2;
else if (y < 20) block = 9;
if(block) chunk.setBlockType(new Vec3(x, y, z), block);
chunk.setSkyLight(new Vec3(x, y, z), 15);
}
}
}
return chunk;
}
serv.world = new World(generateSimpleChunk);
function inject(serv,options) {
serv.world = new World(generations[options["generation"]]);
}

View file

@ -0,0 +1,127 @@
var Chunk = require('prismarine-chunk')(require("../version"));
var Vec3 = require('vec3');
function DiamondSquare(size, roughness, seed) {
// public fields
this.size = size;
this.roughness = roughness;
this.seed = (seed ? seed : Math.random());
var opCount = 0;
// private field
var data = new Array();
// public methods
this.value = function(x, y, v) {
x = parseInt(x);
y = parseInt(y);
if (typeof(v) != 'undefined')
val(x, y, v);
else
return val(x, y);
}
this.clear = function() {
data = new Array();
}
this.opCount = function(v) {
if (typeof(v) != 'undefined')
opCount = v;
else
return opCount;
}
// private methods
function val(x, y, v) {
if (typeof(v) != 'undefined')
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 (data[x + '_' + y] == null) {
opCount++;
var base = 1;
while (((x & base) == 0) && ((y & base) == 0))
base <<= 1;
if (((x & base) != 0) && ((y & base) != 0))
squareStep(x, y, base);
else
diamondStep(x, y, base);
}
return data[x + '_' + y];
}
}
function randFromPair(x, y) {
for (var i = 0; i < 80; i++) {
var xm7 = x % 7;
var xm13 = x % 13;
var xm1301081 = x % 1301081;
var ym8461 = y % 8461;
var ym105467 = y % 105467;
var ym105943 = y % 105943;
//y = (i < 40 ? seed : x);
y = x + 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);
}
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));
}
}
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));
}
}
}
// Selected empirically
var size = 10000000;
var space = new DiamondSquare(size, size/1000, Math.random() * 10000);
function generateSimpleChunk(chunkX, chunkZ) {
var chunk = new Chunk();
var worldX = chunkX * 16 + size/2;
var worldZ = chunkZ * 16 + size/2;
for (var x = 0; x < 16;x++) {
for (var z = 0; z < 16; z++) {
var level = space.value(worldX + x, worldZ + z) * 138;
for (var y = 0; y < 256; y++) {
let block;
if(y == 0) block = 7;
else if(y < level) block = 3;
else if (y == level) block = 2;
else if (y < 20) block = 9;
if(block) chunk.setBlockType(new Vec3(x, y, z), block);
chunk.setSkyLight(new Vec3(x, y, z), 15);
}
}
}
return chunk;
}
module.exports=generateSimpleChunk;

View file

@ -0,0 +1,19 @@
var Chunk = require('prismarine-chunk')(require("../version"));
var Vec3 = require('vec3');
function generateSimpleChunk(chunkX, chunkZ) {
var chunk=new Chunk();
for (var x = 0; x < 16;x++) {
for (var z = 0; z < 16; z++) {
chunk.setBlockType(new Vec3(x, 50, z), 2);
for (var y = 0; y < 256; y++) {
chunk.setSkyLight(new Vec3(x, y, z), 15);
}
}
}
return chunk;
}
module.exports=generateSimpleChunk;