1
0
mirror of https://github.com/HybridDog/builtin_item.git synced 2024-11-14 14:30:37 +01:00

dont override the whole item

This commit is contained in:
HybridDog 2014-08-31 14:12:32 +02:00
parent 64626b15c8
commit 64e2561c60
2 changed files with 187 additions and 180 deletions

270
init.lua
View File

@ -1,185 +1,95 @@
minetest.register_entity(":__builtin:item", {
initial_properties = {
hp_max = 1,
physical = true,
collisionbox = {-0.17,-0.17,-0.17, 0.17,0.17,0.17},
visual = "sprite",
visual_size = {x=0.5, y=0.5},
textures = {""},
spritediv = {x=1, y=1},
initial_sprite_basepos = {x=0, y=0},
is_visible = false,
timer = 0,
},
local function get_nodes(pos)
local tab,n = {},1
for i = -1,1,2 do
for _,p in pairs({
{x=pos.x+i, y=pos.y, z=pos.z},
{x=pos.x, y=pos.y, z=pos.z+i}
}) do
tab[n] = {p, minetest.get_node(p)}
n = n+1
end
end
return tab
end
local function get_flowing_dir(pos)
local data = get_nodes(pos)
local param2 = minetest.get_node(pos).param2
if param2 > 7 then
return
end
for _,i in pairs(data) do
local nd = i[2]
local name = nd.name
local par2 = nd.param2
if name == "default:water_flowing"
and par2 < param2 then
return i[1]
end
end
for _,i in pairs(data) do
local nd = i[2]
local name = nd.name
local par2 = nd.param2
if name == "default:water_flowing"
and par2 >= 11 then
return i[1]
end
end
for _,i in pairs(data) do
local nd = i[2]
local name = nd.name
local par2 = nd.param2
local tmp = minetest.registered_nodes[name]
if tmp
and not tmp.walkable
and name ~= "default:water_flowing" then
return i[1]
end
end
end
local item_entity = minetest.registered_entities["__builtin:item"]
local old_on_step = item_entity.on_step or function()end
item_entity.on_step = function(self, dtime)
old_on_step(self, dtime)
local p = self.object:getpos()
itemstring = '',
physical_state = true,
set_item = function(self, itemstring)
self.itemstring = itemstring
local stack = ItemStack(itemstring)
local itemtable = stack:to_table()
local itemname = nil
if itemtable then
itemname = stack:to_table().name
end
local item_texture = nil
local item_type = ""
if minetest.registered_items[itemname] then
item_texture = minetest.registered_items[itemname].inventory_image
item_type = minetest.registered_items[itemname].type
end
prop = {
is_visible = true,
visual = "sprite",
textures = {"unknown_item.png"}
}
if item_texture and item_texture ~= "" then
prop.visual = "sprite"
prop.textures = {item_texture}
prop.visual_size = {x=0.50, y=0.50}
else
prop.visual = "wielditem"
prop.textures = {itemname}
prop.visual_size = {x=0.20, y=0.20}
prop.automatic_rotate = math.pi * 0.25
end
self.object:set_properties(prop)
end,
get_staticdata = function(self)
--return self.itemstring
return minetest.serialize({
itemstring = self.itemstring,
always_collect = self.always_collect,
timer = self.timer,
})
end,
on_activate = function(self, staticdata, dtime_s)
if string.sub(staticdata, 1, string.len("return")) == "return" then
local data = minetest.deserialize(staticdata)
if data and type(data) == "table" then
self.itemstring = data.itemstring
self.always_collect = data.always_collect
self.timer = data.timer
if not self.timer then
self.timer = 0
end
self.timer = self.timer+dtime_s
end
else
self.itemstring = staticdata
end
self.object:set_armor_groups({immortal=1})
self.object:setvelocity({x=0, y=2, z=0})
self.object:setacceleration({x=0, y=-10, z=0})
self:set_item(self.itemstring)
end,
on_step = function(self, dtime)
local time = tonumber(minetest.setting_get("remove_items"))
if not time then
time = 300
end
if not self.timer then
self.timer = 0
end
self.timer = self.timer + dtime
if time ~= 0 and (self.timer > time) then
self.object:remove()
end
local p = self.object:getpos()
local name = minetest.env:get_node(p).name
if name == "default:lava_flowing" or name == "default:lava_source" then
minetest.sound_play("builtin_item_lava", {pos=self.object:getpos()})
self.object:remove()
return
end
if minetest.registered_nodes[name].liquidtype == "flowing" then
get_flowing_dir = function(self)
local pos = self.object:getpos()
local param2 = minetest.env:get_node(pos).param2
for i,d in ipairs({-1, 1, -1, 1}) do
if i<3 then
pos.x = pos.x+d
else
pos.z = pos.z+d
end
local name = minetest.env:get_node(pos).name
local par2 = minetest.env:get_node(pos).param2
if name == "default:water_flowing" and par2 < param2 then
return pos
end
if i<3 then
pos.x = pos.x-d
else
pos.z = pos.z-d
end
end
end
local vec = get_flowing_dir(self)
if vec then
local v = self.object:getvelocity()
if vec and vec.x-p.x > 0 then
self.object:setvelocity({x=0.5,y=v.y,z=0})
elseif vec and vec.x-p.x < 0 then
self.object:setvelocity({x=-0.5,y=v.y,z=0})
elseif vec and vec.z-p.z > 0 then
self.object:setvelocity({x=0,y=v.y,z=0.5})
elseif vec and vec.z-p.z < 0 then
self.object:setvelocity({x=0,y=v.y,z=-0.5})
end
self.object:setacceleration({x=0, y=-10, z=0})
self.physical_state = true
self.object:set_properties({
physical = true
})
return
end
end
p.y = p.y - 0.3
local nn = minetest.env:get_node(p).name
-- If node is not registered or node is walkably solid
if not minetest.registered_nodes[nn] or minetest.registered_nodes[nn].walkable then
if self.physical_state then
self.object:setvelocity({x=0,y=0,z=0})
self.object:setacceleration({x=0, y=0, z=0})
self.physical_state = false
self.object:set_properties({
physical = false
})
end
else
if not self.physical_state then
self.object:setvelocity({x=0,y=0,z=0})
self.object:setacceleration({x=0, y=-10, z=0})
self.physical_state = true
self.object:set_properties({
physical = true
})
end
end
end,
on_punch = function(self, hitter)
if self.itemstring ~= '' then
local left = hitter:get_inventory():add_item("main", self.itemstring)
if not left:is_empty() then
self.itemstring = left:to_string()
return
end
end
local name = minetest.get_node(p).name
if name == "default:lava_flowing"
or name == "default:lava_source" then
minetest.sound_play("builtin_item_lava", {pos=p})
self.object:remove()
end,
})
return
end
local tmp = minetest.registered_nodes[name]
if tmp
and tmp.liquidtype == "flowing" then
local vec = get_flowing_dir(self.object:getpos())
if vec then
local v = self.object:getvelocity()
if vec.x-p.x > 0 then
self.object:setvelocity({x=0.5,y=v.y,z=0})
elseif vec.x-p.x < 0 then
self.object:setvelocity({x=-0.5,y=v.y,z=0})
elseif vec.z-p.z > 0 then
self.object:setvelocity({x=0,y=v.y,z=0.5})
elseif vec.z-p.z < 0 then
self.object:setvelocity({x=0,y=v.y,z=-0.5})
end
self.object:setacceleration({x=0, y=-10, z=0})
self.physical_state = true
self.object:set_properties({
physical = true
})
end
end
end
minetest.register_entity(":__builtin:item", item_entity)
if minetest.setting_get("log_mods") then
minetest.log("action", "builtin_item loaded")

