diff --git a/ClassRoom.js b/ClassRoom.js deleted file mode 100644 index 6cbfe16..0000000 --- a/ClassRoom.js +++ /dev/null @@ -1,50 +0,0 @@ -module.exports = { - setup: function () { Room.prototype = _Room.prototype; } -} - -class _Room extends Room { - begin(){ - this.setJobQueue(); - this.roomScan(); - this.buildRoads(); - this.memory.init = true; - } - - tick(){ - if(!this.memory.init) this.begin(); - } - - setJobQueue(){ - if(!this.memory.jobs) this.memory.jobs = [ - { role: Role.HARVESTER }, - { role: Role.HARVESTER }, - { role: Role.HARVESTER }, - { role: Role.HARVESTER }, - { role: Role.HARVESTER } - ] - } - - roomScan(){ - this.memory.layout = { - sources: this.find(FIND_SOURCES).map(s=>s.id) - }; - } - - buildRoads(){ - this.memory.layout.sources.forEach( - sId => { - const source = Game.getObjectById(sId); - this.buildRoad(source, this.controller); - this.buildRoad(source, this.find(FIND_MY_STRUCTURES, { filter:{ structureType:STRUCTURE_SPAWN }})[0]); - } - ) - - - } - - buildRoad(from, to){ - const path = from.pos.findPathTo(to); - path.forEach(tile => this.createConstructionSite(tile.x, tile.y, STRUCTURE_ROAD)) - } - -} \ No newline at end of file diff --git a/ClassSpawn.js b/ClassSpawn.js deleted file mode 100644 index 479eaa3..0000000 --- a/ClassSpawn.js +++ /dev/null @@ -1,33 +0,0 @@ -module.exports = { - setup: function () { StructureSpawn.prototype = _Spawn.prototype; } -} - -class _Spawn extends StructureSpawn { - - begin(){ - if (!this.memory.creepCounter) this.memory.creepCounter = 0; - this.memory.init = true; - } - - tick(){ - if(!this.memory.init) this.begin(); - if(this.room.memory.jobs.length){ - const job = this.room.memory.jobs[0]; - const body = [WORK, CARRY, MOVE]; - const response = this.createCreep(body, "harvester", job); - if(response == OK) this.room.memory.jobs.splice(0, 1); - } - } - - createHarvester() { - this.createCreep(body, "harvester", Role.HARVESTER); - } - - createCreep(body, name, job) { - const response = this.spawnCreep(body, name + ": " + this.memory.creepCounter, { - memory: { job: job } - }); - if (response == OK) this.memory.creepCounter++; - return response; - } -} diff --git a/Commands.js b/Commands.js new file mode 100644 index 0000000..7ea600d --- /dev/null +++ b/Commands.js @@ -0,0 +1,61 @@ + + +module.exports = { + setup(){ + global.fullReset = fullReset; + global.resetRooms = resetRooms; + global.resetStructures = resetStructures; + global.resetCreeps = resetCreeps; + global.resetConstruction = resetConstruction; + + global.buildRoads = buildRoads; + } +} + +function fullReset(){ + resetRooms(); + resetCreeps(); + resetStructures(); + resetConstruction(); + global.started = false; + global.compiled = false; + return "OK"; +} + +function resetRooms(){ + Object.values(Game.rooms).forEach(room => room.memory = {}); + return "OK"; +} + +function resetStructures(){ + Object.values(Game.structures).forEach(structure => structure.memory = {}); + return "OK"; +} + +function resetCreeps(){ + Object.values(Game.creeps).forEach(creep => creep.suicide()); + return "OK"; +} + +function resetConstruction(){ + Object.values(Game.rooms).forEach(room=>room.find(FIND_CONSTRUCTION_SITES).forEach(cs=>cs.remove())); + return "OK"; +} + +function buildRoads(){ + Object.values(Game.rooms).forEach(room=> + { + room.memory.layout.sources.forEach( + sId => { + const source = Game.getObjectById(sId); + var roads = []; + roads = roads.concat(source.pos.findPathTo(this.controller)); + roads.pop(); + roads = roads.concat(source.pos.findPathTo(room.find(FIND_MY_STRUCTURES, { filter:{ structureType:STRUCTURE_SPAWN }})[0])); + roads.pop(); + roads.forEach(tile => room.createConstructionSite(tile.x, tile.y, STRUCTURE_ROAD)); + } + ); + }); + return "OK"; +} \ No newline at end of file diff --git a/ClassCreep.js b/CreepClass.js similarity index 72% rename from ClassCreep.js rename to CreepClass.js index d293617..677f32b 100644 --- a/ClassCreep.js +++ b/CreepClass.js @@ -1,4 +1,5 @@ const jobHarvester = require("JobHarvester"); +const jobMiner = require("JobMiner"); module.exports = { setup: function () { @@ -8,7 +9,11 @@ module.exports = { } const Role = { - HARVESTER: 0 + HARVESTER: 0, + MINER: 1, + SUPPLIER: 2, + UPGRADER: 3, + BUILDER: 4 } class _Creep extends Creep { @@ -16,6 +21,7 @@ class _Creep extends Creep { if(!this.memory.job) this.memory.job = { role: Role.HARVESTER }; switch (this.memory.job.role) { case Role.HARVESTER: jobHarvester.begin(this); break; + case Role.MINER: jobMiner.begin(this); break; } this.memory.init = true; } @@ -24,10 +30,7 @@ class _Creep extends Creep { if(!this.memory.init) this.begin(); switch (this.memory.job.role) { case Role.HARVESTER: jobHarvester.tick(this); break; + case Role.MINER: jobMiner.tick(this); break; } - - if(this.ticksToLive == 10) - this.room.memory.jobs.push(this.memory.job); } - } diff --git a/JobBuilder.js b/JobBuilder.js new file mode 100644 index 0000000..9d100dc --- /dev/null +++ b/JobBuilder.js @@ -0,0 +1,8 @@ +module.exports = { + begin(creep){ + }, + + tick(creep){ + + } +} \ No newline at end of file diff --git a/JobHarvester.js b/JobHarvester.js index d54acf2..0d49087 100644 --- a/JobHarvester.js +++ b/JobHarvester.js @@ -1,42 +1,89 @@ module.exports = { begin(creep){ - const jobMemory = creep.memory.job; - if(!jobMemory.harvesting) creep.memory.job.harvesting = false; - if(!jobMemory.counter) creep.memory.job.counter = 0; - creep.memory.init = true; + if(!creep.memory.harvesting) creep.memory.harvesting = false; + if(!creep.memory.counter) creep.memory.counter = 0; }, tick(creep){ - const jobMemory = creep.memory.job; - if(!jobMemory.init) this.begin(creep); - if(jobMemory.harvesting){ - const roomSources = creep.room.memory.layout.sources - const source = Game.getObjectById(roomSources[jobMemory.counter%roomSources.length]); - if(creep.pos.isNearTo(source)) creep.harvest(source); - else creep.moveTo(source); - if(!creep.store.getFreeCapacity(RESOURCE_ENERGY)) jobMemory.harvesting = false; - } else{ - var target; - switch (jobMemory.counter%3) { - case 0: target = creep.room.controller; break; - case 1: target = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {filter:{structureType:STRUCTURE_SPAWN}}); break; - case 2: target = creep.pos.findClosestByRange(FIND_MY_CONSTRUCTION_SITES); break; - } - - var response; - if(creep.pos.isNearTo(target)) { - switch (jobMemory.counter%3) { - case 0: response = creep.upgradeController(target); break; - case 1: response = creep.transfer(target, RESOURCE_ENERGY); break; - case 2: response = creep.build(target); break; - } - if(response == ERR_FULL) creep.memory.job.counter += 1; - } - else creep.moveTo(target); - if(!creep.store.getUsedCapacity(RESOURCE_ENERGY)) { - jobMemory.harvesting = true; - creep.memory.job.counter += 1; - } + if(creep.memory.harvesting) { + pickUpEnergy(creep); + onEnergyFullTurnToJobMode(creep); + } + else { + performJobs(creep); + onOutOfEnergyTurnHarvestingMode(creep); } } +} + +function pickUpEnergy(creep){ + if(!creep.memory.target) { + const target = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES, {filter: {resourceType: RESOURCE_ENERGY}}); + if(target) creep.memory.target = target.id; + } + const resource = Game.getObjectById(creep.memory.target); + if(!resource) creep.memory.target = undefined; + if(creep.pos.isNearTo(resource)) creep.pickup(resource); + else creep.moveTo(resource); +} + +function harvestSource(creep){ + if(!creep.memory.target) creep.memory.target = getSourceIdFromRoomTable(creep); + var source = Game.getObjectById(creep.memory.target); + if(creep.pos.isNearTo(source)) creep.harvest(source); + else creep.moveTo(source); +} + +function onEnergyFullTurnToJobMode(creep){ + if(!creep.store.getFreeCapacity(RESOURCE_ENERGY)) { + creep.memory.harvesting = false; + creep.memory.target = undefined; + } +} + +function performJobs(creep){ + if(!creep.memory.target) creep.memory.target = getJobTarget(creep); + var target = Game.getObjectById(creep.memory.target); + if(creep.pos.isNearTo(target)) doJob(creep, target); + else creep.moveTo(target); +} + +function onOutOfEnergyTurnHarvestingMode(creep){ + if(!creep.store.getUsedCapacity(RESOURCE_ENERGY)) { + creep.memory.harvesting = true; + creep.memory.target = undefined; + creep.memory.counter += 1; + } +} + +function getSourceIdFromRoomTable(creep) { + const sourceTable = creep.room.memory.layout.sources; + const sourceIterator = creep.memory.counter%creep.room.memory.layout.sources.length; + const sourceId = sourceTable[sourceIterator]; + return sourceId; +} + +function getJobTarget(creep) { + var target; + switch (creep.memory.counter%3) { + case 0: target = creep.room.controller; break; + case 1: target = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {filter:{structureType:STRUCTURE_SPAWN}}); break; + case 2: target = creep.pos.findClosestByRange(FIND_MY_CONSTRUCTION_SITES); break; + } + if(target) return target.id; + creep.memory.counter++; + return undefined; +} + +function doJob(creep, target){ + var response; + switch (creep.memory.counter%3) { + case 0: response = creep.upgradeController(target); break; + case 1: response = creep.transfer(target, RESOURCE_ENERGY); break; + case 2: response = creep.build(target); break; + } + if(response == ERR_FULL) { + creep.memory.counter++; + creep.memory.target = undefined; + } } \ No newline at end of file diff --git a/JobMiner.js b/JobMiner.js new file mode 100644 index 0000000..5b06ba1 --- /dev/null +++ b/JobMiner.js @@ -0,0 +1,18 @@ +module.exports = { + begin(creep){}, + + tick(creep){ + const source = Game.getObjectById(creep.memory.job.source); + if(creep.memory.aboveContainer) creep.harvest(source); + if(!creep.pos.isNearTo(source)) { creep.moveTo(source); return; } + if(creep.memory.container) { + const container = Game.getObjectById(creep.memory.container); + if(container.pos.x === creep.pos.x, container.pos.y === creep.pos.y) creep.memory.aboveContainer = true; + else creep.moveTo(container); + } else{ + const container = source.pos.findInRange(FIND_STRUCTURES, 1, {filter:{structureType:STRUCTURE_CONTAINER}})[0]; + if(container) creep.memory.container = container.id; + else creep.harvest(source); + } + } +} \ No newline at end of file diff --git a/JobSupplier.js b/JobSupplier.js new file mode 100644 index 0000000..9d100dc --- /dev/null +++ b/JobSupplier.js @@ -0,0 +1,8 @@ +module.exports = { + begin(creep){ + }, + + tick(creep){ + + } +} \ No newline at end of file diff --git a/JobUpgrader.js b/JobUpgrader.js new file mode 100644 index 0000000..9d100dc --- /dev/null +++ b/JobUpgrader.js @@ -0,0 +1,8 @@ +module.exports = { + begin(creep){ + }, + + tick(creep){ + + } +} \ No newline at end of file diff --git a/RoomClass.js b/RoomClass.js new file mode 100644 index 0000000..2ff017b --- /dev/null +++ b/RoomClass.js @@ -0,0 +1,39 @@ +module.exports = { + setup: function () { Room.prototype = _Room.prototype; } +} + +class _Room extends Room { + begin(){ + roomScan(this); + jobScan(this); + vacancyScan(this); + this.memory.init = true; + } + + tick(){ + if(!this.memory.init) this.begin(); + if(Game.time%100 === 1) vacancyScan(this); + } +} + +function roomScan(room){ + room.memory.layout = { + sources: room.find(FIND_SOURCES).map(s=>s.id) + }; +} + +function jobScan(room){ + if(!room.memory.jobs) room.memory.jobs = Array(10).fill({role: Role.HARVESTER}) + .concat(room.memory.layout.sources.map(s=> {return {role: Role.MINER, source: s}})); +} + +function vacancyScan(room){ + const activeJobs = room.find(FIND_MY_CREEPS).map(creep=>creep.memory.job); + const jobs = [].concat(room.memory.jobs); + activeJobs.forEach(job => { + const index = jobs.indexOf(job); + if(0 <= index) jobs.splice(index,1); + }); + room.memory.vacancies = jobs; +} + diff --git a/Structure.js b/Structure.js new file mode 100644 index 0000000..9343958 --- /dev/null +++ b/Structure.js @@ -0,0 +1,21 @@ +const spawnClass = require("StructureSpawnClass"); + +module.exports = { + setup() { + spawnClass.setup(); + }, + + begin(structure){ + switch (structure.structureType) { + case STRUCTURE_SPAWN: structure.begin(); break; + default: break; + } + }, + + tick(structure){ + switch (structure.structureType) { + case STRUCTURE_SPAWN: structure.tick(); break; + default: break; + } + } +} \ No newline at end of file diff --git a/StructureSpawnClass.js b/StructureSpawnClass.js new file mode 100644 index 0000000..99bd4e9 --- /dev/null +++ b/StructureSpawnClass.js @@ -0,0 +1,42 @@ +module.exports = { + setup: function () { StructureSpawn.prototype = _StructureSpawn.prototype; } +} + +class _StructureSpawn extends StructureSpawn { + begin(){ + if (!this.memory.creepCounter) this.memory.creepCounter = 0; + this.memory.init = true; + } + + tick(){ + if(!this.memory.init) this.begin(); + const job = this.room.memory.vacancies.pop(); + if(job){ + const name = getJobName(job.role); + const body = getBodyByJob(job.role); + if(this.createCreep(job, name, body) != OK) this.room.memory.vacancies.push(job); + } + } + + createCreep(job, name, body) { + const response = this.spawnCreep(body, name + ": " + this.memory.creepCounter, { + memory: { job: job } + }); + if (response == OK) this.memory.creepCounter++; + return response; + } +} + +function getJobName(role){ + switch (role) { + case Role.HARVESTER: return "Harvy"; + case Role.MINER: return "minny"; + } +} + +function getBodyByJob(role){ + switch (role) { + case Role.HARVESTER: return [WORK, CARRY, MOVE, MOVE]; + case Role.MINER: return [WORK, WORK, MOVE]; + } +} \ No newline at end of file diff --git a/main.js b/main.js index ee56e5d..ac48d7c 100644 --- a/main.js +++ b/main.js @@ -1,11 +1,7 @@ -// ######################################## -// ########## MAIN ########## -// ######################################## - - -const creepClass = require("ClassCreep"); -const roomClass = require("ClassRoom"); -const spawnClass = require("ClassSpawn"); +const Commands = require("Commands"); +const CreepClass = require("CreepClass"); +const RoomClass = require("RoomClass"); +const Structure = require("Structure"); module.exports.loop = function () { if(!global.compiled) onRecompile(); @@ -13,28 +9,34 @@ module.exports.loop = function () { else onTick(); } -function onRecompile(){ - creepClass.setup(); - roomClass.setup(); - spawnClass.setup(); +function onRecompile(){ + setupClasses(); console.log("Script recompiled..."); global.compiled = true; } function onRestart(){ Object.values(Game.rooms).forEach(room => room.begin()); - Object.values(Game.spawns).forEach(spawn => spawn.begin()); Object.values(Game.creeps).forEach(creep => creep.begin()); + Object.values(Game.structures).forEach(structure => Structure.begin(structure)); global.started = true; } function onTick(){ Object.values(Game.rooms).forEach(room => room.tick()); - Object.values(Game.spawns).forEach(spawn => spawn.tick()); Object.values(Game.creeps).forEach(creep => creep.tick()); + Object.values(Game.structures).forEach(structure => Structure.tick(structure)); if(!(Game.time % 100)) cleanUp(); } + +function setupClasses(){ + Commands.setup(); + CreepClass.setup(); + RoomClass.setup(); + Structure.setup(); +} + function cleanUp(){ Object.keys(Memory.rooms).forEach(roomName => { if(!Game.rooms[roomName]) Memory.rooms[roomName] = undefined; }); Object.keys(Memory.creeps).forEach(creepName => { if(!Game.creeps[creepName]) Memory.creeps[creepName] = undefined; });