Rewriting from scratch. Fixing old code is just less fun.

This commit is contained in:
DouweRavers
2022-04-29 01:45:27 +02:00
parent 5494f6fb17
commit 3e962ed04a
15 changed files with 159 additions and 974 deletions

75
ClassRoom.js Normal file
View File

@@ -0,0 +1,75 @@
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;
}
// 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;
}
isConstructionQueueFull() { return this.find(FIND_CONSTRUCTION_SITES).length > 90; }
// 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;
});
// Also add all sources
this.find(FIND_SOURCES).forEach(source => {
target_array[index++] = source.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() {
}
}