Compare commits
No commits in common. "bd6631781d963491e19294c9fb7c02a5d927ffeb" and "e6d90d43a4d8f3465daec27e0561d6836df57b5f" have entirely different histories.
bd6631781d
...
e6d90d43a4
25
role_base.js
25
role_base.js
|
@ -1,14 +1,13 @@
|
|||
var body = [ MOVE, WORK, CARRY ];
|
||||
var name = 'Creep';
|
||||
class roleBase{
|
||||
static body = [ MOVE, WORK, CARRY ];
|
||||
static name = 'Creep';
|
||||
/** @param {Creep} creep **/
|
||||
run(creep) { }
|
||||
static spawn(spawnPoint){
|
||||
spawnPoint.spawnCreep(this.body, this.name + Game.time,
|
||||
{memory : {role : this.name}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
function run(creep) { }
|
||||
|
||||
function spawn(spawnPoint, newBody, newName){
|
||||
if(typeof newBody === 'undefined') newBody = body;
|
||||
if(typeof newName === 'undefined') newName = name;
|
||||
spawnPoint.spawnCreep(newBody, newName + Game.time,
|
||||
{memory : {role : newName}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {body,name,run,spawn};
|
||||
module.exports = roleBase;
|
||||
|
|
|
@ -1,26 +1,30 @@
|
|||
var energyUtils = require('util_energy');
|
||||
var roleBase = require('role_base');
|
||||
|
||||
var name = 'builder';
|
||||
class roleBuilder extends roleBase{
|
||||
/** @param {Creep} creep **/
|
||||
static name = 'builder';
|
||||
|
||||
function run(creep) {
|
||||
if(creep.memory.building && creep.store[RESOURCE_ENERGY] == 0) {
|
||||
creep.memory.building = false;
|
||||
}
|
||||
if(!creep.memory.building && creep.store.getFreeCapacity() == 0) {
|
||||
creep.memory.building = true;
|
||||
}
|
||||
static run(creep) {
|
||||
if(creep.memory.building && creep.store[RESOURCE_ENERGY] == 0) {
|
||||
creep.memory.building = false;
|
||||
}
|
||||
if(!creep.memory.building && creep.store.getFreeCapacity() == 0) {
|
||||
creep.memory.building = true;
|
||||
}
|
||||
|
||||
if(creep.memory.building) {
|
||||
var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
|
||||
if(targets.length) {
|
||||
if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
|
||||
creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
energyUtils.gatherEnergy(creep);
|
||||
}
|
||||
}
|
||||
if(creep.memory.building) {
|
||||
var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
|
||||
if(targets.length) {
|
||||
if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
|
||||
creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
energyUtils.gatherEnergy(creep);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {name,run};
|
||||
module.exports = roleBuilder;
|
||||
|
|
|
@ -3,7 +3,6 @@ var roleUpgrader = require('role_upgrader');
|
|||
var roleBuilder = require('role_builder');
|
||||
var roleRepairer = require('role_repairer');
|
||||
var roleGuard = require('role_guard');
|
||||
var roleBase = require('role_base');
|
||||
|
||||
const roleMap = {
|
||||
harvester: roleHarvester,
|
||||
|
@ -24,7 +23,7 @@ var creepCounts = {
|
|||
function runRole(creep){
|
||||
const role = roleMap[creep.memory.role];
|
||||
|
||||
if(typeof role.run === 'function'){
|
||||
if(role !== 'undefined'){
|
||||
role.run(creep);
|
||||
} else {
|
||||
console.log('Unknown or undefined role: ' + creep.memory.role);
|
||||
|
@ -36,10 +35,7 @@ function spawnCreeps(spawn){
|
|||
var roleCreeps = _.filter(Game.creeps, (creep) => creep.memory.role == role);
|
||||
|
||||
if(roleCreeps.length < count && !spawn.spawning) {
|
||||
if(typeof roleMap[role].spawn === 'function')
|
||||
roleMap[role].spawn(spawn);
|
||||
else
|
||||
roleBase.spawn(spawn, roleMap[role].body, roleMap[role].name);
|
||||
roleMap[role].spawn(spawn);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
var body = [ MOVE, MOVE, RANGED_ATTACK, RANGED_ATTACK ];
|
||||
var name = 'streltsy'
|
||||
var roleBase = require('role_base');
|
||||
|
||||
function run(creep) {
|
||||
class roleGuard extends roleBase{
|
||||
static body = [ MOVE, MOVE, RANGED_ATTACK, RANGED_ATTACK ];
|
||||
static name = 'streltsy'
|
||||
/** @param {Creep} creep **/
|
||||
static run(creep) {
|
||||
const target = creep.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
|
||||
if(target) {
|
||||
if(creep.rangedAttack(target) == ERR_NOT_IN_RANGE) {
|
||||
|
@ -21,14 +24,14 @@ function run(creep) {
|
|||
}
|
||||
}
|
||||
}
|
||||
static spawn(spawnPoint, newWaypoints){
|
||||
if(typeof newWaypoints === 'undefined')
|
||||
newWaypoints = ['A','B'];
|
||||
|
||||
function spawn(spawnPoint, newWaypoints){
|
||||
if(typeof newWaypoints === 'undefined')
|
||||
newWaypoints = ['A','B'];
|
||||
spawnPoint.spawnCreep(this.body, this.name + Game.time,
|
||||
{memory : {role : this.name, waypoints : newWaypoints, currentWaypoint : 0}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
spawnPoint.spawnCreep(this.body, this.name + Game.time,
|
||||
{memory : {role : this.name, waypoints : newWaypoints, currentWaypoint : 0}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = {body,name,run,spawn};
|
||||
module.exports = roleGuard;
|
||||
|
|
|
@ -1,19 +1,24 @@
|
|||
var energyUtils = require('util_energy');
|
||||
var roleBase = require('role_base');
|
||||
|
||||
var name = 'harvester';
|
||||
class roleHarvester extends roleBase{
|
||||
/** @param {Creep} creep **/
|
||||
|
||||
function run(creep) {
|
||||
if(creep.store.getFreeCapacity() > 0) {
|
||||
var sources = creep.room.find(FIND_SOURCES);
|
||||
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
|
||||
creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(!energyUtils.depositEnergy(creep)){
|
||||
creep.moveTo(Game.flags.IdleArea);
|
||||
}
|
||||
}
|
||||
}
|
||||
static name = 'harvester';
|
||||
|
||||
module.exports = {name,run};
|
||||
static run(creep) {
|
||||
if(creep.store.getFreeCapacity() > 0) {
|
||||
var sources = creep.room.find(FIND_SOURCES);
|
||||
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
|
||||
creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});
|
||||
}
|
||||
}
|
||||
else {
|
||||
if(!energyUtils.depositEnergy(creep)){
|
||||
creep.moveTo(Game.flags.IdleArea);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = roleHarvester;
|
||||
|
|
|
@ -1,33 +1,36 @@
|
|||
var energyUtils = require('util_energy');
|
||||
var roleBase = require('role_base');
|
||||
|
||||
var name = 'repairer';
|
||||
class roleRepairer extends roleBase {
|
||||
static name = 'repairer';
|
||||
/** @param {Creep} creep **/
|
||||
static run(creep) {
|
||||
if(creep.memory.building && creep.store[RESOURCE_ENERGY] == 0) {
|
||||
creep.memory.building = false;
|
||||
}
|
||||
if(!creep.memory.building && creep.store.getFreeCapacity() == 0) {
|
||||
creep.memory.building = true;
|
||||
}
|
||||
|
||||
function run(creep) {
|
||||
if(creep.memory.building && creep.store[RESOURCE_ENERGY] == 0) {
|
||||
creep.memory.building = false;
|
||||
}
|
||||
if(!creep.memory.building && creep.store.getFreeCapacity() == 0) {
|
||||
creep.memory.building = true;
|
||||
}
|
||||
if(creep.memory.building) {
|
||||
var targets = creep.room.find(FIND_STRUCTURES, {
|
||||
filter: (structure) => {
|
||||
return structure.hitsMax - structure.hits > 0;
|
||||
}
|
||||
});
|
||||
if(targets.length > 0) {
|
||||
if(creep.repair(targets[0]) == ERR_NOT_IN_RANGE) {
|
||||
creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
|
||||
}
|
||||
}
|
||||
else{
|
||||
creep.say("IDLE")
|
||||
}
|
||||
}
|
||||
else {
|
||||
energyUtils.gatherEnergy(creep);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if(creep.memory.building) {
|
||||
var targets = creep.room.find(FIND_STRUCTURES, {
|
||||
filter: (structure) => {
|
||||
return structure.hitsMax - structure.hits > 0;
|
||||
}
|
||||
});
|
||||
if(targets.length > 0) {
|
||||
if(creep.repair(targets[0]) == ERR_NOT_IN_RANGE) {
|
||||
creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
|
||||
}
|
||||
}
|
||||
else{
|
||||
creep.say("IDLE")
|
||||
}
|
||||
}
|
||||
else {
|
||||
energyUtils.gatherEnergy(creep);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {name,run};
|
||||
module.exports = roleRepairer;
|
||||
|
|
|
@ -1,26 +1,30 @@
|
|||
var name = 'upgrader';
|
||||
var roleBase = require('role_base');
|
||||
|
||||
/** @param {Creep} creep **/
|
||||
function run(creep) {
|
||||
class roleUpgrader extends roleBase {
|
||||
static name = 'upgrader';
|
||||
|
||||
if(creep.memory.upgrading && creep.store[RESOURCE_ENERGY] == 0) {
|
||||
creep.memory.upgrading = false;
|
||||
}
|
||||
if(!creep.memory.upgrading && creep.store.getFreeCapacity() == 0) {
|
||||
creep.memory.upgrading = true;
|
||||
}
|
||||
/** @param {Creep} creep **/
|
||||
static run(creep) {
|
||||
|
||||
if(creep.memory.upgrading) {
|
||||
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
|
||||
creep.moveTo(creep.room.controller, {visualizePathStyle: {stroke: '#ffffff'}});
|
||||
}
|
||||
}
|
||||
else {
|
||||
var sources = creep.room.find(FIND_SOURCES);
|
||||
if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) {
|
||||
creep.moveTo(sources[1], {visualizePathStyle: {stroke: '#ffaa00'}});
|
||||
}
|
||||
}
|
||||
}
|
||||
if(creep.memory.upgrading && creep.store[RESOURCE_ENERGY] == 0) {
|
||||
creep.memory.upgrading = false;
|
||||
}
|
||||
if(!creep.memory.upgrading && creep.store.getFreeCapacity() == 0) {
|
||||
creep.memory.upgrading = true;
|
||||
}
|
||||
|
||||
module.exports = {name,run};
|
||||
if(creep.memory.upgrading) {
|
||||
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
|
||||
creep.moveTo(creep.room.controller, {visualizePathStyle: {stroke: '#ffffff'}});
|
||||
}
|
||||
}
|
||||
else {
|
||||
var sources = creep.room.find(FIND_SOURCES);
|
||||
if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) {
|
||||
creep.moveTo(sources[1], {visualizePathStyle: {stroke: '#ffaa00'}});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = roleUpgrader;
|
||||
|
|
Loading…
Reference in a new issue