2017-05-10 14:52:54 -07:00
tsm_railcorridors = { }
-- Load node names
dofile ( minetest.get_modpath ( minetest.get_current_modname ( ) ) .. " /gameconfig.lua " )
-- Settings
2016-11-03 14:42:57 -07:00
local setting
2014-09-28 06:30:54 -07:00
2016-11-14 08:58:10 -08:00
-- Probability function
-- TODO: Check if this is correct
local P = function ( float )
return math.floor ( 32767 * float )
end
2014-09-27 08:30:32 -07:00
-- Wahrscheinlichkeit für jeden Chunk, solche Gänge mit Schienen zu bekommen
2014-09-28 05:39:11 -07:00
-- Probability for every newly generated chunk to get corridors
2017-05-13 07:41:07 -07:00
local probability_railcaves_in_chunk = P ( 0.33333 )
2017-08-09 07:25:07 -07:00
setting = tonumber ( minetest.settings : get ( " tsm_railcorridors_probability_railcaves_in_chunk " ) )
2016-11-03 14:42:57 -07:00
if setting then
2016-11-14 08:58:10 -08:00
probability_railcaves_in_chunk = P ( setting )
2016-11-03 14:42:57 -07:00
end
2014-09-28 06:30:54 -07:00
2014-09-30 14:02:12 -07:00
-- Innerhalb welcher Parameter soll sich die Pfadlänge bewegen? (Forks heben den Maximalwert auf)
-- Minimal and maximal value of path length (forks don't look up this value)
local way_min = 4 ;
local way_max = 7 ;
2017-08-09 07:25:07 -07:00
setting = tonumber ( minetest.settings : get ( " tsm_railcorridors_way_min " ) )
2016-11-03 14:42:57 -07:00
if setting then
way_min = setting
end
2017-08-09 07:25:07 -07:00
setting = tonumber ( minetest.settings : get ( " tsm_railcorridors_way_max " ) )
2016-11-03 14:42:57 -07:00
if setting then
way_max = setting
end
2014-09-30 14:02:12 -07:00
2016-11-03 14:42:57 -07:00
-- Wahrsch. für jeden geraden Teil eines Korridors, Fackeln zu bekommen
-- Probability for every horizontal part of a corridor to be with torches
2016-11-14 08:58:10 -08:00
local probability_torches_in_segment = P ( 0.5 )
2017-08-09 07:25:07 -07:00
setting = tonumber ( minetest.settings : get ( " tsm_railcorridors_probability_torches_in_segment " ) )
2016-11-03 14:42:57 -07:00
if setting then
2016-11-14 08:58:10 -08:00
probability_torches_in_segment = P ( setting )
2016-11-03 14:42:57 -07:00
end
2014-09-27 07:25:50 -07:00
2014-09-28 06:30:54 -07:00
-- Wahrsch. für jeden Teil eines Korridors, nach oben oder nach unten zu gehen
-- Probability for every part of a corridor to go up or down
2016-11-14 08:58:10 -08:00
local probability_up_or_down = P ( 0.2 )
2017-08-09 07:25:07 -07:00
setting = tonumber ( minetest.settings : get ( " tsm_railcorridors_probability_up_or_down " ) )
2016-11-03 14:42:57 -07:00
if setting then
2016-11-14 08:58:10 -08:00
probability_up_or_down = P ( setting )
2016-11-03 14:42:57 -07:00
end
2014-09-30 14:02:12 -07:00
-- Wahrscheinlichkeit für jeden Teil eines Korridors, sich zu verzweigen – vorsicht, wenn fast jeder Gang sich verzweigt, kann der Algorithums unlösbar werden und MT hängt sich auf
-- Probability for every part of a corridor to fork – caution, too high values may cause MT to hang on.
2016-11-14 08:58:10 -08:00
local probability_fork = P ( 0.04 )
2017-08-09 07:25:07 -07:00
setting = tonumber ( minetest.settings : get ( " tsm_railcorridors_probability_fork " ) )
2016-11-03 14:42:57 -07:00
if setting then
2016-11-14 08:58:10 -08:00
probability_fork = P ( setting )
2016-11-03 14:42:57 -07:00
end
2014-09-30 14:02:12 -07:00
-- Wahrscheinlichkeit für jeden geraden Teil eines Korridors eine Kiste zu enthalten
-- Probability for every part of a corridor to contain a chest
2016-11-14 08:58:10 -08:00
local probability_chest = P ( 0.05 )
2017-08-09 07:25:07 -07:00
setting = tonumber ( minetest.settings : get ( " tsm_railcorridors_probability_chest " ) )
2016-11-03 14:42:57 -07:00
if setting then
2016-11-14 08:58:10 -08:00
probability_chest = P ( setting )
2016-11-03 14:42:57 -07:00
end
2014-09-28 06:30:54 -07:00
2017-08-14 06:03:32 -07:00
-- Probability for every part of a corridor to contain a cart
2017-08-14 11:36:22 -07:00
local probability_cart = P ( 0.1 )
2017-08-14 06:03:32 -07:00
setting = tonumber ( minetest.settings : get ( " tsm_railcorridors_probability_cart " ) )
if setting then
probability_cart = P ( setting )
end
2017-05-10 18:13:36 -07:00
-- Probability for a rail corridor system to be damaged
local probability_damage = P ( 0.55 )
2017-08-09 07:25:07 -07:00
setting = tonumber ( minetest.settings : get ( " tsm_railcorridors_probability_damage " ) )
2017-05-10 18:13:36 -07:00
if setting then
probability_damage = P ( setting )
end
2017-08-13 19:21:59 -07:00
-- Enable cobwebs
local place_cobwebs = true
setting = minetest.settings : get_bool ( " tsm_railcorridors_place_cobwebs " )
if setting ~= nil then
place_cobwebs = setting
end
-- Enable mob spawners
local place_mob_spawners = true
setting = minetest.settings : get_bool ( " tsm_railcorridors_place_mob_spawners " )
if setting ~= nil then
place_mob_spawners = setting
end
2017-05-09 15:50:13 -07:00
-- Max. and min. heights between rail corridors are generated
local height_min = - 31000
2017-05-09 16:28:32 -07:00
local height_max = - 30
2017-08-09 07:25:07 -07:00
setting = tonumber ( minetest.settings : get ( " tsm_railcorridors_height_min " ) )
2017-05-09 15:50:13 -07:00
if setting then
height_min = setting
end
2017-08-09 07:25:07 -07:00
setting = tonumber ( minetest.settings : get ( " tsm_railcorridors_height_max " ) )
2017-05-09 15:50:13 -07:00
if setting then
height_max = setting
end
2017-05-12 17:53:17 -07:00
-- Chaos Mode: If enabled, rail corridors don't stop generating when hitting obstacles
2017-08-09 07:25:07 -07:00
local chaos_mode = minetest.settings : get_bool ( " tsm_railcorridors_chaos " ) or false
2017-05-12 17:53:17 -07:00
2014-09-28 06:30:54 -07:00
-- Parameter Ende
2017-08-13 18:16:18 -07:00
-- Random generators
local pr , webperlin_major , webperlin_minor
2014-09-28 10:01:27 -07:00
local pr_initialized = false
2014-09-27 07:25:50 -07:00
2017-05-13 09:17:01 -07:00
local function InitRandomizer ( seed )
2017-08-13 18:16:18 -07:00
-- Mostly used for corridor gen.
2017-05-13 09:17:01 -07:00
pr = PseudoRandom ( seed )
2017-08-13 18:16:18 -07:00
-- Used for cobweb generation, both noises have to reach a high value for cobwebs to appear
webperlin_major = PerlinNoise ( 934 , 3 , 0.6 , 500 )
webperlin_minor = PerlinNoise ( 834 , 3 , 0.6 , 50 )
2014-09-27 07:25:50 -07:00
pr_initialized = true
end
2017-05-10 18:13:36 -07:00
2016-11-12 18:11:46 -08:00
-- Checks if the mapgen is allowed to carve through this structure and only sets
2017-05-13 09:33:44 -07:00
-- the node if it is allowed. Does never build in liquids.
-- If check_above is true, don't build if the node above is attached (e.g. rail)
-- or a liquid.
2017-05-12 15:33:51 -07:00
local function SetNodeIfCanBuild ( pos , node , check_above )
if check_above then
2017-05-13 11:11:56 -07:00
local abovename = minetest.get_node ( { x = pos.x , y = pos.y + 1 , z = pos.z } ) . name
local abovedef = minetest.registered_nodes [ abovename ]
2017-08-13 18:16:18 -07:00
if abovename == " unknown " or abovename == " ignore " or
( abovedef.groups and abovedef.groups . attached_node ) or
-- This is done because cobwebs are often fake liquids
( abovedef.liquidtype ~= " none " and abovename ~= tsm_railcorridors.nodes . cobweb ) then
2017-05-12 15:33:51 -07:00
return false
end
end
2017-05-13 11:11:56 -07:00
local name = minetest.get_node ( pos ) . name
local def = minetest.registered_nodes [ name ]
2017-08-13 18:16:18 -07:00
if name ~= " unknown " and name ~= " ignore " and def.is_ground_content and
2019-02-11 14:30:19 -08:00
( def.liquidtype == " none " or
name == tsm_railcorridors.nodes . cobweb or
name == tsm_railcorridors.nodes . torch_wall or
name == tsm_railcorridors.nodes . torch_floor
) then
2016-11-12 18:11:46 -08:00
minetest.set_node ( pos , node )
return true
else
return false
end
2016-11-12 14:45:08 -08:00
end
2017-05-10 18:13:36 -07:00
-- Tries to place a rail, taking the damage chance into account
local function PlaceRail ( pos , damage_chance )
if damage_chance ~= nil and damage_chance > 0 then
local x = pr : next ( 0 , 100 )
if x <= damage_chance then
2017-05-13 08:55:11 -07:00
return false
2017-05-10 18:13:36 -07:00
end
end
2017-05-13 08:55:11 -07:00
return SetNodeIfCanBuild ( pos , { name = tsm_railcorridors.nodes . rail } )
2017-05-10 18:13:36 -07:00
end
2017-05-09 16:39:04 -07:00
-- Returns true if the node as point can be considered “ground”, that is, a solid material
-- in which mine shafts can be built into, e.g. stone, but not air or water
local function IsGround ( pos )
2017-05-10 17:23:05 -07:00
local nodename = minetest.get_node ( pos ) . name
local nodedef = minetest.registered_nodes [ nodename ]
return nodename ~= " unknown " and nodename ~= " ignore " and nodedef.is_ground_content and nodedef.walkable and nodedef.liquidtype == " none "
2017-05-09 16:39:04 -07:00
end
2017-05-10 13:53:23 -07:00
-- Returns true if rails are allowed to be placed on top of this node
local function IsRailSurface ( pos )
2017-05-10 17:23:05 -07:00
local nodename = minetest.get_node ( pos ) . name
2019-02-11 18:09:22 -08:00
local nodename_above = minetest.get_node ( { x = pos.x , y = pos.y + 2 , z = pos.z } ) . name
2017-05-10 17:23:05 -07:00
local nodedef = minetest.registered_nodes [ nodename ]
2019-02-11 18:09:22 -08:00
return nodename ~= " unknown " and nodename ~= " ignore " and nodedef.walkable and ( nodedef.node_box == nil or nodedef.node_box . type == " regular " ) and nodename_above ~= tsm_railcorridors.nodes . rail
2017-05-10 13:53:23 -07:00
end
2016-11-12 15:38:21 -08:00
-- Checks if the node is empty space which requires to be filled by a platform
local function NeedsPlatform ( pos )
2017-05-10 08:40:07 -07:00
local node = minetest.get_node ( { x = pos.x , y = pos.y - 1 , z = pos.z } )
local node2 = minetest.get_node ( { x = pos.x , y = pos.y - 2 , z = pos.z } )
2016-11-12 15:38:21 -08:00
local nodedef = minetest.registered_nodes [ node.name ]
2019-02-11 16:21:58 -08:00
return
-- Node can be replaced if ground content or rail
-- Rail is replacable to prevent odd rail slopes.
( node.name ~= " ignore " and node.name ~= " unknown " and ( nodedef.is_ground_content or node.name == tsm_railcorridors.nodes . rail ) ) and
-- Node needs platform if node below is not walkable.
-- Unless 2 nodes below there is dirt: This is a special case for the starter cube.
( ( nodedef.walkable == false and node2.name ~= tsm_railcorridors.nodes . dirt ) or
-- Falling nodes alway need to be replaced by a platform, we want a solid and safe ground
( nodedef.groups and nodedef.groups . falling_node ) )
2016-11-12 15:38:21 -08:00
end
2017-05-12 15:17:22 -07:00
-- Create a cube filled with the specified nodes
-- Specialties:
2017-05-12 15:33:51 -07:00
-- * Avoids floating rails for non-solid nodes like air
2017-05-12 15:17:22 -07:00
-- Returns true if all nodes could be set
-- Returns false if setting one or more nodes failed
2017-08-13 19:10:14 -07:00
local function Cube ( p , radius , node , replace_air_only )
2017-05-12 15:17:22 -07:00
local y_top = p.y + radius
2017-05-12 15:33:51 -07:00
local nodedef = minetest.registered_nodes [ node.name ]
2017-05-13 09:33:44 -07:00
local solid = nodedef.walkable and ( nodedef.node_box == nil or nodedef.node_box . type == " regular " ) and nodedef.liquidtype == " none "
2017-05-12 15:17:22 -07:00
-- Check if all the nodes could be set
local built_all = true
2019-02-11 15:31:22 -08:00
for xi = p.x - radius , p.x + radius do
for zi = p.z - radius , p.z + radius do
local column_last_attached = nil
for yi = y_top , p.y - radius , - 1 do
2017-05-12 15:17:22 -07:00
local ok = false
2019-02-11 15:31:22 -08:00
local thisnode = minetest.get_node ( { x = xi , y = yi , z = zi } )
if not solid then
if yi == y_top then
local topnode = minetest.get_node ( { x = xi , y = yi + 1 , z = zi } )
local topdef = minetest.registered_nodes [ topnode.name ]
if minetest.get_item_group ( topnode.name , " attached_node " ) ~= 1 and topdef.liquidtype == " none " then
ok = true
end
elseif column_last_attached and yi == column_last_attached - 1 then
ok = false
else
2017-05-12 15:17:22 -07:00
ok = true
end
2019-02-11 15:31:22 -08:00
if minetest.get_item_group ( thisnode.name , " attached_node " ) == 1 then
column_last_attached = yi
end
2017-05-12 15:17:22 -07:00
else
ok = true
end
local built = false
if ok then
2017-08-13 19:10:14 -07:00
if replace_air_only ~= true then
built = SetNodeIfCanBuild ( { x = xi , y = yi , z = zi } , node )
else
if minetest.get_node ( { x = xi , y = yi , z = zi } ) . name == " air " then
built = SetNodeIfCanBuild ( { x = xi , y = yi , z = zi } , node )
end
end
2017-05-12 15:17:22 -07:00
end
2017-05-12 16:07:45 -07:00
if not built then
2017-05-12 15:17:22 -07:00
built_all = false
end
2014-09-28 10:01:27 -07:00
end
end
2014-09-27 07:25:50 -07:00
end
2017-05-12 15:17:22 -07:00
return built_all
2014-09-27 07:25:50 -07:00
end
2019-02-11 20:22:37 -08:00
function DirtRoom ( p , radius , height )
local y_top = p.y - radius + height + 1
local built_all = true
for xi = p.x - radius , p.x + radius do
for zi = p.z - radius , p.z + radius do
for yi = y_top , p.y - radius , - 1 do
local thisnode = minetest.get_node ( { x = xi , y = yi , z = zi } )
local built = false
if xi == p.x - radius or xi == p.x + radius or zi == p.z - radius or zi == p.z + radius or yi == p.y - radius or yi == y_top then
built = SetNodeIfCanBuild ( { x = xi , y = yi , z = zi } , { name = tsm_railcorridors.nodes . dirt } )
else
built = SetNodeIfCanBuild ( { x = xi , y = yi , z = zi } , { name = " air " } )
end
if not built then
built_all = false
end
end
end
end
return built_all
end
2016-11-12 15:38:21 -08:00
local function Platform ( p , radius , node )
for zi = p.z - radius , p.z + radius do
for xi = p.x - radius , p.x + radius do
2017-05-10 08:40:07 -07:00
local np = NeedsPlatform ( { x = xi , y = p.y , z = zi } )
if np then
minetest.set_node ( { x = xi , y = p.y - 1 , z = zi } , node )
2016-11-12 15:38:21 -08:00
end
end
end
end
2017-05-13 07:37:34 -07:00
2014-09-30 14:02:12 -07:00
-- Random chest items
-- Zufälliger Kisteninhalt
2016-08-11 08:44:02 -07:00
local function rci ( )
2014-10-09 08:21:38 -07:00
if ( minetest.get_modpath ( " treasurer " ) ~= nil ) then
local treasures
2016-11-14 08:58:10 -08:00
if pr : next ( 0 , 100 ) < 3 then
2014-10-09 08:21:38 -07:00
treasures = treasurer.select_random_treasures ( 1 , 2 , 4 )
2016-11-14 08:58:10 -08:00
elseif pr : next ( 0 , 100 ) < 5 then
if pr : next ( 0 , 100 ) < 50 then
2014-10-09 08:21:38 -07:00
treasures = treasurer.select_random_treasures ( 1 , 2 , 4 , " seed " )
else
treasures = treasurer.select_random_treasures ( 1 , 2 , 4 , " seed " )
end
2016-11-14 08:58:10 -08:00
elseif pr : next ( 0 , 1000 ) < 5 then
2017-05-13 08:14:46 -07:00
if minetest.get_modpath ( " tnt " ) then
return " tnt:tnt " .. pr : next ( 1 , 3 )
end
2016-11-14 08:58:10 -08:00
elseif pr : next ( 0 , 1000 ) < 3 then
if pr : next ( 0 , 1000 ) < 800 then
2014-10-09 08:21:38 -07:00
treasures = treasurer.select_random_treasures ( 1 , 3 , 6 , " mineral " )
else
treasures = treasurer.select_random_treasures ( 1 , 5 , 9 , " mineral " )
end
2014-09-30 14:02:12 -07:00
end
2014-10-09 08:21:38 -07:00
if ( treasures ~= nil ) then
2016-11-12 13:35:49 -08:00
if ( # treasures >= 1 ) then
2014-10-09 08:21:38 -07:00
return treasures [ 1 ] : get_name ( )
else
return " "
end
2014-09-30 14:02:12 -07:00
else
2014-10-09 08:21:38 -07:00
return " "
end
else
2017-05-13 07:37:34 -07:00
return tsm_railcorridors.get_default_treasure ( pr )
2014-09-30 14:02:12 -07:00
end
end
-- chests
2017-08-13 18:16:18 -07:00
local function PlaceChest ( pos , param2 )
2017-05-13 07:50:47 -07:00
if SetNodeIfCanBuild ( pos , { name = tsm_railcorridors.nodes . chest , param2 = param2 } ) then
2016-11-12 18:11:46 -08:00
local meta = minetest.get_meta ( pos )
local inv = meta : get_inventory ( )
2017-05-10 18:21:47 -07:00
for i = 1 , inv : get_size ( " main " ) do
2016-11-12 18:11:46 -08:00
inv : set_stack ( " main " , i , ItemStack ( rci ( ) ) )
end
2014-09-30 14:02:12 -07:00
end
2016-11-03 13:44:11 -07:00
end
2017-08-13 18:16:18 -07:00
2017-08-14 08:49:21 -07:00
-- This function checks if a cart has ACTUALLY been spawned.
-- If not, it tries to spawn it again, and again, until it succeeded or
-- it failed too often.
-- To be calld by minetest.after.
-- This is a HORRIBLE 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 ]
local tries = params [ 3 ]
tries = tries - 1
-- 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)
tsm_railcorridors.on_construct_cart ( pos , obj )
return
end
end
if tries <= 0 then
-- Abort if too many tries to avoid excessive function calls
return
end
-- No cart found! :-( Try again …
if minetest.get_node ( pos ) . name == tsm_railcorridors.nodes . rail then
minetest.add_entity ( pos , cart_id )
minetest.after ( 5 , RecheckCartHack , { pos , cart_id , tries } )
2017-08-14 12:02:51 -07:00
return
2017-08-14 08:49:21 -07:00
end
-- The rail may have been destroyed in the meantime, that's why the node is checked.
end
2017-08-13 18:16:18 -07:00
-- 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.
-- side_vector: Required if needs_check is true. Unit vector which points towards the side of the cobweb to place.
local function TryPlaceCobweb ( pos , needs_check , side_vector )
local check_passed = false
if needs_check then
-- Check for walkable nodes above, below or at the side of the cobweb.
-- If any of those nodes is walkable, we are fine.
local check_vectors = {
side_vector ,
{ x = 0 , y = 1 , z = 0 } ,
{ x = 0 , y =- 1 , z = 0 } ,
}
for c = 1 , # check_vectors do
local cpos = vector.add ( pos , check_vectors [ c ] )
local cname = minetest.get_node ( cpos ) . name
local cdef = minetest.registered_nodes [ cname ]
if cname ~= " ignore " and cdef.walkable then
check_passed = true
break
end
end
else
check_passed = true
end
if check_passed then
return SetNodeIfCanBuild ( pos , { name = tsm_railcorridors.nodes . cobweb } )
else
return false
end
end
2014-09-30 14:02:12 -07:00
2016-11-12 16:52:53 -08:00
local function WoodBulk ( pos , wood )
2016-11-12 18:11:46 -08:00
SetNodeIfCanBuild ( { x = pos.x + 1 , y = pos.y , z = pos.z + 1 } , { name = wood } )
SetNodeIfCanBuild ( { x = pos.x - 1 , y = pos.y , z = pos.z + 1 } , { name = wood } )
SetNodeIfCanBuild ( { x = pos.x + 1 , y = pos.y , z = pos.z - 1 } , { name = wood } )
SetNodeIfCanBuild ( { x = pos.x - 1 , y = pos.y , z = pos.z - 1 } , { name = wood } )
2014-09-30 14:02:12 -07:00
end
2014-09-27 07:25:50 -07:00
-- Gänge mit Schienen
2014-09-28 05:39:11 -07:00
-- Corridors with rails
2014-09-27 07:25:50 -07:00
2017-05-13 08:55:11 -07:00
-- Returns <success>, <segments>
-- success: true if corridor could be placed entirely
-- segments: Number of segments successfully placed
2017-08-14 10:40:38 -07:00
local function corridor_part ( start_point , segment_vector , segment_count , wood , post , first_or_final , up_or_down_prev )
2014-09-27 07:25:50 -07:00
local p = { x = start_point.x , y = start_point.y , z = start_point.z }
2016-11-14 08:58:10 -08:00
local torches = pr : next ( ) < probability_torches_in_segment
2014-09-30 14:02:12 -07:00
local dir = { 0 , 0 }
local torchdir = { 1 , 1 }
2016-11-12 16:52:53 -08:00
local node_wood = { name = wood }
local node_fence = { name = post }
2014-09-30 14:02:12 -07:00
if segment_vector.x == 0 and segment_vector.z ~= 0 then
dir = { 1 , 0 }
torchdir = { 5 , 4 }
elseif segment_vector.x ~= 0 and segment_vector.z == 0 then
dir = { 0 , 1 }
torchdir = { 3 , 2 }
end
2014-09-27 07:25:50 -07:00
for segmentindex = 0 , segment_count - 1 do
2017-05-12 17:53:17 -07:00
local dug = Cube ( p , 1 , { name = " air " } )
2017-05-13 08:55:11 -07:00
if not chaos_mode and segmentindex > 0 and not dug then return false , segmentindex end
2016-11-12 15:38:21 -08:00
-- Add wooden platform, if neccessary. To avoid floating rails
if segment_vector.y == 0 then
2017-08-13 09:03:52 -07:00
if segmentindex == 0 and up_or_down_prev then
-- Thin 1× 1 platform directly after going up or down.
-- This is done to avoid placing too much wood at slopes
Platform ( { x = p.x - dir [ 2 ] , y = p.y - 1 , z = p.z - dir [ 1 ] } , 0 , node_wood )
Platform ( { x = p.x , y = p.y - 1 , z = p.z } , 0 , node_wood )
Platform ( { x = p.x + dir [ 2 ] , y = p.y - 1 , z = p.z + dir [ 1 ] } , 0 , node_wood )
else
-- Normal 3× 3 platform
Platform ( { x = p.x , y = p.y - 1 , z = p.z } , 1 , node_wood )
end
2016-11-12 15:38:21 -08:00
end
2014-09-27 07:25:50 -07:00
-- Diese komischen Holz-Konstruktionen
2014-09-28 10:01:27 -07:00
-- These strange wood structs
2016-11-14 08:58:10 -08:00
if segmentindex % 2 == 1 and segment_vector.y == 0 then
2014-09-28 10:01:27 -07:00
local calc = {
p.x + dir [ 1 ] , p.z + dir [ 2 ] , -- X and Z, added by direction
p.x - dir [ 1 ] , p.z - dir [ 2 ] , -- subtracted
2014-09-28 11:40:24 -07:00
p.x + dir [ 2 ] , p.z + dir [ 1 ] , -- orthogonal
2014-09-29 07:16:55 -07:00
p.x - dir [ 2 ] , p.z - dir [ 1 ] , -- orthogonal, the other way
2014-09-28 10:01:27 -07:00
}
2016-11-12 18:11:46 -08:00
--[[ Shape:
WWW
P.P
PrP
pfp
W = wood
P = post ( above floor level )
p = post ( in floor level , only placed if no floor )
2014-09-28 10:01:27 -07:00
2016-11-12 18:11:46 -08:00
From previous generation ( for reference ) :
f = floor
r = rail
. = air
] ]
2016-11-12 19:16:47 -08:00
-- Don't place those wood structs below open air
if not ( minetest.get_node ( { x = calc [ 1 ] , y = p.y + 2 , z = calc [ 2 ] } ) . name == " air " and
minetest.get_node ( { x = calc [ 3 ] , y = p.y + 2 , z = calc [ 4 ] } ) . name == " air " and
minetest.get_node ( { x = p.x , y = p.y + 2 , z = p.z } ) . name == " air " ) then
2016-11-12 18:11:46 -08:00
2016-11-12 19:16:47 -08:00
-- Left post and planks
local left_ok = true
left_ok = SetNodeIfCanBuild ( { x = calc [ 1 ] , y = p.y - 1 , z = calc [ 2 ] } , node_fence )
if left_ok then left_ok = SetNodeIfCanBuild ( { x = calc [ 1 ] , y = p.y , z = calc [ 2 ] } , node_fence ) end
if left_ok then left_ok = SetNodeIfCanBuild ( { x = calc [ 1 ] , y = p.y + 1 , z = calc [ 2 ] } , node_wood ) end
2016-11-12 18:11:46 -08:00
2016-11-12 19:16:47 -08:00
-- Right post and planks
local right_ok = true
right_ok = SetNodeIfCanBuild ( { x = calc [ 3 ] , y = p.y - 1 , z = calc [ 4 ] } , node_fence )
if right_ok then right_ok = SetNodeIfCanBuild ( { x = calc [ 3 ] , y = p.y , z = calc [ 4 ] } , node_fence ) end
if right_ok then right_ok = SetNodeIfCanBuild ( { x = calc [ 3 ] , y = p.y + 1 , z = calc [ 4 ] } , node_wood ) end
-- Middle planks
local top_planks_ok = false
if left_ok and right_ok then top_planks_ok = SetNodeIfCanBuild ( { x = p.x , y = p.y + 1 , z = p.z } , node_wood ) end
if minetest.get_node ( { x = p.x , y = p.y - 2 , z = p.z } ) . name == " air " then
if left_ok then SetNodeIfCanBuild ( { x = calc [ 1 ] , y = p.y - 2 , z = calc [ 2 ] } , node_fence ) end
if right_ok then SetNodeIfCanBuild ( { x = calc [ 3 ] , y = p.y - 2 , z = calc [ 4 ] } , node_fence ) end
end
-- Torches on the middle planks
if torches and top_planks_ok then
-- Place torches at horizontal sides
2017-05-12 15:33:51 -07:00
SetNodeIfCanBuild ( { x = calc [ 5 ] , y = p.y + 1 , z = calc [ 6 ] } , { name = tsm_railcorridors.nodes . torch_wall , param2 = torchdir [ 1 ] } , true )
SetNodeIfCanBuild ( { x = calc [ 7 ] , y = p.y + 1 , z = calc [ 8 ] } , { name = tsm_railcorridors.nodes . torch_wall , param2 = torchdir [ 2 ] } , true )
2016-11-12 13:59:26 -08:00
end
2016-11-12 19:16:47 -08:00
elseif torches then
-- Try to build torches instead of the wood structs
2017-05-10 14:52:54 -07:00
local node = { name = tsm_railcorridors.nodes . torch_floor , param2 = minetest.dir_to_wallmounted ( { x = 0 , y =- 1 , z = 0 } ) }
2016-11-12 19:16:47 -08:00
-- Try two different height levels
2016-11-14 20:09:59 -08:00
local pos1 = { x = calc [ 1 ] , y = p.y - 2 , z = calc [ 2 ] }
local pos2 = { x = calc [ 3 ] , y = p.y - 2 , z = calc [ 4 ] }
local nodedef1 = minetest.registered_nodes [ minetest.get_node ( pos1 ) . name ]
local nodedef2 = minetest.registered_nodes [ minetest.get_node ( pos2 ) . name ]
if nodedef1.walkable then
pos1.y = pos1.y + 1
2016-11-12 19:16:47 -08:00
end
2017-05-12 15:33:51 -07:00
SetNodeIfCanBuild ( pos1 , node , true )
2016-11-14 20:09:59 -08:00
if nodedef2.walkable then
pos2.y = pos2.y + 1
2016-11-12 19:16:47 -08:00
end
2017-05-12 15:33:51 -07:00
SetNodeIfCanBuild ( pos2 , node , true )
2016-11-12 19:16:47 -08:00
2014-09-27 07:25:50 -07:00
end
end
2014-09-28 10:01:27 -07:00
2014-09-30 14:02:12 -07:00
-- nächster Punkt durch Vektoraddition
2014-09-28 05:39:11 -07:00
-- next way point
2014-09-28 10:01:27 -07:00
p = vector.add ( p , segment_vector )
2014-09-27 07:25:50 -07:00
end
2017-05-09 20:26:12 -07:00
2017-08-13 08:40:49 -07:00
-- End of the corridor segment; create the final piece
local dug = Cube ( p , 1 , { name = " air " } )
if not chaos_mode and not dug then return false , segment_count end
if segment_vector.y == 0 then
2017-05-10 08:40:07 -07:00
Platform ( { x = p.x , y = p.y - 1 , z = p.z } , 1 , node_wood )
2017-05-09 20:26:12 -07:00
end
2017-05-13 08:55:11 -07:00
return true , segment_count
2014-09-27 07:25:50 -07:00
end
2017-08-14 10:40:38 -07:00
local function corridor_func ( waypoint , coord , sign , up_or_down , up_or_down_next , up_or_down_prev , up , wood , post , first_or_final , damage , no_spawner )
2014-09-27 07:25:50 -07:00
local segamount = 3
if up_or_down then
segamount = 1
end
if sign then
segamount = 0 - segamount
end
local vek = { x = 0 , y = 0 , z = 0 } ;
2017-05-10 13:27:55 -07:00
local start = table.copy ( waypoint )
2014-09-27 07:25:50 -07:00
if coord == " x " then
vek.x = segamount
2017-05-10 13:27:55 -07:00
if up_or_down and up == false then
start.x = start.x + segamount
end
2014-09-27 07:25:50 -07:00
elseif coord == " z " then
vek.z = segamount
2017-05-10 13:27:55 -07:00
if up_or_down and up == false then
start.z = start.z + segamount
end
2014-09-27 07:25:50 -07:00
end
if up_or_down then
2014-09-28 10:01:27 -07:00
if up then
vek.y = 1
else
vek.y = - 1
end
2014-09-27 07:25:50 -07:00
end
local segcount = pr : next ( 4 , 6 )
2017-05-10 13:27:55 -07:00
if up_or_down and up == false then
Cube ( waypoint , 1 , { name = " air " } )
end
2017-08-14 10:40:38 -07:00
local corridor_dug , corridor_segments_dug = corridor_part ( start , vek , segcount , wood , post , first_or_final , up_or_down_prev )
2014-09-27 07:25:50 -07:00
local corridor_vek = { x = vek.x * segcount , y = vek.y * segcount , z = vek.z * segcount }
-- nachträglich Schienen legen
2014-09-28 05:39:11 -07:00
-- after this: rails
2014-09-27 07:25:50 -07:00
segamount = 1
if sign then
segamount = 0 - segamount
end
if coord == " x " then
vek.x = segamount
elseif coord == " z " then
vek.z = segamount
end
if up_or_down then
2014-09-28 10:01:27 -07:00
if up then
vek.y = 1
else
vek.y = - 1
end
2014-09-27 07:25:50 -07:00
end
2017-08-14 06:03:32 -07:00
-- Calculate chest and cart position
2014-09-30 14:02:12 -07:00
local chestplace = - 1
2017-08-14 06:03:32 -07:00
local cartplace = - 1
2017-08-14 10:40:38 -07:00
local minseg
if first_or_final == " first " then
minseg = 2
else
minseg = 1
end
2017-08-14 06:03:32 -07:00
if corridor_dug and not up_or_down then
if pr : next ( ) < probability_chest then
2017-08-14 10:40:38 -07:00
chestplace = pr : next ( minseg , segcount + 1 )
2017-08-14 06:03:32 -07:00
end
if tsm_railcorridors.carts and # tsm_railcorridors.carts > 0 and pr : next ( ) < probability_cart then
2017-08-14 10:40:38 -07:00
cartplace = pr : next ( minseg , segcount + 1 )
2017-08-14 06:03:32 -07:00
end
2014-09-30 14:02:12 -07:00
end
2017-05-13 08:55:11 -07:00
local railsegcount
if not chaos_mode and not corridor_dug then
railsegcount = corridor_segments_dug * 3
elseif not up_or_down then
railsegcount = segcount * 3
else
railsegcount = segcount
end
for i = 1 , railsegcount do
2017-05-10 10:53:40 -07:00
local p = { x = waypoint.x + vek.x * i , y = waypoint.y + vek.y * i - 1 , z = waypoint.z + vek.z * i }
2017-08-14 06:03:32 -07:00
2017-08-14 09:00:54 -07:00
-- Randomly returns either the left or right side of the main rail.
-- Also returns offset as second return value.
local left_or_right = function ( pos , vek )
local off , facedir
if pr : next ( 1 , 2 ) == 1 then
-- left
off = { x = - vek.z , y = 0 , z = vek.x }
else
-- right
off = { x = vek.z , y = 0 , z = - vek.x }
end
return vector.add ( pos , off ) , off
end
2017-05-10 14:52:54 -07:00
if ( minetest.get_node ( { x = p.x , y = p.y - 1 , z = p.z } ) . name == " air " and minetest.get_node ( { x = p.x , y = p.y - 3 , z = p.z } ) . name ~= tsm_railcorridors.nodes . rail ) then
2017-05-10 10:53:40 -07:00
p.y = p.y - 1 ;
2017-05-13 09:09:55 -07:00
if i == chestplace then
chestplace = chestplace + 1
end
2017-08-14 06:03:32 -07:00
if i == cartplace then
cartplace = cartplace + 1
end
2017-05-10 10:53:40 -07:00
end
2017-08-13 19:10:14 -07:00
-- Chest
2017-05-10 10:53:40 -07:00
if i == chestplace then
2017-08-14 09:00:54 -07:00
local cpos , offset = left_or_right ( p , vek )
if minetest.get_node ( cpos ) . name == post then
2017-05-10 10:53:40 -07:00
chestplace = chestplace + 1
else
2017-08-14 09:00:54 -07:00
PlaceChest ( cpos , minetest.dir_to_facedir ( offset ) )
2014-09-30 14:02:12 -07:00
end
2014-09-27 07:25:50 -07:00
end
2017-08-13 19:10:14 -07:00
2017-08-14 06:03:32 -07:00
-- Rail and cart
if i == cartplace then
2017-08-14 09:00:54 -07:00
local cpos = left_or_right ( p , vek )
if minetest.get_node ( cpos ) . name == post then
2017-08-14 06:03:32 -07:00
cartplace = cartplace + 1
else
2019-02-11 15:06:29 -08:00
local placed
if IsRailSurface ( { x = cpos.x , y = cpos.y - 1 , z = cpos.z } ) then
placed = PlaceRail ( cpos , damage )
else
placed = false
end
2017-08-14 06:03:32 -07:00
if placed then
local cart_type = pr : next ( 1 , # tsm_railcorridors.carts )
-- FIXME: The cart sometimes fails to spawn
2017-08-14 08:49:21 -07:00
-- See <https://github.com/minetest/minetest/issues/4759>
local cart_id = tsm_railcorridors.carts [ cart_type ]
local cart = minetest.add_entity ( cpos , cart_id )
-- 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.
2017-08-14 12:02:51 -07:00
minetest.after ( 3 , RecheckCartHack , { cpos , cart_id , 12 } )
2017-08-14 06:03:32 -07:00
end
end
end
2017-08-13 19:10:14 -07:00
-- Mob spawner (at center)
2017-08-13 20:19:05 -07:00
if place_mob_spawners and tsm_railcorridors.nodes . spawner and not no_spawner and
2017-08-13 19:21:59 -07:00
webperlin_major : get3d ( p ) > 0.3 and webperlin_minor : get3d ( p ) > 0.5 then
2017-08-13 19:10:14 -07:00
-- Place spawner (if activated in gameconfig),
-- enclose in cobwebs and setup the spawner node.
local spawner_placed = SetNodeIfCanBuild ( p , { name = tsm_railcorridors.nodes . spawner } )
if spawner_placed then
local size = 1
if webperlin_major : get3d ( p ) > 0.5 then
size = 2
end
2017-08-13 19:40:36 -07:00
if place_cobwebs then
Cube ( p , size , { name = tsm_railcorridors.nodes . cobweb } , true )
end
2017-08-13 19:10:14 -07:00
tsm_railcorridors.on_construct_spawner ( p )
2017-08-13 20:19:05 -07:00
no_spawner = true
2017-08-13 19:10:14 -07:00
end
end
-- Main rail; this places almost all the rails
if IsRailSurface ( { x = p.x , y = p.y - 1 , z = p.z } ) then
PlaceRail ( p , damage )
end
2017-08-13 18:16:18 -07:00
-- Place cobwebs left and right in the corridor
2017-08-13 19:21:59 -07:00
if place_cobwebs and tsm_railcorridors.nodes . cobweb then
2017-08-13 18:16:18 -07:00
-- Helper function to place a cobweb at the side (based on chance an Perlin noise)
local cobweb_at_side = function ( basepos , vek )
if pr : next ( 1 , 5 ) == 1 then
local h = pr : next ( 0 , 2 ) -- 3 possible cobweb heights
local cpos = { x = basepos.x + vek.x , y = basepos.y + h , z = basepos.z + vek.z }
if webperlin_major : get3d ( cpos ) > 0.05 and webperlin_minor : get3d ( cpos ) > 0.1 then
if h == 0 then
-- No check neccessary at height offset 0 since the cobweb is on the floor
return TryPlaceCobweb ( cpos )
else
-- Check nessessary
return TryPlaceCobweb ( cpos , true , vek )
end
end
end
return false
end
-- Right cobweb
local rvek = { x =- vek.z , y = 0 , z = vek.x }
cobweb_at_side ( p , rvek )
-- Left cobweb
local lvek = { x = vek.z , y = 0 , z =- vek.x }
cobweb_at_side ( p , lvek )
end
2014-09-27 07:25:50 -07:00
end
2014-09-30 14:02:12 -07:00
2017-05-10 13:27:55 -07:00
local offset = table.copy ( corridor_vek )
local final_point = vector.add ( waypoint , offset )
if up_or_down then
if up then
offset.y = offset.y - 1
final_point = vector.add ( waypoint , offset )
else
offset [ coord ] = offset [ coord ] + segamount
final_point = vector.add ( waypoint , offset )
2017-08-13 09:19:48 -07:00
-- After going up or down, 1 missing rail piece must be added
2017-05-10 13:53:23 -07:00
if IsRailSurface ( { x = final_point.x , y = final_point.y - 2 , z = final_point.z } ) then
2017-05-10 18:13:36 -07:00
PlaceRail ( { x = final_point.x , y = final_point.y - 1 , z = final_point.z } , damage )
2017-05-10 13:27:55 -07:00
end
end
end
2017-05-13 08:55:11 -07:00
if not corridor_dug then
2017-08-13 20:19:05 -07:00
return false , no_spawner
2017-05-13 08:55:11 -07:00
else
2017-08-13 20:19:05 -07:00
return final_point , no_spawner
2017-05-13 08:55:11 -07:00
end
2014-09-27 07:25:50 -07:00
end
2017-08-13 20:34:52 -07:00
local function start_corridor ( waypoint , coord , sign , length , psra , wood , post , damage , no_spawner )
2014-09-27 07:25:50 -07:00
local wp = waypoint
local c = coord
local s = sign
2017-05-10 06:58:59 -07:00
local ud = false -- up or down
2017-05-10 12:00:57 -07:00
local udn = false -- up or down is next
2017-08-13 09:03:52 -07:00
local udp = false -- up or down was previous
2014-09-30 14:02:12 -07:00
local up
for i = 1 , length do
2017-05-13 10:48:14 -07:00
local needs_platform
2017-08-13 09:19:48 -07:00
-- Update previous up/down status
udp = ud
-- Update current up/down status
2017-05-10 12:00:57 -07:00
if udn then
2017-05-13 10:48:14 -07:00
needs_platform = NeedsPlatform ( wp )
if needs_platform then
ud = false
end
2014-09-27 07:25:50 -07:00
ud = true
2017-05-09 16:15:52 -07:00
-- Force direction near the height limits
if wp.y >= height_max - 12 then
up = false
elseif wp.y <= height_min + 12 then
up = true
else
-- Chose random direction in between
up = pr : next ( 0 , 2 ) < 1
end
2014-09-27 07:25:50 -07:00
else
2017-05-10 06:58:59 -07:00
ud = false
2014-09-27 07:25:50 -07:00
end
2017-08-13 09:19:48 -07:00
-- Update next up/down status
2017-05-13 10:48:14 -07:00
if pr : next ( ) < probability_up_or_down and i ~= 1 and not udn and not needs_platform then
2017-05-10 12:00:57 -07:00
udn = i < length
2017-05-13 10:48:14 -07:00
elseif udn and not needs_platform then
2017-05-10 12:00:57 -07:00
udn = false
end
2014-09-30 14:02:12 -07:00
-- Make corridor / Korridor graben
2017-08-14 10:40:38 -07:00
local first_or_final
if i == length then
first_or_final = " final "
elseif i == 1 then
first_or_final = " first "
end
wp , no_spawner = corridor_func ( wp , c , s , ud , udn , udp , up , wood , post , first_or_final , damage , no_spawner )
2017-05-12 17:53:17 -07:00
if wp == false then return end
2014-09-30 14:02:12 -07:00
-- Verzweigung?
-- Fork?
2016-11-14 08:58:10 -08:00
if pr : next ( ) < probability_fork then
2014-09-30 14:02:12 -07:00
local p = { x = wp.x , y = wp.y , z = wp.z }
2017-08-13 20:34:52 -07:00
start_corridor ( wp , c , s , pr : next ( way_min , way_max ) , psra , wood , post , damage , no_spawner )
2014-09-30 14:02:12 -07:00
if c == " x " then c = " z " else c = " x " end
2017-08-13 20:34:52 -07:00
start_corridor ( wp , c , s , pr : next ( way_min , way_max ) , psra , wood , post , damage , no_spawner )
start_corridor ( wp , c , not s , pr : next ( way_min , way_max ) , psra , wood , post , damage , no_spawner )
2016-11-12 16:52:53 -08:00
WoodBulk ( { x = p.x , y = p.y - 1 , z = p.z } , wood )
WoodBulk ( { x = p.x , y = p.y , z = p.z } , wood )
WoodBulk ( { x = p.x , y = p.y + 1 , z = p.z } , wood )
WoodBulk ( { x = p.x , y = p.y + 2 , z = p.z } , wood )
2014-09-30 14:02:12 -07:00
return
end
2014-09-27 07:25:50 -07:00
-- coord und sign verändern
2014-09-28 05:39:11 -07:00
-- randomly change sign and coord
2014-09-27 07:25:50 -07:00
if c == " x " then
c = " z "
elseif c == " z " then
c = " x "
end ;
2016-11-14 08:58:10 -08:00
s = pr : next ( 0 , 2 ) < 1
2014-09-27 07:25:50 -07:00
end
end
2016-08-11 08:44:02 -07:00
local function place_corridors ( main_cave_coords , psra )
2017-05-09 16:39:04 -07:00
--[[ ALWAYS start building in the ground. Prevents corridors starting
in mid - air or in liquids . ] ]
if not IsGround ( main_cave_coords ) then
return
end
2017-12-08 07:17:06 -08:00
local center_node = minetest.get_node ( main_cave_coords )
2017-05-09 16:39:04 -07:00
2017-05-10 18:13:36 -07:00
-- Determine if this corridor system is “damaged” (some rails removed) and to which extent
local damage = 0
2017-05-13 13:06:51 -07:00
if pr : next ( ) < probability_damage then
2017-05-10 18:13:36 -07:00
damage = pr : next ( 10 , 50 )
end
2017-05-09 16:39:04 -07:00
--[[ Starter cube: A big hollow dirt cube from which the corridors will extend.
Corridor generation starts here . ] ]
2019-02-11 22:55:23 -08:00
local size = pr : next ( 3 , 9 )
2019-02-11 20:22:37 -08:00
local height = pr : next ( 4 , 7 )
if height > size then
height = size
end
local floor_diff = 1
2016-11-14 08:58:10 -08:00
if pr : next ( 0 , 100 ) < 50 then
2019-02-11 20:22:37 -08:00
floor_diff = 0
2014-09-30 14:02:12 -07:00
end
2019-02-11 20:22:37 -08:00
DirtRoom ( main_cave_coords , size , height )
main_cave_coords.y = main_cave_coords.y - ( size - 2 - floor_diff )
2016-11-12 16:52:53 -08:00
2017-12-08 07:17:06 -08:00
-- Get wood and fence post types, using gameconfig.
local wood , post
if tsm_railcorridors.nodes . corridor_woods_function then
-- Get wood type by gameconfig function
wood , post = tsm_railcorridors.nodes . corridor_woods_function ( main_cave_coords , center_node )
else
-- Select random wood type (found in gameconfig.lua)
local rnd = pr : next ( 1 , 1000 )
local woodtype = 1
local accumulated_chance = 0
for w = 1 , # tsm_railcorridors.nodes . corridor_woods do
local woodtable = tsm_railcorridors.nodes . corridor_woods [ w ]
accumulated_chance = accumulated_chance + woodtable.chance
if accumulated_chance > 1000 then
minetest.log ( " warning " , " [tsm_railcorridors] Warning: Wood chances add up to over 100%! " )
break
end
if rnd <= accumulated_chance then
woodtype = w
break
end
2017-05-10 14:52:54 -07:00
end
2017-12-08 07:17:06 -08:00
wood = tsm_railcorridors.nodes . corridor_woods [ woodtype ] . wood
post = tsm_railcorridors.nodes . corridor_woods [ woodtype ] . post
2016-11-12 16:52:53 -08:00
end
2017-12-08 07:17:06 -08:00
2019-02-11 22:55:23 -08:00
-- Start 2-5 corridors in each direction
local dirs = {
{ axis = " x " , axis2 = " z " , sign = false } ,
{ axis = " x " , axis2 = " z " , sign = true } ,
{ axis = " z " , axis2 = " x " , sign = false } ,
{ axis = " z " , axis2 = " x " , sign = true } ,
}
local first_corridor
local corridors = 2
for i = 1 , 2 do
if pr : next ( 0 , 100 ) < 70 then
corridors = corridors + 1
end
end
if size > 4 then
if pr : next ( 0 , 100 ) < 50 then
corridors = corridors + 1
end
end
local centered_crossing = false
local place_center_rail = false
if corridors <= 4 and pr : next ( 1 , 20 ) >= 11 then
centered_crossing = true
end
local from_center_base = 0
if not centered_crossing then
from_center_base = pr : next ( 0 , size )
end
for i = 1 , math.min ( 4 , corridors ) do
local d = pr : next ( 1 , # dirs )
local dir = dirs [ d ]
local side_offset = 0
if not centered_crossing and size > 3 then
if i == 1 and corridors == 5 then
side_offset = pr : next ( 2 , size - 2 )
if pr : next ( 1 , 2 ) == 1 then
side_offset = - side_offset
end
else
side_offset = pr : next ( - size + 2 , size - 2 )
end
end
local from_center = from_center_base
if not centered_crossing then
if dir.sign then
from_center = - from_center
end
end
if from_center == 0 and side_offset == 0 then
place_center_rail = true
end
if i == 1 then
first_corridor = { sign = dir.sign , axis = dir.axis , axis2 = dir.axis2 , side_offset = side_offset , from_center = from_center }
end
local coords = vector.add ( main_cave_coords , { [ dir.axis ] = from_center , y = 0 , [ dir.axis2 ] = side_offset } )
start_corridor ( coords , dir.axis , dir.sign , pr : next ( way_min , way_max ) , psra , wood , post , damage , false )
table.remove ( dirs , d )
end
if corridors == 5 then
local special_coords = vector.add ( main_cave_coords , { [ first_corridor.axis2 ] = - first_corridor.side_offset , y = 0 , [ first_corridor.axis ] = first_corridor.from_center } )
start_corridor ( special_coords , first_corridor.axis , first_corridor.sign , pr : next ( way_min , way_max ) , psra , wood , post , damage , false )
2014-09-27 07:25:50 -07:00
end
2019-02-11 22:55:23 -08:00
if place_center_rail then
-- Center rail
PlaceRail ( vector.add ( main_cave_coords , { x = 0 , y =- 1 - floor_diff , z = 0 } ) , damage )
2014-09-27 07:25:50 -07:00
end
2019-02-11 22:55:23 -08:00
2014-09-27 07:25:50 -07:00
end
2017-05-13 09:17:01 -07:00
minetest.register_on_generated ( function ( minp , maxp , blockseed )
InitRandomizer ( blockseed )
2017-05-09 16:15:52 -07:00
if minp.y < height_max and maxp.y > height_min and pr : next ( ) < probability_railcaves_in_chunk then
2017-05-13 12:31:34 -07:00
-- Get semi-random height in chunk
local buffer = 5
local y = pr : next ( minp.y + buffer , maxp.y - buffer )
2017-08-13 19:40:36 -07:00
y = math.floor ( math.max ( height_min + buffer , math.min ( height_max - buffer , y ) ) )
2017-05-13 12:31:34 -07:00
2014-09-28 05:39:11 -07:00
-- Mid point of the chunk
2017-08-12 11:38:33 -07:00
local p = { x = minp.x + math.floor ( ( maxp.x - minp.x ) / 2 ) , y = y , z = minp.z + math.floor ( ( maxp.z - minp.z ) / 2 ) }
2014-09-27 08:30:32 -07:00
-- Haupthöhle und alle weiteren
2014-09-28 05:39:11 -07:00
-- Corridors; starting with main cave out of dirt
2014-09-27 07:25:50 -07:00
place_corridors ( p , pr )
end
end )