forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creep.behaviour.upgrader.js
123 lines (123 loc) · 6.07 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
let mod = {};
module.exports = mod;
mod.name = 'upgrader';
let invalidCreep = c => ['miner', 'upgrader'].includes(c.data.creepType) && c.data.determinatedSpot &&
(c.data.ttl > c.data.spawningTime || c.data.ttl > c.data.predictedRenewal);
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 ) {
if (range === 1) {
const creeps = targetPos.lookFor(LOOK_CREEPS);
if (creeps.length && _.some(creeps, invalidCreep)) {
// forget spots that have been improperly selected/unable to move to
delete creep.data.determinatedSpot;
}
}
creep.travelTo( targetPos, {range:0} );
}
return range;
};
mod.run = function(creep) {
if( creep.room.controller.upgradeBlocked ){
creep.data.creepType='recycler';
return;
}
if( !creep.action || creep.action.name !== 'upgrading' ) Population.registerAction(creep, Creep.action.upgrading, creep.room.controller);
if( !creep.data.determinatedSpot ) {
let determineSpots = (ignoreSources=false) => {
let spots = [];
let getSpots = s => {
let args = {
spots: [{
pos: creep.room.controller.pos,
range: 3
},
{
pos: s.pos,
range: 1
}],
checkWalkable: true,
where: pos => !_.some(pos.lookFor(LOOK_CREEPS), invalidCreep) && (ignoreSources || pos.findInRange(creep.room.sources, 1).length === 0),
roomName: creep.pos.roomName
};
return Room.fieldsInRange(args);
};
let linkSpots = creep.room.structures.links.controller ? _.flatten(_.map(creep.room.structures.links.controller, getSpots)) : [];
let containerSpots = creep.room.structures.container.controller ? _.flatten(_.map(creep.room.structures.container.controller, getSpots)) : [];
let storageSpots = creep.room.storage ? getSpots(creep.room.storage) : [];
let terminalSpots = creep.room.terminal ? getSpots(creep.room.terminal) : [];
// priority = close to both link and a form of storage > close to link only > close to a form of storage only
if (linkSpots.length) {
let both = [];
if (both.length === 0 && containerSpots.length) both = _.filter(linkSpots, l => _.some(containerSpots, c => c.isEqualTo(l)));
if (both.length === 0 && storageSpots.length) both = _.filter(linkSpots, l => _.some(storageSpots, c => c.isEqualTo(l)));
if (both.length === 0 && terminalSpots.length) both = _.filter(linkSpots, l => _.some(terminalSpots, c => c.isEqualTo(l)));
return both.length ? both : linkSpots;
}
// priority: containers > storage > terminal
return containerSpots.length ? containerSpots : (storageSpots.length ? storageSpots : terminalSpots);
};
let spots = determineSpots();
if( spots.length > 0 ){
// allow spots near sources
spots = determineSpots(true);
}
if (spots.length > 0) {
// prefer off roads
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});
const speed = creep.data.body ? Math.ceil(creep.data.body.work / (2 * creep.data.body.move)) : 1; // road assumed
if( path ) creep.data.predictedRenewal = creep.data.spawningTime + (path.length * speed);
}
}
}
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( creep.room.controller && creep.pos.getRangeTo(creep.room.controller) <= 3){
let carryThreshold = (creep.data.body&&creep.data.body.work ? creep.data.body.work : (creep.carryCapacity/2));
if( creep.carry.energy <= carryThreshold ){
let store = _.find(creep.room.structures.links.controller, s => s.energy > 0 && creep.pos.isNearTo(s));
if( !store ) store = _.find(creep.room.structures.container.controller, s => s.store[RESOURCE_ENERGY] > 0 && creep.pos.isNearTo(s));
if( !store ) {
store = creep.room.storage && creep.room.storage.charge > 0 &&
creep.pos.isNearTo(creep.room.storage);
}
if( !store ) {
store = creep.room.terminal && creep.room.terminal.store[RESOURCE_ENERGY] > 0.5 * TERMINAL_ENERGY && creep.pos.isNearTo(creep.room.terminal);
}
if( store ) creep.withdraw(store, RESOURCE_ENERGY);
}
creep.controllerSign();
creep.upgradeController(creep.room.controller);
}
}
};
mod.strategies = {
defaultStrategy: {
name: `default-${mod.name}`,
moveOptions: function(options) {
return options || {};
}
}
};
mod.selectStrategies = function(actionName) {
return [mod.strategies.defaultStrategy, mod.strategies[actionName]];
};