-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmineflayer-voxel.js
207 lines (172 loc) · 5.73 KB
/
mineflayer-voxel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
module.exports = init;
var child_process = require('child_process');
var http = require('http');
var fs = require('fs');
function init(mineflayer) {
return function(bot, options) { return inject(mineflayer, bot, options) };
}
function inject(mineflayer, bot, options) {
options = options || {};
var path = require('path')
, express = require('express')
, app = express()
, server = require('http').createServer(app)
, io = require('socket.io').listen(server)
, port = options.port || 0
, host = options.host || '0.0.0.0'
io.set('log level', 0);
if (!options.disableLogging) {
app.use(express.logger('tiny'));
}
// Serve out textures and such
app.use(express.static(path.join(__dirname, 'public')));
// Browservify the voxel code
app.get('/client.js', function(req, res){
res.writeHead(200, {'Content-Type':'application/javascript'});
browserify = child_process.spawn('browserify', [path.join(__dirname, 'app.js')]);
browserify.stdout.pipe(res);
browserify.stderr.pipe(process.stdout)
browserify.on('exit', function (code, signal) {
console.log('child process terminated due to receipt of signal '+signal+', and code '+code);
});
});
// Proxy skin requests
app.get('/skin/:username.png', function(req, res) {
var skinUrl = 'http://skins.minecraft.net/MinecraftSkins/'+req.params.username+'.png';
http.get(skinUrl, function(mojang_res) {
if (mojang_res.statusCode === 200) {
res.writeHead(200, {'content-type':'image/png'});
mojang_res.pipe(res);
} else {
fs.readFile(path.join(__dirname, 'public/char.png'), function(err, data) {
if (err) {
res.writeHead(404);
res.end();
} else {
res.writeHead(200, {'content-type':'image/png'});
res.write(data);
res.end();
}
})
}
});
})
server.listen(port, function() {
console.info("Listening at http://" + host + ":" + server.address().port);
});
var Vec3 = mineflayer.vec3.Vec3;
var chunkSize = new Vec3(32, 32, 32);
Vec3.prototype.chunkPosition = function() {
var offset = this.modulus(chunkSize);
return this.minus(offset).floored();
}
Vec3.prototype.toChunkCoordinate = function() {
var offset = this.modulus(chunkSize);
return this.minus(offset).scaled(1/32).floored();
}
Vec3.prototype.toVoxelCoordinate = function() {
return this.scaled(32);
}
function getBlockInfo() {
// get the highest block id
var max = Object.keys(mineflayer.blocks)
.map(function(v) { return parseInt(v, 10); })
.reduce(function(max, other) { return Math.max(max, other) });
// allocate array to hold the values
var res = new Array(max+1);
var blocks = mineflayer.blocks;
for (var key in blocks) {
if (blocks.hasOwnProperty(key)) {
var obj = blocks[key];
res[obj.id] = obj;
}
}
return res;
}
// in chunk coordinates
function getChunk(position) {
var start = position.toVoxelCoordinate();
var blocks = new Array(chunkSize.volume());
// console.log('getChunk: '+start)
var n = 0;
for (z = 0; z < chunkSize.x; ++z) {
for (y = 0; y < chunkSize.y; ++y) {
for (x = 0; x < chunkSize.z; ++x, ++n) {
var block = bot.blockAt(start.offset(x, y, z));
if (!block) return undefined;
blocks[n] = block.type;
}
}
}
var key = [position.x, position.y, position.z].join('|');
return {
position: start,
key: key,
blocks: blocks
}
}
function sendSpawnChunks(socket, position) {
var centerChunk = position.toChunkCoordinate();
// console.log('centerChunk: '+centerChunk)
var radius = 1;
// var chunks = [];
for (var x = -radius; x <= radius; ++x) {
for (var y = -radius; y <= radius; ++y) {
for (var z = -radius; z <= radius; ++z) {
// console.log('chunk', [x,y,z])
var chunk = getChunk(centerChunk.offset(x,y,z));
if (chunk) {
socket.json.emit('chunkData', chunk);
}
}
}
}
// return chunks;
}
io.sockets.on('connection', function (socket) {
socket.json.emit('blockData', getBlockInfo());
sendSpawnChunks(socket, bot.entity.position);
socket.json.emit('spawn', serializedPosition(bot.entity.position));
bot.on('spawn', function() {
socket.emit('spawn', serializedPosition(bot.entity.position));
})
bot.on('move', function() {
socket.emit('entity', bot.entity);
});
bot.on('entitySpawn', function(entity) {
socket.emit('entitySpawn', entity);
});
bot.on('entityGone', function(entity) {
socket.emit('entityGone', entity);
});
bot.on('entityMoved', function(entity) {
socket.emit('entityMoved', entity);
});
bot.on('blockUpdate', function(oldBlock, newBlock) {
// console.log('blockUpdate new:'+(newBlock ? newBlock.name : '?')+' old:'+(oldBlock ? oldBlock.name : '?'))
if (!newBlock) return;
socket.emit('blockUpdate', serializedBlock(newBlock));
})
socket.on('controlState', function(state) {
bot.setControlState(state.name, state.value);
});
socket.on('look', function(look) {
bot.look(look.yaw, look.pitch);
});
socket.on('missingChunk', function(pos) {
var chunk = getChunk(mineflayer.vec3(x,y,z));
if (chunk)
socket.json.emit('chunkData', chunk);
})
function serializedPosition(position) {
return [position.x, position.y, position.z]
}
function serializedBlock(block) {
return {
name: block.name,
type: block.type,
position: serializedPosition(block.position)
}
}
});
}