Improved performance of walking.
Switches to manual walking when close to target.
This commit is contained in:
@@ -18,31 +18,38 @@ class _Creep extends Creep {
|
||||
this.memory.path = undefined;
|
||||
}
|
||||
|
||||
findTarget(target) {
|
||||
findTarget(TARGET) {
|
||||
if (this.memory.target == undefined) {
|
||||
this.memory.target = this.pos.findClosestByPath(target);
|
||||
if (this.memory.target == null) {
|
||||
this.memory.target = undefined;
|
||||
return ERR_NOT_FOUND;
|
||||
}
|
||||
const target = this.pos.findClosestByPath(TARGET);
|
||||
if (target == null) return ERR_NOT_FOUND;
|
||||
else this.memory.target = target.id;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
findTarget(target, filter) {
|
||||
findTarget(TARGET, filter) {
|
||||
if (this.memory.target == undefined) {
|
||||
this.memory.target = this.pos.findClosestByPath(target, filter);
|
||||
if (this.memory.target == null) {
|
||||
this.memory.target = undefined;
|
||||
return ERR_NOT_FOUND;
|
||||
}
|
||||
const target = this.pos.findClosestByPath(TARGET, filter);
|
||||
if (target == null) return ERR_NOT_FOUND;
|
||||
else this.memory.target = target.id;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
harvestSource() {
|
||||
if (this.findTarget(FIND_SOURCES) == ERR_NOT_FOUND) return;
|
||||
const target = Game.getObjectById(this.memory.target.id);
|
||||
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;
|
||||
const target = Game.getObjectById(this.memory.target);
|
||||
if (this.moveToTarget() == OK) {
|
||||
this.harvest(target);
|
||||
this.toggleChargeMode();
|
||||
@@ -70,13 +77,14 @@ class _Creep extends Creep {
|
||||
supplyStructures() {
|
||||
if (this.findTarget(FIND_MY_STRUCTURES, {
|
||||
filter: (s) => {
|
||||
return s.structureType == STRUCTURE_SPAWN ||
|
||||
return (s.structureType == STRUCTURE_SPAWN ||
|
||||
s.structureType == STRUCTURE_EXTENSION ||
|
||||
s.structureType == STRUCTURE_TOWER ||
|
||||
s.structureType == STRUCTURE_STORAGE;
|
||||
s.structureType == STRUCTURE_STORAGE)
|
||||
&& 0 < s.store.getFreeCapacity();
|
||||
}
|
||||
}) == ERR_NOT_FOUND) return;
|
||||
const target = Game.getObjectById(this.memory.target.id);
|
||||
const target = Game.getObjectById(this.memory.target);
|
||||
if (this.moveToTarget() == OK) {
|
||||
this.transfer(target, RESOURCE_ENERGY);
|
||||
this.toggleChargeMode();
|
||||
@@ -84,14 +92,9 @@ class _Creep extends Creep {
|
||||
}
|
||||
|
||||
upgradeRoom() {
|
||||
if (this.findTarget(FIND_MY_STRUCTURES, {
|
||||
filter: (s) => {
|
||||
return s.structureType == STRUCTURE_CONTROLLER;
|
||||
}
|
||||
}) == ERR_NOT_FOUND) return;
|
||||
const target = Game.getObjectById(this.memory.target.id);
|
||||
this.memory.target = this.room.controller.id;
|
||||
if (this.moveToTarget() == OK) {
|
||||
this.upgradeController();
|
||||
if (this.upgradeController(this.room.controller) == OK) return;
|
||||
this.toggleChargeMode();
|
||||
}
|
||||
}
|
||||
@@ -126,30 +129,27 @@ class _Creep extends Creep {
|
||||
}
|
||||
}
|
||||
|
||||
// Moves to the target.
|
||||
moveToTarget() {
|
||||
if (this.pos.inRangeTo(this.memory.target.pos.x, this.memory.target.pos.y, 1)) {
|
||||
const target = Game.getObjectById(this.memory.target)
|
||||
if (this.pos.inRangeTo(target.pos.x, target.pos.y, 1)) {
|
||||
this.memory.path = undefined;
|
||||
return OK;
|
||||
}
|
||||
if (this.pos.inRangeTo(target.pos.x, target.pos.y, 5)) {
|
||||
this.moveTo(target)
|
||||
return ERR_BUSY;
|
||||
}
|
||||
if (this.memory.path == undefined) {
|
||||
var path = this.pos.findPathTo(this.memory.target.pos, {
|
||||
this.memory.path = this.pos.findPathTo(target.pos, {
|
||||
ignoreCreeps: true, range: 1
|
||||
});
|
||||
path.reverse();
|
||||
this.memory.path = path;
|
||||
}
|
||||
const step = this.memory.path.pop();
|
||||
var response = this.move(step.direction);
|
||||
if (response == OK) {
|
||||
if (this.pos.x == step.x &&
|
||||
this.pos.y == step.y) return ERR_BUSY;
|
||||
response = this.moveTo(this.memory.target.pos.x, this.memory.target.pos.y);
|
||||
}
|
||||
if (response == OK || response == ERR_INVALID_ARGS || response == ERR_NOT_FOUND) {
|
||||
const response = this.moveByPath(this.memory.path);
|
||||
if (response == OK) return ERR_BUSY;
|
||||
if (response == ERR_INVALID_ARGS || response == ERR_NOT_FOUND) {
|
||||
this.moveTo(target);
|
||||
this.memory.path = undefined;
|
||||
return ERR_BUSY;
|
||||
}
|
||||
this.memory.path.push(step);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user