Skip to content
This repository has been archived by the owner on Feb 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #16 from PocketNode/levels
Browse files Browse the repository at this point in the history
Levels
  • Loading branch information
hcortezr authored Mar 1, 2018
2 parents 11eedc3 + c82c88c commit dbdceeb
Show file tree
Hide file tree
Showing 20 changed files with 213 additions and 742 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pocketnode",
"version": "0.0.4",
"version": "0.0.5",
"description": "Server software written in Javascript for Minecraft: PE",
"main": "src/pocketnode/PocketNode.js",
"scripts": {
Expand All @@ -18,8 +18,9 @@
"license": "ISC",
"dependencies": {
"adm-zip": "^0.4.7",
"raknet": "git+https://github.com/PocketNode/RakNet.git",
"pocketnode-binarystream": "git+https://github.com/PocketNode/PocketNode-BinaryStream.git",
"pocketnode-language": "git+https://github.com/PocketNode/PocketNode-Language.git",
"raknet": "git+https://github.com/PocketNode/RakNet.git",
"time-stamp": "^2.0.0"
},
"devDependencies": {
Expand Down
10 changes: 2 additions & 8 deletions src/pocketnode/PocketNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function PocketNode(paths){
this.START_TIME = Date.now();
this.NAME = "PocketNode";
this.CODENAME = "[BEGINNINGS]";
this.VERSION = "0.0.4";
this.VERSION = "0.0.5";
this.API_VERSION = "1.0.0";

let logger = new Logger("Server");
Expand All @@ -19,16 +19,10 @@ function PocketNode(paths){
plugins: Path.normalize(__dirname + "/../../plugins/")
};

for(let i in paths){
if(typeof path[i] !== "undefined"){
path[i] = paths[i];
}
}
for(let i in paths) if(typeof path[i] !== "undefined") path[i] = paths[i];

logger.info("Loading PocketNode...");

global.TRAVIS_BUILD = process.argv.indexOf("--travis-build") !== -1;

let server = new Server(this, logger, path);
if(TRAVIS_BUILD === true){
server.shutdown();
Expand Down
29 changes: 15 additions & 14 deletions src/pocketnode/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ const SFS = pocketnode("utils/SimpleFileSystem");

class Server {
initVars(){
this.PocketNode = {};
this._pocketnode = {};

this._bannedIps = {};
this._bannedNames = {};
this._ops = {};
this._whitelist = {};

this._running = true;
this._stopped = false;

this._pluginManager = {};

this._scheduler = {}; //todo

this._tickCounter = 0;
Expand All @@ -51,18 +51,18 @@ class Server {
this._commandMap = {};

this._resourcePackManager = {};

this._onlineMode = false;

this._raknetAdapter = {};

this._serverId = Math.floor((Math.random() * 99999999)+1);

this._paths = {};
this._config = {};

this._maxPlayers = -1;

this._players = new PlayerList();
this._loggedInPlayers = new PlayerList();
this._playerList = new PlayerList();
Expand All @@ -72,10 +72,11 @@ class Server {
this._entityCount = 0;
}

constructor(PocketNode, logger, paths){
constructor(pocketnode, logger, paths){
this.initVars();

this.PocketNode = PocketNode;
this._pocketnode = pocketnode;

this._logger = logger;
this._paths = paths;

Expand Down Expand Up @@ -148,7 +149,7 @@ class Server {

this._tickCounter = 0;

this.getLogger().info("Done ("+(Date.now() - this.PocketNode.START_TIME)+"ms)!");
this.getLogger().info("Done ("+(Date.now() - this._pocketnode.START_TIME)+"ms)!");

this.tickProcessor();
//this.forceShutdown();
Expand Down Expand Up @@ -183,21 +184,21 @@ class Server {
* @return {string}
*/
getName(){
return this.PocketNode.NAME;
return this._pocketnode.NAME;
}

/**
* @return {string}
*/
getCodeName(){
return this.PocketNode.CODENAME;
return this._pocketnode.CODENAME;
}

/**
* @return {string}
*/
getPocketNodeVersion(){
return this.PocketNode.VERSION;
return this._pocketnode.VERSION;
}

/**
Expand All @@ -218,7 +219,7 @@ class Server {
* @return {string}
*/
getApiVersion(){
return this.PocketNode.API_VERSION;
return this._pocketnode.API_VERSION;
}

/**
Expand Down Expand Up @@ -306,7 +307,7 @@ class Server {
* @return {string}
*/
getMotd(){
return this._config.getNested("server.motd", this.PocketNode.NAME + " Server");
return this._config.getNested("server.motd", this._pocketnode.NAME + " Server");
}

/**
Expand Down
111 changes: 55 additions & 56 deletions src/pocketnode/level/chunk/Chunk.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const BinaryStream = pocketnode("utils/BinaryStream");
const BinaryStream = pocketnode("network/minecraft/NetworkBinaryStream");
const SubChunk = pocketnode("level/chunk/SubChunk");
const EmptySubChunk = pocketnode("level/chunk/EmptySubChunk");

Expand All @@ -7,7 +7,7 @@ class Chunk {
this._x = 0;
this._z = 0;

this._height = 256;
this._height = 16;

/**
* @type {Map<number, SubChunk>}
Expand Down Expand Up @@ -40,24 +40,8 @@ class Chunk {
this._x = x;
this._z = z;

if(subChunks.size !== 0) {
for (let [y, chunk] of subChunks) {
if (y < 0 || y >= this._height) {
throw new Error("Invalid subchunk index " + y);
}

if (chunk.isEmpty()) {
this._subChunks.set(y, new EmptySubChunk());
} else {
this._subChunks.set(y, chunk);
}
}
}

for(let i = 0; i < this._height; ++i){
if(!this._subChunks.has(i)){
this._subChunks.set(i, new EmptySubChunk());
}
for(let y = 0; y < this._height; y++){
this._subChunks.set(y, subChunks.has(y) ? subChunks.get(y) : new EmptySubChunk());
}

if(heightMap.length === 256){
Expand All @@ -76,7 +60,7 @@ class Chunk {
if(biomes.length !== 0){
throw new Error("Wrong Biomes value count, expected 256, got "+biomes.length);
}else{
this._biomes = new Array(256).fill(0);
this._biomes = new Array(256).fill(0x00);
}
}
}
Expand Down Expand Up @@ -162,7 +146,7 @@ class Chunk {
}

setBlockData(x, y, z, data){
return this.getSubChunk(y >> 4).setBlockData(x, y & 0x0f, z, data);
return this.getSubChunk(y >> 4, true).setBlockData(x, y & 0x0f, z, data);
}

getBlockLight(x, y, z){
Expand All @@ -181,17 +165,10 @@ class Chunk {
return this.getSubChunk(y >> 4, true).setBlockSkyLight(x, y & 0x0f, z, level);
}

getSubChunk(y, generateNew = false){
if(y < 0 || y >= this._height){
return new EmptySubChunk();
}else if(generateNew && this._subChunks.has(y) instanceof EmptySubChunk){
this._subChunks.set(y, new SubChunk());
}

if(this._subChunks.get(y) === null){
throw new Error("something broke..");
getSubChunk(y, genNew = false){
if(genNew && this._subChunks.get(y) instanceof EmptySubChunk){
return this._subChunks.set(y, new SubChunk()).get(y);
}

return this._subChunks.get(y);
}

Expand Down Expand Up @@ -222,28 +199,24 @@ class Chunk {
}

recalculateHeightMap(){
for(let z = 0; z < 16; ++z){
for(let x = 0; x < 16; ++x){
let id = this.getHighestBlockId(x, z);

for(let x = 0; x < 16; x++){
for(let z = 0; z < 16; z++){
this.setHeightMap(x, z, this.getHighestBlock(x, z) + 1);
}
}
}

getHighestSubChunk(){
let highest = new EmptySubChunk();
for(let y = 16; y > 0; --y){
if(this._subChunks.has(y)){
for(let y = 15; y >= 0; y--){
if(!this._subChunks.has(y)){
continue;
}
if(this._subChunks.get(y).isEmpty()){
continue;
}
highest = this._subChunks.get(y);
break;
return this._subChunks.get(y);
}
return highest;
return new EmptySubChunk();
}

getHighestBlockId(x, z){
Expand All @@ -255,23 +228,50 @@ class Chunk {
}

getHighestBlock(x, z){
return this.getHighestSubChunk().getHighestBlock(x, z);
let index = this.getHighestSubChunkIndex();
if(index === -1){
return -1;
}

for(let y = index; y >= 0; --y){
let height = this.getSubChunk(y).getHighestBlock(x, z) | (y << 4);
if(height !== -1){
return height;
}
}

return -1;
}

getHighestSubChunkIndex(){
let y;
for(y = this._subChunks.size - 1; y >= 0; --y){
if(this._subChunks.get(y) instanceof EmptySubChunk){
continue;
}
break;
}

return y;
}

getFilledSubChunks(){
this.pruneEmptySubChunks();
return this._subChunks.size;
//this.pruneEmptySubChunks();
//return this._subChunks.size;
return this.getHighestSubChunkIndex() + 1;
}

pruneEmptySubChunks(){
for(let [y, subChunk] of this._subChunks){
if(y < 0 || y >= this._height){
this._subChunks.delete(y);
}else if(subChunk instanceof EmptySubChunk){
for(let y = 15; y >= 0; y--){
if(!this._subChunks.has(y)){
continue;
}else if(subChunk.isEmpty()){
this._subChunks.set(y, new EmptySubChunk());
}

if(!this._subChunks.get(y).isEmpty()){
return;
}

this._subChunks.delete(y);
}
}

Expand Down Expand Up @@ -305,12 +305,11 @@ class Chunk {
let subChunkCount = this.getFilledSubChunks();

stream.writeByte(subChunkCount);
for(let i = 0; i < subChunkCount; i++){
stream.append(this._subChunks.get(i).toBinary());
for(let y = 0; y < subChunkCount; ++y){
stream.append(this._subChunks.get(y).toBinary());
}

this._heightMap.forEach(v => stream.writeShort(v));

this._heightMap.forEach(v => stream.writeLShort(v));
this._biomes.forEach(v => stream.writeByte(v));
stream.writeByte(0);

Expand All @@ -319,7 +318,7 @@ class Chunk {
return stream.getBuffer();
}

static getIndex(x, y, z){
static getIdIndex(x, y, z){
return (x << 12) | (z << 8) | y;
}

Expand Down
Loading

0 comments on commit dbdceeb

Please sign in to comment.