-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhive_autospawn.js
124 lines (114 loc) · 3.74 KB
/
hive_autospawn.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
//const cRoles = ['harvester', 'upgrader', 'builder', 'carrier'];
const CRoles = {
harvester: {
min: 3,
main_body: 'WORK',
type: 'basic'
},
upgrader: {
min: 1,
main_body: 'WORK',
type: 'basic'
},
builder: {
min: 2,
main_body: 'WORK',
type: 'basic'
}
};
const Autospawn = {
run: function() { // TODO: Check for no sources
for (let spawn in Game.spawns) {
let spawnName = spawn;
let sources = Game.spawns[spawn].room.find(FIND_SOURCES);
let energyAvailable = Game.spawns[spawn].room.energyAvailable;
let energyCapacityAvailable = Game.spawns[spawn].room.energyCapacityAvailable;
for (let role in CRoles) {
let creepsFound = _.filter(Game.creeps, (creep) => creep.memory.role == role);
// If energy capacity is 300 then only basic creeps should be spawned
if (energyCapacityAvailable === 300 && creepsFound.length < CRoles[role].min &&
CRoles[role].type === 'basic') {
// If there are multiple sources split them between the workers
assignSource(sources, spawnName, role);
}
}
// Spawn display
if (Game.spawns[spawn].spawning) {
var spawningCreep = Game.creeps[Game.spawns[spawn].spawning.name];
Game.spawns[spawn].room.visual.text(
'🛠️' + spawningCreep.memory.role,
Game.spawns[spawn].pos.x + 1,
Game.spawns[spawn].pos.y, {
align: 'left',
opacity: 0.8
});
}
/*
for(let i = 0, r = cRoles.length; i < r; i++) {
let creepsFound = _.filter(Game.creeps, (creep) => creep.memory.role == cRoles[i]);
if (energyCapacityAvailable === 300 && creepsFound < 1 ) {
let newName = Game.spawns[spawnName].createCreep(c_body,c_name,c_memory);
console.log('Spawning new ' + c_memory.role + ': ' + newName);
}
}
*/
}
}
};
// Search for a source where less than half of the existing creeps are assigned
function assignSource(Sources, spawn, role) {
if (Sources.length === 1) {
let newName = Game.spawns[spawn].createCreep([WORK, CARRY, MOVE],
undefined, {
role: role,
sourceId: Sources[0].id
});
} else if (Sources.length > 1) {
for (let source in Sources) {
let creepsOnSource = _.filter(Game.creeps, (creep) => creep.memory.sourceId == Sources[source].id);
if (creepsOnSource.length < _.size(Game.creeps) / 2 || creepsOnSource.length === 0) {
let newName = Game.spawns[spawn].createCreep([WORK, CARRY, MOVE],
undefined, {
role: role,
sourceId: Sources[source].id
});
break;
}
}
}
}
/*
let ch_base = [WORK,CARRY,MOVE];
let c_name = undefined;
let ch_memory = {
role: 'harvester'
};
if (energyAvailable === energyCapacityAvailable) {
// Deduct cost of base creep(work,carry,move)
energyAvailable -= 200;
//Assuming that 150 energy is needed to add WORK + MOVE Part, energyLeft
// will equal the number of creeps that can be spawned
let energyLeft = Math.floor(energyAvailable / 150);
if (energyLeft >= 1) {
let c_body = [
...Array(energyLeft).fill(WORK),
...c_base,
...Array(energyLeft).fill(MOVE)
];
let newName = Game.spawns[spawnName].createCreep(c_body,c_name,c_memory);
console.log('Spawning new ' + c_memory.role + ': ' + newName);
}
}
if (Game.spawns[spawn].spawning) {
var spawningCreep = Game.creeps[Game.spawns[spawn].spawning.name];
Game.spawns[spawn].room.visual.text(
'🛠️' + spawningCreep.memory.role,
Game.spawns[spawn].pos.x + 1,
Game.spawns[spawn].pos.y, {
align: 'left',
opacity: 0.8
});
}
}
*/
module.exports = Autospawn;