forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreep.behaviour.remoteHauler.js
84 lines (83 loc) · 3.04 KB
/
creep.behaviour.remoteHauler.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
let mod = {};
module.exports = mod;
mod.name = 'remoteHauler';
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){
// at home
if( creep.pos.roomName == creep.data.homeRoom ){
// carrier filled
if( creep.sum > 0 ){
let deposit = []; // deposit energy in...
// links?
if( creep.carry.energy == creep.sum ) deposit = creep.room.structures.links.privateers;
// storage?
if( creep.room.storage ) deposit.push(creep.room.storage);
// containers?
if( creep.room.structures.container ) deposit = deposit.concat( creep.room.structures.container.privateers );
// Choose the closest
if( deposit.length > 0 ){
let target = creep.pos.findClosestByRange(deposit);
if( target.structureType == STRUCTURE_STORAGE && this.assign(creep, Creep.action.storing) ) return;
else if( this.assign(creep, Creep.action.charging, target) ) return;
}
if( this.assign(creep, Creep.action.charging) ) return;
// no deposit :/
// try spawn & extensions
if( this.assign(creep, Creep.action.feeding) ) return;
// TODO: hauler shouldn't work. drop at spawn instead of calling worker behaviour
Creep.behaviour.worker.nextAction(creep);
return;
}
// empty
// travelling
this.gotoTargetRoom(creep);
return;
}
// at target room
else if( creep.data.destiny.room == creep.pos.roomName ){
if( this.assign(creep, Creep.action.uncharging) ) return;
// if it's not full
if( creep.sum < (creep.carryCapacity*0.8) ) {
// get some energy
if( this.assign(creep, Creep.action.picking) ) return;
}
// carrier full or everything picked
this.goHome(creep);
return;
}
// somewhere
else {
if( creep.sum > 0 )
this.goHome(creep);
else
this.gotoTargetRoom(creep);
return;
}
// fallback
// recycle self
let mother = Game.spawns[creep.data.motherSpawn];
if( mother ) {
this.assign(creep, Creep.action.recycling, mother);
}
};
mod.assign = function(creep, action, target){
return (action.isValidAction(creep) && action.isAddableAction(creep) && action.assign(creep, target));
};
mod.gotoTargetRoom = function(creep){
return Creep.action.travelling.assign(creep, Game.flags[creep.data.destiny.targetName]);
};
mod.goHome = function(creep){
return Creep.action.travelling.assign(creep, Game.rooms[creep.data.homeRoom].controller);
};