forked from FRodrigues42/factoratio
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.js
More file actions
80 lines (75 loc) · 2.61 KB
/
model.js
File metadata and controls
80 lines (75 loc) · 2.61 KB
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
/**
* Created by synopia on 27.06.2014.
*/
var model = {
productions: {},
treeLines: {},
id: 0,
init: function() {
model.id = 0;
model.productions = {};
model.treeLines = {};
model.dupes = {};
},
buildProductionTree: function (parentItem, item, relativeSpeed, parentLockSpeed, dupeCheck) {
var data = [];
var recipe = recipes[item];
var production = model.productions[item];
if (production == null) {
model.productions[item] = production = {relativeSpeed: 0, item: item, outputs: {}};
}
production.relativeSpeed += relativeSpeed;
if (parentItem) {
if (production.outputs[parentItem] == null) {
production.outputs[parentItem] = 0
}
production.outputs[parentItem] += relativeSpeed;
}
dupeCheck[item] = {item: item, data: data, duped: false};
if (recipe) {
$.each(recipe.ingredients, function(index, list) {
var ingredient = list[0];
var amount = list[1];
if (!dupeCheck[ingredient]) {
var childDupeCheck = $.extend({}, dupeCheck);
data.push(model.buildProductionTree(item, ingredient, relativeSpeed * amount / recipe.resultCount, parentLockSpeed, childDupeCheck));
} else {
// console.log('dupe at', dupeCheck[ingredient], data);
dupeCheck[ingredient].duped = true;
recipes[ingredient] = undefined; // allows resources to have proper names
model.dupes[ingredient] = true;
data.push(model.insertLine("p", undefined, undefined, [], true, parentLockSpeed));
}
});
}
return model.insertLine("p", item, relativeSpeed, model.dupes[item] ? [] : data, true, parentLockSpeed);
},
buildRatioTree: function() {
var data = [];
$.each(model.productions, function(item, production) {
var outputs = [];
$.each(production.outputs, function(outputItem, speed) {
var line = model.insertLine("r", outputItem, speed / production.relativeSpeed, undefined, true, logic.targetSpeed);
outputs.push(line);
});
var line = model.insertLine("r", item, production.relativeSpeed, outputs, false, logic.targetSpeed);
data.push(line);
});
return data;
},
insertLine: function (type, item, speed, children, open, parentLockSpeed) {
model.id++;
var uniqueId = type + "_" + model.id;
var line = {
id: uniqueId,
value: helpers.getName(item),
item: item,
relativeSpeed: speed,
open: open == undefined ? true : open,
data: children,
lockSpeed: parentLockSpeed
};
model.treeLines[uniqueId] = line;
return line;
}
};