97
init.lua~ Normal file
View File

@ -0,0 +1,97 @@
local function get_nodes(pos)
local tab,n = {},1
for i = -1,1,2 do
for _,p in pairs({
{x=pos.x+i, y=pos.y, z=pos.z},
{x=pos.x, y=pos.y, z=pos.z+i}
}) do
tab[n] = {p, minetest.get_node(p)}
n = n+1
end
end
return tab
end
local function get_flowing_dir(pos)
pos = vector.round(pos)
local data = get_nodes(pos)
local param2 = minetest.get_node(pos).param2
if param2 > 7 then
return
end
for _,i in pairs(data) do
local nd = i[2]
local name = nd.name
local par2 = nd.param2
if name == "default:water_flowing"
and par2 < param2 then
return i[1]
end
end
for _,i in pairs(data) do
local nd = i[2]
local name = nd.name
local par2 = nd.param2
if name == "default:water_flowing"
and par2 >= 11 then
return i[1]
end
end
for _,i in pairs(data) do
local nd = i[2]
local name = nd.name
local par2 = nd.param2
local tmp = minetest.registered_nodes[name]
if tmp
and not tmp.walkable
and name ~= "default:water_flowing" then
return i[1]
end
end
end
local item_entity = minetest.registered_entities["__builtin:item"]
local old_on_step = item_entity.on_step or function()end
item_entity.on_step = function(self, dtime)
old_on_step(self, dtime)
local p = self.object:getpos()
local name = minetest.get_node(p).name
if name == "default:lava_flowing"
or name == "default:lava_source" then
minetest.sound_play("builtin_item_lava", {pos=p})
self.object:remove()
return
end
local tmp = minetest.registered_nodes[name]
if tmp
and tmp.liquidtype == "flowing" then
local vec = get_flowing_dir(self.object:getpos())
if vec then
local v = self.object:getvelocity()
if vec.x-p.x > 0 then
self.object:setvelocity({x=0.5,y=v.y,z=0})
elseif vec.x-p.x < 0 then
self.object:setvelocity({x=-0.5,y=v.y,z=0})
elseif vec.z-p.z > 0 then
self.object:setvelocity({x=0,y=v.y,z=0.5})
elseif vec.z-p.z < 0 then
self.object:setvelocity({x=0,y=v.y,z=-0.5})
end
self.object:setacceleration({x=0, y=-10, z=0})
self.physical_state = true
self.object:set_properties({
physical = true
})
end
end
end
minetest.register_entity(":__builtin:item", item_entity)
if minetest.setting_get("log_mods") then
minetest.log("action", "builtin_item loaded")
end