forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreep.behaviour.upgrader.js
99 lines (98 loc) · 4.17 KB
/
creep.behaviour.upgrader.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
let mod = {};
module.exports = mod;
mod.name = 'upgrader';
mod.approach = function(creep){
let targetPos = new RoomPosition(creep.data.determinatedSpot.x, creep.data.determinatedSpot.y, creep.pos.roomName);
let range = creep.pos.getRangeTo(targetPos);
if( range > 0 )
creep.drive( targetPos, 0, 0, range );
return range;
};
mod.run = function(creep) {
if( creep.room.controller.upgradeBlocked ){
creep.data.creepType='recycler';
return;
}
if( !creep.action ) Population.registerAction(creep, Creep.action.upgrading, creep.room.controller);
if( !creep.data.determinatedSpot ) {
let args = {
spots: [{
pos: creep.room.controller.pos,
range: 3
}],
checkWalkable: true,
where: null,
roomName: creep.pos.roomName
}
let addSpot = s => args.spots.push({
pos: s.pos,
range: 1
});
if( creep.room.structures.container.controller ){
creep.room.structures.container.controller.forEach(addSpot);
}
if( creep.room.structures.links.controller ){
creep.room.structures.links.controller.forEach(addSpot);
}
// dont take already taken
let taken = [];
let findInvalid = entry => {
if( entry.roomName == args.roomName && ['miner', 'upgrader'].includes(entry.creepType) && entry.determinatedSpot && entry.ttl > entry.spawningTime)
taken.push(entry.determinatedSpot)
};
_.forEach(Memory.population, findInvalid);
// dont take miner spots
let invalid = taken.slice(0);
let sourcesInRange = creep.room.controller.pos.findInRange(creep.room.sources, 4);
let addAdjacent = source => source.pos.adjacent.forEach(pos => invalid.push({x:pos.x,y:pos.y}))
sourcesInRange.forEach(addAdjacent);
args.where = pos => { return !_.some(invalid,{x:pos.x,y:pos.y}); };
let spots = Room.fieldsInRange(args);
if( spots.length == 0 ){
// no position found. allow pos near sources
args.where = pos => { return !_.some(taken,{x:pos.x,y:pos.y}); };
spots = Room.fieldsInRange(args);
}
if( spots.length == 0 ){
// no position found. allow any
delete args.where;
spots = Room.fieldsInRange(args);
}
if( spots.length > 0 ){
let spot = creep.pos.findClosestByPath(spots, {filter: pos => {
return !_.some(
creep.room.lookForAt(LOOK_STRUCTURES, pos),
{'structureType': STRUCTURE_ROAD }
);
}})
if( !spot ) spot = creep.pos.findClosestByPath(spots) || spots[0];
if( spot ) {
creep.data.determinatedSpot = {
x: spot.x,
y: spot.y
}
let spawn = Game.spawns[creep.data.motherSpawn];
if( spawn ) {
let path = spot.findPathTo(spawn, {ignoreCreeps: true});
if( path ) creep.data.predictedRenewal = creep.data.spawningTime + path.length; // road assumed
}
}
}
if( !creep.data.determinatedSpot ) logError('Unable to determine working location for upgrader in room ' + creep.pos.roomName);
else if( SAY_ASSIGNMENT ) creep.say(String.fromCharCode(9962), SAY_PUBLIC);
}
if( creep.data.determinatedSpot ) {
if(CHATTY) creep.say('upgrading', SAY_PUBLIC);
let range = this.approach(creep);
if( range == 0 ){
let carryThreshold = (creep.data.body&&creep.data.body.work ? creep.data.body.work : (creep.carryCapacity/2));
if( creep.carry.energy <= carryThreshold ){
let store = creep.room.structures.links.controller.find(l => l.energy > 0);
if( !store ) store = creep.room.structures.container.controller.find(l => l.store.energy > 0);
if( store ) creep.withdraw(store, RESOURCE_ENERGY);
}
creep.controllerSign();
creep.upgradeController(creep.room.controller);
}
}
};