forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creep.behaviour.worker.js
111 lines (108 loc) · 4.83 KB
/
creep.behaviour.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
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
let mod = {};
module.exports = mod;
mod.name = 'worker';
mod.run = function(creep) {
// 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){
if( creep.data.creepType == "worker" && creep.pos.roomName != creep.data.homeRoom && Game.rooms[creep.data.homeRoom] && Game.rooms[creep.data.homeRoom].controller ) {
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;
}
let priority;
if( creep.sum < (creep.carryCapacity*0.5) ) {
priority = [
Creep.action.picking,
Creep.action.dismantling,
Creep.action.withdrawing,
Creep.action.uncharging,
Creep.action.harvesting,
Creep.action.reallocating,
Creep.action.idle];
}
else {
if( creep.room.situation.invasion && creep.room.controller && creep.room.controller.level > 2 ){
priority = [
Creep.action.fueling,
Creep.action.feeding,
Creep.action.repairing,
Creep.action.idle];
} else {
if (creep.data.creepType === "pioneer") { // prioritize building for pioneers, upgrade to RCL2 first
if (creep.room.controller && creep.room.controller.level < 2) {
priority = [
Creep.action.feeding,
Creep.action.upgrading,
Creep.action.building,
Creep.action.repairing,
Creep.action.fueling,
Creep.action.fortifying,
Creep.action.charging,
Creep.action.storing,
Creep.action.picking,
Creep.action.idle];
} else {
priority = [
Creep.action.feeding,
Creep.action.building,
Creep.action.repairing,
Creep.action.fueling,
Creep.action.fortifying,
Creep.action.charging,
Creep.action.upgrading,
Creep.action.storing,
Creep.action.picking,
Creep.action.idle];
}
} else {
priority = [
Creep.action.repairing,
Creep.action.feeding,
Creep.action.building,
Creep.action.fueling,
Creep.action.fortifying,
Creep.action.charging,
Creep.action.upgrading,
Creep.action.storing,
Creep.action.picking,
Creep.action.idle];
}
}
if( creep.room.relativeEnergyAvailable < 1 && (!creep.room.population || !creep.room.population.typeCount['hauler'] || creep.room.population.typeCount['hauler'] < 1 || !creep.room.population.typeCount['miner'] || creep.room.population.typeCount['miner'] < 1) ) {
priority.unshift(Creep.action.feeding);
}
if( creep.room.controller && creep.room.controller.ticksToDowngrade < 2000 ) { // urgent upgrading
priority.unshift(Creep.action.upgrading);
}
}
if( creep.sum > creep.carry.energy ) {
priority.unshift(Creep.action.storing);
}
priority.unshift(Creep.action.bulldozing);
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;
};