-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprototype.spawn.js
112 lines (91 loc) · 4.24 KB
/
prototype.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
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
module.exports = function() {
StructureSpawn.prototype.tryToSpawnCreep = function() {
let energy = this.room.energyAvailable;
energy = (energy <= 1200)?energy:1200;
let creepsInRoom = this.room.find(FIND_MY_CREEPS);
let harvesters = _.filter(creepsInRoom, (c) => c.memory.role == 'harvester');
if(harvesters.length < 2){
this.spawnCustomCreep((energy <= 400)?energy:400, 'harvester');
}
let upgrader = _.filter(creepsInRoom, (c) => c.memory.role == 'upgrader');
if(upgrader.length < 1){
this.spawnCustomCreep(energy, 'upgrader');
}
let builder = _.filter(creepsInRoom, (c) => c.memory.role == 'builder');
if(builder.length < 1){
this.spawnCustomCreep(energy, 'builder');
}
let repairer = _.filter(creepsInRoom, (c) => c.memory.role == 'repairer');
if(repairer.length < 1){
this.spawnCustomCreep(energy, 'repairer');
}
let mineralHarvester = _.filter(creepsInRoom, (c) => c.memory.role == 'mineralHarvester');
if(mineralHarvester.length < 0){
this.spawnCustomCreep(energy, 'mineralHarvester');
}
let containers = this.room.find(FIND_STRUCTURES, {filter: (s) => s.structureType == STRUCTURE_CONTAINER});
//Check if any containers need miners or lorries.
for(let container in containers){
if(Memory.containers[containers[container].id].hasMiner == false){
let result = this.spawnMiner(containers[container]);
if(result != undefined && result == 0){
Memory.containers[containers[container].id].hasMiner = true;
}
break;
}
if(Memory.containers[containers[container].id].hasLorry == false){
let result = this.spawnLorry(energy, containers[container]);
if(result != undefined && result == 0){
Memory.containers[containers[container].id].hasLorry = true;
}
break;
}
}
if(this.memory.creepsLimit != undefined){
let defender = _.filter(creepsInRoom, (c) => c.memory.role == 'defender');
let defenderLimit = this.memory.creepsLimit.defender;
if(defenderLimit && defender < defenderLimit){
this.spawnDefender();
}
}
}
StructureSpawn.prototype.spawnCustomCreep =
function(energy, roleName) {
var numberOfParts = Math.floor(energy / 200);
// make sure the creep is not too big (more than 50 parts)
numberOfParts = Math.min(numberOfParts, Math.floor(50 / 3));
var body = [];
for(let i = 0; i < numberOfParts; i++){
body.push(WORK);
}
for(let i = 0; i < numberOfParts; i++){
body.push(CARRY);
}
for(let i = 0; i < numberOfParts; i++){
body.push(MOVE);
}
return this.spawnCreep(body, roleName + '_' + Game.time, {memory: {role: roleName}});
};
/** @param {StructureContainer} container */
StructureSpawn.prototype.spawnMiner = function(container) {
return this.spawnCreep([WORK, WORK, WORK, WORK, WORK, MOVE], 'miner_' + Game.time, {memory: {role: 'miner', container: container.id}});
};
/** @param {StructureContainer} container */
StructureSpawn.prototype.spawnLorry = function(energy, container) {
// create a body with twice as many CARRY as MOVE parts
var numberOfParts = Math.floor(energy / 150);
// make sure the creep is not too big (more than 50 parts)
numberOfParts = Math.min(numberOfParts, Math.floor(50 / 3));
var body = [];
for (let i = 0; i < numberOfParts * 2; i++) {
body.push(CARRY);
}
for (let i = 0; i < numberOfParts; i++) {
body.push(MOVE);
}
return this.spawnCreep(body, 'lorry_' + Game.time, {memory: {role: 'lorry', container: container.id}});
};
StructureSpawn.prototype.spawnDefender = function() {
return this.spawnCreep([TOUGH, MOVE, MOVE, ATTACK, ATTACK], 'defender_' + Game.time, {memory: {role: 'defender'}});
};
};