From fd9cbaf813a20297132a4bc14ab1cc0ba45c0f38 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Sun, 28 Apr 2024 06:21:46 +0000 Subject: [PATCH] Rework rail_dir_curve to significantly reduce code size --- mods/ENTITIES/mcl_minecarts/rails.lua | 44 +++++---------------------- 1 file changed, 8 insertions(+), 36 deletions(-) diff --git a/mods/ENTITIES/mcl_minecarts/rails.lua b/mods/ENTITIES/mcl_minecarts/rails.lua index f54ccadf4..f4d14f867 100644 --- a/mods/ENTITIES/mcl_minecarts/rails.lua +++ b/mods/ENTITIES/mcl_minecarts/rails.lua @@ -45,47 +45,19 @@ local function rail_dir_sloped(pos, dir, node) return downhill end end +-- Fourdir +-- 0 = north +-- 1 = east +-- 2 = south +-- 3 = west +local CURVE_RAIL_DIRS = { [0] = 1, 1, 2, 2, } 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 - -- 0 = north - -- 1 = east - -- 2 = south - -- 3 = west - - local new_fourdir = 0 - if node.param2 == 0 then -- north - -- South and East - if dir_fourdir == 0 then new_fourdir = 1 end - 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 - + local dir_fourdir = (minetest.dir_to_fourdir(dir) - node.param2 + 4) % 4 + local new_fourdir = (CURVE_RAIL_DIRS[dir_fourdir] + node.param2) % 4 return minetest.fourdir_to_dir(new_fourdir) end - local function rail_dir_tee_off(pos, dir, node) dir = vector.new(dir.x, 0, dir.z)