Files
screeps/creeps.js
2022-04-15 23:05:03 +02:00

220 lines
8.7 KiB
JavaScript

/* ###################################################################
# An inherited creep class and a module to expose it to the loop. #
###################################################################*/
const roles = require("roles");
module.exports = {
configure: function () {
// Replaces the creep class by the MyCreep class which changes the type of all Creep objects as well.
Creep.prototype = MyCreep.prototype;
setConstants();
// configure all creeps (in case new memory requirements).
for (const name in Game.creeps) {
Game.creeps[name].configure();
}
},
// All creeps execute their roles every tick.
execute: function () {
for (const name in Game.creeps) {
Game.creeps[name].execute();
}
}
};
class MyCreep extends Creep {
configure() {
// Basic values every creep has.
if (this.memory.restock == undefined) this.memory.restock = true;
if (this.memory.target == undefined) this.memory.target = null;
if (this.memory.counter == undefined) this.memory.counter = 0;
}
execute() {
// This line is temporary untill the spawns are renewed as well.
if (this.memory.restock == undefined) this.configure();
switch (this.memory.role) {
case HARVESTER:
if (this.memory.restock) {
this.getEnergy();
} else {
const role = this.memory.counter % 3;
if (role == 0) this.memory.role = SUPPLIER;
if (role == 1) this.memory.role = BUILDER;
if (role == 2) this.memory.role = UPGRADER;
this.execute();
this.memory.role = HARVESTER;
}
break;
case MINER:
this.memory.target = this.memory.container_id;
if (this.moveToTarget()) {
this.harvestSource();
}
break;
case SUPPLIER:
if (this.memory.restock) {
this.getEnergy();
} else {
this.supplyEnergy();
}
break;
case BUILDER:
if (this.memory.restock) {
this.getEnergy();
} else {
this.construct();
}
break;
case UPGRADER:
if (this.memory.restock) {
this.getEnergy();
} else {
this.upgrade();
}
break;
case DEFENDER: roles.defend(this);
break;
case RESERVER: roles.reserve(this);
break;
case RESERVED_HARVESTER: roles.harvest_reserved(this);
break;
}
}
// Moves to the object with given id. Returns true if arrived.
moveToTarget() {
const target = Game.getObjectById(this.memory.target);
if (target != null) {
if (this.pos.isEqualTo(target.pos)) return true;
this.moveTo(target);
} else {
console.log(this.name + " can't find object with id: " + this.memory.target);
}
return false;
}
// Searches for a energy source, moves towards it and collects the energy.
getEnergy() {
// Search for an energy source.
if (this.memory.target == null) {
var target = this.pos.findClosestByPath(FIND_DROPPED_RESOURCES);
if (target == null) target = this.pos.findClosestByPath(FIND_TOMBSTONES);
else if (target == null) target = this.pos.findClosestByPath(FIND_STRUCTURES, { filter: { structureType: STRUCTURE_CONTAINER } });
else if (target == null) target = this.room.storage;
else if (target == null) target = this.pos.findClosestByPath(FIND_SOURCES);
if (target != null) this.memory.target = target.id;
}
// Get energy from the source.
var target = Game.getObjectById(this.memory.target);
var response = this.withdraw(target, RESOURCE_ENERGY);
if (response == ERR_INVALID_TARGET) response = this.pickup(target);
else if (response == ERR_INVALID_TARGET) response = this.harvest(target);
if (response == OK);
else if (response == ERR_NOT_IN_RANGE) this.moveToTarget();
else if (response == ERR_FULL) {
this.memory.restock = false;
this.memory.target = null;
} else {
this.memory.target = null;
}
}
// harvests set or closest source, moves to it if to far and return true if fully loaded.
harvestSource() {
if (this.memory.source_id == undefined) {
this.memory.source_id = this.pos.findClosestByPath(FIND_SOURCES).id;
}
const response = this.harvest(Game.getObjectById(this.memory.source_id));
if (response == ERR_NOT_IN_RANGE) this.goTo(this.memory.source_id);
return this.store.getFreeCapacity(RESOURCE_ENERGY) == 0;
}
// supplies a load of energy to a structure who requires it.
supplyEnergy() {
// Search for a structure who requires energy.
if (this.memory.target == null) {
var target = this.pos.findClosestByPath(FIND_MY_STRUCTURES,
{ filter: (s) => (s.structureType == STRUCTURE_TOWER && 100 < s.store.getFreeCapacity(RESOURCE_ENERGY)) });
if (target == null) target = this.pos.findClosestByPath(FIND_MY_STRUCTURES,
{ filter: (s) => ((s.structureType == STRUCTURE_SPAWN || s.structureType == STRUCTURE_EXTENSION) && 0 < s.store.getFreeCapacity(RESOURCE_ENERGY)) });
else if (target == null) target = this.room.storage;
if (target != null) this.memory.target = target.id;
}
// Supply structure with energy or move towards it.
var target = Game.getObjectById(this.memory.target);
const response = this.transfer(target, RESOURCE_ENERGY);
if (response == OK);
else if (response == ERR_NOT_IN_RANGE) this.moveToTarget();
else if (response == ERR_NOT_ENOUGH_RESOURCES) {
this.memory.restock = true;
this.memory.target = null;
} else {
this.memory.target = null;
}
}
construct() {
// Search for a structure who requires energy.
if (this.memory.target == null) {
// Build new buildings
var target = this.pos.findClosestByPath(FIND_MY_CONSTRUCTION_SITES);
// Search buildings that are damaged and sort by most damaged first.
if (target == null) {
var structures = this.room.find(FIND_STRUCTURES,
{ filter: (s) => (s.structureType != STRUCTURE_RAMPART && s.structureType != STRUCTURE_WALL && s.hits < s.hitsMax) });
structures.sort((s1, s2) => (s1.hits - s2.hits));
target = structures[0];
}
// All buildings are in repaired search for walls that can be improved.
if (target == null) {
var structures = this.room.find(FIND_STRUCTURES,
{ filter: (s) => ((s.structureType == STRUCTURE_RAMPART || s.structureType == STRUCTURE_WALL) && s.hits < s.hitsMax) });
structures.sort((s1, s2) => (s1.hits - s2.hits));
target = structures[0];
}
if (target != null) this.memory.target = target.id;
}
// Build or repair the found structure.
var target = Game.getObjectById(this.memory.target);
var response = this.build(target);
if (response == ERR_INVALID_TARGET) response = this.repair(target);
if (response == OK);
else if (response == ERR_NOT_IN_RANGE) this.moveToTarget();
else if (response == ERR_NOT_ENOUGH_RESOURCES) {
this.memory.restock = true;
this.memory.target = null;
} else {
this.memory.target = null;
}
}
upgrade() {
this.memory.target = this.room.controller.id;
// Upgrade the controller or walk towards it.
var response = this.upgradeController(this.room.controller);
if (response == OK);
else if (response == ERR_NOT_IN_RANGE) this.moveToTarget();
else if (response == ERR_NOT_ENOUGH_RESOURCES) {
this.memory.restock = true;
this.memory.target = null;
} else {
this.memory.target = null;
}
}
}
function setConstants() {
global.HARVESTER = 0;
global.MINER = 1;
global.SUPPLIER = 2;
global.BUILDER = 3;
global.UPGRADER = 4;
global.DEFENDER = 5;
global.RESERVER = 6;
global.RESERVED_HARVESTER = 7;
}