From 8c3528419a4ff13ef53fa2593a2b86d3d938bc25 Mon Sep 17 00:00:00 2001 From: Douwe Ravers Date: Fri, 25 Aug 2023 08:48:57 +0200 Subject: [PATCH 1/3] Implemented upgrader, source refill and builder functions to harvester. --- JobHarvester.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/JobHarvester.js b/JobHarvester.js index d5f5d55..29d65d7 100644 --- a/JobHarvester.js +++ b/JobHarvester.js @@ -2,6 +2,7 @@ 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; }, tick(creep){ @@ -12,10 +13,25 @@ module.exports = { else creep.moveTo(source); if(!creep.store.getFreeCapacity(RESOURCE_ENERGY)) jobMemory.harvesting = false; } else{ - const controller = creep.room.controller; - if(creep.pos.isNearTo(controller)) creep.upgradeController(controller); - else creep.moveTo(controller); - if(!creep.store.getUsedCapacity(RESOURCE_ENERGY)) jobMemory.harvesting = true; + 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; + } + + if(creep.pos.isNearTo(target)) { + switch (jobMemory.counter%3) { + case 0: creep.upgradeController(target); break; + case 1: creep.transfer(target, RESOURCE_ENERGY); break; + case 2: creep.build(target); break; + } + } + else creep.moveTo(target); + if(!creep.store.getUsedCapacity(RESOURCE_ENERGY)) { + jobMemory.harvesting = true; + creep.memory.job.counter += 1; + } } } } \ No newline at end of file From 76650e22ac3843cbd11eb1342191d88a212d287d Mon Sep 17 00:00:00 2001 From: Douwe Ravers Date: Fri, 25 Aug 2023 17:30:27 +0200 Subject: [PATCH 2/3] Improved harvester behaviour, reworked the job register system and added auto roadbuilding. --- Class.js | 11 ----------- ClassCreep.js | 25 +++++++++++++++++++++---- ClassRoom.js | 46 +++++++++++++++++++++++++++++++++++++++++++++- ClassSpawn.js | 22 +++++++++++++++++----- Job.js | 26 -------------------------- JobHarvester.js | 15 ++++++++++----- Math.js | 7 +++++++ Room.js | 17 ----------------- Structure.js | 17 ----------------- StructureSpawn.js | 12 ------------ main.js | 26 +++++++++++++------------- 11 files changed, 113 insertions(+), 111 deletions(-) delete mode 100644 Class.js delete mode 100644 Job.js create mode 100644 Math.js delete mode 100644 Room.js delete mode 100644 Structure.js delete mode 100644 StructureSpawn.js diff --git a/Class.js b/Class.js deleted file mode 100644 index 6800893..0000000 --- a/Class.js +++ /dev/null @@ -1,11 +0,0 @@ -const creepClass = require("ClassCreep"); -const roomClass = require("ClassRoom"); -const spawnClass = require("ClassSpawn"); - -module.exports = { - setup(){ - creepClass.setup(); - roomClass.setup(); - spawnClass.setup(); - } -} diff --git a/ClassCreep.js b/ClassCreep.js index 3e107fa..d293617 100644 --- a/ClassCreep.js +++ b/ClassCreep.js @@ -1,3 +1,5 @@ +const jobHarvester = require("JobHarvester"); + module.exports = { setup: function () { Creep.prototype = _Creep.prototype; @@ -9,8 +11,23 @@ const Role = { HARVESTER: 0 } -module.exports = { - setup: function () { Creep.prototype = _Creep.prototype; } -} +class _Creep extends Creep { + begin(){ + if(!this.memory.job) this.memory.job = { role: Role.HARVESTER }; + switch (this.memory.job.role) { + case Role.HARVESTER: jobHarvester.begin(this); break; + } + this.memory.init = true; + } + + tick(){ + if(!this.memory.init) this.begin(); + switch (this.memory.job.role) { + case Role.HARVESTER: jobHarvester.tick(this); break; + } -class _Creep extends Creep {} + if(this.ticksToLive == 10) + this.room.memory.jobs.push(this.memory.job); + } + +} diff --git a/ClassRoom.js b/ClassRoom.js index 3b567d9..6cbfe16 100644 --- a/ClassRoom.js +++ b/ClassRoom.js @@ -2,5 +2,49 @@ module.exports = { setup: function () { Room.prototype = _Room.prototype; } } -class _Room extends Room {} +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 index 2d4ce96..479eaa3 100644 --- a/ClassSpawn.js +++ b/ClassSpawn.js @@ -2,18 +2,30 @@ 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() { - var body = [WORK, CARRY, MOVE]; this.createCreep(body, "harvester", Role.HARVESTER); } - createCreep(body, name, role) { - if (this.memory.creepCounter == undefined) this.memory.creepCounter = 0; + createCreep(body, name, job) { const response = this.spawnCreep(body, name + ": " + this.memory.creepCounter, { - memory: { job: { role: role } } + memory: { job: job } }); if (response == OK) this.memory.creepCounter++; return response; diff --git a/Job.js b/Job.js deleted file mode 100644 index 228a6fc..0000000 --- a/Job.js +++ /dev/null @@ -1,26 +0,0 @@ -const jobHarvester = require("JobHarvester"); - -const Role = { - HARVESTER: 0 -} - - -module.exports = { - setup(){ - global.Role = Role; - }, - - begin(creep){ - if(!creep.memory.job) creep.memory.job = { role: Role.HARVESTER }; - - switch (creep.memory.job.role) { - case Role.HARVESTER: jobHarvester.tick(creep); break; - } - }, - - tick(creep){ - switch (creep.memory.job.role) { - case Role.HARVESTER: jobHarvester.tick(creep); break; - } - } -} \ No newline at end of file diff --git a/JobHarvester.js b/JobHarvester.js index 29d65d7..d54acf2 100644 --- a/JobHarvester.js +++ b/JobHarvester.js @@ -3,12 +3,15 @@ module.exports = { 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; }, tick(creep){ - const jobMemory = creep.memory.job; + const jobMemory = creep.memory.job; + if(!jobMemory.init) this.begin(creep); if(jobMemory.harvesting){ - const source = creep.pos.findClosestByRange(FIND_SOURCES); + 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; @@ -20,12 +23,14 @@ module.exports = { case 2: target = creep.pos.findClosestByRange(FIND_MY_CONSTRUCTION_SITES); break; } + var response; if(creep.pos.isNearTo(target)) { switch (jobMemory.counter%3) { - case 0: creep.upgradeController(target); break; - case 1: creep.transfer(target, RESOURCE_ENERGY); break; - case 2: creep.build(target); break; + 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)) { diff --git a/Math.js b/Math.js new file mode 100644 index 0000000..206b162 --- /dev/null +++ b/Math.js @@ -0,0 +1,7 @@ +module.exports = { + clamp: function(v, min, max){ + if(v Room.begin(room)); - Object.values(Game.spawns).forEach(spawn => Structure.begin(spawn)); - Object.values(Game.creeps).forEach(creep => Job.begin(creep)); + Object.values(Game.rooms).forEach(room => room.begin()); + Object.values(Game.spawns).forEach(spawn => spawn.begin()); + Object.values(Game.creeps).forEach(creep => creep.begin()); global.started = true; } function onTick(){ - Object.values(Game.rooms).forEach(room => Room.tick(room)); - Object.values(Game.spawns).forEach(spawn => Structure.tick(spawn)); - Object.values(Game.creeps).forEach(creep => Job.tick(creep)); + Object.values(Game.rooms).forEach(room => room.tick()); + Object.values(Game.spawns).forEach(spawn => spawn.tick()); + Object.values(Game.creeps).forEach(creep => creep.tick()); if(!(Game.time % 100)) cleanUp(); } From e294ad29fe263d2f319a845b0ed929d3ef3071d6 Mon Sep 17 00:00:00 2001 From: Douwe Ravers Date: Sat, 26 Aug 2023 16:57:38 +0200 Subject: [PATCH 3/3] Created miner, made harvester pick resources and added reset commands. --- ClassRoom.js | 50 -------------- ClassSpawn.js | 33 ---------- Commands.js | 61 +++++++++++++++++ ClassCreep.js => CreepClass.js | 13 ++-- JobBuilder.js | 8 +++ JobHarvester.js | 115 +++++++++++++++++++++++---------- JobMiner.js | 18 ++++++ JobSupplier.js | 8 +++ JobUpgrader.js | 8 +++ RoomClass.js | 39 +++++++++++ Structure.js | 21 ++++++ StructureSpawnClass.js | 42 ++++++++++++ main.js | 30 +++++---- 13 files changed, 310 insertions(+), 136 deletions(-) delete mode 100644 ClassRoom.js delete mode 100644 ClassSpawn.js create mode 100644 Commands.js rename ClassCreep.js => CreepClass.js (72%) create mode 100644 JobBuilder.js create mode 100644 JobMiner.js create mode 100644 JobSupplier.js create mode 100644 JobUpgrader.js create mode 100644 RoomClass.js create mode 100644 Structure.js create mode 100644 StructureSpawnClass.js 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; });