First working version of new system.
This commit is contained in:
147
ClassCreep.js
147
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);
|
||||
}
|
||||
}
|
||||
|
||||
17
ClassRoom.js
17
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
|
||||
|
||||
}
|
||||
|
||||
@@ -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++;
|
||||
}
|
||||
}
|
||||
|
||||
0
_commands.js
Normal file
0
_commands.js
Normal file
25
_creeps.js
Normal file
25
_creeps.js
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
module.exports = {
|
||||
// Gets called when recompiling and when Restart command is called.
|
||||
start: function () {
|
||||
},
|
||||
|
||||
// Gets called every tick.
|
||||
update: function () {
|
||||
}
|
||||
}
|
||||
15
_spawns.js
Normal file
15
_spawns.js
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user