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/main.js

159 lines
5.3 KiB

8 months ago
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()
8 months ago
// tickInit
for (let role of roleModules) {
role.idling = 0
if (role.tickInit) {
role.tickInit()
8 months ago
}
8 months ago
}
8 months ago
8 months ago
for (let role of roleModules) {
if (role.cleanup) {
role.cleanup()
8 months ago
}
8 months ago
}
8 months ago
8 months ago
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]
8 months ago
}
8 months ago
}
8 months ago
8 months ago
for (let [roomName, roomMemory] of Object.entries(Memory.rooms)) {
let room = Game.rooms[roomName]
if (!roomMemory.fatigues) {
roomMemory.fatigues = {}
}
for (let [k, v] of Object.entries(roomMemory.fatigues)) {
let [x, y] = k.split('_')
x = parseInt(x)
y = parseInt(y)
if (room && v > 20) {
room.createConstructionSite(x, y, STRUCTURE_ROAD)
roomMemory.fatigues[k] = 0
}
if (v > 0) {
if (Game.time % 20 === 0) {
roomMemory.fatigues[k]--
}
room.visual.text(roomMemory.fatigues[k], x, y, {size: 0.4})
}
}
}
8 months ago
let spawn = Game.spawns['Spawn1']
8 months ago
global.activeRemotes = spawn.room.memory.activeRemotes || []
8 months ago
8 months ago
// Run modules
8 months ago
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)
8 months ago
}
8 months ago
for (let target of creep.pos.findInRange(FIND_RUINS, 1, {filter: t => t.store[RESOURCE_ENERGY] > 0})) {
creep.withdraw(target, RESOURCE_ENERGY)
8 months ago
}
}
8 months ago
if (!creep.memory.role) {
creep.memory.role = 'harvester'
}
if (ROLES[creep.memory.role].tick(creep) === 'idle') {
ROLES[creep.memory.role].idling++
}
8 months ago
if (creep.fatigue > 0) {
if (creep.pos.findInRange(FIND_STRUCTURES, 0, {filter: s => s.structureType === STRUCTURE_ROAD}).length === 0
&& creep.pos.findInRange(FIND_MY_CONSTRUCTION_SITES, 1, {filter: s => s.structureType === STRUCTURE_ROAD}).length === 0) {
creep.room.memory.fatigues[creep.pos.x + '_' + creep.pos.y] = (creep.room.memory.fatigues[creep.pos.x + '_' + creep.pos.y] || 0) + creep.fatigue
}
}
8 months ago
}
8 months ago
8 months ago
libSpawn.tick(spawn)
8 months ago
8 months ago
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);
// }
8 months ago
8 months ago
let closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
if (closestHostile) {
tower.attack(closestHostile);
8 months ago
}
8 months ago
}
8 months ago
8 months ago
// for (let [role, values] of Object.entries(ROLES)) {
// if (values.idling > 0) {
// console.log(`${values.idling} ${role}s idling`)
// }
// }
8 months ago
8 months ago
spawn.room.visual.text(
`${Game.cpu.getUsed().toFixed(1)} / ${Game.cpu.tickLimit} (${Game.cpu.bucket})`,
1, 1,
{align: 'left', opacity: 0.8});
8 months ago
if (Game.cpu.generatePixel && Game.cpu.bucket === 10000) {
Game.cpu.generatePixel()
}
8 months ago
/*
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))
*/
8 months ago
}