forked from KirkMcDonald/kirkmcdonald.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.js
1280 lines (1209 loc) · 42.1 KB
/
display.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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*Copyright 2015-2019 Kirk McDonald
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
"use strict"
function formatName(name) {
name = name.replace(new RegExp("-", 'g'), " ")
return name[0].toUpperCase() + name.slice(1)
}
function displayRate(x) {
x = x.mul(displayRateFactor)
if (displayFormat == "rational") {
return x.toMixed()
} else {
return x.toDecimal(ratePrecision)
}
}
function displayCount(x) {
if (displayFormat == "rational") {
return x.toMixed()
} else {
return x.toUpDecimal(countPrecision)
}
}
function align(s, prec) {
if (displayFormat == "rational") {
return s
}
var idx = s.indexOf(".")
if (idx == -1) {
idx = s.length
}
var toAdd = prec - s.length + idx
if (prec > 0) {
toAdd += 1
}
while (toAdd > 0) {
s += "\u00A0"
toAdd--
}
return s
}
function alignRate(x) {
return align(displayRate(x), ratePrecision)
}
function alignCount(x) {
return align(displayCount(x), countPrecision)
}
var powerSuffixes = ["\u00A0W", "kW", "MW", "GW", "TW", "PW"]
function alignPower(x, prec) {
if (prec === undefined) {
prec = countPrecision
}
var thousand = RationalFromFloat(1000)
var i = 0
while (thousand.less(x) && i < powerSuffixes.length - 1) {
x = x.div(thousand)
i++
}
return align(displayCount(x), prec) + " " + powerSuffixes[i]
}
var sortOrder = "topo"
function pruneSpec(totals) {
var drop = []
for (var name in spec.spec) {
if (!(name in totals.totals)) {
drop.push(name)
}
}
for (var i = 0; i < drop.length; i++) {
delete spec.spec[drop[i]]
}
drop = []
for (var name in spec.ignore) {
if (!(name in totals.totals)) {
drop.push(name)
}
}
for (var i = 0; i < drop.length; i++) {
delete spec.ignore[drop[i]]
}
}
var globalTotals
// The main top-level calculation function. Called whenever the solution
// requires recalculation.
//
// This function obtains the set of item-rates to solve for from build_targets,
// the set of modules from spec, and obtains a solution from solver. The
// factory counts are then obtained from the solution using the spec.
function itemUpdate() {
var rates = {}
for (var i = 0; i < build_targets.length; i++) {
var target = build_targets[i]
var rate = target.getRate()
rates[target.itemName] = rate
}
globalTotals = solver.solve(rates, spec.ignore, spec)
display()
}
function Header(name, colSpan) {
if (!colSpan) {
colSpan = 1
}
return {"name": name, "colSpan": colSpan}
}
var NO_MODULE = "no module"
function pipeValues(rate) {
var pipes = rate.div(maxPipeThroughput).ceil()
var perPipeRate = rate.div(pipes)
var length = pipeLength(perPipeRate).floor()
return {pipes: pipes, length: length}
}
function ItemIcon(item, canIgnore) {
this.item = item
this.name = item.name
this.extra = null
if (canIgnore) {
this.extra = document.createElement("span")
this.span = document.createElement("span")
this.extra.appendChild(this.span)
this.extra.appendChild(document.createElement("br"))
}
this.icon_col = item.icon_col
this.icon_row = item.icon_row
}
ItemIcon.prototype = {
constructor: ItemIcon,
setText: function(text) {
this.span.textContent = text
},
renderTooltip: function() {
return this.item.renderTooltip(this.extra)
}
}
function BeltIcon(beltItem, beltSpeed) {
if (!beltItem) {
beltItem = solver.items[preferredBelt]
}
if (!beltSpeed) {
beltSpeed = preferredBeltSpeed
}
this.item = beltItem
this.speed = beltSpeed
this.name = this.item.name
this.icon_col = this.item.icon_col
this.icon_row = this.item.icon_row
}
BeltIcon.prototype = {
constructor: BeltIcon,
renderTooltip: function() {
var t = document.createElement("div")
t.classList.add("frame")
var title = document.createElement("h3")
var im = getImage(this, true)
title.appendChild(im)
title.appendChild(new Text(formatName(this.name)))
t.appendChild(title)
var b = document.createElement("b")
b.textContent = "Max throughput: "
t.appendChild(b)
t.appendChild(new Text(displayRate(this.speed) + " items/" + rateName))
return t
}
}
class BeltCells {
constructor(row) {
this.beltCell = document.createElement("td")
row.appendChild(this.beltCell)
let beltCountCell = document.createElement("td")
beltCountCell.classList.add("right-align", "pad-right")
this.beltCountNode = document.createElement("tt")
beltCountCell.appendChild(this.beltCountNode)
row.appendChild(beltCountCell)
}
setRate(rate) {
while (this.beltCell.hasChildNodes()) {
this.beltCell.removeChild(this.beltCell.lastChild)
}
let beltImage = getImage(new BeltIcon())
this.beltCell.appendChild(beltImage)
this.beltCell.appendChild(new Text(" \u00d7"))
let beltCount = rate.div(preferredBeltSpeed)
this.beltCountNode.textContent = alignCount(beltCount)
}
}
class PipeCells {
constructor(row) {
let pipeCell = document.createElement("td")
pipeCell.colSpan = 2
pipeCell.classList.add("pad-right")
row.appendChild(pipeCell)
let pipeItem = solver.items["pipe"]
pipeCell.appendChild(getImage(pipeItem, true))
this.pipeNode = document.createElement("tt")
pipeCell.appendChild(this.pipeNode)
}
setRate(rate) {
if (rate.equal(zero)) {
this.pipeNode.textContent = " \u00d7 0"
return
}
let pipe = pipeValues(rate)
let pipeString = ""
if (one.less(pipe.pipes)) {
pipeString += " \u00d7 " + pipe.pipes.toDecimal(0)
}
pipeString += " \u2264 " + pipe.length.toDecimal(0)
this.pipeNode.textContent = pipeString
}
}
function addBeltCells(row, item) {
if (item.phase == "solid") {
return new BeltCells(row)
} else if (item.phase == "fluid") {
return new PipeCells(row)
} else {
row.appendChild(document.createElement("td"))
row.appendChild(document.createElement("td"))
return null
}
}
function ItemRow(row, item, canIgnore) {
this.item = item
this.arrowCell = document.createElement("td")
var arrowSVG = document.createElementNS("http://www.w3.org/2000/svg", "svg")
arrowSVG.classList.add("breakdown-arrow")
arrowSVG.setAttribute("viewBox", "0 0 16 16")
arrowSVG.setAttribute("width", "16")
arrowSVG.setAttribute("height", "16")
this.arrowCell.appendChild(arrowSVG)
var arrowUse = document.createElementNS("http://www.w3.org/2000/svg", "use")
arrowUse.setAttribute("href", "images/icons.svg#right")
arrowSVG.appendChild(arrowUse)
row.appendChild(this.arrowCell)
var nameCell = document.createElement("td")
nameCell.className = "right-align"
this.itemIcon = new ItemIcon(item, canIgnore)
var im = getImage(this.itemIcon)
im.classList.add("display")
if (canIgnore) {
if (spec.ignore[item.name]) {
this.itemIcon.setText("(Click to unignore.)")
} else {
this.itemIcon.setText("(Click to ignore.)")
}
im.classList.add("recipe-icon")
}
this.image = im
nameCell.appendChild(im)
row.appendChild(nameCell)
var rateCell = document.createElement("td")
rateCell.classList.add("right-align", "pad-right")
var tt = document.createElement("tt")
rateCell.appendChild(tt)
this.rateNode = tt
row.appendChild(rateCell)
this.belts = addBeltCells(row, item)
var wasteCell = document.createElement("td")
wasteCell.classList.add("right-align", "pad-right", "waste")
tt = document.createElement("tt")
wasteCell.appendChild(tt)
this.wasteNode = tt
row.appendChild(wasteCell)
}
ItemRow.prototype = {
constructor: ItemRow,
setIgnore: function(ignore) {
if (ignore) {
this.itemIcon.setText("(Click to unignore.)")
} else {
this.itemIcon.setText("(Click to ignore.)")
}
},
setRate: function(itemRate, waste) {
this.rateNode.textContent = alignRate(itemRate)
this.wasteNode.textContent = alignRate(waste)
if (this.belts !== null) {
this.belts.setRate(itemRate)
}
},
}
function makePopOutCell() {
var popOutCell = document.createElement("td")
popOutCell.classList.add("pad")
var popOutLink = document.createElement("a")
popOutLink.target = "_blank"
popOutLink.title = "Open this item in separate window."
popOutCell.appendChild(popOutLink)
var popOutSVG = document.createElementNS("http://www.w3.org/2000/svg", "svg")
popOutSVG.classList.add("popout")
popOutSVG.setAttribute("viewBox", "0 0 24 24")
popOutSVG.setAttribute("width", "16")
popOutSVG.setAttribute("height", "16")
popOutLink.appendChild(popOutSVG)
var popOutUse = document.createElementNS("http://www.w3.org/2000/svg", "use")
popOutUse.setAttribute("href", "images/icons.svg#popout")
popOutSVG.appendChild(popOutUse)
return popOutCell
}
// A row in the BreakdownTable.
class UsageRow {
constructor(item, destRecipe) {
this.name = item.name
this.item = item
this.node = document.createElement("tr")
this.node.classList.add("breakdown-row", "display-row")
let arrowCell = document.createElement("td")
arrowCell.appendChild(getImage(item))
let arrowSVG = document.createElementNS("http://www.w3.org/2000/svg", "svg")
arrowSVG.classList.add("usage-arrow")
arrowSVG.setAttribute("viewBox", "0 0 18 16")
arrowSVG.setAttribute("width", "18")
arrowSVG.setAttribute("height", "16")
arrowCell.appendChild(arrowSVG)
let arrowUse = document.createElementNS("http://www.w3.org/2000/svg", "use")
arrowUse.setAttribute("href", "images/icons.svg#rightarrow")
arrowSVG.appendChild(arrowUse)
arrowCell.appendChild(getImage(destRecipe))
this.node.appendChild(arrowCell)
let rateCell = document.createElement("td")
rateCell.classList.add("right-align", "pad-right")
let tt = document.createElement("tt")
rateCell.appendChild(tt)
this.rateNode = tt
this.node.appendChild(rateCell)
this.belts = addBeltCells(this.node, item)
this.factory = new FactoryCountCells(this.node)
this.factory.countNode.parentNode.classList.add("pad-right")
let percentCell = document.createElement("td")
percentCell.classList.add("right-align")
this.percentNode = document.createElement("tt")
percentCell.appendChild(this.percentNode)
this.node.appendChild(percentCell)
}
appendTo(parentNode) {
parentNode.appendChild(this.node)
}
setRate(totalRate, portion) {
this.rateNode.textContent = alignRate(portion)
if (this.belts !== null) {
this.belts.setRate(portion)
}
let recipe = null
let recipeRate = zero
if (this.item.recipes.length === 1) {
recipe = this.item.recipes[0]
let amount = recipe.gives(this.item, spec)
recipeRate = portion.div(amount)
}
this.factory.setCount(recipe, recipeRate)
if (totalRate === null) {
this.percentNode.textContent = ""
} else {
let percentage = portion.div(totalRate).mul(RationalFromFloat(100))
if (percentage.less(one)) {
this.percentNode.textContent = "<1%"
} else {
this.percentNode.textContent = percentage.toDecimal(0) + "%"
}
}
}
}
class BreakdownTable {
constructor(item) {
this.item = item
this.row = document.createElement("tr")
this.row.classList.add("breakdown")
this.row.appendChild(document.createElement("td"))
let breakdownCell = document.createElement("td")
breakdownCell.colSpan = 10
this.row.appendChild(breakdownCell)
this.table = document.createElement("table")
breakdownCell.appendChild(this.table)
}
setShow(show) {
if (show) {
this.row.classList.add("breakdown-open")
} else {
this.row.classList.remove("breakdown-open")
}
}
renderTable(itemRow, totals, items, fuelUsers) {
while (this.table.hasChildNodes()) {
this.table.removeChild(this.table.lastChild)
}
let rate = items[this.item.name]
let row = null
// If this recipe is not being ignored, list its inputs.
if (this.item.recipes.length !== 1 || !spec.ignore[this.item.recipes[0].name]) {
for (let recipe of this.item.recipes) {
if (recipe.name in totals.totals) {
let recipeRate = totals.get(recipe.name)
for (let ing of recipe.getIngredients(spec)) {
if (ing.item === this.item) {
continue
}
let subRate = recipeRate.mul(ing.amount)
row = new UsageRow(ing.item, recipe)
row.appendTo(this.table)
row.setRate(null, subRate)
}
}
}
if (row !== null) {
row.node.classList.add("breakdown-last-input")
}
}
let users = this.item.uses
if (fuelUsers.has(this.item)) {
users = users.concat(fuelUsers.get(this.item))
}
let found = false
for (let user of users) {
if (spec.ignore[user.name]) {
continue
}
if (user.name in totals.totals) {
found = true
let subRate = zero
for (let ing of user.getIngredients(spec)) {
if (ing.item === this.item) {
subRate = totals.get(user.name).mul(ing.amount)
break
}
}
let row = new UsageRow(this.item, user)
row.appendTo(this.table)
row.setRate(rate, subRate)
}
}
if (!found && row !== null) {
row.node.classList.remove("breakdown-last-input")
}
}
remove() {
this.row.parentElement.removeChild(this.row)
}
}
function RecipeRow(recipeName, rate, itemRate, waste, fuelUsers) {
this.name = recipeName
this.recipe = solver.recipes[recipeName]
this.rate = rate
this.node = document.createElement("tr")
this.node.classList.add("recipe-row")
this.node.classList.add("display-row")
var canIgnore = this.recipe.canIgnore()
if (spec.ignore[recipeName]) {
if (!canIgnore) {
delete spec.ignore[recipeName]
} else {
this.node.classList.add("ignore")
}
}
this.item = this.recipe.products[0].item
this.itemRow = new ItemRow(this.node, this.item, canIgnore)
if (canIgnore) {
this.itemRow.image.addEventListener("click", new IgnoreHandler(this))
}
this.factoryRow = new FactoryRow(this.node, this.recipe, this.rate)
var popOutCell = makePopOutCell()
this.node.appendChild(popOutCell)
this.popOutLink = popOutCell.firstChild
// Set values.
if (canIgnore) {
this.setIgnore(spec.ignore[recipeName])
}
this.setRate(rate, itemRate, waste, fuelUsers)
this.factoryRow.updateDisplayedModules()
this.breakdown = new BreakdownTable(this.item)
this.itemRow.arrowCell.addEventListener("click", new ToggleBreakdownHandler(this.itemRow, this.breakdown))
}
RecipeRow.prototype = {
constructor: RecipeRow,
appendTo: function(parentNode) {
parentNode.appendChild(this.node)
parentNode.appendChild(this.breakdown.row)
},
// Call whenever this recipe's status in the ignore list changes.
setIgnore: function(ignore) {
if (ignore) {
this.node.classList.add("ignore")
} else {
this.node.classList.remove("ignore")
}
this.itemRow.setIgnore(ignore)
},
hasModules: function() {
return !this.node.classList.contains("no-mods")
},
setDownArrow: function() {
this.factoryRow.downArrow.textContent = "\u2193"
},
setUpDownArrow: function() {
this.factoryRow.downArrow.textContent = "\u2195"
},
setUpArrow: function() {
this.factoryRow.downArrow.textContent = "\u2191"
},
updateDisplayedModules: function() {
this.factoryRow.updateDisplayedModules()
},
totalPower: function() {
if (this.factoryRow.power && this.factoryRow.power.fuel === "electric") {
return this.factoryRow.power.power
}
return zero
},
csv: function() {
var rate = this.rate.mul(this.recipe.gives(this.item, spec))
var parts = [
this.name,
displayRate(rate),
]
parts = parts.concat(this.factoryRow.csv())
return [parts.join(",")]
},
// Sets the new recipe-rate for a recipe, and updates the factory count.
setRate: function(recipeRate, itemRate, waste, fuelUsers) {
this.rate = recipeRate
this.itemRow.setRate(itemRate, waste)
this.factoryRow.displayFactory(recipeRate)
var rate = {}
rate[this.item.name] = itemRate
var link = "#" + formatSettings(rate)
this.popOutLink.href = link
},
setRates: function(totals, items, fuelUsers) {
var recipeRate = totals.get(this.name)
var itemRate = items[this.item.name]
var waste = totals.getWaste(this.item.name)
this.setRate(recipeRate, itemRate, waste, fuelUsers)
this.breakdown.renderTable(this.itemRow, totals, items, fuelUsers)
},
remove: function() {
this.node.parentElement.removeChild(this.node)
this.breakdown.remove()
},
}
class FactoryCountCells {
constructor(row) {
this.factoryCell = document.createElement("td")
this.factoryCell.classList.add("pad", "factory", "right-align", "leftmost")
row.appendChild(this.factoryCell)
let countCell = document.createElement("td")
countCell.classList.add("factory", "right-align")
let tt = document.createElement("tt")
countCell.appendChild(tt)
this.countNode = tt
row.appendChild(countCell)
}
setCount(recipe, recipeRate) {
if (!recipe || recipeRate.isZero()) {
return {"count": zero}
}
let count = spec.getCount(recipe, recipeRate)
if (count.isZero()) {
return {count}
}
let factory = spec.getFactory(recipe)
var image = getImage(factory.factory)
image.classList.add("display")
while (this.factoryCell.hasChildNodes()) {
this.factoryCell.removeChild(this.factoryCell.lastChild)
}
if (recipe.displayGroup !== null || recipe.name !== recipe.products[0].item.name) {
this.factoryCell.appendChild(getImage(recipe))
this.factoryCell.appendChild(new Text(" : "))
}
this.factoryCell.appendChild(image)
this.factoryCell.appendChild(new Text(" \u00d7"))
this.countNode.textContent = alignCount(count)
return {factory, count}
}
}
function FactoryRow(row, recipe) {
this.node = row
var recipeName = recipe.name
this.recipe = recipe
this.factory = null
this.count = zero
this.power = null
this.factoryCells = new FactoryCountCells(row)
this.modulesCell = document.createElement("td")
this.modulesCell.classList.add("pad", "module", "factory")
this.node.appendChild(this.modulesCell)
this.copyButton = document.createElement("button")
this.copyButton.classList.add("ui", "copy")
this.copyButton.textContent = "\u2192"
this.copyButton.title = "copy to rest of modules"
this.copyButton.addEventListener("click", new ModuleCopyHandler(this))
this.modulesCell.appendChild(this.copyButton)
this.dropdowns = []
this.modules = []
var beaconCell = document.createElement("td")
beaconCell.classList.add("pad", "module", "factory")
let {inputs} = moduleDropdown(
d3.select(beaconCell),
"mod-" + recipeName + "-beacon",
d => d === null,
BeaconHandler(recipeName),
d => d === null || d.canBeacon(),
)
this.beacon = inputs
var beaconX = document.createElement("span")
beaconX.appendChild(new Text(" \u00D7 "))
beaconCell.appendChild(beaconX)
this.beaconCount = document.createElement("input")
this.beaconCount.addEventListener("change", new BeaconCountHandler(recipeName))
this.beaconCount.type = "number"
this.beaconCount.value = 0
this.beaconCount.classList.add("beacon")
this.beaconCount.title = "The number of broadcasted modules which will affect this factory."
beaconCell.appendChild(this.beaconCount)
this.node.appendChild(beaconCell)
var downArrowCell = document.createElement("td")
downArrowCell.classList.add("module", "factory")
this.downArrow = document.createElement("button")
this.downArrow.classList.add("ui")
this.downArrow.textContent = "\u2195"
this.downArrow.title = "copy this recipe's modules to all other recipes"
this.downArrow.addEventListener("click", new CopyAllHandler(recipeName))
downArrowCell.appendChild(this.downArrow)
this.node.appendChild(downArrowCell)
this.fuelCell = document.createElement("td")
this.fuelCell.classList.add("pad", "factory")
this.node.appendChild(this.fuelCell)
var powerCell = document.createElement("td")
powerCell.classList.add("factory", "right-align")
let tt = document.createElement("tt")
powerCell.appendChild(tt)
this.powerNode = tt
this.node.appendChild(powerCell)
}
FactoryRow.prototype = {
constructor: FactoryRow,
setPower: function(power) {
while (this.fuelCell.hasChildNodes()) {
this.fuelCell.removeChild(this.fuelCell.lastChild)
}
if (power.fuel === "electric") {
this.powerNode.textContent = alignPower(power.power)
} else if (power.fuel === "chemical") {
var fuelImage = getImage(preferredFuel)
this.fuelCell.appendChild(fuelImage)
this.fuelCell.appendChild(new Text(" \u00d7"))
this.powerNode.textContent = alignRate(power.power.div(preferredFuel.value)) + "/" + rateName
}
},
setHasModules: function() {
this.node.classList.remove("no-mods")
},
setHasNoModules: function() {
this.node.classList.add("no-mods")
},
// Call whenever the minimum factory or factory count might change (e.g. in
// response to speed modules being added/removed).
//
// This may change the factory icon, factory count, number (or presence)
// of module slots, presence of the beacon info, and/or presence of the
// module-copy buttons.
displayFactory: function(rate) {
let {count, factory} = this.factoryCells.setCount(this.recipe, rate)
if (count.isZero()) {
this.setHasNoModules()
return
}
this.count = count
this.factory = factory
var moduleDelta = this.factory.modules.length - this.modules.length
if (moduleDelta < 0) {
this.modules.length = this.factory.modules.length
for (var i = moduleDelta; i < 0; i++) {
this.dropdowns.pop().remove()
}
} else if (moduleDelta > 0) {
for (var i = 0; i < moduleDelta; i++) {
let self = this
var index = this.dropdowns.length
var installedModule = this.factory.modules[index]
let {dropdown, inputs} = moduleDropdown(
d3.select(this.modulesCell),
"mod-" + this.recipe.name + "-" + index,
d => d === installedModule,
ModuleHandler(this, index),
d => d === null || d.canUse(self.recipe),
)
this.dropdowns.push(dropdown.parentNode)
this.modules.push(inputs)
}
}
if (moduleDelta != 0) {
if (this.dropdowns.length > 1) {
this.modulesCell.insertBefore(this.copyButton, this.dropdowns[1])
} else {
this.modulesCell.appendChild(this.copyButton)
}
}
if (this.modules.length > 0) {
this.setHasModules()
} else {
this.setHasNoModules()
}
this.power = this.factory.powerUsage(spec, this.count)
this.setPower(this.power)
},
updateDisplayedModules: function() {
var moduleCount = spec.moduleCount(this.recipe)
if (moduleCount === 0) {
return
}
for (var i = 0; i < moduleCount; i++) {
var module = spec.getModule(this.recipe, i)
this.setDisplayedModule(i, module)
}
// XXX
var beacon = spec.getBeaconInfo(this.recipe)
this.setDisplayedBeacon(beacon.module, beacon.count)
},
setDisplayedModule: function(index, module) {
var name
if (module) {
name = module.name
} else {
name = NO_MODULE
}
this.modules[index][name].checked = true
},
setDisplayedBeacon: function(module, count) {
var name
if (module) {
name = module.name
} else {
name = NO_MODULE
}
this.beacon[name].checked = true
this.beaconCount.value = count.toString()
},
csv: function() {
var parts = []
if (this.count.isZero()) {
parts.push("")
parts.push("")
} else {
parts.push(this.factory.name)
parts.push(displayCount(this.count))
}
if (this.factory && this.factory.modules.length > 0) {
var modules = []
for (var i = 0; i < this.factory.modules.length; i++) {
var module = this.factory.modules[i]
if (module) {
modules.push(module.shortName())
} else {
modules.push("")
}
}
parts.push(modules.join("/"))
if (this.factory.beaconModule && !this.factory.beaconCount.isZero()) {
parts.push(this.factory.beaconModule.shortName())
parts.push(displayCount(this.factory.beaconCount))
} else {
parts.push("")
parts.push("")
}
} else {
parts.push("")
parts.push("")
parts.push("")
}
if (this.factory) {
parts.push(displayCount(this.power.power))
} else {
parts.push("")
}
return parts.join(",")
},
}
function GroupRow(group, itemRates, totals, fuelUsers) {
this.name = group.id
this.group = group
this.items = {}
for (var i = 0; i < group.recipes.length; i++) {
var recipe = group.recipes[i]
for (var j = 0; j < recipe.products.length; j++) {
var ing = recipe.products[j]
this.items[ing.item.name] = ing.item
}
}
this.itemNames = Object.keys(this.items)
var recipeCount = group.recipes.length
var tableRows = Math.max(this.itemNames.length, recipeCount)
this.rows = []
this.itemRows = []
this.breakdowns = []
this.itemRates = []
this.factoryRows = []
for (var i = 0; i < tableRows; i++) {
var row = document.createElement("tr")
row.classList.add("display-row")
row.classList.add("group-row")
if (i < this.itemNames.length) {
let item = this.items[this.itemNames[i]]
let itemRow = new ItemRow(row, item, false)
let breakdown = new BreakdownTable(item)
this.itemRows.push(itemRow)
this.breakdowns.push(breakdown)
itemRow.arrowCell.addEventListener("click", new ToggleBreakdownHandler(itemRow, breakdown))
} else {
row.appendChild(document.createElement("td"))
row.appendChild(document.createElement("td"))
row.appendChild(document.createElement("td"))
row.appendChild(document.createElement("td"))
row.appendChild(document.createElement("td"))
var dummyWaste = document.createElement("td")
dummyWaste.classList.add("waste")
row.appendChild(dummyWaste)
}
if (i < recipeCount) {
var recipe = group.recipes[i]
this.factoryRows.push(new FactoryRow(row, recipe, totals.get(recipe.name)))
} else {
for (var j = 0; j < 7; j++) {
var cell = document.createElement("td")
cell.classList.add("factory")
if (j == 0) {
cell.classList.add("leftmost")
}
row.appendChild(cell)
}
}
this.rows.push(row)
// TODO: Making this work properly with GroupRow requires a little more
// thought. Dummy it out for now.
/*if (i === 0) {
var popOutCell = makePopOutCell()
row.appendChild(popOutCell)
this.popOutLink = popOutCell.firstChild
} else {*/
row.appendChild(document.createElement("td"))
/*}*/
}
this.rows[0].classList.add("group-top-row")
row.classList.add("group-bottom-row")
this.setRates(totals, itemRates, fuelUsers)
this.updateDisplayedModules()
}
GroupRow.prototype = {
constructor: GroupRow,
appendTo: function(parentNode) {
for (var i = 0; i < this.rows.length; i++) {
parentNode.appendChild(this.rows[i])
if (i < this.breakdowns.length) {
parentNode.appendChild(this.breakdowns[i].row)
}
}
},
groupMatches: function(group) {
return this.group.equal(group)
},
setRates: function(totals, itemRates, fuelUsers) {
this.itemRates = []
var rates = {}
for (var i = 0; i < this.itemNames.length; i++) {
var itemName = this.itemNames[i]
var rate = itemRates[itemName]
rates[itemName] = rate
this.itemRates.push(rate)
var waste = totals.getWaste(itemName)
rate = rate.sub(waste)
this.itemRows[i].setRate(rate, waste)
this.breakdowns[i].renderTable(this.itemRows[i], totals, itemRates, fuelUsers)
}
for (var i = 0; i < this.factoryRows.length; i++) {
var row = this.factoryRows[i]
var recipeName = row.recipe.name
row.displayFactory(totals.get(recipeName))
}
},
totalPower: function() {
var power = zero
for (var i = 0; i < this.factoryRows.length; i++) {
var p = this.factoryRows[i].power
if (p.fuel === "electric") {
power = power.add(p.power)
}
}
return power
},
csv: function() {
var lines = []
for (var i = 0; i < this.itemNames.length; i++) {
var itemName = this.itemNames[i]
var rate = displayRate(this.itemRates[i])
var parts = [itemName, rate, "", "", "", "", "", ""]
lines.push(parts.join(","))
}
return lines
},
hasModules: function() {
for (var i = 0; i < this.factoryRows.length; i++) {
if (this.factoryRows[i].modules.length > 0) {
return true
}
}
return false
},
setDownArrow: function() {
for (var i = 0; i < this.factoryRows.length; i++) {
var row = this.factoryRows[i]
if (row.modules.length > 0) {
row.downArrow.textContent = "\u2193"
return
}
}
},
setUpDownArrow: function() {
for (var i = 0; i < this.factoryRows.length; i++) {
this.factoryRows[i].downArrow.textContent = "\u2195"
}
},
setUpArrow: function() {
for (var i = this.factoryRows.length - 1; i >= 0; i--) {
var row = this.factoryRows[i]
if (row.modules.length > 0) {
row.downArrow.textContent = "\u2191"
return
}
}
},
updateDisplayedModules: function() {
for (var i = 0; i < this.factoryRows.length; i++) {
this.factoryRows[i].updateDisplayedModules()