Basic setup new system.
This commit is contained in:
11
Class.js
Normal file
11
Class.js
Normal file
@@ -0,0 +1,11 @@
|
||||
const creepClass = require("ClassCreep");
|
||||
const roomClass = require("ClassRoom");
|
||||
const spawnClass = require("ClassSpawn");
|
||||
|
||||
module.exports = {
|
||||
setup(){
|
||||
creepClass.setup();
|
||||
roomClass.setup();
|
||||
spawnClass.setup();
|
||||
}
|
||||
}
|
||||
16
ClassCreep.js
Normal file
16
ClassCreep.js
Normal file
@@ -0,0 +1,16 @@
|
||||
module.exports = {
|
||||
setup: function () {
|
||||
Creep.prototype = _Creep.prototype;
|
||||
global.Role = Role;
|
||||
}
|
||||
}
|
||||
|
||||
const Role = {
|
||||
HARVESTER: 0
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
setup: function () { Creep.prototype = _Creep.prototype; }
|
||||
}
|
||||
|
||||
class _Creep extends Creep {}
|
||||
6
ClassRoom.js
Normal file
6
ClassRoom.js
Normal file
@@ -0,0 +1,6 @@
|
||||
module.exports = {
|
||||
setup: function () { Room.prototype = _Room.prototype; }
|
||||
}
|
||||
|
||||
class _Room extends Room {}
|
||||
|
||||
@@ -4,23 +4,6 @@ module.exports = {
|
||||
|
||||
|
||||
class _Spawn extends StructureSpawn {
|
||||
init() {
|
||||
}
|
||||
|
||||
requireNewCreep(role) {
|
||||
const roleCount = this.room.find(FIND_MY_CREEPS, {
|
||||
filter: (c) => { return c.memory.role == role; }
|
||||
}).length;
|
||||
|
||||
switch (role) {
|
||||
case ROLE_HARVESTER:
|
||||
return roleCount < 10;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
createHarvester() {
|
||||
var body = [WORK, CARRY, MOVE];
|
||||
@@ -30,8 +13,9 @@ class _Spawn extends StructureSpawn {
|
||||
createCreep(body, name, role) {
|
||||
if (this.memory.creepCounter == undefined) this.memory.creepCounter = 0;
|
||||
const response = this.spawnCreep(body, name + ": " + this.memory.creepCounter, {
|
||||
memory: { role: role }
|
||||
memory: { job: { role: role } }
|
||||
});
|
||||
if (response == OK) this.memory.creepCounter++;
|
||||
return response;
|
||||
}
|
||||
}
|
||||
177
Creep.js
177
Creep.js
@@ -1,177 +0,0 @@
|
||||
module.exports = {
|
||||
setup: function () {
|
||||
Creep.prototype = _Creep.prototype;
|
||||
global.Role = Role;
|
||||
}
|
||||
}
|
||||
|
||||
const Role = {
|
||||
HARVESTER: 0
|
||||
}
|
||||
|
||||
const State = {
|
||||
|
||||
}
|
||||
|
||||
class _Creep extends Creep {
|
||||
begin() {
|
||||
if (this.memory.role == undefined) this.memory.role = Role.HARVESTER;
|
||||
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;
|
||||
}
|
||||
|
||||
findTarget(TARGET) {
|
||||
if (this.memory.target == undefined) {
|
||||
const target = this.pos.findClosestByPath(TARGET);
|
||||
if (target == null) return ERR_NOT_FOUND;
|
||||
else this.memory.target = target.id;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
findTarget(TARGET, filter) {
|
||||
if (this.memory.target == undefined) {
|
||||
const target = this.pos.findClosestByPath(TARGET, filter);
|
||||
if (target == null) return ERR_NOT_FOUND;
|
||||
else this.memory.target = target.id;
|
||||
}
|
||||
return OK;
|
||||
}
|
||||
|
||||
harvestSource() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
harvesterJobs() {
|
||||
if (this.memory.counter == undefined) this.memory.counter = 0;
|
||||
switch (this.memory.counter % 4) {
|
||||
case 0: this.supplyStructures();
|
||||
break;
|
||||
case 1: this.upgradeRoom();
|
||||
break;
|
||||
case 2: this.repairStructures();
|
||||
break;
|
||||
case 3: this.buildConstructionSites();
|
||||
break;
|
||||
default:
|
||||
this.memory.counter++;
|
||||
break;
|
||||
}
|
||||
if (this.memory.target == undefined) this.memory.counter++;
|
||||
}
|
||||
|
||||
supplyStructures() {
|
||||
if (this.findTarget(FIND_MY_STRUCTURES, {
|
||||
filter: (s) => {
|
||||
return (s.structureType == STRUCTURE_SPAWN ||
|
||||
s.structureType == STRUCTURE_EXTENSION ||
|
||||
s.structureType == STRUCTURE_TOWER ||
|
||||
s.structureType == STRUCTURE_STORAGE)
|
||||
&& 0 < s.store.getFreeCapacity();
|
||||
}
|
||||
}) == ERR_NOT_FOUND) return;
|
||||
const target = Game.getObjectById(this.memory.target);
|
||||
if (this.moveToTarget() == OK) {
|
||||
this.transfer(target, RESOURCE_ENERGY);
|
||||
this.toggleChargeMode();
|
||||
}
|
||||
}
|
||||
|
||||
upgradeRoom() {
|
||||
this.memory.target = this.room.controller.id;
|
||||
if (this.moveToTarget() == OK) {
|
||||
if (this.upgradeController(this.room.controller) == OK) return;
|
||||
this.toggleChargeMode();
|
||||
}
|
||||
}
|
||||
|
||||
repairStructures() {
|
||||
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.id);
|
||||
if (this.moveToTarget() == OK) {
|
||||
this.repair(target);
|
||||
this.toggleChargeMode();
|
||||
}
|
||||
}
|
||||
|
||||
buildConstructionSites() {
|
||||
if (this.findTarget(FIND_MY_CONSTRUCTION_SITES) == ERR_NOT_FOUND) return;
|
||||
const target = Game.getObjectById(this.memory.target);
|
||||
if (this.moveToTarget() == OK) {
|
||||
this.build(target);
|
||||
this.toggleChargeMode();
|
||||
}
|
||||
}
|
||||
|
||||
moveToTarget() {
|
||||
const target = Game.getObjectById(this.memory.target)
|
||||
if (this.pos.inRangeTo(target.pos.x, target.pos.y, 1)) {
|
||||
this.memory.path = undefined;
|
||||
return OK;
|
||||
}
|
||||
if (this.pos.inRangeTo(target.pos.x, target.pos.y, 5)) {
|
||||
this.moveTo(target)
|
||||
return ERR_BUSY;
|
||||
}
|
||||
if (this.memory.path == undefined) {
|
||||
this.memory.path = this.pos.findPathTo(target.pos, {
|
||||
ignoreCreeps: true, range: 1
|
||||
});
|
||||
}
|
||||
const response = this.moveByPath(this.memory.path);
|
||||
if (response == OK) return ERR_BUSY;
|
||||
if (response == ERR_INVALID_ARGS || response == ERR_NOT_FOUND) {
|
||||
this.moveTo(target);
|
||||
this.memory.path = undefined;
|
||||
return ERR_BUSY;
|
||||
}
|
||||
}
|
||||
}
|
||||
26
Job.js
Normal file
26
Job.js
Normal file
@@ -0,0 +1,26 @@
|
||||
const jobHarvester = require("JobHarvester");
|
||||
|
||||
const Role = {
|
||||
HARVESTER: 0
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
setup(){
|
||||
global.Role = Role;
|
||||
},
|
||||
|
||||
begin(creep){
|
||||
if(!creep.memory.job) creep.memory.job = { role: Role.HARVESTER };
|
||||
|
||||
switch (creep.memory.job.role) {
|
||||
case Role.HARVESTER: jobHarvester.tick(creep); break;
|
||||
}
|
||||
},
|
||||
|
||||
tick(creep){
|
||||
switch (creep.memory.job.role) {
|
||||
case Role.HARVESTER: jobHarvester.tick(creep); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
21
JobHarvester.js
Normal file
21
JobHarvester.js
Normal file
@@ -0,0 +1,21 @@
|
||||
module.exports = {
|
||||
begin(creep){
|
||||
const jobMemory = creep.memory.job;
|
||||
if(!jobMemory.harvesting) creep.memory.job.harvesting = false;
|
||||
},
|
||||
|
||||
tick(creep){
|
||||
const jobMemory = creep.memory.job;
|
||||
if(jobMemory.harvesting){
|
||||
const source = creep.pos.findClosestByRange(FIND_SOURCES);
|
||||
if(creep.pos.isNearTo(source)) creep.harvest(source);
|
||||
else creep.moveTo(source);
|
||||
if(!creep.store.getFreeCapacity(RESOURCE_ENERGY)) jobMemory.harvesting = false;
|
||||
} else{
|
||||
const controller = creep.room.controller;
|
||||
if(creep.pos.isNearTo(controller)) creep.upgradeController(controller);
|
||||
else creep.moveTo(controller);
|
||||
if(!creep.store.getUsedCapacity(RESOURCE_ENERGY)) jobMemory.harvesting = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Room.js
28
Room.js
@@ -1,17 +1,17 @@
|
||||
module.exports = {
|
||||
setup: function () { Room.prototype = _Room.prototype; }
|
||||
setup(){},
|
||||
|
||||
begin(room){
|
||||
if(!room.memory.jobs) room.memory.jobs = {
|
||||
harvesters: 1
|
||||
}
|
||||
},
|
||||
|
||||
tick(room){
|
||||
if(!(Game.time % 100)) calculateJobs(room);
|
||||
}
|
||||
}
|
||||
|
||||
class _Room extends Room {
|
||||
begin(){}
|
||||
|
||||
tick(){
|
||||
const creepsCount = this.find(FIND_MY_CREEPS).length;
|
||||
console.log(creepsCount);
|
||||
if(creepsCount < 5){
|
||||
const spawn = this.find(FIND_MY_STRUCTURES, {filter: {structureType: STRUCTURE_SPAWN}})[0];
|
||||
spawn.createHarvester();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function calculateJobs(room) {
|
||||
room.memory.jobs.harvesters = 10 - Object.keys(Game.creeps).length;
|
||||
}
|
||||
17
Structure.js
Normal file
17
Structure.js
Normal file
@@ -0,0 +1,17 @@
|
||||
const StructureSpawn = require("StructureSpawn")
|
||||
|
||||
module.exports = {
|
||||
setup(){},
|
||||
|
||||
begin(structure){
|
||||
switch (structure.structureType) {
|
||||
case STRUCTURE_SPAWN: StructureSpawn.begin(structure); break;
|
||||
}
|
||||
},
|
||||
|
||||
tick(structure){
|
||||
switch (structure.structureType) {
|
||||
case STRUCTURE_SPAWN: StructureSpawn.tick(structure); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
StructureSpawn.js
Normal file
12
StructureSpawn.js
Normal file
@@ -0,0 +1,12 @@
|
||||
module.exports = {
|
||||
setup(){},
|
||||
|
||||
begin(spawn){
|
||||
},
|
||||
|
||||
tick(spawn){
|
||||
if(spawn.room.memory.jobs.harvesters) {
|
||||
if(spawn.createHarvester());
|
||||
}
|
||||
}
|
||||
}
|
||||
29
main.js
29
main.js
@@ -3,25 +3,40 @@
|
||||
// ########################################
|
||||
|
||||
|
||||
const system = require("System");
|
||||
|
||||
const Class = require("Class");
|
||||
const Job = require("Job");
|
||||
const Room = require("Room");
|
||||
const Structure = require("Structure");
|
||||
|
||||
module.exports.loop = function () {
|
||||
if(!global.compiled) onRecompile();
|
||||
else if (!global.started) onRestart();
|
||||
else onTick();
|
||||
}
|
||||
|
||||
function onRecompile(){
|
||||
system.setup();
|
||||
function onRecompile(){
|
||||
Class.setup();
|
||||
Job.setup();
|
||||
console.log("Script recompiled...");
|
||||
global.compiled = true;
|
||||
}
|
||||
|
||||
function onRestart(){
|
||||
system.begin();
|
||||
Object.values(Game.rooms).forEach(room => Room.begin(room));
|
||||
Object.values(Game.spawns).forEach(spawn => Structure.begin(spawn));
|
||||
Object.values(Game.creeps).forEach(creep => Job.begin(creep));
|
||||
global.started = true;
|
||||
}
|
||||
|
||||
function onTick(){
|
||||
system.tick();
|
||||
}
|
||||
Object.values(Game.rooms).forEach(room => Room.tick(room));
|
||||
Object.values(Game.spawns).forEach(spawn => Structure.tick(spawn));
|
||||
Object.values(Game.creeps).forEach(creep => Job.tick(creep));
|
||||
if(!(Game.time % 100)) cleanUp();
|
||||
}
|
||||
|
||||
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; });
|
||||
}
|
||||
|
||||
|
||||
26
system.js
26
system.js
@@ -1,26 +0,0 @@
|
||||
const creepClass = require("Creep");
|
||||
const roomClass = require("Room");
|
||||
const spawnClass = require("Spawn");
|
||||
|
||||
// ########################################
|
||||
// ########## SYSTEM ##########
|
||||
// ########################################
|
||||
|
||||
module.exports = {
|
||||
|
||||
setup: function () {
|
||||
creepClass.setup();
|
||||
roomClass.setup();
|
||||
spawnClass.setup();
|
||||
},
|
||||
|
||||
begin: function () {
|
||||
Object.values(Game.rooms).forEach(room => room.begin());
|
||||
Object.values(Game.creeps).forEach(creep => creep.begin());
|
||||
},
|
||||
|
||||
tick: function () {
|
||||
Object.values(Game.rooms).forEach(room => room.tick());
|
||||
Object.values(Game.creeps).forEach(creep => creep.tick());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user