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;
|
this.memory.path = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
findTarget(target) {
|
findTarget(TARGET) {
|
||||||
if (this.memory.target == undefined) {
|
if (this.memory.target == undefined) {
|
||||||
this.memory.target = this.pos.findClosestByPath(target);
|
const target = this.pos.findClosestByPath(TARGET);
|
||||||
if (this.memory.target == null) {
|
if (target == null) return ERR_NOT_FOUND;
|
||||||
this.memory.target = undefined;
|
else this.memory.target = target.id;
|
||||||
return ERR_NOT_FOUND;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
findTarget(target, filter) {
|
findTarget(TARGET, filter) {
|
||||||
if (this.memory.target == undefined) {
|
if (this.memory.target == undefined) {
|
||||||
this.memory.target = this.pos.findClosestByPath(target, filter);
|
const target = this.pos.findClosestByPath(TARGET, filter);
|
||||||
if (this.memory.target == null) {
|
if (target == null) return ERR_NOT_FOUND;
|
||||||
this.memory.target = undefined;
|
else this.memory.target = target.id;
|
||||||
return ERR_NOT_FOUND;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
harvestSource() {
|
harvestSource() {
|
||||||
if (this.findTarget(FIND_SOURCES) == ERR_NOT_FOUND) return;
|
if (this.findTarget(FIND_SOURCES, {
|
||||||
const target = Game.getObjectById(this.memory.target.id);
|
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) {
|
if (this.moveToTarget() == OK) {
|
||||||
this.harvest(target);
|
this.harvest(target);
|
||||||
this.toggleChargeMode();
|
this.toggleChargeMode();
|
||||||
@@ -70,13 +77,14 @@ class _Creep extends Creep {
|
|||||||
supplyStructures() {
|
supplyStructures() {
|
||||||
if (this.findTarget(FIND_MY_STRUCTURES, {
|
if (this.findTarget(FIND_MY_STRUCTURES, {
|
||||||
filter: (s) => {
|
filter: (s) => {
|
||||||
return s.structureType == STRUCTURE_SPAWN ||
|
return (s.structureType == STRUCTURE_SPAWN ||
|
||||||
s.structureType == STRUCTURE_EXTENSION ||
|
s.structureType == STRUCTURE_EXTENSION ||
|
||||||
s.structureType == STRUCTURE_TOWER ||
|
s.structureType == STRUCTURE_TOWER ||
|
||||||
s.structureType == STRUCTURE_STORAGE;
|
s.structureType == STRUCTURE_STORAGE)
|
||||||
|
&& 0 < s.store.getFreeCapacity();
|
||||||
}
|
}
|
||||||
}) == ERR_NOT_FOUND) return;
|
}) == ERR_NOT_FOUND) return;
|
||||||
const target = Game.getObjectById(this.memory.target.id);
|
const target = Game.getObjectById(this.memory.target);
|
||||||
if (this.moveToTarget() == OK) {
|
if (this.moveToTarget() == OK) {
|
||||||
this.transfer(target, RESOURCE_ENERGY);
|
this.transfer(target, RESOURCE_ENERGY);
|
||||||
this.toggleChargeMode();
|
this.toggleChargeMode();
|
||||||
@@ -84,14 +92,9 @@ class _Creep extends Creep {
|
|||||||
}
|
}
|
||||||
|
|
||||||
upgradeRoom() {
|
upgradeRoom() {
|
||||||
if (this.findTarget(FIND_MY_STRUCTURES, {
|
this.memory.target = this.room.controller.id;
|
||||||
filter: (s) => {
|
|
||||||
return s.structureType == STRUCTURE_CONTROLLER;
|
|
||||||
}
|
|
||||||
}) == ERR_NOT_FOUND) return;
|
|
||||||
const target = Game.getObjectById(this.memory.target.id);
|
|
||||||
if (this.moveToTarget() == OK) {
|
if (this.moveToTarget() == OK) {
|
||||||
this.upgradeController();
|
if (this.upgradeController(this.room.controller) == OK) return;
|
||||||
this.toggleChargeMode();
|
this.toggleChargeMode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -126,30 +129,27 @@ class _Creep extends Creep {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Moves to the target.
|
|
||||||
moveToTarget() {
|
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;
|
this.memory.path = undefined;
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
if (this.pos.inRangeTo(target.pos.x, target.pos.y, 5)) {
|
||||||
|
this.moveTo(target)
|
||||||
|
return ERR_BUSY;
|
||||||
|
}
|
||||||
if (this.memory.path == undefined) {
|
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
|
ignoreCreeps: true, range: 1
|
||||||
});
|
});
|
||||||
path.reverse();
|
|
||||||
this.memory.path = path;
|
|
||||||
}
|
}
|
||||||
const step = this.memory.path.pop();
|
const response = this.moveByPath(this.memory.path);
|
||||||
var response = this.move(step.direction);
|
if (response == OK) return ERR_BUSY;
|
||||||
if (response == OK) {
|
if (response == ERR_INVALID_ARGS || response == ERR_NOT_FOUND) {
|
||||||
if (this.pos.x == step.x &&
|
this.moveTo(target);
|
||||||
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) {
|
|
||||||
this.memory.path = undefined;
|
this.memory.path = undefined;
|
||||||
return ERR_BUSY;
|
return ERR_BUSY;
|
||||||
}
|
}
|
||||||
this.memory.path.push(step);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
ClassRoom.js
11
ClassRoom.js
@@ -15,16 +15,8 @@ class _Room extends Room {
|
|||||||
|
|
||||||
//#region Checks
|
//#region Checks
|
||||||
|
|
||||||
// Is the level changed?
|
|
||||||
isNewLevel() {
|
|
||||||
if (this.memory.level == undefined) this.memory.level = -1;
|
|
||||||
const value = this.controller.my && this.controller.level != this.memory.level;
|
|
||||||
if (value) this.memory.level = this.controller.level;
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Is the max amount of construction sites achieved?
|
// Is the max amount of construction sites achieved?
|
||||||
isConstructionQueueFull() { return this.find(FIND_CONSTRUCTION_SITES).length > 90; }
|
canConstruct() { return this.find(FIND_CONSTRUCTION_SITES).length < 90; }
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
//#region Construcion
|
//#region Construcion
|
||||||
@@ -62,7 +54,6 @@ class _Room extends Room {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Build a road for every permutation of the targets.
|
// Build a road for every permutation of the targets.
|
||||||
// TODO: When performance to high make this loop recoverable.
|
|
||||||
for (let i = 0; i < target_array.length; i++) {
|
for (let i = 0; i < target_array.length; i++) {
|
||||||
const begin = target_array[i];
|
const begin = target_array[i];
|
||||||
for (let j = 0; j < target_array.length; j++) {
|
for (let j = 0; j < target_array.length; j++) {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ class _Spawn extends StructureSpawn {
|
|||||||
|
|
||||||
switch (role) {
|
switch (role) {
|
||||||
case ROLE_HARVESTER:
|
case ROLE_HARVESTER:
|
||||||
return roleCount < 5;
|
return roleCount < 10;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
const { round } = require("lodash");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
// Gets called when recompiling and when Restart command is called.
|
// Gets called when recompiling and when Restart command is called.
|
||||||
start: function () {
|
start: function () {
|
||||||
@@ -11,7 +13,10 @@ module.exports = {
|
|||||||
update: function () {
|
update: function () {
|
||||||
for (const creep_name in Memory.creeps) {
|
for (const creep_name in Memory.creeps) {
|
||||||
const creep = Game.creeps[creep_name];
|
const creep = Game.creeps[creep_name];
|
||||||
if (creep == undefined) delete Memory.creeps[creep_name];
|
if (creep == undefined) {
|
||||||
|
delete Memory.creeps[creep_name];
|
||||||
|
return;
|
||||||
|
}
|
||||||
switch (creep.memory.role) {
|
switch (creep.memory.role) {
|
||||||
case ROLE_HARVESTER:
|
case ROLE_HARVESTER:
|
||||||
if (creep.memory.recharge) creep.harvestSource();
|
if (creep.memory.recharge) creep.harvestSource();
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ module.exports = {
|
|||||||
update: function () {
|
update: function () {
|
||||||
for (const room_name in Game.rooms) {
|
for (const room_name in Game.rooms) {
|
||||||
const room = Game.rooms[room_name];
|
const room = Game.rooms[room_name];
|
||||||
if (room.isNewLevel()) {
|
if (Game.time % 1000 == 0) {
|
||||||
// Place new building types if the max construcion sites isn't achieved yet.
|
// Place new building types if the max construcion sites isn't achieved yet.
|
||||||
if (!room.isConstructionQueueFull()) {
|
if (room.canConstruct()) {
|
||||||
room.planRoads();
|
room.planRoads();
|
||||||
room.planStructures();
|
room.planStructures();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user