Fix several undefined global warnings, fix cart movement when over maximum speed, fix cart reattachment to sloped track

This commit is contained in:
teknomunk 2024-04-27 18:12:06 +00:00
parent 02613d3bc8
commit 22f1a63391
3 changed files with 35 additions and 28 deletions

View File

@ -1235,7 +1235,7 @@ function mcl_util.assign_uuid(obj)
end end
-- Update the cache with this new id -- Update the cache with this new id
aoid = mcl_util.get_active_object_id(obj) local aoid = mcl_util.get_active_object_id(obj)
uuid_to_aoid_cache[le._uuid] = aoid uuid_to_aoid_cache[le._uuid] = aoid
return le._uuid return le._uuid

View File

@ -352,7 +352,7 @@ end
function mod.create_minecart(entity_id, pos, dir) function mod.create_minecart(entity_id, pos, dir)
-- Setup cart data -- Setup cart data
local uuid = mcl_util.gen_uuid() local uuid = mcl_util.gen_uuid()
data = make_staticdata( nil, pos, dir ) local data = make_staticdata( nil, pos, dir )
data.uuid = uuid data.uuid = uuid
data.cart_type = entity_id data.cart_type = entity_id
update_cart_data(data) update_cart_data(data)
@ -390,28 +390,21 @@ function mod.place_minecart(itemstack, pointed_thing, placer)
-- Create the entity with the staticdata already setup -- Create the entity with the staticdata already setup
local sd = minetest.serialize({ uuid=uuid, seq=1 }) local sd = minetest.serialize({ uuid=uuid, seq=1 })
local cart = minetest.add_entity(spawn_pos, entity_id, sd) local cart = minetest.add_entity(spawn_pos, entity_id, sd)
local staticdata = get_cart_data(uuid)
cart:set_yaw(minetest.dir_to_yaw(cart_dir)) cart:set_yaw(minetest.dir_to_yaw(cart_dir))
-- Update static data
local le = cart:get_luaentity()
if le then
le._staticdata = data
end
-- Call placer -- Call placer
local le = cart:get_luaentity()
if le._mcl_minecarts_on_place then if le._mcl_minecarts_on_place then
le._mcl_minecarts_on_place(le, placer) le._mcl_minecarts_on_place(le, placer)
end end
if railpos then if railpos then
handle_cart_enter(data, railpos) handle_cart_enter(staticdata, railpos)
end end
local pname = "" local pname = placer and placer:get_player_name() or ""
if placer then
pname = placer:get_player_name()
end
if not minetest.is_creative_enabled(pname) then if not minetest.is_creative_enabled(pname) then
itemstack:take_item() itemstack:take_item()
end end

View File

@ -9,6 +9,7 @@ local mcl_debug,DEBUG = mcl_util.make_mcl_logger("mcl_logging_minecart_debug", "
--mcl_debug = function(msg) print(msg) end --mcl_debug = function(msg) print(msg) end
-- Imports -- Imports
local mcl_physics = mcl_physics or false
local FRICTION = mcl_minecarts.FRICTION local FRICTION = mcl_minecarts.FRICTION
local MAX_TRAIN_LENGTH = mod.MAX_TRAIN_LENGTH local MAX_TRAIN_LENGTH = mod.MAX_TRAIN_LENGTH
local SPEED_MAX = mod.SPEED_MAX local SPEED_MAX = mod.SPEED_MAX
@ -315,7 +316,6 @@ local function do_movement_step(staticdata, dtime)
",v_0="..tostring(v_0).. ",v_0="..tostring(v_0)..
",x_0="..tostring(x_0).. ",x_0="..tostring(x_0)..
",dtime="..tostring(dtime).. ",dtime="..tostring(dtime)..
",timestep="..tostring(timestep)..
",dir="..tostring(staticdata.dir).. ",dir="..tostring(staticdata.dir)..
",connected_at="..tostring(staticdata.connected_at).. ",connected_at="..tostring(staticdata.connected_at)..
",distance="..tostring(staticdata.distance) ",distance="..tostring(staticdata.distance)
@ -347,7 +347,7 @@ local function do_movement_step(staticdata, dtime)
-- Truncate timestep to prevent v_1 from being larger that speed_max -- Truncate timestep to prevent v_1 from being larger that speed_max
local v_max = SPEED_MAX local v_max = SPEED_MAX
if (v_0 ~= v_max) and ( v_0 + a * timestep > v_max) then if (v_0 < v_max) and ( v_0 + a * timestep > v_max) then
timestep = ( v_max - v_0 ) / a timestep = ( v_max - v_0 ) / a
end end
@ -519,7 +519,18 @@ local function do_detached_movement(self, dtime)
end end
-- Try to reconnect to rail -- Try to reconnect to rail
local pos_r = vector.round(self.object:get_pos()) local pos = self.object:get_pos()
local yaw = self.object:get_yaw()
local yaw_dir = minetest.yaw_to_dir(yaw)
local test_positions = {
pos,
vector.offset(vector.add(pos, vector.multiply(yaw_dir, 0.5)),0,-0.3,0),
vector.offset(vector.add(pos, vector.multiply(yaw_dir,-0.5)),0,-0.3,0),
}
for i=1,#test_positions do
test_pos = test_positions[i]
local pos_r = vector.round(test_pos)
local node = minetest.get_node(pos_r) local node = minetest.get_node(pos_r)
if minetest.get_item_group(node.name, "rail") ~= 0 then if minetest.get_item_group(node.name, "rail") ~= 0 then
staticdata.connected_at = pos_r staticdata.connected_at = pos_r
@ -531,10 +542,13 @@ local function do_detached_movement(self, dtime)
-- Use vector projection to only keep the velocity in the new direction of movement on the rail -- Use vector projection to only keep the velocity in the new direction of movement on the rail
-- https://en.wikipedia.org/wiki/Vector_projection -- https://en.wikipedia.org/wiki/Vector_projection
staticdata.velocity = vector.dot(staticdata.dir,freebody_velocity) staticdata.velocity = vector.dot(staticdata.dir,freebody_velocity)
print("Reattached velocity="..tostring(staticdata.velocity)..", freebody_velocity="..tostring(freebody_velocity))
-- Clear freebody movement -- Clear freebody movement
self.object:set_velocity(vector.new(0,0,0)) self.object:set_velocity(vector.new(0,0,0))
self.object:set_acceleration(vector.new(0,0,0)) self.object:set_acceleration(vector.new(0,0,0))
return
end
end end
end end