Skip to content

Instantly share code, notes, and snippets.

@Olical
Created November 21, 2014 00:03
Show Gist options
  • Save Olical/40b67a60b96c9cd446e6 to your computer and use it in GitHub Desktop.
Save Olical/40b67a60b96c9cd446e6 to your computer and use it in GitHub Desktop.
Screeps hervester swarm - http://screeps.com
var _ = require('lodash');
function countType(type) {
return _.size(_.filter(Game.creeps, function (creep, creepName) {
return creepName.split('_')[0] === type;
}));
}
function build(spawn, type, attrNames) {
var attrs = attrNames.map(function (name) {
return Game[name.toUpperCase()];
});
var id = countType(type) + 1;
spawn.createCreep(attrs, type + '_' + id);
}
module.exports = {
worker: function (spawn) {
build(spawn, 'worker', ['work', 'move', 'carry']);
}
};
function harvest(creep) {
var spawn = creep.pos.findNearest(Game.MY_SPAWNS);
if (creep.energy < creep.energyCapacity) {
var source = creep.pos.findNearest(Game.SOURCES);
if (source) {
creep.moveTo(source);
creep.harvest(source);
}
}
else {
creep.moveTo(spawn);
creep.transferEnergy(spawn);
}
}
module.exports = harvest;
var harvest = require('harvest');
var factory = require('factory');
var _ = require('lodash');
_.mapValues(Game.creeps, harvest);
if (_.size(Game.creeps) < 10 && Game.spawns.Spawn1) {
factory.worker(Game.spawns.Spawn1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment