-
-
Notifications
You must be signed in to change notification settings - Fork 923
/
recipe.js
121 lines (109 loc) · 3.18 KB
/
recipe.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
var recipes = require('./enums/recipes');
module.exports = Recipe;
function Recipe(recipeEnumItem) {
this.type = recipeEnumItem.type;
this.count = recipeEnumItem.count;
this.metadata = recipeEnumItem.metadata;
var x, y, row, myRow;
this.inShape = recipeEnumItem.inShape ?
reformatShape(recipeEnumItem.inShape) : null;
this.outShape = recipeEnumItem.outShape ?
reformatShape(recipeEnumItem.outShape) : null;
this.ingredients = recipeEnumItem.ingredients ?
reformatIngredients(recipeEnumItem.ingredients) : null;
this.delta = computeDelta(this);
this.requiresTable = computeRequiresTable(this);
}
Recipe.find = function(itemType, metadata) {
var results = [];
(recipes[itemType] || []).forEach(function(recipeEnumItem) {
if ((metadata == null || recipeEnumItem.metadata === metadata)) {
results.push(new Recipe(recipeEnumItem));
}
});
return results;
};
function computeRequiresTable(recipe) {
var spaceLeft = 4;
var x, y, row;
if (recipe.inShape) {
if (recipe.inShape.length > 2) return true;
for (y = 0; y < recipe.inShape.length; ++y) {
row = recipe.inShape[y];
if (row.length > 2) return true;
for (x = 0; x < row.length; ++x) {
if (row[x]) spaceLeft -= 1;
}
}
}
if (recipe.ingredients) spaceLeft -= recipe.ingredients.length;
return spaceLeft < 0;
}
function computeDelta(recipe) {
// returns a list of item type and metadata with the delta how many more or
// less you will have in your inventory after crafting
var delta = [];
if (recipe.inShape) applyShape(recipe.inShape, -1);
if (recipe.outShape) applyShape(recipe.outShape, 1);
if (recipe.ingredients) applyIngredients(recipe.ingredients);
// add the result
add(recipe.type, recipe.metadata, recipe.count);
return delta;
function add(type, metadata, count) {
metadata = metadata == null ? null : metadata;
for (var i = 0; i < delta.length; ++i) {
var d = delta[i];
if (d.type === type && d.metadata === metadata) {
d.count += count;
return;
}
}
delta.push({
type: type,
metadata: metadata,
count: count,
});
}
function applyShape(shape, direction) {
var x, y, row;
for (y = 0; y < shape.length; ++y) {
row = recipe.inShape[y];
for (x = 0; x < row.length; ++x) {
if (row[x] != null) add(row[x].id, null, direction);
}
}
}
function applyIngredients(ingredients) {
var i, id;
for (i = 0; i < ingredients.length; ++i) {
id = ingredients[i].id;
add(ingredients[i].id, ingredients[i].metadata, -1);
}
}
}
function reformatShape(shape) {
var out = new Array(shape.length);
var x, y, row, outRow;
for (y = 0; y < shape.length; ++y) {
row = shape[y];
out[y] = outRow = new Array(row.length);
for (x = 0; x < outRow.length; ++x) {
outRow[x] = row[x] ? {
id: row[x],
metadata: null,
count: 1,
} : null;
}
}
return out;
}
function reformatIngredients(ingredients) {
var out = new Array(ingredients.length);
for (var i = 0; i < out.length; ++i) {
out[i] = {
id: ingredients[i].id,
metadata: ingredients[i].metadata,
};
}
return out;
}