-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.builder.js
69 lines (58 loc) · 1.86 KB
/
role.builder.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
const tools = require("tools")
const bodyParts = require("./constants.bodyParts");
let roleBuilder = {
label: "builder",
/**
*
* @param {Room} room
* @param maxEnergy
* @returns {("work"|"carry"|"move")[]}
*/
getCreepBodyPartsToSpawn: function (room, energy) {
const baseParts = [WORK, CARRY, MOVE]
const additionalParts = [CARRY, MOVE, WORK, CARRY, MOVE]
let parts = []
for (let i in baseParts) {
let part = baseParts[i]
energy -= bodyParts[part]
parts.push(part)
}
while (energy > 0) {
let any = false
for (let i in additionalParts) {
let part = additionalParts[i]
if (energy >= bodyParts[part]) {
energy -= bodyParts[part]
parts.push(part)
any = true
}
}
if (!any)
break
}
return parts
},
/** @param {Creep} creep **/
run: function(creep) {
if (creep.memory.building && creep.store[RESOURCE_ENERGY] === 0) {
creep.memory.building = false
creep.say('🔄 harvest')
}
if (!creep.memory.building && creep.store.getFreeCapacity() === 0) {
creep.memory.building = true
creep.say('🚧 build')
}
if(creep.memory.building) {
let targets = creep.room.find(FIND_CONSTRUCTION_SITES)
if (targets.length) {
if (creep.build(targets[0]) === ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}})
}
}
}
else {
tools.gatherEnergy(creep)
}
}
}
module.exports = roleBuilder