Fix more rail connection bugs

This commit is contained in:
teknomunk 2024-04-01 06:12:59 +00:00
parent 3391a28fa9
commit 8cd9ee9b32

View file

@ -2,6 +2,34 @@ local vector = vector
local mod = mcl_minecarts local mod = mcl_minecarts
local table_merge = mcl_util.table_merge local table_merge = mcl_util.table_merge
function get_path(base, first, ...)
if not first then return base end
if not base then return end
return get_path(base[first], ...)
end
local function force_get_node(pos)
local node = minetest.get_node(pos)
if node.name ~= "ignore" then return node end
local vm = minetest.get_voxel_manip()
local emin, emax = vm:read_from_map(pos, pos)
local area = VoxelArea:new{
MinEdge = emin,
MaxEdge = emax,
}
local data = vm:get_data()
local param_data = vm:get_light_data()
local param2_data = vm:get_param2_data()
local vi = area:indexp(pos)
return {
name = minetest.get_name_from_content_id(data[vi]),
param = param_data[vi],
param2 = param2_data[vi]
}
end
function mcl_minecarts:get_sign(z) function mcl_minecarts:get_sign(z)
if z == 0 then if z == 0 then
return 0 return 0
@ -10,12 +38,6 @@ function mcl_minecarts:get_sign(z)
end end
end end
function get_path(base, first, ...)
if not first then return base end
if not base then return end
return get_path(base[first], ...)
end
function mcl_minecarts:velocity_to_dir(v) function mcl_minecarts:velocity_to_dir(v)
if math.abs(v.x) > math.abs(v.z) then if math.abs(v.x) > math.abs(v.z) then
return vector.new( return vector.new(
@ -33,25 +55,15 @@ function mcl_minecarts:velocity_to_dir(v)
end end
function mcl_minecarts:is_rail(pos, railtype) function mcl_minecarts:is_rail(pos, railtype)
local node = minetest.get_node(pos).name local node_name = force_get_node(pos).name
if node == "ignore" then
local vm = minetest.get_voxel_manip() if minetest.get_item_group(node_name, "rail") == 0 then
local emin, emax = vm:read_from_map(pos, pos)
local area = VoxelArea:new{
MinEdge = emin,
MaxEdge = emax,
}
local data = vm:get_data()
local vi = area:indexp(pos)
node = minetest.get_name_from_content_id(data[vi])
end
if minetest.get_item_group(node, "rail") == 0 then
return false return false
end end
if not railtype then if not railtype then
return true return true
end end
return minetest.get_item_group(node, "connect_to_raillike") == railtype return minetest.get_item_group(node_name, "connect_to_raillike") == railtype
end end
--[[ --[[
@ -177,10 +189,9 @@ local function update_rail_connections(pos, update_neighbors)
-- Only allow connections to the open ends of rails, as decribed by get_next_dir -- Only allow connections to the open ends of rails, as decribed by get_next_dir
if get_path(nodedef, "groups", "rail") and get_path(nodedef, "_mcl_minecarts", "get_next_dir" ) then if get_path(nodedef, "groups", "rail") and get_path(nodedef, "_mcl_minecarts", "get_next_dir" ) then
--if nodedef and (nodedef.groups or {}).rail and nodedef._mcl_minecarts and nodedef._mcl_minecarts.get_next_dir then local rev_dir = vector.direction(dir,vector.new(0,0,0))
local diff = vector.direction(neighbor, pos) --local next_dir = nodedef._mcl_minecarts.get_next_dir(neighbor, rev_dir, node)
local next_dir = nodedef._mcl_minecarts.get_next_dir(neighbor, diff, node) if mcl_minecarts:is_connection(neighbor, rev_dir) then
if next_dir == diff then
connections = connections + bit.lshift(1,i - 1) connections = connections + bit.lshift(1,i - 1)
end end
end end
@ -220,14 +231,12 @@ local function update_rail_connections(pos, update_neighbors)
local node_def = minetest.registered_nodes[node.name] local node_def = minetest.registered_nodes[node.name]
if get_path(node_def, "_mcl_minecarts", "can_slope") then if get_path(node_def, "_mcl_minecarts", "can_slope") then
for _,dir in ipairs(CONNECTIONS) do for _,dir in ipairs(CONNECTIONS) do
if mcl_minecarts:is_rail(vector.offset(pos,dir.x,1,dir.z)) then local higher_rail_pos = vector.offset(pos,dir.x,1,dir.z)
local rev_dir = vector.direction(dir,vector.new(0,0,0)) local rev_dir = vector.direction(dir,vector.new(0,0,0))
print("try to make slope at "..tostring(pos)) if mcl_minecarts:is_rail(higher_rail_pos) and mcl_minecarts:is_connection(higher_rail_pos, rev_dir) then
make_sloped_if_straight(pos, rev_dir) make_sloped_if_straight(pos, rev_dir)
end end
end end
else
print(node.name.." can't slope")
end end
end end
@ -273,6 +282,16 @@ local diagonal_convert = {
sw = south, sw = south,
} }
function mcl_minecarts:is_connection(pos, dir)
local node = force_get_node(pos)
local nodedef = minetest.registered_nodes[node.name]
local get_next_dir = get_path(nodedef, "_mcl_minecarts", "get_next_dir")
if not get_next_dir then return end
return get_next_dir(pos, dir, node) == dir
end
function mcl_minecarts:get_rail_direction(pos_, dir, ctrl, old_switch, railtype) function mcl_minecarts:get_rail_direction(pos_, dir, ctrl, old_switch, railtype)
local pos = vector.round(pos_) local pos = vector.round(pos_)