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

211 lines
7.8 KiB

8 months ago
const common = require('common')
module.exports = {
8 months ago
name: 'transporter',
prio: 4,
8 months ago
getCount: colonyRoom => Object.keys(colonyRoom.memory.sourcesData).length
+ Math.floor((colonyRoom.find(FIND_DROPPED_RESOURCES).reduce((total, current) => total + current.amount, 0)) / 500)
+ Math.floor((colonyRoom.find(FIND_STRUCTURES, {filter: s => s.structureType === STRUCTURE_CONTAINER && colonyRoom.memory.sourceContainers.indexOf(s.id) !== -1}).reduce((total, current) => total + current.store[RESOURCE_ENERGY], 0)) / 500)
,
8 months ago
bodies: [
8 months ago
[CARRY, MOVE],
8 months ago
[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
tickInit () {
for (let room of Object.values(Game.rooms)) {
if (!room.memory.spawnContainers) {
room.memory.spawnContainers = []
}
if (!room.memory.sourceContainers) {
room.memory.sourceContainers = []
}
if (Game.time % 100 === 0 && room.controller.my) {
let containers = room.controller.pos.findInRange(FIND_STRUCTURES, 4, {filter: s => s.structureType === STRUCTURE_CONTAINER})
if (containers.length > 0) {
room.memory.controllerContainer = containers[0].id
}
room.memory.spawnContainers = []
for (let spawn of room.find(FIND_STRUCTURES, {filter: s => s.structureType === STRUCTURE_SPAWN})) {
containers = spawn.pos.findInRange(FIND_STRUCTURES, 4, {filter: s => s.structureType === STRUCTURE_CONTAINER})
if (containers.length > 0) {
containers.forEach(el => room.memory.spawnContainers.push(el.id))
}
}
room.memory.sourceContainers = []
for (let spawn of room.find(FIND_SOURCES)) {
containers = spawn.pos.findInRange(FIND_STRUCTURES, 2, {filter: s => s.structureType === STRUCTURE_CONTAINER})
if (containers.length > 0) {
containers.forEach(el => room.memory.sourceContainers.push(el.id))
}
}
}
}
},
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
8 months ago
&& s.id !== creep.room.memory.controllerContainer
&& creep.room.memory.spawnContainers.indexOf(s.id) === -1
8 months ago
&& 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
8 months ago
&& s.id !== creep.room.memory.controllerContainer
&& creep.room.memory.spawnContainers.indexOf(s.id) === -1
8 months ago
&& 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 => {
8 months ago
return (s.structureType === STRUCTURE_STORAGE
|| s.id === creep.room.memory.controllerContainer
|| creep.room.memory.spawnContainers.indexOf(s.id) !== -1)
8 months ago
&& 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'
}
};