Chanded some filenames
Removed automatic path placement. Started command section.
This commit is contained in:
134
ClassCreep.js
134
ClassCreep.js
@@ -1,19 +1,34 @@
|
||||
// Replace creep class with _creep class
|
||||
module.exports = {
|
||||
setup: function () { Creep.prototype = _Creep.prototype; }
|
||||
}
|
||||
|
||||
class _Creep extends Creep {
|
||||
|
||||
init() {
|
||||
// if no role is assigned become harvester
|
||||
if (this.memory.role == undefined) this.memory.role = ROLE_HARVESTER;
|
||||
// start by gathering new energy
|
||||
if (this.memory.gathering == undefined) this.memory.gathering = true;
|
||||
this.memory.init = true;
|
||||
if (this.memory.recharge == undefined) this.memory.recharge = true;
|
||||
}
|
||||
|
||||
tick(){
|
||||
switch (this.memory.role) {
|
||||
case ROLE_HARVESTER:
|
||||
if (this.memory.recharge) this.harvestSource();
|
||||
else this.harvesterJobs();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
if (this.memory.target == undefined) {
|
||||
const target = this.pos.findClosestByPath(TARGET);
|
||||
@@ -32,30 +47,26 @@ class _Creep extends Creep {
|
||||
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() {
|
||||
// search for source with highest energy level.
|
||||
if (this.findBestTarget(FIND_SOURCES, function (s1, s2) { return s2.energy - s1.energy }) == ERR_NOT_FOUND) return;
|
||||
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();
|
||||
}
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//#region Roles
|
||||
harvesterJobs() {
|
||||
if (this.memory.counter == undefined) this.memory.counter = 0;
|
||||
switch (this.memory.counter % 4) {
|
||||
@@ -85,11 +96,6 @@ class _Creep extends Creep {
|
||||
}
|
||||
}) == ERR_NOT_FOUND) return;
|
||||
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) {
|
||||
this.transfer(target, RESOURCE_ENERGY);
|
||||
this.toggleChargeMode();
|
||||
@@ -105,26 +111,22 @@ class _Creep extends Creep {
|
||||
}
|
||||
|
||||
repairStructures() {
|
||||
this.findTarget(FIND_MY_STRUCTURES, {
|
||||
if (this.findTarget(FIND_MY_STRUCTURES, {
|
||||
filter: (s) => {
|
||||
return s.hits < s.hitsMax &&
|
||||
s.structureType != STRUCTURE_WALL &&
|
||||
s.structureType != STRUCTURE_RAMPART;
|
||||
}
|
||||
});
|
||||
}) == ERR_NOT_FOUND);
|
||||
if (this.findTarget(FIND_MY_STRUCTURES, {
|
||||
filter: (s) => {
|
||||
return s.hits < s.hitsMax;
|
||||
}
|
||||
}) == ERR_NOT_FOUND) return;
|
||||
const target = Game.getObjectById(this.memory.target);
|
||||
if (target.hits == target.hitsMax) {
|
||||
this.memory.target = undefined;
|
||||
this.repairStructures();
|
||||
return;
|
||||
}
|
||||
|
||||
const target = Game.getObjectById(this.memory.target.id);
|
||||
if (this.moveToTarget() == OK) {
|
||||
if (this.repair(target) == OK) return;
|
||||
this.repair(target);
|
||||
this.toggleChargeMode();
|
||||
}
|
||||
}
|
||||
@@ -132,73 +134,33 @@ class _Creep extends Creep {
|
||||
buildConstructionSites() {
|
||||
if (this.findTarget(FIND_MY_CONSTRUCTION_SITES) == ERR_NOT_FOUND) return;
|
||||
const target = Game.getObjectById(this.memory.target);
|
||||
if (target == null) {
|
||||
this.memory.target = undefined;
|
||||
this.buildConstructionSites();
|
||||
return;
|
||||
}
|
||||
if (this.moveToTarget() == OK) {
|
||||
if (this.build(target) == OK) return;
|
||||
this.build(target);
|
||||
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() {
|
||||
// Memory allocation
|
||||
if (this.memory.move == undefined) this.memory.move = {};
|
||||
|
||||
// Get target to walk to.
|
||||
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)) {
|
||||
this.memory.move.path = undefined;
|
||||
this.memory.path = undefined;
|
||||
return OK;
|
||||
}
|
||||
|
||||
// Check if in locked position every 5 ticks
|
||||
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.pos.inRangeTo(target.pos.x, target.pos.y, 5)) {
|
||||
this.moveTo(target)
|
||||
return ERR_BUSY;
|
||||
}
|
||||
|
||||
// Check if a new path is required.
|
||||
if (this.memory.move.path == undefined) {
|
||||
this.memory.move.path = this.pos.findPathTo(target.pos, {
|
||||
if (this.memory.path == undefined) {
|
||||
this.memory.path = this.pos.findPathTo(target.pos, {
|
||||
ignoreCreeps: true, range: 1
|
||||
});
|
||||
}
|
||||
|
||||
// Perform move
|
||||
const response = this.moveByPath(this.memory.move.path);
|
||||
const response = this.moveByPath(this.memory.path);
|
||||
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) {
|
||||
this.moveTo(target);
|
||||
this.memory.move.path = undefined;
|
||||
this.memory.path = undefined;
|
||||
return ERR_BUSY;
|
||||
}
|
||||
}
|
||||
//#endregion
|
||||
}
|
||||
|
||||
66
ClassRoom.js
66
ClassRoom.js
@@ -12,70 +12,4 @@ class _Room extends Room {
|
||||
// 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
|
||||
|
||||
}
|
||||
|
||||
11
_commandHandler.js
Normal file
11
_commandHandler.js
Normal file
@@ -0,0 +1,11 @@
|
||||
module.exports = {
|
||||
// Gets called when recompiling and when Restart command is called.
|
||||
start: function () {
|
||||
global.ClearConstructions = clearConstructions;
|
||||
}
|
||||
}
|
||||
|
||||
function clearConstructions(){
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
const { round } = require("lodash");
|
||||
|
||||
module.exports = {
|
||||
// Gets called when recompiling and when Restart command is called.
|
||||
start: function () {
|
||||
@@ -17,15 +15,7 @@ module.exports = {
|
||||
delete Memory.creeps[creep_name];
|
||||
return;
|
||||
}
|
||||
if (creep.memory.init == undefined) creep.init();
|
||||
switch (creep.memory.role) {
|
||||
case ROLE_HARVESTER:
|
||||
if (creep.memory.gathering) creep.harvestSource();
|
||||
else creep.harvesterJobs();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
creep.tick();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,13 +9,6 @@ module.exports = {
|
||||
update: function () {
|
||||
for (const room_name in Game.rooms) {
|
||||
const room = Game.rooms[room_name];
|
||||
if (Game.time % 100 == 0) {
|
||||
// Place new building types if the max construcion sites isn't achieved yet.
|
||||
if (room.canConstruct()) {
|
||||
room.planRoads();
|
||||
room.planStructures();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
5
main.js
5
main.js
@@ -3,7 +3,7 @@
|
||||
# Screeps 2022 #
|
||||
######################*/
|
||||
|
||||
const system = require("_system");
|
||||
const system = require("system");
|
||||
|
||||
module.exports.loop = function () {
|
||||
if (global.compiled == undefined) {
|
||||
@@ -27,7 +27,7 @@ const creepClass = require("ClassCreep");
|
||||
const roomClass = require("ClassRoom");
|
||||
const spawnClass = require("ClassSpawn");
|
||||
|
||||
// Overwrite all game defined classes by mine inherited ones.
|
||||
// Overwrite all game defined classes by inherited ones.
|
||||
function setupClasses() {
|
||||
creepClass.setup();
|
||||
roomClass.setup();
|
||||
@@ -44,5 +44,4 @@ function setConstants() {
|
||||
|
||||
// The roles of creeps.
|
||||
global.ROLE_HARVESTER = 0;
|
||||
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
/**
|
||||
* Basis of the entire system. Everything will be managed from here on out.
|
||||
*/
|
||||
const rooms = require("_rooms")
|
||||
const spawns = require("_spawns")
|
||||
const creeps = require("_creeps")
|
||||
const rooms = require("_roomsHandler")
|
||||
const spawns = require("_spawnsHandler")
|
||||
const creeps = require("_creepsHandler")
|
||||
const commands = require("_commandHandler")
|
||||
|
||||
module.exports = {
|
||||
// Gets called when recompiling and when Restart command is called.
|
||||
@@ -11,6 +12,7 @@ module.exports = {
|
||||
rooms.start();
|
||||
spawns.start();
|
||||
creeps.start();
|
||||
commands.start();
|
||||
},
|
||||
|
||||
// Gets called every tick.
|
||||
Reference in New Issue
Block a user