From db2f02b485051a994fd2175a3e0cab46d99ef3c3 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Sun, 28 Apr 2024 08:09:42 +0000 Subject: [PATCH] Restore 45 degree cart movement, remove warning about unknown global --- mods/ENTITIES/mcl_minecarts/functions.lua | 39 +++++++++++++++++++++-- mods/ENTITIES/mcl_minecarts/movement.lua | 2 +- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/mods/ENTITIES/mcl_minecarts/functions.lua b/mods/ENTITIES/mcl_minecarts/functions.lua index 7a44d036c..d9e12fb02 100644 --- a/mods/ENTITIES/mcl_minecarts/functions.lua +++ b/mods/ENTITIES/mcl_minecarts/functions.lua @@ -301,9 +301,8 @@ local function is_ahead_slope(pos, dir) local node_name = force_get_node(below).name return minetest.get_item_group(node_name, "rail_slope") ~= 0 end -function mcl_minecarts:get_rail_direction(pos_, dir) - local pos = vector.round(pos_) +local function get_rail_direction_inner(pos, dir) -- Handle new track types that have track-specific direction handler local node = minetest.get_node(pos) local node_def = minetest.registered_nodes[node.name] @@ -328,6 +327,42 @@ function mcl_minecarts:get_rail_direction(pos_, dir) return dir end +function mcl_minecarts:get_rail_direction(pos_, dir) + local pos = vector.round(pos_) + + -- diagonal direction handling + if dir.x ~= 0 and dir.z ~= 0 then + -- Check both possible diagonal movements + local dir_a = vector.new(dir.x,0,0) + local dir_b = vector.new(0,0,dir.z) + local new_dir_a = mcl_minecarts:get_rail_direction(pos, dir_a) + local new_dir_b = mcl_minecarts:get_rail_direction(pos, dir_b) + + -- If either is the same diagonal direction, continue as you were + if vector.equals(dir,new_dir_a) or vector.equals(dir,new_dir_b) then + return dir + + -- Otherwise, if either would try to move in the same direction as + -- what tried, move that direction + elseif vector.equals(dir_a, new_dir_a) then + return new_dir_a + elseif vector.equals(dir_b, new_dir_b) then + return new_dir_b + end + + -- And if none of these were true, fall thru into standard behavior + end + + local new_dir = get_rail_direction_inner(pos, dir) + + -- Check four 45 degree movement + local next_rails_dir = get_rail_direction_inner(vector.add(pos, new_dir), new_dir) + if vector.equals(next_rails_dir, dir) and not vector.equals(new_dir, next_rails_dir) then + return vector.add(new_dir, next_rails_dir) + else + return new_dir + end +end function mod.update_cart_orientation(self) local staticdata = self._staticdata diff --git a/mods/ENTITIES/mcl_minecarts/movement.lua b/mods/ENTITIES/mcl_minecarts/movement.lua index 06e67ed94..522d533ab 100644 --- a/mods/ENTITIES/mcl_minecarts/movement.lua +++ b/mods/ENTITIES/mcl_minecarts/movement.lua @@ -531,7 +531,7 @@ local function do_detached_movement(self, dtime) } for i=1,#test_positions do - test_pos = test_positions[i] + local test_pos = test_positions[i] local pos_r = vector.round(test_pos) local node = minetest.get_node(pos_r) if minetest.get_item_group(node.name, "rail") ~= 0 then