-
Notifications
You must be signed in to change notification settings - Fork 6
/
factory.js
469 lines (452 loc) · 15 KB
/
factory.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
/*Copyright 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 { Formatter } from "./align.js"
import { renderDebug } from "./debug.js"
import { displayItems } from "./display.js"
import { formatSettings } from "./fragment.js"
import { Rational, zero, half, one } from "./rational.js"
import { DisabledRecipe } from "./recipe.js"
import { solve } from "./solve.js"
import { BuildTarget } from "./target.js"
import { renderTotals } from "./visualize.js"
const DEFAULT_ITEM_NAME = "Circuit board"
// higher in list == harder to acquire
// Broadly speaking, corresponds to tech tree.
// Much of this order is arbitrary; don't read too much into it.
// This strictly relates to acquisition as a resource directly from the world.
// If an item can be acquired from crafting via lower-priority items, then the
// solution will prefer the craft over harvesting the resource.
const DEFAULT_PRIORITY = [
// common starting-world resources
["Water", "Iron ore", "Copper ore", "Coal ore", "Stone ore"],
// starting-world resources acquired with tech
["Crude oil"],
// everything else; figure it out later
[
"Hydrogen",
"Deuterium",
"Silicon ore",
"Titanium ore",
"Fire ice",
"Kimberlite ore",
"Organic crystal",
"Spiniform stalagmite crystal",
"Sulfuric acid",
"Optical grating crystal",
"Unipolar magnet",
"Fractal silicon",
"Critical photon",
],
// manually harvested
["Plant fuel", "Log"],
]
class PriorityLevel {
constructor() {
this.recipes = new Set()
}
getRecipeArray() {
return Array.from(this.recipes)
}
add(recipe) {
this.recipes.add(recipe)
}
remove(recipe) {
this.recipes.delete(recipe)
}
has(recipe) {
return this.recipes.has(recipe)
}
isEmpty() {
return this.recipes.size == 0
}
}
//export let DEFAULT_BELT = 0
class FactorySpecification {
constructor() {
// Game data definitions
this.items = null
this.recipes = null
this.buildings = null
this.belts = null
this.defaultItem = null
this.buildTargets = []
this.belt = null
this.ignore = new Set()
this.disable = new Set()
this.defaultDisable = new Set()
this.priority = []
this.format = new Formatter()
this.lastPartial = null
this.lastTableau = null
this.lastMetadata = null
this.lastSolution = null
}
setData(items, recipes, buildings, belts) {
this.items = items
let itemMap = new Map()
this.defaultDisable = new Set()
for (let [itemKey, item] of items) {
if (item.name === DEFAULT_ITEM_NAME) {
this.defaultItem = item
}
let rowMap = itemMap.get(item.category)
if (rowMap === undefined) {
rowMap = new Map()
itemMap.set(item.category, rowMap)
}
let row = rowMap.get(item.row)
if (row === undefined) {
row = []
rowMap.set(item.row, row)
}
row.push(item)
// By default, disable all recipes but one for (most) items that
// have multiple recipes.
if (item.recipes.length > 1) {
let chosen = null
let skip = false
for (let recipe of item.recipes) {
if (recipe.isResource()) {
chosen = recipe
}
// If any of this item's recipes produce multiple items,
// then don't disable any of these recipes, and let the
// linear program sort it out.
if (recipe.products.length > 1) {
skip = true
}
}
if (skip) {
continue
}
if (chosen === null) {
chosen = item.recipes[0]
}
for (let recipe of item.recipes) {
if (recipe !== chosen) {
this.defaultDisable.add(recipe)
}
}
}
}
// item vs. building ends up in the same order as in the data file
this.itemRows = []
for (let [category, rowMap] of itemMap) {
let row = []
for (let [rowNumber, rowItems] of rowMap) {
row.push(rowItems)
}
row.sort((a, b) => a[0].row - b[0].row)
this.itemRows.push(row)
}
this.recipes = recipes
this.resourceNameMap = new Map()
for (let [key, recipe] of recipes) {
if (recipe.isResource()) {
this.resourceNameMap.set(recipe.name, recipe)
}
}
this.buildings = new Map()
for (let building of buildings) {
let category = this.buildings.get(building.category)
if (category === undefined) {
category = []
this.buildings.set(building.category, category)
}
category.push(building)
/*if (minerCategories.has(building.category)) {
this.miners.set(building.key, building)
}*/
}
this.belts = belts
this.belt = belts.values().next().value
// Called during renderSettings.
//this.setDefaultPriority()
//this.initMinerSettings()
}
setDefaultDisable() {
this.disable.clear()
for (let recipe of this.defaultDisable) {
this.disable.add(recipe)
}
}
isDefaultDisable() {
if (this.disable.size !== this.defaultDisable.size) {
return false
}
for (let recipe of this.disable) {
if (!this.defaultDisable.has(recipe)) {
return false
}
}
return true
}
setDisable(recipe) {
this.disable.add(recipe)
}
setEnable(recipe) {
this.disable.delete(recipe)
}
setDefaultPriority() {
let tiers = []
for (let tierNames of DEFAULT_PRIORITY) {
let tier = []
for (let name of tierNames) {
tier.push(this.resourceNameMap.get(name).key)
}
tiers.push(tier)
}
this.setPriorities(tiers)
}
setPriorities(tiers) {
this.priority = []
for (let tier of tiers) {
let tierList = new PriorityLevel()
for (let key of tier) {
let recipe = this.recipes.get(key)
if (!recipe) {
throw new Error("bad resource key: " + key)
}
tierList.add(recipe)
}
this.priority.push(tierList)
}
}
isDefaultPriority() {
if (this.priority.length !== DEFAULT_PRIORITY.length) {
return false
}
for (let i = 0; i < this.priority.length; i++) {
let pri = this.priority[i]
let def = DEFAULT_PRIORITY[i]
if (pri.recipes.size !== def.length) {
return false
}
for (let name of def) {
if (!pri.has(this.resourceNameMap.get(name))) {
return false
}
}
}
return true
}
/*initMinerSettings() {
this.minerSettings = new Map()
for (let [recipeKey, recipe] of this.recipes) {
if (minerCategories.has(recipe.category)) {
let miners = this.buildings.get(recipe.category)
// Default to miner mk1.
let miner = miners[0]
// Default to normal purity.
let purity = DEFAULT_PURITY
this.minerSettings.set(recipe, {miner, purity})
}
}
}*/
// Moves recipe to the given priority level. If the recipe's old
// priority is empty as a result, removes it and returns true. Returns
// false otherwise.
setPriority(recipe, priority) {
let oldPriority = null
let i = 0
for (; i < this.priority.length; i++) {
let p = this.priority[i]
if (p.has(recipe)) {
oldPriority = p
break
}
}
oldPriority.remove(recipe)
priority.add(recipe)
if (oldPriority.isEmpty()) {
this.priority.splice(i, 1)
return true
}
return false
}
// Creates a new priority level immediately preceding the given one.
// If the given priority is null, adds the new priority to the end of
// the priority list.
//
// Returns the new PriorityLevel.
addPriorityBefore(priority) {
let newPriority = new PriorityLevel()
if (priority === null) {
this.priority.push(newPriority)
} else {
for (let i = 0; i < this.priority.length; i++) {
if (this.priority[i] === priority) {
this.priority.splice(i, 0, newPriority)
break
}
}
}
return newPriority
}
getUses(item) {
let recipes = []
for (let recipe of item.uses) {
if (!this.disable.has(recipe)) {
recipes.push(recipe)
}
}
return recipes
}
getRecipes(item) {
let recipes = []
for (let recipe of item.recipes) {
if (!this.disable.has(recipe)) {
recipes.push(recipe)
}
}
// The logic here is complicated, but has to do with which recipes we
// want to be present in the solution, related to ignored items.
if (recipes.length === 0) {
let recipe = item.disableRecipe
if (recipe === null) {
recipe = new DisabledRecipe(item, true)
item.disableRecipe = recipe
}
return [recipe]
}
if (this.ignore.has(item)) {
if (recipes.length !== 1 || recipes[0].products.length !== 1) {
let recipe = item.ignoreRecipe
if (recipe === null) {
recipe = new DisabledRecipe(item, false)
item.ignoreRecipe = recipe
}
return [recipe, ...recipes]
}
}
return recipes
}
_getItemGraph(item, recipes) {
for (let recipe of this.getRecipes(item)) {
if (recipes.has(recipe)) {
continue
}
recipes.add(recipe)
for (let ing of recipe.ingredients) {
this._getItemGraph(ing.item, recipes)
}
}
}
// Returns the set of recipes which may contribute to the production of
// the given collection of items.
getRecipeGraph(items) {
let graph = new Set()
for (let [item, rate] of items) {
this._getItemGraph(item, graph)
}
return graph
}
getRecipe(item) {
return item.recipes[0]
}
getBuilding(recipe) {
if (recipe === null || recipe.category === null) {
return null
//} else if (this.minerSettings.has(recipe)) {
// return this.minerSettings.get(recipe).miner
} else {
// NOTE: Only miners offer alternative buildings. May need to
// revisit this if higher tiers of constructors are added.
return this.buildings.get(recipe.category)[0]
}
}
// Returns the recipe-rate at which a single building can produce a recipe.
// Returns null for recipes that do not have a building.
getRecipeRate(recipe) {
let building = this.getBuilding(recipe)
if (building === null) {
return null
}
return building.getRecipeRate(this, recipe)
}
getCount(recipe, rate) {
let building = this.getBuilding(recipe)
if (building === null) {
return zero
}
return building.getCount(this, recipe, rate)
}
getBeltCount(rate) {
return rate.div(this.belt.rate)
}
/*getPowerUsage(recipe, rate, itemCount) {
let building = this.getBuilding(recipe)
if (building === null) {
return {average: zero, peak: zero}
}
let count = this.getCount(recipe, rate)
let average = building.power.mul(count)
let peak = building.power.mul(count.ceil())
let overclock = this.overclock.get(recipe)
if (overclock !== undefined) {
// The result of this exponent will typically be irrational, so
// this approximation is a necessity. Because overclock is limited
// to the range [1.0, 2.5], any imprecision introduced by this
// approximation is minimal (and is probably less than is present
// in the game itself).
let overclockFactor = Rational.from_float(Math.pow(overclock.toFloat(), 1.6))
average = average.mul(overclockFactor)
peak = peak.mul(overclockFactor)
}
return {average, peak}
}*/
addTarget(itemKey) {
if (itemKey === undefined) {
itemKey = this.defaultItem.key
}
let item = this.items.get(itemKey)
let target = new BuildTarget(this.buildTargets.length, itemKey, item, this.itemRows)
this.buildTargets.push(target)
d3.select("#targets").insert(() => target.element, "#plusButton")
return target
}
removeTarget(target) {
this.buildTargets.splice(target.index, 1)
for (let i=target.index; i < this.buildTargets.length; i++) {
this.buildTargets[i].index--
}
d3.select(target.element).remove()
}
toggleIgnore(recipe) {
if (this.ignore.has(recipe)) {
this.ignore.delete(recipe)
} else {
this.ignore.add(recipe)
}
}
solve() {
let outputs = new Map()
for (let target of this.buildTargets) {
let rate = target.getRate()
rate = rate.add(outputs.get(target.item) || zero)
outputs.set(target.item, rate)
}
let totals = solve(this, outputs)
return totals
}
setHash() {
window.location.hash = "#" + formatSettings()
}
updateSolution() {
let totals = this.solve()
displayItems(this, totals)
//renderTotals(totals, this.buildTargets, this.ignore)
this.setHash()
renderDebug()
}
}
export let spec = new FactorySpecification()
window.spec = spec