diff --git a/ClassCreep.js b/ClassCreep.js index 7e367d8..785bda4 100644 --- a/ClassCreep.js +++ b/ClassCreep.js @@ -4,5 +4,152 @@ module.exports = { class _Creep extends Creep { init() { + if (this.memory.role == undefined) this.memory.role = ROLE_HARVESTER; + if (this.memory.recharge == undefined) this.memory.recharge = true; + } + + toggleChargeMode() { + if (this.store.getFreeCapacity(RESOURCE_ENERGY) == 0) { + this.memory.recharge = false; + } else if (this.store.getUsedCapacity(RESOURCE_ENERGY) == 0) { + this.memory.recharge = true; + } else return; + this.memory.target = undefined; + this.memory.path = undefined; + } + + findTarget(target) { + if (this.memory.target == undefined) { + this.memory.target = this.pos.findClosestByPath(target); + if (this.memory.target == null) { + this.memory.target = undefined; + return ERR_NOT_FOUND; + } + } + return OK; + } + + findTarget(target, filter) { + if (this.memory.target == undefined) { + this.memory.target = this.pos.findClosestByPath(target, filter); + if (this.memory.target == null) { + this.memory.target = undefined; + return ERR_NOT_FOUND; + } + } + return OK; + } + + harvestSource() { + if (this.findTarget(FIND_SOURCES) == ERR_NOT_FOUND) return; + const target = Game.getObjectById(this.memory.target.id); + if (this.moveToTarget() == OK) { + this.harvest(target); + this.toggleChargeMode(); + } + } + + harvesterJobs() { + if (this.memory.counter == undefined) this.memory.counter = 0; + switch (this.memory.counter % 4) { + case 0: this.supplyStructures(); + break; + case 1: this.upgradeRoom(); + break; + case 2: this.repairStructures(); + break; + case 3: this.buildConstructionSites(); + break; + default: + this.memory.counter++; + break; + } + if (this.memory.target == undefined) this.memory.counter++; + } + + supplyStructures() { + if (this.findTarget(FIND_MY_STRUCTURES, { + filter: (s) => { + return s.structureType == STRUCTURE_SPAWN || + s.structureType == STRUCTURE_EXTENSION || + s.structureType == STRUCTURE_TOWER || + s.structureType == STRUCTURE_STORAGE; + } + }) == ERR_NOT_FOUND) return; + const target = Game.getObjectById(this.memory.target.id); + if (this.moveToTarget() == OK) { + this.transfer(target, RESOURCE_ENERGY); + this.toggleChargeMode(); + } + } + + upgradeRoom() { + if (this.findTarget(FIND_MY_STRUCTURES, { + filter: (s) => { + return s.structureType == STRUCTURE_CONTROLLER; + } + }) == ERR_NOT_FOUND) return; + const target = Game.getObjectById(this.memory.target.id); + if (this.moveToTarget() == OK) { + this.upgradeController(); + this.toggleChargeMode(); + } + } + + repairStructures() { + if (this.findTarget(FIND_MY_STRUCTURES, { + filter: (s) => { + return s.hits < s.hitsMax && + s.structureType != STRUCTURE_WALL && + s.structureType != STRUCTURE_RAMPART; + } + }) == ERR_NOT_FOUND); + if (this.findTarget(FIND_MY_STRUCTURES, { + filter: (s) => { + return s.hits < s.hitsMax; + } + }) == ERR_NOT_FOUND) return; + + const target = Game.getObjectById(this.memory.target.id); + if (this.moveToTarget() == OK) { + this.repair(target); + this.toggleChargeMode(); + } + } + + buildConstructionSites() { + if (this.findTarget(FIND_MY_CONSTRUCTION_SITES) == ERR_NOT_FOUND) return; + const target = Game.getObjectById(this.memory.target.id); + if (this.moveToTarget() == OK) { + this.build(target); + this.toggleChargeMode(); + } + } + + // Moves to the target. + moveToTarget() { + if (this.pos.inRangeTo(this.memory.target.pos.x, this.memory.target.pos.y, 1)) { + this.memory.path = undefined; + return OK; + } + if (this.memory.path == undefined) { + var path = this.pos.findPathTo(this.memory.target.pos, { + ignoreCreeps: true, range: 1 + }); + path.reverse(); + this.memory.path = path; + } + const step = this.memory.path.pop(); + var response = this.move(step.direction); + if (response == OK) { + if (this.pos.x == step.x && + this.pos.y == step.y) return ERR_BUSY; + response = this.moveTo(this.memory.target.pos.x, this.memory.target.pos.y); + } + if (response == OK || response == ERR_INVALID_ARGS || response == ERR_NOT_FOUND) { + this.memory.path = undefined; + return ERR_BUSY; + } + this.memory.path.push(step); } } diff --git a/ClassRoom.js b/ClassRoom.js index 0f7c3a1..67fe4dd 100644 --- a/ClassRoom.js +++ b/ClassRoom.js @@ -13,6 +13,8 @@ class _Room extends Room { if (this.controller.my && this.memory.state == ROOMSTATE_NEUTRAL) this.memory.state = ROOMSTATE_CAPITAL; } + //#region Checks + // Is the level changed? isNewLevel() { if (this.memory.level == undefined) this.memory.level = -1; @@ -21,7 +23,11 @@ class _Room extends Room { return value; } + // Is the max amount of construction sites achieved? isConstructionQueueFull() { return this.find(FIND_CONSTRUCTION_SITES).length > 90; } + //#endregion + + //#region Construcion // Place construction sites for the road system. planRoads() { @@ -45,12 +51,18 @@ class _Room extends Room { target_array[index++] = structure.pos; }); - // Also add all sources + // Add all sources to targets. this.find(FIND_SOURCES).forEach(source => { target_array[index++] = source.pos; }); + // Add minerals to targers + this.find(FIND_MINERALS).forEach(mineral => { + target_array[index++] = mineral.pos; + }); + // Build a road for every permutation of the targets. + // TODO: When performance to high make this loop recoverable. for (let i = 0; i < target_array.length; i++) { const begin = target_array[i]; for (let j = 0; j < target_array.length; j++) { @@ -72,4 +84,7 @@ class _Room extends Room { planStructures() { } + + //#endregion + } diff --git a/ClassSpawn.js b/ClassSpawn.js index e440a78..d43fb53 100644 --- a/ClassSpawn.js +++ b/ClassSpawn.js @@ -6,4 +6,32 @@ module.exports = { class _Spawn extends StructureSpawn { init() { } + + requireNewCreep(role) { + const roleCount = this.room.find(FIND_MY_CREEPS, { + filter: (c) => { return c.memory.role == role; } + }).length; + + switch (role) { + case ROLE_HARVESTER: + return roleCount < 5; + break; + + default: + break; + } + } + + 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; + const response = this.spawnCreep(body, name + ": " + this.memory.creepCounter, { + memory: { role: role } + }); + if (response == OK) this.memory.creepCounter++; + } } diff --git a/_commands.js b/_commands.js new file mode 100644 index 0000000..e69de29 diff --git a/_creeps.js b/_creeps.js new file mode 100644 index 0000000..5b1afbf --- /dev/null +++ b/_creeps.js @@ -0,0 +1,25 @@ +module.exports = { + // Gets called when recompiling and when Restart command is called. + start: function () { + for (const creep_name in Game.creeps) { + const creep = Game.creeps[creep_name]; + creep.init(); + } + }, + + // Gets called every tick. + update: function () { + for (const creep_name in Memory.creeps) { + const creep = Game.creeps[creep_name]; + if (creep == undefined) delete Memory.creeps[creep_name]; + switch (creep.memory.role) { + case ROLE_HARVESTER: + if (creep.memory.recharge) creep.harvestSource(); + else creep.harvesterJobs(); + break; + default: + break; + } + } + } +} \ No newline at end of file diff --git a/_roles.js b/_roles.js deleted file mode 100644 index 8de96c7..0000000 --- a/_roles.js +++ /dev/null @@ -1,9 +0,0 @@ -module.exports = { - // Gets called when recompiling and when Restart command is called. - start: function () { - }, - - // Gets called every tick. - update: function () { - } -} \ No newline at end of file diff --git a/_spawns.js b/_spawns.js new file mode 100644 index 0000000..4fda0fd --- /dev/null +++ b/_spawns.js @@ -0,0 +1,15 @@ +module.exports = { + start: function () { + for (const spawn_name in Game.spawns) { + const spawn = Game.spawns[spawn_name]; + spawn.init(); + } + }, + + update: function () { + for (const spawn_name in Game.spawns) { + const spawn = Game.spawns[spawn_name]; + if (spawn.requireNewCreep(ROLE_HARVESTER)) spawn.createHarvester(); + } + } +} \ No newline at end of file diff --git a/_system.js b/_system.js index d275fbd..4940eec 100644 --- a/_system.js +++ b/_system.js @@ -2,15 +2,21 @@ * Basis of the entire system. Everything will be managed from here on out. */ const rooms = require("_rooms") +const spawns = require("_spawns") +const creeps = require("_creeps") module.exports = { // Gets called when recompiling and when Restart command is called. start: function () { rooms.start(); + spawns.start(); + creeps.start(); }, // Gets called every tick. update: function () { rooms.update(); + spawns.update(); + creeps.update(); } } diff --git a/main.js b/main.js index 0955097..81a562d 100644 --- a/main.js +++ b/main.js @@ -41,4 +41,8 @@ function setConstants() { global.ROOMSTATE_CAPITAL = 1; global.ROOMSTATE_COLONY = 2; global.ROOMSTATE_OUTPOST = 3; + + // The roles of creeps. + global.ROLE_HARVESTER = 0; + } \ No newline at end of file