forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopulation.js
259 lines (248 loc) · 9.99 KB
/
population.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
let mod = {};
module.exports = mod;
mod.getCreep = function(creepName) {
return Memory.population[creepName];
};
mod.setCreep = function(val) {
Memory.population[val.creepName] = val;
return Memory.population[val.creepName];
};
mod.registerCreep = function(creepName, creepType, creepCost, room, spawnName, body, destiny = null){
var entry = this.setCreep({
creepName: creepName,
creepType: creepType,
weight: creepCost,
roomName: room.name,
homeRoom: room.name,
motherSpawn: spawnName,
actionName: null,
targetId: null,
spawningTime: 0,
flagName: null,
body: _.countBy(body),
destiny: destiny
});
this.countCreep(room, entry);
};
mod.unregisterCreep = function(creepName){
delete Memory.population[creepName];
delete Memory.creeps[creepName];
};
mod.registerAction = function(creep, action, target, entry) {
if( entry === undefined ) entry = this.getCreep(creep.name);
entry.carryCapacityLeft = creep.carryCapacity - creep.sum;
let room = creep.room;
if( room.population === undefined ) {
room.population = {
typeCount: {},
typeWeight: {},
actionCount: {},
actionWeight: {}
};
}
if( creep.action ){
// unregister action
if( room.population.actionCount[creep.action.name] === undefined )
room.population.actionCount[creep.action.name] = 0;
else room.population.actionCount[creep.action.name]--;
if( room.population.actionWeight[creep.action.name] === undefined )
room.population.actionWeight[creep.action.name] = 0;
else room.population.actionWeight[creep.action.name] -= entry.weight;
if( this.actionCount[creep.action.name] === undefined )
this.actionCount[creep.action.name] = 0;
else this.actionCount[creep.action.name]--;
if( this.actionWeight[creep.action.name] === undefined )
this.actionWeight[creep.action.name] = 0;
else this.actionWeight[creep.action.name] -= entry.weight;
}
// register action
entry.actionName = action.name;
if( room.population.actionCount[action.name] === undefined )
room.population.actionCount[action.name] = 1;
else room.population.actionCount[action.name]++;
if( room.population.actionWeight[action.name] === undefined )
room.population.actionWeight[action.name] = entry.weight;
else room.population.actionWeight[action.name] += entry.weight;
if( this.actionCount[action.name] === undefined )
this.actionCount[action.name] = 1;
else this.actionCount[action.name]++;
if( this.actionWeight[action.name] === undefined )
this.actionWeight[action.name] = entry.weight;
else this.actionWeight[action.name] += entry.weight;
let targetId = target.id || target.name;
let oldTargetId;
if( entry.targetId ) {
// unregister target
let oldTarget = entry.targetId ? Game.getObjectById(entry.targetId) || Game.spawns[entry.targetId] || Game.flags[entry.targetId] : null;
if( oldTarget ){
oldTargetId = oldTarget.id || oldTarget.name;
if( oldTarget.targetOf ) {
let byName = elem => elem.creepName === creep.name;
let index = oldTarget.targetOf.findIndex(byName);
if( index > -1 ) oldTarget.targetOf.splice(index, 1);
}
}
}
// register target
entry.targetId = targetId;
if( target ) {
if( target.targetOf === undefined )
target.targetOf = [entry];
else target.targetOf.push(entry);
}
// clear saved path
if( targetId != oldTargetId ) {
delete entry.path;
}
creep.action = action;
creep.target = target;
creep.data = entry;
};
mod.registerCreepFlag = function(creep, flag) {
if( flag && creep.data && creep.data.flagName && creep.data.flagName == flag.name && creep.flag.name == flag.name )
return;
if( creep.data && creep.data.flagName ){
// unregister flag
let oldFlag = Game.flags[creep.data.flagName];
if( oldFlag && oldFlag.targetOf ){
let byName = elem => elem.creepName === creep.name;
let index = oldFlag.targetOf.findIndex(byName);
if( index > -1 ) oldFlag.targetOf.splice(index, 1);
}
}
if( !flag ) {
delete creep.data.flagName;
delete creep.flag;
}
else {
if( flag.targetOf === undefined ) flag.targetOf = [creep.data];
else flag.targetOf.push(creep.data);
creep.flag = flag;
creep.data.flagName = flag.name;
}
};
mod.countCreep = function(room, entry){
entry.roomName = room.name;
if( room.population === undefined ) {
room.population = {
typeCount: {},
typeWeight: {},
actionCount: {},
actionWeight: {}
};
}
if( room.population.typeCount[entry.creepType] === undefined )
room.population.typeCount[entry.creepType] = 1;
else room.population.typeCount[entry.creepType]++;
if( room.population.typeWeight[entry.creepType] === undefined )
room.population.typeWeight[entry.creepType] = entry.weight;
else room.population.typeWeight[entry.creepType] += entry.weight;
if( this.typeCount[entry.creepType] === undefined )
this.typeCount[entry.creepType] = 1;
else this.typeCount[entry.creepType]++;
if( this.typeWeight[entry.creepType] === undefined )
this.typeWeight[entry.creepType] = entry.weight;
else this.typeWeight[entry.creepType] += entry.weight;
};
mod.flush = function() {
this.typeCount = {};
this.typeWeight = {};
this.actionCount = {};
this.actionWeight = {};
this.died = [];
this.predictedRenewal = [];
this.spawned = [];
this.spawnsToProbe = [];
if(_.isUndefined(Memory.population)) {
Memory.population = {};
}
};
mod.analyze = function(){
let register = entry => {
let creep = Game.creeps[entry.creepName];
if ( !creep ) {
if(CENSUS_ANNOUNCEMENTS) global.logSystem(entry.homeRoom, dye(CRAYON.death, 'Good night ' + entry.creepName + '!') );
this.died.push(entry.creepName);
}
else {
creep.data = entry;
delete creep.action;
delete creep.target;
delete creep.flag;
if( creep.spawning ) { // count spawning time
entry.spawningTime++;
}
else if( creep.ticksToLive == ( creep.data.body.claim !== undefined ? 499 : 1499 ) ){ // spawning complete
this.spawned.push(entry.creepName);
if (Game.spawns[entry.motherSpawn]) this.spawnsToProbe.push(entry.motherSpawn); // only push living spawns
}
else if(creep.ticksToLive == ( entry.predictedRenewal ? entry.predictedRenewal : entry.spawningTime)) { // will die in ticks equal to spawning time or custom
if(CENSUS_ANNOUNCEMENTS) console.log(dye(CRAYON.system, entry.creepName + ' > ') + dye(CRAYON.death, 'Farewell!') );
this.predictedRenewal.push(creep.name);
if( !this.spawnsToProbe.includes(entry.motherSpawn) && entry.motherSpawn != 'unknown' && Game.spawns[entry.motherSpawn] ) {
this.spawnsToProbe.push(entry.motherSpawn);
}
}
entry.ttl = creep.ticksToLive;
if( entry.creepType &&
( creep.ticksToLive === undefined ||
creep.ticksToLive > entry.spawningTime )) {
this.countCreep(creep.room, entry);
}
if( entry.flagName ){
var flag = Game.flags[entry.flagName];
if( !flag )
delete entry.flagName;
else {
if( flag.targetOf === undefined ) flag.targetOf = [entry];
else flag.targetOf.push(entry);
creep.flag = flag;
}
}
let action = ( entry.actionName && Creep.action[entry.actionName] ) ? Creep.action[entry.actionName] : null;
let target = action && entry.targetId ? Game.getObjectById(entry.targetId) || Game.spawns[entry.targetId] || Game.flags[entry.targetId] : null;
if( action && target ) this.registerAction( creep, action, target, entry );
else {
delete entry.actionName;
delete entry.targetId;
creep.action = null;
creep.target = null;
}
creep.data = entry;
}
};
_.forEach(Memory.population, register);
let validateAssignment = entry => {
let creep = Game.creeps[entry.creepName];
if( creep && creep.action && creep.target) {
let oldId = creep.target.id || creep.target.name;
let target = creep.action.validateActionTarget(creep, creep.target);
if( !target ) {
delete entry.actionName;
delete entry.targetId;
creep.action = null;
creep.target = null;
} else if( oldId != target.id || target.name ) {
this.registerAction( creep, creep.action, target, entry );
}
}
}
_.forEach(Memory.population, validateAssignment);
};
mod.execute = function(){
let triggerCompleted = name => Creep.spawningCompleted.trigger(Game.creeps[name]);
this.spawned.forEach(triggerCompleted);
// Creep.died.on(n => console.log(`Creep ${n} died!`));
let triggerDied = name => Creep.died.trigger(name);
this.died.forEach(triggerDied);
let triggerRenewal = name => Creep.predictedRenewal.trigger(Game.creeps[name]);
this.predictedRenewal.forEach(triggerRenewal);
if( Game.time % SPAWN_INTERVAL != 0 ) {
let probeSpawn = spawnName => Game.spawns[spawnName].execute();
this.spawnsToProbe.forEach(probeSpawn);
}
};
mod.cleanup = function(){
let unregister = name => Population.unregisterCreep(name);
this.died.forEach(unregister);
};