-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathagentManager.js
70 lines (63 loc) · 1.76 KB
/
agentManager.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
var mineflayer = require('mineflayer');
// When we want to run multiple agents this will be helpful
var Agent = require('./Agent/agent.js')
function AgentManager(host, port, amount) {
this.host = host || "localhost";
this.port = port || 25565;
this.agents = [];
this.amount = amount || 1;
}
AgentManager.prototype.start = function () {
for (var i = 0; i < this.amount; ++i) {
this.spawnAgent(i);
}
// this.startLoop();
// console.log("Agents are ready!")
// this.loop();
}
// The agent will not spawn while I am excuting other code???
AgentManager.prototype.spawnAgent = function (id) {
var self = this;
var name = "Agent_" + id;
var bot = mineflayer.createBot({
host: this.host,
port: this.port,
username: name,
// password: process.argv[5], // sorry microsoft :'(
verbose: true,
});
var agent = new Agent(bot);
bot.on('spawn', function() {
// var test = new Agent(this);
// test.update();
// if()
// self.startLoop(true);
setInterval(agent.update.bind(agent), 100); // give our agent a reaction time?
// agent.update();
});
this.agents.push(agent);
}
//TODO: update to do multiple agents
AgentManager.prototype.startLoop = function (loop) {
if (!loop)
return;
for (var i = 0; i < this.agents.length; i++) {
var agent = this.agents[i];
if (agent.ready()) {
loop = false;
}
}
this.startLoop(loop)
}
AgentManager.prototype.loop = function () {
for (var i = 0; i < this.agents.length; i++) {
var agent = this.agents[i];
agent.update();
}
this.loop();
}
function test(agent) {
agent.update();
test(agent);
}
module.exports = AgentManager;