Update mineshafts for new rail and minecarts, add loot to generated chest and hopper minecarts (and remove notes about a hack)

This commit is contained in:
teknomunk 2024-04-12 09:11:46 +00:00
parent 80a45c2c0d
commit 59f64ca947
4 changed files with 86 additions and 61 deletions

View file

@ -309,6 +309,20 @@ function DEFAULT_CART_DEF:on_death(killer)
kill_cart(self._staticdata)
end
-- Create a minecart
function mod.create_minecart(entity_id, pos, dir)
-- Setup cart data
local uuid = mcl_util.gen_uuid()
data = make_staticdata( nil, pos, dir )
data.uuid = uuid
data.cart_type = entity_id
update_cart_data(data)
save_cart_data(uuid)
return uuid
end
local create_minecart = mod.create_minecart
-- Place a minecart at pointed_thing
function mod.place_minecart(itemstack, pointed_thing, placer)
if not pointed_thing.type == "node" then
@ -332,13 +346,7 @@ function mod.place_minecart(itemstack, pointed_thing, placer)
local entity_id = entity_mapping[itemstack:get_name()]
-- Setup cart data
local uuid = mcl_util.gen_uuid()
data = make_staticdata( nil, railpos, cart_dir )
data.uuid = uuid
data.cart_type = entity_id
update_cart_data(data)
save_cart_data(uuid)
local uuid = create_minecart(entity_id, railpos, cart_dir)
-- Create the entity with the staticdata already setup
local sd = minetest.serialize({ uuid=uuid, seq=1 })

View file

@ -3,17 +3,38 @@
-- Adapted for MineClone 2!
-- Imports
local create_minecart = mcl_minecarts.create_minecart
local get_cart_data = mcl_minecarts.get_cart_data
local save_cart_data = mcl_minecarts.save_cart_data
-- Node names (Don't use aliases!)
tsm_railcorridors.nodes = {
dirt = "mcl_core:dirt",
chest = "mcl_chests:chest",
rail = "mcl_minecarts:rail",
rail = "mcl_minecarts:rail_v2",
torch_floor = "mcl_torches:torch",
torch_wall = "mcl_torches:torch_wall",
cobweb = "mcl_core:cobweb",
spawner = "mcl_mobspawners:spawner",
}
local update_rail_connections = mcl_minecarts.update_rail_connections
local rails_to_update = {}
tsm_railcorridors.on_place_node = {
[tsm_railcorridors.nodes.rail] = function(pos, node)
rails_to_update[#rails_to_update + 1] = pos
end,
}
tsm_railcorridors.on_start = function()
rails_to_update = {}
end
tsm_railcorridors.on_finish = function()
for _,pos in pairs(rails_to_update) do
update_rail_connections(pos, {legacy = true, ignore_neighbor_connections = true})
end
end
local mg_name = minetest.get_mapgen_setting("mg_name")
if mg_name == "v6" then
@ -37,19 +58,31 @@ else
end
-- TODO: Use minecart with chest instead of normal minecart
tsm_railcorridors.carts = { "mcl_minecarts:minecart" }
tsm_railcorridors.carts = { "mcl_minecarts:chest_minecart", "mcl_minecarts:hopper_minecart", "mcl_minecarts:minecart" }
local has_loot = {
["mcl_minecarts:chest_minecart"] = true,
["mcl_minecarts:hopper_minceart"] = true,
}
function tsm_railcorridors.on_construct_cart(pos, cart)
-- TODO: Fill cart with treasures
function tsm_railcorridors.create_cart_staticdata(entity_id,pos, pr)
local uuid = create_minecart(entity_id, pos, vector.new(1,0,0))
-- This is it? There's this giant hack announced in
-- the other file and I grep for the function and it's
-- a stub? :)
-- Fill the cart with loot
local cartdata = get_cart_data(uuid)
if cartdata and has_loot[entity_id] then
local items = tsm_railcorridors.get_treasures(pr)
-- The path here using some minetest.after hackery was
-- deactivated in init.lua - reactivate when this does
-- something the function is called RecheckCartHack.
-- Convert from ItemStack to itemstrings
for k,item in pairs(items) do
items[k] = item:to_string()
end
cartdata.inventory = items
print("cartdata = "..dump(cartdata))
save_cart_data(uuid)
end
return minetest.serialize({ uuid=uuid, seq=1 })
end
-- Fallback function. Returns a random treasure. This function is called for chests
@ -102,11 +135,11 @@ function tsm_railcorridors.get_treasures(pr)
stacks_min = 3,
stacks_max = 3,
items = {
{ itemstring = "mcl_minecarts:rail", weight = 20, amount_min = 4, amount_max = 8 },
{ itemstring = "mcl_minecarts:rail_v2", weight = 20, amount_min = 4, amount_max = 8 },
{ itemstring = "mcl_torches:torch", weight = 15, amount_min = 1, amount_max = 16 },
{ itemstring = "mcl_minecarts:activator_rail", weight = 5, amount_min = 1, amount_max = 4 },
{ itemstring = "mcl_minecarts:detector_rail", weight = 5, amount_min = 1, amount_max = 4 },
{ itemstring = "mcl_minecarts:golden_rail", weight = 5, amount_min = 1, amount_max = 4 },
{ itemstring = "mcl_minecarts:activator_rail_v2", weight = 5, amount_min = 1, amount_max = 4 },
{ itemstring = "mcl_minecarts:detector_rail_v2", weight = 5, amount_min = 1, amount_max = 4 },
{ itemstring = "mcl_minecarts:golden_rail_v2", weight = 5, amount_min = 1, amount_max = 4 },
}
},
-- non-MC loot: 50% chance to add a minecart, offered as alternative to spawning minecarts on rails.

