-
Notifications
You must be signed in to change notification settings - Fork 10
/
stats.js
409 lines (327 loc) · 13.5 KB
/
stats.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
* @fileOverview Screeps module. Abstract base object containing data and
* functions for handling statistics.
* @author Piers Shepperson
*/
/**
* Abstract base object containing data and functions for handling statistics.
* @module raceBase
*/
"use strict";
var updateThisTicksStats = function (room) {
//console.log(room,"strange error");
if (undefined === room.memory.stats
|| room.memory.stats === null
|| !Array.isArray(room.memory.stats) )
return;
var index = room.memory.stats["ticks"].length-1;
if (undefined === room.memory.stats["ticks"])
return;
room.memory.stats["ticks"][index].controllerProgress = room.controller.progress;
room.memory.stats["ticks"][index].energyCapacityAvailable = room.energyCapacityAvailable;
room.memory.stats["ticks"][index].energyAvailable = room.energyAvailable;
var energyRemaining = 0;
var sources = room.find(FIND_SOURCES);
for (var source in sources) {
energyRemaining = energyRemaining + sources[source].energy;
}
room.memory.stats["ticks"][index].energyHarvested = room.memory.stats.sourceEnergyLastTick - energyRemaining;
var countMinerals = 0;
var minerals = room.find(FIND_MINERALS);
for (var mineral in minerals) {
countMinerals = countMinerals + minerals[mineral].mineralAmount;
}
room.memory.stats["ticks"][index].mineralsHarvested = room.memory.stats.mineralsLastTick - countMinerals;
var creeps = room.find(FIND_CREEPS);
room.memory.stats["ticks"][index].creeps = creeps.length;
var _harvester = 0;
var _upgrader = 0;
var _builder = 0;
var _repairer = 0;
var _linker = 0;
var _miner= 0;
var _spawnBuilder = 0;
var _otherRoles = 0;
for (var creep in creeps)
{
if (creeps[creep].memory === undefined)
// console.log(creep, "stats",creeps[creep]);
if (creeps[creep].memory !== undefined)
{
switch (creeps[creep].memory.role) {
case"harvester":
_harvester++; break;
case "upgrader":
_upgrader++; break;
case "builder":
_builder++; break;
case "repairer":
_repairer++; break;
case "linker":
_linker++; break;
case "miner":
_miner ++; break;
case "spawn.builder":
_spawnBuilder = _spawnBuilder + 1; break;
default:
_otherRoles++;
}
}
}
room.memory.stats["ticks"][index].harvester = _harvester;
room.memory.stats["ticks"][index].upgrader = _upgrader;
room.memory.stats["ticks"][index].builder = _builder;
room.memory.stats["ticks"][index].repairer = _repairer;
room.memory.stats["ticks"][index].linker = _linker;
room.memory.stats["ticks"][index].miner = _miner;
room.memory.stats["ticks"][index].spawnBuilder = _spawnBuilder;
room.memory.stats["ticks"][index].otherRoles = _otherRoles;
}
function RoomStatsTick (room) {
this.tick = Game.time;
this.time = new Date();
this.range = 1;
this.creepActions = stats.EMPTY_CREEP_ACTIONS;
}
function SumStatsArray (room, name) {
var lastEntry = room.memory.stats[name].length - 1;
this.tick = room.memory.stats[name][0].tick;
this.range = room.memory.stats[name][lastEntry].range * room.memory.stats[name].length;
this.controllerProgress = room.memory.stats[name][lastEntry].controllerProgress;
this.energyCapacityAvailable = room.memory.stats[name][lastEntry].energyCapacityAvailable;
this.energyAvailable = room.memory.stats[name][lastEntry].energyCapacityAvailable;
this.creepActions = { harvest : 0, build : 0, upgrade : 0, repair : 0, extract : 0 };
var _energyHarvested= 0;
var _mineralsHarvested = 0;
var _creeps = 0;
var _harvester = 0;
var _upgrader = 0;
var _builder = 0;
var _repairer = 0;
var _linker = 0;
var _miner= 0;
var _spawnBuilder = 0;
var _otherRoles = 0;
var _creepProduction = 0;
var _harvest = 0;
var _buld = 0;
var _upgrade = 0;
var _repair = 0;
var _extract= 0;
for (var stats in room.memory.stats[name]) {
_energyHarvested = _energyHarvested + room.memory.stats[name][stats].energyHarvested;
_mineralsHarvested = _mineralsHarvested + room.memory.stats[name][stats].mineralsHarvested;
_creeps = _creeps + room.memory.stats[name][stats].creeps;
_harvester = _harvester + room.memory.stats[name][stats].harvester;
_upgrader = _upgrader + room.memory.stats[name][stats].upgrader;
_builder = _builder + room.memory.stats[name][stats].builder;
_repairer = _repairer + room.memory.stats[name][stats].repairer;
_linker = _linker + room.memory.stats[name][stats].linker;
_miner = _miner + room.memory.stats[name][stats].miner;
_spawnBuilder = _spawnBuilder + room.memory.stats[name][stats].spawnBuilder;
_otherRoles = _otherRoles + room.memory.stats[name][stats].otherRoles;
_creepProduction = _creepProduction + room.memory.stats[name][stats].creepProduction;
if (undefined !== room.memory.stats[name][stats].creepActions)
{
_harvest = _harvest + room.memory.stats[name][stats].creepActions["harvest"];
_buld = _buld + room.memory.stats[name][stats].creepActions["build"];
_upgrade = _upgrade + room.memory.stats[name][stats].creepActions["upgrade"];
_repair = _repair + room.memory.stats[name][stats].creepActions["repair"];
_extract= _extract + room.memory.stats[name][stats].creepActions["extract"];
}
}
this.energyHarvested = _energyHarvested;
this.mineralsHarvested = _mineralsHarvested;
this.creeps = _creeps;
this.harvester = _harvester;
this.upgrader = _upgrader;
this.builder = _builder;
this.repairer = _repairer;
this.linker = _linker;
this.spawnBuilder = _spawnBuilder;
this.otherRoles = _otherRoles;
this.creepProduction = _creepProduction;
this.creepActions["harvest"] = _harvest;
this.creepActions["build"] = _buld;
this.creepActions["upgrade"] = _upgrade;
this.creepActions["repair"] = _repair;
this.creepActions["extract"] = _extract;
}
var stats = {
NUM_GENERATIONS_TO_STORE: 80,
TICKS_PER_TENTICKS: 10,
TENTICKS_PER_HUNDREDTICKS: 10,
HUNDREDTICKS_PER_GENERATION: 15,
NOTIFYPERIOD: 120,
on: false,
Act: {
HARVEST: "harvest",
BUILD: "build",
UPGRADE: "upgrade",
REPAIR: "repair",
EXTRACT: "extract",
TRANSFER: "transfer"
},
EMPTY_STATS: { ticks : [], tenTicks : [], hundredTicks : [], generations : [] },
EMPTY_CREEP_ACTIONS: { harvest : 0, build : 0, upgrade : 0, repair : 0, extract : 0 },
initilise: function(room) {
if (undefined === room) {
// console.log("Initilising stats: room undefiend");
return false;
} else if (undefined === room.controller) {
// console.log("Initilising stats:",room,"does not have a contoller.");
return false;
} else if ( !room.controller.my ){
// console.log("Initilising stats:",room,"does not belong to me.");
return false;
} else {
room.memory.stats = this.EMPTY_STATS;
this.rememberThisTicksResouceCounts(room);
return true;
}
},
initiliseTick: function() {
for (var name in Game.rooms) {
var room = Game.rooms[name];
// room.memory.stats = undefined;
// console.log("InicilisingStatis", room.memory.stats);
if (undefined === room.memory.stats
|| {} === room.memory.stats
|| !room.memory.stats ) {
// console.log("About to initialise stats for ",room);
if (!this.initilise(room)) {
continue;
}
}
var thisTicksStats = new RoomStatsTick(room);
// console.log(room,"stats mem",JSON.stringify( room.memory.stats));
var index = room.memory.stats["ticks"].push(thisTicksStats) -1;
}
},
upadateTick: function() {
//return;
for (var name in Game.rooms) {
var room = Game.rooms[name];
updateThisTicksStats(room);
this.rememberThisTicksResouceCounts(room);
this.updateAggregateStats(room);
}
},
rememberThisTicksResouceCounts: function(room) {
if (undefined === room.memory.stats)
return;
var energyAvalibale = 0;
var sources = room.find(FIND_SOURCES);
for (var source in sources) {
if (1 == sources[source].ticksToRegeneration) {
energyAvalibale = energyAvalibale + sources[source].energyCapacity
} else {
energyAvalibale = energyAvalibale + sources[source].energy;
}
}
room.memory.stats.sourceEnergyLastTick = energyAvalibale;
var countMinerals = 0;
var minerals = room.find(FIND_MINERALS)
for (var mineral in minerals) {
if (1 == minerals[mineral].ticksToRegeneration) {
countMinerals = undefined;
} else {
countMinerals = countMinerals + minerals[mineral].mineralAmount;
}
}
room.memory.stats.mineralsLastTick = countMinerals;
},
updateAggregateStats: function(room) {
if (undefined === room.memory.stats)
return;
if ((room.memory.stats["ticks"]).length >= this.TICKS_PER_TENTICKS) {
var tenTicksStats = new SumStatsArray(room, "ticks");
room.memory.stats["tenTicks"].push(tenTicksStats);
room.memory.stats["ticks"] = [];
}
if (room.memory.stats["tenTicks"].length >= this.TENTICKS_PER_HUNDREDTICKS) {
var hundredTicksStats = new SumStatsArray(room, "tenTicks");
room.memory.stats["hundredTicks"].push(hundredTicksStats);
room.memory.stats.tenTicks = [];
}
if (room.memory.stats.hundredTicks.length >= this.HUNDREDTICKS_PER_GENERATION) {
var generationStats = new SumStatsArray(room, "hundredTicks");
room.memory.stats["generations"].push(generationStats);
room.memory.stats.hundredTicks = [];
}
if (room.memory.stats["generations"].length > this.NUM_GENERATIONS_TO_STORE)
room.memory.stats["generations"].shift();
},
updateCreepAction: function (room, action, power) {
// if (undefined === room.memory.stats)
// return;
// var index = room.memory.stats["ticks"].length-1;
// room.memory.stats["ticks"][index].creepActions[action] =
// room.memory.stats["ticks"][index].creepActions[action] + power
},
build: function (creep,target) {
var rtv = creep.build(target);
if (OK == rtv && this.on)
this.updateCreepAction(creep.room, this.Act.BUILD, BUILD_POWER);
return rtv;
},
harvest: function(creep, target) {
var rtv = creep.harvest(target);
if (OK == rtv && this.on)
this.updateCreepAction(creep.room, this.Act.HARVEST, HARVEST_POWER);;
return rtv;
},
upgrade: function(creep, target) {
"use strict";
this.upgradeController(creep,target);
},
upgradeController: function(creep, target) {
var rtv = creep.upgradeController(target);
if (OK == rtv && this.on)
this.updateCreepAction(creep.room, this.Act.UPGRADE, UPGRADE_CONTROLLER_POWER);;
return rtv;
},
repair: function(creep, target) {
var rtv = creep.repair(target);
if (OK == rtv && this.on)
this.updateCreepAction(creep.room, this.Act.REPAIR, 1);
return rtv;
},
transfer: function(creep, target, resourceType, amount) {
return creep.transfer(target, resourceType, amount);
},
// var result = spawn.createCreep(body, undefined, {policyId: policy.id});
// if(_.isString(result)) {
// raceBase.setRole(Game.creeps[result], race.ROLE_DEFULT);
// console.log("New creep produced with name:", result,JSON.stringify(result));
createCreep: function(spawn, body, name, policy) {
//console.log(spawn);
var rtv = spawn.createCreep(body, name, {policyId: policy});
if (_.isString(rtv) && this.on) {
var raceBase = require("race.base");
var room = spawn.room;
var index = room.memory.stats["ticks"].length-1;
var energyThisCreep = raceBase.getEnergyFromBody(body);
if (undefined !== room.memory.stats["ticks"][index]) {
room.memory.stats["ticks"][index].creepProduction =
room.memory.stats["ticks"][index].creepProduction + energyThisCreep;
}
}
return rtv;
},
deleteStats: function () {
for (var name in Game.rooms) {
var room = Game.rooms[name];
room.memory.stats.ticks = undefined;
room.memory.stats.tenTicks = undefined;
room.memory.stats.hundredTicks = undefined;
room.memory.stats.generations = undefined;
room.memory.stats.sourceEnergyLastTick = undefined;
room.memory.stats.mineralsLastTick = undefined;
rooom.memory.test = undefined;
room.memory.stats = undefined;
}
}
// stats = require("stats"); stats.deleteStats();
};
module.exports = stats;