-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.room.js
90 lines (81 loc) · 2.18 KB
/
memory.room.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
/**
* Initialize the memory storage for a new container.
* Stores meta info about the container (e.g. source/mineral nearby) and set up storage targets.
*
* @param {StructureContainer} container
*/
function initMemoryContainer(container) {
let source = container.pos.findInRange(FIND_SOURCES, 1)[0];
let mineral = container.pos.findInRange(FIND_MINERALS, 1)[0];
let controller = container.pos.inRangeTo(container.room.controller.pos, 4);
let miner = source || mineral ? null : undefined;
let sourceID = undefined;
if (source) {
sourceID = source.id;
}
if (mineral) {
sourceID = mineral.id;
}
let threshold = {};
if (source) {
threshold[RESOURCE_ENERGY] = 0;
}
if (mineral) {
threshold[mineral.mineralType] = 0;
}
if (controller) {
threshold[RESOURCE_ENERGY] = container.storeCapacity;
}
if (_.isEmpty(threshold)) {
threshold[RESOURCE_ENERGY] = container.storeCapacity;
}
return {
threshold: threshold,
miner: miner,
sourceID: sourceID,
};
}
/**
* Initialize the memory storage for a new room
*
* @param {Room} room
*/
function init(room) {
Memory.rooms[room.name] = {};
let memory = Memory.rooms[room.name];
memory.containers = {};
room.find(FIND_STRUCTURES, {filter: (s) => s.structureType === STRUCTURE_CONTAINER}).map(c => {
memory.containers[c.id] = initMemoryContainer(c);
});
}
function update(room) {
let memory = room.memory;
for (let container in memory.containers) {
if (!Game.getObjectById(container)) {
delete memory.containers[container];
}
}
room.find(FIND_STRUCTURES, {filter: (s) => s.structureType === STRUCTURE_CONTAINER}).map(c => {
if (memory.containers[c.id] === undefined) {
memory.containers[c.id] = initMemoryContainer(c);
}
});
}
/**
* Handle memory management of a room.
*
* @param {Room} room
*/
function handle(room) {
if (!Memory.rooms) {
Memory.rooms = {};
}
if (!room.memory) {
init(room);
} else if (Game.time % 50 === 0) {
update(room);
}
}
module.exports = {
handle: handle,
};