Files
screeps/StructureSpawnClass.js
2023-08-31 19:13:22 +02:00

64 lines
1.9 KiB
JavaScript

module.exports = {
setup: function () { StructureSpawn.prototype = _StructureSpawn.prototype; }
}
class _StructureSpawn extends StructureSpawn {
begin(){
if (!this.memory.creepCounter) this.memory.creepCounter = 0;
this.memory.init = true;
}
tick(){
if(!this.memory.init) this.begin();
if(Game.time%100) return;
if(this.room.energyAvailable < 300) return;
const job = this.room.memory.vacancies.pop();
if(job){
const name = getJobName(job.role);
const body = getBodyByJob(job.role, this.room.energyAvailable);
if(this.createCreep(job, name, body) != OK) this.room.memory.vacancies.push(job);
}
}
createCreep(job, name, body) {
const response = this.spawnCreep(body, name + ": " + this.memory.creepCounter, {
memory: { job: job }
});
if (response == OK) this.memory.creepCounter++;
return response;
}
}
function getJobName(role){
switch (role) {
case Role.BUILDER: return "Bob";
case Role.MINER: return "minny";
case Role.SUPPLIER: return "Sully";
case Role.UPGRADER: return "Uppa";
}
}
function getBodyByJob(role, energyAvailability){
const body = [];
switch (role) {
case Role.BUILDER: body.push(WORK); energyAvailability -= 100; break;
case Role.UPGRADER: body.push(CARRY); body.push(MOVE); energyAvailability -= 100; break;
case Role.MINER: body.push(MOVE); energyAvailability -= 100; break;
}
var unitEnergyCost = 0;
while(unitEnergyCost <= energyAvailability) {
switch (role) {
case Role.BUILDER: body.push(MOVE); body.push(CARRY); unitEnergyCost = 100; break;
case Role.UPGRADER:
case Role.MINER:
body.push(WORK); unitEnergyCost = 100;
if(role === Role.MINER && body.length > 5) energyAvailability = -1;
break;
case Role.SUPPLIER: body.push(CARRY); body.push(MOVE); unitEnergyCost = 100; break;
default: energyAvailability = -1; break;
}
energyAvailability -= unitEnergyCost;
}
return body;
}