-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProspectorBot.js
More file actions
91 lines (74 loc) · 3.42 KB
/
ProspectorBot.js
File metadata and controls
91 lines (74 loc) · 3.42 KB
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
const { DropOffCreator } = require('./DropOffCreator');
const { Foreman } = require('./Foreman');
const { Retreater } = require('./Retreater');
const { Kamikaze } = require('./Kamikaze');
const { Prospector } = require('./Prospector');
const { ShipCreator } = require('./ShipCreator');
const hlt = require('./hlt');
const logging = require('./hlt/logging');
const constants = require('./constants');
const game = new hlt.Game();
game.initialize().then(async () => {
await game.ready('#TeamEric');
logging.info(`My Player ID is ${game.myId}.`);
while (true) {
await game.updateFrame();
const { gameMap, me } = game;
const commandQueue = [];
const dropOffCreator = new DropOffCreator();
const shipCreator = new ShipCreator();
const foreman = new Foreman();
const retreater = new Retreater();
const kamikaze = new Kamikaze();
const prospector = new Prospector();
let seams = [];
let dropOffId = -1; //used to ensure ship doesn't get two commands
//find muliple energy laden seams within the game map
//need more than 1 to create entropy and get out of local maximums
foreman.generateTopSeams(gameMap, me, seams);
//if it makes sense to create a new dropoff from
//an existing ship, do so.
if (dropOffCreator.shouldCreateDropOff(game, me)) {
let obj = dropOffCreator.makeDropOff(gameMap, me)
dropOffId = obj.dropOffId;
commandQueue.push(obj.command);
}
//every game turn we should tell every ship what to do
for (const ship of me.getShips()) {
// if ship has dropped off its halite for the final time
// it should self destruct to make room for other ships
if (kamikaze.shouldDestroyItself(game, ship)) {
commandQueue.push(kamikaze.destroy(ship, gameMap));
}
// if ship is getting close to full capacity
// retreat to nearest drop off location
else if (retreater.shouldReturnToBase(ship, dropOffId, game)) {
commandQueue.push(retreater.retreat(gameMap, me, ship));
}
// if the ships current game position has too little halite
// should go to the nearest maximum seam location
else if (prospector.shouldMoveToAnotherLocation(ship, dropOffId, gameMap)) {
const entropy = Math.floor(Math.random() * constants.ENTROPY);
// added periodic randomness to get out of local maximums
if (entropy == 0) {
const destination = me.shipyard.position;
const safeMove = gameMap.naiveNavigate(ship, destination);
commandQueue.push(ship.move(safeMove));
}
//find the best next position on the most maximized energy seam
else {
commandQueue.push(prospector.navigate(gameMap, ship, seams));
}
}
// there is an implicit else here that says if it isn't time to go
// back and the ships current position has enough halite then just
// stay still collecting halite
}
// this is an important turning mechanism. We should not just create
// ships just because we have enough halite to do so
if (shipCreator.shouldCreateShip(game, me, gameMap)) {
commandQueue.push(shipCreator.makeShip(me));
}
await game.endTurn(commandQueue);
}
});