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
2016-11-14 08:58:10 -08:00
local probability_railcaves_in_chunk = P ( 1 / 3 )
2016-11-03 14:42:57 -07:00
setting = tonumber ( minetest.setting_get ( " tsm_railcorridors_probability_railcaves_in_chunk " ) )
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 ;
2016-11-03 14:42:57 -07:00
setting = tonumber ( minetest.setting_get ( " tsm_railcorridors_way_min " ) )
if setting then
way_min = setting
end
setting = tonumber ( minetest.setting_get ( " tsm_railcorridors_way_max " ) )
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 )
2016-11-03 14:42:57 -07:00
setting = tonumber ( minetest.setting_get ( " tsm_railcorridors_probability_torches_in_segment " ) )
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 )
2016-11-03 14:42:57 -07:00
setting = tonumber ( minetest.setting_get ( " tsm_railcorridors_probability_up_or_down " ) )
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 )
2016-11-03 14:42:57 -07:00
setting = tonumber ( minetest.setting_get ( " tsm_railcorridors_probability_fork " ) )
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 )
2016-11-03 14:42:57 -07:00
setting = tonumber ( minetest.setting_get ( " tsm_railcorridors_probability_chest " ) )
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-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-05-09 15:50:13 -07:00
setting = tonumber ( minetest.setting_get ( " tsm_railcorridors_height_min " ) )
if setting then
height_min = setting
end
setting = tonumber ( minetest.setting_get ( " tsm_railcorridors_height_max " ) )
if setting then
height_max = setting
end
2014-09-28 06:30:54 -07:00
-- Parameter Ende
2014-09-30 14:02:12 -07:00
-- random generator
2014-09-27 07:25:50 -07:00
local pr
2014-09-28 10:01:27 -07:00
local pr_initialized = false
2014-09-27 07:25:50 -07:00
2016-08-11 08:44:02 -07:00
local function InitRandomizer ( seeed )
2014-09-27 07:25:50 -07:00
pr = PseudoRandom ( seeed )
pr_initialized = true
end
2016-11-12 18:11:46 -08:00
-- Checks if the mapgen is allowed to carve through this structure and only sets
-- the node if it is allowed.
local function SetNodeIfCanBuild ( pos , node )
if minetest.registered_nodes [ minetest.get_node ( pos ) . name ] . is_ground_content then
minetest.set_node ( pos , node )
return true
else
return false
end
2016-11-12 14:45:08 -08: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 )
local nodedef = minetest.registered_nodes [ minetest.get_node ( pos ) . name ]
return nodedef.is_ground_content and nodedef.walkable and nodedef.liquidtype == " none "
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 )
local nodedef = minetest.registered_nodes [ minetest.get_node ( pos ) . name ]
return nodedef.walkable and ( nodedef.node_box == nil or nodedef.node_box . type == " regular " )
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 ]
2017-05-10 14:52:54 -07:00
return nodedef.is_ground_content and nodedef.walkable == false and node2.name ~= tsm_railcorridors.nodes . dirt
2016-11-12 15:38:21 -08:00
end
2014-09-27 07:25:50 -07:00
-- Würfel…
2014-09-28 05:39:11 -07:00
-- Cube…
2016-08-11 08:44:02 -07:00
local function Cube ( p , radius , node )
2014-09-27 07:25:50 -07:00
for zi = p.z - radius , p.z + radius do
2014-09-28 10:01:27 -07:00
for yi = p.y - radius , p.y + radius do
for xi = p.x - radius , p.x + radius do
2016-11-12 18:11:46 -08:00
SetNodeIfCanBuild ( { x = xi , y = yi , z = zi } , node )
2014-09-28 10:01:27 -07:00
end
end
2014-09-27 07:25:50 -07:00
end
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
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
return " tnt:tnt " .. pr : next ( 1 , 3 )
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
2016-11-14 08:58:10 -08:00
if pr : next ( 0 , 100 ) < 3 then
return " farming:bread " .. pr : next ( 1 , 3 )
elseif pr : next ( 0 , 100 ) < 5 then
if pr : next ( 0 , 100 ) < 50 then
return " farming:seed_cotton " .. pr : next ( 1 , 5 )
2014-10-09 08:21:38 -07:00
else
2016-11-14 08:58:10 -08:00
return " farming:seed_wheat " .. pr : next ( 1 , 5 )
2014-10-09 08:21:38 -07:00
end
2016-11-14 08:58:10 -08:00
elseif pr : next ( 0 , 1000 ) < 5 then
return " tnt:tnt " .. pr : next ( 1 , 3 )
elseif pr : next ( 0 , 1000 ) < 3 then
if pr : next ( 0 , 1000 ) < 800 then
return " default:mese_crystal " .. pr : next ( 1 , 3 )
2014-10-09 08:21:38 -07:00
else
2016-11-14 08:58:10 -08:00
return " default:diamond " .. pr : next ( 1 , 3 )
2014-10-09 08:21:38 -07:00
end
2016-11-12 13:35:49 -08:00
else
return " "
2014-09-30 14:02:12 -07:00
end
end
end
-- chests
2016-08-11 08:44:02 -07:00
local function Place_Chest ( pos )
2017-05-10 14:52:54 -07:00
if SetNodeIfCanBuild ( pos , { name = tsm_railcorridors.nodes . chest } ) then
2016-11-12 18:11:46 -08:00
local meta = minetest.get_meta ( pos )
local inv = meta : get_inventory ( )
for i = 1 , 32 do
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
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-09 20:26:12 -07:00
local function corridor_part ( start_point , segment_vector , segment_count , wood , post , is_final )
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
Cube ( p , 1 , { name = " air " } )
2016-11-12 15:38:21 -08:00
-- Add wooden platform, if neccessary. To avoid floating rails
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 )
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-10 14:52:54 -07:00
SetNodeIfCanBuild ( { x = calc [ 5 ] , y = p.y + 1 , z = calc [ 6 ] } , { name = tsm_railcorridors.nodes . torch_wall , param2 = torchdir [ 1 ] } )
SetNodeIfCanBuild ( { x = calc [ 7 ] , y = p.y + 1 , z = calc [ 8 ] } , { name = tsm_railcorridors.nodes . torch_wall , param2 = torchdir [ 2 ] } )
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
2016-11-14 20:09:59 -08:00
SetNodeIfCanBuild ( pos1 , node )
if nodedef2.walkable then
pos2.y = pos2.y + 1
2016-11-12 19:16:47 -08:00
end
2016-11-14 20:09:59 -08:00
SetNodeIfCanBuild ( pos2 , node )
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
-- End of the corridor; create the final piece
if is_final then
Cube ( p , 1 , { name = " air " } )
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
2014-09-27 07:25:50 -07:00
end
2017-05-10 12:00:57 -07:00
local function corridor_func ( waypoint , coord , sign , up_or_down , up , wood , post , is_final , up_or_down_next )
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
corridor_part ( start , vek , segcount , wood , post , is_final )
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
if not up_or_down then
2016-11-14 08:42:14 -08:00
segcount = segcount * 3
2014-09-27 07:25:50 -07:00
end
2014-09-30 14:02:12 -07:00
local chestplace = - 1
2017-05-10 10:53:40 -07:00
if not up_or_down and pr : next ( ) < probability_chest then
2016-11-14 08:58:10 -08:00
chestplace = pr : next ( 1 , segcount + 1 )
2014-09-30 14:02:12 -07:00
end
2017-05-10 10:53:40 -07:00
for i = 1 , segcount do
local p = { x = waypoint.x + vek.x * i , y = waypoint.y + vek.y * i - 1 , z = waypoint.z + vek.z * i }
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 ;
end
2017-05-10 13:53:23 -07:00
if IsRailSurface ( { x = p.x , y = p.y - 1 , z = p.z } ) then
2017-05-10 14:52:54 -07:00
SetNodeIfCanBuild ( p , { name = tsm_railcorridors.nodes . rail } )
2017-05-10 10:53:40 -07:00
end
if i == chestplace then
if minetest.get_node ( { x = p.x + vek.z , y = p.y - 1 , z = p.z - vek.x } ) . name == post then
chestplace = chestplace + 1
else
Place_Chest ( { x = p.x + vek.z , y = p.y , z = p.z - vek.x } )
2014-09-30 14:02:12 -07:00
end
2014-09-27 07:25:50 -07:00
end
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-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 14:52:54 -07:00
SetNodeIfCanBuild ( { x = final_point.x , y = final_point.y - 1 , z = final_point.z } , { name = tsm_railcorridors.nodes . rail } )
2017-05-10 13:27:55 -07:00
end
end
end
return final_point
2014-09-27 07:25:50 -07:00
end
2016-11-12 16:52:53 -08:00
local function start_corridor ( waypoint , coord , sign , length , psra , wood , post )
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
2014-09-30 14:02:12 -07:00
local up
for i = 1 , length do
2017-05-10 06:58:59 -07:00
-- Up or down?
2017-05-10 12:00:57 -07:00
if udn then
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-05-10 12:00:57 -07:00
-- Update up/down next
if pr : next ( ) < probability_up_or_down and i ~= 1 and not udn then
udn = i < length
elseif udn then
udn = false
end
2014-09-30 14:02:12 -07:00
-- Make corridor / Korridor graben
2017-05-10 12:00:57 -07:00
wp = corridor_func ( wp , c , s , ud , up , wood , post , i == length , udn )
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 }
2016-11-14 08:58:10 -08:00
start_corridor ( wp , c , s , pr : next ( way_min , way_max ) , psra , wood , post )
2014-09-30 14:02:12 -07:00
if c == " x " then c = " z " else c = " x " end
2016-11-14 08:58:10 -08:00
start_corridor ( wp , c , s , pr : next ( way_min , way_max ) , psra , wood , post )
start_corridor ( wp , c , not s , pr : next ( way_min , way_max ) , psra , wood , post )
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
--[[ Starter cube: A big hollow dirt cube from which the corridors will extend.
Corridor generation starts here . ] ]
2016-11-14 08:58:10 -08:00
if pr : next ( 0 , 100 ) < 50 then
2017-05-10 14:52:54 -07:00
Cube ( main_cave_coords , 4 , { name = tsm_railcorridors.nodes . dirt } )
2014-09-30 14:02:12 -07:00
Cube ( main_cave_coords , 3 , { name = " air " } )
2017-05-10 14:52:54 -07:00
SetNodeIfCanBuild ( { x = main_cave_coords.x , y = main_cave_coords.y - 3 , z = main_cave_coords.z } , { name = tsm_railcorridors.nodes . rail } )
2014-09-30 14:02:12 -07:00
main_cave_coords.y = main_cave_coords.y - 1
else
2017-05-10 14:52:54 -07:00
Cube ( main_cave_coords , 3 , { name = tsm_railcorridors.nodes . dirt } )
2014-09-30 14:02:12 -07:00
Cube ( main_cave_coords , 2 , { name = " air " } )
2017-05-10 14:52:54 -07:00
SetNodeIfCanBuild ( { x = main_cave_coords.x , y = main_cave_coords.y - 2 , z = main_cave_coords.z } , { name = tsm_railcorridors.nodes . rail } )
2014-09-30 14:02:12 -07:00
end
2016-11-14 08:58:10 -08:00
local xs = pr : next ( 0 , 2 ) < 1
local zs = pr : next ( 0 , 2 ) < 1 ;
2016-11-12 16:52:53 -08:00
2017-05-10 14:52:54 -07:00
-- Select random wood type (found in gameconfig.lua)
2016-11-14 05:18:53 -08:00
local rnd = pr : next ( 1 , 1000 )
2016-11-12 16:52:53 -08:00
2017-05-10 14:52:54 -07:00
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
2016-11-12 16:52:53 -08:00
end
2017-05-10 14:52:54 -07:00
local wood = tsm_railcorridors.nodes . corridor_woods [ woodtype ] . wood
local post = tsm_railcorridors.nodes . corridor_woods [ woodtype ] . post
2016-11-14 08:58:10 -08:00
start_corridor ( main_cave_coords , " x " , xs , pr : next ( way_min , way_max ) , psra , wood , post )
start_corridor ( main_cave_coords , " z " , zs , pr : next ( way_min , way_max ) , psra , wood , post )
2014-09-27 07:25:50 -07:00
-- Auch mal die andere Richtung?
2014-09-28 05:39:11 -07:00
-- Try the other direction?
2016-11-14 08:58:10 -08:00
if pr : next ( 0 , 100 ) < 70 then
start_corridor ( main_cave_coords , " x " , not xs , pr : next ( way_min , way_max ) , psra , wood , post )
2014-09-27 07:25:50 -07:00
end
2016-11-14 08:58:10 -08:00
if pr : next ( 0 , 100 ) < 70 then
start_corridor ( main_cave_coords , " z " , not zs , pr : next ( way_min , way_max ) , psra , wood , post )
2014-09-27 07:25:50 -07:00
end
end
minetest.register_on_generated ( function ( minp , maxp , seed )
if not pr_initialized then
InitRandomizer ( seed )
end
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
2014-09-27 07:25:50 -07:00
-- Mittelpunkt berechnen
2014-09-28 05:39:11 -07:00
-- Mid point of the chunk
2017-05-09 16:27:42 -07:00
local y = math.floor ( math.max ( height_min , math.min ( height_max , minp.y + ( maxp.y - minp.y ) / 2 ) ) )
local p = { x = minp.x + ( maxp.x - minp.x ) / 2 , y = y , z = minp.z + ( 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 )