diff --git a/mods/ENTITIES/mcl_minecarts/carts.lua b/mods/ENTITIES/mcl_minecarts/carts.lua index 11d802e1f..63dde3a53 100644 --- a/mods/ENTITIES/mcl_minecarts/carts.lua +++ b/mods/ENTITIES/mcl_minecarts/carts.lua @@ -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 }) diff --git a/mods/MAPGEN/tsm_railcorridors/gameconfig.lua b/mods/MAPGEN/tsm_railcorridors/gameconfig.lua index 2e80c60c8..f1b678d3b 100644 --- a/mods/MAPGEN/tsm_railcorridors/gameconfig.lua +++ b/mods/MAPGEN/tsm_railcorridors/gameconfig.lua @@ -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 @@ -36,7 +57,11 @@ else end end -tsm_railcorridors.carts = { "mcl_minecarts:chest_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, +} -- This is called after a spawner has been placed by the game. -- Use this to properly set up the metadata and stuff. @@ -46,17 +71,32 @@ function tsm_railcorridors.on_construct_spawner(pos) mcl_mobspawners.setup_spawner(pos, "mobs_mc:cave_spider", 0, 7) end - -- This is called after a cart has been placed by the game. -- Use this to properly set up entity metadata and stuff. +-- * entity_id - type of cart to create -- * pos: Position of cart --- * cart: Cart entity -function tsm_railcorridors.on_construct_cart(_, cart, pr_carts) - local l = cart:get_luaentity() - local inv = mcl_entity_invs.load_inv(l,27) - local items = tsm_railcorridors.get_treasures(pr_carts) - mcl_loot.fill_inventory(inv, "main", items, pr_carts) - mcl_entity_invs.save_inv(l) +-- * pr: pseudorandom +function tsm_railcorridors.create_cart_staticdata(entity_id, pos, pr) + local uuid = create_minecart(entity_id, pos, vector.new(1,0,0)) + + -- 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) + + -- TODO: determine if we should convert to use mcl_loot + -- mcl_loot.fill_inventory(inv, "main", items, pr_carts) + -- 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 @@ -104,11 +144,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. diff --git a/mods/MAPGEN/tsm_railcorridors/init.lua b/mods/MAPGEN/tsm_railcorridors/init.lua index 66b4d779b..1ff80fb4d 100644 --- a/mods/MAPGEN/tsm_railcorridors/init.lua +++ b/mods/MAPGEN/tsm_railcorridors/init.lua @@ -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") @@ -169,6 +171,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 @@ -392,27 +398,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. -local function RecheckCartHack(params) - local pos = params[1] - local cart_id = params[2] - -- Find cart - for _, obj in ipairs(minetest.get_objects_inside_radius(pos, 1)) do - if obj ~= nil 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, pr_carts) - 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. @@ -935,13 +920,13 @@ local function spawn_carts() -- See 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 (supposedly in 5.9.0?) - minetest.after(3, RecheckCartHack, {cpos, cart_id}) + -- Try to create cart staticdata + local hook = tsm_railcorridors.create_cart_staticdata + if hook then cart_staticdata = hook(cart_id, cpos, pr) end + + minetest.add_entity(cpos, cart_id, cart_staticdata) end end carts_table = {} @@ -950,7 +935,7 @@ end -- Start generation of a rail corridor system -- main_cave_coords is the center of the floor of the dirt room, from which -- all corridors expand. -local function create_corridor_system(main_cave_coords) +local function create_corridor_system(main_cave_coords, pr) -- Dirt room size local maxsize = 6 @@ -1105,13 +1090,22 @@ mcl_structures.register_structure("mineshaft",{ y_min = mcl_vars.mg_overworld_min, place_func = function(pos,_,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) - create_corridor_system(p) + + 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,