forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreep.behaviour.claimer.js
48 lines (47 loc) · 1.85 KB
/
creep.behaviour.claimer.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
let mod = {};
module.exports = mod;
mod.name = 'claimer';
mod.run = function(creep) {
// Assign next Action
let oldTargetId = creep.data.targetId;
if( creep.action == null || creep.action.name == 'idle') {
if( creep.data.destiny && creep.data.destiny.task && Task[creep.data.destiny.task] && Task[creep.data.destiny.task].nextAction )
Task[creep.data.destiny.task].nextAction(creep);
else 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));
}
if( creep.hits < creep.hitsMax ) { // creep injured. move to next owned room
if (creep.data) {
if (!creep.data.nearestHome || !Game.rooms[creep.data.nearestHome]) creep.data.nearestHome = Room.bestSpawnRoomFor(creep.pos.roomName);
if (creep.data.nearestHome) {
let room = Game.rooms[creep.data.nearestHome];
if (room) {
let range = creep.pos.getRangeTo(room.controller);
if (range > 1) creep.travelTo( room.controller.pos );
}
}
}
}
if( DEBUG && TRACE ) trace('Behaviour', {creepName:creep.name, run:creep.action && creep.action.name || 'none', [mod.name]: 'run', Behaviour:mod.name});
};
mod.nextAction = function(creep){
let priority = [
Creep.action.claiming,
Creep.action.reserving,
Creep.action.bulldozing,
Creep.action.idle
];
for(var iAction = 0; iAction < priority.length; iAction++) {
var action = priority[iAction];
if(action.isValidAction(creep) &&
action.isAddableAction(creep) &&
action.assign(creep)) {
return;
}
}
};