More fixes for minecart-hopper movement

This commit is contained in:
teknomunk 2024-04-12 18:17:17 +00:00
parent e9c4cdf62f
commit e5fb891d99
3 changed files with 33 additions and 19 deletions

View File

@ -1212,16 +1212,16 @@ function mcl_util.assign_uuid(obj)
return le._uuid return le._uuid
end end
function mcl_util.metadata_timer(meta, name, dtime) function mcl_util.metadata_last_act(meta, name, delay)
local tick = false local last_act = meta:get_float(name)
local timer = meta:get_float(name) local now = minetest.get_us_time() * 1e-6
if timer < dtime then if last_act > now + 0.5 then
tick = true -- Last action was in the future, clock went backwards, so reset
timer = timer + 1 elseif last_act >= now - delay then
else return false
timer = timer - dtime
end end
meta:set_float(name, timer)
if not tick then return true end meta:set_float(name, now)
return true
end end

View File

@ -445,12 +445,13 @@ local function do_movement( staticdata, dtime )
assert(staticdata) assert(staticdata)
-- Allow the carts to be delay for the rest of the world to react before moving again -- Allow the carts to be delay for the rest of the world to react before moving again
--[[
if ( staticdata.delay or 0 ) > dtime then if ( staticdata.delay or 0 ) > dtime then
staticdata.delay = staticdata.delay - dtime staticdata.delay = staticdata.delay - dtime
return return
else else
staticdata.delay = 0 staticdata.delay = 0
end end]]
-- Break long movements at block boundaries to make it -- Break long movements at block boundaries to make it
-- it impossible to jump across gaps due to server lag -- it impossible to jump across gaps due to server lag

View File

@ -96,7 +96,12 @@ local function bent_hopper_act(pos, node, active_object_count, active_object_cou
mcl_util.hopper_pull(pos, src_pos) mcl_util.hopper_pull(pos, src_pos)
end end
--[[
Returns true if an item was pushed to the minecart
]]
local function hopper_push_to_mc(mc_ent, dest_pos, inv_size) local function hopper_push_to_mc(mc_ent, dest_pos, inv_size)
if not mcl_util.metadata_last_act(minetest.get_meta(dest_pos), "hopper_push_timer", 1) then return false end
local dest_inv = mcl_entity_invs.load_inv(mc_ent, inv_size) local dest_inv = mcl_entity_invs.load_inv(mc_ent, inv_size)
if not dest_inv then if not dest_inv then
mcl_log("No inv") mcl_log("No inv")
@ -136,7 +141,12 @@ local function hopper_push_to_mc(mc_ent, dest_pos, inv_size)
end end
end end
end end
--[[
Returns true if an item was pulled from the minecart
]]
local function hopper_pull_from_mc(mc_ent, dest_pos, inv_size) local function hopper_pull_from_mc(mc_ent, dest_pos, inv_size)
if not mcl_util.metadata_last_act(minetest.get_meta(dest_pos), "hopper_pull_timer", 1) then return false end
local inv = mcl_entity_invs.load_inv(mc_ent, inv_size) local inv = mcl_entity_invs.load_inv(mc_ent, inv_size)
if not inv then if not inv then
mcl_log("No inv") mcl_log("No inv")
@ -309,12 +319,12 @@ local def_hopper = {
return return
end end
if not mcl_util.metadata_timer(minetest.get_meta(pos), "minecart_hopper_timer", dtime) then return end
local cart_pos = mcl_minecarts.get_cart_position(cartdata) local cart_pos = mcl_minecarts.get_cart_position(cartdata)
if not cart_pos then return false end if not cart_pos then return false end
if pos.x ~= cart_pos.x or pos.z ~= cart_pos.z then return end if vector.distance(cart_pos, pos) > 1.5 then
cart:remove_node_watch(pos)
return
end
if vector.direction(pos,cart_pos).y > 0 then if vector.direction(pos,cart_pos).y > 0 then
-- The cart is above us, pull from minecart -- The cart is above us, pull from minecart
hopper_pull_from_mc(cart, pos, 5) hopper_pull_from_mc(cart, pos, 5)
@ -538,6 +548,7 @@ local def_hopper_side = {
end, end,
_mcl_minecarts_on_leave_below = function(pos, cart, next_dir) _mcl_minecarts_on_leave_below = function(pos, cart, next_dir)
if not cart then return end if not cart then return end
cart:remove_node_watch(pos) cart:remove_node_watch(pos)
end, end,
_mcl_minecarts_on_enter_side = function(pos, cart, next_dir, rail_pos) _mcl_minecarts_on_enter_side = function(pos, cart, next_dir, rail_pos)
@ -561,6 +572,8 @@ local def_hopper_side = {
if cart.groups and (cart.groups.container or 0) ~= 0 then if cart.groups and (cart.groups.container or 0) ~= 0 then
cart:add_node_watch(pos) cart:add_node_watch(pos)
end end
hopper_push_to_mc(cart, pos, 5)
end, end,
_mcl_minecarts_on_leave_side = function(pos, cart, next_dir) _mcl_minecarts_on_leave_side = function(pos, cart, next_dir)
if not cart then return end if not cart then return end
@ -570,13 +583,13 @@ local def_hopper_side = {
_mcl_minecarts_node_on_step = function(pos, cart, dtime, cartdata) _mcl_minecarts_node_on_step = function(pos, cart, dtime, cartdata)
if not cart then return end if not cart then return end
if not mcl_util.metadata_timer(minetest.get_meta(pos), "minecart_hopper_timer", dtime) then return end
local cart_pos = mcl_minecarts.get_cart_position(cartdata) local cart_pos = mcl_minecarts.get_cart_position(cartdata)
if not cart_pos then return false end if not cart_pos then return false end
if cart_pos.x ~= pos.x or cart_pos.z ~= pos.x then return end if vector.distance(cart_pos, pos) > 1.5 then
cart:remove_node_watch(pos)
return false
end
print("uuid="..cartdata.uuid)
if cart_pos.y == pos.y then if cart_pos.y == pos.y then
hopper_push_to_mc(cart, pos, 5) hopper_push_to_mc(cart, pos, 5)
elseif cart_pos.y > pos.y then elseif cart_pos.y > pos.y then