forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visuals.js
662 lines (610 loc) · 26.5 KB
/
visuals.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
const Visuals = class {};
module.exports = Visuals;
Visuals.extend = function() {
Object.defineProperties(Visuals, {
barStyle: {
configurable: true,
value: {fill: '#2B2B2B', opacity: 0.8, stroke: '#000000',},
},
sparklineStyle: {
configurable: true,
value: [{
key: 'limit',
min: Game.cpu.limit * 0.5,
max: Game.cpu.limit * 1.5,
stroke: '#808080',
opacity: 0.25,
}, {
key: 'cpu',
min: Game.cpu.limit * 0.5,
max: Game.cpu.limit * 1.5,
stroke: '#FFFF00',
opacity: 0.5,
}, {
key: 'bucket',
min: 0,
max: 10000,
stroke: '#00FFFF',
opacity: 0.5,
}],
},
tooltipStyle: {
configurable: true,
value: {align: 'left', font: 0.4,},
},
weakestStyle: {
configurable: true,
value: {radius: 0.4, fill: '#FF0000', opacity: 0.3, strokeWidth: 0,},
}
});
Visuals.drawGlobal = function() {
const vis = new RoomVisual();
const bufferWidth = 1;
const sectionWidth = 49 / 5;
const BAR_STYLE = Visuals.barStyle;
let x = bufferWidth;
const y = 2;
if (VISUALS.ROOM) {
// GCL
x = bufferWidth * 2 + sectionWidth;
vis.rect(x, y - 0.75, sectionWidth, 1, BAR_STYLE);
const GCL_PERCENTAGE = Game.gcl.progress / Game.gcl.progressTotal;
vis.rect(x, y - 0.75, GCL_PERCENTAGE * sectionWidth, 1, {
fill: getColourByPercentage(GCL_PERCENTAGE, true),
opacity: BAR_STYLE.opacity
});
vis.text(`GCL: ${Game.gcl.level} (${(GCL_PERCENTAGE * 100).toFixed(2)}%)`, x + sectionWidth / 2, y);
// CPU
x += sectionWidth + bufferWidth;
vis.rect(x, y - 0.75, sectionWidth, 1, BAR_STYLE);
const CPU_PERCENTAGE = Game.cpu.getUsed() / Game.cpu.limit;
const FUNCTIONAL_CPU_PERCENTAGE = Math.min(1, CPU_PERCENTAGE);
vis.rect(x, y - 0.75, FUNCTIONAL_CPU_PERCENTAGE * sectionWidth, 1, {
fill: getColourByPercentage(FUNCTIONAL_CPU_PERCENTAGE),
opacity: BAR_STYLE.opacity
});
vis.text(`CPU: ${(CPU_PERCENTAGE * 100).toFixed(2)}%`, x + sectionWidth / 2, y);
// Bucket
x += sectionWidth + bufferWidth;
vis.rect(x, y - 0.75, sectionWidth, 1, BAR_STYLE);
const BUCKET_PERCENTAGE = Game.cpu.bucket / 10000;
vis.rect(x, y - 0.75, BUCKET_PERCENTAGE * sectionWidth, 1, {
fill: getColourByPercentage(BUCKET_PERCENTAGE, true),
opacity: BAR_STYLE.opacity
});
vis.text(`Bucket: ${Game.cpu.bucket}`, x + sectionWidth / 2, y);
// Tick
x += sectionWidth + bufferWidth;
vis.text(`Tick: ${Game.time}`, x, y, {align: 'left'});
}
if (VISUALS.CPU) {
Visuals.drawSparkline(undefined, 1.5, 46.5, 20, 2, _.map(Memory.visualStats.cpu, (v, i) => Memory.visualStats.cpu[i]), Visuals.sparklineStyle);
}
};
Visuals.drawSparkline = function(room, x, y, w, h, values, options) {
const vis = room ? new RoomVisual(room) : new RoomVisual();
_.forEach(options, option => {
vis.poly(_.map(values, (v, i) => [x + w * (i / (values.length - 1)), y + h * (1 - (v[option.key] - option.min) / (option.max - option.min))]), option);
});
};
Visuals.collectSparklineStats = function() {
if (!_.get(Memory, 'visualStats.cpu')) {
_.set(Memory, 'visualStats.cpu', []);
}
Memory.visualStats.cpu.push({
limit: Game.cpu.limit,
bucket: Game.cpu.bucket,
cpu: Game.cpu.getUsed(),
});
if (Memory.visualStats.cpu.length >= 100) {
Memory.visualStats.cpu.shift();
}
};
Visuals.drawRoomInfo = function(room) {
const vis = new RoomVisual(room.name);
let x;
let y = 0;
// Room Name, centered middle
vis.text(`Room: ${vis.roomName}`, 24.5, ++y);
// Display bars: RCL, GCL, CPU, Bucket, Tick #
const bufferWidth = 1;
const sectionWidth = 49 / 5;
const BAR_STYLE = Visuals.barStyle;
// RCL
x = bufferWidth;
vis.rect(x, ++y - 0.75, sectionWidth, 1, BAR_STYLE);
let text;
let RCL_PERCENTAGE;
if (room.controller.level === 8) {
RCL_PERCENTAGE = 1;
text = `RCL: 8`;
} else if (room.controller.reservation) {
RCL_PERCENTAGE = 0;
text = `Reserved: ${room.controller.reservation.ticksToEnd}`;
} else if (room.controller.owner) {
RCL_PERCENTAGE = room.controller.progress / room.controller.progressTotal;
text = `RCL: ${room.controller.level} (${(RCL_PERCENTAGE * 100).toFixed(2)}%)`;
} else {
RCL_PERCENTAGE = 0;
text = `Unowned`;
}
RCL_PERCENTAGE = Math.min(1, RCL_PERCENTAGE);
vis.rect(x, y - 0.75, RCL_PERCENTAGE * sectionWidth, 1, {
fill: getColourByPercentage(RCL_PERCENTAGE, true),
opacity: BAR_STYLE.opacity
});
vis.text(text, x + sectionWidth / 2, y);
if (VISUALS.ROOM_GLOBAL) {
// New line
y += 1.5;
x = bufferWidth;
} else {
x += sectionWidth + bufferWidth;
}
// Display Creep Count, Energy Available
if (!room.controller.reservation) {
vis.rect(x, y - 0.75, sectionWidth, 1, BAR_STYLE);
const ENERGY_PERCENTAGE = room.energyAvailable / room.energyCapacityAvailable || 0;
vis.rect(x, y - 0.75, Math.min(1, ENERGY_PERCENTAGE) * sectionWidth, 1, {
fill: getColourByPercentage(ENERGY_PERCENTAGE, true),
opacity: BAR_STYLE.opacity
});
vis.text(`Energy: ${room.energyAvailable}/${room.energyCapacityAvailable} (${(ENERGY_PERCENTAGE * 100).toFixed(2)}%)`, x + sectionWidth / 2, y);
}
};
Visuals.drawSpawnInfo = function(spawn) {
if (!spawn.spawning) return;
const vis = new RoomVisual(spawn.room.name);
vis.text(`${spawn.spawning.name} (${((spawn.spawning.needTime - spawn.spawning.remainingTime) / spawn.spawning.needTime * 100).toFixed(1)}%)`, spawn.pos.x + 1, spawn.pos.y - 0.5, Visuals.tooltipStyle);
};
Visuals.drawMineralInfo = function(mineral) {
const vis = new RoomVisual(mineral.room.name);
let x = mineral.pos.x + 1;
let y = mineral.pos.y - 0.5;
if (mineral.mineralAmount) {
vis.text(`Amount: ${formatNum(mineral.mineralAmount)}`, x, y, Visuals.tooltipStyle);
} else {
vis.text(`Regen: ${formatNum(mineral.ticksToRegeneration)}`, x, y, Visuals.tooltipStyle);
}
};
Visuals.drawSourceInfo = function(source) {
const vis = new RoomVisual(source.room.name);
let x = source.pos.x + 0.5;
let y = source.pos.y - 0.5;
if (source.energy) {
vis.text(`Amount: ${source.energy}`, x, y, Visuals.tooltipStyle);
} else {
vis.text(`Regen: ${source.ticksToRegeneration}`, x, y, Visuals.tooltipStyle);
}
};
Visuals.drawControllerInfo = function(controller) {
const vis = new RoomVisual(controller.room.name);
const BASE_X = controller.pos.x + 1;
let y = controller.pos.y - 0.5;
const style = Visuals.tooltipStyle;
let line0 = `L: ${controller.level}`;
let line1 = `P: ${formatNum(controller.progress)}/${formatNum(controller.progressTotal)} (${(controller.progress / controller.progressTotal * 100).toFixed(2)}%)`;
let line2 = `D: ${formatNum(controller.ticksToDowngrade)}`;
if (controller.level === 8) {
line1 = undefined;
} else if (controller.reservation) {
line0 = 'L: Reserved';
line1 = `P: ${controller.reservation.username}`;
line2 = `D: ${controller.reservation.ticksToEnd}`;
} else if (!controller.owner) {
return;
}
vis.text(line0, BASE_X, y, style);
if (line1) {
vis.text(line1, BASE_X, y += 0.4, style);
}
if (controller.ticksToDowngrade < CONTROLLER_DOWNGRADE[controller.level] || controller.reservation) {
let downgradeStyle = Object.assign({}, style, {color: '#FF0000'});
vis.text(line2, BASE_X, y += 0.4, downgradeStyle);
}
};
Visuals.highlightWeakest = function(room, type) {
const vis = new RoomVisual(room.name);
let weakest = _(room.find(FIND_STRUCTURES)).filter(s => s.structureType === type).min(s => s.hits);
if (weakest && weakest.pos) {
vis.circle(weakest.pos.x, weakest.pos.y, Visuals.weakestStyle);
let y = weakest.pos.y - 0.5; // base y pos - consistent with spawns, labs, and controllers
const look = weakest.pos.lookFor(LOOK_STRUCTURES);
const towers = _.find(look, o => o instanceof StructureTower);
if (towers && VISUALS.TOWER) {
y += 0.4;
} else {
const spawns = _.find(look, o => o instanceof StructureSpawn && o.spawning);
if (spawns && VISUALS.SPAWN) {
// if structure shares a position with a spawn (road, rampart), lower to next line
// spawn must be spawning, and spawn visuals must be enabled
y += 0.4;
} else {
const labs = _.find(look, o => o instanceof StructureLab);
if (labs && VISUALS.LABS) {
// same as spawns, move the weakest structure text until it's on its own line
if (labs.energy) y += 0.4;
if (labs.mineralAmount) y += 0.4;
if (labs.cooldown) y += 0.4;
}
}
}
vis.text(`H: ${formatNum(weakest.hits)} (${(weakest.hits / weakest.hitsMax * 100).toFixed(2)}%)`, weakest.pos.x + 1, y, Visuals.tooltipStyle);
}
};
Visuals.drawRoomOrders = function(room) {
const vis = new RoomVisual(room.name);
const x = 43;
let y = 4.5;
if (!room.memory.resources || !room.memory.resources.orders) {
return;
}
if (VISUALS.STORAGE && room.storage) {
y += 2 + _.size(room.storage.store) * 0.6;
}
if (VISUALS.TERMINAL && room.terminal) {
y += 2 + _.size(room.terminal.store) * 0.6;
}
vis.text('Room Orders', x, ++y, {align: 'left'});
for (let order of room.memory.resources.orders) {
vis.text(`${order.type}: ${formatNum(order.amount)}`, x, y += 0.6, Object.assign({color: getResourceColour(order.type)}, Visuals.tooltipStyle));
}
};
Visuals.drawRoomOffers = function(room) {
const vis = new RoomVisual(room.name);
const x = 43;
let y = 4.5;
if (!room.memory.resources || !room.memory.resources.offers) {
return;
}
if (VISUALS.STORAGE && room.storage) {
y += 2 + _.size(room.storage.store) * 0.6;
}
if (VISUALS.TERMINAL && room.terminal) {
y += 2 + _.size(room.terminal.store) * 0.6;
}
if (VISUALS.ROOM_ORDERS && room.memory.resources.orders) {
y += 2 + _.size(room.memory.resources.orders) * 0.6;
}
vis.text('Room Offerings', x, ++y, {align: 'left'});
for (let offer of room.memory.resources.offers) {
vis.text(`${offer.type}: ${formatNum(offer.amount)} (to ${offer.room})`, x, y += 0.6, Object.assign({color: getResourceColour(offer.type)}, Visuals.tooltipStyle));
}
};
Visuals.storage = function(room) {
if (room.storage) {
const vis = new RoomVisual(room.name);
const x = 43;
let y = 4.5;
vis.text('Storage Contents', x, ++y, {align: 'left'});
storageObject(vis, room.storage.store, x, y);
}
};
Visuals.terminal = function(room) {
if (room.terminal) {
const vis = new RoomVisual(room.name);
const x = 43;
let y = 4.5;
if (VISUALS.STORAGE && room.storage) {
y += 2 + _.size(room.storage.store) * 0.6;
}
vis.text('Terminal Contents', x, ++y, {align: 'left'});
storageObject(vis, room.terminal.store, x, y);
}
};
Visuals.drawTowerInfo = function(tower) {
const vis = new RoomVisual(tower.room.name);
vis.text(`E: ${tower.energy}/${tower.energyCapacity}`, tower.pos.x + 1, tower.pos.y - 0.5, Visuals.tooltipStyle);
};
Visuals.drawTransactions = function(room) {
if (room.terminal) {
const vis = new RoomVisual(room.name);
const x = room.terminal.pos.x;
let y = room.terminal.pos.y - 1;
const transactions = _(Game.market.incomingTransactions)
.concat(Game.market.outgoingTransactions)
.filter(transaction => transaction.from === room.name || transaction.to === room.name)
.sortBy('time')
.reverse()
.slice(0, 2)
.value();
if (transactions.length === 0) {
return;
}
if (transactions.length === 2) {
y -= 0.4;
}
transactions.forEach(transaction => {
const outgoing = transaction.sender.username === room.controller.owner.username;
const toSelf = transaction.recipient ? transaction.sender.username === transaction.recipient.username : false;
const receiving = room.name === transaction.to;
const colour = outgoing || receiving ? '#00FF00' : '#FF0000';
const prefix = outgoing ? '+' : '-';
let text = '';
if (toSelf || !transaction.order) {
const roomName = receiving ? transaction.from : transaction.to;
text = `${roomName} : ${transaction.amount} ${transaction.resourceType}`;
} else {
text = `${prefix}${transaction.amount * transaction.order.price}`;
}
//const detailedText = `${prefix}${transaction.amount * transaction.order.price} : ${transaction.resourceType} * ${transaction.amount}`;
vis.text(text, x, y, {font: Visuals.tooltipStyle.font, color: colour,});
y += 0.4;
});
}
};
Visuals.drawLabs = function(room) {
const vis = new RoomVisual(room.name);
for (let lab of room.structures.labs.all) {
if (lab.energy || lab.mineralAmount || lab.cooldown) {
const x = lab.pos.x + 0.8;
let y = lab.pos.y - 0.5;
if (lab.energy) {
vis.text(`E: ${formatNum(lab.energy)}`, x, y, Object.assign({color: getResourceColour(RESOURCE_ENERGY)}, Visuals.tooltipStyle));
}
if (lab.mineralAmount) {
vis.text(`M: ${lab.mineralType} (${formatNum(lab.mineralAmount)})`, x, y += 0.4, Object.assign({color: getResourceColour(lab.mineralType)}, Visuals.tooltipStyle));
}
if (lab.cooldown) {
vis.text(`C: ${lab.cooldown}`, x, y += 0.4, Object.assign({color: '#FF0000'}, Visuals.tooltipStyle));
}
}
}
};
Visuals.setHeatMapData = function(room) {
if (room.memory.heatmap === undefined) {
room.memory.heatmap = {};
for (let x = 0; x < 50; x++) {
for (let y = 0; y < 50; y++) {
const look = room.lookForAt(LOOK_TERRAIN, x, y);
if (look && look[0] === 'wall') continue;
const key = `${String.fromCharCode(32 + x)}${String.fromCharCode(32 + y)}_x${x}-y${y}`;
room.memory.heatmap[key] = 0;
}
}
}
room.creeps.filter(creep => !creep.spawning).forEach(creep => {
const x = creep.pos.x;
const y = creep.pos.y;
const key = `${String.fromCharCode(32 + x)}${String.fromCharCode(32 + y)}_x${x}-y${y}`;
room.memory.heatmap[key]++;
});
};
Visuals.drawHeatMapData = function(room) {
const vis = new RoomVisual(room.name);
const data = Object.keys(room.memory.heatmap).map(k => {
return {
n: room.memory.heatmap[k],
x: k.charCodeAt(0) - 32,
y: k.charCodeAt(1) - 32,
};
});
const MAP_DATA = _.filter(data, d => d.n > 0);
const PERCENTAGE_MAX = _.sum(MAP_DATA, d => d.n) / MAP_DATA.length * 2;
MAP_DATA.forEach(d => {
const PERCENTAGE = d.n / PERCENTAGE_MAX;
const colour = getColourByPercentage(PERCENTAGE > 1 ? 1 : PERCENTAGE);
vis.rect(d.x - 0.5, d.y - 0.5, 1, 1, {fill: colour});
});
};
Visuals.creepPathStyle = function(creep) {
function randomColour() {
let c = '#';
while (c.length < 7) {
c += (Math.random()).toString(16).substr(-6).substr(-1);
}
return c;
}
creep.data.pathColour = creep.data.pathColour ? creep.data.pathColour : randomColour(); // set colour in memory. Makes tracking easier, and prevents rainbows.
return {
width: 0.15,
color: creep.data.pathColour,
lineStyle: 'dashed',
};
};
Visuals.drawCreepPath = function(room) {
const vis = new RoomVisual(room.name);
room.creeps.forEach(creep => {
if (creep.action === Creep.action.idle) return; // don't draw idle path
if (_.chain(creep.pos).pick(['x','y']).eq(creep.data.determinatedSpot).value()) return;
if (creep.memory && creep.memory._travel && creep.memory._travel.path) {
const path = creep.memory._travel.path.substr(1);
const style = Visuals.creepPathStyle(creep);
let x = creep.pos.x;
let y = creep.pos.y;
if (creep.fatigue === 0) {
const initDirection = +creep.memory._travel.path[0]; // get initial so we know where to set the start (x, y)
if (initDirection === TOP) {
--y;
} else if (initDirection === TOP_RIGHT) {
++x;
--y;
} else if (initDirection === RIGHT) {
++x;
} else if (initDirection === BOTTOM_RIGHT) {
++x;
++y;
} else if (initDirection === BOTTOM) {
++y;
} else if (initDirection === BOTTOM_LEFT) {
--x;
++y;
} else if (initDirection === LEFT) {
--x;
} else if (initDirection === TOP_LEFT) {
--x;
--y;
} else {
// we're not supposed to be here.
}
}
for (let direction of path) {
direction = +direction; // force coerce to number
if (direction === TOP) {
vis.line(x, y, x, --y, style);
} else if (direction === TOP_RIGHT) {
vis.line(x, y, ++x, --y, style);
} else if (direction === RIGHT) {
vis.line(x, y, ++x, y, style);
} else if (direction === BOTTOM_RIGHT) {
vis.line(x, y, ++x, ++y, style);
} else if (direction === BOTTOM) {
vis.line(x, y, x, ++y, style);
} else if (direction === BOTTOM_LEFT) {
vis.line(x, y, --x, ++y, style);
} else if (direction === LEFT) {
vis.line(x, y, --x, y, style);
} else if (direction === TOP_LEFT) {
vis.line(x, y, --x, --y, style);
} else {
// we're not supposed to be here.
}
}
}
});
};
Visuals.drawLine = function(from, to, style) {
if (from instanceof RoomObject) from = from.pos;
if (to instanceof RoomObject) to = to.pos;
if (!(from instanceof RoomPosition || to instanceof RoomPosition)) throw new Error('Visuals: Point not a Room Position');
if (from.roomName !== to.roomName) return;
const vis = new RoomVisual(from.roomName);
style = style instanceof Creep
? Visuals.creepPathStyle(style)
: (style || {});
vis.line(from, to, style);
};
};
Visuals.run = function() {
const p = Util.startProfiling('Visuals', {enabled:PROFILING.VISUALS});
for (let roomName in Game.rooms) {
const room = Game.rooms[roomName];
if (!ROOM_VISUALS_ALL && !room.my) continue;
if (!room.controller) continue;
const p2 = Util.startProfiling('Visuals:' + roomName, {enabled:PROFILING.VISUALS});
if (Memory.heatmap === undefined) Memory.heatmap = false;
if (VISUALS.HEATMAP) {
if (Game.time % VISUALS.HEATMAP_INTERVAL === 0) {
Visuals.setHeatMapData(room);
p2.checkCPU('Heatmap.set', PROFILING.VISUALS_LIMIT);
}
if (Memory.heatmap) {
Visuals.drawHeatMapData(room);
p2.checkCPU('Heatmap.draw', PROFILING.VISUALS_LIMIT);
continue;
}
}
if (VISUALS.ROOM) {
Visuals.drawRoomInfo(room, VISUALS.ROOM_GLOBAL);
p2.checkCPU('Room Info', PROFILING.VISUALS_LIMIT);
}
if (VISUALS.ROOM_ORDERS) {
Visuals.drawRoomOrders(room);
p2.checkCPU('Room Orders', PROFILING.VISUALS_LIMIT);
}
if (VISUALS.ROOM_OFFERS) {
Visuals.drawRoomOffers(room);
p2.checkCPU('Room Offers', PROFILING.VISUALS_LIMIT);
}
if (VISUALS.CONTROLLER) {
Visuals.drawControllerInfo(room.controller);
p2.checkCPU('Controller', PROFILING.VISUALS_LIMIT);
}
if (VISUALS.SPAWN) {
room.structures.spawns.filter(s => s.spawning).forEach(Visuals.drawSpawnInfo);
p2.checkCPU('Spawns', PROFILING.VISUALS_LIMIT);
}
if (VISUALS.MINERAL) {
let [mineral] = room.minerals;
if (mineral) {
Visuals.drawMineralInfo(mineral);
p2.checkCPU('Mineral', PROFILING.VISUALS_LIMIT);
}
}
if (VISUALS.SOURCE) {
room.sources.forEach(Visuals.drawSourceInfo);
p2.checkCPU('Sources', PROFILING.VISUALS_LIMIT);
}
if (VISUALS.WALL) {
Visuals.highlightWeakest(room, STRUCTURE_WALL);
p2.checkCPU('Walls', PROFILING.VISUALS_LIMIT);
}
if (VISUALS.RAMPART) {
Visuals.highlightWeakest(room, STRUCTURE_RAMPART);
p2.checkCPU('Ramparts', PROFILING.VISUALS_LIMIT);
}
if (VISUALS.ROAD) {
Visuals.highlightWeakest(room, STRUCTURE_ROAD);
p2.checkCPU('Roads', PROFILING.VISUALS_LIMIT);
}
if (VISUALS.STORAGE) {
Visuals.storage(room);
p2.checkCPU('Storage', PROFILING.VISUALS_LIMIT);
}
if (VISUALS.TERMINAL) {
Visuals.terminal(room);
p2.checkCPU('Terminal', PROFILING.VISUALS_LIMIT);
}
if (VISUALS.TOWER) {
room.structures.towers.forEach(Visuals.drawTowerInfo);
p2.checkCPU('Towers', PROFILING.VISUALS_LIMIT);
}
if (VISUALS.TRANSACTIONS) {
Visuals.drawTransactions(room);
p2.checkCPU('Transactions', PROFILING.VISUALS_LIMIT);
}
if (VISUALS.LABS) {
Visuals.drawLabs(room);
p2.checkCPU('Labs', PROFILING.VISUALS_LIMIT);
}
if (VISUALS.CREEP) {
Visuals.drawCreepPath(room);
p2.checkCPU('Creep Paths', PROFILING.VISUALS_LIMIT);
}
}
p.checkCPU('Total for all rooms', PROFILING.VISUALS_LIMIT);
if (VISUALS.ROOM_GLOBAL) {
if (VISUALS.CPU) {
Visuals.collectSparklineStats();
p.checkCPU('CPU Sparklines', PROFILING.VISUALS_LIMIT);
}
Visuals.drawGlobal();
p.checkCPU('Global', PROFILING.VISUALS_LIMIT);
}
};
function formatNum(n) {
if (n >= 1000000) {
return (n / 1000000).toFixed(2) + 'M';
} else if (n >= 1000) {
return (n / 1000).toFixed(1) + 'K';
}
return n;
}
function storageObject(vis, store, x, startY) {
Object.keys(store).forEach(resource => vis.text(`${resource}: ${formatNum(store[resource])}`, x, startY += 0.6, Object.assign({color: getResourceColour(resource)}, Visuals.tooltipStyle)));
}
function getResourceColour(resourceType) {
const BASE = {
[RESOURCE_ENERGY]: '#FFE56D',
[RESOURCE_POWER]: '#FF0000',
[RESOURCE_CATALYST]: '#FF7A7A',
[RESOURCE_GHODIUM]: '#FFFFFF',
[RESOURCE_HYDROGEN]: '#CCCCCC',
[RESOURCE_KEANIUM]: '#9370FF',
[RESOURCE_LEMERGIUM]: '#89F4A5',
[RESOURCE_OXYGEN]: '#CCCCCC',
[RESOURCE_UTRIUM]: '#88D6F7',
[RESOURCE_ZYNTHIUM]: '#F2D28B',
};
let colour = BASE[resourceType];
if (!colour) {
let compoundType = [RESOURCE_UTRIUM, RESOURCE_LEMERGIUM, RESOURCE_KEANIUM, RESOURCE_ZYNTHIUM, RESOURCE_GHODIUM, RESOURCE_HYDROGEN, RESOURCE_OXYGEN].find(type => resourceType.indexOf(type) !== -1);
colour = BASE[compoundType];
}
return colour;
}
function getColourByPercentage(percentage, reverse = false) {
const value = reverse ? percentage : 1 - percentage;
const hue = (value * 120).toString(10);
return `hsl(${hue}, 100%, 50%)`;
}