From 08b41a3b392005e7078b09d68854e6df562d5fd7 Mon Sep 17 00:00:00 2001 From: nixnoxus Date: Sun, 24 Mar 2024 05:29:44 +0000 Subject: [PATCH] accelerate non-moving carts on `mcl_minecarts:golden_rail_on` (#4097) accelerate a non-moving minecart away from a solid opaque block the powered rail is facing Reviewed-on: https://git.minetest.land/MineClone2/MineClone2/pulls/4097 Reviewed-by: the-real-herowl Co-authored-by: nixnoxus Co-committed-by: nixnoxus --- mods/ENTITIES/mcl_minecarts/functions.lua | 32 ++++++++++++++++++++++- mods/ENTITIES/mcl_minecarts/init.lua | 31 ++++++++++++++++------ mods/ENTITIES/mcl_minecarts/rails.lua | 16 ++++++++++++ 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/mods/ENTITIES/mcl_minecarts/functions.lua b/mods/ENTITIES/mcl_minecarts/functions.lua index 2f0dfe0ae..1792e9252 100644 --- a/mods/ENTITIES/mcl_minecarts/functions.lua +++ b/mods/ENTITIES/mcl_minecarts/functions.lua @@ -134,4 +134,34 @@ function mcl_minecarts:get_rail_direction(pos_, dir, ctrl, old_switch, railtype) end end return {x=0, y=0, z=0} -end \ No newline at end of file +end + +local plane_adjacents = { + vector.new(-1,0,0), + vector.new(1,0,0), + vector.new(0,0,-1), + vector.new(0,0,1), +} + +function mcl_minecarts:get_start_direction(pos) + local dir + local i = 0 + while (not dir and i < #plane_adjacents) do + i = i+1 + local node = minetest.get_node_or_nil(vector.add(pos, plane_adjacents[i])) + if node ~= nil + and minetest.get_item_group(node.name, "rail") == 0 + and minetest.get_item_group(node.name, "solid") == 1 + and minetest.get_item_group(node.name, "opaque") == 1 + then + dir = mcl_minecarts:check_front_up_down(pos, vector.multiply(plane_adjacents[i], -1), true) + end + end + return dir +end + +function mcl_minecarts:set_velocity(obj, dir, factor) + obj._velocity = vector.multiply(dir, factor or 3) + obj._old_pos = nil + obj._punched = true +end diff --git a/mods/ENTITIES/mcl_minecarts/init.lua b/mods/ENTITIES/mcl_minecarts/init.lua index b87175494..89ae44349 100644 --- a/mods/ENTITIES/mcl_minecarts/init.lua +++ b/mods/ENTITIES/mcl_minecarts/init.lua @@ -241,9 +241,7 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o if vector.equals(cart_dir, {x=0, y=0, z=0}) then return end - self._velocity = vector.multiply(cart_dir, 3) - self._old_pos = nil - self._punched = true + mcl_minecarts:set_velocity(self, cart_dir) return end @@ -300,9 +298,7 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o time_from_last_punch = math.min(time_from_last_punch, tool_capabilities.full_punch_interval) local f = 3 * (time_from_last_punch / tool_capabilities.full_punch_interval) - self._velocity = vector.multiply(cart_dir, f) - self._old_pos = nil - self._punched = true + mcl_minecarts:set_velocity(self, cart_dir, f) end cart.on_activate_by_rail = on_activate_by_rail @@ -470,7 +466,7 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o return end - local dir, last_switch = nil, nil + local dir, last_switch, restart_pos = nil, nil, nil if not pos then pos = self.object:get_pos() end @@ -497,6 +493,9 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o minetest.swap_node(rou_pos, newnode) mesecon.receptor_on(rou_pos) end + if node.name == "mcl_minecarts:golden_rail_on" then + restart_pos = rou_pos + end if node_old.name == "mcl_minecarts:detector_rail_on" then local newnode = {name="mcl_minecarts:detector_rail", param2 = node_old.param2} minetest.swap_node(rou_old, newnode) @@ -647,6 +646,14 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o if update.pos then self.object:set_pos(pos) end + + -- stopped on "mcl_minecarts:golden_rail_on" + if vector.equals(vel, {x=0, y=0, z=0}) and restart_pos then + local dir = mcl_minecarts:get_start_direction(restart_pos) + if dir then + mcl_minecarts:set_velocity(self, dir) + end + end end function cart:get_staticdata() @@ -687,7 +694,15 @@ function mcl_minecarts.place_minecart(itemstack, pointed_thing, placer) if le then le._railtype = railtype end - local cart_dir = mcl_minecarts:get_rail_direction(railpos, {x=1, y=0, z=0}, nil, nil, railtype) + local cart_dir + if node.name == "mcl_minecarts:golden_rail_on" then + cart_dir = mcl_minecarts:get_start_direction(railpos) + end + if cart_dir then + mcl_minecarts:set_velocity(le, cart_dir) + else + cart_dir = mcl_minecarts:get_rail_direction(railpos, {x=1, y=0, z=0}, nil, nil, railtype) + end cart:set_yaw(minetest.dir_to_yaw(cart_dir)) local pname = "" diff --git a/mods/ENTITIES/mcl_minecarts/rails.lua b/mods/ENTITIES/mcl_minecarts/rails.lua index 9c9050341..17c854e68 100644 --- a/mods/ENTITIES/mcl_minecarts/rails.lua +++ b/mods/ENTITIES/mcl_minecarts/rails.lua @@ -112,6 +112,22 @@ register_rail("mcl_minecarts:golden_rail_on", onstate = "mcl_minecarts:golden_rail_on", rules = rail_rules_long, }, + effector = { + action_on = function(pos, node) + local dir = mcl_minecarts:get_start_direction(pos) + if not dir then return end + local objs = minetest.get_objects_inside_radius(pos, 1) + for _, o in pairs(objs) do + local l = o:get_luaentity() + local v = o:get_velocity() + if l and string.sub(l.name, 1, 14) == "mcl_minecarts:" + and v and vector.equals(v, vector.zero()) + then + mcl_minecarts:set_velocity(l, dir) + end + end +end, + }, }, drop = "mcl_minecarts:golden_rail", },