Merge pull request #11 from minetest-mods/rewrite

Rewrite
This commit is contained in:
texmex 2018-06-17 07:41:43 +02:00 committed by GitHub
commit b576e882a7
4 changed files with 294 additions and 161 deletions

3
CONTRIBUTING.md Normal file
View File

@ -0,0 +1,3 @@
- The `master` branch should be stable to end-users at all times. It should target latest Minetest point release.
- Put rewrites and new features in branches.
- Conform with setting defaults so that end-user upgrades doesn't change expected in-game behavior. Discuss default changes in an issue if one really need to change.

View File

@ -1,5 +1,6 @@
# item_drop # item_drop
By [PilzAdam](https://github.com/PilzAdam), [texmex](https://github.com/tacotexmex/). By [PilzAdam](https://github.com/PilzAdam),
[texmex](https://github.com/tacotexmex/), [hybriddog](https://github.com/hybriddog/).
## Description ## Description
This mod adds Minecraft like drop/pick up of items to Minetest. This mod adds Minecraft like drop/pick up of items to Minetest.
@ -8,26 +9,46 @@ This mod adds Minecraft like drop/pick up of items to Minetest.
LGPLv2.1/CC BY-SA 3.0. LGPLv2.1/CC BY-SA 3.0.
## Notes ## Notes
item_drop can be played with Minetest 0.4.16 or above. It was originally developed by [PilzAdam](https://github.com/PilzAdam/item_drop). item_drop can be played with Minetest 0.4.16 or above. It was originally
developed by [PilzAdam](https://github.com/PilzAdam/item_drop).
## List of features ## List of features
- All settings may be configured from within the game itself (Settings tab > Advanced settings > Mods > item_drop) * All settings may be configured from within the game itself.
- Drops nodes as in-world items on dig if `enable_item_drop` is `true`. (true by default) (Settings tab > Advanced settings > Mods > item_drop)
- Pulls items to the player's inventory if `enable_item_pickup` is `true`. (true by default) It uses a node radius set in `pickup_radius` (default 0.75) * Drops nodes as in-world items on dig if `item_drop.enable_item_drop` is
- Plays a sound the items are picked up, with the gain level set it `pickup_gain` (default 0.4) `true` (true by default) It does nothing in creative mode.
- Requires a key to be pressed in order to pull items if `enable_item_pickup_key` is `true`. (true by default). The keytypes to choose from by setting `item_pickup_keytype` are: * Puts dropped items to the player's inventory if `item_drop.enable_item_pickup`
- Use key (`Use`) is `true` (true by default)
- Sneak key (`Sneak`) * It uses a node radius set in `item_drop.pickup_radius` (default 0.75),
- Left and Right keys combined (`LeftAndRight`) if items are within this radius around the player's belt, they're picked.
- Right mouse button (`RMB`) * If `item_drop.pickup_age` is something positive, items dropped by players
- Sneak key and right mouse button combined (`SneakAndRMB`) are ignored for this time to avoid instantly picking up when dropping.
* If `item_drop.pickup_age` is `-1`, items are only picked when they don't
move, it's another fix for instant item picking.
* If `item_drop.magnet_radius` is bigger than `item_drop.pickup_radius`,
items between these radii are flying to the player for
`item_drop.magnet_time` seconds, after this time, they're picked or stop
flying.
* Plays a sound when the items are picked up with the gain level set to
`item_drop.pickup_sound_gain` (default 0.2)
* Requires a key to be pressed in order to pick items if
`item_drop.enable_pickup_key` is `true` (true by default)
* The keytypes to choose from by setting `item_pickup_keytype` are:
* Use key (`Use`)
* Sneak key (`Sneak`)
* Left and Right keys combined (`LeftAndRight`)
* Right mouse button (`RMB`)
* Sneak key and right mouse button combined (`SneakAndRMB`)
* If `item_drop.pickup_keyinvert` is `true` (false by default), items are
collected when the key is not pressed instead of when it's pressed.
## Known issues ## Known issues
## Bug reports and suggestions ## Bug reports and suggestions
You can report bugs or suggest ideas by [filing an issue](http://github.com/tacotexmex/item_drop/issues/new). You can report bugs or suggest ideas by
[filing an issue](http://github.com/minetest-mods/item_drop/issues/new).
## Links ## Links
* [Download ZIP](https://github.com/tacotexmex/item_drop/archive/master.zip) * [Download ZIP](https://github.com/minetest-mods/item_drop/archive/master.zip)
* [Source](https://github.com/tacotexmex/item_drop/) * [Source](https://github.com/minetest-mods/item_drop/)
* [Forum thread](https://forum.minetest.net/viewtopic.php?t=16913) * [Forum thread](https://forum.minetest.net/viewtopic.php?t=16913)

328
init.lua
View File

@ -1,126 +1,212 @@
local pickup = minetest.settings:get_bool("enable_item_pickup") ~= false local load_time_start = minetest.get_us_time()
local drop = minetest.settings:get_bool("enable_item_drop") ~= false
local key = minetest.settings:get_bool("enable_item_pickup_key") ~= false
local keytype = minetest.settings:get("item_pickup_keytype") or "Use"
local pickup_gain = tonumber(minetest.settings:get("item_pickup_gain")) or 0.4
local pickup_radius = tonumber(minetest.settings:get("item_pickup_radius")) or 0.75
local damage_enabled = minetest.settings:get_bool("enable_damage")
local timer = 0 if minetest.settings:get_bool("item_drop.enable_item_pickup") ~= false then
local pickup_gain = tonumber(
if pickup then minetest.settings:get("item_drop.pickup_sound_gain")) or 0.2
minetest.register_globalstep(function(dtime) local pickup_radius = tonumber(
minetest.settings:get("item_drop.pickup_radius")) or 0.75
timer = timer + dtime local magnet_radius = tonumber(
if timer < 0.2 then return end minetest.settings:get("item_drop.magnet_radius")) or -1
timer = 0 local magnet_time = tonumber(
minetest.settings:get("item_drop.magnet_time")) or 5.0
for _,player in ipairs(minetest.get_connected_players()) do local pickup_age = tonumber(
local keys_pressed = false minetest.settings:get("item_drop.pickup_age")) or 0.5
local key_triggered = minetest.settings:get_bool(
local control = player:get_player_control() "item_drop.enable_pickup_key") ~= false
local key_invert = minetest.settings:get_bool(
if keytype == "Use" then "item_drop.pickup_keyinvert") or false
keys_pressed = control.aux1 local keytype
elseif keytype == "Sneak" then if key_triggered then
keys_pressed = control.sneak keytype = minetest.settings:get("item_drop.pickup_keytype") or "Use"
elseif keytype == "LeftAndRight" then -- LeftAndRight combination -- disable pickup age if picking is explicitly enabled by the player
keys_pressed = control.left and control.right if not key_invert then
elseif keytype == "RMB" then pickup_age = math.min(pickup_age, 0)
keys_pressed = control.RMB end
elseif keytype == "SneakAndRMB" then -- SneakAndRMB combination
keys_pressed = control.sneak and control.RMB
end end
if not keys_pressed and key local magnet_mode = magnet_radius > pickup_radius
or (damage_enabled and player:get_hp() <= 0) then local zero_velocity_mode = pickup_age == -1
if magnet_mode
and zero_velocity_mode then
error"zero velocity mode can't be used together with magnet mode"
end
-- adds the item to the inventory and removes the object
local function collect_item(ent, pos, player)
minetest.sound_play("item_drop_pickup", {
pos = pos,
gain = pickup_gain,
})
ent:on_punch(player)
end
-- opt_get_ent gets the object's luaentity if it can be collected
local opt_get_ent
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
end
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
end
end
local afterflight
if magnet_mode then
-- take item or reset velocity after flying a second
function afterflight(object, inv, player)
-- TODO: test what happens if player left the game
local ent = opt_get_ent(object)
if not ent then
return
end
local item = ItemStack(ent.itemstring)
if inv
and inv:room_for_item("main", item) then
collect_item(ent, object:get_pos(), player)
else
object:setvelocity({x=0,y=0,z=0})
ent.physical_state = true
ent.object:set_properties({
physical = true
})
end
end
end
-- 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
-- tests if the player has the keys pressed to enable item picking
local function has_keys_pressed(player)
if not key_triggered then
return true
end
local control = player:get_player_control()
local keys_pressed
if keytype == 0 then -- LeftAndRight combination
keys_pressed = control.left and control.right
elseif keytype == 1 then -- SneakAndRMB combination
keys_pressed = control.sneak and control.RMB
else
keys_pressed = control[keytype]
end
return keys_pressed ~= key_invert
end
-- called for each player to possibly collect an item, returns true if so
local function pickupfunc(player)
if not has_keys_pressed(player)
or not minetest.get_player_privs(player:get_player_name()).interact
or player:get_hp() <= 0 then
return return
end end
local pos = player:getpos() local pos = player:getpos()
pos.y = pos.y+0.5 pos.y = pos.y+0.5
local inv = player:get_inventory() local inv
local objectlist = minetest.get_objects_inside_radius(pos, pickup_radius) local objectlist = minetest.get_objects_inside_radius(pos,
for _,object in ipairs(objectlist) do magnet_mode and magnet_radius or pickup_radius)
if not object:is_player() for i = 1,#objectlist do
and object:get_luaentity() local object = objectlist[i]
and object:get_luaentity().name == "__builtin:item" then local ent = opt_get_ent(object)
local pos2 = object:getpos() if ent then
local distance = math.sqrt(((pos2.x - pos.x) * (pos2.x - pos.x)) + ((pos2.y - pos.y) * (pos2.y - pos.y)) if not inv then
+ ((pos2.z - pos.z) * (pos2.z - pos.z))) inv = player:get_inventory()
if distance <= 1 then if not inv then
if inv and inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then minetest.log("error", "[item_drop] Couldn't " ..
inv:add_item("main", ItemStack(object:get_luaentity().itemstring)) "get inventory")
if object:get_luaentity().itemstring ~= "" then return
minetest.sound_play("item_drop_pickup", {
to_player = player:get_player_name(),
gain = pickup_gain,
})
end end
object:get_luaentity().itemstring = ""
object:remove()
end end
else local item = ItemStack(ent.itemstring)
if object:get_luaentity().collect then if inv:room_for_item("main", item) then
if inv and inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then local flying_item
local pos1 = pos local pos2
pos1.y = pos1.y+0.2 if magnet_mode then
local vec = {x=pos1.x-pos2.x, y=pos1.y-pos2.y, z=pos1.z-pos2.z} pos2 = object:getpos()
vec.x = vec.x*3 flying_item = vector.distance(pos, pos2) > pickup_radius
vec.y = vec.y*3 end
vec.z = vec.z*3 if not flying_item then
object:setvelocity(vec) -- collect one item at a time to avoid the loud pop
object:get_luaentity().physical_state = false collect_item(ent, pos, player)
object:get_luaentity().object:set_properties({ return true
end
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 physical = false
}) })
minetest.after(1, function() minetest.after(magnet_time, afterflight,
local lua = object:get_luaentity() object, inv, player)
if not lua or not lua.itemstring then
return
end 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(),
gain = pickup_gain,
})
end end
object:get_luaentity().itemstring = "" end
object:remove() end
end
local function pickup_step()
local got_item
local players = minetest.get_connected_players()
for i = 1,#players do
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 else
object:setvelocity({x=0,y=0,z=0}) time = 0.2
object:get_luaentity().physical_state = true
object:get_luaentity().object:set_properties({
physical = true
})
end end
end, {player, object}) minetest.after(time, pickup_step)
end end
end minetest.after(3.0, pickup_step)
end
end
end
end
end)
end end
if drop then if minetest.settings:get_bool("item_drop.enable_item_drop") ~= false
function minetest.handle_node_drops(pos, drops, digger) and not minetest.settings:get_bool("creative_mode") then
function minetest.handle_node_drops(pos, drops)
local inv for i = 1,#drops do
local diggerPos = pos local item = drops[i]
if minetest.settings:get_bool("creative_mode") and digger and digger:is_player() then
inv = digger:get_inventory()
diggerPos = digger:getpos()
end
for _,item in ipairs(drops) do
local count, name local count, name
if type(item) == "string" then if type(item) == "string" then
count = 1 count = 1
@ -130,26 +216,34 @@ if drop then
name = item:get_name() name = item:get_name()
end end
if not inv or not inv:contains_item("main", ItemStack(name)) then for _ = 1,count do
for i=1,count do local obj = minetest.add_item(pos, name)
if not obj then
local adjustedPos = {x=diggerPos.x, y=diggerPos.y, z=diggerPos.z} error("Couldn't spawn item")
local obj = minetest.add_item(adjustedPos, 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})
end end
local vel = obj:getvelocity()
local x = math.random(-5, 4)
if x >= 0 then
x = x+1
end end
vel.x = 1 / x
local z = math.random(-5, 4)
if z >= 0 then
z = z+1
end
vel.z = 1 / z
obj:setvelocity(vel)
end end
end end
end end
end end
if minetest.settings:get("log_mods") then minetest.log("action", "[Mod] item_drop loaded") end
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)
end

View File

@ -1,17 +1,32 @@
#Pick up items automatically #Pick up items automatically
enable_item_pickup (Enable item pickups) bool true item_drop.enable_item_pickup (Enable item pickups) bool true
#Drop items in-world on dig #Drop items in-world on dig, does nothing in creative mode
enable_item_drop (Enable item drops) bool true item_drop.enable_item_drop (Enable item drops) bool true
#Use a key to pick up items #Use a key to pick up items
enable_item_pickup_key (Use pickup key) bool true item_drop.enable_pickup_key (Use pickup key) bool true
#Collect items when the key is not pressed instead of when it is pressed
item_drop.pickup_keyinvert (Invert pickup key) bool false
#What keytype to use as pickup key #What keytype to use as pickup key
item_pickup_keytype (Pickup keytype) enum Use Use,Sneak,LeftAndRight,RMB,SneakAndRMB item_drop.pickup_keytype (Pickup keytype) enum Use Use,Sneak,LeftAndRight,RMB,SneakAndRMB
#The volume of the pickup sound #The volume of the pickup sound
item_pickup_gain (Pickup sound gain) float 0.4 item_drop.pickup_sound_gain (Pickup sound gain) float 0.4
#Player pickup radius #Player pickup radius, the maximum distance from which items can be collected
item_pickup_radius (Pickup radius) float 0.75 item_drop.pickup_radius (Pickup radius) float 0.75
#Magnet radius, items between pickup_radius and this begin flying to the player
#Set it to -1 (or something else smaller than pickup_radius) to disable item
#flying
item_drop.magnet_radius (Magnet radius) float -1
#Item flight duration, items flying for more than this time are added to the
#player's inventory
item_drop.magnet_time (Magnet time) float 5.0
#Time delay in seconds after autopicking an item if it's dropped by a player
item_drop.pickup_age (Pickup age) float 0.5