forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creep.behaviour.remoteHauler.js
115 lines (114 loc) · 4.51 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
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
112
113
114
115
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, target) ) return;
else if( this.assign(creep, Creep.action.charging, target) ) return;
else if( this.assign(creep, Creep.action.storing) ) return; // prefer storage
}
if( this.assign(creep, Creep.action.charging) ) return;
// no deposit :/
// try spawn & extensions
if( this.assign(creep, Creep.action.feeding) ) return;
if( this.assign(creep, Creep.action.dropping) ) return;
else {
const drop = r => { if(creep.carry[r] > 0 ) creep.drop(r); };
_.forEach(Object.keys(creep.carry), drop);
return this.assign(creep, Creep.action.idle);
}
}
// empty
// travelling
if (this.gotoTargetRoom(creep)) {
return;
}
}
// at target room
else if( creep.data.destiny.room == creep.pos.roomName ){
// TODO: This should perhaps check which distance is greater and make this decision based on that plus its load size
if( creep.sum / creep.carryCapacity > REMOTE_HAULER.MIN_LOAD) {
this.goHome(creep);
return;
}
// picking last until we have strategies that can compare cost vs benefit otherwise remoteHaulers bounce between piles of dropped energy
if( this.assign(creep, Creep.action.uncharging) ) return;
// if( this.assign(creep, Creep.action.robbing) ) return;
if( this.assign(creep, Creep.action.picking) ) return;
// wait
if ( creep.sum === 0 ) {
let source = creep.pos.findClosestByRange(creep.room.sources);
if (creep.room && source && creep.pos.getRangeTo(source) > 3) {
creep.data.travelRange = 3;
return Creep.action.travelling.assign(creep, source);
}
}
return this.assign(creep, Creep.action.idle);
}
// somewhere
else {
let ret = false;
// TODO: This should perhaps check which distance is greater and make this decision based on that plus its load size
if( creep.sum / creep.carryCapacity > REMOTE_HAULER.MIN_LOAD )
ret = this.goHome(creep);
else
ret = this.gotoTargetRoom(creep);
if (ret) {
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){
const targetFlag = creep.data.destiny ? Game.flags[creep.data.destiny.targetName] : null;
if (targetFlag) return Creep.action.travelling.assignRoom(creep, targetFlag.pos.roomName);
};
mod.goHome = function(creep){
return Creep.action.travelling.assignRoom(creep, creep.data.homeRoom);
};
mod.selectStrategies = function(actionName) {
return [mod.strategies.defaultStrategy, mod.strategies[actionName]];
};
mod.strategies = {
defaultStrategy: {
name: `default-${mod.name}`
},
picking: {
name: `picking-${mod.name}`,
energyOnly: false
}
};