You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
screeps/default/role.upgrader.js

78 lines
2.7 KiB

const common = require('common')
let idling = 0
module.exports = {
idling,
name: 'upgrader',
prio: 6,
getCount: colonyRoom => {
let cc = Game.getObjectById(colonyRoom.memory.controllerContainer)
let cl = Game.getObjectById(colonyRoom.memory.controllerLink)
if(colonyRoom.storage && cl) {
return Math.ceil(colonyRoom.storage.store[RESOURCE_ENERGY] / 50000)
}
else if(colonyRoom.storage && cc) {
return Math.ceil(colonyRoom.storage.store[RESOURCE_ENERGY] / 100000) + Math.ceil(cc.store[RESOURCE_ENERGY] / 500) + (cc.store[RESOURCE_ENERGY] > 1900 ? 2 : 0)
}
else if(colonyRoom.storage) {
return Math.ceil(colonyRoom.storage.store[RESOURCE_ENERGY] / 50000)
}
else if (cc) {
return Math.ceil(cc.store[RESOURCE_ENERGY] / 500) + (cc.store[RESOURCE_ENERGY] > 1900 ? 2 : 0)
}
return Math.min(colonyRoom.controller.level, 2)
},
maxExpands: 4,
baseBody: [WORK, CARRY, MOVE],
expandBody: [WORK, WORK, MOVE],
tick (creep) {
if (creep.store.getFreeCapacity(RESOURCE_ENERGY) > 0) {
let dropped = creep.pos.findInRange(FIND_DROPPED_RESOURCES, 1);
if (dropped.length > 0) {
creep.pickup(dropped[0])
}
let target
let targets = creep.pos.findInRange(FIND_TOMBSTONES, 1, {
filter: t => t.store[RESOURCE_ENERGY] > 0
})
if (targets.length) {
target = targets[0]
}
if (!target) {
targets = creep.pos.findInRange(FIND_STRUCTURES, 1, {
filter: s => (s.structureType === STRUCTURE_CONTAINER || s.structureType === STRUCTURE_LINK || s.structureType === STRUCTURE_STORAGE)
&& s.store[RESOURCE_ENERGY] > 0
})
if (targets.length) {
target = targets[0]
}
}
if (!target && creep.store[RESOURCE_ENERGY] === 0) {
if (creep.room.memory.controllerLink) {
target = Game.getObjectById(creep.room.memory.controllerLink)
} else {
target = creep.room.controller.pos.findClosestByRange(FIND_STRUCTURES, {
filter: s => (s.structureType === STRUCTURE_CONTAINER || s.structureType === STRUCTURE_LINK || s.structureType === STRUCTURE_STORAGE)
&& s.store[RESOURCE_ENERGY] > 0
})
}
}
if (target) {
if (creep.withdraw(target, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(target)
return
}
}
}
let actionResult = creep.upgradeController(creep.room.controller)
if (actionResult === OK) {
return
} else if (actionResult === ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller, {visualizePathStyle: {stroke: '#ffffff'}})
}
return 'idle'
}
};