forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreep.action.uncharging.js
87 lines (86 loc) · 3.59 KB
/
creep.action.uncharging.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
let action = new Creep.Action('uncharging'); // get from container
module.exports = action;
action.renewTarget = false;
action.isAddableAction = function(creep){ return true; }
action.isAddableTarget = function(target){ return true;}
action.isValidAction = function(creep){ return creep.sum < creep.carryCapacity; }
action.isValidTarget = function(target, creep){
if( !target ) return false;
if( target.structureType == 'link' ){
return target.energy > 0;
} else if( target.structureType == 'container' ) {
let min = 0;
if(target.source === true && target.controller == true) min = target.storeCapacity * MANAGED_CONTAINER_TRIGGER;
else if( creep.data.creepType.indexOf('remote') >= 0 ) min = 250;
else min = 500;
return target.sum > min;
}
return false;
};
action.newTarget = function(creep){
// if storage link is not empty & no controller link < 15% => uncharge
if( creep.room.structures.links.storage.length > 0 ){
let linkStorage = creep.room.structures.links.storage.find(l => l.energy > 0);
if( linkStorage ){
let emptyControllerLink = creep.room.structures.links.controller.find(l => l.energy < l.energyCapacity * 0.15);
if( !emptyControllerLink || linkStorage.energy <= linkStorage.energyCapacity * 0.85 ) // also clear half filled
return linkStorage;
}
}
var that = this;
if( creep.room.structures.container.in.length > 0 ) {
let min;
if( creep.data.creepType.indexOf('remote') >= 0 ) min = 250;
else min = 500;
// take from fullest IN container having energy
let target = null;
let currMax = 0;
let fullest = cont => {
if( that.isValidTarget(cont, creep) ){
let available = cont.sum;
if( cont.targetOf )
available -= _.sum( cont.targetOf.map( t => ( t.actionName == 'uncharging' ? t.carryCapacityLeft : 0 )));
if( available < Math.min(creep.carryCapacity - creep.sum, min) ) return;
if( available > currMax ){
currMax = available;
target = cont;
}
}
};
_.forEach(creep.room.structures.container.in, fullest);
return target;
}
};
action.work = function(creep){
let workResult = OK;
if( creep.target.source === true && creep.target.controller == true ) {
// managed container fun...
let max = creep.target.sum - (creep.target.storeCapacity * (1-MANAGED_CONTAINER_TRIGGER));
if( max < 1) workResult = ERR_NOT_ENOUGH_RESOURCES;
else {
let space = creep.carryCapacity - creep.sum;
let amount = _.min([creep.target.store.energy, max, space]);
creep.target._sum -= amount;
workResult = creep.withdraw(creep.target, RESOURCE_ENERGY, amount);
}
} else if (creep.target.store != null ) {
// container
let withdraw = r => {
if( creep.target.store[r] > 0 )
workResult = creep.withdraw(creep.target, r);
};
_.forEach(Object.keys(creep.target.store), withdraw);
} else { // link
workResult = creep.withdraw(creep.target, RESOURCE_ENERGY);
}
// unregister
delete creep.data.actionName;
delete creep.data.targetId;
creep.action = null;
creep.target = null;
return workResult;
};
action.onAssignment = function(creep, target) {
//if( SAY_ASSIGNMENT ) creep.say(String.fromCharCode(9738), SAY_PUBLIC);
if( SAY_ASSIGNMENT ) creep.say('\u{1F4E4}\u{FE0E}', SAY_PUBLIC);
};