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

169 lines
5.6 KiB

8 months ago
const common = require('common')
module.exports = {
8 months ago
name: 'transporter',
prio: 4,
count: 1,
bodies: [
[CARRY, CARRY, MOVE],
[CARRY, CARRY, MOVE, CARRY, CARRY, MOVE],
[CARRY, CARRY, MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE],
[CARRY, CARRY, MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE],
],
8 months ago
tick (creep) {
if (creep.store.getFreeCapacity(RESOURCE_ENERGY) === 0) {
creep.memory.loading = false
}
else if (creep.store[RESOURCE_ENERGY] === 0) {
creep.memory.loading = true
}
if (creep.store[RESOURCE_ENERGY] >= 50) {
let quickfillTargets = creep.pos.findInRange(FIND_STRUCTURES, 2, {
filter: s => {
8 months ago
return (s.structureType === STRUCTURE_SPAWN
|| s.structureType === STRUCTURE_EXTENSION)
8 months ago
&& s.store.getFreeCapacity(RESOURCE_ENERGY) > 0
}
})
if (quickfillTargets.length > 0) {
delete creep.memory.idling
let quickfillTarget = creep.pos.findClosestByRange(quickfillTargets)
if (creep.transfer(quickfillTarget, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(quickfillTarget, {visualizePathStyle: {stroke: '#729075'}});
return
}
}
}
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 quickloadTargets = creep.pos.findInRange(FIND_STRUCTURES, 2, {
filter: s => {
return s.structureType === STRUCTURE_CONTAINER
&& s.store[RESOURCE_ENERGY] > creep.store.getFreeCapacity(RESOURCE_ENERGY)
}
})
if (quickloadTargets.length > 0) {
delete creep.memory.idling
let quickloadTarget = creep.pos.findClosestByPath(quickloadTargets)
if (creep.withdraw(quickloadTarget, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(quickloadTarget, {visualizePathStyle: {stroke: '#729075'}});
return
}
}
}
if (creep.memory.loading) {
let target = creep.pos.findClosestByPath(FIND_TOMBSTONES, {
filter: (structure) => structure.store[RESOURCE_ENERGY] > 0
})
if (!target) {
target = creep.pos.findClosestByPath(FIND_RUINS, {
filter: (structure) => structure.store[RESOURCE_ENERGY] > 0
})
}
if (!target) {
target = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: s => {
return s.structureType === STRUCTURE_CONTAINER
&& s.store[RESOURCE_ENERGY] > 100
}
})
}
if (!target) {
target = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: s => {
return s.structureType === STRUCTURE_STORAGE
&& s.store[RESOURCE_ENERGY] > 100
}
})
}
if (target) {
if (creep.withdraw(target, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(target, {visualizePathStyle: {stroke: '#f7e180'}})
}
delete creep.memory.idling
return
}
if (!target) {
target = creep.pos.findClosestByPath(FIND_MY_CREEPS, {
filter: c => {
return c.memory.role === 'harvester' && c.store[RESOURCE_ENERGY] > 0
}
})
if (target) {
if (!creep.pos.inRangeTo(target, 1)) {
creep.moveTo(target, {visualizePathStyle: {stroke: '#f7e180'}})
}
delete creep.memory.idling
return
}
}
}
else {
let target = _.findKey(Memory.refillers, el => el === creep.name)
if (target) {
target = Game.getObjectById(target)
}
if (!target) {
target = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: s => (
s.structureType === STRUCTURE_EXTENSION
|| s.structureType === STRUCTURE_SPAWN
|| s.structureType === STRUCTURE_TOWER
)
&& s.store.getFreeCapacity(RESOURCE_ENERGY) > 0
&& !Memory.refillers[s.id]
})
}
if (!target) {
target = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: s => {
return s.structureType === STRUCTURE_LINK && s.store.getFreeCapacity(RESOURCE_ENERGY) > Math.min(creep.store[RESOURCE_ENERGY], 400)
}
})
}
if (!target) {
target = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: s => {
return s.structureType === STRUCTURE_STORAGE
&& s.store.getFreeCapacity(RESOURCE_ENERGY) > 0
}
})
}
if (target) {
if (target.store.getCapacity(RESOURCE_ENERGY) <= creep.store.getCapacity(RESOURCE_ENERGY)) {
Memory.refillers[target.id] = creep.name
}
if (creep.transfer(target, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(target, {visualizePathStyle: {stroke: '#00ff0f'}});
}
delete creep.memory.idling
return
}
}
if (!creep.memory.idling) {
creep.memory.idling = 0
}
if (creep.memory.idling > 3) {
if (creep.store[RESOURCE_ENERGY] > 10) {
creep.memory.loading = false
}
else if (creep.store.getFreeCapacity(RESOURCE_ENERGY) > 10) {
creep.memory.loading = true
}
creep.moveTo(creep.pos.findClosestByRange(FIND_FLAGS, {filter: flag => flag.name.startsWith('idle')}), {visualizePathStyle: {stroke: '#ff0000'}});
}
if (creep.memory.idling) {
creep.say('idle ' + creep.memory.idling)
}
creep.memory.idling++
return 'idle'
}
};