Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.73 KB | None | 0 0
  1. local storage = game:GetService("ServerStorage")
  2. local trees = storage["Trees"]
  3.  
  4. math.randomseed(tick())
  5.  
  6. local Base = Instance.new("Part")
  7. Base.Name = "Trunk"
  8. Base.formFactor = "Custom"
  9. Base.TopSurface = 0
  10. Base.BottomSurface = 0
  11. Base.Anchored = true
  12.  
  13. local Leaves = Base:Clone()
  14. Leaves.Name = "Leaves"
  15. Leaves.CanCollide = false
  16.  
  17. local leafmesh = Instance.new("BlockMesh")
  18. leafmesh.Parent = Leaves
  19.  
  20. function dot(c1,c2)
  21.     local m = CFrame.Angles(math.pi/2,0,0)
  22.     return (c1*m).lookVector:Dot((c2*m).lookVector)
  23. end
  24.  
  25. local leaf_mult = {
  26.     Vector3.new(.40,.40,.40);
  27.     Vector3.new(.40,.40,.40);
  28.     Vector3.new(.40,.40,.40);
  29.     Vector3.new(.40,.40,.40);
  30. }
  31.  
  32. local function Branch(base,c, material, colour)
  33.     if c <= 0 then
  34.         local leaves = Leaves:Clone()
  35.         leaves.BrickColor = colour
  36.         leaves.Material = Enum.Material[material]
  37.         local vol = (base.Size.x + base.Size.y + base.Size.z)
  38.         leaves.Mesh.Scale = leaf_mult[math.random(1, #leaf_mult)] * math.random(vol/3*10, vol/3*12) / 10
  39.         leaves.Size = leaves.Mesh.Scale*4
  40.         leaves.CFrame = base.CFrame * CFrame.new(0, base.Size.y/2, 0)
  41.         leaves.Parent = base.Parent
  42.     else
  43.         local pos = base.CFrame*CFrame.new(0, base.Size/2, 0)
  44.         local height = base.Size.y
  45.         local width = base.Size.x
  46.         local nb = math.random(2, 2)
  47.         local r = math.random(45, 135)
  48.         local da = math.random(20+55/c, 40+40/c)
  49.         local ba = math.random(-da/3, da/3)
  50.         for i=0,nb-1 do
  51.             wait()
  52.             local branch = base:Clone()
  53.             branch.Name = "Branch"
  54.             local h = height*math.random(95,115)/100
  55.             local new = branch.CFrame * CFrame.new(0,height/2,0) * CFrame.Angles(0,0,math.rad(ba))
  56.             new = new * CFrame.Angles(0, i*(math.pi*2/nb)+r, math.rad(da/2)) * CFrame.new(0, h/2, 0)
  57.             local w = dot(new, branch.CFrame) * width * 0.9
  58.             branch.Size = Vector3.new(w, h, w)
  59.             branch.CFrame = new
  60.             branch.Parent = base.Parent
  61.             Branch(branch, c-1, material, colour)
  62.         end
  63.     end
  64.     wait()
  65. end
  66.  
  67. local function generate_tree(location, complexity, width, height, material_1, material_2, colour_1, colour_2)
  68.     local tree = Instance.new("Model")
  69.     tree.Name = "Tree"
  70.     tree.archivable = false
  71.     tree.Parent = workspace
  72.     local base = Base:Clone()
  73.     base.Parent = tree
  74.     base.Size = Vector3.new(width, height, width)
  75.     base.CFrame = CFrame.new(location) * CFrame.new(0,height/2,0) * CFrame.Angles(0, math.rad(math.random(1,360)), 0)
  76.     base.BrickColor = colour_1
  77.     base.Material = Enum.Material[material_1]
  78.     Branch(base, complexity, material_2, colour_2)
  79.     local health = Instance.new("IntValue", tree)
  80.     health.Name = "HP"
  81.     health.Value = script.Parent.Health.Value
  82.     local treeType = Instance.new("StringValue", tree)
  83.     treeType.Name = "TreeType"
  84.     treeType.Value = script.Parent.TreeType.Value
  85.     local regenerationPosition = Instance.new("Vector3Value", tree)
  86.     regenerationPosition.Name = "Position"
  87.     regenerationPosition.Value = script.Parent.Position
  88.     return tree
  89. end
  90.  
  91. game.Workspace.ChildRemoved:connect(function(child)
  92.     if child:IsA("Model") and (string.lower(child.Name) == "tree") then
  93.         local newTree = trees[child.TreeType.Value]:Clone()
  94.         local c, w, h = math.max(1, newTree.Complexity.Value), math.max(1, newTree.Width.Value), math.max(1, newTree.Height.Value)
  95.         generate_tree(child.Position.Value-Vector3.new(0, newTree.Size.Y, 0), c, w, h, newTree.MaterialB.Value, newTree.MaterialL.Value, newTree.TrunkColour.Value, newTree.LeafColour.Value)
  96.     end
  97. end)
  98.  
  99. local starter_trees = game.Workspace.Starter_Trees
  100.  
  101. for i,v in pairs(starter_trees:GetChildren()) do
  102.     spawn(function()
  103.         local c, w, h = math.max(1, v.Complexity.Value), math.max(1, v.Width.Value), math.max(1, v.Height.Value)
  104.         generate_tree(v.CFrame.p-Vector3.new(0, v.Size.Y, 0), c, w, h, v.MaterialB.Value, v.MaterialL.Value, v.TrunkColour.Value, v.LeafColour.Value)
  105.     end)
  106. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement