-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRole_OuterMover.js
57 lines (56 loc) · 2.25 KB
/
Role_OuterMover.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
const logger = require('utils.log').getLogger("OuterMover");
module.exports = config => ({
// 获取能量矿
source: creep => {
// 要去的房间
const room = Game.rooms[config.targetRoomName]
// 如果该房间不存在就先往房间走
if (!room) {
creep.moveTo(new RoomPosition(25, 25, config.targetRoomName))
return;
}
const source = Game.getObjectById(config.sourceId);
if (source) {
if (creep.withdraw(source, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.say("🔽");
creep.moveTo(source);
}
} else {
logger.warn(creep.name + "找不到对应的取能建筑!");
}
},
// 存储能量逻辑
target: creep => {
let fixFlag = false;
const roomName = creep.getTemplateConfig("roomName");
if (creep.room.name !== roomName) {
//在非出生房间寻找沿途需要修理的 Road
const fixTarget = creep.pos.findInRange(FIND_STRUCTURES, 1, {
filter: (structure) => structure.hits / structure.hitsMax <= 0.9 && structure.structureType === STRUCTURE_ROAD
});
if (fixTarget.length) {
logger.debug(creep.name + "正在维护沿途道路!");
creep.repair(fixTarget[0]);
fixFlag = true;
}
}
//如沿途没有需要维修的 Road,则将能量运回出生房间 Storage
if (!fixFlag) {
const target = Game.rooms[roomName].storage;
if (target) {
const result = creep.transfer(target, RESOURCE_ENERGY);
if (result === ERR_NOT_IN_RANGE) {
creep.say("🔼");
creep.moveTo(target);
} else if (result === ERR_FULL) {
//目标储存建筑已满,迫不得已丢弃资源以保持外矿运转
creep.drop(RESOURCE_ENERGY);
}
} else {
logger.warn(`[${creep.name}]所在房间[${creep.getTemplateConfig("roomName")}]Storage尚未建好,不建议开启外矿`);
}
}
},
// 状态切换条件
switch: creep => creep.updateState()
})