-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspawner.js
131 lines (121 loc) · 5.09 KB
/
spawner.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*jshint esversion: 6 */
/*jshint loopfunc: true */
var utilities = require('utilities');
/*
* Module code goes here. Use 'module.exports' to export things:
* module.exports.thing = 'a thing';
*
* You can import it from another modules like this:
* var mod = require('util.spawner');
* mod.thing == 'a thing'; // true
*/
/*
* Harvester Configuration
* numHarvesters is the number of harvester creeps that should exist at all times
* harvesterBody is the body configuration that will be used for spawning any new harvesters
*/
var numHarvesters = 2;
var harvesterConfig = {
work: 2,
carry: 1,
move: 1
};
/*
* Builder Configuration
* numBuilders is the number of builder creeps that should exist at all times
* builderBody is the body configuration that will be used for spawning any new builders
*/
var numBuilders = 2;
var builderConfig = {
work: 2,
carry: 1,
move: 1
};
/*
* Upgrader Configuration
* numUpgraders is the number of upgrader creeps that should exist at all times
* upgraderBody is the body configuration that will be used for spawning any new upgraders
*/
var numUpgraders = 2;
var upgraderConfig = {
work: 2,
carry: 1,
move: 1
};
/*
* Supplier Configuration
* numSuppliers is the number of supplier creeps that should exist at all times
* supplierBody is the body configuration that will be used for spawning any new suppliers
*/
var numSuppliers = 0;
var supplierBody = {
work: 2,
carry: 1,
move: 1
};
var spawner = {
run: function() {
for (var r in Game.rooms) {
var room = Game.rooms[r];
var spawners = room.find(FIND_STRUCTURES, {
filter: (structure) => {
return structure.structureType == STRUCTURE_SPAWN;
}
});
if(spawners.length > 0) {
var spawner = spawners[0];
var builders = _.filter(Game.creeps, (creep) => creep.memory.role == 'builder' && creep.room == room);
var harvesters = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester' && creep.room == room);
var upgraders = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgrader' && creep.room == room);
var suppliers = _.filter(Game.creeps, (creep) => creep.memory.role == 'supplier' && creep.room == room);
console.log('Creep Stats: ' +
'Builders=' + builders.length + '/' + numBuilders + ', ' +
'Harvesters=' + harvesters.length + '/' + numHarvesters + ', ' +
'Upgraders='+upgraders.length + '/' + numUpgraders + ', ' +
'Suppliers='+suppliers.length + '/' + numSuppliers
);
// console.log({'Builders': builders.length, 'Harvesters': harvesters.length, 'Upgraders': upgraders.length, 'Suppliers': suppliers.length});
// console.log(utilities.buildBody(harvesterConfig));
var newName = "";
var newType = "";
var newCost = 0;
var newConfig = [];
if(harvesters.length < numHarvesters) {
newType = "harvester";
newCost = utilities.getCost(harvesterConfig);
newConfig = utilities.buildBody(harvesterConfig);
} else if(builders.length < numBuilders) {
newType = "builder";
newCost = utilities.getCost(builderConfig);
newConfig = utilities.buildBody(builderConfig);
} else if(upgraders.length < numUpgraders) {
newType = "upgrader";
newCost = utilities.getCost(upgraderConfig);
newConfig = utilities.buildBody(upgraderConfig);
}
if (newType !== "") {
newName = spawner.createCreep(newConfig, undefined, {role: newType});
}
if (_.isString(newName) && newName !== "") {
console.log('Spawning new ' + newType + ': ' + newName);
} else {
if (newName == -1) {
console.log('SPAWN ERROR: Not owner of spawn');
} else if (newName == -3) {
console.log('SPAWN ERROR: Creep with name already exists');
} else if (newName == -4) {
console.log('SPAWN ERROR: Spawn is already busy creating a creep');
} else if (newName == -6) {
console.log('SPAWN ERROR: Not enough energy to create ' + newType + ', body requires '+newCost+' you have '+room.energyAvailable+'/'+room.energyCapacityAvailable);
// console.log(newConfig);
} else if (newName == -10) {
console.log('SPAWN ERROR: Body is not properly described');
} else if (newName == -14) {
console.log('SPAWN ERROR: Room Controller level is not enough to use this spawn');
}
}
}
}
}
};
module.exports = spawner;