const common = require('common') module.exports = { name: 'transporter', prio: 4, 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) , bodies: [ [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, CARRY, CARRY, MOVE], ], 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)) } } } } }, 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 => { return (s.structureType === STRUCTURE_SPAWN || s.structureType === STRUCTURE_EXTENSION) && 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.id !== creep.room.memory.controllerContainer && creep.room.memory.spawnContainers.indexOf(s.id) === -1 && 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.id !== creep.room.memory.controllerContainer && creep.room.memory.spawnContainers.indexOf(s.id) === -1 && 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.id === creep.room.memory.controllerContainer || creep.room.memory.spawnContainers.indexOf(s.id) !== -1) && 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' } };