-
Notifications
You must be signed in to change notification settings - Fork 157
/
Copy pathbuilding.js
332 lines (318 loc) · 9.84 KB
/
building.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
/*Copyright 2019-2021 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 { powerRepr } from "./display.js"
import { Icon } from "./icon.js"
import { Rational, zero, one } from "./rational.js"
let thirty = Rational.from_float(30)
class Building {
constructor(key, name, col, row, categories, speed, prodBonus, moduleSlots, power, fuel) {
this.key = key
this.name = name
this.categories = new Set(categories)
this.speed = speed
this.prodBonus = prodBonus
this.moduleSlots = moduleSlots
this.power = power
this.fuel = fuel
this.icon_col = col
this.icon_row = row
this.icon = new Icon(this)
}
less(other) {
if (!this.speed.equal(other.speed)) {
return this.speed.less(other.speed)
}
return this.moduleSlots < other.moduleSlots
}
getCount(spec, recipe, rate) {
return rate.div(this.getRecipeRate(spec, recipe))
}
getRecipeRate(spec, recipe) {
let modules = spec.getModuleSpec(recipe)
let speedEffect
if (modules) {
speedEffect = modules.speedEffect()
} else {
speedEffect = one
}
return recipe.time.reciprocate().mul(this.speed).mul(speedEffect)
}
canBeacon() {
return this.moduleSlots > 0
}
prodEffect(spec) {
return this.prodBonus
}
drain() {
return this.power.div(thirty)
}
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 = t.append("div")
line.append("b")
.text("Energy consumption: ")
let {power, suffix} = powerRepr(this.power)
line.append("span")
.text(`${power.toDecimal(0)} ${suffix}`)
line = t.append("div")
line.append("b")
.text("Crafting speed: ")
line.append("span")
.text(this.speed.toDecimal())
line = t.append("div")
line.append("b")
.text("Module slots: ")
line.append("span")
.text(String(this.moduleSlots))
return t.node()
}
}
class Miner extends Building {
constructor(key, name, col, row, categories, miningSpeed, moduleSlots, power, fuel) {
super(key, name, col, row, categories, zero, zero, moduleSlots, power, fuel)
this.miningSpeed = miningSpeed
}
less(other) {
return this.miningSpeed.less(other.miningSpeed)
}
drain() {
return zero
}
getRecipeRate(spec, recipe) {
let modules = spec.getModuleSpec(recipe)
let speedEffect
if (modules) {
speedEffect = modules.speedEffect()
} else {
speedEffect = one
}
return this.miningSpeed.div(recipe.miningTime).mul(speedEffect)
}
prodEffect(spec) {
return spec.miningProd
}
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 = t.append("div")
line.append("b")
.text("Energy consumption: ")
let {power, suffix} = powerRepr(this.power)
line.append("span")
.text(`${power.toDecimal(0)} ${suffix}`)
line = t.append("div")
line.append("b")
.text("Mining speed: ")
line.append("span")
.text(this.miningSpeed.toDecimal())
line = t.append("div")
line.append("b")
.text("Module slots: ")
line.append("span")
.text(String(this.moduleSlots))
return t.node()
}
}
class OffshorePump extends Building {
constructor(key, name, col, row, pumpingSpeed) {
super(key, name, col, row, ["offshore-pumping"], zero, zero, 0, zero, null)
this.pumpingSpeed = pumpingSpeed
}
less(other) {
return this.pumpingSpeed.less(other.pumpingSpeed)
}
getRecipeRate(spec, recipe) {
return this.pumpingSpeed
}
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 = t.append("div")
line.append("b")
.text("Pumping speed: ")
line.append("span")
.text(`${spec.format.rate(this.pumpingSpeed)}/${spec.format.rateName}`)
return t.node()
}
}
let rocketLaunchDuration = Rational.from_floats(2434, 60)
function launchRate(spec) {
let partRecipe = spec.recipes.get("rocket-part")
let partFactory = spec.getBuilding(partRecipe)
let partItem = spec.items.get("rocket-part")
let gives = partRecipe.gives(partItem)
// The base rate at which the silo can make rocket parts.
let rate = Building.prototype.getRecipeRate.call(partFactory, spec, partRecipe)
// Number of times to complete the rocket part recipe per launch.
let perLaunch = Rational.from_float(100).div(gives)
// Total length of time required to launch a rocket.
let time = perLaunch.div(rate).add(rocketLaunchDuration)
let launchRate = time.reciprocate()
let partRate = perLaunch.div(time)
return {part: partRate, launch: launchRate}
}
class RocketLaunch extends Building {
getRecipeRate(spec, recipe) {
return launchRate(spec).launch
}
}
class RocketSilo extends Building {
getRecipeRate(spec, recipe) {
return launchRate(spec).part
}
}
function renderTooltipBase() {
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))
return t.node()
}
export function getBuildings(data, items) {
let buildings = []
let reactorDef = items.get("nuclear-reactor")
let reactor = new Building(
"nuclear-reactor",
reactorDef.name,
reactorDef.icon_col,
reactorDef.icon_row,
["nuclear"],
one,
zero,
0,
zero,
null
)
reactor.renderTooltip = renderTooltipBase
buildings.push(reactor)
let boilerItem = items.get("boiler")
let boilerDef
for (let d of data.boilers) {
if (d.key === "boiler") {
boilerDef = d
break
}
}
let boiler_energy = Rational.from_float(boilerDef.energy_consumption)
let boiler = new Building(
"boiler",
boilerItem.name,
boilerItem.icon_col,
boilerItem.icon_row,
["boiler"],
one,
zero,
0,
boiler_energy,
"chemical",
//boilerDef.target_temperature,
)
boiler.renderTooltip = renderTooltipBase
buildings.push(boiler)
let siloDef = items.get("rocket-silo")
let launch = new RocketLaunch(
"rocket-silo",
siloDef.name,
siloDef.icon_col,
siloDef.icon_row,
["rocket-launch"],
one,
zero,
0,
zero,
null
)
launch.renderTooltip = renderTooltipBase
buildings.push(launch)
for (let d of data.crafting_machines) {
let fuel = null
if (d.energy_source && d.energy_source.type === "burner") {
fuel = d.energy_source.fuel_category
}
let prod = zero
if (d.prod_bonus) {
prod = Rational.from_float_approximate(d.prod_bonus)
}
buildings.push(new Building(
d.key,
d.localized_name.en,
d.icon_col,
d.icon_row,
d.crafting_categories,
Rational.from_float_approximate(d.crafting_speed),
prod,
d.module_slots,
Rational.from_float_approximate(d.energy_usage),
fuel
))
}
for (let d of data.rocket_silo) {
buildings.push(new RocketSilo(
d.key,
d.localized_name.en,
d.icon_col,
d.icon_row,
d.crafting_categories,
Rational.from_float_approximate(d.crafting_speed),
zero,
d.module_slots,
Rational.from_float_approximate(d.energy_usage),
null
))
}
for (let d of data.offshore_pumps) {
// Pumping speed is given in units/tick.
let speed = Rational.from_float_approximate(d.pumping_speed).mul(Rational.from_float(60))
buildings.push(new OffshorePump(
d.key,
d.localized_name.en,
d.icon_col,
d.icon_row,
speed,
))
}
for (let d of data.mining_drills) {
if (d.key == "pumpjack") {
continue
}
let fuel = null
if (d.energy_source && d.energy_source.type === "burner") {
fuel = d.energy_source.fuel_category
}
buildings.push(new Miner(
d.key,
d.localized_name.en,
d.icon_col,
d.icon_row,
d.resource_categories,
Rational.from_float_approximate(d.mining_speed),
d.module_slots,
Rational.from_float_approximate(d.energy_usage),
fuel
))
}
return buildings
}