Improved harvester behaviour, reworked the job register system and added auto roadbuilding.
This commit is contained in:
11
Class.js
11
Class.js
@@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
const jobHarvester = require("JobHarvester");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
setup: function () {
|
setup: function () {
|
||||||
Creep.prototype = _Creep.prototype;
|
Creep.prototype = _Creep.prototype;
|
||||||
@@ -9,8 +11,23 @@ const Role = {
|
|||||||
HARVESTER: 0
|
HARVESTER: 0
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
class _Creep extends Creep {
|
||||||
setup: function () { Creep.prototype = _Creep.prototype; }
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
class _Creep extends Creep {}
|
tick(){
|
||||||
|
if(!this.memory.init) this.begin();
|
||||||
|
switch (this.memory.job.role) {
|
||||||
|
case Role.HARVESTER: jobHarvester.tick(this); break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this.ticksToLive == 10)
|
||||||
|
this.room.memory.jobs.push(this.memory.job);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
46
ClassRoom.js
46
ClassRoom.js
@@ -2,5 +2,49 @@ module.exports = {
|
|||||||
setup: function () { Room.prototype = _Room.prototype; }
|
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))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,18 +2,30 @@ module.exports = {
|
|||||||
setup: function () { StructureSpawn.prototype = _Spawn.prototype; }
|
setup: function () { StructureSpawn.prototype = _Spawn.prototype; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class _Spawn extends StructureSpawn {
|
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() {
|
createHarvester() {
|
||||||
var body = [WORK, CARRY, MOVE];
|
|
||||||
this.createCreep(body, "harvester", Role.HARVESTER);
|
this.createCreep(body, "harvester", Role.HARVESTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
createCreep(body, name, role) {
|
createCreep(body, name, job) {
|
||||||
if (this.memory.creepCounter == undefined) this.memory.creepCounter = 0;
|
|
||||||
const response = this.spawnCreep(body, name + ": " + this.memory.creepCounter, {
|
const response = this.spawnCreep(body, name + ": " + this.memory.creepCounter, {
|
||||||
memory: { job: { role: role } }
|
memory: { job: job }
|
||||||
});
|
});
|
||||||
if (response == OK) this.memory.creepCounter++;
|
if (response == OK) this.memory.creepCounter++;
|
||||||
return response;
|
return response;
|
||||||
|
|||||||
26
Job.js
26
Job.js
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -3,12 +3,15 @@ module.exports = {
|
|||||||
const jobMemory = creep.memory.job;
|
const jobMemory = creep.memory.job;
|
||||||
if(!jobMemory.harvesting) creep.memory.job.harvesting = false;
|
if(!jobMemory.harvesting) creep.memory.job.harvesting = false;
|
||||||
if(!jobMemory.counter) creep.memory.job.counter = 0;
|
if(!jobMemory.counter) creep.memory.job.counter = 0;
|
||||||
|
creep.memory.init = true;
|
||||||
},
|
},
|
||||||
|
|
||||||
tick(creep){
|
tick(creep){
|
||||||
const jobMemory = creep.memory.job;
|
const jobMemory = creep.memory.job;
|
||||||
|
if(!jobMemory.init) this.begin(creep);
|
||||||
if(jobMemory.harvesting){
|
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);
|
if(creep.pos.isNearTo(source)) creep.harvest(source);
|
||||||
else creep.moveTo(source);
|
else creep.moveTo(source);
|
||||||
if(!creep.store.getFreeCapacity(RESOURCE_ENERGY)) jobMemory.harvesting = false;
|
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;
|
case 2: target = creep.pos.findClosestByRange(FIND_MY_CONSTRUCTION_SITES); break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var response;
|
||||||
if(creep.pos.isNearTo(target)) {
|
if(creep.pos.isNearTo(target)) {
|
||||||
switch (jobMemory.counter%3) {
|
switch (jobMemory.counter%3) {
|
||||||
case 0: creep.upgradeController(target); break;
|
case 0: response = creep.upgradeController(target); break;
|
||||||
case 1: creep.transfer(target, RESOURCE_ENERGY); break;
|
case 1: response = creep.transfer(target, RESOURCE_ENERGY); break;
|
||||||
case 2: creep.build(target); break;
|
case 2: response = creep.build(target); break;
|
||||||
}
|
}
|
||||||
|
if(response == ERR_FULL) creep.memory.job.counter += 1;
|
||||||
}
|
}
|
||||||
else creep.moveTo(target);
|
else creep.moveTo(target);
|
||||||
if(!creep.store.getUsedCapacity(RESOURCE_ENERGY)) {
|
if(!creep.store.getUsedCapacity(RESOURCE_ENERGY)) {
|
||||||
|
|||||||
7
Math.js
Normal file
7
Math.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
module.exports = {
|
||||||
|
clamp: function(v, min, max){
|
||||||
|
if(v<min) v = min;
|
||||||
|
else if (max < v) v = max;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
}
|
||||||
17
Room.js
17
Room.js
@@ -1,17 +0,0 @@
|
|||||||
module.exports = {
|
|
||||||
setup(){},
|
|
||||||
|
|
||||||
begin(room){
|
|
||||||
if(!room.memory.jobs) room.memory.jobs = {
|
|
||||||
harvesters: 1
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
tick(room){
|
|
||||||
if(!(Game.time % 100)) calculateJobs(room);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function calculateJobs(room) {
|
|
||||||
room.memory.jobs.harvesters = 10 - Object.keys(Game.creeps).length;
|
|
||||||
}
|
|
||||||
17
Structure.js
17
Structure.js
@@ -1,17 +0,0 @@
|
|||||||
const StructureSpawn = require("StructureSpawn")
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
setup(){},
|
|
||||||
|
|
||||||
begin(structure){
|
|
||||||
switch (structure.structureType) {
|
|
||||||
case STRUCTURE_SPAWN: StructureSpawn.begin(structure); break;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
tick(structure){
|
|
||||||
switch (structure.structureType) {
|
|
||||||
case STRUCTURE_SPAWN: StructureSpawn.tick(structure); break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
module.exports = {
|
|
||||||
setup(){},
|
|
||||||
|
|
||||||
begin(spawn){
|
|
||||||
},
|
|
||||||
|
|
||||||
tick(spawn){
|
|
||||||
if(spawn.room.memory.jobs.harvesters) {
|
|
||||||
if(spawn.createHarvester());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
24
main.js
24
main.js
@@ -3,10 +3,9 @@
|
|||||||
// ########################################
|
// ########################################
|
||||||
|
|
||||||
|
|
||||||
const Class = require("Class");
|
const creepClass = require("ClassCreep");
|
||||||
const Job = require("Job");
|
const roomClass = require("ClassRoom");
|
||||||
const Room = require("Room");
|
const spawnClass = require("ClassSpawn");
|
||||||
const Structure = require("Structure");
|
|
||||||
|
|
||||||
module.exports.loop = function () {
|
module.exports.loop = function () {
|
||||||
if(!global.compiled) onRecompile();
|
if(!global.compiled) onRecompile();
|
||||||
@@ -15,23 +14,24 @@ module.exports.loop = function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function onRecompile(){
|
function onRecompile(){
|
||||||
Class.setup();
|
creepClass.setup();
|
||||||
Job.setup();
|
roomClass.setup();
|
||||||
|
spawnClass.setup();
|
||||||
console.log("Script recompiled...");
|
console.log("Script recompiled...");
|
||||||
global.compiled = true;
|
global.compiled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function onRestart(){
|
function onRestart(){
|
||||||
Object.values(Game.rooms).forEach(room => Room.begin(room));
|
Object.values(Game.rooms).forEach(room => room.begin());
|
||||||
Object.values(Game.spawns).forEach(spawn => Structure.begin(spawn));
|
Object.values(Game.spawns).forEach(spawn => spawn.begin());
|
||||||
Object.values(Game.creeps).forEach(creep => Job.begin(creep));
|
Object.values(Game.creeps).forEach(creep => creep.begin());
|
||||||
global.started = true;
|
global.started = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function onTick(){
|
function onTick(){
|
||||||
Object.values(Game.rooms).forEach(room => Room.tick(room));
|
Object.values(Game.rooms).forEach(room => room.tick());
|
||||||
Object.values(Game.spawns).forEach(spawn => Structure.tick(spawn));
|
Object.values(Game.spawns).forEach(spawn => spawn.tick());
|
||||||
Object.values(Game.creeps).forEach(creep => Job.tick(creep));
|
Object.values(Game.creeps).forEach(creep => creep.tick());
|
||||||
if(!(Game.time % 100)) cleanUp();
|
if(!(Game.time % 100)) cleanUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user