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.builder.js

118 lines
3.9 KiB

const common = require('common')
let idling = 0
let maxWallHits = 0
module.exports = {
name: 'builder',
prio: 5,
count: 1,
maxExpands: 4,
baseBody: [WORK, CARRY, MOVE, MOVE],
expandBody: [WORK, CARRY, MOVE, MOVE],
idling,
maxWallHits,
cleanup () {
for(let [id, creep] of Object.entries(Memory.repairers)) {
let repairable = Game.getObjectById(id)
if (!repairable
|| repairable.hits === repairable.hitsMax
|| !Game.creeps[creep]
|| ((repairable.structureType === STRUCTURE_WALL || repairable.structureType === STRUCTURE_RAMPART)
&& repairable.hits > maxWallHits)
) {
delete Memory.repairers[id]
}
}
},
tickInit () {
for (let room of Object.values(Game.rooms)) {
if (room.find(FIND_STRUCTURES, {filter: s => s.structureType === STRUCTURE_SPAWN}).length === 0) {
continue
}
let walls = room.find(FIND_STRUCTURES, {
filter: structure => (structure.structureType === STRUCTURE_WALL || structure.structureType === STRUCTURE_RAMPART)
})
let wallAvgHits = 0
if (walls.length > 0) {
wallAvgHits = walls.reduce((total, el) => total + el.hits, 0)
wallAvgHits = wallAvgHits / walls.length
}
maxWallHits = Math.min(wallAvgHits, 200000)
}
},
tick (creep) {
let harvest = common.harvestEnergy(creep)
if (harvest === common.consts.HARVESTING) {
return
}
if (creep.store.getFreeCapacity(RESOURCE_ENERGY)) {
let dropped = creep.pos.findInRange(FIND_DROPPED_RESOURCES, 1);
if (dropped.length > 0) {
creep.pickup(dropped[0])
}
let targets = creep.pos.findInRange(FIND_STRUCTURES, 1, {
filter: s => (s.structureType === STRUCTURE_CONTAINER || s.structureType === STRUCTURE_STORAGE)
&& s.store[RESOURCE_ENERGY] > 0
})
if (targets.length) {
creep.withdraw(targets[0], RESOURCE_ENERGY)
}
}
let repairTarget = _.findKey(Memory.repairers, el => el === creep.name)
if (repairTarget) {
repairTarget = Game.getObjectById(repairTarget)
}
if (!repairTarget) {
repairTarget = creep.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (structure) => {
return (
structure.structureType !== STRUCTURE_WALL && structure.structureType !== STRUCTURE_RAMPART
) && (
structure.hits < structure.hitsMax * 0.5 ||
structure.hitsMax - structure.hits >= creep.store.getCapacity(RESOURCE_ENERGY) * 100
) && !Memory.repairers[structure.id]
}
});
}
if (repairTarget) {
Memory.repairers[repairTarget.id] = creep.name
if(creep.repair(repairTarget) === ERR_NOT_IN_RANGE) {
creep.moveTo(repairTarget, {visualizePathStyle: {stroke: '#1a43cb'}});
}
return
}
let target = creep.pos.findClosestByPath(FIND_CONSTRUCTION_SITES, {
filter: (structure) => structure.structureType === STRUCTURE_EXTENSION // || structure.structureType == STRUCTURE_CONTAINER
})
if (!target) {
target = creep.pos.findClosestByPath(FIND_CONSTRUCTION_SITES);
}
if (target) {
if (creep.build(target) === ERR_NOT_IN_RANGE) {
creep.moveTo(target, {visualizePathStyle: {stroke: '#729075'}});
}
return
}
let wallTarget = creep.pos.findClosestByRange(FIND_STRUCTURES, {
filter: structure => structure.hits < structure.hitsMax
&& structure.hits <= maxWallHits
&& (structure.structureType === STRUCTURE_WALL || structure.structureType === STRUCTURE_RAMPART)
})
if (wallTarget) {
if(creep.repair(wallTarget) === ERR_NOT_IN_RANGE) {
creep.moveTo(wallTarget, {visualizePathStyle: {stroke: '#4d4747'}});
}
return
}
creep.moveTo(creep.pos.findClosestByRange(FIND_FLAGS, {filter: flag => flag.name.startsWith('idle')}), {visualizePathStyle: {stroke: '#ff0000'}});
return 'idle'
}
}