forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creep.behaviour.remoteMiner.js
133 lines (128 loc) · 5.77 KB
/
creep.behaviour.remoteMiner.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
124
125
126
127
128
129
130
131
132
133
let mod = {};
module.exports = mod;
mod.name = 'remoteMiner';
mod.run = function(creep) {
let oldTargetId = creep.data.targetId;
// assign Action
if( creep.room.name == creep.data.destiny.room ){
// if we're there, be a miner.
this.mine(creep);
return;
} else {
// else go there
Creep.action.travelling.assign(creep, Game.flags[creep.data.destiny.targetName]);
}
// 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.mine = function(creep) {
let source;
if( !creep.data.determinatedTarget ) { // select source
let notDeterminated = source => {
let hasThisSource = data => { return data.determinatedTarget == source.id && data.ttl > 100 };
let existingBranding = _.find(Memory.population, hasThisSource);
return !existingBranding;
};
source = _.find(creep.room.sources, notDeterminated);
if( source ) {
creep.data.determinatedTarget = source.id;
}
if( SAY_ASSIGNMENT ) creep.say(String.fromCharCode(9935), SAY_PUBLIC);
} else { // get dedicated source
source = Game.getObjectById(creep.data.determinatedTarget);
}
if( source ) {
if( !creep.action ) Population.registerAction(creep, Creep.action.harvesting, source);
if( !creep.data.determinatedSpot ) {
let args = {
spots: [{
pos: source.pos,
range: 1
}],
checkWalkable: true,
where: null,
roomName: creep.pos.roomName
}
let invalid = [];
let findInvalid = entry => {
if( entry.roomName == args.roomName && ['miner', 'upgrader'].includes(entry.creepType) && entry.determinatedSpot && entry.ttl > entry.spawningTime)
invalid.push(entry.determinatedSpot)
};
_.forEach(Memory.population, findInvalid);
args.where = pos => { return !_.some(invalid,{x:pos.x,y:pos.y}); };
if( source.container )
args.spots.push({
pos: source.container.pos,
range: 1
});
let 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_CONTAINER}
);
}})
if( !spot ) spot = creep.pos.findClosestByPath(spots) || spots[0];
if( spot ) creep.data.determinatedSpot = {
x: spot.x,
y: spot.y
}
}
if( !creep.data.determinatedSpot ) logError('Unable to determine working location for miner in room ' + creep.pos.roomName);
}
if( creep.data.determinatedSpot ) {
let carrying = creep.sum;
if( source.energy == 0) {
Creep.behaviour.worker.run(creep);
} else if( source.link && source.link.energy < source.link.energyCapacity ) {
if(CHATTY) creep.say('harvesting', SAY_PUBLIC);
let range = this.approach(creep);
if( range == 0 ){
if(carrying > ( creep.carryCapacity - ( creep.data.body&&creep.data.body.work ? (creep.data.body.work*2) : (creep.carryCapacity/2) )))
creep.transfer(source.link, RESOURCE_ENERGY);
creep.harvest(source);
}
} else if( source.container && source.container.sum < source.container.storeCapacity ) {
if(CHATTY) creep.say('harvesting', SAY_PUBLIC);
let range = this.approach(creep);
if( range == 0 ){
if( carrying > ( creep.carryCapacity - ( creep.data.body&&creep.data.body.work ? (creep.data.body.work*2) : (creep.carryCapacity/2) ))){
let transfer = r => { if(creep.carry[r] > 0 ) creep.transfer(source.container, r); };
_.forEach(Object.keys(creep.carry), transfer);
}
creep.harvest(source);
}
} else if( creep.room.population && creep.room.population.typeCount['hauler'] && creep.room.population.typeCount['hauler'] > 0 ) {
if(CHATTY) creep.say('dropmining', SAY_PUBLIC);
let range = this.approach(creep);
if( range == 0 ){
if( carrying > ( creep.carryCapacity -
( creep.data.body&&creep.data.body.work ? (creep.data.body.work*2) : (creep.carryCapacity/2) ))) {
if( OOPS ) creep.say(String.fromCharCode(8681), SAY_PUBLIC);
let drop = r => { if(creep.carry[r] > 0 ) creep.drop(r); };
_.forEach(Object.keys(creep.carry), drop);
}
creep.harvest(source);
}
} else {
Creep.behaviour.worker.run(creep);
}
}
}
};
mod.approach = function(creep){
let targetPos = new RoomPosition(creep.data.determinatedSpot.x, creep.data.determinatedSpot.y, creep.data.destiny.room);
let range = creep.pos.getRangeTo(targetPos);
if( range > 0 ) {
creep.drive( targetPos, 0, 0, range );
if( range <= 2 && !creep.data.predictedRenewal ) {
creep.data.predictedRenewal = _.min([500, 1500 - creep.ticksToLive + creep.data.spawningTime]);
}
}
return range;
};