Reimplemented 5/7 roles.
This commit is contained in:
156
creeps.js
156
creeps.js
@@ -27,56 +27,184 @@ module.exports = {
|
||||
|
||||
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 roles.ROLE_HARVESTER: roles.harvest(this);
|
||||
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 roles.ROLE_MINER:
|
||||
if (this.goTo(this.memory.container_id)) {
|
||||
case MINER:
|
||||
this.memory.target = this.memory.container_id;
|
||||
if (this.moveToTarget()) {
|
||||
this.harvestSource();
|
||||
}
|
||||
break;
|
||||
case roles.ROLE_SUPPLIER: roles.supply(this);
|
||||
case SUPPLIER:
|
||||
if (this.memory.restock) {
|
||||
this.getEnergy();
|
||||
} else {
|
||||
this.supplyEnergy();
|
||||
}
|
||||
break;
|
||||
case roles.ROLE_BUILDER: roles.build(this);
|
||||
case BUILDER:
|
||||
if (this.memory.restock) {
|
||||
this.getEnergy();
|
||||
} else {
|
||||
this.construct();
|
||||
}
|
||||
break;
|
||||
case roles.ROLE_UPGRADER: roles.upgrade(this);
|
||||
case UPGRADER:
|
||||
if (this.memory.restock) {
|
||||
this.getEnergy();
|
||||
} else {
|
||||
this.upgrade();
|
||||
}
|
||||
break;
|
||||
case roles.ROLE_DEFENDER: roles.defend(this);
|
||||
case DEFENDER: roles.defend(this);
|
||||
break;
|
||||
case roles.ROLE_RESERVER: roles.reserve(this);
|
||||
case RESERVER: roles.reserve(this);
|
||||
break;
|
||||
case roles.ROLE_RESERVED_HARVESTER: roles.harvest_reserved(this);
|
||||
case RESERVED_HARVESTER: roles.harvest_reserved(this);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Moves to the object with given id. Returns true if arrived.
|
||||
goTo(id) {
|
||||
const target = Game.getObjectById(id)
|
||||
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: " + id);
|
||||
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 result = this.harvest(Game.getObjectById(this.memory.source_id));
|
||||
if (result == ERR_NOT_IN_RANGE) this.goTo(this.memory.source_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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user