currently i have a script (provided below unwillingly because i already know someone is going to ask for it) that populates areas with trees, it can’t be used live due to taking too long so I’m deciding to pre-generate, but it also takes a bit so ![]()
![]()
![]()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Part = ReplicatedStorage:WaitForChild("Part")
local treeSpawners = script.Parent
-- just take the tree from somewhere else
local treeTemplate = treeSpawners.Parent.Parent["the one and only"].Tree
-- for the pseduopseduorandoms
local daft = require(ReplicatedStorage:WaitForChild("daft"))
local placedTreeBounds = {}
local DEBUG = false
local enabled = false
local function worldAABB_from_bbox(cf, size)
local half = size * 0.5
local right = cf.RightVector
local up = cf.UpVector
local look = cf.LookVector
local corners = {}
for ix = -1, 1, 2 do
for iy = -1, 1, 2 do
for iz = -1, 1, 2 do
local corner = cf.Position
+ right * (ix * half.X)
+ up * (iy * half.Y)
+ look * (iz * half.Z)
table.insert(corners, corner)
end
end
end
local min = Vector3.new(corners[1].X, corners[1].Y, corners[1].Z)
local max = min
for i = 2, #corners do
local c = corners[i]
min = Vector3.new(math.min(min.X, c.X), math.min(min.Y, c.Y), math.min(min.Z, c.Z))
max = Vector3.new(math.max(max.X, c.X), math.max(max.Y, c.Y), math.max(max.Z, c.Z))
end
return { Min = min, Max = max }
end
local function isOverlapping(a, b, ignoreY)
if ignoreY == nil then ignoreY = true end
local minA, maxA = a.Min, a.Max
local minB, maxB = b.Min, b.Max
if DEBUG then
print("Compare A.Min", minA, "A.Max", maxA)
print(" B.Min", minB, "B.Max", maxB)
end
if ignoreY then
local separated = (maxA.X < minB.X) or (minA.X > maxB.X) or
(maxA.Z < minB.Z) or (minA.Z > maxB.Z)
if DEBUG then print("Separated XZ?", separated) end
return not separated
else
local separated = (maxA.X < minB.X) or (minA.X > maxB.X) or
(maxA.Y < minB.Y) or (minA.Y > maxB.Y) or
(maxA.Z < minB.Z) or (minA.Z > maxB.Z)
if DEBUG then print("Separated XYZ?", separated) end
return not separated
end
end
local spawnerParts = {}
for _, child in ipairs(treeSpawners:GetChildren()) do
if child:IsA("BasePart") and not script:FindFirstAncestor("Trees") then
table.insert(spawnerParts, child)
end
end
if #spawnerParts == 0 then
warn("No spawner parts found")
return
end
local treeCount = math.huge
local maxTry = 1000
local placed = 0
local tries = 0
while placed < treeCount and tries < maxTry and enabled do
local spawner = spawnerParts[math.random(1, #spawnerParts)]
if spawner then
if tries > 10 and tries < 143 then
if math.random(1, 10) == 1 then
task.wait()
end
end
if tries > 143 then
task.wait()
end
local tree = treeTemplate:Clone()
tree.Parent = workspace
-- place it at the spawner
local min = spawner.Position - (0.5 * spawner.Size)
local max = spawner.Position + (0.5 * spawner.Size)
-- decide which random to gun for, some can only be chosen if tries aren't low enough due to their more... static nature
local randomx
local randomz
local minX = math.floor(min.X)
local maxX = math.floor(max.X)
local minZ = math.floor(min.Z)
local maxZ = math.floor(max.Z)
local randomizer = math.random(4)
if randomizer == 4 and tries < 4 then
randomx = daft:timerand(minX, maxX)
randomz = daft:timerand(minZ, maxZ)
elseif randomizer == 3 and tries < 3 then
randomx = daft:currentidrand(minX, maxX)
randomz = daft:currentidrand(minZ, maxZ)
elseif randomizer == 2 then
randomx = daft:statsrand(minX, maxX)
randomz = daft:statsrand(minZ, maxZ)
else
randomx = math.random(min.X, max.X)
randomz = math.random(minZ, maxZ)
end
tree:PivotTo(CFrame.new(randomx, 10001, randomz))
local cf, size = tree:GetBoundingBox()
local candidateBounds = worldAABB_from_bbox(cf, size)
if DEBUG then
print("--- candidate ---")
print("center", cf.Position, "size", size)
print("min", candidateBounds.Min, "max", candidateBounds.Max)
end
local overlaps = false
for _, bounds in ipairs(placedTreeBounds) do
if isOverlapping(candidateBounds, bounds, true) then
overlaps = true
if DEBUG then
print("Rejected: overlaps with a previous tree")
end
break
end
end
if overlaps then
tries = tries + 1
tree:Destroy()
else
table.insert(placedTreeBounds, candidateBounds)
placed = placed + 1
tries = 0
if DEBUG then print("Placed tree #", placed) end
end
end
end
if DEBUG then
print(("Finished: placed %d trees (tries %d)"):format(placed, tries))
end
for i, v in spawnerParts do
v.Color = Color3.fromRGB(210, 255, 221)
end
script:Destroy()