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 = {
|
module.exports = {
|
||||||
setup: function () { Creep.prototype = _Creep.prototype; }
|
setup: function () { Creep.prototype = _Creep.prototype; }
|
||||||
}
|
}
|
||||||
|
|
||||||
class _Creep extends Creep {
|
class _Creep extends Creep {
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
|
// if no role is assigned become harvester
|
||||||
if (this.memory.role == undefined) this.memory.role = ROLE_HARVESTER;
|
if (this.memory.role == undefined) this.memory.role = ROLE_HARVESTER;
|
||||||
if (this.memory.recharge == undefined) this.memory.recharge = true;
|
// start by gathering new energy
|
||||||
}
|
if (this.memory.gathering == undefined) this.memory.gathering = true;
|
||||||
|
this.memory.init = 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//#region Target detection
|
||||||
findTarget(TARGET) {
|
findTarget(TARGET) {
|
||||||
if (this.memory.target == undefined) {
|
if (this.memory.target == undefined) {
|
||||||
const target = this.pos.findClosestByPath(TARGET);
|
const target = this.pos.findClosestByPath(TARGET);
|
||||||
@@ -36,26 +32,30 @@ class _Creep extends Creep {
|
|||||||
return OK;
|
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() {
|
harvestSource() {
|
||||||
if (this.findTarget(FIND_SOURCES, {
|
// search for source with highest energy level.
|
||||||
filter: (s) => {
|
if (this.findBestTarget(FIND_SOURCES, function (s1, s2) { return s2.energy - s1.energy }) == ERR_NOT_FOUND) return;
|
||||||
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);
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//#endregion
|
||||||
|
|
||||||
|
//#region Roles
|
||||||
harvesterJobs() {
|
harvesterJobs() {
|
||||||
if (this.memory.counter == undefined) this.memory.counter = 0;
|
if (this.memory.counter == undefined) this.memory.counter = 0;
|
||||||
switch (this.memory.counter % 4) {
|
switch (this.memory.counter % 4) {
|
||||||
@@ -85,6 +85,11 @@ class _Creep extends Creep {
|
|||||||
}
|
}
|
||||||
}) == ERR_NOT_FOUND) return;
|
}) == ERR_NOT_FOUND) return;
|
||||||
const target = Game.getObjectById(this.memory.target);
|
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) {
|
if (this.moveToTarget() == OK) {
|
||||||
this.transfer(target, RESOURCE_ENERGY);
|
this.transfer(target, RESOURCE_ENERGY);
|
||||||
this.toggleChargeMode();
|
this.toggleChargeMode();
|
||||||
@@ -100,56 +105,100 @@ class _Creep extends Creep {
|
|||||||
}
|
}
|
||||||
|
|
||||||
repairStructures() {
|
repairStructures() {
|
||||||
if (this.findTarget(FIND_MY_STRUCTURES, {
|
this.findTarget(FIND_MY_STRUCTURES, {
|
||||||
filter: (s) => {
|
filter: (s) => {
|
||||||
return s.hits < s.hitsMax &&
|
return s.hits < s.hitsMax &&
|
||||||
s.structureType != STRUCTURE_WALL &&
|
s.structureType != STRUCTURE_WALL &&
|
||||||
s.structureType != STRUCTURE_RAMPART;
|
s.structureType != STRUCTURE_RAMPART;
|
||||||
}
|
}
|
||||||
}) == ERR_NOT_FOUND);
|
});
|
||||||
if (this.findTarget(FIND_MY_STRUCTURES, {
|
if (this.findTarget(FIND_MY_STRUCTURES, {
|
||||||
filter: (s) => {
|
filter: (s) => {
|
||||||
return s.hits < s.hitsMax;
|
return s.hits < s.hitsMax;
|
||||||
}
|
}
|
||||||
}) == ERR_NOT_FOUND) return;
|
}) == ERR_NOT_FOUND) return;
|
||||||
|
const target = Game.getObjectById(this.memory.target);
|
||||||
const target = Game.getObjectById(this.memory.target.id);
|
if (target.hits == target.hitsMax) {
|
||||||
|
this.memory.target = undefined;
|
||||||
|
this.repairStructures();
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (this.moveToTarget() == OK) {
|
if (this.moveToTarget() == OK) {
|
||||||
this.repair(target);
|
if (this.repair(target) == OK) return;
|
||||||
this.toggleChargeMode();
|
this.toggleChargeMode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buildConstructionSites() {
|
buildConstructionSites() {
|
||||||
if (this.findTarget(FIND_MY_CONSTRUCTION_SITES) == ERR_NOT_FOUND) return;
|
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) {
|
if (this.moveToTarget() == OK) {
|
||||||
this.build(target);
|
if (this.build(target) == OK) return;
|
||||||
this.toggleChargeMode();
|
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() {
|
moveToTarget() {
|
||||||
|
// Memory allocation
|
||||||
|
if (this.memory.move == undefined) this.memory.move = {};
|
||||||
|
|
||||||
|
// Get target to walk to.
|
||||||
const target = Game.getObjectById(this.memory.target)
|
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)) {
|
if (this.pos.inRangeTo(target.pos.x, target.pos.y, 1)) {
|
||||||
this.memory.path = undefined;
|
this.memory.move.path = undefined;
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
if (this.pos.inRangeTo(target.pos.x, target.pos.y, 5)) {
|
|
||||||
this.moveTo(target)
|
// Check if in locked position every 5 ticks
|
||||||
return ERR_BUSY;
|
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
|
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 (response == OK) return ERR_BUSY;
|
||||||
|
// If error occurs remove path and use moveTo to move.
|
||||||
if (response == ERR_INVALID_ARGS || response == ERR_NOT_FOUND) {
|
if (response == ERR_INVALID_ARGS || response == ERR_NOT_FOUND) {
|
||||||
this.moveTo(target);
|
this.moveTo(target);
|
||||||
this.memory.path = undefined;
|
this.memory.move.path = undefined;
|
||||||
return ERR_BUSY;
|
return ERR_BUSY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//#endregion
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ class _Room extends Room {
|
|||||||
}
|
}
|
||||||
|
|
||||||
planStructures() {
|
planStructures() {
|
||||||
|
this.memory.roadsPlanned = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|||||||
@@ -17,9 +17,10 @@ module.exports = {
|
|||||||
delete Memory.creeps[creep_name];
|
delete Memory.creeps[creep_name];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (creep.memory.init == undefined) creep.init();
|
||||||
switch (creep.memory.role) {
|
switch (creep.memory.role) {
|
||||||
case ROLE_HARVESTER:
|
case ROLE_HARVESTER:
|
||||||
if (creep.memory.recharge) creep.harvestSource();
|
if (creep.memory.gathering) creep.harvestSource();
|
||||||
else creep.harvesterJobs();
|
else creep.harvesterJobs();
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ 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 (Game.time % 1000 == 0) {
|
if (Game.time % 100 == 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.canConstruct()) {
|
if (room.canConstruct()) {
|
||||||
room.planRoads();
|
room.planRoads();
|
||||||
|
|||||||
Reference in New Issue
Block a user