MineClone2/mods/builtin_item/init.lua

201 lines
5.5 KiB
Lua
Raw Normal View History

2015-06-29 20:55:56 +03:00
minetest.register_entity(":__builtin:item", {
initial_properties = {
hp_max = 1,
physical = true,
2017-01-06 03:03:58 +02:00
collisionbox = {-0.175, -0.175, -0.175, 0.175, 0.175, 0.175},
collide_with_objects = false,
2015-06-29 20:55:56 +03:00
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,
},
2017-01-06 03:03:58 +02:00
itemstring = "",
2015-06-29 20:55:56 +03:00
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
2017-01-06 03:03:58 +02:00
prop.visual = "wielditem"
prop.textures = {itemname}
prop.visual_size = {x=0.25, y=0.25}
2015-06-29 20:55:56 +03:00
else
prop.visual = "wielditem"
prop.textures = {itemname}
2017-01-06 03:03:58 +02:00
prop.visual_size = {x=0.25, y=0.25}
prop.automatic_rotate = math.pi * 0.5
2015-06-29 20:55:56 +03:00
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
2017-01-06 03:03:58 +02:00
time = 600
2015-06-29 20:55:56 +03:00
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()
2017-01-06 03:03:58 +02:00
local name = minetest.get_node(p).name
2017-01-10 08:13:51 +02:00
if (minetest.registered_nodes[name] and minetest.registered_nodes[name].damage_per_second > 0) or name == "maptools:igniter" then
2017-01-06 03:03:58 +02:00
minetest.sound_play("builtin_item_lava", {pos = self.object:getpos(), gain = 0.5})
2015-06-29 20:55:56 +03:00
self.object:remove()
return
end
2017-01-06 03:03:58 +02:00
--[[ if name == "default:water_source" then
self.object:setacceleration({x = 0, y = 4, z = 0})
else
self.object:setacceleration({x = 0, y = -10, z = 0})
end
--]]
2015-06-29 20:55:56 +03:00
2017-01-10 08:13:51 +02:00
if minetest.registered_nodes[name] and minetest.registered_nodes[name].liquidtype == "flowing" then
2015-06-29 20:55:56 +03:00
get_flowing_dir = function(self)
local pos = self.object:getpos()
2017-01-06 03:03:58 +02:00
local param2 = minetest.get_node(pos).param2
2015-06-29 20:55:56 +03:00
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
2017-01-06 03:03:58 +02:00
local name = minetest.get_node(pos).name
local par2 = minetest.get_node(pos).param2
2015-06-29 20:55:56 +03:00
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
2017-01-06 03:03:58 +02:00
self.object:setacceleration({x = 0, y = 0, z = 0})
self.object:setvelocity({x = 1, y = -0.22, z = 0})
2015-06-29 20:55:56 +03:00
elseif vec and vec.x-p.x < 0 then
2017-01-06 03:03:58 +02:00
self.object:setacceleration({x = 0, y = 0, z = 0})
self.object:setvelocity({x = -1, y = -0.22, z = 0})
2015-06-29 20:55:56 +03:00
elseif vec and vec.z-p.z > 0 then
2017-01-06 03:03:58 +02:00
self.object:setacceleration({x = 0, y = 0, z = 0})
self.object:setvelocity({x = 0, y = -0.22, z = 1})
2015-06-29 20:55:56 +03:00
elseif vec and vec.z-p.z < 0 then
2017-01-06 03:03:58 +02:00
self.object:setacceleration({x = 0, y = 0, z = 0})
self.object:setvelocity({x = 0, y = -0.22, z = -1})
2015-06-29 20:55:56 +03:00
end
2017-01-06 03:03:58 +02:00
self.object:setacceleration({x = 0, y = -10, z = 0})
2015-06-29 20:55:56 +03:00
self.physical_state = true
self.object:set_properties({
physical = true
})
return
end
end
p.y = p.y - 0.3
2017-01-06 03:03:58 +02:00
local nn = minetest.get_node(p).name
-- If node is not registered or node is walkably solid.
2015-06-29 20:55:56 +03:00
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,
2017-01-06 03:03:58 +02:00
--[[ This causes a duplication glitch if a player walks upon an item and clicks on it at the same time.
2015-06-29 20:55:56 +03:00
on_punch = function(self, hitter)
2017-01-06 03:03:58 +02:00
if self.itemstring ~= "" then
2015-06-29 20:55:56 +03:00
local left = hitter:get_inventory():add_item("main", self.itemstring)
if not left:is_empty() then
self.itemstring = left:to_string()
return
end
end
self.object:remove()
end,
2017-01-06 03:03:58 +02:00
--]]
2015-06-29 20:55:56 +03:00
})
if minetest.setting_get("log_mods") then
2017-01-06 03:03:58 +02:00
minetest.log("action", "[builtin_item] loaded.")
2015-06-29 20:55:56 +03:00
end