-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.js
177 lines (150 loc) · 5.44 KB
/
lib.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
function getEnergySource(creep, energyClaims) {
var sourceMap = {};
var availableSources = [];
var roomSources = creep.room.find(FIND_SOURCES);
for (var roomSource of roomSources) {
sourceMap[roomSource.pos.x + ',' + roomSource.pos.y] = {
"num": 0,
"spots": getOpenSpots(creep.room, roomSource.pos),
"source": roomSource
};
}
if (energyClaims !== undefined) {
for (var [creepName, claimPos] of Object.entries(energyClaims)) {
if (creepName != creep.name && claimPos !== null) {
sourceMap[claimPos.pos.x + ',' + claimPos.pos.y]['num']++;
}
}
}
for (var sourceInfo of Object.values(sourceMap)) {
if (sourceInfo['num'] < sourceInfo['spots'] && sourceInfo['source']['energy'] > 0) {
availableSources.push(sourceInfo['source']);
}
}
if (availableSources.length == 0) {
for (var roomSource of roomSources) {
availableSources.push(roomSource);
}
}
return(creep.pos.findClosestByPath(availableSources));
}
function buildExtensions(room, extensions) {
room.createConstructionSite(x + 1, y, STRUCTURE_EXTENSION);
room.createConstructionSite(x + 1, y + 1, STRUCTURE_EXTENSION);
room.createConstructionSite(x, y + 1, STRUCTURE_EXTENSION);
room.createConstructionSite(x, y, STRUCTURE_EXTENSION);
}
function buildThing(room, coords, thing) {
for (var coord of coords) {
let rc = room.createConstructionSite(coord[0], coord[1], thing);
//console.log('site ' + rc);
}
}
function getOpenSpots(room, pos) {
var spots = 0;
var myX = pos.x;
var myY = pos.y;
var surroundingSpots = [
[myX, myY + 1],
[myX, myY - 1],
[myX + 1, myY + 1],
[myX + 1, myY - 1],
[myX + 1, myY],
[myX - 1, myY + 1],
[myX - 1, myY - 1],
[myX - 1, myY]
]
for (var myPos of surroundingSpots) {
for (var terrain of room.lookAt(myPos[0], myPos[1])) {
if (terrain['type'] == 'terrain' && terrain['terrain'] != 'wall') {
spots++;
}
}
}
return(spots);
}
function flipRole(room, fromRole, toRole) {
var flipped = false;
for (var creep of room.find(FIND_CREEPS)) {
if (creep.memory.role === undefined) {
creep.memory.role = toRole;
flipped = true;
break;
}
}
if (!flipped) {
for (var creep of room.find(FIND_CREEPS)) {
if (creep.memory.role == fromRole) {
creep.memory.role = toRole;
break;
}
}
}
}
function assignJobs(room, workerInfo, creepCap) {
var existingRoles = {};
for (var creep of room.find(FIND_CREEPS)) {
if (existingRoles[creep.memory.role] === undefined) {
existingRoles[creep.memory.role] = 1;
} else {
existingRoles[creep.memory.role]++;
}
}
if ((existingRoles['gatherer'] < workerInfo['gatherer'] || existingRoles['gatherer'] === undefined) && (existingRoles['upgrader'] !== undefined || existingRoles['undefined'] !== undefined)) {
flipRole(room, 'upgrader', 'gatherer');
} else if ((existingRoles['upgrader'] < workerInfo['upgrader'] || existingRoles['upgrader'] === undefined ) && (existingRoles['gatherer'] > workerInfo['gatherer'] || existingRoles['undefined'] !== undefined)) {
flipRole(room, 'gatherer', 'upgrader');
} else if (workerInfo['builder'] > 0 && (existingRoles['builder'] < workerInfo['builder'] || existingRoles['builder'] === undefined)) {
if (room.find(FIND_CONSTRUCTION_SITES).length == 0) {
flipRole(room, 'builder', 'gatherer');
} else if (existingRoles['upgrader'] > workerInfo['upgrader'] || existingRoles['undefined'] > 0) {
flipRole(room, 'upgrader', 'builder');
} else if (existingRoles['gatherer'] > workerInfo['gatherer']) {
flipRole(room, 'gatherer', 'builder');
}
}
}
function getCreepCost(parts) {
var totalCost = 0;
for (part of parts) {
totalCost += BODYPART_COST[part];
}
return(totalCost)
}
function buildParts(parts) {
var finalParts = [];
for (var type in parts) {
for (let i = 0; i < parts[type]; i++) {
finalParts.push(type);
}
}
return(finalParts);
}
function getFreeTargets(creep, targetClaims, minDist) {
var freeTargets = [];
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_EXTENSION ||
structure.structureType == STRUCTURE_SPAWN ||
structure.structureType == STRUCTURE_TOWER) &&
structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
}
});
if (targetClaims === undefined) {
return(targets);
}
for (var target of targets) {
var claimed = false;
for (var [creepName, creepPos] of Object.entries(targetClaims)) {
if ((creep.name != creepName) && Math.abs(target.pos.x - creepPos.x) + Math.abs(target.pos.y - creepPos.y) <= minDist) {
claimed = true;
break;
}
}
if (!claimed) {
freeTargets.push(target);
}
}
return(freeTargets);
}
module.exports = { getEnergySource, buildExtensions, getOpenSpots, assignJobs, getCreepCost, buildThing, buildParts, getFreeTargets };