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/common.js

77 lines
2.5 KiB

8 months ago
const consts = {
FINISHED: 1,
HARVESTING: 2,
CONTINUE_ACTIVITY: 3,
}
8 months ago
global.costMatrices = {}
8 months ago
module.exports = {
consts,
8 months ago
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))
},
8 months ago
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
}
}