forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreep.action.dropping.js
51 lines (51 loc) · 1.99 KB
/
creep.action.dropping.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
let action = new Creep.Action('dropping');
module.exports = action;
action.targetRange = 1;
action.reachedRange = 0;
action.isValidAction = function(creep){
return creep.sum > 0;
};
action.newTarget = function(creep) {
// drop off at drop pile or the nearest spawn
let drop = creep.pos.findClosestByRange(creep.room.structures.piles);
if( !drop ) {
drop = creep.pos.findClosestByRange(creep.room.structures.spawns);
}
if( !drop ) {
drop = creep.pos.findClosestByRange(creep.room.find(FIND_FLAGS, FlagDir.flagFilter(FLAG_COLOR.claim.spawn)));
}
if (!drop) {
drop = creep.pos.findClosestByRange(_.filter(creep.room.constructionSites, {structureType: STRUCTURE_SPAWN}));
}
if (!drop) {
drop = creep.room.controller;
}
return drop;
};
action.work = function(creep) {
let ret = OK;
let isSpawnFlag = f => f && Flag.compare(f, FLAG_COLOR.claim.spawn);
if (!(creep.target instanceof StructureSpawn || creep.target instanceof ConstructionSite
|| creep.target instanceof StructureController || isSpawnFlag(creep.target))) {
let range = creep.pos.getRangeTo(creep.target);
if( range > 0 && creep.data.lastPos && creep.data.path && !_.eq(creep.pos, creep.data.lastPos) ) {
// If the destination is walkable, try to move there before dropping
let invalidObject = o => {
return ((o.type == LOOK_TERRAIN && o.terrain == 'wall') ||
o.type == LOOK_CREEPS ||
(o.type == LOOK_STRUCTURES && OBSTACLE_OBJECT_TYPES.includes(o.structure.structureType) ));
};
let look = creep.room.lookAt(target);
if (!_.some(look, invalidObject)) {
return ret;
}
}
}
for(let resourceType in creep.carry) {
ret = creep.drop(resourceType);
}
return ret;
};
action.onAssignment = function(creep, target) {
if( SAY_ASSIGNMENT ) creep.say(String.fromCharCode(8681), SAY_PUBLIC);
};