forked from KirkMcDonald/kirkmcdonald.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.js
242 lines (214 loc) · 8.39 KB
/
init.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
/*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"
var recipeTable
// Contains collections of items and recipes. (solve.js)
var solver
// Contains module and factory settings, as well as other settings. (factory.js)
var spec
// Map from module name to Module object.
var modules
// Array of modules, sorted by 'order'.
var sortedModules
// Map from short module name to Module object.
var shortModules
// Array of arrays of modules, separated by category and sorted.
var moduleRows
// Array of Belt objects, sorted by speed.
var belts
// Array of Fuel objects, sorted by value.
var fuel
// Array of item groups, in turn divided into subgroups. For display purposes.
var itemGroups
// Boolean with whether to use old (0.16) calculations.
var useLegacyCalculations
// Size of the sprite sheet, as [x, y] array.
var spriteSheetSize
var initDone = false
// Set the page back to a state immediately following initial setup, but before
// the dataset is loaded for the first time.
//
// This is intended to be called when the top-level dataset is changed.
// Therefore, it also resets the fragment and settings.
function reset() {
window.location.hash = ""
build_targets = []
var targetList = document.getElementById("targets")
var plus = targetList.lastChild
var newTargetList = document.createElement("ul")
newTargetList.id = "targets"
newTargetList.classList.add("targets")
newTargetList.appendChild(plus)
var targetParent = document.getElementById("targetparent")
targetParent.replaceChild(newTargetList, targetList)
var oldSteps = document.getElementById("steps")
var newSteps = document.createElement("table")
newSteps.id = "steps"
oldSteps.parentNode.replaceChild(newSteps, oldSteps)
var oldTotals = document.getElementById("totals")
var newTotals = document.createElement("table")
newTotals.id = "totals"
oldTotals.parentNode.replaceChild(newTotals, oldTotals)
}
function loadDataRunner(modName, callback) {
var xobj = new XMLHttpRequest()
var mod = MODIFICATIONS[modName]
if (!mod) {
mod = MODIFICATIONS[DEFAULT_MODIFICATION]
}
spriteSheetSize = mod.sheetSize
useLegacyCalculations = mod.legacy
var filename = "data/" + mod.filename
xobj.overrideMimeType("application/json")
xobj.open("GET", filename, true)
xobj.onreadystatechange = function() {
if (xobj.readyState == 4 && xobj.status == "200") {
var data = JSON.parse(xobj.responseText)
callback(data)
}
}
xobj.send(null)
}
function loadData(modName, settings) {
recipeTable = new RecipeTable(document.getElementById("totals"))
if (!settings) {
settings = {}
}
loadDataRunner(modName, function(data) {
getSprites(data)
var graph = getRecipeGraph(data)
modules = getModules(data)
sortedModules = sorted(modules, function(m) { return modules[m].order })
moduleRows = []
let category = null
for (let moduleName of sortedModules) {
let module = modules[moduleName]
if (module.category !== category) {
category = module.category
moduleRows.push([])
}
moduleRows[moduleRows.length - 1].push(module)
}
shortModules = {}
for (var moduleName in modules) {
var module = modules[moduleName]
shortModules[module.shortName()] = module
}
var factories = getFactories(data)
spec = new FactorySpec(factories)
if ("ignore" in settings) {
var ignore = settings.ignore.split(",")
for (var i = 0; i < ignore.length; i++) {
spec.ignore[ignore[i]] = true
}
}
var items = graph[0]
var recipes = graph[1]
belts = getBelts(data)
fuel = getFuel(data, items)["chemical"]
itemGroups = getItemGroups(items, data)
solver = new Solver(items, recipes)
renderSettings(settings)
solver.findSubgraphs(spec)
if ("items" in settings && settings.items != "") {
var targets = settings.items.split(",")
for (var i=0; i < targets.length; i++) {
var targetString = targets[i]
var parts = targetString.split(":")
var name = parts[0]
var target = addTarget(name)
var type = parts[1]
if (type == "f") {
var j = parts[2].indexOf(";")
if (j === -1) {
target.setFactories(0, parts[2])
} else {
var count = parts[2].slice(0, j)
var idx = Number(parts[2].slice(j+1))
target.setFactories(idx, count)
target.displayRecipes()
}
} else if (type == "r") {
target.setRate(parts[2])
} else {
throw new Error("unknown target type")
}
}
} else {
addTarget()
}
if ("modules" in settings && settings.modules != "") {
var moduleSettings = settings.modules.split(",")
for (var i=0; i < moduleSettings.length; i++) {
var bothSettings = moduleSettings[i].split(";")
var factoryModuleSettings = bothSettings[0]
var beaconSettings = bothSettings[1]
var singleModuleSettings = factoryModuleSettings.split(":")
var recipeName = singleModuleSettings[0]
var recipe = recipes[recipeName]
var moduleNameList = singleModuleSettings.slice(1)
for (var j=0; j < moduleNameList.length; j++) {
var moduleName = moduleNameList[j]
if (moduleName) {
var module
if (moduleName in modules) {
module = modules[moduleName]
} else if (moduleName in shortModules) {
module = shortModules[moduleName]
} else if (moduleName === "null") {
module = null
}
if (module !== undefined) {
spec.setModule(recipe, j, module)
}
}
}
if (beaconSettings) {
beaconSettings = beaconSettings.split(":")
var moduleName = beaconSettings[0]
var module
if (moduleName in modules) {
module = modules[moduleName]
} else if (moduleName in shortModules) {
module = shortModules[moduleName]
} else if (moduleName === "null") {
module = null
}
var factory = spec.getFactory(recipe)
if (factory) {
var count = RationalFromFloat(Number(beaconSettings[1]))
factory.beaconModule = module
factory.beaconCount = count
}
}
}
}
initDone = true
itemUpdate()
// Prune factory spec after first solution is calculated.
pruneSpec(globalTotals)
window.location.hash = "#" + formatSettings()
})
}
function init() {
var settings = loadSettings(window.location.hash)
if (OVERRIDE !== null) {
addOverrideOptions(OVERRIDE)
}
renderDataSetOptions(settings)
if ("tab" in settings) {
currentTab = settings.tab + "_tab"
}
loadData(currentMod(), settings)
// We don't need to call clickVisualize here, as we will properly render
// the graph when we call itemUpdate() at the end of initialization.
clickTab(currentTab)
}