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

Commit dbdceeb

Browse files
authored
Merge pull request #16 from PocketNode/levels
Levels
2 parents 11eedc3 + c82c88c commit dbdceeb

20 files changed

Lines changed: 213 additions & 742 deletions

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pocketnode",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "Server software written in Javascript for Minecraft: PE",
55
"main": "src/pocketnode/PocketNode.js",
66
"scripts": {
@@ -18,8 +18,9 @@
1818
"license": "ISC",
1919
"dependencies": {
2020
"adm-zip": "^0.4.7",
21-
"raknet": "git+https://github.com/PocketNode/RakNet.git",
21+
"pocketnode-binarystream": "git+https://github.com/PocketNode/PocketNode-BinaryStream.git",
2222
"pocketnode-language": "git+https://github.com/PocketNode/PocketNode-Language.git",
23+
"raknet": "git+https://github.com/PocketNode/RakNet.git",
2324
"time-stamp": "^2.0.0"
2425
},
2526
"devDependencies": {

src/pocketnode/PocketNode.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function PocketNode(paths){
99
this.START_TIME = Date.now();
1010
this.NAME = "PocketNode";
1111
this.CODENAME = "[BEGINNINGS]";
12-
this.VERSION = "0.0.4";
12+
this.VERSION = "0.0.5";
1313
this.API_VERSION = "1.0.0";
1414

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

22-
for(let i in paths){
23-
if(typeof path[i] !== "undefined"){
24-
path[i] = paths[i];
25-
}
26-
}
22+
for(let i in paths) if(typeof path[i] !== "undefined") path[i] = paths[i];
2723

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

30-
global.TRAVIS_BUILD = process.argv.indexOf("--travis-build") !== -1;
31-
3226
let server = new Server(this, logger, path);
3327
if(TRAVIS_BUILD === true){
3428
server.shutdown();

src/pocketnode/Server.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ const SFS = pocketnode("utils/SimpleFileSystem");
2323

2424
class Server {
2525
initVars(){
26-
this.PocketNode = {};
26+
this._pocketnode = {};
2727

2828
this._bannedIps = {};
2929
this._bannedNames = {};
3030
this._ops = {};
3131
this._whitelist = {};
32-
32+
3333
this._running = true;
3434
this._stopped = false;
3535

3636
this._pluginManager = {};
37-
37+
3838
this._scheduler = {}; //todo
3939

4040
this._tickCounter = 0;
@@ -51,18 +51,18 @@ class Server {
5151
this._commandMap = {};
5252

5353
this._resourcePackManager = {};
54-
54+
5555
this._onlineMode = false;
5656

5757
this._raknetAdapter = {};
58-
58+
5959
this._serverId = Math.floor((Math.random() * 99999999)+1);
6060

6161
this._paths = {};
6262
this._config = {};
6363

6464
this._maxPlayers = -1;
65-
65+
6666
this._players = new PlayerList();
6767
this._loggedInPlayers = new PlayerList();
6868
this._playerList = new PlayerList();
@@ -72,10 +72,11 @@ class Server {
7272
this._entityCount = 0;
7373
}
7474

75-
constructor(PocketNode, logger, paths){
75+
constructor(pocketnode, logger, paths){
7676
this.initVars();
7777

78-
this.PocketNode = PocketNode;
78+
this._pocketnode = pocketnode;
79+
7980
this._logger = logger;
8081
this._paths = paths;
8182

@@ -148,7 +149,7 @@ class Server {
148149

149150
this._tickCounter = 0;
150151

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

153154
this.tickProcessor();
154155
//this.forceShutdown();
@@ -183,21 +184,21 @@ class Server {
183184
* @return {string}
184185
*/
185186
getName(){
186-
return this.PocketNode.NAME;
187+
return this._pocketnode.NAME;
187188
}
188189

189190
/**
190191
* @return {string}
191192
*/
192193
getCodeName(){
193-
return this.PocketNode.CODENAME;
194+
return this._pocketnode.CODENAME;
194195
}
195196

196197
/**
197198
* @return {string}
198199
*/
199200
getPocketNodeVersion(){
200-
return this.PocketNode.VERSION;
201+
return this._pocketnode.VERSION;
201202
}
202203

203204
/**
@@ -218,7 +219,7 @@ class Server {
218219
* @return {string}
219220
*/
220221
getApiVersion(){
221-
return this.PocketNode.API_VERSION;
222+
return this._pocketnode.API_VERSION;
222223
}
223224

224225
/**
@@ -306,7 +307,7 @@ class Server {
306307
* @return {string}
307308
*/
308309
getMotd(){
309-
return this._config.getNested("server.motd", this.PocketNode.NAME + " Server");
310+
return this._config.getNested("server.motd", this._pocketnode.NAME + " Server");
310311
}
311312

312313
/**

src/pocketnode/level/chunk/Chunk.js

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const BinaryStream = pocketnode("utils/BinaryStream");
1+
const BinaryStream = pocketnode("network/minecraft/NetworkBinaryStream");
22
const SubChunk = pocketnode("level/chunk/SubChunk");
33
const EmptySubChunk = pocketnode("level/chunk/EmptySubChunk");
44

@@ -7,7 +7,7 @@ class Chunk {
77
this._x = 0;
88
this._z = 0;
99

10-
this._height = 256;
10+
this._height = 16;
1111

1212
/**
1313
* @type {Map<number, SubChunk>}
@@ -40,24 +40,8 @@ class Chunk {
4040
this._x = x;
4141
this._z = z;
4242

43-
if(subChunks.size !== 0) {
44-
for (let [y, chunk] of subChunks) {
45-
if (y < 0 || y >= this._height) {
46-
throw new Error("Invalid subchunk index " + y);
47-
}
48-
49-
if (chunk.isEmpty()) {
50-
this._subChunks.set(y, new EmptySubChunk());
51-
} else {
52-
this._subChunks.set(y, chunk);
53-
}
54-
}
55-
}
56-
57-
for(let i = 0; i < this._height; ++i){
58-
if(!this._subChunks.has(i)){
59-
this._subChunks.set(i, new EmptySubChunk());
60-
}
43+
for(let y = 0; y < this._height; y++){
44+
this._subChunks.set(y, subChunks.has(y) ? subChunks.get(y) : new EmptySubChunk());
6145
}
6246

6347
if(heightMap.length === 256){
@@ -76,7 +60,7 @@ class Chunk {
7660
if(biomes.length !== 0){
7761
throw new Error("Wrong Biomes value count, expected 256, got "+biomes.length);
7862
}else{
79-
this._biomes = new Array(256).fill(0);
63+
this._biomes = new Array(256).fill(0x00);
8064
}
8165
}
8266
}
@@ -162,7 +146,7 @@ class Chunk {
162146
}
163147

164148
setBlockData(x, y, z, data){
165-
return this.getSubChunk(y >> 4).setBlockData(x, y & 0x0f, z, data);
149+
return this.getSubChunk(y >> 4, true).setBlockData(x, y & 0x0f, z, data);
166150
}
167151

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

184-
getSubChunk(y, generateNew = false){
185-
if(y < 0 || y >= this._height){
186-
return new EmptySubChunk();
187-
}else if(generateNew && this._subChunks.has(y) instanceof EmptySubChunk){
188-
this._subChunks.set(y, new SubChunk());
189-
}
190-
191-
if(this._subChunks.get(y) === null){
192-
throw new Error("something broke..");
168+
getSubChunk(y, genNew = false){
169+
if(genNew && this._subChunks.get(y) instanceof EmptySubChunk){
170+
return this._subChunks.set(y, new SubChunk()).get(y);
193171
}
194-
195172
return this._subChunks.get(y);
196173
}
197174

@@ -222,28 +199,24 @@ class Chunk {
222199
}
223200

224201
recalculateHeightMap(){
225-
for(let z = 0; z < 16; ++z){
226-
for(let x = 0; x < 16; ++x){
227-
let id = this.getHighestBlockId(x, z);
228-
202+
for(let x = 0; x < 16; x++){
203+
for(let z = 0; z < 16; z++){
229204
this.setHeightMap(x, z, this.getHighestBlock(x, z) + 1);
230205
}
231206
}
232207
}
233208

234209
getHighestSubChunk(){
235-
let highest = new EmptySubChunk();
236-
for(let y = 16; y > 0; --y){
237-
if(this._subChunks.has(y)){
210+
for(let y = 15; y >= 0; y--){
211+
if(!this._subChunks.has(y)){
238212
continue;
239213
}
240214
if(this._subChunks.get(y).isEmpty()){
241215
continue;
242216
}
243-
highest = this._subChunks.get(y);
244-
break;
217+
return this._subChunks.get(y);
245218
}
246-
return highest;
219+
return new EmptySubChunk();
247220
}
248221

249222
getHighestBlockId(x, z){
@@ -255,23 +228,50 @@ class Chunk {
255228
}
256229

257230
getHighestBlock(x, z){
258-
return this.getHighestSubChunk().getHighestBlock(x, z);
231+
let index = this.getHighestSubChunkIndex();
232+
if(index === -1){
233+
return -1;
234+
}
235+
236+
for(let y = index; y >= 0; --y){
237+
let height = this.getSubChunk(y).getHighestBlock(x, z) | (y << 4);
238+
if(height !== -1){
239+
return height;
240+
}
241+
}
242+
243+
return -1;
244+
}
245+
246+
getHighestSubChunkIndex(){
247+
let y;
248+
for(y = this._subChunks.size - 1; y >= 0; --y){
249+
if(this._subChunks.get(y) instanceof EmptySubChunk){
250+
continue;
251+
}
252+
break;
253+
}
254+
255+
return y;
259256
}
260257

261258
getFilledSubChunks(){
262-
this.pruneEmptySubChunks();
263-
return this._subChunks.size;
259+
//this.pruneEmptySubChunks();
260+
//return this._subChunks.size;
261+
return this.getHighestSubChunkIndex() + 1;
264262
}
265263

266264
pruneEmptySubChunks(){
267-
for(let [y, subChunk] of this._subChunks){
268-
if(y < 0 || y >= this._height){
269-
this._subChunks.delete(y);
270-
}else if(subChunk instanceof EmptySubChunk){
265+
for(let y = 15; y >= 0; y--){
266+
if(!this._subChunks.has(y)){
271267
continue;
272-
}else if(subChunk.isEmpty()){
273-
this._subChunks.set(y, new EmptySubChunk());
274268
}
269+
270+
if(!this._subChunks.get(y).isEmpty()){
271+
return;
272+
}
273+
274+
this._subChunks.delete(y);
275275
}
276276
}
277277

@@ -305,12 +305,11 @@ class Chunk {
305305
let subChunkCount = this.getFilledSubChunks();
306306

307307
stream.writeByte(subChunkCount);
308-
for(let i = 0; i < subChunkCount; i++){
309-
stream.append(this._subChunks.get(i).toBinary());
308+
for(let y = 0; y < subChunkCount; ++y){
309+
stream.append(this._subChunks.get(y).toBinary());
310310
}
311311

312-
this._heightMap.forEach(v => stream.writeShort(v));
313-
312+
this._heightMap.forEach(v => stream.writeLShort(v));
314313
this._biomes.forEach(v => stream.writeByte(v));
315314
stream.writeByte(0);
316315

@@ -319,7 +318,7 @@ class Chunk {
319318
return stream.getBuffer();
320319
}
321320

322-
static getIndex(x, y, z){
321+
static getIdIndex(x, y, z){
323322
return (x << 12) | (z << 8) | y;
324323
}
325324

0 commit comments

Comments
 (0)