Change staticdata serialization (with migration from old data), disable debugging code used to investigate https://git.minetest.land/MineClone2/MineClone2/issues/2446

This commit is contained in:
teknomunk 2024-02-13 10:42:38 +00:00
parent 44514e0fad
commit 2a9aaa02af

View file

@ -207,15 +207,22 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
_old_pos = nil, _old_pos = nil,
_old_vel = {x=0, y=0, z=0}, _old_vel = {x=0, y=0, z=0},
_old_switch = 0, _old_switch = 0,
_railtype = nil, _staticdata = {
railtype = nil,
},
} }
function cart:on_activate(staticdata, dtime_s) function cart:on_activate(staticdata, dtime_s)
-- Initialize -- Initialize
local data = minetest.deserialize(staticdata) local data = minetest.deserialize(staticdata)
if type(data) == "table" then if type(data) == "table" then
self._railtype = data._railtype -- Migrate old data
self._passenger = data._passenger if data._railtype then
data.railtype = data._railtype
data._railtype = nil
end
self._staticdata = data
end end
self.object:set_armor_groups({immortal=1}) self.object:set_armor_groups({immortal=1})
@ -231,13 +238,13 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
function cart:on_punch(puncher, time_from_last_punch, tool_capabilities, direction) function cart:on_punch(puncher, time_from_last_punch, tool_capabilities, direction)
local pos = self.object:get_pos() local pos = self.object:get_pos()
if not self._railtype then if not self._staticdata.railtype then
local node = minetest.get_node(vector.floor(pos)).name local node = minetest.get_node(vector.floor(pos)).name
self._railtype = minetest.get_item_group(node, "connect_to_raillike") self._staticdata.railtype = minetest.get_item_group(node, "connect_to_raillike")
end end
if not puncher or not puncher:is_player() then if not puncher or not puncher:is_player() then
local cart_dir = mcl_minecarts:get_rail_direction(pos, {x=1, y=0, z=0}, nil, nil, self._railtype) local cart_dir = mcl_minecarts:get_rail_direction(pos, {x=1, y=0, z=0}, nil, nil, self._staticdata.railtype)
if vector.equals(cart_dir, {x=0, y=0, z=0}) then if vector.equals(cart_dir, {x=0, y=0, z=0}) then
return return
end end
@ -290,7 +297,7 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
local punch_dir = mcl_minecarts:velocity_to_dir(puncher:get_look_dir()) local punch_dir = mcl_minecarts:velocity_to_dir(puncher:get_look_dir())
punch_dir.y = 0 punch_dir.y = 0
local cart_dir = mcl_minecarts:get_rail_direction(pos, punch_dir, nil, nil, self._railtype) local cart_dir = mcl_minecarts:get_rail_direction(pos, punch_dir, nil, nil, self._staticdata.railtype)
if vector.equals(cart_dir, {x=0, y=0, z=0}) then if vector.equals(cart_dir, {x=0, y=0, z=0}) then
return return
end end
@ -364,7 +371,7 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
for _,v in ipairs({"x","y","z"}) do for _,v in ipairs({"x","y","z"}) do
if math.abs(diff[v]) > 1.1 then if math.abs(diff[v]) > 1.1 then
local expected_pos = vector.add(self._old_pos, self._old_dir) local expected_pos = vector.add(self._old_pos, self._old_dir)
dir, last_switch = mcl_minecarts:get_rail_direction(pos, self._old_dir, ctrl, self._old_switch, self._railtype) dir, last_switch = mcl_minecarts:get_rail_direction(pos, self._old_dir, ctrl, self._old_switch, self._staticdata.railtype)
if vector.equals(dir, {x=0, y=0, z=0}) then if vector.equals(dir, {x=0, y=0, z=0}) then
dir = false dir = false
@ -388,7 +395,7 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
local cart_dir = mcl_minecarts:velocity_to_dir(vel) local cart_dir = mcl_minecarts:velocity_to_dir(vel)
local max_vel = mcl_minecarts.speed_max local max_vel = mcl_minecarts.speed_max
if not dir then if not dir then
dir, last_switch = mcl_minecarts:get_rail_direction(pos, cart_dir, ctrl, self._old_switch, self._railtype) dir, last_switch = mcl_minecarts:get_rail_direction(pos, cart_dir, ctrl, self._old_switch, self._staticdata.railtype)
end end
local new_acc = {x=0, y=0, z=0} local new_acc = {x=0, y=0, z=0}
@ -533,15 +540,18 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
end end
-- Debug -- Debug
local node = minetest.get_node(pos).name if false then
local dist = 0 local node = minetest.get_node(pos).name
if pos and self._old_pos then local dist = 0
dist = vector.distance(pos,self._old_pos) if pos and self._old_pos then
dist = vector.distance(pos,self._old_pos)
end
if dist > 1.5 then
print("pos="..tostring(pos)..",dist="..tostring(dist)..",node="..tostring(node)..",old_pos="..
tostring(self._old_pos)..",vel="..tostring(vel))
end
-- Rail jumps can occur when dist > 1.5, because the cart can skip over a gap in track
end end
if dist > 1.5 then
print("pos="..tostring(pos)..",dist="..tostring(dist)..",node="..tostring(node)..",old_pos="..tostring(self._old_pos)..",vel="..tostring(vel))
end
-- Rail jumps can occur when dist > 1.5, because the cart can skip over a gap in track
-- Grab mob -- Grab mob
if math.random(1,20) > 15 and not self._passenger then if math.random(1,20) > 15 and not self._passenger then
@ -577,7 +587,7 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
rou_pos = vector.round(pos) rou_pos = vector.round(pos)
node = minetest.get_node(rou_pos) node = minetest.get_node(rou_pos)
local g = minetest.get_item_group(node.name, "connect_to_raillike") local g = minetest.get_item_group(node.name, "connect_to_raillike")
if g ~= self._railtype and self._railtype then if g ~= self._staticdata.railtype and self._staticdata.railtype then
-- Detach driver -- Detach driver
if player then if player then
if self._old_pos then if self._old_pos then
@ -668,7 +678,7 @@ local function register_entity(entity_id, mesh, textures, drop, on_rightclick, o
end end
function cart:get_staticdata() function cart:get_staticdata()
return minetest.serialize({_railtype = self._railtype}) return minetest.serialize(self._staticdata)
end end
minetest.register_entity(entity_id, cart) minetest.register_entity(entity_id, cart)
@ -703,7 +713,7 @@ function mcl_minecarts.place_minecart(itemstack, pointed_thing, placer)
local railtype = minetest.get_item_group(node.name, "connect_to_raillike") local railtype = minetest.get_item_group(node.name, "connect_to_raillike")
local le = cart:get_luaentity() local le = cart:get_luaentity()
if le then if le then
le._railtype = railtype le._staticdata.railtype = railtype
end end
local cart_dir local cart_dir
if node.name == "mcl_minecarts:golden_rail_on" then if node.name == "mcl_minecarts:golden_rail_on" then