Remove memory leak for cart data, check distance to players before respawning distant carts to prevent adding entities that are immediately inactivated

This commit is contained in:
teknomunk 2024-04-09 22:32:58 +00:00
parent 15c13b7bb3
commit 778e42165a

View File

@ -119,20 +119,15 @@ function DEFAULT_CART_DEF:on_activate(staticdata, dtime_s)
data = cd data = cd
end end
-- Initialize -- Fix up types
if type(data) == "table" then data.dir = vector.new(data.dir)
-- Fix up types
data.dir = vector.new(data.dir)
-- Fix mass -- Fix mass
data.mass = data.mass or 1 data.mass = data.mass or 1
-- Make sure all carts have an ID to isolate them -- Make sure all carts have an ID to isolate them
self._uuid = data.uuid self._uuid = data.uuid
data.uuid = mcl_util.get_uuid(self.object) self._staticdata = data
self._staticdata = data
end
-- Activate cart if on powered activator rail -- Activate cart if on powered activator rail
if self.on_activate_by_rail then if self.on_activate_by_rail then
@ -311,9 +306,6 @@ function mcl_minecarts.place_minecart(itemstack, pointed_thing, placer)
end end
local entity_id = entity_mapping[itemstack:get_name()] local entity_id = entity_mapping[itemstack:get_name()]
local cart = minetest.add_entity(spawn_pos, entity_id)
cart:set_yaw(minetest.dir_to_yaw(cart_dir))
-- Setup cart data -- Setup cart data
local uuid = mcl_util.get_uuid(cart) local uuid = mcl_util.get_uuid(cart)
@ -323,6 +315,12 @@ function mcl_minecarts.place_minecart(itemstack, pointed_thing, placer)
update_cart_data(data) update_cart_data(data)
save_cart_data(uuid) save_cart_data(uuid)
-- Create the entity with the staticdata already setup
local sd = minetest.serialize({ uuid=uuid, seq=1 })
local cart = minetest.add_entity(spawn_pos, entity_id, sd)
cart:set_yaw(minetest.dir_to_yaw(cart_dir))
-- Update static data -- Update static data
local le = cart:get_luaentity() local le = cart:get_luaentity()
if le then if le then
@ -465,14 +463,24 @@ end
local function respawn_cart(cart) local function respawn_cart(cart)
local cart_type = cart.cart_type or "mcl_minecarts:minecart" local cart_type = cart.cart_type or "mcl_minecarts:minecart"
local pos = mod.get_cart_position(cart) local pos = mod.get_cart_position(cart)
print("Respawning cart #"..cart.uuid.." at "..tostring(pos))
local players = minetest.get_connected_players()
local distance = nil
for _,player in pairs(players) do
local d = vector.distance(player:get_pos(), pos)
if not distance or d < distance then distance = d end
end
if not distance or distance > 115 then return end
print("Respawning cart #"..cart.uuid.." at "..tostring(pos)..",distance="..distance)
-- Update sequence so that old cart entities get removed -- Update sequence so that old cart entities get removed
cart.seq = (cart.seq or 1) + 1 cart.seq = (cart.seq or 1) + 1
save_cart_data(cart.uuid) save_cart_data(cart.uuid)
-- Create the new entity -- Create the new entity
local entity = minetest.add_entity(pos, cart_type) local sd = minetest.serialize({ uuid=cart.uuid, seq=cart.seq })
local entity = minetest.add_entity(pos, cart_type, sd)
local le = entity:get_luaentity() local le = entity:get_luaentity()
le._seq = cart.seq le._seq = cart.seq
le._uuid = cart.uuid le._uuid = cart.uuid