-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhelper.logistic.js
139 lines (124 loc) · 4.93 KB
/
helper.logistic.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
var storeStructures = [
STRUCTURE_CONTAINER,
STRUCTURE_LINK,
STRUCTURE_STORAGE,
STRUCTURE_TERMINAL
];
module.exports = {
obtainResults: {
withdrawn: 1,
harvested: 2,
moving: 3,
pickedUp: 4,
empty: 5
},
obtainEnergy: function(creep, source, considerStorage) {
this.pickupSpareEnergy(creep);
if(considerStorage) {
var result = this.obtainEnergyFromStore(creep, creep.room.storage);
if(result && result !== this.obtainResults.empty) {
return result;
}
}
if(!source) return null;
var store = this.storeFor(source);
var result = this.obtainEnergyFromStore(creep, store);
if((!result || result === this.obtainResults.empty) && _.find(creep.body, (p) => p.type === WORK)) {
result = creep.harvest(source);
if(result == OK) {
return this.obtainResults.harvested;
} else if(result == ERR_NOT_IN_RANGE) {
creep.goTo(source);
return this.obtainResults.moving;
}
} else if(result === this.obtainResults.empty) {
if(!creep.pos.isNearTo(store)) {
creep.goTo(store);
return this.obtainResults.moving;
}
} else {
return result;
}
return null; // something unexpected happened
},
obtainEnergyFromStore: function(creep, store) {
if(store) {
if(store.store.energy === 0) return this.obtainResults.empty;
if(creep.pos.isNearTo(store)) {
if(creep.withdraw(store, RESOURCE_ENERGY) === OK) return this.obtainResults.withdrawn;
} else {
creep.goTo(store);
return this.obtainResults.moving;
}
}
return null;
},
pickupSpareEnergy: function(creep) {
var resources = creep.pos.lookFor(LOOK_ENERGY);
// TODO: fix to work with any resource (and not pickup resource even if we want energy)
if(resources.length > 0 && resources[0].resourceType == RESOURCE_ENERGY) {
return creep.pickup(resources[0]) == OK;
}
return false;
},
storeFor: function(target, includeConstructions, structureType) {
if(!target) return null;
if(storeStructures.includes(target.structureType) && (!structureType || structureType == target.structureType)) return target;
if(!includeConstructions && !structureType) {
var stores = target.room.memory.stores;
if(stores) {
var store = Game.getObjectById(stores[target.id]);
if(store) return store;
}
}
var structures = target.pos.findInRange(FIND_STRUCTURES, 2);
var store = _.find(structures, (r) => storeStructures.includes(r.structureType) && (!structureType || structureType == r.structureType));
if(store) {
target.room.memory.stores = target.room.memory.stores || {};
target.room.memory.stores[target.id] = store.id;
return store;
}
if(includeConstructions) {
var constructions = target.pos.findInRange(FIND_CONSTRUCTION_SITES, 2);
return _.find(constructions, (r) => storeStructures.includes(r.structureType) && (!structureType || structureType == r.structureType));
} else {
return null;
}
},
distanceByPath: function(source, destination) {
if(Memory.distances && Memory.distances[source.id] && Memory.distances[source.id][destination.id]) {
return Memory.distances[source.id][destination.id];
}
// TODO: consider some kinds of obstacles?
var pathResult = PathFinder.search(source.pos, [{ pos: destination.pos, range: 1 }]);
var path = pathResult.path;
Memory.distances = Memory.distances || {};
Memory.distances[source.id] = Memory.distances[source.id] || {};
Memory.distances[source.id][destination.id] = path.length;
return path.length;
},
cleanupCaches: function() {
for(let sourceId in Memory.distances) {
if(Game.getObjectById(sourceId)) {
let current = Memory.distances[sourceId];
for(let destinationId in current) {
if(!Game.getObjectById(destinationId)) {
delete current[destinationId];
}
}
} else {
delete Memory.distances[sourceId]
}
}
for(let roomMemory of _.values(Memory.rooms)) {
if(!roomMemory.stores) continue;
for(let storeId in roomMemory.stores) {
if(!Game.getObjectById(storeId)) {
delete roomMemory.stores[storeId];
}
}
}
}
};
const profiler = require("screeps-profiler");
profiler.registerObject(module.exports, 'logistics');