Slight improvements to movement. Started using regions.
This commit is contained in:
125
ClassCreep.js
125
ClassCreep.js
@@ -1,23 +1,19 @@
|
||||
// Replace creep class with _creep class
|
||||
module.exports = {
|
||||
setup: function () { Creep.prototype = _Creep.prototype; }
|
||||
}
|
||||
|
||||
class _Creep extends Creep {
|
||||
|
||||
init() {
|
||||
// if no role is assigned become harvester
|
||||
if (this.memory.role == undefined) this.memory.role = ROLE_HARVESTER;
|
||||
if (this.memory.recharge == undefined) this.memory.recharge = true;
|
||||
}
|
||||
|
||||
toggleChargeMode() {
|
||||
if (this.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
||||
this.memory.recharge = false;
|
||||
} else if (this.store.getUsedCapacity(RESOURCE_ENERGY) == 0) {
|
||||
this.memory.recharge = true;
|
||||
} else return;
|
||||
this.memory.target = undefined;
|
||||
this.memory.path = undefined;
|
||||
// start by gathering new energy
|
||||
if (this.memory.gathering == undefined) this.memory.gathering = true;
|
||||
this.memory.init = true;
|
||||
}
|
||||
|
||||
//#region Target detection
|
||||
findTarget(TARGET) {
|
||||
if (this.memory.target == undefined) {
|
||||
const target = this.pos.findClosestByPath(TARGET);
|
||||
@@ -36,26 +32,30 @@ class _Creep extends Creep {
|
||||
return OK;
|
||||
}
|
||||
|
||||
findBestTarget(TARGET, sort) {
|
||||
if (this.memory.target == undefined) {
|
||||
const targets = this.room.find(TARGET);
|
||||
const target = targets.sort(sort).shift();
|
||||
if (target == null) return ERR_NOT_FOUND;
|
||||
else this.memory.target = target.id;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//#region Gathering
|
||||
harvestSource() {
|
||||
if (this.findTarget(FIND_SOURCES, {
|
||||
filter: (s) => {
|
||||
const area = s.room.lookAtArea(s.pos.y - 1, s.pos.x - 1, s.pos.y + 1, s.pos.y + 1, true);
|
||||
var free_space = 0;
|
||||
area.forEach(entry => {
|
||||
if (entry.type == "creep") free_space--;
|
||||
else if (entry.type == "terrain" && entry.terrain == "plain") free_space++;
|
||||
else if (entry.type == "terrain" && entry.terrain == "swamp") free_space++;
|
||||
});
|
||||
return free_space > 0;
|
||||
}
|
||||
}) == ERR_NOT_FOUND) return;
|
||||
// search for source with highest energy level.
|
||||
if (this.findBestTarget(FIND_SOURCES, function (s1, s2) { return s2.energy - s1.energy }) == ERR_NOT_FOUND) return;
|
||||
const target = Game.getObjectById(this.memory.target);
|
||||
if (this.moveToTarget() == OK) {
|
||||
this.harvest(target);
|
||||
this.toggleChargeMode();
|
||||
}
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//#region Roles
|
||||
harvesterJobs() {
|
||||
if (this.memory.counter == undefined) this.memory.counter = 0;
|
||||
switch (this.memory.counter % 4) {
|
||||
@@ -85,6 +85,11 @@ class _Creep extends Creep {
|
||||
}
|
||||
}) == ERR_NOT_FOUND) return;
|
||||
const target = Game.getObjectById(this.memory.target);
|
||||
if (target.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
||||
this.memory.target = undefined;
|
||||
this.supplyStructures();
|
||||
return;
|
||||
}
|
||||
if (this.moveToTarget() == OK) {
|
||||
this.transfer(target, RESOURCE_ENERGY);
|
||||
this.toggleChargeMode();
|
||||
@@ -100,56 +105,100 @@ class _Creep extends Creep {
|
||||
}
|
||||
|
||||
repairStructures() {
|
||||
if (this.findTarget(FIND_MY_STRUCTURES, {
|
||||
this.findTarget(FIND_MY_STRUCTURES, {
|
||||
filter: (s) => {
|
||||
return s.hits < s.hitsMax &&
|
||||
s.structureType != STRUCTURE_WALL &&
|
||||
s.structureType != STRUCTURE_RAMPART;
|
||||
}
|
||||
}) == ERR_NOT_FOUND);
|
||||
});
|
||||
if (this.findTarget(FIND_MY_STRUCTURES, {
|
||||
filter: (s) => {
|
||||
return s.hits < s.hitsMax;
|
||||
}
|
||||
}) == ERR_NOT_FOUND) return;
|
||||
|
||||
const target = Game.getObjectById(this.memory.target.id);
|
||||
const target = Game.getObjectById(this.memory.target);
|
||||
if (target.hits == target.hitsMax) {
|
||||
this.memory.target = undefined;
|
||||
this.repairStructures();
|
||||
return;
|
||||
}
|
||||
if (this.moveToTarget() == OK) {
|
||||
this.repair(target);
|
||||
if (this.repair(target) == OK) return;
|
||||
this.toggleChargeMode();
|
||||
}
|
||||
}
|
||||
|
||||
buildConstructionSites() {
|
||||
if (this.findTarget(FIND_MY_CONSTRUCTION_SITES) == ERR_NOT_FOUND) return;
|
||||
const target = Game.getObjectById(this.memory.target.id);
|
||||
const target = Game.getObjectById(this.memory.target);
|
||||
if (target == null) {
|
||||
this.memory.target = undefined;
|
||||
this.buildConstructionSites();
|
||||
return;
|
||||
}
|
||||
if (this.moveToTarget() == OK) {
|
||||
this.build(target);
|
||||
if (this.build(target) == OK) return;
|
||||
this.toggleChargeMode();
|
||||
}
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//#region State
|
||||
toggleChargeMode() {
|
||||
if (this.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
||||
this.memory.gathering = false;
|
||||
} else if (this.store.getUsedCapacity(RESOURCE_ENERGY) == 0) {
|
||||
this.memory.gathering = true;
|
||||
} else return;
|
||||
this.memory.target = undefined;
|
||||
this.memory.path = undefined;
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//#region Movement
|
||||
moveToTarget() {
|
||||
// Memory allocation
|
||||
if (this.memory.move == undefined) this.memory.move = {};
|
||||
|
||||
// Get target to walk to.
|
||||
const target = Game.getObjectById(this.memory.target)
|
||||
if (target == null) return ERR_INVALID_TARGET;
|
||||
|
||||
// Check if arrived at target.
|
||||
if (this.pos.inRangeTo(target.pos.x, target.pos.y, 1)) {
|
||||
this.memory.path = undefined;
|
||||
this.memory.move.path = undefined;
|
||||
return OK;
|
||||
}
|
||||
if (this.pos.inRangeTo(target.pos.x, target.pos.y, 5)) {
|
||||
this.moveTo(target)
|
||||
return ERR_BUSY;
|
||||
|
||||
// Check if in locked position every 5 ticks
|
||||
if (this.memory.move.last_pos == undefined) this.memory.move.last_pos = this.pos;
|
||||
if (this.ticksToLive % 5 == 0) {
|
||||
if (this.memory.move.last_pos.x == this.pos.x &&
|
||||
this.memory.move.last_pos.y == this.pos.y) {
|
||||
this.moveTo(target);
|
||||
this.memory.move.path = undefined;
|
||||
return ERR_BUSY;
|
||||
}
|
||||
this.memory.move.last_pos = this.pos;
|
||||
}
|
||||
if (this.memory.path == undefined) {
|
||||
this.memory.path = this.pos.findPathTo(target.pos, {
|
||||
|
||||
// Check if a new path is required.
|
||||
if (this.memory.move.path == undefined) {
|
||||
this.memory.move.path = this.pos.findPathTo(target.pos, {
|
||||
ignoreCreeps: true, range: 1
|
||||
});
|
||||
}
|
||||
const response = this.moveByPath(this.memory.path);
|
||||
|
||||
// Perform move
|
||||
const response = this.moveByPath(this.memory.move.path);
|
||||
if (response == OK) return ERR_BUSY;
|
||||
// If error occurs remove path and use moveTo to move.
|
||||
if (response == ERR_INVALID_ARGS || response == ERR_NOT_FOUND) {
|
||||
this.moveTo(target);
|
||||
this.memory.path = undefined;
|
||||
this.memory.move.path = undefined;
|
||||
return ERR_BUSY;
|
||||
}
|
||||
}
|
||||
//#endregion
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ class _Room extends Room {
|
||||
}
|
||||
|
||||
planStructures() {
|
||||
|
||||
this.memory.roadsPlanned = false;
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
@@ -17,9 +17,10 @@ module.exports = {
|
||||
delete Memory.creeps[creep_name];
|
||||
return;
|
||||
}
|
||||
if (creep.memory.init == undefined) creep.init();
|
||||
switch (creep.memory.role) {
|
||||
case ROLE_HARVESTER:
|
||||
if (creep.memory.recharge) creep.harvestSource();
|
||||
if (creep.memory.gathering) creep.harvestSource();
|
||||
else creep.harvesterJobs();
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -9,7 +9,7 @@ module.exports = {
|
||||
update: function () {
|
||||
for (const room_name in Game.rooms) {
|
||||
const room = Game.rooms[room_name];
|
||||
if (Game.time % 1000 == 0) {
|
||||
if (Game.time % 100 == 0) {
|
||||
// Place new building types if the max construcion sites isn't achieved yet.
|
||||
if (room.canConstruct()) {
|
||||
room.planRoads();
|
||||
|
||||
Reference in New Issue
Block a user