-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathmodule.js
302 lines (291 loc) · 9.68 KB
/
module.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
/*Copyright 2015-2024 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.*/
import { makeDropdown, addInputs } from "./dropdown.js"
import { Icon, sprites } from "./icon.js"
import { useLegacyCalculation } from "./init.js"
import { Rational, zero, half, one } from "./rational.js"
import { sorted } from "./sort.js"
let hundred = Rational.from_float(100)
function percent(x) {
let sign = ""
if (!x.less(zero)) {
sign = "+"
}
return `${sign}${x.mul(hundred).toDecimal()}%`
}
class Module {
constructor(key, name, col, row, category, order, productivity, speed, power) {
// Other module effects not modeled by this calculator.
this.key = key
this.name = name
this.category = category
this.order = order
this.productivity = productivity
this.speed = speed
this.power = power
this.icon_col = col
this.icon_row = row
this.icon = new Icon(this)
}
// This naming scheme is some older cruft, which works in the vanilla
// dataset, but it's possible other datasets would render it unworkable.
shortName() {
return this.key[0] + this.key[this.key.length - 1]
}
canUse(recipe) {
if (this.hasProdEffect() && !recipe.allow_productivity) {
return false
}
return true
}
canBeacon() {
return this.productivity.isZero()
}
hasProdEffect() {
return !this.productivity.isZero()
}
renderTooltip() {
let self = this
let t = d3.create("div")
.classed("frame", true)
let header = t.append("h3")
header.append(() => self.icon.make(32, true))
header.append(() => new Text(self.name))
let line
if (!this.power.isZero()) {
line = t.append("div")
line.append("b")
.text("Energy consumption: ")
line.append("span")
.text(percent(this.power))
}
if (!this.speed.isZero()) {
line = t.append("div")
line.append("b")
.text("Speed: ")
line.append("span")
.text(percent(this.speed))
}
if (!this.productivity.isZero()) {
line = t.append("div")
line.append("b")
.text("Productivity: ")
line.append("span")
.text(percent(this.productivity))
}
return t.node()
}
}
export function moduleDropdown(selector, data) {
let moduleDropdownSpan = selector.selectAll("span.module-wrapper")
.data(data)
.join(
enter => {
let s = enter.append("span")
.classed("module-wrapper", true)
makeDropdown(s)
return s
}
)
let moduleDropdown = moduleDropdownSpan.selectAll("div.dropdown")
moduleDropdown.selectAll("div.moduleRow")
.data(d => d.inputRows)
.join("div")
.classed("moduleRow", true)
.selectAll("span.input")
.data(d => d)
.join(
enter => {
let s = enter.append("span")
.classed("input", true)
let label = addInputs(
s,
d => d.cell.name,
d => d.checked(),
d => d.choose(),
)
label.append(function(d) {
if (d.module === null) {
return sprites.get("slot_icon_module").icon.make(32)
} else {
return d.module.icon.make(32, false, this.parentNode.parentNode.parentNode)
}
})
return s
},
update => {
update.selectAll("input").property("checked", d => d.checked())
return update
},
)
}
// ModuleSpec represents the set of modules (including beacons) configured for
// a given recipe.
export class ModuleSpec {
constructor(recipe, spec) {
this.recipe = recipe
this.building = null
this.modules = []
this.beaconModules = [spec.defaultBeacon[0], spec.defaultBeacon[1]]
this.beaconCount = spec.defaultBeaconCount
}
setBuilding(building, spec) {
this.building = building
if (this.modules.length > building.moduleSlots) {
this.modules.length = building.moduleSlots
}
let toAdd = spec.getDefaultModule(this.recipe)
while (this.modules.length < building.moduleSlots) {
this.modules.push(toAdd)
}
}
getModule(index) {
return this.modules[index]
}
// Returns true if the module change requires a recalculation.
setModule(index, module) {
if (index >= this.modules.length) {
return false
}
let oldModule = this.modules[index]
let needRecalc = (oldModule && oldModule.hasProdEffect()) || (module && module.hasProdEffect())
this.modules[index] = module
return needRecalc
}
setBeaconModule(module, i) {
this.beaconModules[i] = module
}
setBeaconCount(count) {
this.beaconCount = count
}
speedEffect() {
let speed = one
for (let module of this.modules) {
if (!module) {
continue
}
speed = speed.add(module.speed)
}
if (this.modules.length > 0) {
for (let module of this.beaconModules) {
if (module === null) {
continue
}
let beacon = module.speed.mul(this.beaconCount).mul(beaconEffect)
if (!useLegacyCalculation) {
let i = this.beaconCount.ceil().toFloat() - 1
if (i >= beaconProfile.length) {
i = beaconProfile.length - 1
}
beacon = beacon.mul(beaconProfile[i])
}
speed = speed.add(beacon)
}
}
return speed
}
prodEffect(spec) {
let prod = one
for (let module of this.modules) {
if (!module) {
continue
}
prod = prod.add(module.productivity)
}
prod = prod.add(this.building.prodEffect(spec))
return prod
}
powerEffect(spec) {
let power = one
for (let module of this.modules) {
if (!module) {
continue
}
power = power.add(module.power)
}
if (this.modules.length > 0) {
for (let module of this.beaconModules) {
if (module === null) {
continue
}
let beacon = module.power.mul(this.beaconCount).mul(beaconEffect)
if (!useLegacyCalculation) {
let i = this.beaconCount.ceil().toFloat() - 1
if (i >= beaconProfile.length) {
i = beaconProfile.length - 1
}
beacon = beacon.mul(beaconProfile[i])
}
power = power.add(beacon)
}
}
let minimum = Rational.from_floats(1, 5)
if (power.less(minimum)) {
power = minimum
}
return power
}
}
export let moduleRows = null
export let shortModules = null
let beaconProfile
let beaconEffect
export function getModules(data, items) {
let modules = new Map()
for (let d of data.modules) {
let item = items.get(d.item_key)
let effect = d.effect
let category = d.category
let order = item.order
let speed = Rational.from_float_approximate(effect.speed || 0)
let productivity = Rational.from_float_approximate(effect.productivity || 0)
let power = Rational.from_float_approximate(effect.consumption || 0)
modules.set(d.item_key, new Module(
d.item_key,
item.name,
item.icon_col,
item.icon_row,
category,
order,
productivity,
speed,
power,
))
}
let sortedModules = sorted(modules.values(), m => m.order)
moduleRows = [[null]]
shortModules = new Map()
let category = null
for (let module of sortedModules) {
if (module.category !== category) {
category = module.category
moduleRows.push([])
}
moduleRows[moduleRows.length - 1].push(module)
let shortName = module.shortName()
if (shortModules.has(shortName)) {
// This does not occur in the vanilla data, but let's plan ahead.
module.shortName = function() { return this.key }
shortName = module.key
}
shortModules.set(shortName, module)
}
beaconEffect = Rational.from_float_approximate(data.beacon.distribution_effectivity)
if (useLegacyCalculation) {
beaconProfile = null
} else {
beaconProfile = []
for (let x of data.beacon.profile) {
beaconProfile.push(Rational.from_float_approximate(x))
}
}
return modules
}