diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index 4722ad575..28f5503d1 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -1171,3 +1171,11 @@ local function table_merge(base, overlay) end mcl_util.table_merge = table_merge +function mcl_util.table_keys(t) + local keys = {} + for k,_ in pairs(t) do + keys[#keys + 1] = k + end + return keys +end + diff --git a/mods/ENTITIES/mcl_minecarts/functions.lua b/mods/ENTITIES/mcl_minecarts/functions.lua index 63673b631..766412f71 100644 --- a/mods/ENTITIES/mcl_minecarts/functions.lua +++ b/mods/ENTITIES/mcl_minecarts/functions.lua @@ -145,7 +145,6 @@ local function check_connection_rule(pos, connections, rule) return true end -mod.check_connection_rules = check_connection_rules local function make_sloped_if_straight(pos, dir) local node = minetest.get_node(pos) diff --git a/mods/ENTITIES/mcl_minecarts/init.lua b/mods/ENTITIES/mcl_minecarts/init.lua index 2ee7f8e12..137de228a 100644 --- a/mods/ENTITIES/mcl_minecarts/init.lua +++ b/mods/ENTITIES/mcl_minecarts/init.lua @@ -39,6 +39,22 @@ mcl_minecarts.on_enter_below = function(pos, cart, next_dir, node_def) end end +local function detach_minecart(self) + local staticdata = self._staticdata + print("Detaching minecart") + + staticdata.connected_at = nil + self.object:set_velocity(staticdata.dir * staticdata.velocity) +end +local function try_detach_minecart(self) + local staticdata = self._staticdata + + local node = minetest.get_node(staticdata.connected_at) + if minetest.get_item_group(node.name, "rail") == 0 then + detach_minecart(self) + end +end + --[[ Array of hooks { {u,v,w}, name } Actual position is pos + u * dir + v * right + w * up @@ -175,6 +191,9 @@ end local function calculate_acceleration(self, staticdata) local acceleration = 0 + -- Fix up movement data + staticdata.velocity = staticdata.velocity or 0 + -- Apply friction if moving if staticdata.velocity > 0 then acceleration = -friction @@ -338,6 +357,8 @@ local function do_movement_step(self, dtime) -- Enter the new node handle_cart_enter(self, pos, next_dir) + try_detach_minecart(self) + -- Handle end of track if next_dir == staticdata.dir * -1 and next_dir.y == 0 then if DEBUG then print("Stopping cart at end of track at "..tostring(pos)) end @@ -412,6 +433,40 @@ local function do_movement( self, dtime ) end end +local function do_detached_movement(self, dtime) + local staticdata = self._staticdata + + -- Apply physics + if mcl_physics then + mcl_physics.apply_entity_environmental_physics(self) + else + -- Simple physics + local friction = self.object:get_velocity() + friction.y = 0 + + local accel = vector.new(0,-9.81,0) -- gravity + accel = vector.add(accel, vector.multiply(friction,-0.9)) + self.object:set_acceleration(accel) + end + + -- Try to reconnect to rail + local pos_r = vector.round(self.object:get_pos()) + local node = minetest.get_node(pos_r) + if minetest.get_item_group(node.name, "rail") ~= 0 then + print("Reconnected railcart at "..tostring(pos_r)) + staticdata.connected_at = pos_r + staticdata.railtype = node.name + + local freebody_velocity = self.object:get_velocity() + staticdata.dir = vector.normalize(freebody_velocity) + staticdata.velocity = vector.length(freebody_velocity) + + -- Clear freebody movement + self.object:set_velocity(vector.new(0,0,0)) + self.object:set_acceleration(vector.new(0,0,0)) + end +end + local function detach_driver(self) if not self._driver then return @@ -756,6 +811,7 @@ local function register_entity(entity_id, def) end -- Fix railtype field + local pos = self.object:get_pos() if staticdata.connected_at and not staticdata.railtype then local node = minetest.get_node(vector.floor(pos)).name staticdata.railtype = minetest.get_item_group(node, "connect_to_raillike") @@ -769,8 +825,6 @@ local function register_entity(entity_id, def) staticdata.hopper_delay = staticdata.hopper_delay - dtime end - local pos, rou_pos, node = self.object:get_pos() - -- Controls local ctrl, player = nil, nil if self._driver then @@ -794,7 +848,11 @@ local function register_entity(entity_id, def) end end - do_movement(self, dtime) + if staticdata.connected_at then + do_movement(self, dtime) + else + do_detached_movement(self, dtime) + end -- TODO: move this into mcl_core:cactus _mcl_minecarts_on_enter_side -- Drop minecart if it collides with a cactus node @@ -850,7 +908,6 @@ function mcl_minecarts.place_minecart(itemstack, pointed_thing, placer) -- Call placer if le._mcl_minecarts_on_place then - --print("Calling on_place") le._mcl_minecarts_on_place(le, placer) end diff --git a/mods/ENTITIES/mcl_minecarts/mod.conf b/mods/ENTITIES/mcl_minecarts/mod.conf index 73148ebc3..994abe809 100644 --- a/mods/ENTITIES/mcl_minecarts/mod.conf +++ b/mods/ENTITIES/mcl_minecarts/mod.conf @@ -2,4 +2,4 @@ name = mcl_minecarts author = Krock description = Minecarts are vehicles to move players quickly on rails. depends = mcl_title, mcl_explosions, mcl_core, mcl_util, mcl_sounds, mcl_player, mcl_achievements, mcl_chests, mcl_furnaces, mesecons_commandblock, mcl_hoppers, mcl_tnt, mesecons, mcl_entity_invs -optional_depends = doc_identifier, mcl_wip +optional_depends = doc_identifier, mcl_wip, mcl_physics diff --git a/mods/ENTITIES/mcl_minecarts/rails.lua b/mods/ENTITIES/mcl_minecarts/rails.lua index 367d91970..a1872ab4a 100644 --- a/mods/ENTITIES/mcl_minecarts/rails.lua +++ b/mods/ENTITIES/mcl_minecarts/rails.lua @@ -372,7 +372,6 @@ local function register_rail_sloped(itemstring, def) -- Make registrations minetest.register_node(itemstring, ndef) - if craft then minetest.register_craft(craft) end end mod.register_rail_sloped = register_rail_sloped @@ -595,13 +594,12 @@ if minetest.get_modpath("doc") then doc.add_entry_alias("nodes", "mcl_minecarts:golden_rail", "nodes", "mcl_minecarts:golden_rail_on") end -if 0==0 then local CURVY_RAILS_MAP = { ["mcl_minecarts:rail"] = "mcl_minecarts:rail_v2", } minetest.register_lbm({ name = "mcl_minecarts:update_legacy_curvy_rails", - nodenames = {"mcl_minecarts:rail"}, + nodenames = mcl_util.table_keys(CURVY_RAILS_MAP), action = function(pos, node) node.name = CURVY_RAILS_MAP[node.name] if node.name then @@ -620,7 +618,7 @@ local STRAIGHT_RAILS_MAP ={ } minetest.register_lbm({ name = "mcl_minecarts:update_legacy_straight_rails", - nodenames = {"mcl_minecarts:golden_rail"}, + nodenames = mcl_util.table_keys(STRAIGHT_RAILS_MAP), action = function(pos, node) node.name = STRAIGHT_RAILS_MAP[node.name] if node.name then @@ -641,4 +639,3 @@ minetest.register_lbm({ end end }) -end