-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServiceGameObjectFinder.js
66 lines (53 loc) · 1.59 KB
/
ServiceGameObjectFinder.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
"use strict";
const Service = require('Service');
module.exports = class ServiceGameObjectFinder extends Service
{
constructor(locator)
{
super();
this.screepsApi = locator.getService(SERVICE_NAMES.SCREEPS_API);
}
getNearestUnreservedResoucePositionAboveLevelInRoom(from, resourceType=RESOURCE_ENERGY, aboveLevel=0)
{
const isResourcecontainingStructure = function(structure)
{
switch(structure.structureType)
{
case STRUCTURE_CONTAINER:
case STRUCTURE_STORAGE:
case STRUCTURE_TERMINAL:
return !isNullOrUndefined(structure.store[resourceType]) &&
structure.store[resourceType] > aboveLevel;
case STRUCTURE_EXTENSION:
case STRUCTURE_LINK:
return resourceType === RESOURCE_ENERGY && structure.energy > aboveLevel;
case STRUCTURE_POWER_SPAWN:
return (resourceType === RESOURCE_ENERGY && structure.energy > aboveLevel) ||
(resourceType === RESOURCE_POWER && structure.power > aboveLevel);
default:
return false;
}
};
const fromRoomPosition = this.screepsApi.getRoomPosition(from);
const room = this.screepsApi.getRoom(from.roomName);
const candPositions =
_.map(
(
_.remove(
room.find(FIND_DROPPED_RESOURCES)
, (resource) => resource.resourceType === resourceType ||
resource.amount <= aboveLevel
)
).concat(
_.remove(
room.find(FIND_STRUCTURES)
, (structure) => isResourcecontainingStructure(structure)
)
)
, (candidate)=>candidate.pos
);
if(candPositions.length === 0)
return null;
return fromRoomPosition.findClosestByPath(candPositions);
}
};