Merge pull request 'More fixes to item collection on laggy servers' (#1442) from jordan4ibanez/MineClone2-FixA:master into master

Reviewed-on: https://git.minetest.land/MineClone2/MineClone2/pulls/1442
This commit is contained in:
jordan4ibanez 2021-04-03 00:43:42 +00:00
commit b83b4c55fa

View File

@ -4,6 +4,7 @@ local mcl_item_entity = {}
--basic settings --basic settings
local item_drop_settings = {} --settings table local item_drop_settings = {} --settings table
item_drop_settings.dug_buffer = 0.65 -- the warm up period before a dug item can be collected
item_drop_settings.age = 1.0 --how old a dropped item (_insta_collect==false) has to be before collecting item_drop_settings.age = 1.0 --how old a dropped item (_insta_collect==false) has to be before collecting
item_drop_settings.radius_magnet = 2.0 --radius of item magnet. MUST BE LARGER THAN radius_collect! item_drop_settings.radius_magnet = 2.0 --radius of item magnet. MUST BE LARGER THAN radius_collect!
item_drop_settings.xp_radius_magnet = 7.25 --radius of xp magnet. MUST BE LARGER THAN radius_collect! item_drop_settings.xp_radius_magnet = 7.25 --radius of xp magnet. MUST BE LARGER THAN radius_collect!
@ -74,69 +75,50 @@ local disable_physics = function(object, luaentity, ignore_check, reset_movement
end end
end end
--this is a 0.2 second tick globally across all players
local item_check_ticker = 0
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
item_check_ticker = item_check_ticker + dtime for _,player in pairs(minetest.get_connected_players()) do
if item_check_ticker >= 0.2 then if player:get_hp() > 0 or not minetest.settings:get_bool("enable_damage") then
item_check_ticker = 0 local pos = player:get_pos()
local inv = player:get_inventory()
local checkpos = {x=pos.x,y=pos.y + item_drop_settings.player_collect_height,z=pos.z}
for _,player in pairs(minetest.get_connected_players()) do --magnet and collection
if player:get_hp() > 0 or not minetest.settings:get_bool("enable_damage") then for _,object in pairs(minetest.get_objects_inside_radius(checkpos, item_drop_settings.xp_radius_magnet)) do
local pos = player:get_pos() if not object:is_player() and vector.distance(checkpos, object:get_pos()) < item_drop_settings.radius_magnet and object:get_luaentity() and object:get_luaentity().name == "__builtin:item" and object:get_luaentity()._magnet_timer and (object:get_luaentity()._insta_collect or (object:get_luaentity().age > item_drop_settings.age)) then
local inv = player:get_inventory()
local checkpos = {x=pos.x,y=pos.y + item_drop_settings.player_collect_height,z=pos.z}
--magnet and collection if object:get_luaentity()._magnet_timer >= 0 and object:get_luaentity()._magnet_timer < item_drop_settings.magnet_time and inv and inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then
for _,object in pairs(minetest.get_objects_inside_radius(checkpos, item_drop_settings.xp_radius_magnet)) do
if not object:is_player() and vector.distance(checkpos, object:get_pos()) < item_drop_settings.radius_magnet and object:get_luaentity() and object:get_luaentity().name == "__builtin:item" and object:get_luaentity()._magnet_timer and (object:get_luaentity()._insta_collect or (object:get_luaentity().age > item_drop_settings.age)) then
object:get_luaentity()._magnet_timer = object:get_luaentity()._magnet_timer + dtime
local collected = false
if object:get_luaentity()._magnet_timer >= 0 and object:get_luaentity()._magnet_timer < item_drop_settings.magnet_time and inv and inv:room_for_item("main", ItemStack(object:get_luaentity().itemstring)) then
-- Collection -- Collection
if not object:get_luaentity()._removed then if not object:get_luaentity()._removed then
-- Ignore if itemstring is not set yet -- Ignore if itemstring is not set yet
if object:get_luaentity().itemstring ~= "" then if object:get_luaentity().itemstring ~= "" then
inv:add_item("main", ItemStack(object:get_luaentity().itemstring)) inv:add_item("main", ItemStack(object:get_luaentity().itemstring))
minetest.sound_play("item_drop_pickup", { minetest.sound_play("item_drop_pickup", {
pos = pos, pos = pos,
max_hear_distance = 16, max_hear_distance = 16,
gain = 1.0, gain = 1.0,
}, true) }, true)
check_pickup_achievements(object, player) check_pickup_achievements(object, player)
object:move_to(checkpos, false) object:move_to(checkpos, true)
-- Destroy entity -- Destroy entity
-- This just prevents this section to be run again because object:remove() doesn't remove the item immediately. -- This just prevents this section to be run again because object:remove() doesn't remove the item immediately.
object:get_luaentity()._removed = true object:get_luaentity()._removed = true
collected = true
end
end end
end end
if not collected then
if object:get_luaentity()._magnet_timer > 1 then
object:get_luaentity()._magnet_timer = -item_drop_settings.magnet_time
object:get_luaentity()._magnet_active = false
elseif object:get_luaentity()._magnet_timer < 0 then
object:get_luaentity()._magnet_timer = object:get_luaentity()._magnet_timer + dtime
end
end
elseif not object:is_player() and object:get_luaentity() and object:get_luaentity().name == "mcl_experience:orb" then
local entity = object:get_luaentity()
entity.collector = player:get_player_name()
entity.collected = true
end end
end
elseif not object:is_player() and object:get_luaentity() and object:get_luaentity().name == "mcl_experience:orb" then
local entity = object:get_luaentity()
entity.collector = player:get_player_name()
entity.collected = true
end
end end
end end
end end
end) end)
@ -291,6 +273,12 @@ function minetest.handle_node_drops(pos, drops, digger)
z = -z z = -z
end end
obj:set_velocity({x=1/x, y=obj:get_velocity().y, z=1/z}) obj:set_velocity({x=1/x, y=obj:get_velocity().y, z=1/z})
obj:get_luaentity().age = item_drop_settings.dug_buffer
obj:get_luaentity()._insta_collect = false
print(obj:get_luaentity().age)
end end
end end
end end