-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
186 lines (157 loc) · 6.76 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
var roleUtil = require('util.role');
var spawnUtil = require('util.spawn');
var loopMem = require('util.loopMem');
var gc = require('util.gc');
var withDefault = require('util.withDefault');
var workerBuild = require('build.worker');
var extractorBuild = require('build.extractor');
module.exports.loop = function () {
var roomName = 'W9N68';
loopMem.newLoop();
//require('util.spawn').requestSpawn(5, require('build.worker'), ['builder']);
//require('util.role').convertAny('worker', 'upgrader', 'builder');
gc.doGC();
var builds = {};
for (name in Game.creeps) {
var creep = Game.creeps[name];
if(creep.memory.build == 'extractor' && creep.ticksToLive < 50) {
spawnUtil.requestSpawn(spawnUtil.PRIORITY_DEFAULT, extractorBuild, [roleUtil.DEFAULT_ROLE]);
}
var build = withDefault.getObj(builds, creep.memory.build);
var role = creep.memory.role || roleUtil.DEFAULT_ROLE;
build[role] = withDefault.getNum(build, role) + 1;
//console.log(creep.memory.build + "/" + role + " - " + name);
}
var sortedBuilds = _.sortBy(Object.keys(builds));
for (build of sortedBuilds) {
var roles = builds[build];
var msg = " " + build + "(" + _.sum(roles) + "):\n ";
var sortedRoles = _.sortBy(Object.keys(roles));
for (role of sortedRoles) {
msg = msg + role + ": " + roles[role] + " ";
}
console.log(msg);
}
var extractors = _.filter(Game.creeps, (creep) => creep.memory.build == 'extractor');
var harvesters = _.filter(Game.creeps, (creep) => creep.memory.role == 'harvester');
var upgraders = _.filter(Game.creeps, (creep) => creep.memory.role == 'upgrader');
var repairers = _.filter(Game.creeps, (creep) => creep.memory.role == 'repairer');
var builders = _.filter(Game.creeps, (creep) => creep.memory.role == 'builder');
var constructionUnitsNeeded = 0;
for (i in Game.constructionSites) {
var site = Game.constructionSites[i];
constructionUnitsNeeded += site.progressTotal - site.progress;
}
//console.log("c needed: " + constructionUnitsNeeded);
var desiredHarvesters = 4;
var desiredUpgraders = 2;
var desiredRepairers = 3;// - Game.rooms[roomName].find(FIND_MY_STRUCTURES, {filter: {structureType: STRUCTURE_TOWER}}).length;
var desiredBuilders = Math.min(6, Math.ceil(constructionUnitsNeeded / 100));
//console.log("desireed builders: " + desiredBuilders);
var desiredTotal = 16;
var numCreeps = Object.keys(Game.creeps).length;
if (numCreeps <= 2) {
desiredHarvesters = 1;
desiredUpgraders = 1;
} else if (numCreeps <= 3) {
desiredHarvesters = 2;
desiredUpgraders = 1;
}
if (extractors.length < 2) {
spawnUtil.requestSpawn(spawnUtil.PRIORITY_DEFAULT, extractorBuild, [roleUtil.DEFAULT_ROLE]);
}
if(harvesters.length < desiredHarvesters) {
if (harvesters.length == 0 || upgraders.length > 1) {
roleUtil.convertAny('worker', 'upgrader', 'harvester');
}
}
if(upgraders.length < desiredUpgraders) {
if (builders.length > 0) {
roleUtil.convertAny('worker', 'builder', 'upgrader');
} else if (harvesters.length > 1 && upgraders.length == 0) {
roleUtil.convertAny('worker', 'harvester', 'upgrader');
}
}
if(repairers.length < desiredRepairers) {
if (upgraders.length > desiredUpgraders) {
roleUtil.convertAny('worker', 'upgrader', 'repairer');
} else {
spawnUtil.requestSpawn(spawnUtil.PRIORITY_DEFAULT, workerBuild, ['repairer']);
}
}
if(builders.length < desiredBuilders) {
if (upgraders.length > desiredUpgraders) {
roleUtil.convertAny('worker', 'upgrader', 'builder');
} else {
spawnUtil.requestSpawn(spawnUtil.PRIORITY_DEFAULT, workerBuild, ['builder']);
}
}
if (builders.length > desiredBuilders) {
console.log("Too many builders");
var numToConvert = builders.length - desiredBuilders;
for (var i = 0; i < numToConvert; i++) {
if (repairers.length < desiredRepairers) {
roleUtil.convertAny('worker', 'builder', 'repairer');
} else {
roleUtil.convertAny('worker', 'builder', 'upgrader');
}
}
}
numCreeps = Object.keys(Game.creeps).length;
if (numCreeps < desiredTotal) {
//console.log("Too few creeps: found " + numCreeps + " wanted " + desiredTotal);
spawnUtil.requestSpawn(spawnUtil.PRIORITY_DEFAULT, workerBuild, ['upgrader']);
}
if (numCreeps <= 3) {
if (numCreeps == 0 || numCreeps == 2) {
spawnUtil.spawn([WORK, WORK, CARRY, MOVE], 'worker', 'harvester', 1);
} else if (numCreeps == 1) {
spawnUtil.spawn([WORK, WORK, CARRY, MOVE], 'worker', 'upgrader', 1);
}
}
spawnUtil.executeQueues();
var extractors = [];
var workers = [];
for(var name in Game.creeps) {
var creep = Game.creeps[name];
var build = creep.memory.build;
if (build == extractorBuild.build) {
extractors.push(creep);
} else if (build == workerBuild.build) {
workers.push(creep);
} else {
console.log("Unknown build! " + build);
}
}
extractorBuild.run(extractors);
workerBuild.run(workers);
var towers = Game.rooms[roomName].find(FIND_MY_STRUCTURES,
{filter: {structureType: STRUCTURE_TOWER}});
for (tower of towers) {
var closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS);
if(closestHostile) {
tower.attack(closestHostile);
try {
var username = closestHostile.owner.username;
Game.notify(`User ${username} spotted in room ${roomName}`);
} catch (err) {
Game.notify(err);
}
} else {
var closestDamagedStructure = tower.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (structure) => ((structure.hits < (structure.hitsMax * 0.9)) &&
(structure.hitsMax < 1000000 ||
structure.hits < ((tower.energy < tower.energyCapacity / 2) ? 1000 : 25000)))
});
if(closestDamagedStructure) {
tower.repair(closestDamagedStructure);
}
}
}
// var roads = _.filter(Game.constructionSites, (site) => site.structureType == STRUCTURE_ROAD);
// var walls = _.filter(Game.constructionSites, (site) => site.structureType == STRUCTURE_WALL);
// for (var n in walls) {
// var s = walls[n];
// s.remove();
// }
}