Simon_34545

generation

Sep 1st, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.78 KB | None | 0 0
  1. -- at the time of writing these comments the game is availible to copy, if you use it please credit me
  2. -- i might make this unavailible at anytime so...
  3.  
  4. local x = 0 -- counter for how many blocks generated
  5. local z = 0 -- z position of currently generating blocks - gets overwritten
  6. local block = nil -- currently generating block - gets overwritten
  7. local seed = 1 -- generation seed - gets overwritten by player
  8. local y = 0 -- y position of currently generating blocks - gets overwritten
  9. local chunksize = 16 -- size of chunks that are generated - im not sure how changing this affects generation
  10. local size = 69 -- smoothness of terrain - idk why i called it size
  11. local xpos = 0 -- x position of currently generating blocks - gets overwritten
  12. local lcount = 0 -- amount of rows of blocks generated - gets overwritten
  13. local amplifier = 5 -- amplifier for terrain - high amiplifier + high smoothness = giant hills - low amplifier + low smoothness = tiny spiky mountains - gets overwritten by player
  14. local chunkmodel = nil -- the currently generating chunk's model - gets overwritten
  15. local genxpos = 0 -- i think this went unused
  16. local genzpos = 0 -- i think this went unused
  17. local isfirstblock = true -- meant to set the chunks "position" so i could use render distance but it made it laggier than without render distance so i might add fog like minecraft 0.0.23a_01
  18. local chunkrowx = 0 -- i think this went unused
  19. local chunkrowz = 0 -- i think this went unused
  20. local mapsizechunk = 4 -- the dimensions of the map in chunks - gets overwritten by player
  21. local rowchunkcount = 0 -- the amount of chunks that have been generated in a row of chunks - gets overwritten
  22. local chunkrowcount = 0 -- the amount of rows of chunks that have been generated
  23. local topwoodblock = Vector3.new(0, 0, 0) -- the position highest block of wood of wood generated used to reference where to generate leaves - gets overwritten
  24. local treeblockamount = 0 -- the amount of leaves or logs generated - used for determining if dirt is finished generating
  25. local texture = nil -- the texture being added the the currently generating block
  26.  
  27. function leaves(wood, model, x, y, z)
  28.     local leaves = Instance.new("Part", model)
  29.     leaves.Name = "Leaves"
  30.     leaves.Size = Vector3.new(2.5, 2.5, 2.5)
  31.     leaves.Anchored = true
  32.     leaves.Transparency = 1
  33.     texture = Instance.new("Decal", leaves)
  34.     texture.Texture = "http://www.roblox.com/asset/?id=5633520791"
  35.     texture.Face = "Left"
  36.     texture = Instance.new("Decal", leaves)
  37.     texture.Texture = "http://www.roblox.com/asset/?id=5633520791"
  38.     texture.Face = "Right"
  39.     texture = Instance.new("Decal", leaves)
  40.     texture.Texture = "http://www.roblox.com/asset/?id=5633520791"
  41.     texture.Face = "Front"
  42.     texture = Instance.new("Decal", leaves)
  43.     texture.Texture = "http://www.roblox.com/asset/?id=5633520791"
  44.     texture.Face = "Back"
  45.     texture = Instance.new("Decal", leaves)
  46.     texture.Texture = "http://www.roblox.com/asset/?id=5633520791"
  47.     texture.Face = "Bottom"
  48.     texture = Instance.new("Decal", leaves)
  49.     texture.Texture = "http://www.roblox.com/asset/?id=5633520791"
  50.     texture.Face = "Top"
  51.     leaves.Position = wood + Vector3.new(x, y, z)
  52. end
  53.  
  54. function genlayer(cmodel)
  55.     while x < chunksize do
  56.         -- create the block
  57.         block = Instance.new("Part")
  58.         block.Size = Vector3.new(2.5, 2.5, 2.5)
  59.         block.Anchored = true
  60.         texture = Instance.new("Decal", block)
  61.         texture.Texture = "rbxgameasset://Images/grass_dirt"
  62.         texture.Face = "Left"
  63.         texture = Instance.new("Decal", block)
  64.         texture.Texture = "rbxgameasset://Images/grass_dirt"
  65.         texture.Face = "Right"
  66.         texture = Instance.new("Decal", block)
  67.         texture.Texture = "rbxgameasset://Images/grass_dirt"
  68.         texture.Face = "Front"
  69.         texture = Instance.new("Decal", block)
  70.         texture.Texture = "rbxgameasset://Images/grass_dirt"
  71.         texture.Face = "Back"
  72.         texture = Instance.new("Decal", block)
  73.         texture.Texture = "rbxgameasset://Images/dirt (1)"
  74.         texture.Face = "Bottom"
  75.         texture = Instance.new("Decal", block)
  76.         texture.Texture = "rbxgameasset://Images/grass"
  77.         texture.Face = "Top"
  78.         -- if its the first block, then name it chunk position
  79.         if isfirstblock == true then
  80.             block.Name = "ChunkPos"
  81.             isfirstblock = false
  82.         end
  83.         -- find the y value of the block using math.noise
  84.         y = math.floor(math.noise(xpos / size, z / size, seed) * amplifier)
  85.         y = 2.5 * y
  86.         y = 80 + y
  87.         --print(y)
  88.         block.Position = Vector3.new(xpos, y, z)
  89.         z = z + 2.5
  90.         block.Parent = cmodel
  91.        
  92.         x = x + 1
  93.     end
  94. end
  95. function genchunk(chunkzpos)
  96.     chunkmodel = Instance.new("Model")
  97.     --chunkmodel.Parent = game.ServerStorage.Folder
  98.     chunkmodel.Parent = workspace.GeneratedTerrain
  99.     local chunkseed = Instance.new("IntValue", chunkmodel)
  100.     chunkseed.Value = seed * ((rowchunkcount + chunkrowcount) + (rowchunkcount * chunkrowcount))
  101.     print(chunkseed.Value)
  102.     while lcount < chunksize do
  103.         z = chunkzpos
  104.         x = 0
  105.         genlayer(chunkmodel)
  106.         lcount = lcount + 1
  107.         xpos = xpos + 2.5
  108.     end
  109.     isfirstblock = true
  110.     wait()
  111. end
  112. function genchunkrow(rowx)
  113.     while rowchunkcount < mapsizechunk do
  114.         xpos = rowx
  115.         lcount = 0
  116.         genchunk(z)
  117.         rowchunkcount = rowchunkcount + 1
  118.     end
  119. end
  120. function coalthingy(coal)
  121.     for i,x in pairs(coal:GetTouchingParts()) do
  122.         if math.random(1,3) == 2 and x.Name == "Stone" then
  123.             for i,v in pairs(x:GetChilden()) do
  124.                 if not v:IsA("TouchTransmitter") then
  125.                     v.Texture = "rbxgameasset://Images/rock_coal"
  126.                 end
  127.             end
  128.             if math.random(1,3) == 3 then
  129.                 coalthingy(x)
  130.             end
  131.         end
  132.     end
  133. end
  134. function generate()
  135.     local hint = Instance.new("Hint", workspace)
  136.     hint.Text = "Generating grass and trees"
  137.     -- set values
  138.     workspace.AllowRenderDistance.Value = false
  139.     amplifier = workspace.Amplifier.Value
  140.     size = workspace.Size.Value
  141.     seed = workspace.Seed.Value
  142.     math.randomseed(seed)
  143.     mapsizechunk = workspace.MapSize.Value
  144.     workspace.RandomSeed.Value = seed
  145.     treeblockamount = 0
  146.     --wait(2)
  147.     -- clear terrain
  148.     for i,v in pairs(workspace.GeneratedTerrain:GetChildren()) do
  149.         v:Destroy()
  150.         wait(0.1)
  151.     end
  152.     -- generate
  153.     while chunkrowcount < mapsizechunk do
  154.         z = 0
  155.         genchunkrow(xpos)
  156.         chunkrowcount = chunkrowcount + 1
  157.         rowchunkcount = 0
  158.     end
  159.     --repeat wait() until #game.ServerStorage.Folder:GetChildren() == mapsizechunk * mapsizechunk
  160.     --for i,v in pairs(game.ServerStorage.Folder:GetChildren()) do
  161.     --  v.Parent = workspace.GeneratedTerrain
  162.     --end
  163.     repeat wait() until #workspace.GeneratedTerrain:GetChildren() == mapsizechunk * mapsizechunk
  164.     hint.Text = "Generating dirt and stone"
  165.     wait(5)
  166.     for i,v in pairs(workspace.GeneratedTerrain:GetDescendants()) do
  167.         if v.Name == "Part" or v.Name == "ChunkPos" then
  168.             local dirt = Instance.new("Part", v.Parent)
  169.             dirt.Size = Vector3.new(2.5, 2.5, 2.5)
  170.             dirt.Anchored = true
  171.             dirt.Name = "Dirt"
  172.             dirt.Position = v.Position - Vector3.new(0, 2.5, 0)
  173.             texture = Instance.new("Decal", dirt)
  174.             texture.Texture = "rbxgameasset://Images/dirt (1)"
  175.             texture.Face = "Left"
  176.             texture = Instance.new("Decal", dirt)
  177.             texture.Texture = "rbxgameasset://Images/dirt (1)"
  178.             texture.Face = "Right"
  179.             texture = Instance.new("Decal", dirt)
  180.             texture.Texture = "rbxgameasset://Images/dirt (1)"
  181.             texture.Face = "Front"
  182.             texture = Instance.new("Decal", dirt)
  183.             texture.Texture = "rbxgameasset://Images/dirt (1)"
  184.             texture.Face = "Back"
  185.             texture = Instance.new("Decal", dirt)
  186.             texture.Texture = "rbxgameasset://Images/dirt (1)"
  187.             texture.Face = "Bottom"
  188.             texture = Instance.new("Decal", dirt)
  189.             texture.Texture = "rbxgameasset://Images/dirt (1)"
  190.             texture.Face = "Top"
  191.             dirt = Instance.new("Part", v.Parent)
  192.             dirt.Name = "Dirt"
  193.             dirt.Size = Vector3.new(2.5, 2.5, 2.5)
  194.             dirt.Anchored = true
  195.             dirt.Position = v.Position - Vector3.new(0, 5, 0)
  196.             texture = Instance.new("Decal", dirt)
  197.             texture.Texture = "rbxgameasset://Images/dirt (1)"
  198.             texture.Face = "Left"
  199.             texture = Instance.new("Decal", dirt)
  200.             texture.Texture = "rbxgameasset://Images/dirt (1)"
  201.             texture.Face = "Right"
  202.             texture = Instance.new("Decal", dirt)
  203.             texture.Texture = "rbxgameasset://Images/dirt (1)"
  204.             texture.Face = "Front"
  205.             texture = Instance.new("Decal", dirt)
  206.             texture.Texture = "rbxgameasset://Images/dirt (1)"
  207.             texture.Face = "Back"
  208.             texture = Instance.new("Decal", dirt)
  209.             texture.Texture = "rbxgameasset://Images/dirt (1)"
  210.             texture.Face = "Bottom"
  211.             texture = Instance.new("Decal", dirt)
  212.             texture.Texture = "rbxgameasset://Images/dirt (1)"
  213.             texture.Face = "Top"
  214.             spawn(function()
  215.                 local generated = false
  216.                 local stone = nil
  217.                 local pos = dirt.Position.Y
  218.                 generated = false
  219.                 while pos > (workspace.Stone.Value * 2.5) + 2.5 do
  220.                     generated = true
  221.                     pos = pos - 2.5
  222.                     stone = Instance.new("Part", v.Parent)
  223.                     stone.Size = Vector3.new(2.5, 2.5, 2.5)
  224.                     stone.Anchored = true
  225.                     stone.Name = "Stone"
  226.                     texture = Instance.new("Decal", stone)
  227.                     texture.Texture = "rbxgameasset://Images/stone (1)"
  228.                     texture.Face = "Left"
  229.                     texture = Instance.new("Decal", stone)
  230.                     texture.Texture = "rbxgameasset://Images/stone (1)"
  231.                     texture.Face = "Right"
  232.                     texture = Instance.new("Decal", stone)
  233.                     texture.Texture = "rbxgameasset://Images/stone (1)"
  234.                     texture.Face = "Front"
  235.                     texture = Instance.new("Decal", stone)
  236.                     texture.Texture = "rbxgameasset://Images/stone (1)"
  237.                     texture.Face = "Back"
  238.                     texture = Instance.new("Decal", stone)
  239.                     texture.Texture = "rbxgameasset://Images/stone (1)"
  240.                     texture.Face = "Bottom"
  241.                     texture = Instance.new("Decal", stone)
  242.                     texture.Texture = "rbxgameasset://Images/stone (1)"
  243.                     texture.Face = "Top"
  244.                     stone.Position = Vector3.new(dirt.Position.X, pos, dirt.Position.Z)
  245.                     local touched = stone.Touched:Connect(function() end)
  246.                     touched:Disconnect()
  247.                 end
  248.                 if generated == true then
  249.                     stone.Name = "Bedrock"
  250.                     for i,x in pairs(stone:GetChildren()) do
  251.                         if x:IsA("Decal") then
  252.                             x.Texture = "http://www.roblox.com/asset/?id=5634415312"
  253.                         end
  254.                     end
  255.                 end
  256.             end)
  257.         end
  258.     end
  259.     --repeat wait() until (#workspace.GeneratedTerrain:GetDescendants() - (mapsizechunk * mapsizechunk) - treeblockamount) == (((mapsizechunk * 16) * (mapsizechunk * 16)) * 3)
  260.     wait(5)
  261.     hint.Text = "Generating trees"
  262.     for i,v in pairs(workspace.GeneratedTerrain:GetDescendants()) do
  263.         if v.Name == "Part" then
  264.             if seed ~= v.Parent.Value.Value then
  265.                 seed = v.Parent.Value.Value
  266.                 print(v.Parent.Value.Value)
  267.                 math.randomseed(seed)
  268.             end
  269.             if math.random(1, 256) == 69 then -- 1 in 256 chance of generating a tree, considering a chunk is 16x16 blocks and 16x16 = 256 theres is on average 1 tree per chunk
  270.                 local log = game.ServerStorage.Log:Clone()
  271.                 log.Position = v.Position + Vector3.new(0, 2.5, 0)
  272.                 log.Parent = v.Parent
  273.                 log = game.ServerStorage.Log:Clone()
  274.                 log.Position = v.Position + Vector3.new(0, 5, 0)
  275.                 log.Parent = v.Parent
  276.                 log = game.ServerStorage.Log:Clone()
  277.                 log.Position = v.Position + Vector3.new(0, 7.5, 0)
  278.                 log.Parent = v.Parent
  279.                 log = game.ServerStorage.Log:Clone()
  280.                 log.Position = v.Position + Vector3.new(0, 10, 0)
  281.                 log.Parent = v.Parent
  282.                 treeblockamount = treeblockamount + 4
  283.                 if math.random(1,3) == 1 then -- 1 in 3 chance of the tree being five blocks tall
  284.                     topwoodblock = v.Position + Vector3.new(0, 12.5, 0)
  285.                     log = game.ServerStorage.Log:Clone()
  286.                     log.Position = v.Position + Vector3.new(0, 12.5, 0)
  287.                     log.Parent = v.Parent
  288.                     treeblockamount = treeblockamount + 1
  289.                     if math.random(1,2) == 2 then -- if is a five block tall, 50/50 chance of being 6 blocks tall
  290.                         log = game.ServerStorage.Log:Clone()
  291.                         log.Position = v.Position + Vector3.new(0, 15, 0)
  292.                         log.Parent = v.Parent
  293.                         topwoodblock = v.Position + Vector3.new(0, 15, 0)
  294.                         treeblockamount = treeblockamount + 1
  295.                     end
  296.                 else
  297.                     topwoodblock = v.Position + Vector3.new(0, 10, 0)
  298.                 end
  299.                 wait(0.1)
  300.                 leaves(topwoodblock, v.Parent, 0, 2.5, 0)
  301.                 leaves(topwoodblock, v.Parent, 2.5, 2.5, 0)
  302.                 leaves(topwoodblock, v.Parent, 0, 2.5, 2.5)
  303.                 leaves(topwoodblock, v.Parent, -2.5, 2.5, 0)
  304.                 leaves(topwoodblock, v.Parent, 0, 2.5, -2.5)
  305.                 leaves(topwoodblock, v.Parent, 2.5, 0, 0)
  306.                 leaves(topwoodblock, v.Parent, 0, 0, 2.5)
  307.                 leaves(topwoodblock, v.Parent, -2.5, 0, 0)
  308.                 leaves(topwoodblock, v.Parent, 0, 0, -2.5)
  309.                 treeblockamount = treeblockamount + 9
  310.                 if math.random(1,4) == 1 then
  311.                     leaves(topwoodblock, v.Parent, 2.5, 0, -2.5)
  312.                     treeblockamount = treeblockamount + 1
  313.                 end
  314.                 if math.random(1,4) == 2 then
  315.                     leaves(topwoodblock, v.Parent, -2.5, 0, 2.5)
  316.                     treeblockamount = treeblockamount + 1
  317.                 end
  318.                 if math.random(1,4) == 3 then
  319.                     leaves(topwoodblock, v.Parent, -2.5, 0, -2.5)
  320.                     treeblockamount = treeblockamount + 1
  321.                 end
  322.                 if math.random(1,4) == 4 then
  323.                     leaves(topwoodblock, v.Parent, 2.5, 0, 2.5)
  324.                     treeblockamount = treeblockamount + 1
  325.                 end
  326.                 --upper ring
  327.                 leaves(topwoodblock, v.Parent, -2.5, -2.5, 2.5)
  328.                 leaves(topwoodblock, v.Parent, 2.5, -2.5, -2.5)
  329.                 leaves(topwoodblock, v.Parent, 2.5, -2.5, 2.5)
  330.                 leaves(topwoodblock, v.Parent, -2.5, -2.5, -2.5)
  331.                 leaves(topwoodblock, v.Parent, 0, -2.5, 2.5)
  332.                 leaves(topwoodblock, v.Parent, 2.5, -2.5, 0)
  333.                 leaves(topwoodblock, v.Parent, 0, -2.5, -2.5)
  334.                 leaves(topwoodblock, v.Parent, -2.5, -2.5, 0)
  335.                 -- lower ring
  336.                 leaves(topwoodblock, v.Parent, -2.5, -5, 2.5)
  337.                 leaves(topwoodblock, v.Parent, 2.5, -5, -2.5)
  338.                 leaves(topwoodblock, v.Parent, 2.5, -5, 2.5)
  339.                 leaves(topwoodblock, v.Parent, -2.5, -5, -2.5)
  340.                 leaves(topwoodblock, v.Parent, 0, -5, 2.5)
  341.                 leaves(topwoodblock, v.Parent, 2.5, -5, 0)
  342.                 leaves(topwoodblock, v.Parent, 0, -5, -2.5)
  343.                 leaves(topwoodblock, v.Parent, -2.5, -5, 0)
  344.                 -- upper outer ring
  345.                 leaves(topwoodblock, v.Parent, 5, -2.5, 2.5)
  346.                 leaves(topwoodblock, v.Parent, 5, -2.5, -2.5)
  347.                 leaves(topwoodblock, v.Parent, -5, -2.5, 2.5)
  348.                 leaves(topwoodblock, v.Parent, -5, -2.5, -2.5)
  349.                 leaves(topwoodblock, v.Parent, 2.5, -2.5, 5)
  350.                 leaves(topwoodblock, v.Parent, -2.5, -2.5, 5)
  351.                 leaves(topwoodblock, v.Parent, 2.5, -2.5, -5)
  352.                 leaves(topwoodblock, v.Parent, -2.5, -2.5, -5)
  353.                 leaves(topwoodblock, v.Parent, 0, -2.5, 5)
  354.                 leaves(topwoodblock, v.Parent, 5, -2.5, 0)
  355.                 leaves(topwoodblock, v.Parent, 0, -2.5, -5)
  356.                 leaves(topwoodblock, v.Parent, -5, -2.5, 0)
  357.                 -- lower outer ring
  358.                 leaves(topwoodblock, v.Parent, 5, -5, 2.5)
  359.                 leaves(topwoodblock, v.Parent, 5, -5, -2.5)
  360.                 leaves(topwoodblock, v.Parent, -5, -5, 2.5)
  361.                 leaves(topwoodblock, v.Parent, -5, -5, -2.5)
  362.                 leaves(topwoodblock, v.Parent, 2.5, -5, 5)
  363.                 leaves(topwoodblock, v.Parent, -2.5, -5, 5)
  364.                 leaves(topwoodblock, v.Parent, 2.5, -5, -5)
  365.                 leaves(topwoodblock, v.Parent, -2.5, -5, -5)
  366.                 leaves(topwoodblock, v.Parent, 0, -5, 5)
  367.                 leaves(topwoodblock, v.Parent, 5, -5, 0)
  368.                 leaves(topwoodblock, v.Parent, 0, -5, -5)
  369.                 leaves(topwoodblock, v.Parent, -5, -5, 0)
  370.                 treeblockamount = treeblockamount + 40
  371.                 -- corners
  372.                 if math.random(1,3) == 1 then
  373.                     leaves(topwoodblock, v.Parent, -5, -2.5, 5)
  374.                     treeblockamount = treeblockamount + 1
  375.                 end
  376.                 if math.random(1,3) == 2 then
  377.                     leaves(topwoodblock, v.Parent, 5, -2.5, -5)
  378.                     treeblockamount = treeblockamount + 1
  379.                 end
  380.                 if math.random(1,3) == 3 then
  381.                     leaves(topwoodblock, v.Parent, -5, -2.5, -5)
  382.                     treeblockamount = treeblockamount + 1
  383.                 end
  384.                 if math.random(1,3) == 1 then
  385.                     leaves(topwoodblock, v.Parent, 5, -2.5, 5)
  386.                     treeblockamount = treeblockamount + 1
  387.                 end
  388.                 if math.random(1,3) == 1 then
  389.                     leaves(topwoodblock, v.Parent, -5, -5, 5)
  390.                     treeblockamount = treeblockamount + 1
  391.                 end
  392.                 if math.random(1,3) == 2 then
  393.                     leaves(topwoodblock, v.Parent, 5, -5, -5)
  394.                     treeblockamount = treeblockamount + 1
  395.                 end
  396.                 if math.random(1,3) == 3 then
  397.                     leaves(topwoodblock, v.Parent, -5, -5, -5)
  398.                     treeblockamount = treeblockamount + 1
  399.                 end
  400.                 if math.random(1,3) == 1 then
  401.                     leaves(topwoodblock, v.Parent, 5, -5, 5)
  402.                     treeblockamount = treeblockamount + 1
  403.                 end
  404.             end
  405.         end
  406.     end
  407.     hint.Text = "Generating coal."
  408.     print("ok")
  409.     wait(1)
  410.     for i,v in pairs(workspace.GeneratedTerrain:GetDescendants()) do
  411.         if v.Name == "Stone" then
  412.             if seed ~= v.Parent.Value.Value then
  413.                 seed = v.Parent.Value.Value
  414.                 print(v.Parent.Value.Value)
  415.                 math.randomseed(seed)
  416.             end
  417.             if math.random(1, 512) == 1 then
  418.                 v.Name = "RockCoal"
  419.                 for i,x in pairs(v:GetChildren()) do
  420.                     if not v:IsA("TouchTransmitter") then
  421.                         x.Texture = "rbxgameasset://Images/rock_coal"
  422.                     end
  423.                 end
  424.                 coalthingy(v)
  425.             end
  426.         end
  427.     end
  428.     hint.Text = "Generating iron."
  429.     hint.Text = "Generating gold."
  430.     hint.Text = "Done."
  431.     -- reset values
  432.     chunkrowcount = 0
  433.     z = 0
  434.     x = 0
  435.     xpos = 0
  436.     lcount = 0
  437.     wait(5)
  438.     hint:Destroy()
  439. end
  440. workspace.Generate.Head.ClickDetector.MouseClick:Connect(generate)
Add Comment
Please, Sign In to add comment