forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreep.behaviour.collapseWorker.js
92 lines (87 loc) · 3.35 KB
/
creep.behaviour.collapseWorker.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
91
92
let mod = {};
module.exports = mod;
mod.name = 'collapseWorker';
mod.run = function(creep) {
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){
if( creep.pos.roomName !== creep.data.homeRoom ) {
if( DEBUG && TRACE ) trace('Behaviour', {actionName:'travelling', behaviourName:this.name, creepName:creep.name, assigned: true, Behaviour:'nextAction', Action:'assign'});
Creep.action.travelling.assignRoom(creep, creep.data.homeRoom);
return true;
}
if( !creep.room.collapsed ) {
return Creep.action.recycling.assign(creep);
}
let priority;
if( creep.sum < (creep.carryCapacity*0.5) ) {
priority = [
Creep.action.picking,
Creep.action.withdrawing,
Creep.action.uncharging,
Creep.action.harvesting,
Creep.action.dismantling,
Creep.action.reallocating,
Creep.action.idle];
}
else {
if( creep.room.situation.invasion && creep.room.controller && creep.room.controller.level > 2 ){
priority = [
Creep.action.feeding,
Creep.action.fueling,
Creep.action.repairing,
Creep.action.idle];
} else {
priority = [
Creep.action.feeding,
Creep.action.fueling,
Creep.action.charging,
Creep.action.repairing,
Creep.action.building,
Creep.action.fortifying,
Creep.action.upgrading,
Creep.action.storing,
Creep.action.picking,
Creep.action.idle];
}
if( creep.room.controller && creep.room.controller.ticksToDowngrade < 500 ) { // urgent upgrading
priority.unshift(Creep.action.upgrading);
}
}
for(var iAction = 0; iAction < priority.length; iAction++) {
var action = priority[iAction];
const valid = action.isValidAction(creep);
if( DEBUG && TRACE ) trace('Action', {actionName:action.name, behaviourName:this.name, creepName:creep.name, valid, Action:'isValidAction'});
if( !valid ) continue;
const addable = action.isAddableAction(creep);
if( DEBUG && TRACE ) trace('Action', {actionName:action.name, behaviourName:this.name, creepName:creep.name, addable, Action:'isAddableAction'});
if( !addable ) continue;
const assigned = action.assign(creep);
if( assigned ) {
if( DEBUG && TRACE ) trace(assigned ? 'Behaviour' : 'Action', {actionName:action.name, behaviourName:this.name, reepName:creep.name, assigned, Behaviour:'nextAction', Action:'assign'});
return true;
}
}
return false;
};
mod.strategies = {
defaultStrategy: {
name: `default-${mod.name}`,
canWithdrawEnergy: function(creep, target) {
return function(amount) {
return amount > 0;
};
},
},
};
mod.selectStrategies = function(actionName) {
return [mod.strategies.defaultStrategy, mod.strategies[actionName]];
};