Rework rail_dir_curve to significantly reduce code size

This commit is contained in:
teknomunk 2024-04-28 06:21:46 +00:00
parent bb0f0747ae
commit d12896c162
1 changed files with 8 additions and 36 deletions

View File

@ -45,47 +45,19 @@ local function rail_dir_sloped(pos, dir, node)
return downhill return downhill
end end
end end
local function rail_dir_curve(pos, dir, node)
dir = vector.new(dir.x, 0, dir.z)
local dir_fourdir = minetest.dir_to_fourdir(dir)
-- Fourdir -- Fourdir
-- 0 = north -- 0 = north
-- 1 = east -- 1 = east
-- 2 = south -- 2 = south
-- 3 = west -- 3 = west
local CURVE_RAIL_DIRS = { [0] = 1, 1, 2, 2, }
local new_fourdir = 0 local function rail_dir_curve(pos, dir, node)
if node.param2 == 0 then -- north dir = vector.new(dir.x, 0, dir.z)
-- South and East local dir_fourdir = (minetest.dir_to_fourdir(dir) - node.param2 + 4) % 4
if dir_fourdir == 0 then new_fourdir = 1 end local new_fourdir = (CURVE_RAIL_DIRS[dir_fourdir] + node.param2) % 4
if dir_fourdir == 1 then new_fourdir = 1 end
if dir_fourdir == 2 then new_fourdir = 2 end
if dir_fourdir == 3 then new_fourdir = 2 end
elseif node.param2 == 1 then -- east
-- South and West
if dir_fourdir == 1 then new_fourdir = 2 end
if dir_fourdir == 2 then new_fourdir = 2 end
if dir_fourdir == 3 then new_fourdir = 3 end
if dir_fourdir == 0 then new_fourdir = 3 end
elseif node.param2 == 2 then -- south
-- North and West
if dir_fourdir == 2 then new_fourdir = 3 end
if dir_fourdir == 3 then new_fourdir = 3 end
if dir_fourdir == 0 then new_fourdir = 0 end
if dir_fourdir == 1 then new_fourdir = 0 end
elseif node.param2 == 3 then -- west
-- North and East
if dir_fourdir == 3 then new_fourdir = 0 end
if dir_fourdir == 0 then new_fourdir = 0 end
if dir_fourdir == 1 then new_fourdir = 1 end
if dir_fourdir == 2 then new_fourdir = 1 end
end
return minetest.fourdir_to_dir(new_fourdir) return minetest.fourdir_to_dir(new_fourdir)
end end
local function rail_dir_tee_off(pos, dir, node) local function rail_dir_tee_off(pos, dir, node)
dir = vector.new(dir.x, 0, dir.z) dir = vector.new(dir.x, 0, dir.z)