2017-09-11 14:09:24 +02:00
|
|
|
local load_time_start = minetest.get_us_time()
|
2018-06-17 09:27:43 +02:00
|
|
|
pickup_radius = tonumber(minetest.settings:get("item_pickup_radius"))
|
2017-09-11 14:09:24 +02:00
|
|
|
|
2018-06-18 20:54:59 +02:00
|
|
|
if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false and
|
2018-06-17 09:27:43 +02:00
|
|
|
minetest.settings:get_bool("enable_item_pickup") ~= false then
|
2017-09-06 15:56:04 +02:00
|
|
|
local pickup_gain = tonumber(
|
2018-06-17 09:27:43 +02:00
|
|
|
minetest.settings:get("item_drop.pickup_sound_gain")) or
|
|
|
|
tonumber(minetest.settings:get("item_pickup_gain")) or 0.2
|
2018-06-18 10:18:06 +02:00
|
|
|
local pickup_particle =
|
|
|
|
minetest.settings:get_bool("item_drop.pickup_particle") ~= false
|
2017-09-06 15:56:04 +02:00
|
|
|
local pickup_radius = tonumber(
|
|
|
|
minetest.settings:get("item_drop.pickup_radius")) or 0.75
|
2017-09-08 20:25:16 +02:00
|
|
|
local magnet_radius = tonumber(
|
|
|
|
minetest.settings:get("item_drop.magnet_radius")) or -1
|
2017-09-08 20:42:28 +02:00
|
|
|
local magnet_time = tonumber(
|
|
|
|
minetest.settings:get("item_drop.magnet_time")) or 5.0
|
2017-09-07 20:39:49 +02:00
|
|
|
local pickup_age = tonumber(
|
|
|
|
minetest.settings:get("item_drop.pickup_age")) or 0.5
|
2017-09-06 15:56:04 +02:00
|
|
|
local key_triggered = minetest.settings:get_bool(
|
2018-06-17 09:27:43 +02:00
|
|
|
"item_drop.enable_pickup_key") or
|
|
|
|
minetest.settings:get_bool("enable_item_pickup_key") ~= false
|
2017-09-08 20:36:36 +02:00
|
|
|
local key_invert = minetest.settings:get_bool(
|
2017-09-12 09:55:16 +02:00
|
|
|
"item_drop.pickup_keyinvert") or false
|
2017-09-06 15:51:34 +02:00
|
|
|
local keytype
|
|
|
|
if key_triggered then
|
2018-06-17 09:27:43 +02:00
|
|
|
keytype = minetest.settings:get("item_drop.pickup_keytype") or
|
|
|
|
minetest.settings:get("item_pickup_keytype") or "Use"
|
2017-09-16 12:57:21 +02:00
|
|
|
-- disable pickup age if picking is explicitly enabled by the player
|
|
|
|
if not key_invert then
|
|
|
|
pickup_age = math.min(pickup_age, 0)
|
|
|
|
end
|
2017-09-06 15:51:34 +02:00
|
|
|
end
|
|
|
|
|
2017-09-08 20:25:16 +02:00
|
|
|
local magnet_mode = magnet_radius > pickup_radius
|
2017-09-07 21:13:10 +02:00
|
|
|
local zero_velocity_mode = pickup_age == -1
|
2017-09-08 20:25:16 +02:00
|
|
|
if magnet_mode
|
|
|
|
and zero_velocity_mode then
|
|
|
|
error"zero velocity mode can't be used together with magnet mode"
|
|
|
|
end
|
2017-09-07 21:13:10 +02:00
|
|
|
|
2017-09-07 21:19:33 +02:00
|
|
|
-- adds the item to the inventory and removes the object
|
2017-09-11 14:44:31 +02:00
|
|
|
local function collect_item(ent, pos, player)
|
2017-09-07 21:19:33 +02:00
|
|
|
minetest.sound_play("item_drop_pickup", {
|
|
|
|
pos = pos,
|
|
|
|
gain = pickup_gain,
|
|
|
|
})
|
2018-06-18 10:18:06 +02:00
|
|
|
if pickup_particle then
|
|
|
|
local item = minetest.registered_nodes[ent.itemstring:gsub("(.*)%s.*$","%1")]
|
|
|
|
local image = ""
|
|
|
|
if minetest.registered_items[item.name] and minetest.registered_items[item.name].tiles then
|
|
|
|
if minetest.registered_items[item.name].tiles[1] then
|
|
|
|
local dt = minetest.registered_items[item.name].drawtype
|
|
|
|
if dt == "normal" or dt == "allfaces" or dt == "allfaces_optional"
|
|
|
|
or dt == "glasslike" or dt =="glasslike_framed" or dt == "glasslike_framed_optional"
|
|
|
|
or dt == "liquid" or dt == "flowingliquid" then
|
|
|
|
local tiles = minetest.registered_items[item.name].tiles
|
|
|
|
|
|
|
|
local top = tiles[1]
|
|
|
|
if (type(top) == "table") then top = top.item end
|
|
|
|
local left = tiles[3]
|
|
|
|
if not left then left = top end
|
|
|
|
if (type(left) == "table") then left = left.item end
|
|
|
|
local right = tiles[5]
|
|
|
|
if not right then right = left end
|
|
|
|
if (type(right) == "table") then right = right.item end
|
|
|
|
|
|
|
|
image = "[inventorycube{"..top.."{"..left.."{"..right
|
|
|
|
else
|
|
|
|
image = minetest.registered_items[item.name].inventory_image
|
|
|
|
if not image then image = minetest.registered_items[item.name].tiles[1] end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if item then
|
|
|
|
local texture = item.tiles[1] or item.inventory_image or ""
|
|
|
|
if item.drawtype == "normal" then
|
|
|
|
texture = item.tiles
|
|
|
|
end
|
|
|
|
minetest.add_particle({
|
|
|
|
pos = {x = pos.x, y = pos.y + 1.5, z = pos.z},
|
|
|
|
velocity = {x = 0, y = 1, z = 0},
|
|
|
|
acceleration = {x = 0, y = -4, z = 0},
|
|
|
|
expirationtime = 0.2,
|
|
|
|
size = 3,--math.random() + 0.5,
|
|
|
|
vertical = false,
|
|
|
|
texture = image,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
2017-09-11 14:44:31 +02:00
|
|
|
ent:on_punch(player)
|
2017-09-07 21:19:33 +02:00
|
|
|
end
|
|
|
|
|
2017-09-07 21:13:10 +02:00
|
|
|
-- opt_get_ent gets the object's luaentity if it can be collected
|
2017-09-08 20:25:16 +02:00
|
|
|
local opt_get_ent
|
2017-09-07 21:13:10 +02:00
|
|
|
if zero_velocity_mode then
|
|
|
|
function opt_get_ent(object)
|
|
|
|
if object:is_player()
|
|
|
|
or not vector.equals(object:getvelocity(), {x=0, y=0, z=0}) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local ent = object:get_luaentity()
|
|
|
|
if not ent
|
|
|
|
or ent.name ~= "__builtin:item"
|
|
|
|
or ent.itemstring == "" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
return ent
|
2017-09-07 20:39:49 +02:00
|
|
|
end
|
2017-09-07 21:13:10 +02:00
|
|
|
else
|
|
|
|
function opt_get_ent(object)
|
|
|
|
if object:is_player() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local ent = object:get_luaentity()
|
|
|
|
if not ent
|
|
|
|
or ent.name ~= "__builtin:item"
|
|
|
|
or (ent.dropped_by and ent.age < pickup_age)
|
|
|
|
or ent.itemstring == "" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
return ent
|
2017-09-07 20:39:49 +02:00
|
|
|
end
|
2017-09-08 20:25:16 +02:00
|
|
|
end
|
2017-09-07 20:39:49 +02:00
|
|
|
|
2017-09-08 20:25:16 +02:00
|
|
|
local afterflight
|
|
|
|
if magnet_mode then
|
2017-09-07 21:13:10 +02:00
|
|
|
-- take item or reset velocity after flying a second
|
2017-09-11 14:44:31 +02:00
|
|
|
function afterflight(object, inv, player)
|
|
|
|
-- TODO: test what happens if player left the game
|
2017-09-07 21:13:10 +02:00
|
|
|
local ent = opt_get_ent(object)
|
|
|
|
if not ent then
|
|
|
|
return
|
|
|
|
end
|
2017-09-07 21:19:33 +02:00
|
|
|
local item = ItemStack(ent.itemstring)
|
2017-09-07 21:13:10 +02:00
|
|
|
if inv
|
2017-09-07 21:19:33 +02:00
|
|
|
and inv:room_for_item("main", item) then
|
2017-09-11 14:44:31 +02:00
|
|
|
collect_item(ent, object:get_pos(), player)
|
2017-09-07 21:13:10 +02:00
|
|
|
else
|
|
|
|
object:setvelocity({x=0,y=0,z=0})
|
|
|
|
ent.physical_state = true
|
|
|
|
ent.object:set_properties({
|
|
|
|
physical = true
|
|
|
|
})
|
|
|
|
end
|
2017-09-07 20:57:02 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-08 20:57:05 +02:00
|
|
|
-- set keytype to the key name if possible
|
|
|
|
if keytype == "Use" then
|
|
|
|
keytype = "aux1"
|
|
|
|
elseif keytype == "Sneak" then
|
|
|
|
keytype = "sneak"
|
|
|
|
elseif keytype == "LeftAndRight" then -- LeftAndRight combination
|
|
|
|
keytype = 0
|
|
|
|
elseif keytype == "SneakAndRMB" then -- SneakAndRMB combination
|
|
|
|
keytype = 1
|
|
|
|
end
|
2017-09-06 15:51:34 +02:00
|
|
|
|
|
|
|
|
2017-09-08 20:57:05 +02:00
|
|
|
-- tests if the player has the keys pressed to enable item picking
|
2017-09-16 13:18:20 +02:00
|
|
|
local function has_keys_pressed(player)
|
2017-09-08 20:57:05 +02:00
|
|
|
if not key_triggered then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
local control = player:get_player_control()
|
|
|
|
local keys_pressed
|
|
|
|
if keytype == 0 then -- LeftAndRight combination
|
2017-09-07 21:00:55 +02:00
|
|
|
keys_pressed = control.left and control.right
|
2017-09-15 18:40:11 +02:00
|
|
|
elseif keytype == 1 then -- SneakAndRMB combination
|
2017-09-07 21:00:55 +02:00
|
|
|
keys_pressed = control.sneak and control.RMB
|
2017-09-08 20:57:05 +02:00
|
|
|
else
|
|
|
|
keys_pressed = control[keytype]
|
2017-09-07 21:00:55 +02:00
|
|
|
end
|
2017-09-06 15:51:34 +02:00
|
|
|
|
2017-09-08 20:57:05 +02:00
|
|
|
return keys_pressed ~= key_invert
|
|
|
|
end
|
|
|
|
|
2017-09-12 10:13:49 +02:00
|
|
|
-- called for each player to possibly collect an item, returns true if so
|
2017-09-08 20:57:05 +02:00
|
|
|
local function pickupfunc(player)
|
2017-09-16 13:18:20 +02:00
|
|
|
if not has_keys_pressed(player)
|
2017-09-11 14:08:01 +02:00
|
|
|
or not minetest.get_player_privs(player:get_player_name()).interact
|
2017-09-08 20:46:36 +02:00
|
|
|
or player:get_hp() <= 0 then
|
2017-09-07 21:00:55 +02:00
|
|
|
return
|
|
|
|
end
|
2017-09-06 15:51:34 +02:00
|
|
|
|
2017-09-07 21:00:55 +02:00
|
|
|
local pos = player:getpos()
|
|
|
|
pos.y = pos.y+0.5
|
|
|
|
local inv
|
2017-09-06 15:51:34 +02:00
|
|
|
|
2017-09-07 21:00:55 +02:00
|
|
|
local objectlist = minetest.get_objects_inside_radius(pos,
|
2017-09-08 20:25:16 +02:00
|
|
|
magnet_mode and magnet_radius or pickup_radius)
|
2017-09-07 21:00:55 +02:00
|
|
|
for i = 1,#objectlist do
|
|
|
|
local object = objectlist[i]
|
|
|
|
local ent = opt_get_ent(object)
|
|
|
|
if ent then
|
|
|
|
if not inv then
|
|
|
|
inv = player:get_inventory()
|
2017-09-07 20:39:49 +02:00
|
|
|
if not inv then
|
2017-09-07 21:00:55 +02:00
|
|
|
minetest.log("error", "[item_drop] Couldn't " ..
|
|
|
|
"get inventory")
|
|
|
|
return
|
2017-09-07 20:39:49 +02:00
|
|
|
end
|
2017-09-07 21:00:55 +02:00
|
|
|
end
|
2017-09-07 21:19:33 +02:00
|
|
|
local item = ItemStack(ent.itemstring)
|
|
|
|
if inv:room_for_item("main", item) then
|
2017-09-12 10:13:49 +02:00
|
|
|
local flying_item
|
|
|
|
local pos2
|
|
|
|
if magnet_mode then
|
|
|
|
pos2 = object:getpos()
|
|
|
|
flying_item = vector.distance(pos, pos2) > pickup_radius
|
|
|
|
end
|
|
|
|
if not flying_item then
|
|
|
|
-- collect one item at a time to avoid the loud pop
|
2017-09-11 14:44:31 +02:00
|
|
|
collect_item(ent, pos, player)
|
2017-09-08 21:26:08 +02:00
|
|
|
return true
|
|
|
|
end
|
2017-09-12 10:13:49 +02:00
|
|
|
local vel = vector.multiply(
|
|
|
|
vector.subtract(pos, pos2), 3)
|
|
|
|
vel.y = vel.y + 0.6
|
|
|
|
object:setvelocity(vel)
|
|
|
|
if ent.physical_state then
|
|
|
|
ent.physical_state = false
|
|
|
|
ent.object:set_properties({
|
|
|
|
physical = false
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.after(magnet_time, afterflight,
|
|
|
|
object, inv, player)
|
2017-09-06 15:51:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function pickup_step()
|
2017-09-08 21:26:08 +02:00
|
|
|
local got_item
|
2017-09-07 21:00:55 +02:00
|
|
|
local players = minetest.get_connected_players()
|
|
|
|
for i = 1,#players do
|
2017-09-08 21:26:08 +02:00
|
|
|
got_item = got_item or pickupfunc(players[i])
|
|
|
|
end
|
|
|
|
-- lower step if takeable item(s) were found
|
|
|
|
local time
|
|
|
|
if got_item then
|
|
|
|
time = 0.02
|
|
|
|
else
|
|
|
|
time = 0.2
|
2017-09-07 21:00:55 +02:00
|
|
|
end
|
2017-09-08 21:26:08 +02:00
|
|
|
minetest.after(time, pickup_step)
|
2017-09-06 15:51:34 +02:00
|
|
|
end
|
|
|
|
minetest.after(3.0, pickup_step)
|
2017-06-26 15:57:23 +02:00
|
|
|
end
|
2012-09-01 03:49:44 +02:00
|
|
|
|
2018-06-18 20:54:59 +02:00
|
|
|
if minetest.settings:get_bool("item_drop.enable_item_drop") ~= false and
|
2018-06-17 09:27:43 +02:00
|
|
|
minetest.settings:get_bool("enable_item_drop") ~= false
|
2017-09-11 14:35:55 +02:00
|
|
|
and not minetest.settings:get_bool("creative_mode") then
|
|
|
|
function minetest.handle_node_drops(pos, drops)
|
2017-09-11 14:22:17 +02:00
|
|
|
for i = 1,#drops do
|
|
|
|
local item = drops[i]
|
2017-09-06 15:51:34 +02:00
|
|
|
local count, name
|
|
|
|
if type(item) == "string" then
|
|
|
|
count = 1
|
|
|
|
name = item
|
|
|
|
else
|
|
|
|
count = item:get_count()
|
|
|
|
name = item:get_name()
|
|
|
|
end
|
|
|
|
|
2017-09-11 14:35:55 +02:00
|
|
|
for _ = 1,count do
|
|
|
|
local obj = minetest.add_item(pos, name)
|
|
|
|
if not obj then
|
|
|
|
error("Couldn't spawn item")
|
|
|
|
end
|
2017-09-06 15:51:34 +02:00
|
|
|
|
2017-09-11 14:35:55 +02:00
|
|
|
local vel = obj:getvelocity()
|
|
|
|
local x = math.random(-5, 4)
|
|
|
|
if x >= 0 then
|
|
|
|
x = x+1
|
|
|
|
end
|
2017-09-15 10:27:28 +02:00
|
|
|
vel.x = 1 / x
|
2017-09-11 14:35:55 +02:00
|
|
|
local z = math.random(-5, 4)
|
|
|
|
if z >= 0 then
|
|
|
|
z = z+1
|
2017-09-06 15:51:34 +02:00
|
|
|
end
|
2017-09-15 10:27:28 +02:00
|
|
|
vel.z = 1 / z
|
2017-09-11 14:35:55 +02:00
|
|
|
obj:setvelocity(vel)
|
2017-09-06 15:51:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-09-03 18:52:59 +02:00
|
|
|
end
|
2012-10-07 19:44:16 +02:00
|
|
|
|
2017-09-11 14:09:24 +02:00
|
|
|
|
|
|
|
local time = (minetest.get_us_time() - load_time_start) / 1000000
|
|
|
|
local msg = "[item_drop] loaded after ca. " .. time .. " seconds."
|
|
|
|
if time > 0.01 then
|
|
|
|
print(msg)
|
|
|
|
else
|
|
|
|
minetest.log("info", msg)
|
2017-09-06 15:51:34 +02:00
|
|
|
end
|