Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
91448edc0a | ||
|
|
e294ad29fe | ||
|
|
76650e22ac | ||
|
|
8c3528419a | ||
|
|
ccd0557339 | ||
|
|
ae3812aa83 | ||
|
|
15c86d3c14 | ||
|
|
90448c79e4 | ||
|
|
ef9159260b | ||
|
|
1806935c50 | ||
|
|
4e3ad07fff | ||
|
|
744b4a2478 | ||
|
|
3e962ed04a | ||
|
|
5494f6fb17 |
61
Commands.js
Normal file
61
Commands.js
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
setup(){
|
||||||
|
global.fullReset = fullReset;
|
||||||
|
global.resetRooms = resetRooms;
|
||||||
|
global.resetStructures = resetStructures;
|
||||||
|
global.resetCreeps = resetCreeps;
|
||||||
|
global.resetConstruction = resetConstruction;
|
||||||
|
|
||||||
|
global.buildRoads = buildRoads;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function fullReset(){
|
||||||
|
resetRooms();
|
||||||
|
resetCreeps();
|
||||||
|
resetStructures();
|
||||||
|
resetConstruction();
|
||||||
|
global.started = false;
|
||||||
|
global.compiled = false;
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetRooms(){
|
||||||
|
Object.values(Game.rooms).forEach(room => room.memory = {});
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetStructures(){
|
||||||
|
Object.values(Game.structures).forEach(structure => structure.memory = {});
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetCreeps(){
|
||||||
|
Object.values(Game.creeps).forEach(creep => creep.suicide());
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetConstruction(){
|
||||||
|
Object.values(Game.rooms).forEach(room=>room.find(FIND_CONSTRUCTION_SITES).forEach(cs=>cs.remove()));
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildRoads(){
|
||||||
|
Object.values(Game.rooms).forEach(room=>
|
||||||
|
{
|
||||||
|
room.memory.layout.sources.forEach(
|
||||||
|
sId => {
|
||||||
|
const source = Game.getObjectById(sId);
|
||||||
|
var roads = [];
|
||||||
|
roads = roads.concat(source.pos.findPathTo(room.controller));
|
||||||
|
roads.pop();
|
||||||
|
roads = roads.concat(source.pos.findPathTo(room.find(FIND_MY_STRUCTURES, { filter:{ structureType:STRUCTURE_SPAWN }})[0]));
|
||||||
|
roads.pop();
|
||||||
|
roads.forEach(tile => room.createConstructionSite(tile.x, tile.y, STRUCTURE_ROAD));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
return "OK";
|
||||||
|
}
|
||||||
36
CreepClass.js
Normal file
36
CreepClass.js
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
const jobHarvester = require("JobHarvester");
|
||||||
|
const jobMiner = require("JobMiner");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
setup: function () {
|
||||||
|
Creep.prototype = _Creep.prototype;
|
||||||
|
global.Role = Role;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Role = {
|
||||||
|
HARVESTER: 0,
|
||||||
|
MINER: 1,
|
||||||
|
SUPPLIER: 2,
|
||||||
|
UPGRADER: 3,
|
||||||
|
BUILDER: 4
|
||||||
|
}
|
||||||
|
|
||||||
|
class _Creep extends Creep {
|
||||||
|
begin(){
|
||||||
|
if(!this.memory.job) this.memory.job = { role: Role.HARVESTER };
|
||||||
|
switch (this.memory.job.role) {
|
||||||
|
case Role.HARVESTER: jobHarvester.begin(this); break;
|
||||||
|
case Role.MINER: jobMiner.begin(this); break;
|
||||||
|
}
|
||||||
|
this.memory.init = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
tick(){
|
||||||
|
if(!this.memory.init) this.begin();
|
||||||
|
switch (this.memory.job.role) {
|
||||||
|
case Role.HARVESTER: jobHarvester.tick(this); break;
|
||||||
|
case Role.MINER: jobMiner.tick(this); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
8
JobBuilder.js
Normal file
8
JobBuilder.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
module.exports = {
|
||||||
|
begin(creep){
|
||||||
|
},
|
||||||
|
|
||||||
|
tick(creep){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
89
JobHarvester.js
Normal file
89
JobHarvester.js
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
module.exports = {
|
||||||
|
begin(creep){
|
||||||
|
if(!creep.memory.harvesting) creep.memory.harvesting = false;
|
||||||
|
if(!creep.memory.counter) creep.memory.counter = 0;
|
||||||
|
},
|
||||||
|
|
||||||
|
tick(creep){
|
||||||
|
if(creep.memory.harvesting) {
|
||||||
|
pickUpEnergy(creep);
|
||||||
|
onEnergyFullTurnToJobMode(creep);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
performJobs(creep);
|
||||||
|
onOutOfEnergyTurnHarvestingMode(creep);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function pickUpEnergy(creep){
|
||||||
|
if(!creep.memory.target) {
|
||||||
|
const target = creep.pos.findClosestByRange(FIND_DROPPED_RESOURCES, {filter: {resourceType: RESOURCE_ENERGY}});
|
||||||
|
if(target) creep.memory.target = target.id;
|
||||||
|
}
|
||||||
|
const resource = Game.getObjectById(creep.memory.target);
|
||||||
|
if(!resource) creep.memory.target = undefined;
|
||||||
|
if(creep.pos.isNearTo(resource)) creep.pickup(resource);
|
||||||
|
else creep.moveTo(resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
function harvestSource(creep){
|
||||||
|
if(!creep.memory.target) creep.memory.target = getSourceIdFromRoomTable(creep);
|
||||||
|
var source = Game.getObjectById(creep.memory.target);
|
||||||
|
if(creep.pos.isNearTo(source)) creep.harvest(source);
|
||||||
|
else creep.moveTo(source);
|
||||||
|
}
|
||||||
|
|
||||||
|
function onEnergyFullTurnToJobMode(creep){
|
||||||
|
if(!creep.store.getFreeCapacity(RESOURCE_ENERGY)) {
|
||||||
|
creep.memory.harvesting = false;
|
||||||
|
creep.memory.target = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function performJobs(creep){
|
||||||
|
if(!creep.memory.target) creep.memory.target = getJobTarget(creep);
|
||||||
|
var target = Game.getObjectById(creep.memory.target);
|
||||||
|
if(creep.pos.isNearTo(target)) doJob(creep, target);
|
||||||
|
else creep.moveTo(target);
|
||||||
|
}
|
||||||
|
|
||||||
|
function onOutOfEnergyTurnHarvestingMode(creep){
|
||||||
|
if(!creep.store.getUsedCapacity(RESOURCE_ENERGY)) {
|
||||||
|
creep.memory.harvesting = true;
|
||||||
|
creep.memory.target = undefined;
|
||||||
|
creep.memory.counter += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSourceIdFromRoomTable(creep) {
|
||||||
|
const sourceTable = creep.room.memory.layout.sources;
|
||||||
|
const sourceIterator = creep.memory.counter%creep.room.memory.layout.sources.length;
|
||||||
|
const sourceId = sourceTable[sourceIterator];
|
||||||
|
return sourceId;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getJobTarget(creep) {
|
||||||
|
var target;
|
||||||
|
switch (creep.memory.counter%3) {
|
||||||
|
case 0: target = creep.room.controller; break;
|
||||||
|
case 1: target = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {filter:{structureType:STRUCTURE_SPAWN}}); break;
|
||||||
|
case 2: target = creep.pos.findClosestByRange(FIND_MY_CONSTRUCTION_SITES); break;
|
||||||
|
}
|
||||||
|
if(target) return target.id;
|
||||||
|
creep.memory.counter++;
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function doJob(creep, target){
|
||||||
|
var response;
|
||||||
|
switch (creep.memory.counter%3) {
|
||||||
|
case 0: response = creep.upgradeController(target); break;
|
||||||
|
case 1: response = creep.transfer(target, RESOURCE_ENERGY); break;
|
||||||
|
case 2: response = creep.build(target); break;
|
||||||
|
}
|
||||||
|
if(response == ERR_FULL) {
|
||||||
|
creep.memory.counter++;
|
||||||
|
creep.memory.target = undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
18
JobMiner.js
Normal file
18
JobMiner.js
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
module.exports = {
|
||||||
|
begin(creep){},
|
||||||
|
|
||||||
|
tick(creep){
|
||||||
|
const source = Game.getObjectById(creep.memory.job.source);
|
||||||
|
if(creep.memory.aboveContainer) creep.harvest(source);
|
||||||
|
if(!creep.pos.isNearTo(source)) { creep.moveTo(source); return; }
|
||||||
|
if(creep.memory.container) {
|
||||||
|
const container = Game.getObjectById(creep.memory.container);
|
||||||
|
if(container.pos.x === creep.pos.x, container.pos.y === creep.pos.y) creep.memory.aboveContainer = true;
|
||||||
|
else creep.moveTo(container);
|
||||||
|
} else{
|
||||||
|
const container = source.pos.findInRange(FIND_STRUCTURES, 1, {filter:{structureType:STRUCTURE_CONTAINER}})[0];
|
||||||
|
if(container) creep.memory.container = container.id;
|
||||||
|
else creep.harvest(source);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
8
JobSupplier.js
Normal file
8
JobSupplier.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
module.exports = {
|
||||||
|
begin(creep){
|
||||||
|
},
|
||||||
|
|
||||||
|
tick(creep){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
8
JobUpgrader.js
Normal file
8
JobUpgrader.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
module.exports = {
|
||||||
|
begin(creep){
|
||||||
|
},
|
||||||
|
|
||||||
|
tick(creep){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
7
Math.js
Normal file
7
Math.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
module.exports = {
|
||||||
|
clamp: function(v, min, max){
|
||||||
|
if(v<min) v = min;
|
||||||
|
else if (max < v) v = max;
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
}
|
||||||
44
RoomClass.js
Normal file
44
RoomClass.js
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
module.exports = {
|
||||||
|
setup: function () { Room.prototype = _Room.prototype; }
|
||||||
|
}
|
||||||
|
|
||||||
|
class _Room extends Room {
|
||||||
|
begin(){
|
||||||
|
roomScan(this);
|
||||||
|
jobScan(this);
|
||||||
|
vacancyScan(this);
|
||||||
|
this.memory.init = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
tick(){
|
||||||
|
if(!this.memory.init) this.begin();
|
||||||
|
if(Game.time%100 === 1) vacancyScan(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function roomScan(room){
|
||||||
|
room.memory.layout = {
|
||||||
|
sources: room.find(FIND_SOURCES).map(s=>s.id)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function jobScan(room){
|
||||||
|
if(!room.memory.jobs) room.memory.jobs = Array(10).fill({role: Role.HARVESTER})
|
||||||
|
.concat(room.memory.layout.sources.map(s=> {return {role: Role.MINER, source: s}}));
|
||||||
|
}
|
||||||
|
|
||||||
|
function vacancyScan(room){
|
||||||
|
const activeJobs = room.find(FIND_MY_CREEPS).map(creep=>creep.memory.job);
|
||||||
|
console.log(activeJobs.toString());
|
||||||
|
console.log(room.memory.jobs.toString());
|
||||||
|
const jobs = room.memory.jobs.filter((j)=>{
|
||||||
|
const index = activeJobs.findIndex(aj=> _.isEqual(aj,j));
|
||||||
|
if(index < 0) return true;
|
||||||
|
activeJobs.splice(index,1);
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
console.log(jobs.toString());
|
||||||
|
|
||||||
|
room.memory.vacancies = jobs;
|
||||||
|
}
|
||||||
|
|
||||||
21
Structure.js
Normal file
21
Structure.js
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
const spawnClass = require("StructureSpawnClass");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
setup() {
|
||||||
|
spawnClass.setup();
|
||||||
|
},
|
||||||
|
|
||||||
|
begin(structure){
|
||||||
|
switch (structure.structureType) {
|
||||||
|
case STRUCTURE_SPAWN: structure.begin(); break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
tick(structure){
|
||||||
|
switch (structure.structureType) {
|
||||||
|
case STRUCTURE_SPAWN: structure.tick(); break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
43
StructureSpawnClass.js
Normal file
43
StructureSpawnClass.js
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
module.exports = {
|
||||||
|
setup: function () { StructureSpawn.prototype = _StructureSpawn.prototype; }
|
||||||
|
}
|
||||||
|
|
||||||
|
class _StructureSpawn extends StructureSpawn {
|
||||||
|
begin(){
|
||||||
|
if (!this.memory.creepCounter) this.memory.creepCounter = 0;
|
||||||
|
this.memory.init = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
tick(){
|
||||||
|
if(!this.memory.init) this.begin();
|
||||||
|
if(this.store.getUsedCapacity(RESOURCE_ENERGY) < 300) return;
|
||||||
|
const job = this.room.memory.vacancies.pop();
|
||||||
|
if(job){
|
||||||
|
const name = getJobName(job.role);
|
||||||
|
const body = getBodyByJob(job.role);
|
||||||
|
if(this.createCreep(job, name, body) != OK) this.room.memory.vacancies.push(job);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
createCreep(job, name, body) {
|
||||||
|
const response = this.spawnCreep(body, name + ": " + this.memory.creepCounter, {
|
||||||
|
memory: { job: job }
|
||||||
|
});
|
||||||
|
if (response == OK) this.memory.creepCounter++;
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getJobName(role){
|
||||||
|
switch (role) {
|
||||||
|
case Role.HARVESTER: return "Harvy";
|
||||||
|
case Role.MINER: return "minny";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getBodyByJob(role){
|
||||||
|
switch (role) {
|
||||||
|
case Role.HARVESTER: return [WORK, CARRY, MOVE, MOVE];
|
||||||
|
case Role.MINER: return [WORK, WORK, MOVE];
|
||||||
|
}
|
||||||
|
}
|
||||||
59
ai_main.js
59
ai_main.js
@@ -1,59 +0,0 @@
|
|||||||
|
|
||||||
/* ###########################
|
|
||||||
# Cross room managment AI #
|
|
||||||
########################### */
|
|
||||||
|
|
||||||
var roles = require("roles");
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
run: function () {
|
|
||||||
if (Memory.AImain == null) Memory.AImain = {};
|
|
||||||
if (Memory.AImain.MainHub == null) Memory.AImain.MainHub = "insert name"
|
|
||||||
for (var n_flag in Game.flags) {
|
|
||||||
var flag = Game.flags[n_flag];
|
|
||||||
if (flag.color == COLOR_YELLOW) {
|
|
||||||
if (flag.room != undefined && flag.room.find(FIND_MY_CREEPS).length != flag.room.find(FIND_CREEPS).length) {
|
|
||||||
Game.spawns[Memory.AImain.MainHub].spawnCreep([TOUGH, TOUGH, TOUGH, TOUGH, TOUGH, TOUGH, TOUGH, ATTACK, ATTACK, ATTACK, MOVE],
|
|
||||||
"Defender", { memory: { role: roles.ROLE_DEFENDER, flag: flag.name } });
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (flag.room == undefined || (flag.room != undefined && (flag.room.controller.reservation == undefined || flag.room.controller.reservation.ticksToEnd < 4500))) {
|
|
||||||
if (Memory.AImain.MainHub != "insert name") Game.spawns[Memory.AImain.MainHub].spawnCreep([MOVE, CLAIM, CLAIM], "columbus: "+flag.name, { memory: { role: roles.ROLE_RESERVER, target: flag.name, home: Memory.AImain.MainHub } });
|
|
||||||
}
|
|
||||||
if (flag.room != undefined && flag.room.controller.reservation != undefined) {
|
|
||||||
var containers = flag.room.find(FIND_STRUCTURES, { filter: (s) => (s.structureType == STRUCTURE_CONTAINER) });
|
|
||||||
if (containers.length > 0) {
|
|
||||||
var has_miner = false;
|
|
||||||
var creeps = flag.room.find(FIND_CREEPS);
|
|
||||||
for (var cp in creeps) {
|
|
||||||
console.log(creeps[cp]);
|
|
||||||
var creep = creeps[cp];
|
|
||||||
if (creep.memory.role == roles.ROLE_MINER) {
|
|
||||||
has_miner = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!has_miner) {
|
|
||||||
Game.spawns[Memory.AImain.MainHub].spawnCreep([WORK, WORK, WORK, WORK, WORK, MOVE], "ForeignMiner", { memory: { role: roles.ROLE_MINER, container_id: containers[0].id } });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var harvs = flag.room.find(FIND_MY_CREEPS).length;
|
|
||||||
var source_id = flag.room.find(FIND_SOURCES)[0].id;
|
|
||||||
if (harvs < 4) {
|
|
||||||
var spawn = Game.spawns[Memory.AImain.MainHub];
|
|
||||||
var body = [];
|
|
||||||
for (let i = 0; i < Math.floor(spawn.room.energyAvailable / 200); i++) {
|
|
||||||
body.push(WORK);
|
|
||||||
body.push(CARRY);
|
|
||||||
body.push(MOVE);
|
|
||||||
}
|
|
||||||
var name = "ForeignHarvy" + spawn.memory.creep_iterate;
|
|
||||||
spawn.spawnCreep(body, name, {
|
|
||||||
memory:
|
|
||||||
{ role: roles.ROLE_RESERVED_HARVESTER, source_id: source_id }
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
33
commands.js
33
commands.js
@@ -1,33 +0,0 @@
|
|||||||
module.exports = {
|
|
||||||
configure: function () {
|
|
||||||
// Room management
|
|
||||||
global.RoomInfo = function () { RoomInfo(); };
|
|
||||||
global.RoomAdd = function (name, colonize) { RoomAdd(name, colonize) };
|
|
||||||
global.RoomRemove = function (name) { RoomRemove(name) };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//#region Room management
|
|
||||||
function RoomInfo() {
|
|
||||||
var message = "";
|
|
||||||
Memory.system.rooms.forEach(room => {
|
|
||||||
message += room.name + ": ";
|
|
||||||
message += room.colonize ? "colonize: " : "control: ";
|
|
||||||
var roomObject = Game.rooms[room.name];
|
|
||||||
if (roomObject.controller != undefined) {
|
|
||||||
message += roomObject.controller.my ? "done" : "in progress";
|
|
||||||
}
|
|
||||||
message += "\n";
|
|
||||||
});
|
|
||||||
console.log(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
function RoomAdd(name, colonize) {
|
|
||||||
Memory.system.rooms.push({ name: name, colonize: colonize });
|
|
||||||
}
|
|
||||||
|
|
||||||
function RoomRemove(name) {
|
|
||||||
Memory.system.rooms = Memory.system.rooms.filter((x) => { return x.name != name; })
|
|
||||||
}
|
|
||||||
//#endregion
|
|
||||||
|
|
||||||
92
creeps.js
92
creeps.js
@@ -1,92 +0,0 @@
|
|||||||
|
|
||||||
/* ###################################################################
|
|
||||||
# An inherited creep class and a module to expose it to the loop. #
|
|
||||||
###################################################################*/
|
|
||||||
|
|
||||||
const roles = require("roles");
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
|
|
||||||
configure: function () {
|
|
||||||
// Replaces the creep class by the MyCreep class which changes the type of all Creep objects as well.
|
|
||||||
Creep.prototype = MyCreep.prototype;
|
|
||||||
setConstants();
|
|
||||||
// configure all creeps (in case new memory requirements).
|
|
||||||
for (const name in Game.creeps) {
|
|
||||||
Game.creeps[name].configure();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
// All creeps execute their roles every tick.
|
|
||||||
execute: function () {
|
|
||||||
for (const name in Game.creeps) {
|
|
||||||
Game.creeps[name].execute();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
class MyCreep extends Creep {
|
|
||||||
configure() {
|
|
||||||
}
|
|
||||||
|
|
||||||
execute() {
|
|
||||||
switch (this.memory.role) {
|
|
||||||
case roles.ROLE_HARVESTER: roles.harvest(this);
|
|
||||||
break;
|
|
||||||
case roles.ROLE_MINER:
|
|
||||||
if (this.goTo(this.memory.container_id)) {
|
|
||||||
this.harvestSource();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case roles.ROLE_SUPPLIER: roles.supply(this);
|
|
||||||
break;
|
|
||||||
case roles.ROLE_BUILDER: roles.build(this);
|
|
||||||
break;
|
|
||||||
case roles.ROLE_UPGRADER: roles.upgrade(this);
|
|
||||||
break;
|
|
||||||
case roles.ROLE_DEFENDER: roles.defend(this);
|
|
||||||
break;
|
|
||||||
case roles.ROLE_RESERVER: roles.reserve(this);
|
|
||||||
break;
|
|
||||||
case roles.ROLE_RESERVED_HARVESTER: roles.harvest_reserved(this);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Moves to the object with given id. Returns true if arrived.
|
|
||||||
goTo(id) {
|
|
||||||
const target = Game.getObjectById(id)
|
|
||||||
if (target != null) {
|
|
||||||
if (this.pos.isEqualTo(target.pos)) return true;
|
|
||||||
this.moveTo(target);
|
|
||||||
} else {
|
|
||||||
console.log(this.name + " can't find object with id: " + id);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// harvests set or closest source, moves to it if to far and return true if fully loaded.
|
|
||||||
harvestSource() {
|
|
||||||
if (this.memory.source_id == undefined) {
|
|
||||||
this.memory.source_id = this.pos.findClosestByPath(FIND_SOURCES).id;
|
|
||||||
}
|
|
||||||
const result = this.harvest(Game.getObjectById(this.memory.source_id));
|
|
||||||
if (result == ERR_NOT_IN_RANGE) this.goTo(this.memory.source_id);
|
|
||||||
return this.store.getFreeCapacity(RESOURCE_ENERGY) == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
function setConstants() {
|
|
||||||
global.HARVESTER = 0;
|
|
||||||
global.MINER = 1;
|
|
||||||
global.SUPPLIER = 2;
|
|
||||||
global.BUILDER = 3;
|
|
||||||
global.UPGRADER = 4;
|
|
||||||
global.DEFENDER = 5;
|
|
||||||
global.RESERVER = 6;
|
|
||||||
global.RESERVED_HARVESTER = 7;
|
|
||||||
}
|
|
||||||
64
main.js
64
main.js
@@ -1,40 +1,44 @@
|
|||||||
|
const Commands = require("Commands");
|
||||||
/* ######################
|
const CreepClass = require("CreepClass");
|
||||||
# Screeps 2022 #
|
const RoomClass = require("RoomClass");
|
||||||
######################*/
|
const Structure = require("Structure");
|
||||||
|
|
||||||
const spawns = require("spawns");
|
|
||||||
const creeps = require("creeps");
|
|
||||||
const towers = require("towers");
|
|
||||||
const ai_main = require("ai_main");
|
|
||||||
|
|
||||||
const system = require("system");
|
|
||||||
|
|
||||||
module.exports.loop = function () {
|
module.exports.loop = function () {
|
||||||
// New system
|
if(!global.compiled) onRecompile();
|
||||||
if (global.started == undefined) {
|
else if (!global.started) onRestart();
|
||||||
console.log("Recompiled...");
|
else onTick();
|
||||||
|
}
|
||||||
|
|
||||||
|
function onRecompile(){
|
||||||
|
setupClasses();
|
||||||
|
console.log("Script recompiled...");
|
||||||
|
global.compiled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function onRestart(){
|
||||||
|
Object.values(Game.rooms).forEach(room => room.begin());
|
||||||
|
Object.values(Game.creeps).forEach(creep => creep.begin());
|
||||||
|
Object.values(Game.structures).forEach(structure => Structure.begin(structure));
|
||||||
global.started = true;
|
global.started = true;
|
||||||
system.start();
|
|
||||||
}
|
}
|
||||||
system.update();
|
|
||||||
|
|
||||||
|
function onTick(){
|
||||||
|
Object.values(Game.rooms).forEach(room => room.tick());
|
||||||
// Old system
|
Object.values(Game.creeps).forEach(creep => creep.tick());
|
||||||
ai_main.run();
|
Object.values(Game.structures).forEach(structure => Structure.tick(structure));
|
||||||
towers.run();
|
if(!(Game.time % 100)) cleanUp();
|
||||||
spawns.run();
|
|
||||||
if (Game.time % 60 == 0) {
|
|
||||||
clear_dead_creeps_from_memory();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function clear_dead_creeps_from_memory() {
|
function setupClasses(){
|
||||||
for (var cr in Memory.creeps) {
|
Commands.setup();
|
||||||
if (Game.creeps[cr] == null) {
|
CreepClass.setup();
|
||||||
delete (Memory.creeps[cr]);
|
RoomClass.setup();
|
||||||
}
|
Structure.setup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cleanUp(){
|
||||||
|
Object.keys(Memory.rooms).forEach(roomName => { if(!Game.rooms[roomName]) Memory.rooms[roomName] = undefined; });
|
||||||
|
Object.keys(Memory.creeps).forEach(creepName => { if(!Game.creeps[creepName]) Memory.creeps[creepName] = undefined; });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
398
roles.js
398
roles.js
@@ -1,398 +0,0 @@
|
|||||||
|
|
||||||
/* ##############################
|
|
||||||
# Roles: Creep functionality #
|
|
||||||
############################## */
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
|
|
||||||
ROLE_HARVESTER: 0,
|
|
||||||
ROLE_MINER: 1,
|
|
||||||
ROLE_SUPPLIER: 2,
|
|
||||||
ROLE_BUILDER: 3,
|
|
||||||
ROLE_UPGRADER: 4,
|
|
||||||
ROLE_DEFENDER: 5,
|
|
||||||
ROLE_RESERVER: 6,
|
|
||||||
ROLE_RESERVED_HARVESTER: 7,
|
|
||||||
|
|
||||||
harvest: function (creep) {
|
|
||||||
setupMemory();
|
|
||||||
if (creep.memory.harvesting) {
|
|
||||||
extractEnergySource();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
searchTarget();
|
|
||||||
var response = performJob();
|
|
||||||
handleResponse(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
function setupMemory() {
|
|
||||||
if (creep.memory.harvesting == undefined) creep.memory.harvesting = true;
|
|
||||||
if (creep.memory.counter == undefined) creep.memory.counter = 0;
|
|
||||||
if (creep.memory.target == undefined) creep.memory.target = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function extractEnergySource() {
|
|
||||||
var result = OK;
|
|
||||||
var target = creep.pos.findClosestByPath(FIND_STRUCTURES, { filter: (s) => (s.structureType == STRUCTURE_CONTAINER && s.store.getUsedCapacity(RESOURCE_ENERGY) > creep.store.getFreeCapacity(RESOURCE_ENERGY)) });
|
|
||||||
if (target == null) {
|
|
||||||
target = Game.getObjectById(creep.memory.source_id);
|
|
||||||
result = creep.harvest(target);
|
|
||||||
} else {
|
|
||||||
result = creep.withdraw(target, RESOURCE_ENERGY);
|
|
||||||
}
|
|
||||||
if (result == ERR_NOT_IN_RANGE) {
|
|
||||||
creep.moveTo(target);
|
|
||||||
}
|
|
||||||
if (result == ERR_NOT_ENOUGH_RESOURCES || creep.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
|
||||||
creep.memory.harvesting = false;
|
|
||||||
creep.memory.target = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function searchTarget() {
|
|
||||||
if (creep.memory.target == null) {
|
|
||||||
var role_type = creep.memory.counter % 10;
|
|
||||||
if (role_type == 0) {
|
|
||||||
creep.memory.target = creep.room.controller.id;
|
|
||||||
}
|
|
||||||
else if (role_type < 5) {
|
|
||||||
var target = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {
|
|
||||||
filter: (s) => (s.structureType == STRUCTURE_SPAWN || s.structureType == STRUCTURE_EXTENSION) && s.store.getFreeCapacity(RESOURCE_ENERGY) > 0
|
|
||||||
});
|
|
||||||
if (target != null) creep.memory.target = target.id;
|
|
||||||
else creep.memory.counter += 1;
|
|
||||||
}
|
|
||||||
else if (role_type < 9) {
|
|
||||||
var target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
|
|
||||||
if (target != null) creep.memory.target = target.id;
|
|
||||||
else creep.memory.counter += 1;
|
|
||||||
} else {
|
|
||||||
var target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
|
|
||||||
filter: (s) => (s.structureType != STRUCTURE_WALL && s.structureType != STRUCTURE_RAMPART) && s.hits < s.hitsMax
|
|
||||||
});
|
|
||||||
if (target != null) creep.memory.target = target.id;
|
|
||||||
else creep.memory.counter += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function performJob() {
|
|
||||||
var target = Game.getObjectById(creep.memory.target);
|
|
||||||
var response = OK;
|
|
||||||
var role_type = creep.memory.counter % 10;
|
|
||||||
if (role_type == 0) {
|
|
||||||
response = creep.transfer(target, RESOURCE_ENERGY);
|
|
||||||
}
|
|
||||||
else if (role_type < 5) {
|
|
||||||
response = creep.transfer(target, RESOURCE_ENERGY);
|
|
||||||
if (target != null && target.store.getFreeCapacity(RESOURCE_ENERGY) == 0) creep.memory.target = null;
|
|
||||||
}
|
|
||||||
else if (role_type < 9) {
|
|
||||||
response = creep.build(target);
|
|
||||||
if (target != null && target.progress == target.progressTotal) creep.memory.target = null;
|
|
||||||
} else {
|
|
||||||
response = creep.repair(target);
|
|
||||||
if (target != null && target.hits == target.hitsMax) creep.memory.target = null;
|
|
||||||
}
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleResponse(response) {
|
|
||||||
if (response == ERR_NOT_IN_RANGE) {
|
|
||||||
var target = Game.getObjectById(creep.memory.target);
|
|
||||||
creep.moveTo(target);
|
|
||||||
} else if (response == ERR_FULL || response == ERR_INVALID_TARGET) {
|
|
||||||
creep.memory.target = null;
|
|
||||||
}
|
|
||||||
if (creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0) {
|
|
||||||
creep.memory.harvesting = true;
|
|
||||||
creep.memory.target = null;
|
|
||||||
creep.memory.counter += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
mine: function (creep) {
|
|
||||||
var container = Game.getObjectById(creep.memory.container_id);
|
|
||||||
if (container != null && creep.pos.isEqualTo(container.pos)) {
|
|
||||||
if (creep.memory.source_id == undefined) creep.memory.source_id = creep.pos.findClosestByPath(FIND_SOURCES).id;
|
|
||||||
creep.harvest(Game.getObjectById(creep.memory.source_id));
|
|
||||||
} else {
|
|
||||||
creep.moveTo(Game.getObjectById(creep.memory.container_id));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
supply: function (creep) {
|
|
||||||
// Memory creation
|
|
||||||
if (creep.memory.restock == undefined) creep.memory.restock = true;
|
|
||||||
if (creep.memory.target == undefined) creep.memory.target = null;
|
|
||||||
// Job implementation
|
|
||||||
if (creep.memory.restock) {
|
|
||||||
if (creep.memory.target == null) { // aquire target
|
|
||||||
var target = creep.pos.findClosestByPath(FIND_DROPPED_RESOURCES);
|
|
||||||
if (target == null) target = creep.pos.findClosestByPath(FIND_TOMBSTONES);
|
|
||||||
if (target == null) target = creep.pos.findClosestByPath(FIND_STRUCTURES,
|
|
||||||
{
|
|
||||||
filter: (s) => (s.structureType == STRUCTURE_CONTAINER
|
|
||||||
&& 0 < s.store.getUsedCapacity(RESOURCE_ENERGY))
|
|
||||||
});
|
|
||||||
if (target == null) target = creep.room.storage;
|
|
||||||
if (target != null) creep.memory.target = target.id;
|
|
||||||
}
|
|
||||||
var target = Game.getObjectById(creep.memory.target);
|
|
||||||
if (creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE || creep.pickup(target) == ERR_NOT_IN_RANGE) creep.moveTo(target);
|
|
||||||
if (target != null && target.store != null && target.store.getUsedCapacity(RESOURCE_ENERGY) == 0) creep.memory.target = null;
|
|
||||||
if (creep.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
|
||||||
creep.memory.restock = false;
|
|
||||||
creep.memory.target = null;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (creep.memory.target == null) { // aquire target
|
|
||||||
var structure = creep.pos.findClosestByPath(FIND_MY_STRUCTURES,
|
|
||||||
{ filter: (s) => (s.structureType == STRUCTURE_TOWER && s.store.getUsedCapacity(RESOURCE_ENERGY) < 900) });
|
|
||||||
if (structure == null) structure = creep.pos.findClosestByPath(FIND_MY_STRUCTURES,
|
|
||||||
{
|
|
||||||
filter: (s) => ((s.structureType == STRUCTURE_SPAWN || s.structureType == STRUCTURE_EXTENSION)
|
|
||||||
&& s.store.getFreeCapacity(RESOURCE_ENERGY) > s.store.getCapacity(RESOURCE_ENERGY) * 0.1)
|
|
||||||
});
|
|
||||||
if (structure == null) structure = creep.pos.findClosestByPath(FIND_MY_STRUCTURES,
|
|
||||||
{ filter: (s) => ((s.id == "5f3e44e1014017c66fc5df10") && s.store.getFreeCapacity() > 100) });
|
|
||||||
if (structure == null) structure = creep.room.storage;
|
|
||||||
if (structure == null) return;
|
|
||||||
creep.memory.target = structure.id;
|
|
||||||
}
|
|
||||||
var target = Game.getObjectById(creep.memory.target);
|
|
||||||
if (creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) creep.moveTo(target);
|
|
||||||
if (target != null && target.store.getFreeCapacity(RESOURCE_ENERGY) == 0) creep.memory.target = null;
|
|
||||||
if (creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0) {
|
|
||||||
creep.memory.restock = true;
|
|
||||||
creep.memory.target = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
build: function (creep) {
|
|
||||||
//creep.memory.target = null;
|
|
||||||
|
|
||||||
// Memory creation
|
|
||||||
if (creep.memory.restock == undefined) creep.memory.restock = true;
|
|
||||||
if (creep.memory.target == undefined) creep.memory.target = null;
|
|
||||||
|
|
||||||
// Job implementation
|
|
||||||
if (creep.memory.restock) {
|
|
||||||
if (creep.memory.target == null) {
|
|
||||||
if (creep.room.storage != null && 1000 < creep.room.storage.store.getUsedCapacity(RESOURCE_ENERGY)) creep.memory.target = creep.room.storage.id;
|
|
||||||
else {
|
|
||||||
var container = creep.pos.findClosestByPath(FIND_STRUCTURES,
|
|
||||||
{
|
|
||||||
filter: (s) => (s.structureType == STRUCTURE_CONTAINER
|
|
||||||
&& 0 < s.store.getUsedCapacity(RESOURCE_ENERGY))
|
|
||||||
});
|
|
||||||
if (container != null) creep.memory.target = container.id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var target = Game.getObjectById(creep.memory.target);
|
|
||||||
if (creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) creep.moveTo(target);
|
|
||||||
if (target != null && target.store.getUsedCapacity(RESOURCE_ENERGY) < creep.store.getFreeCapacity(RESOURCE_ENERGY) * 0.4) creep.memory.target = null;
|
|
||||||
if (creep.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
|
||||||
creep.memory.restock = false;
|
|
||||||
creep.memory.target = null;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (creep.memory.target == null) { // aquire target
|
|
||||||
// constuction
|
|
||||||
var structure = creep.pos.findClosestByRange(FIND_MY_CONSTRUCTION_SITES);
|
|
||||||
|
|
||||||
// buildings
|
|
||||||
if (structure == null) {
|
|
||||||
var buildings = creep.room.find(FIND_STRUCTURES,
|
|
||||||
{ filter: (s) => ((s.structureType != STRUCTURE_RAMPART || s.structureType != STRUCTURE_WALL) && s.hits < s.hitsMax) });
|
|
||||||
for (var building in buildings) {
|
|
||||||
building = buildings[building];
|
|
||||||
if (structure == null || structure.hits > building.hits) structure = building;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ramparts and walls
|
|
||||||
if (structure == null) {
|
|
||||||
var ramparts = creep.room.find(FIND_STRUCTURES,
|
|
||||||
{ filter: (s) => ((s.structureType == STRUCTURE_RAMPART || s.structureType == STRUCTURE_WALL) && s.hits < s.hitsMax) });
|
|
||||||
for (var ramp in ramparts) {
|
|
||||||
ramp = ramparts[ramp];
|
|
||||||
if (structure == null || structure.hits > ramp.hits) structure = ramp;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
creep.memory.target = structure.id;
|
|
||||||
}
|
|
||||||
var target = Game.getObjectById(creep.memory.target);
|
|
||||||
if (target == null || target.hits == target.hitsMax) creep.memory.target = null;
|
|
||||||
if (creep.build(target) == ERR_NOT_IN_RANGE || creep.repair(target) == ERR_NOT_IN_RANGE) creep.moveTo(target);
|
|
||||||
if (creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0) {
|
|
||||||
creep.memory.restock = true;
|
|
||||||
creep.memory.target = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
upgrade: function (creep) {
|
|
||||||
// Memory creation
|
|
||||||
if (creep.memory.restock == undefined) creep.memory.restock = true;
|
|
||||||
if (creep.memory.target == undefined) creep.memory.target = null;
|
|
||||||
|
|
||||||
// Job implementation
|
|
||||||
if (creep.memory.restock) {
|
|
||||||
if (creep.memory.target == null) {
|
|
||||||
if (creep.room.storage != null && 1000 < creep.room.storage.store.getUsedCapacity(RESOURCE_ENERGY)) creep.memory.target = creep.room.storage.id;
|
|
||||||
else {
|
|
||||||
var container = creep.pos.findClosestByPath(FIND_STRUCTURES,
|
|
||||||
{
|
|
||||||
filter: (s) => (s.structureType == STRUCTURE_CONTAINER
|
|
||||||
&& 0 < s.store.getUsedCapacity(RESOURCE_ENERGY))
|
|
||||||
});
|
|
||||||
if (container != null) creep.memory.target = container.id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var target = Game.getObjectById(creep.memory.target);
|
|
||||||
if (creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) creep.moveTo(target);
|
|
||||||
if (target != null && target.store.getUsedCapacity(RESOURCE_ENERGY) < creep.store.getFreeCapacity(RESOURCE_ENERGY) * 0.4) creep.memory.target = null;
|
|
||||||
if (creep.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
|
||||||
creep.memory.restock = false;
|
|
||||||
creep.memory.target = null;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (creep.memory.target == null) { // aquire target
|
|
||||||
var structure = structure = creep.room.controller;
|
|
||||||
creep.memory.target = structure.id;
|
|
||||||
}
|
|
||||||
var target = Game.getObjectById(creep.memory.target);
|
|
||||||
if (creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) creep.moveTo(target);
|
|
||||||
if (creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0) {
|
|
||||||
creep.memory.restock = true;
|
|
||||||
creep.memory.target = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
defend: function (creep) {
|
|
||||||
if (creep.memory.flag != undefined && Game.flags[creep.memory.flag].room.name != creep.room.name) creep.moveTo(Game.flags[creep.memory.flag]);
|
|
||||||
if (creep.memory.target == undefined) creep.memory.target = null;
|
|
||||||
if (creep.memory.target == null) {
|
|
||||||
var enemy = creep.pos.findClosestByPath(FIND_HOSTILE_CREEPS);
|
|
||||||
if (enemy != null) creep.memory.target = enemy.id;
|
|
||||||
}
|
|
||||||
var enemy = Game.getObjectById(creep.memory.target);
|
|
||||||
if (enemy != null) {
|
|
||||||
creep.say("ATTACK");
|
|
||||||
if (creep.attack(enemy) == ERR_NOT_IN_RANGE) creep.moveTo(enemy);
|
|
||||||
} else {
|
|
||||||
creep.memory.target = null;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
reserve: function (creep) {
|
|
||||||
if (Game.flags[creep.memory.target].room != undefined && Game.flags[creep.memory.target].room == creep.room) {
|
|
||||||
if (creep.memory.target == "xxx") {
|
|
||||||
if (creep.claimController(creep.room.controller) == ERR_NOT_IN_RANGE) {
|
|
||||||
creep.moveTo(creep.room.controller);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
if (creep.reserveController(creep.room.controller) == ERR_NOT_IN_RANGE) {
|
|
||||||
creep.moveTo(creep.room.controller);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
creep.moveTo(Game.flags[creep.memory.target]);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
harvest_reserved: function (creep) {
|
|
||||||
creep.memory.home = "SpawnAlpha"
|
|
||||||
//memory setup
|
|
||||||
if (creep.memory.harvesting == undefined) creep.memory.harvesting = true;
|
|
||||||
if (creep.memory.counter == undefined) creep.memory.counter = 0;
|
|
||||||
if (creep.memory.target == undefined) creep.memory.target = null;
|
|
||||||
|
|
||||||
if (creep.memory.harvesting) {
|
|
||||||
var source = Game.getObjectById(creep.memory.source_id);
|
|
||||||
if (source != undefined && creep.room.name == source.room.name) {
|
|
||||||
var container = creep.pos.findClosestByPath(FIND_STRUCTURES, { filter: (s) => (s.structureType == STRUCTURE_CONTAINER && s.store.getUsedCapacity(RESOURCE_ENERGY) > creep.store.getFreeCapacity(RESOURCE_ENERGY)) });
|
|
||||||
if (container == null) {
|
|
||||||
result = creep.harvest(source);
|
|
||||||
if (result == ERR_NOT_IN_RANGE) {
|
|
||||||
creep.moveTo(source);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
var result = creep.withdraw(container, RESOURCE_ENERGY);
|
|
||||||
if (result == ERR_NOT_IN_RANGE) {
|
|
||||||
creep.moveTo(container);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (creep.store.getFreeCapacity(RESOURCE_ENERGY) == 0) {
|
|
||||||
creep.memory.harvesting = false;
|
|
||||||
creep.memory.target = null;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
creep.moveTo(source);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (creep.memory.target == null) {
|
|
||||||
switch (creep.memory.counter % 3) {
|
|
||||||
case 0: // building
|
|
||||||
creep.memory.target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
|
|
||||||
if (creep.memory.target != null) creep.memory.target = creep.memory.target.id;
|
|
||||||
else creep.memory.counter += 1;
|
|
||||||
break;
|
|
||||||
case 1: // repairing (no walls or ramparts)
|
|
||||||
creep.memory.target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
|
|
||||||
filter: (s) => (s.structureType != STRUCTURE_WALL && s.structureType != STRUCTURE_RAMPART) && s.hits < s.hitsMax - creep.store.getUsedCapacity(RESOURCE_ENERGY) * 3 / 4
|
|
||||||
});
|
|
||||||
if (creep.memory.target != null) creep.memory.target = creep.memory.target.id;
|
|
||||||
else creep.memory.counter += 1;
|
|
||||||
break;
|
|
||||||
case 2: // suppling
|
|
||||||
var home = Game.spawns[creep.memory.home];
|
|
||||||
if (creep.room.name == home.room.name) {
|
|
||||||
creep.memory.target = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {
|
|
||||||
filter: (s) => (s.structureType == STRUCTURE_SPAWN || s.structureType == STRUCTURE_EXTENSION || s.structureType == STRUCTURE_STORAGE) && s.store.getFreeCapacity(RESOURCE_ENERGY) > 0
|
|
||||||
});
|
|
||||||
if (creep.memory.target != null) creep.memory.target = creep.memory.target.id;
|
|
||||||
else creep.memory.counter += 1;
|
|
||||||
} else {
|
|
||||||
creep.moveTo(home);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var target = Game.getObjectById(creep.memory.target);
|
|
||||||
var response = OK;
|
|
||||||
switch (creep.memory.counter % 3) {
|
|
||||||
case 0: // building
|
|
||||||
response = creep.build(target);
|
|
||||||
if (target != null && target.progress == target.progressTotal) creep.memory.target = null;
|
|
||||||
break;
|
|
||||||
case 1: // repairing
|
|
||||||
response = creep.repair(target);
|
|
||||||
if (target != null && target.hits == target.hitsMax) creep.memory.target = null;
|
|
||||||
break;
|
|
||||||
case 2: // suppling
|
|
||||||
response = creep.transfer(target, RESOURCE_ENERGY);
|
|
||||||
if (target != null && target.store.getFreeCapacity(RESOURCE_ENERGY) == 0) creep.memory.target = null;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (response == ERR_NOT_IN_RANGE) {
|
|
||||||
creep.moveTo(target);
|
|
||||||
} else if (response == ERR_FULL || response == ERR_INVALID_TARGET) {
|
|
||||||
creep.memory.target = null;
|
|
||||||
}
|
|
||||||
if (creep.store.getUsedCapacity(RESOURCE_ENERGY) == 0) {
|
|
||||||
creep.memory.harvesting = true;
|
|
||||||
creep.memory.target = null;
|
|
||||||
creep.memory.counter += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
7
rooms.js
7
rooms.js
@@ -1,7 +0,0 @@
|
|||||||
/**
|
|
||||||
* Managing the behavior of buildings and creeps is on a per room level.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
}
|
|
||||||
183
spawns.js
183
spawns.js
@@ -1,183 +0,0 @@
|
|||||||
|
|
||||||
/* #####################################
|
|
||||||
# Controls the way creeps are spawn #
|
|
||||||
#####################################*/
|
|
||||||
|
|
||||||
var roles = require("roles");
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
run: function () {
|
|
||||||
for (var sp in Game.spawns) {
|
|
||||||
sp = Game.spawns[sp]
|
|
||||||
// values
|
|
||||||
var creep_count = 0
|
|
||||||
for (var cr in Game.creeps) {
|
|
||||||
if (Game.creeps[cr].room.name == sp.room.name) creep_count += 1;
|
|
||||||
}
|
|
||||||
var source_count = 0;
|
|
||||||
if (sp.memory.sources_ids != undefined) source_count = sp.memory.sources_ids.length;
|
|
||||||
|
|
||||||
|
|
||||||
// Memory setup
|
|
||||||
if (sp.memory.creep_iterate == undefined) sp.memory.creep_iterate = 0;
|
|
||||||
|
|
||||||
if (sp.memory.sources_ids == undefined) {
|
|
||||||
sp.memory.sources_ids = [];
|
|
||||||
var sources = sp.room.find(FIND_SOURCES);
|
|
||||||
for (var source in sources) {
|
|
||||||
source = sources[source];
|
|
||||||
sp.memory.sources_ids.push(source.id);
|
|
||||||
}
|
|
||||||
source_count = sp.memory.sources_ids.length;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sp.memory.containers_ids == undefined || sp.memory.containers_ids.length != source_count) {
|
|
||||||
sp.memory.containers_ids = [];
|
|
||||||
var containers = sp.room.find(FIND_STRUCTURES, { filter: { structureType: STRUCTURE_CONTAINER } });
|
|
||||||
if (containers.length > 0) {
|
|
||||||
for (let i = 0; i < containers.length; i += 1) {
|
|
||||||
sp.memory.containers_ids.push(containers[i].id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Different spawning configurations
|
|
||||||
|
|
||||||
// Add improved versions at top of if-else statement
|
|
||||||
if (sp.memory.containers_ids.length >= source_count && sp.room.energyCapacityAvailable >= 550 && creep_count > source_count) { // Miner method: Miners fille containers rest picks up from them
|
|
||||||
// Check current creep_jobs
|
|
||||||
var jobs = { miner: 0, supplier: 0, builder: 0, upgrader: 0, defender: 0 };
|
|
||||||
var container_id = sp.memory.containers_ids[0];
|
|
||||||
for (var creep in Game.creeps) {
|
|
||||||
var creep = Game.creeps[creep];
|
|
||||||
if (creep.room.name != sp.room.name) continue;
|
|
||||||
switch (creep.memory.role) {
|
|
||||||
case roles.ROLE_MINER:
|
|
||||||
jobs.miner += 1;
|
|
||||||
for (let i = 0; i < sp.memory.containers_ids.length; i++) {
|
|
||||||
if (creep.memory.container_id != sp.memory.containers_ids[i]) container_id = sp.memory.containers_ids[i];
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case roles.ROLE_SUPPLIER:
|
|
||||||
jobs.supplier += creep.body.length;
|
|
||||||
break;
|
|
||||||
case roles.ROLE_BUILDER:
|
|
||||||
jobs.builder += creep.body.length;
|
|
||||||
break;
|
|
||||||
case roles.ROLE_UPGRADER:
|
|
||||||
jobs.upgrader += creep.body.length;
|
|
||||||
break;
|
|
||||||
case roles.ROLE_DEFENDER:
|
|
||||||
jobs.defender += 1;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (jobs.miner < source_count) {
|
|
||||||
if (sp.room.energyAvailable >= 550) create_miner(sp, container_id);
|
|
||||||
}
|
|
||||||
else if (jobs.supplier < source_count * 15 && sp.room.energyAvailable >= 300 && jobs.miner > 0) create_supplier(sp);
|
|
||||||
else if (jobs.builder < source_count * 5 && sp.room.energyAvailable >= 300 && jobs.miner > 0) create_builder(sp);
|
|
||||||
else if (jobs.upgrader < source_count * 5 && sp.room.energyAvailable >= 300 && jobs.miner > 0) create_upgrader(sp);
|
|
||||||
else if (jobs.defender < 0 && sp.room.energyAvailable >= sp.room.energyCapacityAvailable * 0.8 && jobs.miner > 0) create_defender(sp);
|
|
||||||
} else { // Harvesting method: everybody mines own sources
|
|
||||||
if (sp.room.find(FIND_MY_CREEPS).length < source_count * 5 && sp.room.energyAvailable >= 300) {
|
|
||||||
var source_id = sp.memory.sources_ids[sp.memory.creep_iterate % source_count];
|
|
||||||
create_harvester(sp, source_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
function create_harvester(sp, source_id) {
|
|
||||||
var body = [];
|
|
||||||
for (let i = 0; i < Math.floor(sp.room.energyAvailable / 200); i++) {
|
|
||||||
body.push(WORK);
|
|
||||||
body.push(CARRY);
|
|
||||||
body.push(MOVE);
|
|
||||||
}
|
|
||||||
var name = "Harvy" + sp.memory.creep_iterate;
|
|
||||||
sp.spawnCreep(body, name, {
|
|
||||||
memory:
|
|
||||||
{ role: roles.ROLE_HARVESTER, source_id: source_id }
|
|
||||||
});
|
|
||||||
sp.memory.creep_iterate += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
function create_miner(sp, container_id) {
|
|
||||||
var body = [WORK, WORK, WORK, WORK, WORK, MOVE]; // (creep)5*work(2energy/tick)=10energy/tick -> (source)3000energy/300tick=10energy/tick
|
|
||||||
var name = "Mindy" + sp.memory.creep_iterate;
|
|
||||||
sp.spawnCreep(body, name, {
|
|
||||||
memory:
|
|
||||||
{ role: roles.ROLE_MINER, container_id: container_id }
|
|
||||||
});
|
|
||||||
sp.memory.creep_iterate += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
function create_supplier(sp) {
|
|
||||||
var body = [WORK];
|
|
||||||
|
|
||||||
for (let i = 0; i < Math.floor(sp.room.energyAvailable / 100) - 1 && i < 8; i++) { // as big as possible
|
|
||||||
body.push(CARRY);
|
|
||||||
body.push(MOVE);
|
|
||||||
}
|
|
||||||
var name = "Suppry" + sp.memory.creep_iterate;
|
|
||||||
sp.spawnCreep(body, name, {
|
|
||||||
memory:
|
|
||||||
{ role: roles.ROLE_SUPPLIER }
|
|
||||||
});
|
|
||||||
sp.memory.creep_iterate += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
function create_builder(sp) {
|
|
||||||
var body = [];
|
|
||||||
|
|
||||||
for (let i = 0; i < Math.floor(sp.room.energyAvailable / 200); i++) { // as big as possible
|
|
||||||
body.push(CARRY);
|
|
||||||
body.push(MOVE);
|
|
||||||
body.push(WORK);
|
|
||||||
}
|
|
||||||
var name = "Bobby" + sp.memory.creep_iterate;
|
|
||||||
sp.spawnCreep(body, name, {
|
|
||||||
memory:
|
|
||||||
{ role: roles.ROLE_BUILDER }
|
|
||||||
});
|
|
||||||
sp.memory.creep_iterate += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
function create_upgrader(sp) {
|
|
||||||
var body = [];
|
|
||||||
|
|
||||||
for (let i = 0; i < Math.floor(sp.room.energyAvailable / 300); i++) { // as big as possible
|
|
||||||
body.push(CARRY);
|
|
||||||
body.push(MOVE);
|
|
||||||
body.push(WORK);
|
|
||||||
body.push(WORK);
|
|
||||||
}
|
|
||||||
var name = "Undy" + sp.memory.creep_iterate;
|
|
||||||
sp.spawnCreep(body, name, {
|
|
||||||
memory:
|
|
||||||
{ role: roles.ROLE_UPGRADER }
|
|
||||||
});
|
|
||||||
sp.memory.creep_iterate += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
function create_defender(sp) {
|
|
||||||
var body = [];
|
|
||||||
|
|
||||||
for (let i = 0; i < Math.floor(sp.room.energyAvailable / 150); i++) { // as big as possible
|
|
||||||
body.push(TOUGH);
|
|
||||||
body.push(TOUGH);
|
|
||||||
body.push(ATTACK);
|
|
||||||
body.push(MOVE);
|
|
||||||
}
|
|
||||||
var name = "Destroyer CODE" + sp.memory.creep_iterate;
|
|
||||||
sp.spawnCreep(body, name, {
|
|
||||||
memory:
|
|
||||||
{ role: roles.ROLE_DEFENDER }
|
|
||||||
});
|
|
||||||
sp.memory.creep_iterate += 1;
|
|
||||||
}
|
|
||||||
19
system.js
19
system.js
@@ -1,19 +0,0 @@
|
|||||||
/**
|
|
||||||
* Basis of the entire system. Everything will be managed from here on out.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const commands = require("commands");
|
|
||||||
const creeps = require("creeps")
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
// Gets called when recompiling and when Restart command is called.
|
|
||||||
start: function () {
|
|
||||||
commands.configure();
|
|
||||||
creeps.configure();
|
|
||||||
},
|
|
||||||
|
|
||||||
// Gets called every tick.
|
|
||||||
update: function () {
|
|
||||||
creeps.execute();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
36
towers.js
36
towers.js
@@ -1,36 +0,0 @@
|
|||||||
module.exports = {
|
|
||||||
|
|
||||||
run: function(){
|
|
||||||
for(var spawn in Game.spawns){
|
|
||||||
var room = Game.spawns[spawn].room;
|
|
||||||
var towers = room.find(FIND_MY_STRUCTURES,
|
|
||||||
{filter: {structureType: STRUCTURE_TOWER}});
|
|
||||||
|
|
||||||
for(var tower in towers){
|
|
||||||
tower = towers[tower];
|
|
||||||
var enemy = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
|
|
||||||
if(enemy != null) tower.attack(enemy);
|
|
||||||
else {
|
|
||||||
var healt_creep = false;
|
|
||||||
for(var creep in Game.creeps){
|
|
||||||
creep = Game.creeps[creep];
|
|
||||||
if(creep.hits < creep.hitsMax) {
|
|
||||||
tower.heal(creep);
|
|
||||||
healt_creep = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!healt_creep && 500 < tower.energy){
|
|
||||||
var structure = tower.pos.findClosestByPath(FIND_STRUCTURES,
|
|
||||||
{filter: (s)=>(s.structureType != STRUCTURE_RAMPART && s.structureType != STRUCTURE_WALL && s.hits < s.hitsMax)});
|
|
||||||
if(750 < tower.energy && structure == null) {
|
|
||||||
var structures = tower.room.find(FIND_STRUCTURES, {filter: { structureType: STRUCTURE_RAMPART, structureType: STRUCTURE_WALL }});
|
|
||||||
structures.sort(function(a,b){return a.hits - b.hits});
|
|
||||||
structure = structures[0];
|
|
||||||
}
|
|
||||||
tower.repair(structure);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user