const consts = { FINISHED: 1, HARVESTING: 2, CONTINUE_ACTIVITY: 3, } global.costMatrices = {} module.exports = { consts, findWalkablePath (from, goals, opts) { let roomCallback = roomName => { if (costMatrices[roomName] || Game.time % 500 === 0) { return costMatrices[roomName] } let room = Game.rooms[roomName]; // In this example `room` will always exist, but since // PathFinder supports searches which span multiple rooms // you should be careful! if (!room) return; let costs = new PathFinder.CostMatrix; room.find(FIND_STRUCTURES).forEach(function(struct) { if (struct.structureType === STRUCTURE_ROAD) { // Favor roads over plain tiles costs.set(struct.pos.x, struct.pos.y, 1); } else if (struct.structureType !== STRUCTURE_CONTAINER && (struct.structureType !== STRUCTURE_RAMPART || !struct.my)) { // Can't walk through non-walkable buildings costs.set(struct.pos.x, struct.pos.y, 0xff); } }); costMatrices[roomName] = costs return costs } return PathFinder.search(from, goals, Object.assign({roomCallback}, opts)) }, harvestEnergy (creep) { if (!creep.memory.harvestingEnergy && creep.store[RESOURCE_ENERGY] > 0) { return consts.CONTINUE_ACTIVITY } creep.memory.harvestingEnergy = creep.store.getFreeCapacity() > 0 if (!creep.memory.harvestingEnergy) { return consts.FINISHED } // if (creep.store[RESOURCE_ENERGY] === 0) { // creep.say('🔄 fetch'); // } let target if (!target) { target = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: s => (s.structureType === STRUCTURE_CONTAINER || s.structureType === STRUCTURE_STORAGE) && s.store[RESOURCE_ENERGY] >= creep.store.getFreeCapacity(RESOURCE_ENERGY) }); } if(target) { let result = creep.withdraw(target, RESOURCE_ENERGY) if (result === ERR_NOT_IN_RANGE) { creep.moveTo(target, {visualizePathStyle: {stroke: '#ffaa00'}}); } else if (result === OK) { return consts.FINISHED } } else { let target = creep.pos.findClosestByRange(FIND_MY_CREEPS, { filter: creep => creep.memory.role === 'harvester' && creep.store[RESOURCE_ENERGY] > 0 }); if (target) { creep.moveTo(target, {visualizePathStyle: {stroke: '#ffaa00'}}); } } return consts.HARVESTING } }