forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreep.behaviour.hauler.js
59 lines (56 loc) · 1.9 KB
/
creep.behaviour.hauler.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
let mod = {};
module.exports = mod;
mod.name = 'hauler';
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.pos.roomName != creep.data.homeRoom && Game.rooms[creep.data.homeRoom] && Game.rooms[creep.data.homeRoom].controller ) {
Creep.action.travelling.assign(creep, Game.rooms[creep.data.homeRoom].controller);
return;
}
let priority;
if( creep.sum < creep.carryCapacity/2 ) {
priority = [
Creep.action.uncharging,
Creep.action.picking,
Creep.action.reallocating,
Creep.action.withdrawing,
Creep.action.idle];
}
else {
priority = [
Creep.action.feeding,
Creep.action.charging,
Creep.action.fueling,
Creep.action.storing,
Creep.action.idle];
if ( creep.sum > creep.carry.energy ||
( !creep.room.situation.invasion
&& SPAWN_DEFENSE_ON_ATTACK
&& creep.room.conserveForDefense && creep.room.relativeEnergyAvailable > 0.8)) {
priority.unshift(Creep.action.storing);
}
if (creep.room.structures.urgentRepairable.length > 0 ) {
priority.unshift(Creep.action.fueling);
}
}
for(var iAction = 0; iAction < priority.length; iAction++) {
var action = priority[iAction];
if(action.isValidAction(creep) &&
action.isAddableAction(creep) &&
action.assign(creep)) {
return;
}
}
};