-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
216 lines (192 loc) · 7.33 KB
/
main.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
*
* Fleetster Node.js Developer.
* Test Assignment
*
*
* Description:
* Screeps game to build powerful colony.
*
* Main Purpose:
* -Upgrade our Room Controller.
* -Build structures.
*
*
* We have 2 main roles here Harvest/Build and Upgrade creeps.
*
* Upgrader creeps should work on upgrading the room Controller.
*
* Harvester/Builder creep should work on building and energizing structure.
* if harvesters doesn't found structures needs energy then convert them to builders to build structures.
* if builder doesn't found construction sites to build then convert them to harvesters to energize structures.
* note about converting we don't convert all creeps at once instead we convert one by one so we can have a building and energizing case
*
*
* Tips
*
* More Extensions MORE powerful Creeps
* we should dynamically add parts to creeps based on the energyAvailable bodyCreator function
*
* Bigger Colony Bigger Resources
* we should every time we had full energyCapacityAvailable build an extension to push the current capacity limit for more energy reservation
*
* Dying creeps
* we should regenerate (powerful if we can) creeps and maintain the 2 roles of creeps equally as many as extension we have
*
*
* @author Mohamed Habashy <[email protected]>
*
*/
// requiring the harvester creep module
var roleHarvester = require('role.harvester');
// requiring the upgrader creep module
var roleUpgrader = require('role.upgrader');
// requiring the builder creep module
var roleBuilder = require('role.builder');
// Loop Module will be execute with every tick
module.exports.loop = function () {
// current number of extensions available
var extensions = 0;
// current room name
var roomName = '';
// current spawn name
var spwanName = '';
// freeing memory of died creeps
for (var name in Memory.creeps) {
if (!Game.creeps[name]) {
delete Memory.creeps[name];
console.log('Clearing non-existing creep memory:', name);
}
}
// getting current room spawn
for (var name in Game.spawns) {
// current spawn name as now we only use one
spwanName = name;
}
// for every room we have currently only one we check if we reached full energy capacity
// then we create upgrader creep or extension
for (var name in Game.rooms) {
console.log('Room "' + name + '" has ' + Game.rooms[name].energyAvailable + ' energy');
// current room name
roomName = name;
// spawn position x,y
var spawn_pos = Game.spawns[spwanName].pos;
// get how many extensions we have
extensions = Game.rooms[name].find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION);
}
}).length;
/**
* Bigger Colony Bigger Resources
*
*
*/
// if we reached full energy capacity
if (Game.rooms[name].energyAvailable == Game.rooms[name].energyCapacityAvailable) {
console.log('Room "' + name + '" has FULL ' + Game.rooms[name].energyAvailable + ' energy');
// if we reached full capacity then we must create a upgrader to help upgrading the Room Controller if we don't have any extensions
if (extensions == 0) {
var newName = Game.spawns[spwanName].createCreep(bodyCreator(), undefined, {
role: 'upgrader'
});
console.log('Spawning new upgrader: ' + newName);
}
// when full energy create EXTENSION to store energy
console.log('creating extension ');
// create the extension relative to the spawn position and extensions already exist
Game.rooms[name].createConstructionSite(spawn_pos.x + extensions + 1, spawn_pos.y + extensions + 1, STRUCTURE_EXTENSION);
}
}
/*
* Create creep body based on extensions available
* More Extensions More powerful Creeps
*
*
* @return {Array} creep body part
*
*/
function bodyCreator() {
// default creep body parts and subtract it's cost(200) from the energyAvailable
var array = [WORK, CARRY, MOVE];
// get floor of the quotient of the energyAvailable divided by 50 (min creep body part)
// so we can build powerful creep based on available energy
switch (Math.floor((Game.rooms[roomName].energyAvailable - 200) / 50)) {
case 4:
// we have 200 more energy than default(200) == total 400
// create more heavy strong fast creep
array.push(WORK);
array.push(CARRY);
array.push(MOVE);
break;
case 3:
// we have 150 more energy than default(200) == total 350
// create more heavy strong creep
array.push(WORK);
array.push(CARRY);
break;
case 2:
// we have 100 more energy than default(200) == total 300
// create more strong fast creep
array.push(CARRY);
array.push(MOVE);
break;
case 1:
// we have 50 more energy than default(200) == total 250
// create more fast creep
array.push(MOVE);
break;
default:
break;
}
// if we have 1 or extensions and our energy < 250 we should wait to harvest more energy to build big creep
if (extensions > 1 && Game.rooms[roomName].energyAvailable < 250) {
return 0;
}
// return body creep array parts
return array;
}
/**
* Keeping creeps from dying
* we need to maintain harvesters and upgraders as many as we have extensions
*
*
*/
// get how many harvesters we have
var harvesters = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester');
console.log('Harvesters: ' + harvesters.length);
// if harvesters less than extension then create new harvester
if (harvesters.length <= extensions) {
var newName = Game.spawns[spwanName].createCreep(bodyCreator(), undefined, {
role: 'harvester'
});
console.log('Spawning new harvester: ' + newName);
}
// get how many upgraders we have
var upgraders = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgrader');
console.log('upgraders: ' + upgraders.length);
// if upgraders less than extension then create new upgrader
if (upgraders.length <= extensions) {
var newName = Game.spawns[spwanName].createCreep(bodyCreator(), undefined, {
role: 'upgrader'
});
console.log('Spawning new upgrader: ' + newName);
}
/**
* Distribute creeps based on their roles.
*
*
*/
for (var name in Game.creeps) {
var creep = Game.creeps[name];
if (creep.memory.role == 'harvester') {
roleHarvester.run(creep);
}
if (creep.memory.role == 'upgrader') {
roleUpgrader.run(creep);
}
if (creep.memory.role == 'builder') {
roleBuilder.run(creep);
}
}
}