|
|
|
global.hostileCreepCount = {}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
name: 'hauler',
|
|
|
|
prio: 11,
|
|
|
|
tickInit () {
|
|
|
|
for (let room of Object.values(Game.rooms)) {
|
|
|
|
hostileCreepCount[room.name] = room.find(FIND_HOSTILE_CREEPS, {filter: c => creepIsAttacker(c)}).length
|
|
|
|
if (!room.memory.hauler) {
|
|
|
|
room.memory.hauler = {}
|
|
|
|
}
|
|
|
|
let harvesters = room.find(FIND_MY_CREEPS, {filter: c => c.memory.role === 'remoteHarvester' && c.memory.room === room.name})
|
|
|
|
let filledContainers = room.find(FIND_STRUCTURES, {filter: s => s.structureType === STRUCTURE_CONTAINER && s.store[RESOURCE_ENERGY] > 0})
|
|
|
|
if (room.find(FIND_STRUCTURES, {filter: s => s.structureType === STRUCTURE_TOWER || s.structureType === STRUCTURE_SPAWN}).length > 0
|
|
|
|
|| (filledContainers.length === 0 && harvesters.length === 0)
|
|
|
|
|| room.find(FIND_HOSTILE_CREEPS, {filter: c => creepIsAttacker(c)}).length > 0
|
|
|
|
) {
|
|
|
|
room.memory.hauler.targetCount = 0
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
room.memory.hauler.targetCount = Math.max(filledContainers.length, harvesters.length) * 4
|
|
|
|
}
|
|
|
|
},
|
|
|
|
nextSpawn (spawn, roleCreeps) {
|
|
|
|
for (let room of Object.values(Game.rooms)) {
|
|
|
|
if (_.filter(roleCreeps, {memory: {room: room.name}}).length < room.memory.hauler.targetCount) {
|
|
|
|
return [
|
|
|
|
[CARRY, MOVE, CARRY, MOVE, CARRY, MOVE, CARRY, MOVE],
|
|
|
|
{room: room.name}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
tick (creep) {
|
|
|
|
if (creep.store.getFreeCapacity(RESOURCE_ENERGY) > 0 && hostileCreepCount[creep.memory.room] === 0) {
|
|
|
|
if (moveToRoom(creep, creep.memory.room) === MOVING) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let dropped = creep.pos.findInRange(FIND_DROPPED_RESOURCES, 1);
|
|
|
|
if (dropped.length > 0) {
|
|
|
|
creep.pickup(dropped[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
dropped = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES, {filter: r => r.amount >= 100});
|
|
|
|
if (dropped) {
|
|
|
|
if (creep.pickup(dropped) === ERR_NOT_IN_RANGE) {
|
|
|
|
creep.moveTo(dropped)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let target = creep.pos.findClosestByRange(FIND_TOMBSTONES, {
|
|
|
|
filter: (structure) => structure.store[RESOURCE_ENERGY] > 0
|
|
|
|
})
|
|
|
|
if (!target) {
|
|
|
|
target = creep.pos.findClosestByRange(FIND_RUINS, {
|
|
|
|
filter: (structure) => structure.store[RESOURCE_ENERGY] > 0
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (!target) {
|
|
|
|
target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
|
|
|
|
filter: s => {
|
|
|
|
return s.structureType === STRUCTURE_CONTAINER && s.store[RESOURCE_ENERGY] > creep.store.getFreeCapacity(RESOURCE_ENERGY)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (target) {
|
|
|
|
if (creep.withdraw(target, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
|
|
|
|
creep.moveTo(target, {visualizePathStyle: {stroke: '#f7e180'}})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!target) {
|
|
|
|
target = creep.pos.findClosestByRange(FIND_MY_CREEPS, {
|
|
|
|
filter: s => {
|
|
|
|
return s.store[RESOURCE_ENERGY] >= s.store.getCapacity(RESOURCE_ENERGY) * 0.5
|
|
|
|
&& s.memory.role === 'remoteHarvester'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (target) {
|
|
|
|
if (creep.pos.getRangeTo(target) > 1) {
|
|
|
|
creep.moveTo(target, {visualizePathStyle: {stroke: '#f7e180'}})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (creep.store[RESOURCE_ENERGY] > 0) {
|
|
|
|
let target = Game.rooms[creep.memory.colony].storage
|
|
|
|
if (!target) {
|
|
|
|
for (let scId of Game.rooms[creep.memory.colony].memory.spawnContainers) {
|
|
|
|
let container = Game.getObjectById(scId)
|
|
|
|
if (container && container.store.getFreeCapacity(RESOURCE_ENERGY) > 0) {
|
|
|
|
target = container
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (creep.transfer(target, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
|
|
|
|
creep.moveTo(target)
|
|
|
|
}
|
|
|
|
} else if (moveToRoom(creep, creep.memory.colony) === MOVING) {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
creep.moveTo(creep.pos.findClosestByRange(FIND_FLAGS, {filter: flag => flag.name.startsWith('idle')}), {visualizePathStyle: {stroke: '#ff0000'}});
|
|
|
|
return 'idle'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|