-
Notifications
You must be signed in to change notification settings - Fork 1
/
spawn.js
66 lines (61 loc) · 2.54 KB
/
spawn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const defaultBody = [WORK,CARRY,MOVE];
module.exports = {
genBody: function(spawn, forceSmall){
if(forceSmall){
return defaultBody;
}else if(spawn.room.energyCapacityAvailable>=800){
return [WORK,WORK,WORK,WORK,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE];
}else if(spawn.room.energyCapacityAvailable>=500){
return [WORK,WORK,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE];
}else if(spawn.room.energyCapacityAvailable>=300){
return [WORK,CARRY,CARRY,MOVE,MOVE];
}else{
return defaultBody;
}
},
spawn(spawn, creepRole){
let errCode = spawn.spawnCreep(this.genBody(spawn,Memory.forceSmall), creepRole + Game.time,{memory: {role: creepRole}});
if(errCode==OK){
if(Memory.forceSmall){
delete Memory.forceSmall;
console.log('Spawning tiny'+creepRole+' at '+spawn.name);
}else{
console.log('Spawning '+creepRole+' at '+spawn.name);
}
}else if(errCode==ERR_NOT_ENOUGH_ENERGY){
if(!Memory.spawnTries) Memory.spawnTries = 0;
Memory.spawnTries += 1;
if(Memory.spawnTries > 30){
console.log('Failed to spawn '+creepRole+' for '+Memory.spawnTries+' ticks.');
Memory.forceSmall = true
delete Memory.spawnTries;
}
}
},
run: function(spawn) {
var harvesters = _.filter(Game.creeps, (creep)=>creep.memory.role=='harvester');
var builders = _.filter(Game.creeps, (creep)=>creep.memory.role=='builder');
var upgraders = _.filter(Game.creeps, (creep)=>creep.memory.role=='upgrader');
var repairers = _.filter(Game.creeps, (creep)=>creep.memory.role=='repairer');
var scouts = _.filter(Game.creeps, (creep)=>creep.memory.role=='scout');
if(harvesters.length<3){
this.spawn(spawn,"harvester");
}else if(upgraders.length<2){
this.spawn(spawn,"upgrader");
}else if(builders.length<3){
this.spawn(spawn,"builder");
}else if(repairers.length<2){
this.spawn(spawn,"repairer");
}else if(scouts.length<1){
this.spawn(spawn,"scout");
}
if(spawn.spawning) {
var spawningCreep = Game.creeps[spawn.spawning.name];
spawn.room.visual.text(
'🛠️' + spawningCreep.memory.role,
spawn.pos.x + 1,
spawn.pos.y,
{align: 'left', opacity: 0.8});
}
}
};