-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.worker
114 lines (106 loc) · 5.01 KB
/
role.worker
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
var observation = require('observation');
const roleWorker = {
run: function (creep) {
// Check the worker's current task
if (!creep.memory.task) {
let numBuilders = _.sum(Game.creeps, (c) => c.memory.role === 'worker' && c.memory.task === 'build');
let numUpgraders = _.sum(Game.creeps, (c) => c.memory.role === 'worker' && c.memory.task === 'upgrade');
let numRepairers = _.sum(Game.creeps, (c) => c.memory.role === 'worker' && c.memory.task === 'repair');
if (numBuilders < 3) {
creep.memory.task = 'build';
creep.say('🚧 build');
} else if (numBuilders >= 3 && numUpgraders < 2) {
creep.memory.task = 'upgrade';
creep.say('⚡ upgrade');
} else {
creep.memory.task = 'repair';
creep.say('🔧 repair');
}
}
// 检查observation中的warning状态
if (observation.warning) {
creep.say('Warning! Pausing construction');
return;
}
// Check if the worker is currently working and has no energy left
if (creep.memory.working && creep.store[RESOURCE_ENERGY] === 0) {
// Set the worker to not working and make it say "🔄 harvest"
creep.memory.working = false;
creep.say('🔄 harvest');
}
// Check if the worker is not currently working and has full energy capacity
if (!creep.memory.working && creep.store.getFreeCapacity() === 0) {
// Set the worker to working and make it say "🚧 build" or "⚡ upgrade" or "🔧 repair"
creep.memory.working = true;
}
// If the worker is currently working
if (creep.memory.working) {
// Check the worker's current task
switch (creep.memory.task) {
case 'build':
// Find all the construction sites in the room
var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
// If there are any construction sites
if (targets.length) {
// Build the first construction site in the list
if (creep.build(targets[0]) == ERR_NOT_IN_RANGE) {
// If the construction site is too far away, move towards it
creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}});
}
} else {
// If there are no construction sites, go to upgrade the controller
if (creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
// If the controller is too far away, move towards it
creep.moveTo(creep.room.controller, {visualizePathStyle: {stroke: '#ffffff'}});
}
}
break;
case 'upgrade':
// Upgrade the controller of the current room
if (creep.upgradeController(creep.room.controller) === ERR_NOT_IN_RANGE) {
// If the controller is too far away, move towards it
creep.moveTo(creep.room.controller, {visualizePathStyle: {stroke: '#ffffff'}});
}
break;
case 'repair':
// Find all structures that need repair
let damagedStructures = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => structure.hits < structure.hitsMax
});
// If there are any structures that need repair
if (damagedStructures.length > 0) {
// Repair the first structure in the list
if (creep.repair(damagedStructures[0]) === ERR_NOT_IN_RANGE) {
// If the structure is too far away, move towards it
creep.moveTo(damagedStructures[0], {visualizePathStyle: {stroke: '#ffffff'}});
}
} else {
// If there are no construction sites, go to upgrade the controller
if (creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
// If the controller is too far away, move towards it
creep.moveTo(creep.room.controller, {visualizePathStyle: {stroke: '#ffffff'}});
}
}
break;
}
}
// If the worker is not currently working, move to the closest energy source and collect energy
else {
// Find the closest spawn, extension or container with energy
var target = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_SPAWN || structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_CONTAINER) &&
structure.store[RESOURCE_ENERGY] > 0;
}
});
if (target) {
// Withdraw energy from the closest spawn, extension or container with energy
if (creep.withdraw(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
// If the spawn, extension or container is too far away, move towards it
creep.moveTo(target, {visualizePathStyle: {stroke: '#ffffff'}});
}
}
}
}
};
module.exports = roleWorker;