forked from KirkMcDonald/kirkmcdonald.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve.js
170 lines (162 loc) · 5.43 KB
/
solve.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
/*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 UnknownRecipe(item) {
this.name = item.name
this.item = item
}
function walk(item, seen, solvers) {
for (var i = 0; i < solvers.length; i++) {
var m = solvers[i]
if (item.name in m.outputs) {
return m
}
}
seen[item.name] = item
for (var i = 0; i < item.recipes.length; i++) {
var recipe = item.recipes[i]
for (var j = 0; j < recipe.ingredients.length; j++) {
var ing = recipe.ingredients[j]
if (ing.item.name in seen) {
continue
}
var m = walk(ing.item, seen, solvers)
if (m) {
return m
}
}
}
return null
}
function insertBefore(array, newItem, existingItem) {
if (!existingItem) {
array.push(newItem)
return
}
for (var i = 0; i < array.length; i++) {
if (array[i] === existingItem) {
array.splice(i, 0, newItem)
return
}
}
array.push(newItem)
}
function topologicalOrder(matrixSolvers) {
var result = []
for (var i = 0; i < matrixSolvers.length; i++) {
var m = matrixSolvers[i]
var items = {}
// Obtain set of items depended on by the group.
for (var j = 0; j < m.inputRecipes.length; j++) {
var recipe = m.inputRecipes[j]
for (var k = 0; k < recipe.ingredients.length; k++) {
var ing = recipe.ingredients[k]
items[ing.item.name] = ing.item
}
}
var dep = null
for (var itemName in items) {
var item = items[itemName]
var m2 = walk(item, {}, matrixSolvers)
if (m2) {
dep = m2
break
}
}
insertBefore(result, m, dep)
}
return result
}
function Solver(items, recipes) {
this.items = items
this.recipes = recipes
this.disabledRecipes = {}
this.matrixSolvers = []
}
Solver.prototype = {
constructor: Solver,
findSubgraphs: function(spec) {
var r = findGroups(spec, this.items, this.recipes)
// Clear all group tags.
for (var recipeName in this.recipes) {
var recipe = this.recipes[recipeName]
recipe.displayGroup = null
recipe.solveGroup = null
}
for (var i = 0; i < r.simple.length; i++) {
var group = r.simple[i]
// The order in which these group IDs are assigned does not matter.
for (var recipeName in group) {
group[recipeName].displayGroup = i
}
}
var groups = r.groups
this.matrixSolvers = []
for (var i = 0; i < groups.length; i++) {
var group = groups[i]
this.matrixSolvers.push(new MatrixSolver(spec, group))
for (var recipeName in group) {
group[recipeName].solveGroup = i
}
}
this.matrixSolvers = topologicalOrder(this.matrixSolvers)
},
addDisabledRecipes: function(recipes) {
for (var recipeName in recipes) {
this.disabledRecipes[recipeName] = true
}
},
removeDisabledRecipes: function(recipes) {
for (var recipeName in recipes) {
delete this.disabledRecipes[recipeName]
}
},
solve: function(rates, ignore, spec) {
var unknowns = {}
var totals = new Totals()
for (var itemName in rates) {
var item = this.items[itemName]
var rate = rates[itemName]
var subTotals = item.produce(rate, ignore, spec)
totals.combine(subTotals)
}
if (Object.keys(totals.unfinished).length == 0) {
return totals
}
for (var i = 0; i < this.matrixSolvers.length; i++) {
var solver = this.matrixSolvers[i]
var match = solver.match(totals.unfinished)
if (Object.keys(match).length == 0) {
continue
}
var solution = solver.solveFor(match, spec, this.disabledRecipes)
for (var itemName in match) {
delete totals.unfinished[itemName]
}
for (var recipeName in solution.solution) {
var rate = solution.solution[recipeName]
var recipe = this.recipes[recipeName]
if (solver.inputRecipes.indexOf(recipe) !== -1) {
var ing = recipe.products[0]
var subRate = recipe.gives(ing.item, spec).mul(rate)
var subTotals = ing.item.produce(subRate, ignore, spec)
totals.combine(subTotals, true)
} else {
totals.add(recipeName, rate)
}
}
for (var itemName in solution.waste) {
totals.addWaste(itemName, solution.waste[itemName])
}
}
return totals
}
}