From f1b23796cced37fc970ac3234e6eaabdb7966974 Mon Sep 17 00:00:00 2001 From: teknomunk Date: Sun, 31 Mar 2024 20:20:34 +0000 Subject: [PATCH] Get sloped connections working correctly --- mods/ENTITIES/mcl_minecarts/functions.lua | 79 +++++++++++++++---- .../mcl_minecarts/models/sloped_track.obj | 8 +- mods/ENTITIES/mcl_minecarts/rails.lua | 51 ++++++++---- 3 files changed, 102 insertions(+), 36 deletions(-) diff --git a/mods/ENTITIES/mcl_minecarts/functions.lua b/mods/ENTITIES/mcl_minecarts/functions.lua index f8adf8b7f..8265d1139 100644 --- a/mods/ENTITIES/mcl_minecarts/functions.lua +++ b/mods/ENTITIES/mcl_minecarts/functions.lua @@ -10,6 +10,12 @@ function mcl_minecarts:get_sign(z) 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) if math.abs(v.x) > math.abs(v.z) then return vector.new( @@ -82,13 +88,13 @@ mod.west = west local CONNECTIONS = { north, south, east, west } local HORIZONTAL_STANDARD_RULES = { - [N] = { "", 0, mask = N, score = 1 }, - [S] = { "", 0, mask = S, score = 1 }, - [N+S] = { "", 0, mask = N+S, score = 2 }, + [N] = { "", 0, mask = N, score = 1, can_slope = true }, + [S] = { "", 0, mask = S, score = 1, can_slope = true }, + [N+S] = { "", 0, mask = N+S, score = 2, can_slope = true }, - [E] = { "", 1, mask = E, score = 1 }, - [W] = { "", 1, mask = W, score = 1 }, - [E+W] = { "", 1, mask = E+W, score = 2 }, + [E] = { "", 1, mask = E, score = 1, can_slope = true }, + [W] = { "", 1, mask = W, score = 1, can_slope = true }, + [E+W] = { "", 1, mask = E+W, score = 2, can_slope = true }, } local HORIZONTAL_CURVES_RULES = { @@ -127,6 +133,26 @@ local function check_connection_rule(pos, connections, rule) end mod.check_connection_rules = check_connection_rules +local function make_sloped_if_straight(pos, dir) + local node = minetest.get_node(pos) + local nodedef = minetest.registered_nodes[node.name] + + local param2 = 0 + if dir == east then + param2 = 3 + elseif dir == west then + param2 = 1 + elseif dir == north then + param2 = 2 + elseif dir == south then + param2 = 0 + end + + if get_path( nodedef, "_mcl_minecarts", "railtype" ) == "straight" then + minetest.swap_node(pos, {name = nodedef._mcl_minecarts.base_name .. "_sloped", param2 = param2}) + end +end + local function update_rail_connections(pos, update_neighbors) local node = minetest.get_node(pos) local nodedef = minetest.registered_nodes[node.name] @@ -150,13 +176,17 @@ local function update_rail_connections(pos, update_neighbors) local nodedef = minetest.registered_nodes[node.name] -- Only allow connections to the open ends of rails, as decribed by get_next_dir - if (nodedef.groups or {}).rail and nodedef._mcl_minecarts and 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 diff = vector.direction(neighbor, pos) local next_dir = nodedef._mcl_minecarts.get_next_dir(neighbor, diff, node) if next_dir == diff then connections = connections + bit.lshift(1,i - 1) end end + + -- Check for rasing rails to slopes + make_sloped_if_straight( vector.offset(neighbor, 0, -1, 0), dir ) end -- Select the best allowed connection @@ -171,20 +201,35 @@ local function update_rail_connections(pos, update_neighbors) end end end - if not rule then return end + if rule then - -- Apply the mapping - local new_name = nodedef._mcl_minecarts.base_name..rule[1] - if new_name ~= node.name or node.param2 ~= rule[2] then - print("swapping "..node.name.." for "..new_name..","..tostring(rule[2]).." at "..tostring(pos)) - node.name = new_name - node.param2 = rule[2] - minetest.swap_node(pos, node) + -- Apply the mapping + local new_name = nodedef._mcl_minecarts.base_name..rule[1] + if new_name ~= node.name or node.param2 ~= rule[2] then + print("swapping "..node.name.." for "..new_name..","..tostring(rule[2]).." at "..tostring(pos)) + node.name = new_name + node.param2 = rule[2] + minetest.swap_node(pos, node) + end + + if rule.after then + rule.after(rule, pos, connections) + end end - if rule.after then - rule.after(rule, pos, connections) + local node_def = minetest.registered_nodes[node.name] + if get_path(node_def, "_mcl_minecarts", "can_slope") then + for _,dir in ipairs(CONNECTIONS) do + if mcl_minecarts:is_rail(vector.offset(pos,dir.x,1,dir.z)) then + local rev_dir = vector.direction(dir,vector.new(0,0,0)) + print("try to make slope at "..tostring(pos)) + make_sloped_if_straight(pos, rev_dir) + end + end + else + print(node.name.." can't slope") end + end mod.update_rail_connections = update_rail_connections diff --git a/mods/ENTITIES/mcl_minecarts/models/sloped_track.obj b/mods/ENTITIES/mcl_minecarts/models/sloped_track.obj index 86edb4b58..b5c106278 100644 --- a/mods/ENTITIES/mcl_minecarts/models/sloped_track.obj +++ b/mods/ENTITIES/mcl_minecarts/models/sloped_track.obj @@ -1,10 +1,10 @@ # hand-made Wavefront .OBJ file for sloped rail mtllib mcl_minecarts_rail.mtl o sloped_rail.001 -v -0.500000 -0.500000 -0.500000 -v -0.500000 0.500000 0.500000 -v 0.500000 0.500000 0.500000 -v 0.500000 -0.500000 -0.500000 +v -0.500000 -0.437500 -0.500000 +v -0.500000 0.562500 0.500000 +v 0.500000 0.562500 0.500000 +v 0.500000 -0.437500 -0.500000 vt 1.000000 0.000000 vt 1.000000 1.000000 vt 0.000000 1.000000 diff --git a/mods/ENTITIES/mcl_minecarts/rails.lua b/mods/ENTITIES/mcl_minecarts/rails.lua index df39fb314..ba02717cd 100644 --- a/mods/ENTITIES/mcl_minecarts/rails.lua +++ b/mods/ENTITIES/mcl_minecarts/rails.lua @@ -98,7 +98,7 @@ local BASE_DEF = { } }, paramtype = "light", - paramtype2 = "facedir", + paramtype2 = "4dir", } table_merge(BASE_DEF, RAIL_DEFAULTS) -- Merge together old rail values @@ -213,27 +213,32 @@ end local function register_straight_rail(base_name, tiles, def) def = def or {} local base_def = table.copy(BASE_DEF) - table_merge(base_def,{ + local sloped_def = table.copy(SLOPED_RAIL_DEF) + local add = { tiles = { tiles[1] }, - _mcl_minecarts = { base_name = base_name }, drop = base_name, groups = { - rail = mod.RAIL_GROUPS.STRANDARD, + rail = mod.RAIL_GROUPS.STANDARD, }, _mcl_minecarts = { base_name = base_name, - get_next_dir = rail_dir_straight + get_next_dir = rail_dir_straight, + can_slope = true, }, - }) - table_merge(base_def, def) + } + table_merge(base_def, add); table_merge(sloped_def, add) + table_merge(base_def, def); table_merge(sloped_def, def) -- Register the base node mod.register_rail(base_name, base_def) - base_def.craft = false + base_def.craft = nil; sloped_def.craft = nil table_merge(base_def,{ groups = { not_in_creative_inventory = 1, }, + _mcl_minecarts = { + railtype = "straight", + }, }) -- Sloped variant @@ -243,6 +248,9 @@ local function register_straight_rail(base_name, tiles, def) get_next_dir = rail_dir_cross, }, tiles = { tiles[1] }, + _mcl_minecarts = { + railtype = "sloped", + }, })) end mod.register_straight_rail = register_straight_rail @@ -250,21 +258,25 @@ mod.register_straight_rail = register_straight_rail local function register_curves_rail(base_name, tiles, def) def = def or {} local base_def = table.copy(BASE_DEF) - table_merge(base_def,{ + local sloped_def = table.copy(SLOPED_RAIL_DEF) + local add = { _mcl_minecarts = { base_name = base_name }, groups = { rail = mod.RAIL_GROUPS.CURVES }, drop = base_name, - }) - table_merge(base_def, def) + } + table_merge(base_def, add); table_merge(sloped_def, add) + table_merge(base_def, def); table_merge(sloped_def, def) -- Register the base node mod.register_rail(base_name, table_merge(table.copy(base_def),{ tiles = { tiles[1] }, _mcl_minecarts = { - get_next_dir = rail_dir_straight - } + get_next_dir = rail_dir_straight, + railtype = "straight", + can_slope = true, + }, })) -- Update for other variants @@ -280,6 +292,7 @@ local function register_curves_rail(base_name, tiles, def) tiles = { tiles[2] }, _mcl_minecarts = { get_next_dir = rail_dir_curve, + railtype = "corner", }, })) @@ -297,12 +310,16 @@ local function register_curves_rail(base_name, tiles, def) end, rules = mesecon.rules.alldirs, } - } + }, + _mcl_minecarts = { + railtype = "corner", + }, })) mod.register_rail(base_name.."_tee_on", table_merge(table.copy(base_def),{ tiles = { tiles[4] }, _mcl_minecarts = { get_next_dir = rail_dir_tee, + railtype = "tee", }, mesecons = { effector = { @@ -316,10 +333,11 @@ local function register_curves_rail(base_name, tiles, def) })) -- Sloped variant - mod.register_rail_sloped(base_name.."_sloped", table_merge(table.copy(base_def),{ + mod.register_rail_sloped(base_name.."_sloped", table_merge(table.copy(sloped_def),{ description = S("Sloped Rail"), -- Temporary name to make debugging easier _mcl_minecarts = { get_next_dir = rail_dir_cross, + railtype = "tee", }, tiles = { tiles[1] }, })) @@ -327,6 +345,9 @@ local function register_curves_rail(base_name, tiles, def) -- Cross variant mod.register_rail(base_name.."_cross", table_merge(table.copy(base_def),{ tiles = { tiles[5] }, + _mcl_minecarts = { + railtype = "cross", + }, })) end mod.register_curves_rail = register_curves_rail