diff options
Diffstat (limited to 'item_drop')
-rw-r--r-- | item_drop/depends.txt | 1 | ||||
-rw-r--r-- | item_drop/init.lua | 151 | ||||
-rw-r--r-- | item_drop/item_entity.lua | 92 | ||||
-rw-r--r-- | item_drop/item_entity_old.lua | 126 | ||||
-rw-r--r-- | item_drop/sounds/item_drop_pickup.1.ogg | bin | 9981 -> 7584 bytes | |||
-rw-r--r-- | item_drop/sounds/item_drop_pickup.2.ogg | bin | 10477 -> 7635 bytes | |||
-rw-r--r-- | item_drop/sounds/item_drop_pickup.3.ogg | bin | 9586 -> 7146 bytes | |||
-rw-r--r-- | item_drop/sounds/item_drop_pickup.4.ogg | bin | 9696 -> 7250 bytes |
8 files changed, 264 insertions, 106 deletions
diff --git a/item_drop/depends.txt b/item_drop/depends.txt new file mode 100644 index 0000000..b88d3ff --- /dev/null +++ b/item_drop/depends.txt @@ -0,0 +1 @@ +technic diff --git a/item_drop/init.lua b/item_drop/init.lua index 8cd3b14..54b617f 100644 --- a/item_drop/init.lua +++ b/item_drop/init.lua @@ -1,109 +1,74 @@ dofile(minetest.get_modpath("item_drop").."/item_entity.lua") time_pick = 3 -minetest.register_globalstep(function(dtime) - for _,player in ipairs(minetest.get_connected_players()) do - local pos = player:getpos() - pos.y = pos.y+0.5 - local inv = player:get_inventory() - for _,object in ipairs(minetest.env:get_objects_inside_radius(pos, 1)) do - if not object:is_player() and object:get_luaentity() and object:get_luaentity().name == "__builtin:item" then - if inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then - if object:get_luaentity().timer > time_pick then - inv:add_item("main", ItemStack(object:get_luaentity().itemstring)) - if object:get_luaentity().itemstring ~= "" then - minetest.sound_play("item_drop_pickup", { - to_player = player:get_player_name(), - }) - end - object:get_luaentity().itemstring = "" - object:remove() - end - end - end - end - - for _,object in ipairs(minetest.env:get_objects_inside_radius(pos, 3)) do - if not object:is_player() and object:get_luaentity() and object:get_luaentity().name == "__builtin:item" then - --print(dump(object:getpos().y-player:getpos().y)) - if object:getpos().y-player:getpos().y > 0 then - if object:get_luaentity().collect then - if inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then - if object:get_luaentity().timer > time_pick then - local pos1 = pos - 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*3 - vec.y = vec.y*3 - vec.z = vec.z*3 - object:setvelocity(vec) - - minetest.after(1, function(args) - local lua = object:get_luaentity() - if object == nil or lua == nil or lua.itemstring == nil then - return - end - if inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then - inv:add_item("main", ItemStack(object:get_luaentity().itemstring)) - if object:get_luaentity().itemstring ~= "" then - minetest.sound_play("item_drop_pickup", { - to_player = player:get_player_name(), - }) - end - object:get_luaentity().itemstring = "" - object:remove() - else - object:setvelocity({x=0,y=0,z=0}) - end - end, {player, object}) + +if technic.config:getBool("enable_item_pickup") then + minetest.register_globalstep(function(dtime) + for _,player in ipairs(minetest.get_connected_players()) do + if player and player:get_hp() > 0 then + local pos = player:getpos() + pos.y = pos.y+0.5 + local inv = player:get_inventory() + for _,object in ipairs(minetest.env:get_objects_inside_radius(pos, 2)) do + if not object:is_player() and object:get_luaentity() then + local obj=object:get_luaentity() + if obj.name == "__builtin:item" then + if inv:room_for_item("main", ItemStack(obj.itemstring)) then + if obj.timer > time_pick then + inv:add_item("main", ItemStack(obj.itemstring)) + if obj.itemstring ~= "" then + minetest.sound_play("item_drop_pickup",{pos = pos, gain = 1.0, max_hear_distance = 10}) + end + if object:get_luaentity() then + object:get_luaentity().itemstring = "" + object:remove() + end end - end - else - minetest.after(0.5, function(entity) - entity.collect = true - end, object:get_luaentity()) end end end + end end - end -end) + end) +end -function minetest.handle_node_drops(pos, drops, digger) - for _,item in ipairs(drops) do - local count, name - if type(item) == "string" then - count = 1 - name = item - else - count = item:get_count() - name = item:get_name() - end - for i=1,count do - local obj = minetest.env:add_item(pos, 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}) - obj:get_luaentity().timer = time_pick - -- FIXME this doesnt work for deactiveted objects - if minetest.setting_get("remove_items") and tonumber(minetest.setting_get("remove_items")) then - minetest.after(tonumber(minetest.setting_get("remove_items")), function(obj) - obj:remove() - end, obj) +if technic.config:getBool("enable_item_drop") then + function minetest.handle_node_drops(pos, drops, digger) + for _,item in ipairs(drops) do + local count, name + if type(item) == "string" then + count = 1 + name = item + else + count = item:get_count() + name = item:get_name() + end + for i=1,count do + local obj = minetest.env:add_item(pos, 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}) + obj:get_luaentity().timer = time_pick + -- FIXME this doesnt work for deactiveted objects + if minetest.setting_get("remove_items") and tonumber(minetest.setting_get("remove_items")) then + minetest.after(tonumber(minetest.setting_get("remove_items")), function(obj) + obj:remove() + end, obj) + end end end end end end + --[[ minetest.register_on_dieplayer(function(name, pos) local inv = name:get_inventory() @@ -137,4 +102,4 @@ minetest.register_on_dieplayer(function(name, pos) end end) ]]-- -print("DROPS LOADED!")
\ No newline at end of file +print("DROPS LOADED!") diff --git a/item_drop/item_entity.lua b/item_drop/item_entity.lua index 6e479a1..5da8285 100644 --- a/item_drop/item_entity.lua +++ b/item_drop/item_entity.lua @@ -24,7 +24,7 @@ minetest.register_entity(":__builtin:item", { itemstring = '', physical_state = true, timer = 0, - + set_item = function(self, itemstring) self.itemstring = itemstring local stack = ItemStack(itemstring) @@ -62,15 +62,21 @@ minetest.register_entity(":__builtin:item", { return minetest.serialize({ itemstring = self.itemstring, always_collect = self.always_collect, + timer = self.timer, }) end, - on_activate = function(self, staticdata) + 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 @@ -80,18 +86,79 @@ minetest.register_entity(":__builtin:item", { self.object:setacceleration({x=0, y=-10, z=0}) self:set_item(self.itemstring) end, - + on_step = function(self, dtime) + local time = 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 (self.timer > 300) then + 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(),gain = 1.0, max_hear_distance = 10}) + self.object:remove() + return + end + + if minetest.registered_nodes[name] and 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 and resting on nodebox - local v = self.object:getvelocity() - if not minetest.registered_nodes[nn] or minetest.registered_nodes[nn].walkable and v.y == 0 then + -- 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}) @@ -114,13 +181,12 @@ minetest.register_entity(":__builtin:item", { 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 + hitter:get_inventory():add_item("main", self.itemstring) end self.object:remove() end, }) -print("ITEM ENTITY LOADED") + +if minetest.setting_get("log_mods") then + minetest.log("action", "builtin_item loaded") +end diff --git a/item_drop/item_entity_old.lua b/item_drop/item_entity_old.lua new file mode 100644 index 0000000..6e479a1 --- /dev/null +++ b/item_drop/item_entity_old.lua @@ -0,0 +1,126 @@ +-- Minetest: builtin/item_entity.lua + +function minetest.spawn_item(pos, item) + -- Take item in any format + local stack = ItemStack(item) + local obj = minetest.env:add_entity(pos, "__builtin:item") + obj:get_luaentity():set_item(stack:to_string()) + return obj +end + +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, + }, + + itemstring = '', + physical_state = true, + timer = 0, + + 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, + }) + end, + + on_activate = function(self, staticdata) + 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 + 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) + self.timer = self.timer + dtime + if (self.timer > 300) then + self.object:remove() + end + local p = self.object:getpos() + p.y = p.y - 0.3 + local nn = minetest.env:get_node(p).name + -- If node is not registered or node is walkably solid and resting on nodebox + local v = self.object:getvelocity() + if not minetest.registered_nodes[nn] or minetest.registered_nodes[nn].walkable and v.y == 0 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 + self.object:remove() + end, +}) +print("ITEM ENTITY LOADED") diff --git a/item_drop/sounds/item_drop_pickup.1.ogg b/item_drop/sounds/item_drop_pickup.1.ogg Binary files differindex 2ae432d..f5ea6b9 100644 --- a/item_drop/sounds/item_drop_pickup.1.ogg +++ b/item_drop/sounds/item_drop_pickup.1.ogg diff --git a/item_drop/sounds/item_drop_pickup.2.ogg b/item_drop/sounds/item_drop_pickup.2.ogg Binary files differindex f58bf08..0ed4869 100644 --- a/item_drop/sounds/item_drop_pickup.2.ogg +++ b/item_drop/sounds/item_drop_pickup.2.ogg diff --git a/item_drop/sounds/item_drop_pickup.3.ogg b/item_drop/sounds/item_drop_pickup.3.ogg Binary files differindex cf57c94..3e2d1e0 100644 --- a/item_drop/sounds/item_drop_pickup.3.ogg +++ b/item_drop/sounds/item_drop_pickup.3.ogg diff --git a/item_drop/sounds/item_drop_pickup.4.ogg b/item_drop/sounds/item_drop_pickup.4.ogg Binary files differindex bfe99d9..13430e2 100644 --- a/item_drop/sounds/item_drop_pickup.4.ogg +++ b/item_drop/sounds/item_drop_pickup.4.ogg |