Skip to content

Instantly share code, notes, and snippets.

@AustinLMayes
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AustinLMayes/c822a9d37c6801301dc3 to your computer and use it in GitHub Desktop.
Save AustinLMayes/c822a9d37c6801301dc3 to your computer and use it in GitHub Desktop.
My Chaos Bot.
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function inWater(bot) {
if ((bot.blockAt(bot.entity.position.offset(0, -1, 0)) == null || bot.blockAt(bot.entity.position) == null)) {return false;};
return (bot.blockAt(bot.entity.position.offset(0, -1, 0)).name == "water" || bot.blockAt(bot.entity.position).name == "water")
}
module.exports.inWater=inWater;
module.exports.getRandomInt=getRandomInt;
var goingForward = false, goingBack = false, goingLeft = false, goingRight = false, jumping = false;
var bot = null;
function init(owner) {
bot = owner;
}
function goForward(state) {
bot.setControlState('forward', state);
goingForward = state;
}
function goBack(state) {
bot.setControlState('back', state);
goingBack = state;
}
function goLeft(state) {
bot.setControlState('left', state);
goingLeft = state;
}
function goRight(state) {
bot.setControlState('right', state);
goingRight = state;
}
function jump(state) {
bot.setControlState('jump', state);
jumping = state;
}
function sprint(state) {
bot.setControlState('sprint', state);
}
// Variables
module.exports = {
goingForward: goingForward,
goingBack: goingBack,
goingLeft:goingLeft,
goingRight:goingRight,
jumping: jumping
};
// Control
module.exports.init=init;
module.exports.goForward=goForward;
module.exports.goBack=goBack;
module.exports.goLeft=goLeft;
module.exports.goRight=goRight;
module.exports.jump=jump;
module.exports.sprint=sprint;
main = require("./../main.js");
controls = main.controls;
commons = main.commons;
positionupdater = main.positionupdater;
var bot = null;
function init(owner) {
bot = owner;
// Startup
bindEvents(bot);
announcePosition();
randomForwardBack();
randomChat();
randomSpawn();
randomLook();
randomSprint();
randomJump();
randomLeftRight();
}
function unload() {
// Listeners
bot.removeListener("move", dontGetStuckInWater);
// Intervals
keepGoing = false;
}
var messages = [
"/help",
"Help Me Test Chaos Thory: http://tinyurl.com/nhaby4v",
]; // Global so it can be modified
var keepGoing = true;
function bindEvents(bot) {
bot.on('move', dontGetStuckInWater);
}
function dontGetStuckInWater() {
var waterChance = commons.getRandomInt(1, 3);
if (bot.entity.position.y < 143 && commons.inWater(bot) && waterChance != 2) {
//TODO: find a non-water block and walk.
}
}
function randomChat() {
var chat = commons.getRandomInt(180000, 240000);
// I believe Avicus Anti AFk checks for chat, so /help is there just to reset the timer.
var msg = commons.getRandomInt(0, messages.length - 1);
setTimeout(function() {
if (!keepGoing) {return}
bot.chat(messages[msg]);
if (keepGoing) {randomChat();}
}, chat);
}
function announcePosition() {
setTimeout(function() {
if (!keepGoing) {return}
bot.chat(positionupdater.positionString());
if (keepGoing) {announcePosition();}
}, 180000);
}
function randomSpawn() {
var spawn = commons.getRandomInt(60000, 180000);
setTimeout(function() {
if (!keepGoing) {return}
if (bot.entity.position.y < 140 && commons.getRandomInt(1,3) == 1) {bot.chat("/spawn");}
if (keepGoing) {randomSpawn();}
}, spawn);
}
function randomSprint() {
var start = commons.getRandomInt(2000, 17000);
var stop = commons.getRandomInt(5000, 15000);
setTimeout(function() {
if (!keepGoing) {return}
if (!controls.goingForward || commons.inWater()) {return}; // Don't look like a hacker.
controls.sprint(true);
setTimeout(function() {
if (!keepGoing) {return}
controls.sprint(false);
if (keepGoing) {randomSprint();}
}, stop);
}, start);
}
function randomJump() {
var start = commons.getRandomInt(15000, 45000);
var stop = commons.getRandomInt(5000, 15000);
setTimeout(function() {
if (!keepGoing) {return}
controls.jump(true);
setTimeout(function() {
if (!keepGoing) {return}
controls.jump(false);
if (keepGoing) {randomJump();}
}, stop);
}, start);
}
function randomLook() {
var look = commons.getRandomInt(10000, 45000);
var yaw = commons.getRandomInt(0, 365);
setTimeout(function() {
if (!keepGoing) {return}
bot.look(yaw, 0);
if (keepGoing) {randomLook();}
}, look);
}
function randomForwardBack() {
var forward = commons.getRandomInt(2000, 33000);
var back = commons.getRandomInt(2000, 33000);
var stop = commons.getRandomInt(2000, 33000);
setTimeout(function() {
if (!keepGoing) {return}
controls.goForward(true);
setTimeout(function() {
if (!keepGoing) {return}
controls.goForward(false);
controls.goBack(true);
setTimeout(function() {
if (!keepGoing) {return}
controls.goBack(false);
if (keepGoing) {randomForwardBack();}
}, stop);
}, back);
}, forward);
}
function randomLeftRight() {
var left = commons.getRandomInt(2000, 33000);
var right = commons.getRandomInt(2000, 33000);
var stop = commons.getRandomInt(2000, 33000);
setTimeout(function() {
if (!keepGoing) {return}
controls.goLeft(true);
setTimeout(function() {
if (!keepGoing) {return}
controls.goLeft(false);
controls.goRight(true);
setTimeout(function() {
if (!keepGoing) {return}
controls.goRight(false);
if (keepGoing) {randomLeftRight();}
}, stop);
}, right);
}, left);
}
module.exports = {
init:init,
unload:unload
};
var mineflayer = require('../mineflayer');
var auth = {
username: "",
verbose: true,
password: "",
host: "avic.us"
};
var bot;
var commons, controls, positionupdater, unjammer;
var modules;
init();
function init() {
console.log("---- Node Minecraft Bot ----")
console.log("Creating bot...");
bot = mineflayer.createBot(auth);
console.log("Loading utils...");
setupUtils();
module.exports= {
bot:bot,
commons:commons,
controls:controls,
positionupdater:positionupdater,
unjammer:unjammer,
stop:stopAll,
reset:reset
};
console.log("Loading modules...");
registerModules();
console.log("Loaded " + modules.length + " modules.");
module.exports.modules=modules;
}
function setupUtils() {
utilsPackage = "./utils/";
commons = require(utilsPackage + "commons");
controls = require(utilsPackage + "controls");
controls.init(bot);
positionupdater = require(utilsPackage + "positionupdater");
positionupdater.bind(bot);
unjammer = require(utilsPackage + "unjammer");
unjammer.run(bot, function() {
console.log("I'm stuck, running /spawn");
bot.chat("/spawn");
});
}
function stopAll() {
console.log("Stopping services...");
unjammer.stop();
console.log("Unloading modules...");
modules.LobbyChaos.unload();
bot.end();
}
function reset() {
stopAll()
console.log("Waiting 5 seconds, then restarting bot...");
setTimeout(function() {init();}, 5000)
}
function registerModules() {
modulePath = "./modules/";
modules = {
LobbyChaos:require(modulePath + "lobbychaos")
}
modules.LobbyChaos.init(bot)
}
var bot = null;
var positionX = 0;
var positionY = 0;
var positionZ = 0;
function bind(owner) {
if (bot != null) {bot.removeListener("move", update())} // Get rid of old instances.
bot = owner;
bot.on("move", update);
}
function positionString() {
return "Position:" +
" X:" + Math.floor(positionX) +
" Y:" + Math.floor(positionY) +
" Z:" + Math.floor(positionZ);
}
function update() {
positionX = bot.entity.position.x;
positionY = bot.entity.position.y;
positionZ = bot.entity.position.z;
}
module.exports = {
bind:bind,
positionString:positionString,
positionX:positionX,
positionY:positionY,
positionZ:positionZ
};
main = require("../main");
controls = main.controls;
positionupdater = main.positionupdater;
var inJam = false;
var runner = null;
var bot = null;
function run(owner, onError) {
bot = owner;
var lastCheckedPosX = 0; // Default
var lastCheckedPosZ = 0; // Default
runner = setInterval(function(){
if (controls.goingRight || controls.goingLeft || controls.goingBack || controls.goingForward) {
if (lastCheckedPosX == positionUpdater.positionX && lastCheckedPosZ == positionUpdater.positionZ) {
if (inJam) {return}; // Already handling
inJam = true;
getOutOfAJam(onError);
} else {
inJam = false;
triedJumping = false;
triedForward = false;
triedBack = false;
triedLeft = false;
triedRight = false;
}
} else {
inJam = false;
triedJumping = false;
triedForward = false;
triedBack = false;
triedLeft = false;
triedRight = false;
}
// Save
lastCheckedPosX = positionupdater.positionZ;
lastCheckedPosZ = positionupdater.positionZ;
}, 700)
}
function stop() {
clearInterval(runner);
inJam = false;
}
// Currently trying
var tryingToJump = false, tryingToForward = false, tryingToBack = false, tryingToLeft = false, tryingToRight = false;
// Already Tryied
var triedJumping = false, triedForward = false, triedBack = false, triedLeft = false, triedRight = false;
function getOutOfAJam(onError) {
if (!inJam) {return;}
if (!triedJumping) {
tryToJump();
return;
}
else if (!triedForward) {
console.goBack(false);
tryToForward();
return;
}
else if (!triedBack) {
console.goForward(false);
tryToBack();
return;
}
else if (!triedLeft) {
console.goRight(false);
tryToLeft();
return;
}
else if (!triedRight) {
console.goLeft(false);
tryToRight();
return;
}
else {
onError();
}
}
function tryToJump() {
// Haven't started trying yet and haven't tried already
if (!tryingToJump && !triedJumping) {
if (!jumping) {
// Not jumping, try.
controls.jump(true);
tryingToJump = true;
setTimeout(function(){tryToJump();}, 500); // Check again
} else {
// Already jumping, make sure true.
tryingToJump = true;
setTimeout(function(){tryToJump();}, 500); // Check again
}
} else if (tryingToJump) {
setTimeout(function() {
// Done everything, set trying to false and tried to true.
controls.jump(false);
tryingToJump = false;
triedJumping = true;
getOutOfAJam();
}, 1000)
}
}
function tryToForward() {
// Haven't started trying yet and haven't tried already
if (!tryingToForward && !triedForward) {
if (!goingForward) {
// Not going forward, try.
controls.goForward(true);
tryingToForward = true;
setTimeout(function(){tryToForward();}, 500); // Check again
} else {
// Already going forward, make sure true.
tryingToForward = true;
setTimeout(function(){tryToForward();}, 500); // Check again
}
} else if (tryingToForward) {
setTimeout(function() {
// Done everything, set trying to false and tried to true.
controls.goForward(false);
tryingToForward = false;
triedForward = true;
getOutOfAJam();
}, 1000)
}
}
function tryToBack() {
// Haven't started trying yet and haven't tried already
if (!tryingToBack && !triedBack) {
if (!goingBack) {
// Not going back, try.
controls.goBack(true);
tryingToBack = true;
setTimeout(function(){tryToBack();}, 500); // Check again
} else {
// Already going back, make sure true.
tryingToBack = true;
setTimeout(function(){tryToBack();}, 500); // Check again
}
} else if (tryingToBack) {
setTimeout(function() {
// Done everything, set trying to false and tried to true.
controls.goBack(false);
tryingToBack = false;
triedBack = true;
getOutOfAJam();
}, 1000)
}
}
function tryToLeft() {
// Haven't started trying yet and haven't tried already
if (!tryingToLeft && !triedLeft) {
if (!goingLeft) {
// Not going left, try.
controls.goLeft(true);
tryingToLeft = true;
setTimeout(function(){tryToLeft();}, 500); // Check again
} else {
// Already going left, make sure true.
tryingToLeft = true;
setTimeout(function(){tryToLeft();}, 500); // Check again
}
} else if (tryingToLeft) {
setTimeout(function() {
// Done everything, set trying to false and tried to true.
controls.goLeft(false);
tryingToLeft = false;
triedLeft = true;
getOutOfAJam();
}, 1000)
}
}
function tryToRight() {
// Haven't started trying yet and haven't tried already
if (!tryingToRight && !triedRight) {
if (!goingRight) {
// Not going right, try.
controls.goRight(true);
tryingToRight = true;
setTimeout(function(){tryToRight();}, 500); // Check again
} else {
// Already going right, make sure true.
tryingToRight = true;
setTimeout(function(){tryToRight();}, 500); // Check again
}
} else if (tryingToRight) {
setTimeout(function() {
// Done everything, set trying to false and tried to true.
controls.goRight(false);
tryingToRight = false;
triedRight = true;
getOutOfAJam();
}, 1000)
}
}
module.exports.run=run;
module.exports.stop=stop;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment