@@ -32,15 +32,18 @@ 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 q = howmany / outn + local ntimes = math.floor(howmany / (rec.mass or 1)) return { tool = tool, wkpc = wkpc; - cost = rec.cost * q; - qty = q, outn = outn; + cost = rec.cost * ntimes; + ntimes = ntimes; + tqty = math.floor(howmany / outn), outn = outn; + gqty = ntimes * outn; tech = tech; rec = rec; inv = inv; } @@ -64,19 +67,20 @@ -- obey level restrictions. TODO honor Rend if (wmat and wmat.data.level or 0) > (tmat and (tmat.data.maxlevel or tmat.data.level) or 0) then return end - + local tech = sorcery.lathe.tooltech(tool) 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 - if rec[i] and minetest.registered_items[ItemStack(rec[i].output):get_name()] then - local max = wkpc:get_count() + 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()) @@ -83,11 +87,11 @@ end if max > 0 then stk = ItemStack(rec[i].output) stk:set_count(stk:get_count() * max) - inv:set_stack('preview',i,stk) end end + inv:set_stack('preview',i,stk) j = j + 1 end -- make sure remaining slots are clear @@ -185,13 +189,16 @@ 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(4 * ch * l.cost * lfac) + l.tool:add_wear(ch * l.cost * lfac) end - l.wkpc:take_item(l.qty) + 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 minetest.sound_play('sorcery_clank', { pos = pos, gain = 0.9 }) end sorcery.lathe.update(pos) end;