From 2b0847eae62d0854cfdaeb7ecae96d9b5b9ed57b Mon Sep 17 00:00:00 2001 From: PilzAdam Date: Sun, 2 Sep 2012 15:57:42 +0200 Subject: [PATCH] Major code reorganisation --- init.lua | 176 ++++++++++++++++++++++--------------------------------- 1 file changed, 69 insertions(+), 107 deletions(-) diff --git a/init.lua b/init.lua index faf22b4..957c159 100644 --- a/init.lua +++ b/init.lua @@ -24,13 +24,14 @@ minetest.register_globalstep(function(dtime) if object:get_luaentity().collect then if inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then local pos1 = pos - pos1.y = pos1.y+0.3 + pos1.y = pos1.y+0.2 local pos2 = object:getpos() local vec = {x=pos1.x-pos2.x, y=pos1.y-pos2.y, z=pos1.z-pos2.z} - vec.x = vec.x*15 - vec.y = vec.y*15 - vec.z = vec.z*15 - object:setacceleration(vec) + vec.x = vec.x*3 + vec.y = vec.y*3 + vec.z = vec.z*3 + --object:setacceleration(vec) + object:setvelocity(vec) end else minetest.after(0.5, function(entity) @@ -42,116 +43,77 @@ minetest.register_globalstep(function(dtime) end end) -drops = {} -after_dig_nodes = {} -get_drops = function(tab) - if not tab.items then - return {tab} - end - local ret = {} - if tab.rarity and math.random(1, tab.rarity) ~= 1 then - return {} - end - local max = tab.max_items - if not max then - max = #tab+100 - end - for i,item in ipairs(tab.items) do - if #ret>=max then - break - end - if item.items == nil then - table.insert(ret, item) - else - for _,item2 in ipairs(get_drops(item)) do - table.insert(ret, item2) - end - end - end - return ret +function minetest.get_node_drops(nodename, toolname) + return {} end -minetest.after(0, function() - for name,node in pairs(minetest.registered_nodes) do - if node.drop ~= nil then - drops[name] = node.drop +function minetest.get_drops(nodename, toolname) + local drop = ItemStack({name=nodename}):get_definition().drop + if drop == nil then + -- default drop + return {ItemStack({name=nodename})} + elseif type(drop) == "string" then + -- itemstring drop + return {ItemStack(drop)} + elseif drop.items == nil then + -- drop = {} to disable default drop + return {} + end + + -- Extended drop table + local got_items = {} + local got_count = 0 + local _, item, tool + for _, item in ipairs(drop.items) do + local good_rarity = true + local good_tool = true + if item.rarity ~= nil then + good_rarity = item.rarity < 1 or math.random(item.rarity) == 1 end - if node.after_dig_node ~= nil then - after_dig_nodes[name] = node.after_dig_nodes - end - local new_node = {} - new_node.drop = "" - new_node.after_dig_node = function(pos, oldnode, oldmetadata, digger) - if after_dig_nodes[oldnode.name] ~= nil then - after_dig_nodes[oldnode.name](pos, oldnode, oldmetadata, digger) - end - local name = oldnode.name - if drops[name] == nil then - local obj = minetest.env:add_item(pos, oldnode) - if obj ~= nil then - obj:get_luaentity().collect = true - local x = math.random(1, 5) - if math.random(1,2) == 1 then - x = -x - end - local z = math.random(1, 5) - if math.random(1,2) == 1 then - z = -z - end - obj:setvelocity({x=1/x, y=obj:getvelocity().y, z=1/z}) - end - else - if drops[name].items == nil then - oldnode.name = drops[name] - local num = 1 - if string.find(oldnode.name, " ") then - num = tonumber(string.sub(oldnode.name, string.find(oldnode.name, " ")+1)) - oldnode.name = string.sub(oldnode.name, 1, string.find(oldnode.name, " ")-1) - end - for i=1,num do - local obj = minetest.env:add_item(pos, oldnode) - if obj ~= nil then - obj:get_luaentity().collect = true - local x = math.random(1, 5) - if math.random(1,2) == 1 then - x = -x - end - local z = math.random(1, 5) - if math.random(1,2) == 1 then - z = -z - end - obj:setvelocity({x=1/x, y=obj:getvelocity().y, z=1/z}) - end - end + if item.tools ~= nil then + good_tool = false + for _, tool in ipairs(item.tools) do + if tool:sub(1, 1) == '~' then + good_tool = toolname:find(tool:sub(2)) ~= nil else - for _,item in ipairs(get_drops(drops[oldnode.name])) do - oldnode.name = item - local obj = minetest.env:add_item(pos, oldnode) - if obj ~= nil then - obj:get_luaentity().collect = true - local x = math.random(1, 5) - if math.random(1,2) == 1 then - x = -x - end - local z = math.random(1, 5) - if math.random(1,2) == 1 then - z = -z - end - obj:setvelocity({x=1/x, y=obj:getvelocity().y, z=1/z}) - end - end + good_tool = toolname == tool + end + if good_tool then + break end end - end - - for name,value in pairs(node) do - if name ~= "drop" and name ~= "after_dig_node" then - new_node[name] = value + end + if good_rarity and good_tool then + got_count = got_count + 1 + for _, add_item in ipairs(item.items) do + got_items[#got_items+1] = add_item + end + if drop.max_items ~= nil and got_count == drop.max_items then + break + end + end + end + return got_items +end + +minetest.register_on_dignode(function(pos, oldnode, digger) + local drop = minetest.get_drops(oldnode.name, digger:get_wielded_item():get_name()) + for _,item in ipairs(drop) do + for i=1,item:get_count() do + local obj = minetest.env:add_item(pos, item:get_name()) + if obj ~= nil then + obj:get_luaentity().collect = true + local x = math.random(1, 5) + if math.random(1,2) == 1 then + x = -x + end + local z = math.random(1, 5) + if math.random(1,2) == 1 then + z = -z + end + obj:setvelocity({x=1/x, y=obj:getvelocity().y, z=1/z}) end end - - minetest.register_node(":"..new_node.name, new_node) - end end)