-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRole_Worker.js
70 lines (69 loc) · 3.05 KB
/
Role_Worker.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
const logger = require('utils.log').getLogger("Worker");
const defaultResourceAmount = 20000;
module.exports = ({
// 拿取货物逻辑
source: creep => {
//首先检测身上有没有上次任务剩余资源
if (!creep.cleanBag(creep.room.memory.moveResource)) {
return;
}
let source = null;
if (creep.room.memory.direction === "Out") {
source = Game.getObjectById(creep.room.getFactory());
} else if (creep.room.memory.direction === "In") {
source = creep.room.storage;
}
if (source) {
creep.say("🔽");
if (source.store[creep.room.memory.moveResource] === 0) {
creep.memory.working = true;
return;
}
const actionResult = creep.withdraw(source, creep.room.memory.moveResource);
if (actionResult === ERR_NOT_IN_RANGE) {
creep.moveTo(source);
} else if (actionResult != OK) {
logger.debug(`\n当前运输物品:${creep.room.memory.moveResource}\n当前Creep携带量:${creep.store.getUsedCapacity(creep.room.memory.moveResource)}\n当前总空间:${creep.store.getCapacity(creep.room.memory.moveResource)}`)
logger.info(`${creep}拿取结果出错:${actionResult}`);
}
} else {
logger.info(`[${creep.name}]缺失提取货物目标`);
}
},
// 存储货物逻辑
target: creep => {
let target = null;
if (creep.room.memory.direction === "Out") {
target = creep.room.terminal;
} else if (creep.room.memory.direction === "In") {
target = Game.getObjectById(creep.room.getFactory());
}
if (target) {
creep.say("🔼");
const actionResult = creep.transfer(target, creep.room.memory.moveResource);
if (actionResult === ERR_NOT_IN_RANGE) {
creep.moveTo(target);
} else if (actionResult === ERR_FULL) {
//当工厂存储满后,直接重新筛选要搬运的资源
creep.room.memory.moveResource = null;
} else if (actionResult != OK) {
logger.info(`[${creep}]存储结果出错:${actionResult}`);
logger.info(`[${creep.name}]当前被指派搬运物品:${creep.room.memory.moveResource}`);
}
} else {
logger.info(`[${creep.name}]缺失存储货物目标`);
}
},
// 状态切换条件
switch: creep => {
// creep 身上没有矿物 && creep 之前的状态为“工作”
if (creep.store[creep.room.memory.moveResource] === 0 && creep.memory.working) {
creep.memory.working = false;
}
// creep 身上矿物满了 && creep 之前的状态为“不工作”
if (creep.store.getUsedCapacity(creep.room.memory.moveResource) === creep.store.getCapacity(creep.room.memory.moveResource) && !creep.memory.working) {
creep.memory.working = true;
}
return creep.memory.working;
}
})