|
|
|
const libLink = require('link')
|
|
|
|
const libSpawn = require('spawn')
|
|
|
|
|
|
|
|
require('globals')
|
|
|
|
|
|
|
|
|
|
|
|
module.exports.loop = function () {
|
|
|
|
// Cleanups
|
|
|
|
if (!Memory.repairers) {
|
|
|
|
Memory.repairers = {}
|
|
|
|
}
|
|
|
|
if (!Memory.refillers) {
|
|
|
|
Memory.refillers = {}
|
|
|
|
}
|
|
|
|
for (let name in Memory.creeps) {
|
|
|
|
if (!Game.creeps[name]) {
|
|
|
|
delete Memory.creeps[name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
libLink.tickInit()
|
|
|
|
libSpawn.tickInit()
|
|
|
|
|
|
|
|
// tickInit
|
|
|
|
for (let role of roleModules) {
|
|
|
|
role.idling = 0
|
|
|
|
if (role.tickInit) {
|
|
|
|
role.tickInit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let role of roleModules) {
|
|
|
|
if (role.cleanup) {
|
|
|
|
role.cleanup()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let [id, creep] of Object.entries(Memory.refillers)) {
|
|
|
|
let target = Game.getObjectById(id)
|
|
|
|
if (!target || target.store.getFreeCapacity(RESOURCE_ENERGY) === 0 || !Game.creeps[creep]) {
|
|
|
|
delete Memory.refillers[id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let spawn = Game.spawns['Spawn1']
|
|
|
|
|
|
|
|
// Run modules
|
|
|
|
for (let link of spawn.room.find(FIND_STRUCTURES, {filter: s => s.structureType === STRUCTURE_LINK})) {
|
|
|
|
libLink.tick(link)
|
|
|
|
}
|
|
|
|
for (let creep of Object.values(Game.creeps)) {
|
|
|
|
if (creep.store.getFreeCapacity(RESOURCE_ENERGY) > 0) {
|
|
|
|
for (let target of creep.pos.findInRange(FIND_TOMBSTONES, 1, {filter: t => t.store[RESOURCE_ENERGY] > 0})) {
|
|
|
|
creep.withdraw(target, RESOURCE_ENERGY)
|
|
|
|
}
|
|
|
|
for (let target of creep.pos.findInRange(FIND_RUINS, 1, {filter: t => t.store[RESOURCE_ENERGY] > 0})) {
|
|
|
|
creep.withdraw(target, RESOURCE_ENERGY)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!creep.memory.role) {
|
|
|
|
creep.memory.role = 'harvester'
|
|
|
|
}
|
|
|
|
if (ROLES[creep.memory.role].tick(creep) === 'idle') {
|
|
|
|
ROLES[creep.memory.role].idling++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
libSpawn.tick(spawn)
|
|
|
|
|
|
|
|
let towers = spawn.room.find(FIND_STRUCTURES, {filter: structure => structure.structureType === STRUCTURE_TOWER})
|
|
|
|
for (let tower of towers) {
|
|
|
|
// var closestDamagedStructure = tower.pos.findClosestByRange(FIND_STRUCTURES, {
|
|
|
|
// filter: (structure) => structure.hits < structure.hitsMax
|
|
|
|
// });
|
|
|
|
// if(closestDamagedStructure) {
|
|
|
|
// tower.repair(closestDamagedStructure);
|
|
|
|
// }
|
|
|
|
|
|
|
|
let closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
|
|
|
|
if (closestHostile) {
|
|
|
|
tower.attack(closestHostile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for (let [role, values] of Object.entries(ROLES)) {
|
|
|
|
// if (values.idling > 0) {
|
|
|
|
// console.log(`${values.idling} ${role}s idling`)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
spawn.room.visual.text(
|
|
|
|
`${Game.cpu.getUsed().toFixed(1)} / ${Game.cpu.tickLimit} (${Game.cpu.bucket})`,
|
|
|
|
1, 1,
|
|
|
|
{align: 'left', opacity: 0.8});
|
|
|
|
|
|
|
|
if (Game.cpu.generatePixel && Game.cpu.bucket === 10000) {
|
|
|
|
Game.cpu.generatePixel()
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
PathFinder.search(
|
|
|
|
new RoomPosition(11, 7, 'E8S4'),
|
|
|
|
new RoomPosition(8, 11, 'E9S4'),
|
|
|
|
{roomCallback: 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);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return costs;
|
|
|
|
}}
|
|
|
|
).path.forEach(el => new RoomVisual(el.roomName).circle(el))
|
|
|
|
*/
|
|
|
|
}
|