8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
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
...
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
|
-- a+b = compose a new spec from the spec parts a and b.
-- this is used e.g. for creating tier-based
-- fabspecs.
--
-- * used for determining quantities. that is,
-- f*x = spec to make x instances of f
--
-- new fab fields must be defined in starlit.type.fab.opClass.
-- this maps a name to fn(a,b,n) -> quant, where a is the first
-- argument, b is a compounding amount, and n is a quantity of
-- items to produce. fields that are unnamed will be underwritten
local function fQuant(a,b,n) return ((a or 0)+(b or 0))*n end
local function fFac (a,b,n)
if a == nil and b == nil then return nil end
................................................................................
f = (a or 1)*(b or 1)
end
return f*n
end
local function fReq (a,b,n) return a or b end
local function fFlag (a,b,n) return a and b end
local function fSize (a,b,n) return math.max(a,b) end
local opClass = {
-- fabrication eligibility will be determined by which kinds
-- of input a particular fabricator can introduce. e.g. a
-- printer with a but no cache can only print items whose
-- recipe only names elements as ingredients
-- ingredients
element = fQuant; -- (g)
gas = fQuant; -- ()
liquid = fQuant; -- (l)
crystal = fQuant; -- (g)
item = fQuant; -- n
metal = fQuant; -- (g)
metalIngot = fQuant; -- (g)
-- factors
cost = fFac; -- units vary
time = fFac; -- (s)
-- print: base printing time
size = fSize;
-- printBay: size of the printer bay necessary to produce the item
req = fReq;
flag = fFlag; -- means that can be used to produce the item & misc flags
-- print: allow production with a printer
-- smelt: allow production with a smelter
-- all else defaults to underwrite
}
local F = string.format
local strClass = {
element = function(x, n)
local el = starlit.world.material.element[x]
return lib.math.si('g', n) .. ' ' .. (el.sym or el.name)
end;
metal = function(x, n)
local met = starlit.world.material.metal[x]
return lib.math.si('g', n) .. ' ' .. met.name
end;
liquid = function(x, n)
local liq = starlit.world.material.liquid[x]
return lib.math.si('L', n) .. ' ' .. liq.name
end;
gas = function(x, n)
local gas = starlit.world.material.gas[x]
return lib.math.si('g', n) .. ' ' .. gas.name
end;
item = function(x, n)
local i = minetest.registered_items[x]
return tostring(n) .. 'x ' .. i.short_description
end;
}
local order = {
'element', 'metal', 'liquid', 'gas', 'item'
}
local lib = starlit.mod.lib
local fab fab = lib.class {
__name = 'starlit:fab';
opClass = opClass;
strClass = strClass;
order = order;
construct = function(q) return q end;
__index = {
elementalize = function(self)
local e = fab {element = self.element or {}}
for _, kind in pairs {'metal', 'gas', 'liquid'} do
for m,mass in pairs(self[kind] or {}) do
................................................................................
local fml = {}
for i, v in ipairs(sub) do fml[i] = v.f end
if f then table.insert(fml, f) end
fml = table.concat(fml, ' + ')
return fml, ts
end;
};
__tostring = function(self)
local t = {}
for i,o in ipairs(order) do
if self[o] then
for mat,amt in pairs(self[o]) do
if amt > 0 then
table.insert(t, strClass[o](mat, amt))
end
end
end
end
return table.concat(t, ", ")
end;
__add = function(a,b)
local new = fab {}
for cat, vals in pairs(a) do
new[cat] = lib.tbl.copy(vals)
end
for cat, vals in pairs(b) do
if not new[cat] then
new[cat] = lib.tbl.copy(vals)
else
local f = opClass[cat]
for k,v in pairs(vals) do
local n = f(new[cat][k], v, 1)
new[cat][k] = n > 0 and n or nil
end
end
end
return new
end;
__mul = function(x,n)
local new = fab {}
for cat, vals in pairs(x) do
new[cat] = {}
local f = opClass[cat]
for k,v in pairs(vals) do
local num = f(v,nil,n)
new[cat][k] = num > 0 and num or nil
end
end
return new
end;
|
|
>
>
>
>
|
<
<
|
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
|
>
>
|
<
>
>
>
>
>
>
>
>
>
>
|
<
>
|
|
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
<
|
|
|
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
..
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
...
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
|
-- a+b = compose a new spec from the spec parts a and b.
-- this is used e.g. for creating tier-based
-- fabspecs.
--
-- * used for determining quantities. that is,
-- f*x = spec to make x instances of f
--
-- new fab fields must be defined in starlit.type.fab.fields.
-- this maps a name to fn(a,b,n) -> quant, where a is the first
-- argument, b is a compounding amount, and n is a quantity of
-- items to produce. fields that are unnamed will be underwritten
local function fQuant(a,b,n) return ((a or 0)+(b or 0))*n end
local function fFac (a,b,n)
if a == nil and b == nil then return nil end
................................................................................
f = (a or 1)*(b or 1)
end
return f*n
end
local function fReq (a,b,n) return a or b end
local function fFlag (a,b,n) return a and b end
local function fSize (a,b,n) return math.max(a,b) end
local F = string.format
local lib = starlit.mod.lib
local fields = {
-- fabrication eligibility will be determined by which kinds
-- of input a particular fabricator can introduce. e.g. a
-- printer with a but no cache can only print items whose
-- recipe only names elements as ingredients
element = {
name = {"element", "elements"};
string = function(x, n, long)
local el = starlit.world.material.element.db[x]
return lib.math.si('g', n) .. ' ' .. ((not long and el.sym) or el.name)
end;
image = function(x, n)
return string.format('starlit-element-%s.png', x)
end;
op = fQuant;
};
metal ={
name = {"metal", "metals"};
string = function(x, n)
local met = starlit.world.material.metal.db[x]
return lib.math.si('g', n) .. ' ' .. met.name
end;
image = function(x, n)
local met = starlit.world.material.metal.db[x]
return ItemStack(met.form.ingot):get_definition().inventory_image
end;
op = fQuant;
};
liquid = {
name = {"liquid", "liquids"};
string = function(x, n)
local liq = starlit.world.material.liquid.db[x]
return lib.math.si('L', n) .. ' ' .. liq.name
end;
op = fQuant;
};
gas = {
name = {"gas", "gasses"};
string = function(x, n)
local gas = starlit.world.material.gas.db[x]
return lib.math.si('g', n) .. ' ' .. gas.name
end;
op = fQuant;
};
-- crystal = {
-- op = fQuant;
-- };
item = {
name = {"item", "items"};
string = function(x, n)
local i = minetest.registered_items[x]
return tostring(n) .. 'x ' .. i.short_description
end;
};
-- factors
cost = {op=fFac}; -- units vary
time = {op=fFac}; -- (s)
-- print: base printing time
size = {op=fSize};
-- printBay: size of the printer bay necessary to produce the item
req = {op=fReq};
flag = {op=fFlag}; -- means that can be used to produce the item & misc flags
-- print: allow production with a printer
-- smelt: allow production with a smelter
-- all else defaults to underwrite
}
local order = {
'element', 'metal', 'liquid', 'gas', 'item'
}
local lib = starlit.mod.lib
local fab fab = lib.class {
__name = 'starlit:fab';
fields = fields;
order = order;
construct = function(q) return q end;
__index = {
elementalize = function(self)
local e = fab {element = self.element or {}}
for _, kind in pairs {'metal', 'gas', 'liquid'} do
for m,mass in pairs(self[kind] or {}) do
................................................................................
local fml = {}
for i, v in ipairs(sub) do fml[i] = v.f end
if f then table.insert(fml, f) end
fml = table.concat(fml, ' + ')
return fml, ts
end;
visualize = function(self)
local all = {}
for i,o in ipairs(order) do
local t = {}
if self[o] then
for mat,amt in pairs(self[o]) do
local v = {}
v.id = mat
v.n = amt
if fields[o].string then
v.label = fields[o].string(mat,amt,true)
end
if fields[o].image then
v.img = fields[o].image(mat,amt)
end
table.insert(t,v)
end
end
if fields[o].sort then
table.sort(t, function(a,b) return fields[o].sort(a.id, b.id) end)
end
if next(t) then table.insert(all, {
id=o, list=t;
header=fields[o].name[t[2] and 2 or 1];
}) end
end
return all
end;
};
__tostring = function(self)
local t = {}
for i,o in ipairs(order) do
if self[o] and fields[o].string then
for mat,amt in pairs(self[o]) do
if amt > 0 then
table.insert(t, fields[o].string(mat, amt))
end
end
end
end
return table.concat(t, ", ")
end;
__add = function(a,b)
local new = fab {}
for cat, vals in pairs(a) do
new[cat] = lib.tbl.copy(vals)
end
for cat, vals in pairs(b) do
if not new[cat] then
new[cat] = lib.tbl.copy(vals)
else
local f = fields[cat].op
for k,v in pairs(vals) do
local n = f(new[cat][k], v, 1)
new[cat][k] = n > 0 and n or nil
end
end
end
return new
end;
__mul = function(x,n)
local new = fab {}
for cat, vals in pairs(x) do
new[cat] = {}
local f = fields[cat].op
for k,v in pairs(vals) do
local num = f(v,nil,n)
new[cat][k] = num > 0 and num or nil
end
end
return new
end;
|