Attempt to fix chest minecarts, at least for 5.9 (#4684)

Not using the `RecheckCartHack` on >5.9 seems to help with #4670 - not tested on older minetest; chest minecarts might still be empty there when the block is unloaded in the meantime. For <5.9, maybe it helps to decrease the time interval, 3 seconds seems to fairly long.

This also makes the minecarts random: 40% minecart, 40% chest minecart, 20% tnt minecart.

Reviewed-on: https://git.minetest.land/VoxeLibre/VoxeLibre/pulls/4684
Reviewed-by: the-real-herowl <the-real-herowl@noreply.git.minetest.land>
Co-authored-by: kno10 <erich.schubert@gmail.com>
Co-committed-by: kno10 <erich.schubert@gmail.com>
This commit is contained in:
kno10 2024-11-10 11:41:19 +01:00 committed by the-real-herowl
parent c428fa576b
commit f6f5481f30
2 changed files with 20 additions and 7 deletions

View file

@ -36,7 +36,11 @@ else
end end
end end
tsm_railcorridors.carts = { "mcl_minecarts:chest_minecart" } tsm_railcorridors.carts = {
"mcl_minecarts:minecart", "mcl_minecarts:minecart",
"mcl_minecarts:chest_minecart", "mcl_minecarts:chest_minecart",
"mcl_minecarts:tnt_minecart"
}
-- This is called after a spawner has been placed by the game. -- This is called after a spawner has been placed by the game.
-- Use this to properly set up the metadata and stuff. -- Use this to properly set up the metadata and stuff.
@ -54,9 +58,11 @@ end
function tsm_railcorridors.on_construct_cart(_, cart, pr_carts) function tsm_railcorridors.on_construct_cart(_, cart, pr_carts)
local l = cart:get_luaentity() local l = cart:get_luaentity()
local inv = mcl_entity_invs.load_inv(l,27) local inv = mcl_entity_invs.load_inv(l,27)
local items = tsm_railcorridors.get_treasures(pr_carts) if inv then -- otherwise probably not a chest minecart
mcl_loot.fill_inventory(inv, "main", items, pr_carts) local items = tsm_railcorridors.get_treasures(pr_carts)
mcl_entity_invs.save_inv(l) mcl_loot.fill_inventory(inv, "main", items, pr_carts)
mcl_entity_invs.save_inv(l)
end
end end
-- Fallback function. Returns a random treasure. This function is called for chests -- Fallback function. Returns a random treasure. This function is called for chests

View file

@ -397,7 +397,9 @@ end
-- This is a workaround thanks to the fact that minetest.add_entity is unreliable as fuck -- This is a workaround thanks to the fact that minetest.add_entity is unreliable as fuck
-- See: https://github.com/minetest/minetest/issues/4759 -- See: https://github.com/minetest/minetest/issues/4759
-- FIXME: Kill this horrible hack with fire as soon you can. -- FIXME: Kill this horrible hack with fire as soon you can.
local function RecheckCartHack(params) local RecheckCartHack = nil
if not minetest.features.random_state_restore then -- proxy for minetest > 5.9.0, this feature will not be removed
RecheckCartHack = function(params)
local pos = params[1] local pos = params[1]
local cart_id = params[2] local cart_id = params[2]
-- Find cart -- Find cart
@ -412,6 +414,7 @@ local function RecheckCartHack(params)
end end
minetest.log("info", "[tsm_railcorridors] Cart spawn FAILED: "..minetest.pos_to_string(pos)) minetest.log("info", "[tsm_railcorridors] Cart spawn FAILED: "..minetest.pos_to_string(pos))
end end
end
-- Try to place a cobweb. -- Try to place a cobweb.
-- pos: Position of cobweb -- pos: Position of cobweb
@ -935,13 +938,17 @@ local function spawn_carts()
-- See <https://github.com/minetest/minetest/issues/4759> -- See <https://github.com/minetest/minetest/issues/4759>
local cart_id = tsm_railcorridors.carts[cart_type] local cart_id = tsm_railcorridors.carts[cart_type]
minetest.log("info", "[tsm_railcorridors] Cart spawn attempt: "..minetest.pos_to_string(cpos)) minetest.log("info", "[tsm_railcorridors] Cart spawn attempt: "..minetest.pos_to_string(cpos))
minetest.add_entity(cpos, cart_id) local obj = minetest.add_entity(cpos, cart_id)
-- This checks if the cart is actually spawned, it's a giant hack! -- This checks if the cart is actually spawned, it's a giant hack!
-- Note that the callback function is also called there. -- Note that the callback function is also called there.
-- TODO: Move callback function to this position when the -- TODO: Move callback function to this position when the
-- minetest.add_entity bug has been fixed (supposedly in 5.9.0?) -- minetest.add_entity bug has been fixed (supposedly in 5.9.0?)
minetest.after(3, RecheckCartHack, {cpos, cart_id}) if RecheckCartHack then
minetest.after(3, RecheckCartHack, {cpos, cart_id})
else
tsm_railcorridors.on_construct_cart(cpos, obj, pr_carts)
end
end end
end end
carts_table = {} carts_table = {}