View file

@ -1,7 +1,9 @@
local pairs = pairs
local tonumber = tonumber
tsm_railcorridors = {}
tsm_railcorridors = {
after = {},
}
-- Load node names
dofile(minetest.get_modpath(minetest.get_current_modname()).."/gameconfig.lua")
@ -170,6 +172,10 @@ local function SetNodeIfCanBuild(pos, node, check_above, can_replace_rail)
(can_replace_rail and name == tsm_railcorridors.nodes.rail)
) then
minetest.set_node(pos, node)
local after = tsm_railcorridors.on_place_node[node.name]
if after then
after(pos, node)
end
return true
else
return false
@ -394,34 +400,6 @@ local function PlaceChest(pos, param2)
end
end
-- This function checks if a cart has ACTUALLY been spawned.
-- To be calld by minetest.after.
-- This is a workaround thanks to the fact that minetest.add_entity is unreliable as fuck
-- See: https://github.com/minetest/minetest/issues/4759
-- FIXME: Kill this horrible hack with fire as soon you can.
-- Why did anyone activate it in the first place? It doesn't
-- have a function seeing as there are no chest minecarts yet.
--[[
local function RecheckCartHack(params)
local pos = params[1]
local cart_id = params[2]
-- Find cart
for _, obj in pairs(minetest.get_objects_inside_radius(pos, 1)) do
if obj and obj:get_luaentity().name == cart_id then
-- Cart found! We can now safely call the callback func.
-- (calling it earlier has the danger of failing)
minetest.log("info", "[tsm_railcorridors] Cart spawn succeeded: "..minetest.pos_to_string(pos))
tsm_railcorridors.on_construct_cart(pos, obj)
return
end
end
minetest.log("info", "[tsm_railcorridors] Cart spawn FAILED: "..minetest.pos_to_string(pos))
end
--]]
-- Try to place a cobweb.
-- pos: Position of cobweb
-- needs_check: If true, checks if any of the nodes above, below or to the side of the cobweb.
@ -944,16 +922,13 @@ local function spawn_carts()
-- See <https://github.com/minetest/minetest/issues/4759>
local cart_id = tsm_railcorridors.carts[cart_type]
minetest.log("info", "[tsm_railcorridors] Cart spawn attempt: "..minetest.pos_to_string(cpos))
minetest.add_entity(cpos, cart_id)
local cart_staticdata = nil
-- This checks if the cart is actually spawned, it's a giant hack!
-- Note that the callback function is also called there.
-- TODO: Move callback function to this position when the
-- minetest.add_entity bug has been fixed.
-- Try to create cart staticdata
local hook = tsm_railcorridors.create_cart_staticdata
if hook then cart_staticdata = hook(cart_id, cpos, pr) end
-- minetest.after(3, RecheckCartHack, {cpos, cart_id})
-- This whole recheck logic leads to a stub right now
-- it can be reenabled when chest carts are a thing.
minetest.add_entity(cpos, cart_id, cart_staticdata)
end
end
carts_table = {}
@ -1117,13 +1092,22 @@ mcl_structures.register_structure("mineshaft",{
y_min = mcl_vars.mg_overworld_min,
place_func = function(pos,def,pr,blockseed)
local r = pr:next(-50,-10)
r = -10
local p = vector.offset(pos,0,r,0)
if p.y < mcl_vars.mg_overworld_min + 5 then
p.y = mcl_vars.mg_overworld_min + 5
end
if p.y > -10 then return true end
--if p.y > -10 then return true end
InitRandomizer(blockseed)
local hook = tsm_railcorridors.on_start
if hook then hook() end
create_corridor_system(p, pr)
local hook = tsm_railcorridors.on_finish
if hook then hook() end
return true
end,

View file

@ -25,7 +25,7 @@ tsm_railcorridors_probability_chest (Chest probability) float 0.05 0.0 1.0
#of finding a cart in rail corridors with high rail damage will be lower.
#NOTE: Due to a bug in Minetest <https://github.com/minetest/minetest/issues/4759>
#carts often fail to spawn even if they should.
tsm_railcorridors_probability_cart (Cart probability) float 0.0 0.0 1.0
tsm_railcorridors_probability_cart (Cart probability) float 0.05 0.0 1.0
#If enabled, cobwebs may be placed in some corridors.
#Currently, cobwebs are only supported with the Mobs Redo mod.