31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
..
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
...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
...
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
|
local R = sorcery.lathe.recipes
sorcery.lathe.get = function(pos,idx,howmany)
local inv = minetest.get_meta(pos):get_inventory()
local tool = inv:get_stack('tool',1)
local tech = sorcery.lathe.tooltech(tool)
if not tech then return nil end
local wkpc = inv:get_stack('workpiece',1)
howmany = howmany or wkpc:get_count()
local rec = R[wkpc:get_name()][tech][idx]
local outn = ItemStack(rec.output):get_count()
local ntimes = math.floor(howmany / (rec.mass or 1))
return {
tool = tool, wkpc = wkpc;
cost = rec.cost * ntimes;
ntimes = ntimes;
tqty = math.floor(howmany / outn) * (rec.mass or 1), outn = outn;
gqty = ntimes * outn;
tech = tech;
rec = rec;
inv = inv;
}
end
sorcery.lathe.update = function(pos)
................................................................................
local rec = R[wkpc:get_name()][tech]
tech = sorcery.lathe.techs[tech]
-- fill in the preview slots
local j = 1
for i=1, inv:get_size 'preview' do
local stk = ItemStack()
if rec[i] and minetest.registered_items[ItemStack(rec[i].output):get_name()] and (rec[i].mass == nil or rec[i].mass <= wkpc:get_count()) then
local l = sorcery.lathe.get(pos, i, wkpc:get_count())
local max = l.ntimes --math.floor(wkpc:get_count() / (rec[i].mass or 1))
if tech.dmg then
-- TODO count remaining tool uses
elseif tech.consume then
max = math.min(max, tool:get_count())
end
if max > 0 then
stk = ItemStack(rec[i].output)
stk:set_count(stk:get_count() * max)
end
end
inv:set_stack('preview',i,stk)
j = j + 1
end
-- make sure remaining slots are clear
................................................................................
local m = minetest.get_meta(pos)
local i = m:get_inventory()
i:set_size('workpiece', 1);
i:set_size('tool', 1);
i:set_size('preview', 8);
m:set_string('formspec', [[
formspec_version[3] size[10.25,8]
list[context;tool;1.50,1;1,1]
list[context;workpiece;3,1;1,1]
list[context;preview;5.25,0.25;4,2]
list[current_player;main;0.25,3;8,4]
listring[current_player;main] listring[context;workpiece]
listring[current_player;main] listring[context;tool]
listring[current_player;main] listring[context;preview]
listring[current_player;main]
................................................................................
on_metadata_inventory_put = sorcery.lathe.update;
on_metadata_inventory_take = function(pos, list, idx, stack, user)
if list == 'preview' then
local l = sorcery.lathe.get(pos,idx,stack:get_count())
if sorcery.lathe.techs[l.tech].consume then
l.tool:take_item(l.cost)
elseif sorcery.lathe.techs[l.tech].dmg then
local mat = sorcery.itemclass.get(l.tool,'material')
local mmat = sorcery.itemclass.get(l.wkpc,'metal')
local dur = 100
local lfac = 1
if mat then
local dur = mat.data.durability or dur
lfac = (mmat and mmat.data.level or 1) /
(mat.data.maxlevel or mat.data.level or 1)
end
local ch = 65535 / dur
l.tool:add_wear(ch * l.cost * lfac)
end
l.wkpc:take_item(l.tqty)
l.inv:set_stack('tool', 1, l.tool)
l.inv:set_stack('workpiece', 1, l.wkpc)
if l.rec.leftover then
sorcery.lib.node.insert(ItemStack(l.rec.leftover), 'workpiece', pos, user, l.inv)
end
|
<
>
|
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
>
|
|
>
|
<
>
>
>
>
>
>
>
>
|
|
|
<
<
<
<
<
<
<
<
<
<
|
|
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
..
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
...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
...
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
local R = sorcery.lathe.recipes
sorcery.lathe.get = function(pos,idx,howmany)
local inv = minetest.get_meta(pos):get_inventory()
local tool = inv:get_stack('tool',1)
local tech = sorcery.lathe.tooltech(tool)
if not tech then return nil end
local wkpc = inv:get_stack('workpiece',1)
local rec = R[wkpc:get_name()][tech][idx]
local outn = ItemStack(rec.output):get_count()
howmany = howmany or math.floor(wkpc:get_count()/(rec.mass or 1))*outn
local ntimes = math.floor(howmany / outn)
local tmat = sorcery.itemclass.get(tool,'material')
local wmat = sorcery.itemclass.get(wkpc,'material')
local dur = 100
local lfac = 1
if tmat then
local dur = tmat.data.durability or dur
lfac = (wmat and wmat.data.level or 1) /
(tmat.data.maxlevel or tmat.data.level or 1)
end
local ch = 65535 / dur
local wear = 2 * (ch * rec.cost * ntimes * lfac);
return {
tool = tool, wkpc = wkpc;
cost = rec.cost * ntimes;
wear = wear;
ntimes = ntimes;
tqty = ntimes * (rec.mass or 1), outn = outn;
tech = tech;
rec = rec;
inv = inv;
}
end
sorcery.lathe.update = function(pos)
................................................................................
local rec = R[wkpc:get_name()][tech]
tech = sorcery.lathe.techs[tech]
-- fill in the preview slots
local j = 1
for i=1, inv:get_size 'preview' do
local stk = ItemStack()
local os = rec[i] and ItemStack(rec[i].output)
if rec[i] and minetest.registered_items[os:get_name()] and (rec[i].mass == nil or rec[i].mass <= wkpc:get_count()) then
local l = sorcery.lathe.get(pos, i)
local max = l.ntimes
--math.floor(wkpc:get_count() / (rec[i].mass or 1))
if tech.dmg then
local lw = l.wear
while lw + tool:get_wear() > 65535 do
max = max - 1
if max == 0 then break end
lw = sorcery.lathe.get(pos, i, max).wear
end
elseif tech.consume then
max = math.min(max, tool:get_count())
end
if max > 0 then
stk = ItemStack(rec[i].output)
local ct = math.min(stk:get_count() * max, stk:get_stack_max())
ct = ct - (ct % os:get_count())
stk:set_count(ct)
end
end
inv:set_stack('preview',i,stk)
j = j + 1
end
-- make sure remaining slots are clear
................................................................................
local m = minetest.get_meta(pos)
local i = m:get_inventory()
i:set_size('workpiece', 1);
i:set_size('tool', 1);
i:set_size('preview', 8);
m:set_string('formspec', [[
formspec_version[3] size[10.25,8]
list[context;tool;1.25,1;1,1]
list[context;workpiece;2.75,1;1,1]
list[context;preview;5.25,0.25;4,2]
list[current_player;main;0.25,3;8,4]
listring[current_player;main] listring[context;workpiece]
listring[current_player;main] listring[context;tool]
listring[current_player;main] listring[context;preview]
listring[current_player;main]
................................................................................
on_metadata_inventory_put = sorcery.lathe.update;
on_metadata_inventory_take = function(pos, list, idx, stack, user)
if list == 'preview' then
local l = sorcery.lathe.get(pos,idx,stack:get_count())
if sorcery.lathe.techs[l.tech].consume then
l.tool:take_item(l.cost)
elseif sorcery.lathe.techs[l.tech].dmg then
l.tool:add_wear(l.wear)
end
l.wkpc:take_item(l.tqty)
l.inv:set_stack('tool', 1, l.tool)
l.inv:set_stack('workpiece', 1, l.wkpc)
if l.rec.leftover then
sorcery.lib.node.insert(ItemStack(l.rec.leftover), 'workpiece', pos, user, l.inv)
end
|