82 lines
2.4 KiB
JavaScript
82 lines
2.4 KiB
JavaScript
module.exports = {
|
|
setup: function () { Room.prototype = _Room.prototype; }
|
|
}
|
|
|
|
|
|
class _Room extends Room {
|
|
// Initialize the room object
|
|
init() {
|
|
// Initialize memory
|
|
if (this.memory.state == undefined) this.memory.state = ROOMSTATE_NEUTRAL;
|
|
|
|
// Start game when spawn is in neutral room make it the capital.
|
|
if (this.controller.my && this.memory.state == ROOMSTATE_NEUTRAL) this.memory.state = ROOMSTATE_CAPITAL;
|
|
}
|
|
|
|
//#region Checks
|
|
|
|
// Is the max amount of construction sites achieved?
|
|
canConstruct() { return this.find(FIND_CONSTRUCTION_SITES).length < 90; }
|
|
//#endregion
|
|
|
|
//#region Construcion
|
|
|
|
// Place construction sites for the road system.
|
|
planRoads() {
|
|
if (this.memory.roadsPlanned == undefined) this.memory.roadsPlanned = false;
|
|
if (this.memory.roadsPlanned) return;
|
|
var target_array = [];
|
|
var index = 0;
|
|
|
|
// For any structure of given type add to targets.
|
|
this.find(FIND_MY_STRUCTURES, {
|
|
filter: function (structure) {
|
|
var value = structure.structureType == STRUCTURE_SPAWN;
|
|
value = value || structure.structureType == STRUCTURE_CONTAINER;
|
|
value = value || structure.structureType == STRUCTURE_CONTROLLER;
|
|
value = value || structure.structureType == STRUCTURE_EXTENSION;
|
|
value = value || structure.structureType == STRUCTURE_STORAGE;
|
|
value = value || structure.structureType == STRUCTURE_TOWER;
|
|
return value;
|
|
}
|
|
}).forEach(structure => {
|
|
target_array[index++] = structure.pos;
|
|
});
|
|
|
|
// Add all sources to targets.
|
|
this.find(FIND_SOURCES).forEach(source => {
|
|
target_array[index++] = source.pos;
|
|
});
|
|
|
|
// Add minerals to targers
|
|
this.find(FIND_MINERALS).forEach(mineral => {
|
|
target_array[index++] = mineral.pos;
|
|
});
|
|
|
|
// Build a road for every permutation of the targets.
|
|
for (let i = 0; i < target_array.length; i++) {
|
|
const begin = target_array[i];
|
|
for (let j = 0; j < target_array.length; j++) {
|
|
const end = target_array[j];
|
|
if (j == i) continue;
|
|
var path = this.findPath(begin, end, { ignoreCreeps: true, swampCost: 2 });
|
|
// Remove the begin and end tile.
|
|
path = path.slice(1, path.length - 1);
|
|
// Build road on every step.
|
|
path.forEach(step => {
|
|
const response = this.createConstructionSite(step.x, step.y, STRUCTURE_ROAD);
|
|
if (response == ERR_FULL) return;
|
|
});
|
|
}
|
|
}
|
|
this.memory.roadsPlanned = true;
|
|
}
|
|
|
|
planStructures() {
|
|
this.memory.roadsPlanned = false;
|
|
}
|
|
|
|
//#endregion
|
|
|
|
}
|