Skip to content

Commit

Permalink
Partial implementation of block digging
Browse files Browse the repository at this point in the history
  • Loading branch information
David Carne committed Sep 22, 2010
1 parent fac4460 commit 1bb7b45
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
Empty file modified chunk.js 100755 → 100644
Empty file.
35 changes: 34 additions & 1 deletion nodecraft.js
Expand Up @@ -167,6 +167,18 @@ function login(session, pkt) {
session.pump();
}

function blockdig(session, pkt) {
if (pkt.status == 0x3)
{
session.world.terrain.setCellType(pkt.x,pkt.y,pkt.z,0x0);
session.stream.write(ps.makePacket({
type: 0x35,
x: pkt.x, y: pkt.y, z: pkt.z, blockType: 0,
blockMetadata: 0
}));
}
}

function flying(session, pkt) {
}

Expand All @@ -175,10 +187,30 @@ var packets = {
0x01: login,
0x02: handshake,
0x0a: flying,
0x0e: blockdig,
};

world = new Object();


var world = new Object();
world.terrain = new terrain.WorldTerrain();
world.time = 0;
world.sessions = [];

function sendTicks()
{
for (var i=0; i<world.sessions.length; i++)
{
var session = world.sessions[i];
session.stream.write(ps.makePacket({
type: 0x04,
time: world.time
}));
}
world.time += 20;
}

setTimeout(1000, sendTicks());

var server = net.createServer(function(stream) {
stream.on('connect', function () {
Expand All @@ -193,6 +225,7 @@ var server = net.createServer(function(stream) {
var clientsession = new session.Session();
clientsession.stream = stream;
clientsession.world = world;
world.sessions.push(clientsession);

var partialData = new Buffer(0);
stream.on('data', function (data) {
Expand Down
Empty file modified proxy.js 100755 → 100644
Empty file.
29 changes: 29 additions & 0 deletions ps.js
Expand Up @@ -67,6 +67,24 @@ var packItems = function (items) {
}
return buf;
}

var unpackMultiBlocks = function (pkt) {
var blocks = [];
var numBlocks = parsers.short(pkt);
for (var i = 0; i < numBlocks; i++)
{
coord = parsers.short(pkt);
blocks.push({x: (coord >> 12), z: ((coord >> 8) & 0xF), y: (coord & 0xFF)})
}
for (var i = 0; i < numBlocks; i++)
blocks[i].type = parsers.byte(pkt);

for (var i = 0; i < numBlocks; i++)
blocks[i].meta = parsers.byte(pkt);

return blocks;
}

var unpackItems = function (pkt) {
var items = [];
var numItems = parsers.short(pkt);
Expand All @@ -91,18 +109,26 @@ function bool(name) { return ['bool', name]; }
function double(name) { return ['double', name]; }
function float(name) { return ['float', name]; }
function items(name) { return ['items', name]; }
function multiblock(name) { return ['multiblock', name]; }
function intstr(name) { return ['intstr', name]; }

var clientPacketStructure = {
0x00: [],
0x01: [int('protoVer'), str('username'), str('password')],
0x02: [str('username')],
0x05: [int('invType'), items('items')],
0x0a: [bool('isFlying')],
0x0b: [double('x'), double('y'), double('stance'), double('z'),
bool('flying')],
0x0c: [float('rotation'), float('pitch'), bool('flying')],
0x0d: [double('x'), double('y'), double('stance'), double('z'),
float('rotation'), float('pitch'), bool('flying')],

0x0e: [byte('status'), int('x'), byte('y'), int('z'), byte('face')],
0x10: [int('uid'), short('item')],
0x12: [int('uid'), byte('unk')],

0xff: [str('message')], // disconnect
}

var serverPacketStructure = {
Expand Down Expand Up @@ -132,6 +158,7 @@ var serverPacketStructure = {
0x32: [int('x'), int('z'), bool('mode')], // prechunk
0x33: [int('x'), short('y'), int('z'), byte('sizeX'), byte('sizeY'),
byte('sizeZ'), intstr('chunk')], // map chunk, gzipped
0x34: [int('x'), int('z'), multiblock('blocks')], // multi block change
0x35: [int('x'), byte('y'), int('z'), byte('blockType'), byte('blockMetadata')],
0x3b: [int('x'), short('y'), int('z'), str('nbt')],
0xff: [str('message')], // disconnect
Expand All @@ -149,6 +176,7 @@ var packetNames = {
0x0b: 'PLAYER_POSITION',
0x0c: 'PLAYER_LOOK',
0x0d: 'PLAYER_MOVE_LOOK',
0x0e: 'DIG_BLOCK',
0x10: 'WIELD',
0x12: 'ARM_ANIM',
0x14: 'PLAYER_SPAWN',
Expand Down Expand Up @@ -195,6 +223,7 @@ var parsers = {
bool: unpackBool,
float: unpack_fmt('f'),
double: unpack_fmt('d'),
multiblock: unpackMultiBlocks,
items: unpackItems,
intstr: unpackIntString,
}
Expand Down
19 changes: 16 additions & 3 deletions terrain.js
Expand Up @@ -83,7 +83,7 @@ WorldTerrain.prototype.getChunk = function(x, z, done_callback) {
done_callback(this.chunks[[x,z]]);
}

WorldTerrain.prototype.getCell = function(x,y,z, done_callback)
WorldTerrain.prototype.getCellType = function(x,y,z, done_callback)
{
var me = this;

Expand All @@ -95,6 +95,19 @@ WorldTerrain.prototype.getCell = function(x,y,z, done_callback)
});
}

WorldTerrain.prototype.setCellType = function(x,y,z,t)
{
var me = this;

this.getChunk(x-x%this.chunk_xz_granularity, z-z%this.chunk_xz_granularity,
function(chunk_data) {
var x_i = x%me.chunk_xz_granularity;
var z_i = z%me.chunk_xz_granularity;
chunk_data.setType(x_i, y, z_i, t);
});
}


WorldTerrain.prototype.getMaxHeight = function(x, z, done_callback) {
var currentY = 127;
var me = this;
Expand All @@ -107,10 +120,10 @@ WorldTerrain.prototype.getMaxHeight = function(x, z, done_callback) {
return;
}
currentY--;
me.getCell(x, currentY, z, iterate);
me.getCellType(x, currentY, z, iterate);
}

me.getCell(x, currentY, z, iterate);
me.getCellType(x, currentY, z, iterate);
}

module.exports.WorldTerrain = WorldTerrain;

0 comments on commit 1bb7b45

Please sign in to comment.