forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creep.behaviour.privateer.js
153 lines (150 loc) · 6.17 KB
/
creep.behaviour.privateer.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
let mod = {};
module.exports = mod;
mod.name = 'privateer';
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));
}
if( creep.hits < creep.hitsMax ) { // creep injured. move to next owned room
let nextHome = Room.bestSpawnRoomFor(creep.pos.roomName);
if( nextHome )
creep.drive( nextHome.controller.pos, 3, 5);
}
};
mod.nextAction = function(creep){
let carrySum = creep.sum;
// at home
if( creep.pos.roomName == creep.data.homeRoom ){
// carrier filled
if( carrySum > 0 ){
let deposit = []; // deposit energy in...
// links?
if( creep.carry.energy == carrySum ) deposit = creep.room.structures.links.privateers;
// storage?
if( creep.room.storage ) deposit.push(creep.room.storage);
// containers?
if( creep.room.structures.container ) deposit = deposit.concat( creep.room.structures.container.privateers );
// Choose the closest
if( deposit.length > 0 ){
let target = creep.pos.findClosestByRange(deposit);
if( target.structureType == STRUCTURE_STORAGE && Creep.action.storing.assign(creep, target) ) return;
else if(Creep.action.charging.assign(creep, target) ) return;
}
//if( Creep.action.storing.assign(creep) ) return;
if( Creep.action.charging.assign(creep) ) return;
if( !creep.room.ally && Creep.action.storing.assign(creep) ) return;
Creep.behaviour.worker.nextAction(creep);
return;
}
// empty
// travelling
if( this.exploitNextRoom(creep) )
return;
else {
// no new flag
// behave as worker
Creep.behaviour.worker.nextAction(creep);
return;
}
}
// not at home
else {
// at target room
if( creep.flag && creep.flag.pos.roomName == creep.pos.roomName ){
// check invader/cloaking state
if( creep.room.situation.invasion &&
(creep.flag.color != FLAG_COLOR.invade.robbing.color || creep.flag.secondaryColor != FLAG_COLOR.invade.robbing.secondaryColor )) {
creep.flag.cloaking = 50; // TODO: set to Infinity & release when solved
this.exploitNextRoom(creep);
return;
}
// get some energy
if( creep.sum < creep.carryCapacity*0.4 ) {
// sources depleted
if( creep.room.sourceEnergyAvailable == 0 ){
// cloak flag
creep.flag.cloaking = _.max([creep.room.ticksToNextRegeneration-20,0]); // approach a bit earlier
// travelling
this.exploitNextRoom(creep);
return;
}
// energy available
else {
// harvesting or picking
var actions = [
Creep.action.dismantling,
Creep.action.picking,
Creep.action.robbing,
Creep.action.harvesting
];
// TODO: Add extracting (if extractor present) ?
for(var iAction = 0; iAction < actions.length; iAction++) {
var action = actions[iAction];
if(action.isValidAction(creep) &&
action.isAddableAction(creep) &&
action.assign(creep))
return;
}
// no targets in current room
creep.flag.cloaking = 50;
this.exploitNextRoom(creep);
return;
}
}
// carrier full
else {
var actions = [Creep.action.building];
for(var iAction = 0; iAction < actions.length; iAction++) {
var action = actions[iAction];
if(action.isValidAction(creep) &&
action.isAddableAction(creep) &&
action.assign(creep))
return;
}
Population.registerCreepFlag(creep, null);
Creep.action.travelling.assign(creep, Game.rooms[creep.data.homeRoom].controller);
return;
}
}
// not at target room
else {
this.exploitNextRoom(creep);
return;
}
}
// fallback
Creep.action.idle.assign(creep);
};
mod.exploitNextRoom = function(creep){
if( creep.sum < creep.carryCapacity*0.4 ) {
// calc by distance to home room
let validColor = flagEntry => (
(flagEntry.color == FLAG_COLOR.invade.exploit.color && flagEntry.secondaryColor == FLAG_COLOR.invade.exploit.secondaryColor) ||
(flagEntry.color == FLAG_COLOR.invade.robbing.color && flagEntry.secondaryColor == FLAG_COLOR.invade.robbing.secondaryColor)
);
let flag = FlagDir.find(validColor, Game.rooms[creep.data.homeRoom].controller.pos, false, FlagDir.exploitMod, creep.name);
// new flag found
if( flag ) {
// travelling
if( Creep.action.travelling.assign(creep, flag) ) {
Population.registerCreepFlag(creep, flag);
return true;
}
}
}
// no new flag
// go home
Population.registerCreepFlag(creep, null);
Creep.action.travelling.assign(creep, Game.rooms[creep.data.homeRoom].controller);
return false;
};