1 Commits

Author SHA1 Message Date
49fb82d4e9 Pitch one sound instead of using multiple ones 2019-07-09 01:16:48 +02:00
7 changed files with 38 additions and 102 deletions

View File

@ -1,11 +0,0 @@
on: [push, pull_request]
name: build
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: lint
uses: Roang-zero1/factorio-mod-luacheck@master
with:
luacheckrc_url: https://raw.githubusercontent.com/minetest-mods/item_drop/master/.luacheckrc

View File

@ -1,19 +0,0 @@
unused_args = false
allow_defined_top = true
max_line_length = 999
ignore = {
"name", "drops", "i",
}
globals = {
"minetest",
}
read_globals = {
string = {fields = {"split", "trim"}},
table = {fields = {"copy", "getn"}},
"vector", "ItemStack",
"dump",
}

View File

@ -1,14 +1,15 @@
# Item Drop [![](https://github.com/minetest-mods/item_drop/workflows/build/badge.svg)](https://github.com/minetest-mods/item_drop/actions) [![License](https://img.shields.io/badge/license-LGPLv2.1%2B-blue.svg)](https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html) # item_drop
A highly configurable mod providing item magnet and in-world node drops\
By [PilzAdam](https://github.com/PilzAdam), By [PilzAdam](https://github.com/PilzAdam),
[texmex](https://github.com/tacotexmex/), [hybriddog](https://github.com/hybriddog/). [texmex](https://github.com/tacotexmex/), [hybriddog](https://github.com/hybriddog/).
## Description
A highly configurable mod providing item magnet and in-world node drops
## Licensing ## Licensing
LGPLv2.1/CC BY-SA 3.0. Particle code from WCILA mod by Aurailus, originally licensed MIT. LGPLv2.1/CC BY-SA 3.0. Particle code from WCILA mod by Aurailus, originally licensed MIT.
## Notes ## Notes
`item_drop` can be played with Minetest 0.4.16 or above. It was originally item_drop can be played with Minetest 0.4.16 or above. It was originally
developed by [PilzAdam](https://github.com/PilzAdam/item_drop). developed by [PilzAdam](https://github.com/PilzAdam/item_drop).
## List of features ## List of features

101
init.lua
View File

@ -1,4 +1,5 @@
local load_time_start = minetest.get_us_time() local load_time_start = minetest.get_us_time()
math.randomseed(os.time())
-- Functions which can be overridden by mods -- Functions which can be overridden by mods
item_drop = { item_drop = {
@ -93,46 +94,35 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
flowingliquid = true, flowingliquid = true,
} }
-- Get an image string from a tile definition
local function tile_to_image(tile, fallback_image)
if not tile then
return fallback_image
end
local tile_type = type(tile)
if tile_type == "string" then
return tile
end
assert(tile_type == "table", "Tile definition is not a string or table")
local image = tile.name or tile.image
assert(image, "Tile definition has no image file specified")
if tile.color then
local colorstr = minetest.colorspec_to_colorstring(tile.color)
if colorstr then
return image .. "^[multiply:" .. colorstr
end
end
return image
end
-- adds the item to the inventory and removes the object -- adds the item to the inventory and removes the object
local function collect_item(ent, pos, player) local function collect_item(ent, pos, player)
item_drop.before_collect(ent, pos, player) item_drop.before_collect(ent, pos, player)
minetest.sound_play("item_drop_pickup", { minetest.sound_play("item_drop_pickup", {
pos = pos, pos = pos,
gain = pickup_gain, gain = pickup_gain,
}, true) pitch = (100 - (math.random(-2, 2)) * 10) / 100,
})
if pickup_particle then if pickup_particle then
local item = minetest.registered_nodes[ local item = minetest.registered_nodes[
ent.itemstring:gsub("(.*)%s.*$", "%1")] ent.itemstring:gsub("(.*)%s.*$", "%1")]
local image local image = ""
if item and item.tiles and item.tiles[1] then if item and item.tiles and item.tiles[1] then
if inventorycube_drawtypes[item.drawtype] then if inventorycube_drawtypes[item.drawtype] then
local tiles = item.tiles local tiles = item.tiles
-- color in the tile definition is handled by tile_to_image.
-- color in the node definition is not yet supported here. local top = tiles[1]
local top = tile_to_image(tiles[1]) if type(top) == "table" then
local left = tile_to_image(tiles[3], top) top = top.name
local right = tile_to_image(tiles[5], left) end
local left = tiles[3] or top
if type(left) == "table" then
left = left.name
end
local right = tiles[5] or left
if type(right) == "table" then
right = right.name
end
image = minetest.inventorycube(top, left, right) image = minetest.inventorycube(top, left, right)
else else
image = item.inventory_image or item.tiles[1] image = item.inventory_image or item.tiles[1]
@ -213,9 +203,9 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
local itemdef = minetest.registered_entities["__builtin:item"] local itemdef = minetest.registered_entities["__builtin:item"]
local old_on_step = itemdef.on_step local old_on_step = itemdef.on_step
local function do_nothing() end local function do_nothing() end
function itemdef.on_step(self, ...) function itemdef.on_step(self, dtime)
if not self.is_magnet_item then if not self.is_magnet_item then
return old_on_step(self, ...) return old_on_step(self, dtime)
end end
ObjectRef = ObjectRef or getmetatable(self.object) ObjectRef = ObjectRef or getmetatable(self.object)
local old_funcs = {} local old_funcs = {}
@ -224,7 +214,7 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
old_funcs[method] = ObjectRef[method] old_funcs[method] = ObjectRef[method]
ObjectRef[method] = do_nothing ObjectRef[method] = do_nothing
end end
old_on_step(self, ...) old_on_step(self, dtime)
for i = 1, #blocked_methods do for i = 1, #blocked_methods do
local method = blocked_methods[i] local method = blocked_methods[i]
ObjectRef[method] = old_funcs[method] ObjectRef[method] = old_funcs[method]
@ -264,13 +254,6 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
return keys_pressed ~= key_invert return keys_pressed ~= key_invert
end end
local function is_inside_map(pos)
local bound = 31000
return -bound < pos.x and pos.x < bound
and -bound < pos.y and pos.y < bound
and -bound < pos.z and pos.z < bound
end
-- called for each player to possibly collect an item, returns true if so -- called for each player to possibly collect an item, returns true if so
local function pickupfunc(player) local function pickupfunc(player)
if not has_keys_pressed(player) if not has_keys_pressed(player)
@ -280,12 +263,8 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
end end
local pos = player:get_pos() local pos = player:get_pos()
if not is_inside_map(pos) then
-- get_objects_inside_radius crashes for too far positions
return
end
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, local objectlist = minetest.get_objects_inside_radius(pos,
magnet_mode and magnet_radius or pickup_radius) magnet_mode and magnet_radius or pickup_radius)
@ -294,6 +273,14 @@ if legacy_setting_getbool("item_drop.enable_item_pickup",
local ent = opt_get_ent(object) local ent = opt_get_ent(object)
if ent if ent
and item_drop.can_pickup(ent, player) then and item_drop.can_pickup(ent, player) then
if not inv then
inv = player:get_inventory()
if not inv then
minetest.log("error", "[item_drop] Couldn't " ..
"get inventory")
return
end
end
local item = ItemStack(ent.itemstring) local item = ItemStack(ent.itemstring)
if inv:room_for_item("main", item) then if inv:room_for_item("main", item) then
local flying_item local flying_item
@ -388,13 +375,7 @@ and not minetest.settings:get_bool("creative_mode") then
end end
end end
local old_handle_node_drops = minetest.handle_node_drops function minetest.handle_node_drops(pos, drops)
function minetest.handle_node_drops(pos, drops, player)
if not player or player.is_fake_player then
-- Node Breaker or similar machines should receive items in the
-- inventory
return old_handle_node_drops(pos, drops, player)
end
for i = 1,#drops do for i = 1,#drops do
local item = drops[i] local item = drops[i]
if type(item) == "string" then if type(item) == "string" then
@ -424,29 +405,13 @@ and not minetest.settings:get_bool("creative_mode") then
end end
end end
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
time = 0.2
end
minetest.after(time, pickup_step)
end
minetest.after(3.0, pickup_step)
end end
local time = (minetest.get_us_time() - load_time_start) / 1000000 local time = (minetest.get_us_time() - load_time_start) / 1000000
local msg = "[item_drop] loaded after ca. " .. time .. " seconds." local msg = "[item_drop] loaded after ca. " .. time .. " seconds."
if time > 0.01 then if time > 0.01 then
print(msg) print(msg)
else else
minetest.log("action", msg) minetest.log("info", msg)
end end

Binary file not shown.

Binary file not shown.

Binary file not shown.