@@ -34,18 +34,30 @@ 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)) + 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 = math.floor(howmany / outn) * (rec.mass or 1), outn = outn; - gqty = ntimes * outn; + tqty = ntimes * (rec.mass or 1), outn = outn; tech = tech; rec = rec; inv = inv; } @@ -78,19 +90,28 @@ -- 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)) + 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 - -- TODO count remaining tool uses + 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) - stk:set_count(stk:get_count() * max) + 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 @@ -133,10 +154,10 @@ 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;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] @@ -181,19 +202,9 @@ 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) + 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)