Compare commits

...

7 commits

13 changed files with 302 additions and 61 deletions

View file

@ -1,4 +1,5 @@
var roleDispatcher = require('role_dispatcher');
var utilProgress = require('util_progress');
module.exports.loop = function () {
@ -9,6 +10,8 @@ module.exports.loop = function () {
}
roleDispatcher.spawnCreeps(Game.spawns['Moscow']);
utilProgress.checkProgress();
if(Game.spawns['Moscow'].spawning) {

View file

@ -1,14 +1,14 @@
var body = [ MOVE, WORK, CARRY ];
var name = 'Creep';
function run(creep) { }
function spawn(spawnPoint, newBody, newName){
if(typeof newBody === 'undefined') newBody = body;
function spawn(spawnPoint, newName){
var newBody = spawnPoint.room.memory.creepBodies[newName];
if(typeof newName === 'undefined') newName = name;
spawnPoint.spawnCreep(newBody, newName + Game.time,
spawnPoint.spawnCreep(newBody, newName + spawnPoint.room.name + "-" + Game.time,
{memory : {role : newName}
});
}
module.exports = {body,name,run,spawn};
module.exports = {name,run,spawn};

View file

@ -11,10 +11,10 @@ function run(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'}});
var target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
if(target != null) {
if(creep.build(target) == ERR_NOT_IN_RANGE) {
creep.moveTo(target);
}
}
}

View file

@ -4,22 +4,99 @@ var roleBuilder = require('role_builder');
var roleRepairer = require('role_repairer');
var roleGuard = require('role_guard');
var roleBase = require('role_base');
var roleEqualizer = require('role_equalizer');
var utils = require('util_etc');
const roleMap = {
harvester: roleHarvester,
upgrader: roleUpgrader,
builder: roleBuilder,
repairer: roleRepairer,
streltsy: roleGuard
streltsy: roleGuard,
desatilnik: roleEqualizer
};
var creepCounts = {
"harvester" : 3,
"builder" : 1,
"upgrader" : 2,
"repairer" : 1,
"streltsy" : 2
};
// calculates how much space there is for creeps next to
// energy sources, in order to have the perfect amount
function calculateTotalSourceSpace(room){
var sources = room.find(FIND_SOURCES);
var count = 0;
for(const source of sources){
count += utils.calculateSourceSpace(source);
}
return count;
}
function calculateCreepCounts(room){
var creepCounts = {
"harvester" : 3,
"builder" : 1,
"upgrader" : 1,
"repairer" : 1,
"streltsy" : 0,
"desatilnik" : 0
};
// 2 harvesters per source by default
creepCounts.harvester = calculateTotalSourceSpace(room);
if(room.memory.containers == 0){
// -1 harvester until at least a container built in order
// to reduce congestion
creepCounts.harvester -= 1;
}
// streltsy cost 400 energy, and thus cant be built
// you have at least 2 extensions (300+50+50)
// i guess there wouldnt be a big difference since
// they wouldnt spawn anyways, but whatever
if(room.memory.extensions >= 2){
creepCounts.streltsy += 2;
}
if(room.memory.extensions >= 3){
creepCounts.builder += 1;
creepCounts.upgrader += 1;
}
if(room.memory.containers >= 2){
creepCounts.desatilnik += 1;
}
room.memory.creepCounts = creepCounts;
}
function calculateCreepBodies(room){
var creepBodies = {
// 200 base cost
"harvester" : [WORK, CARRY, MOVE],
"builder" : [WORK, CARRY, MOVE],
"upgrader" : [WORK, CARRY, MOVE],
"repairer" : [WORK, CARRY, MOVE],
// 400 base cost
"streltsy" : [MOVE, MOVE, RANGED_ATTACK, RANGED_ATTACK],
"desatilnik" : [CARRY, CARRY, CARRY, MOVE, MOVE, MOVE]
};
// 400 cost
if(room.memory.extensions >= 2){
creepBodies.upgrader = [MOVE, MOVE, WORK, WORK, CARRY, CARRY];
creepBodes.builder = [MOVE, MOVE, WORK, WORK, CARRY, CARRY];
}
// 450 cost
if(room.memory.extensions >= 3){
creepBodies.harvester = [MOVE, MOVE, WORK, WORK, WORK, CARRY];
}
room.memory.creepBodies = creepBodies;
}
function runRole(creep){
const role = roleMap[creep.memory.role];
@ -32,17 +109,19 @@ function runRole(creep){
}
function spawnCreeps(spawn){
for(const [role, count] of Object.entries(creepCounts)){
calculateCreepCounts(spawn.room);
for(const [role, count] of Object.entries(spawn.room.memory.creepCounts)){
var roleCreeps = _.filter(Game.creeps, (creep) => creep.memory.role == role);
if(roleCreeps.length < count && !spawn.spawning) {
calculateCreepBodies(spawn.room);
if(typeof roleMap[role].spawn === 'function')
roleMap[role].spawn(spawn);
else
roleBase.spawn(spawn, roleMap[role].body, roleMap[role].name);
roleBase.spawn(spawn, roleMap[role].name);
break;
}
}
}
module.exports = { runRole, spawnCreeps, creepCounts};
module.exports = { runRole, spawnCreeps};

46
role_equalizer.js Normal file
View file

@ -0,0 +1,46 @@
var energyUtils = require('util_energy');
var _ = require('lodash');
var name = 'desatilnik';
function run(creep) {
if(creep.room.memory.containers == 0) return;
if(creep.store.getFreeCapacity() > 0 ){
var containers = creep.room.find(FIND_STRUCTURES, {
filter : { structureType: STRUCTURE_CONTAINER}
});
var maxContainer = containers[0];
for(const container of containers){
if(container.store[RESOURCE_ENERGY] > maxContainer.store[RESOURCE_ENERGY]){
maxContainer = container;
}
};
if(creep.withdraw(maxContainer, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(maxContainer);
}
}
else{
var containers = creep.room.find(FIND_STRUCTURES, {
filter : { structureType: STRUCTURE_CONTAINER}
});
var minContainer = containers[0];
for(const container of containers){
if(container.store[RESOURCE_ENERGY] < minContainer.store[RESOURCE_ENERGY]){
minContainer = container;
}
};
if(creep.transfer(minContainer, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(minContainer);
}
}
}
module.exports = {name,run};

View file

@ -1,4 +1,3 @@
var body = [ MOVE, MOVE, RANGED_ATTACK, RANGED_ATTACK ];
var name = 'streltsy'
function run(creep) {
@ -23,12 +22,13 @@ function run(creep) {
}
function spawn(spawnPoint, newWaypoints){
var newBody = spawnPoint.room.memory.creepBodies[name];
if(typeof newWaypoints === 'undefined')
newWaypoints = ['A','B'];
spawnPoint.spawnCreep(this.body, this.name + Game.time,
spawnPoint.spawnCreep(newBody, this.name + spawnPoint.room.name + "-" + Game.time,
{memory : {role : this.name, waypoints : newWaypoints, currentWaypoint : 0}
});
}
module.exports = {body,name,run,spawn};
module.exports = {name,run,spawn};

View file

@ -3,10 +3,19 @@ var energyUtils = require('util_energy');
var name = 'harvester';
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'}});
if(!creep.memory.harvesting && creep.store[RESOURCE_ENERGY] == 0){
creep.memory.harvesting = true;
}
if(creep.memory.harvesting && creep.store.getFreeCapacity() == 0){
creep.memory.harvesting = false;
}
if(creep.memory.harvesting) {
if(typeof creep.memory.assignedSource === 'undefined') energyUtils.sourceDelegate(creep);
var source = Game.getObjectById(creep.memory.assignedSource);
if(creep.harvest(source) == ERR_NOT_IN_RANGE) {
creep.moveTo(source);
}
}
else {

View file

@ -11,14 +11,14 @@ function run(creep) {
}
if(creep.memory.building) {
var targets = creep.room.find(FIND_STRUCTURES, {
var target = creep.pos.findClosestByRange(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'}});
if(target != null) {
if(creep.repair(target) == ERR_NOT_IN_RANGE) {
creep.moveTo(target, {visualizePathStyle: {stroke: '#ffffff'}});
}
}
else{

View file

@ -1,3 +1,4 @@
var energyUtils = require('util_energy');
var name = 'upgrader';
/** @param {Creep} creep **/
@ -16,10 +17,7 @@ function run(creep) {
}
}
else {
var sources = creep.room.find(FIND_SOURCES);
if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[1], {visualizePathStyle: {stroke: '#ffaa00'}});
}
energyUtils.gatherEnergy(creep);
}
}

18
util_autobuild.js Normal file
View file

@ -0,0 +1,18 @@
function determineNextBlueprint(room){
return STRUCTURE_EXTENSION;
}
function addBlueprint(room){
let foundPosition = false;
let spawn = room.find(FIND_MY_SPAWNS);
let position = spawn.pos;
let spiralOffset = 1;
while(!foundPosition){
position.y -= spiralOffset++;
position.x -= spiralOffset++;
position.y += spiralOffset++;
position.x += spiralOffset++;
}
}
module.exports = { addBlueprint }

View file

@ -1,48 +1,105 @@
var utils = require('util_etc');
var energyUtils = {
// get energy from a container (if available) otherwise just mine for it at
// an energy source
gatherEnergy : function(creep) {
var storage = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER) &&
structure.store.getUsedCapacity(RESOURCE_ENERGY) > 0;
}
});
/**
if(storage.length == 0){
storage = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_SPAWN) &&
structure.store.getUsedCapacity(RESOURCE_ENERGY) > 0;
}
});
}
**/
if(creep.withdraw(storage[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(storage[0], {visualizePathStyle: {stroke: '#ffaa00'}});
let storage;
if(creep.room.memory.containers > 0){
storage = creep.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER) &&
structure.store.getUsedCapacity(RESOURCE_ENERGY) > 0;
}
});
}
else{
if(typeof creep.memory.assignedSource === 'undefined') energyUtils.sourceDelegate(creep);
let source = Game.getObjectById(creep.memory.assignedSource);
if(creep.harvest(source) == ERR_NOT_IN_RANGE) {
creep.moveTo(source);
}
return;
}
if(storage == null) return;
if(creep.withdraw(storage, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(storage);
}
},
//assign a creep to source in the room the creep is in
sourceDelegate : function(creep){
const room = creep.room;
const sources = creep.room.find(FIND_SOURCES);
let minSource = sources[0].id;
let minSourceCount = 0;
if(typeof room.memory[sources[0].id] !== 'undefined')
minSourceCount = room.memory[sources[0].id].length;
for(const source of sources){
if(typeof room.memory[source.id] === 'undefined')
room.memory[source.id] = [];
room.memory[source.id].forEach(function(assignedCreep, index){
if(!Game.creeps[assignedCreep]){
room.memory[source.id].splice(index, 1);
}
});
if(room.memory[source.id].length < minSourceCount
&& room.memory[source.id].length < utils.calculateSourceSpace(source)){
minSource = source.id;
minSourceCount = room.memory[source.id].length;
}
}
room.memory[minSource].push(creep.name);
creep.memory.assignedSource = minSource;
},
// put energy into storage, first if possible into a spawner / extension
// otherwise put it into a container
depositEnergy : function(creep) {
var targets = creep.room.find(FIND_STRUCTURES, {
var target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_SPAWN ||
structure.structureType == STRUCTURE_EXTENSION) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
}
});
if(targets.length == 0){
targets = creep.room.find(FIND_STRUCTURES, {
if(target == null){
target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER ||
structure.structureType == STRUCTURE_TOWER) &&
return (structure.structureType == STRUCTURE_CONTAINER) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
}
});
}
if(targets.length > 0) {
if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
if(target != null) {
if(creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(target, {visualizePathStyle: {stroke: '#ffffff'}});
}
return true;
}
// if no storage anywhere, then just build something
// in order to speed up early game, since no storage
if(creep.room.memory.containers == 0){
target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
if(target != null){
if(creep.build(target) == ERR_NOT_IN_RANGE){
creep.moveTo(target);
}
return true;
}
}
return false;
}
}

16
util_etc.js Normal file
View file

@ -0,0 +1,16 @@
function calculateSourceSpace(source){
var count = 0;
for(let y = -1; y <= 1; y++){
for(let x = -1; x <= 1; x++){
if(x == 0 && y == 0) continue;
for(var object of source.room.lookAt(source.pos.x + x, source.pos.y + y)){
if(object.type != 'terrain') continue;
if(object.terrain != 'wall') count++;
}
}
}
return count;
}
module.exports = { calculateSourceSpace };

View file

@ -1,5 +1,20 @@
var progressUtils = {
checkProgress : function() {
for(var roomName in Game.rooms){
const room = Game.rooms[roomName];
const structures = room.find(FIND_STRUCTURES);
room.memory.spawns = 0;
room.memory.containers = 0;
room.memory.extensions = 0;
for(const structure of structures){
if(structure.structureType == STRUCTURE_CONTAINER) room.memory.containers++;
if(structure.structureType == STRUCTURE_SPAWN) room.memory.spawns++;
if(structure.structureType == STRUCTURE_EXTENSION) room.memory.extensions++;
};
}
}
}