Skip to content

Instantly share code, notes, and snippets.

@JJWTimmer
Last active August 29, 2015 14:25
Show Gist options
  • Save JJWTimmer/64cc4dad90bdc3061ad0 to your computer and use it in GitHub Desktop.
Save JJWTimmer/64cc4dad90bdc3061ad0 to your computer and use it in GitHub Desktop.
module.exports = function () {
const roles = [
{
name: 'harvester',
population: 8,
prio: 100,
body: [WORK, CARRY, CARRY, MOVE, MOVE]
},
{
name: 'builder',
population: 5,
prio: 75,
body: [WORK, CARRY, MOVE, MOVE]
},
{
name: 'guard',
population: 5,
prio: 50,
body: [ATTACK, TOUGH, MOVE, MOVE]
},
{
name: 'booster',
population: 5,
prio: 25,
body: [WORK, CARRY, MOVE, MOVE]
}
];
var spawn = Game.spawns['JT-HQ'];
var PriorityQueue = require('priorityQueue');
var creepsQueue = new PriorityQueue(function (a, b) {
return a.prio - b.prio;
});
function populateQueue(role) {
var findMissingIds = function (creeps) {
var names = creeps.map(creep => creep.name);
var ids = names.map(name => parseInt(name.replace(role.name, '')));
var missing = [];
for (var i = 0; i < role.population; i++) {
if (!_.includes(ids, i)) {
missing.push(i);
}
}
return missing;
};
var creeps = _.filter(Game.creeps, creep => creep.memory.role === role.name);
if (creeps.length < role.population) {
var missingIds = findMissingIds(creeps, role.name, role.population);
for (var id of missingIds) {
const creepName = `${role.name}${id}`;
creepsQueue.enq({
body: role.body,
name: creepName,
memory: {id: id, role: role.name},
prio: role.prio
});
}
}
}
_.forEach(roles, role => populateQueue(role));
if (!creepsQueue.isEmpty()) {
var creepSpec = creepsQueue.deq();
var result = spawn.createCreep(creepSpec.body, creepSpec.name, creepSpec.memory);
if (result !== creepSpec.name) {
console.log('Spawn error:', result, creepSpec.name);
}
}
};
@utiuti
Copy link

utiuti commented Jul 25, 2015

var creeprole = {
role: 'guard',
quelle: aspawn.id,
ziel: null,
modus: null
};
var creepbody = [TOUGH, TOUGH, TOUGH, TOUGH, ATTACK, MOVE, ATTACK, MOVE, ATTACK, MOVE];
var j = creepbody.length;
for (var i = 0; i < j - 3; i++) {
var result = aspawn.createCreep(creepbody, undefined, creeprole);
if (_.isString(result)) {
return true;
} else {
creepbody.shift();
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment