forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creep.behaviour.pioneer.js
57 lines (54 loc) · 2.29 KB
/
creep.behaviour.pioneer.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
let mod = {};
module.exports = mod;
mod.name = 'pioneer';
mod.run = function(creep) {
// Assign next Action
let oldTargetId = creep.data.targetId;
if( creep.action == null || creep.action.name == 'idle') {
if( creep.data.destiny && creep.data.destiny.task && Task[creep.data.destiny.task] && Task[creep.data.destiny.task].nextAction )
Task[creep.data.destiny.task].nextAction(creep);
else this.nextAction(creep);
}
// 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.nextAction = function(creep) {
var flag;
if( creep.data.destiny ) flag = Game.flags[creep.data.destiny.flagName];
if( flag ) {
// not at target room
if( !flag.room || flag.pos.roomName != creep.pos.roomName ){
// travel to target room
if( Creep.action.travelling.assign(creep, flag)) {
Population.registerCreepFlag(creep, flag);
return true;
}
}
// if target room claimed
if( flag.room && flag.room.controller.my ) {
let spawnFlag = FlagDir.find(FLAG_COLOR.claim.spawn, creep.pos, true) ;
// and has spawn flag
if( spawnFlag ) {
// but spawn is complete
if( spawnFlag.room.structures.spawns && spawnFlag.room.structures.spawns.length > 0 ){
// remove spawn flag
spawnFlag.remove();
// also remove exploit flags
let remove = f => Game.flags[f.name].remove();
_.forEach(FlagDir.filter(FLAG_COLOR.invade.exploit, spawnFlag.pos, true), remove);
}
else { // no spawn => build it
let spawnSite = flag.room.constructionSites.some(s => s.structureType === STRUCTURE_SPAWN);
if( !spawnSite ) // no spawn construction site yet
flag.room.createConstructionSite(spawnFlag, STRUCTURE_SPAWN); // create spawn construction site
}
}
}
}
// else run as worker
Creep.behaviour.worker.nextAction(creep);
};