Major code reorganisation

This commit is contained in:
PilzAdam 2012-09-02 15:57:42 +02:00
parent 065685eab1
commit 2b0847eae6

146
init.lua

@ -24,13 +24,14 @@ minetest.register_globalstep(function(dtime)
if object:get_luaentity().collect then if object:get_luaentity().collect then
if inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then if inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then
local pos1 = pos local pos1 = pos
pos1.y = pos1.y+0.3 pos1.y = pos1.y+0.2
local pos2 = object:getpos() local pos2 = object:getpos()
local vec = {x=pos1.x-pos2.x, y=pos1.y-pos2.y, z=pos1.z-pos2.z} local vec = {x=pos1.x-pos2.x, y=pos1.y-pos2.y, z=pos1.z-pos2.z}
vec.x = vec.x*15 vec.x = vec.x*3
vec.y = vec.y*15 vec.y = vec.y*3
vec.z = vec.z*15 vec.z = vec.z*3
object:setacceleration(vec) --object:setacceleration(vec)
object:setvelocity(vec)
end end
else else
minetest.after(0.5, function(entity) minetest.after(0.5, function(entity)
@ -42,75 +43,65 @@ minetest.register_globalstep(function(dtime)
end end
end) end)
drops = {}
after_dig_nodes = {}
get_drops = function(tab) function minetest.get_node_drops(nodename, toolname)
if not tab.items then
return {tab}
end
local ret = {}
if tab.rarity and math.random(1, tab.rarity) ~= 1 then
return {} return {}
end end
local max = tab.max_items
if not max then function minetest.get_drops(nodename, toolname)
max = #tab+100 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 end
for i,item in ipairs(tab.items) do
if #ret>=max then -- 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 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
good_tool = toolname == tool
end
if good_tool then
break break
end end
if item.items == nil then end
table.insert(ret, item) end
else if good_rarity and good_tool then
for _,item2 in ipairs(get_drops(item)) do got_count = got_count + 1
table.insert(ret, item2) 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 end
end end
return ret return got_items
end end
minetest.after(0, function() minetest.register_on_dignode(function(pos, oldnode, digger)
for name,node in pairs(minetest.registered_nodes) do local drop = minetest.get_drops(oldnode.name, digger:get_wielded_item():get_name())
if node.drop ~= nil then for _,item in ipairs(drop) do
drops[name] = node.drop for i=1,item:get_count() do
end local obj = minetest.env:add_item(pos, item:get_name())
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 if obj ~= nil then
obj:get_luaentity().collect = true obj:get_luaentity().collect = true
local x = math.random(1, 5) local x = math.random(1, 5)
@ -124,34 +115,5 @@ minetest.after(0, function()
obj:setvelocity({x=1/x, y=obj:getvelocity().y, z=1/z}) obj:setvelocity({x=1/x, y=obj:getvelocity().y, z=1/z})
end end
end end
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
end
end
end
for name,value in pairs(node) do
if name ~= "drop" and name ~= "after_dig_node" then
new_node[name] = value
end
end
minetest.register_node(":"..new_node.name, new_node)
end end
end) end)