Improved harvester behaviour, reworked the job register system and added auto roadbuilding.

This commit is contained in:
Douwe Ravers
2023-08-25 17:30:27 +02:00
parent 8c3528419a
commit 76650e22ac
11 changed files with 113 additions and 111 deletions

View File

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

View File

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

View File

@@ -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))
}
}

View File

@@ -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;

26
Job.js
View File

@@ -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;
}
}
}

View File

@@ -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)) {

7
Math.js Normal file
View 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
View File

@@ -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;
}

View File

@@ -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;
}
}
}

View File

@@ -1,12 +0,0 @@
module.exports = {
setup(){},
begin(spawn){
},
tick(spawn){
if(spawn.room.memory.jobs.harvesters) {
if(spawn.createHarvester());
}
}
}

26
main.js
View File

@@ -3,11 +3,10 @@
// ########################################
const Class = require("Class");
const Job = require("Job");
const Room = require("Room");
const Structure = require("Structure");
const creepClass = require("ClassCreep");
const roomClass = require("ClassRoom");
const spawnClass = require("ClassSpawn");
module.exports.loop = function () {
if(!global.compiled) onRecompile();
else if (!global.started) onRestart();
@@ -15,23 +14,24 @@ module.exports.loop = function () {
}
function onRecompile(){
Class.setup();
Job.setup();
creepClass.setup();
roomClass.setup();
spawnClass.setup();
console.log("Script recompiled...");
global.compiled = true;
}
function onRestart(){
Object.values(Game.rooms).forEach(room => 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();
}