start fixing spawn : improve player arrays, use vec3, fix relative position

This commit is contained in:
Romain Beaumont 2015-08-25 22:29:46 +02:00
parent e21819ddd4
commit 5839d97eaa
3 changed files with 45 additions and 342 deletions

82
app.js
View file

@ -5,8 +5,8 @@ var World = require('prismarine-chunk');
var fs = require('fs');
var timeStarted = Math.floor(new Date() / 1000).toString();
var playersConnected = [];
var playerMoveData = [];
var playerLookData = [];
var uuidToPlayer = {};
var vec3 = require("vec3");
var options = {
motd: settings.motd,
@ -34,33 +34,39 @@ var server = mc.createServer(options);
createLog();
}
function transformUuid(s)
{
return s.split("-").map(function(item) { return parseInt(item, 16); });
}
server.on('login', function(client) {
entityMaxId++;
client.id=entityMaxId;
playersConnected.push(client);
uuidToPlayer[client.uuid]=client;
playersConnected.forEach(function(entry) {
if(entry != client) {
var pos=uuidToPlayer[entry.uuid].position;
client.write('named_entity_spawn', {
entityId: entry.id,
playerUUID: entry.uuid.split("-").map(function(item) { return parseInt(item, 16); }),
x: playerMoveData[entry.uuid].x,
y: playerMoveData[entry.uuid].y,
z: playerMoveData[entry.uuid].z,
yaw: playerLookData[entry.uuid],
pitch: playerLookData[entry.uuid],
playerUUID: transformUuid(entry.uuid),
x: pos ? pos.x : 6,
y: pos ? pos.y : 52,
z: pos ? pos.z : 6,
yaw: 0,
pitch: 0,
currentItem: 0,
metadata: []
});
}
});
Object.keys(server.clients).forEach(function(clientKey) {
var otherClient=server.clients[clientKey]
playersConnected.forEach(function(otherClient) {
otherClient.write('player_info', {
action: 0,
data: [{
UUID: client.uuid.split("-").map(function(item) { return parseInt(item, 16); }),
UUID: transformUuid(client.uuid),
name: client.username,
properties: [],
gamemode: 0,
@ -98,26 +104,37 @@ server.on('login', function(client) {
});
client.on('position', function(packet) {
playerMoveData[client.uuid] = { x: packet.x, y: packet.y, z: packet.z, id: packet.id, onGround: packet.onGround};
client.write('rel_entity_move', {
entityId: playerMoveData[client.uuid].id,
dX: playerMoveData[client.uuid].x,
dY: playerMoveData[client.uuid].y,
dZ: playerMoveData[client.uuid].z,
onGround: playerMoveData[client.uuid].onGround
});
var position = new vec3(packet.x,packet.y,packet.z);
var onGround=packet.onGround;
sendRelativePositionChange(client,position,onGround);
});
client.on('position_look', function(packet) {
playerLookData[client.uuid] = { x: packet.yaw, y: packet.pitch, id: packet.id, onGround: packet.onGround};
client.write('entity_look', {
entityId: playerLookData[client.uuid].id,
yaw: playerLookData[client.uuid].yaw,
pitch: playerLookData[client.uuid].pitch,
onGround: playerLookData[client.uuid].onGround
});
var position = new vec3(packet.x,packet.y,packet.z);
var onGround=packet.onGround;
sendRelativePositionChange(client,position,onGround);
});
function sendRelativePositionChange(client,newPosition,onGround) {
if (uuidToPlayer[client.uuid].position) {
var diff = newPosition.minus(uuidToPlayer[client.uuid].position);
if(diff.distanceTo(new vec3(0,0,0))>0.1)
playersConnected.forEach(function (otherClient) {
if (otherClient != client) {
otherClient.write('rel_entity_move', {
entityId: uuidToPlayer[client.uuid].id,
dX: Math.floor(diff.x*32),
dY: Math.floor(diff.y*32),
dZ: Math.floor(diff.z*32),
onGround: onGround
});
}
});
}
uuidToPlayer[client.uuid].position = newPosition;
uuidToPlayer[client.uuid].onGround=onGround;
}
client.on('error', function(error) {
console.log('[ERR] ' + error.stack);
log('[ERR]: Client: ' + error.stack);
@ -134,19 +151,6 @@ server.on('login', function(client) {
maxPlayers: server.maxPlayers
});
// client.write('player_info', {
// action: 0,
// data: [{
// UUID: client.uuid.split("-").map(function(item) { return parseInt(item, 16); }),
// name: client.username,
// properties: [],
// gamemode: 0,
// ping: 1,
// hasDisplayName: false,
// //displayName: client.username
// }]
// });
var packetData = {
x: 0,
z: 0,

View file

@ -19,7 +19,8 @@
"buffers": "0.1.1",
"mocha": "~2.2.5",
"chai": "~3.2.0",
"prismarine-chunk": "~0.1.0"
"prismarine-chunk": "~0.1.0",
"vec3":"0.1.3"
},
"repository": {
"type": "git",

View file

@ -1,302 +0,0 @@
// THIS CODE WILL BE REMOVED IN A FUTURE RELEASE!
// var util = require('util')
// var zlib = require('zlib')
// var Buffers = require('buffers');
// module.exports = World;
// function BiomeData() {
// this.data = null;
// }
// BiomeData.prototype.fill = function() {
// if (!this.data) {
// this.data = new Buffer(256);
// for (var i = 0;i < 256;i++) {
// this.data[i] = 0;
// }
// }
// }
// BiomeData.prototype.unpack = function(buf) {
// this.data = buf.slice(0, 256);
// }
// BiomeData.prototype.pack = function() {
// this.fill();
// return this.data;
// }
// BiomeData.prototype.get = function(x, y, z) {
// this.fill();
// return this.data[x + ((y * 16) + z) * 16];
// }
// BiomeData.prototype.put = function(x, y, z, data) {
// this.fill();
// this.data[x + ((y * 16) + z) * 16] = data;
// }
// function ChunkData() {
// this.length = 16*16*16;
// this.data = null;
// }
// ChunkData.prototype.fill = function() {
// if (!this.data) {
// this.data = new Buffer(this.length);
// for (var i = 0;i < this.length;i++) {
// this.data[i] = 0;
// }
// }
// }
// ChunkData.prototype.unpack = function(buff) {
// this.data = buff.slice(0, this.length);
// }
// ChunkData.prototype.pack = function() {
// this.fill();
// return this.data;
// }
// ChunkData.prototype.get = function(x, y, z) {
// this.fill();
// return this.data[x + ((y * 16) + z) * 16];
// }
// ChunkData.prototype.put = function(x, y, z, data) {
// this.fill();
// this.data[x + ((y * 16) + z) * 16] = data;
// }
// function ChunkDataNibble() {
// this.length = 16*16*8;
// this.data = null;
// }
// util.inherits(ChunkDataNibble, ChunkData);
// ChunkDataNibble.prototype.get = function(x, y, z) {
// this.fill();
// var r = x % 2;
// x = Math.floor(x/2);
// var i = x + ((y * 16) + z) * 16;
// if (r === 0) {
// return this.data[i] >>> 4;
// } else {
// return this.data[i] & 0x0F;
// }
// }
// ChunkDataNibble.prototype.put = function(x, y, z, data) {
// this.fill();
// var r = x % 2;
// x = Math.floor(x/2);
// var i = x + ((y * 16) + z) * 16;
// if (r === 0) {
// this.data[i] = (this.data[i] & 0x0F) | ((data & 0x0F) << 4);
// } else {
// this.data[i] = (this.data[i] & 0xF0) | (this.data & 0x0F);
// }
// }
// function Chunk() {
// this.block_data = new ChunkData();
// this.block_meta = new ChunkDataNibble();
// this.block_add = new ChunkDataNibble();
// this.light_block = new ChunkDataNibble();
// this.light_sky = new ChunkDataNibble();
// }
// function ChunkColumn() {
// this.chunks = new Array(16);
// for (var i = 0;i < 16;i++) this.chunks[i] = null;
// this.biome = new BiomeData();
// }
// ChunkColumn.prototype.unpack = function(buff, mask1, mask2, skylight) {
// if (typeof skylight == "undefined") skylight = true;
// this.unpack_section(buff, "block_data", mask1);
// this.unpack_section(buff, "block_meta", mask1);
// this.unpack_section(buff, "light_block", mask1);
// if (skylight)
// this.unpack_section(buff, "light_sky", mask1);
// this.unpack_section(buff, "block_add", mask2);
// this.biome.unpack(buff);
// }
// ChunkColumn.prototype.unpack_section = function(buff, section, mask) {
// for (var i = 0; i < 16; i++) {
// if (mask & (1 << i)) {
// if (this.chunks[i] === null) {
// this.chunks[i] = new Chunk();
// }
// this.chunks[i][section].unpack(buff);
// }
// }
// }
// ChunkColumn.prototype.pack = function() {
// var bufs = [];
// var mask1 = 0;
// for (var i = 0; i < 16; i++) {
// if (this.chunks[i] !== null) {
// mask1 |= 1 << i;
// }
// }
// var block_data = this.pack_section("block_data");
// var block_meta = this.pack_section("block_meta");
// var light_block = this.pack_section("light_block");
// var light_sky = this.pack_section("light_sky");
// var mask2 = 0;
// bufs.push(block_data, block_meta, light_block, light_sky);
// return {
// data: Buffer.concat(bufs),
// mask1: mask1,
// mask2: mask2,
// skylight: true
// };
// }
// ChunkColumn.prototype.pack_section = function(section) {
// var bufs = [];
// for (var i = 0; i < 16; i++) {
// if (this.chunks[i] !== null)
// bufs.push(this.chunks[i][section].pack());
// }
// return Buffer.concat(bufs);
// }
// function World() {
// this.columns = {};
// }
// World.prototype.unpack = function(packetData) {
// var data = zlib.inflate(packetData.compressedChunkData);
// packetData.meta.forEach(function (meta) {
// var key = [meta.x, meta.z];
// var column;
// if (key in this.columns) {
// column = this.columns[key];
// } else {
// column = new ChunkColumn();
// this.columns[key] = column;
// }
// column.unpack(data, meta.bitMap, meta.addMap, packetData.skyLightSent);
// });
// }
// World.prototype.packMapChunkBulk = function() {
// var bufs = [];
// var metadatas = [];
// var cb = arguments[arguments.length - 1];
// // First pass, get all the metadatas and buffers.
// for (var i = 0;i < arguments.length - 1; i++) {
// var arg = arguments[i];
// var data = this.columns[arg];
// if (this.columns === null) {
// continue;
// }
// var packetContent = data.pack();
// bufs.push(packetContent.data);
// var metadata = {
// x: arg[0],
// z: arg[1],
// bitMap: packetContent.mask1,
// addBitMap: packetContent.mask2
// }
// metadatas.push(metadata);
// }
// cb(null, {
// skyLightSent: true,
// meta: metadatas,
// data: Buffer.concat(bufs)
// });
// }
// World.prototype.get = function(x, y, z, key) {
// var rx = x % 16;
// x = Math.floor(x / 16);
// var ry = y % 16;
// y = Math.floor(y / 16);
// var rz = z % 16;
// z = Math.floor(z / 16);
// if (!([x,z] in this.columns)) {
// return 0;
// }
// var column = this.columns[[x,z]];
// var chunk = column.chunks[y];
// if (chunk == null) {
// return 0;
// }
// return chunk[key].get(rx, ry, rz);
// }
// World.prototype.put = function(x, y, z, key, data) {
// var rx = x % 16;
// x = Math.floor(x / 16);
// var ry = y % 16;
// y = Math.floor(y / 16);
// var rz = z % 16;
// z = Math.floor(z / 16);
// var column;
// if ([x,z] in this.columns) {
// column = this.columns[[x,z]];
// } else {
// column = new ChunkColumn();
// this.columns[[x,z]] = column;
// }
// var chunk = column.chunks[y];
// if (chunk == null) {
// chunk = new Chunk();
// column.chunks[y] = chunk;
// }
// chunk[key].put(rx, ry, rz, data);
// }
// World.prototype.get_biome = function(x, z) {
// var rx = x % 16;
// x = Math.floor(x / 16);
// var rz = z % 16;
// z = Math.floor(z / 16);
// if (!([x,z] in this.columns)) {
// return 0;
// }
// return this.columns[[x,z]].biome.get(rx, rz);
// }
// World.prototype.put_biome = function(x, z, data) {
// var rx = x % 16;
// x = Math.floor(x / 16);
// var rz = z % 16;
// z = Math.floor(z / 16);
// var column;
// if ([x,z] in this.columns) {
// column = this.columns[[x,z]];
// } else {
// column = new ChunkColumn();
// this.columns[[x,z]] = column;
// }
// return column.biome.put(rx, rz, data);
// }