forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreep.behaviour.remoteWorker.js
69 lines (66 loc) · 2.05 KB
/
creep.behaviour.remoteWorker.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
let mod = {};
module.exports = mod;
mod.name = 'remoteWorker';
mod.run = function(creep) {
if (Creep.action.avoiding.run(creep)) {
return;
}
// Assign next Action
let oldTargetId = creep.data.targetId;
if( creep.action == null || creep.action.name == 'idle' ) {
this.nextAction(creep);
}
// Do some work
if( creep.action && creep.target ) {
creep.action.step(creep);
} else {
logError('Creep without action/activity!\nCreep: ' + creep.name + '\ndata: ' + JSON.stringify(creep.data));
}
};
mod.nextAction = function(creep){
// at target room
if( creep.data.destiny.room == creep.pos.roomName ){
let priority;
// get some energy
if( creep.sum < creep.carryCapacity * 0.8 ) {
priority = [
Creep.action.picking,
Creep.action.uncharging,
Creep.action.withdrawing,
Creep.action.harvesting,
Creep.action.idle];
} else {
priority = [
Creep.action.repairing,
Creep.action.building,
Creep.action.recycling
];
}
for(var iAction = 0; iAction < priority.length; iAction++) {
var action = priority[iAction];
if(action.isValidAction(creep) &&
action.isAddableAction(creep) &&
action.assign(creep)) {
return;
}
}
}
// not at target room
else {
this.gotoTargetRoom(creep);
return;
}
// fallback
// recycle self
let mother = Game.spawns[creep.data.motherSpawn];
if( mother ) {
Creep.action.recycling.assign(creep, mother);
}
};
mod.gotoTargetRoom = function(creep){
const targetFlag = creep.data.destiny ? Game.flags[creep.data.destiny.targetName] : null;
if (targetFlag) return Creep.action.travelling.assignRoom(creep, targetFlag.pos.roomName);
};
mod.goHome = function(creep){
return Creep.action.travelling.assignRoom(creep, creep.data.homeRoom);
};