Skip to content

Instantly share code, notes, and snippets.

@vogonistic
Created January 25, 2013 19:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vogonistic/4637048 to your computer and use it in GitHub Desktop.
Save vogonistic/4637048 to your computer and use it in GitHub Desktop.
Test bot for lookAt. Has a function looksAt which uses simple ray tracing to figure out which block a player is looking at.
// create and connect the bot
var mineflayer = require('mineflayer');
var bot = mineflayer.createBot({ username: 'testLookAt' });
bot.on('login', function() {
setTimeout(function() {
// find a block to test with
verify(findBlockAtVector(-0.25, -0.5, 0.00));
verify(findBlockAtVector( 0.25, -0.5, 0.00));
verify(findBlockAtVector( 0.00, -0.5, -0.25));
verify(findBlockAtVector( 0.00, -0.5, 0.25));
function findBlockAtVector(x, y, z) {
console.log(' - test vector: '+mineflayer.vec3(x, y, z))
var targetPos = bot.entity.position.offset(0, bot.entity.height, 0);
var block = bot.blockAt(targetPos);
while (block.boundingBox === 'empty') {
targetPos.translate(x, y, z);
var block = bot.blockAt(targetPos);
}
return targetPos.floored().offset(0.5,0.5,0.5);
}
function verify(targetPos) {
var targetFloored = targetPos.floored();
// look at the target
bot.lookAt(targetPos);
// raytrace and see where it takes us
var targetPos2 = looksAt(bot.username);
// now look at that position
bot.lookAt(targetPos2);
// and raytrace it again
var targetPos3 = looksAt(bot.username);
// should all be the same point if the calculations are done correctly
if (targetFloored.equals(targetPos2.floored()) && targetFloored.equals(targetPos3.floored())) {
console.log(' - '+targetFloored+' is verified in reverse and forwards')
} else {
console.log(' - '+targetFloored+' doesn\'t match the reverses: '+targetPos2.floored()+', '+targetPos3.floored())
}
}
}, 1000);
});
function looksAt(player) {
var e = bot.players[player].entity
var start = e.position.offset(0, e.height, 0);
var length = 0.0;
var block = bot.blockAt(start);
var vec = mineflayer.vec3(
-Math.cos(e.pitch) * Math.sin(e.yaw),
Math.sin(e.pitch),
-Math.cos(e.pitch) * Math.cos(e.yaw)
);
var vecAbs = vec.abs();
var vec_off = mineflayer.vec3(
(vec.x > 0 ? 1 : 0),
(vec.y > 0 ? 1 : 0),
(vec.z > 0 ? 1 : 0)
)
var lastPos = start;
var tests = 0;
while (block.boundingBox == 'empty') {
var diff = mineflayer.vec3(
Math.abs(Math.floor(lastPos.x + vec_off.x) - lastPos.x),
Math.abs(Math.floor(lastPos.y + vec_off.y) - lastPos.y),
Math.abs(Math.floor(lastPos.z + vec_off.z) - lastPos.z)
)
var normVec = vec_div(diff, vecAbs);
var dir = vec_min_point(normVec);
length += normVec[dir] + 0.001;
lastPos = start.plus(vec.scaled(length));
block = bot.blockAt(lastPos);
if (++tests > 100) {
return;
}
}
return lastPos;
};
vec_min_point = function(vec) {
var m = Math.min(vec.x, vec.y, vec.z);
if (m === vec.x) return 'x';
else if (m === vec.y) return 'y';
else if (m === vec.z) return 'z';
else return '?'
}
vec_div = function(left, scalar_vec) {
return mineflayer.vec3(left.x / scalar_vec.x, left.y / scalar_vec.y, left.z / scalar_vec.z);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment