Differences From
Artifact [f27b9cba36]:
31 31 local R = sorcery.lathe.recipes
32 32 sorcery.lathe.get = function(pos,idx,howmany)
33 33 local inv = minetest.get_meta(pos):get_inventory()
34 34 local tool = inv:get_stack('tool',1)
35 35 local tech = sorcery.lathe.tooltech(tool)
36 36 if not tech then return nil end
37 37 local wkpc = inv:get_stack('workpiece',1)
38 - howmany = howmany or wkpc:get_count()
39 38 local rec = R[wkpc:get_name()][tech][idx]
40 39 local outn = ItemStack(rec.output):get_count()
41 - local ntimes = math.floor(howmany / (rec.mass or 1))
40 + howmany = howmany or math.floor(wkpc:get_count()/(rec.mass or 1))*outn
41 + local ntimes = math.floor(howmany / outn)
42 +
43 + local tmat = sorcery.itemclass.get(tool,'material')
44 + local wmat = sorcery.itemclass.get(wkpc,'material')
45 + local dur = 100
46 + local lfac = 1
47 + if tmat then
48 + local dur = tmat.data.durability or dur
49 + lfac = (wmat and wmat.data.level or 1) /
50 + (tmat.data.maxlevel or tmat.data.level or 1)
51 + end
52 + local ch = 65535 / dur
53 + local wear = 2 * (ch * rec.cost * ntimes * lfac);
42 54 return {
43 55 tool = tool, wkpc = wkpc;
44 56 cost = rec.cost * ntimes;
57 + wear = wear;
45 58 ntimes = ntimes;
46 - tqty = math.floor(howmany / outn) * (rec.mass or 1), outn = outn;
47 - gqty = ntimes * outn;
59 + tqty = ntimes * (rec.mass or 1), outn = outn;
48 60 tech = tech;
49 61 rec = rec;
50 62 inv = inv;
51 63 }
52 64 end
53 65
54 66 sorcery.lathe.update = function(pos)
................................................................................
75 87 local rec = R[wkpc:get_name()][tech]
76 88 tech = sorcery.lathe.techs[tech]
77 89
78 90 -- fill in the preview slots
79 91 local j = 1
80 92 for i=1, inv:get_size 'preview' do
81 93 local stk = ItemStack()
82 - 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
83 - local l = sorcery.lathe.get(pos, i, wkpc:get_count())
84 - local max = l.ntimes --math.floor(wkpc:get_count() / (rec[i].mass or 1))
94 + local os = rec[i] and ItemStack(rec[i].output)
95 + if rec[i] and minetest.registered_items[os:get_name()] and (rec[i].mass == nil or rec[i].mass <= wkpc:get_count()) then
96 + local l = sorcery.lathe.get(pos, i)
97 + local max = l.ntimes
98 + --math.floor(wkpc:get_count() / (rec[i].mass or 1))
85 99 if tech.dmg then
86 - -- TODO count remaining tool uses
100 + local lw = l.wear
101 + while lw + tool:get_wear() > 65535 do
102 + max = max - 1
103 + if max == 0 then break end
104 + lw = sorcery.lathe.get(pos, i, max).wear
105 + end
87 106 elseif tech.consume then
88 107 max = math.min(max, tool:get_count())
89 108 end
90 109 if max > 0 then
91 110 stk = ItemStack(rec[i].output)
92 - stk:set_count(stk:get_count() * max)
111 + local ct = math.min(stk:get_count() * max, stk:get_stack_max())
112 + ct = ct - (ct % os:get_count())
113 + stk:set_count(ct)
93 114 end
94 115 end
95 116 inv:set_stack('preview',i,stk)
96 117 j = j + 1
97 118 end
98 119
99 120 -- make sure remaining slots are clear
................................................................................
130 151 local m = minetest.get_meta(pos)
131 152 local i = m:get_inventory()
132 153 i:set_size('workpiece', 1);
133 154 i:set_size('tool', 1);
134 155 i:set_size('preview', 8);
135 156 m:set_string('formspec', [[
136 157 formspec_version[3] size[10.25,8]
137 - list[context;tool;1.50,1;1,1]
138 - list[context;workpiece;3,1;1,1]
158 + list[context;tool;1.25,1;1,1]
159 + list[context;workpiece;2.75,1;1,1]
139 160 list[context;preview;5.25,0.25;4,2]
140 161 list[current_player;main;0.25,3;8,4]
141 162
142 163 listring[current_player;main] listring[context;workpiece]
143 164 listring[current_player;main] listring[context;tool]
144 165 listring[current_player;main] listring[context;preview]
145 166 listring[current_player;main]
................................................................................
178 199 on_metadata_inventory_put = sorcery.lathe.update;
179 200 on_metadata_inventory_take = function(pos, list, idx, stack, user)
180 201 if list == 'preview' then
181 202 local l = sorcery.lathe.get(pos,idx,stack:get_count())
182 203 if sorcery.lathe.techs[l.tech].consume then
183 204 l.tool:take_item(l.cost)
184 205 elseif sorcery.lathe.techs[l.tech].dmg then
185 - local mat = sorcery.itemclass.get(l.tool,'material')
186 - local mmat = sorcery.itemclass.get(l.wkpc,'metal')
187 - local dur = 100
188 - local lfac = 1
189 - if mat then
190 - local dur = mat.data.durability or dur
191 - lfac = (mmat and mmat.data.level or 1) /
192 - (mat.data.maxlevel or mat.data.level or 1)
193 - end
194 - local ch = 65535 / dur
195 - l.tool:add_wear(ch * l.cost * lfac)
206 + l.tool:add_wear(l.wear)
196 207 end
197 208 l.wkpc:take_item(l.tqty)
198 209 l.inv:set_stack('tool', 1, l.tool)
199 210 l.inv:set_stack('workpiece', 1, l.wkpc)
200 211 if l.rec.leftover then
201 212 sorcery.lib.node.insert(ItemStack(l.rec.leftover), 'workpiece', pos, user, l.inv)
202 213 end