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(); }