I’m trying to make a cave generation system for my voxel based mining game and I found this uncopylocked game with working code for cave generation, and I have 0 clue on how I can use this sorta system on my own block generation system.
This is my block generation system
local function determineBlock(depth, luck, multiplier, isReset)
local ore = nil
if depth == 0 then
local random = math.random(1, #OreTable[1])
local willSpawn = nil
if luck <= 0 then
willSpawn = math.random(1, OreTable[1][random].chance)
else
willSpawn = math.random(1, luck)
end
if willSpawn == 1 then
ore = OreTable[1][random].oBlock:Clone()
if OreTable[1][random].chance >= 1000000 then
ReplicatedStorage.EventText:FireAllClients("Mythical")
elseif OreTable[1][random].chance >= 10000000 then
ReplicatedStorage.EventText:FireAllClients("Celestial")
elseif OreTable[1][random].chance >= 50000000 then
ReplicatedStorage.EventText:FireAllClients("Godly")
end
if OreTable[1][random].oBlock.Name == "Turbonite" then
ReplicatedStorage.EventText:FireAllClients("Turbonite")
elseif OreTable[1][random].oBlock.Name == "Quintessence Quasar" then
ReplicatedStorage.EventText:FireAllClients("QuintessenceQuasar")
end
return ore
else
ore = Layers[1]:Clone()
return ore
end
else
local random = math.random(1, #OreTable[math.ceil(depth / 1000)])
local willSpawn = nil
if luck <= 0 then
willSpawn = math.random(1, OreTable[math.ceil(depth / 1000)][random].chance)
else
willSpawn = math.random(1, luck)
end
if willSpawn == 1 then
ore = OreTable[math.ceil(depth / 1000)][random].oBlock:Clone()
if OreTable[math.ceil(depth / 1000)][random].chance >= 1000000 then
ReplicatedStorage.EventText:FireAllClients("Mythical")
elseif OreTable[math.ceil(depth / 1000)][random].chance >= 10000000 then
ReplicatedStorage.EventText:FireAllClients("Celestial")
elseif OreTable[math.ceil(depth / 1000)][random].chance >= 50000000 then
ReplicatedStorage.EventText:FireAllClients("Godly")
end
if OreTable[math.ceil(depth / 1000)][random].oBlock.Name == "Turbonite" then
ReplicatedStorage.EventText:FireAllClients("Turbonite")
elseif OreTable[math.ceil(depth / 1000)][random].oBlock.Name == "Quintessence Quasar" then
ReplicatedStorage.EventText:FireAllClients("QuintessenceQuasar")
end
return ore
else
ore = Layers[math.ceil(depth / 1000)]:Clone()
return ore
end
end
end
local function generateOres(position, depth, luck, multiplier, isReset)
if not position then
return
end
local generated = workspace.Generated
for _, offset in ipairs(POSITIONS) do
local newPosition = position + offset
if newPosition.Y > 0 then
continue
end
if generated:FindFirstChild(tostring(newPosition)) then
continue
end
local ore = determineBlock(depth, luck, multiplier, isReset)
ore.Position = newPosition
ore.Parent = workspace.Cubes
local value = Instance.new("Vector3Value")
value.Value = newPosition
value.Name = tostring(newPosition)
value.Parent = generated
end
end
and this is the uncopylocked game’s block + cave generation system (its a module)
local Ore = {}
Ore.Info = {
["Stone"] = {
Cash = 1;
Health = 3;
Depths = {
Min = 0;
Max = 100;
};
Rarity = 0;
};
["Copper"] = {
Cash = 3;
Health = 5;
Depths = {
Min = 5;
Max = 100;
};
Rarity = 950;
};
["Demon"] = {
Cash = 3;
Health = 5;
Depths = {
Min = 101;
Max = 10000000;
};
Rarity = 0;
};
};
local Origin = CFrame.new(0,10,0)
local SavedPositions = {}
math.randomseed(tick())
local sortedpairs=function(t,order)
local keys = {}
for k in pairs(t) do keys[#keys+1] = k end
if order then
table.sort(keys, function(a,b) return order(t, a, b) end)
else
table.sort(keys)
end
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
function PickOre(layer)
local gotOres = {}
local Y = math.abs((layer.Y-Origin.Y)/6)
for i,v in pairs(Ore.Info) do
if Y >= v.Depths.Min and Y <= v.Depths.Max then
gotOres[i] = v.Rarity
end
end
local Rarity = math.random(0,1000)
local GotFirstNumber
local Sames = {}
for i,v in sortedpairs(gotOres, function(t,a,b) return t[b] < t[a] end) do
if v <= Rarity then
GotFirstNumber = v
for ii,vv in pairs(gotOres) do
if vv == GotFirstNumber then
Sames[#Sames+1] = ii
end
end
break
end
end
local GetFinalOre = Sames[math.random(#Sames)]
local MakeOre = game.ServerStorage.OresFolder:FindFirstChild(GetFinalOre):Clone()
return MakeOre
end
local CaveResolution = 60 --50
local Thrash = 0.4 --0.49
function GenerateOre(x,y,z,mining)
local gotPos = CFrame.new(x*6,y*6,z*6)
if mining and (mining * gotPos).Y > Origin.Y then return end
if mining and SavedPositions[tostring((mining * gotPos).p)] then return end
local oCF = (mining and (mining * gotPos)) or (Origin * gotPos)
local thingy = (mining and (mining * gotPos)) or (Origin * gotPos)
local ore = PickOre(thingy)
local Mineable = Instance.new("BoolValue")
Mineable.Name = "CanMine"
Mineable.Value = true
print(oCF)
ore.CFrame = oCF
if mining and (mining * gotPos).Y >= Origin.Y then
Mineable.Value = false
end
SavedPositions[tostring(ore.Position)] = true
Mineable.Parent = ore
ore.Parent = workspace.Ores
local m = math.noise((ore.CFrame.X+math.random())/CaveResolution,ore.CFrame.Y/CaveResolution,ore.CFrame.Z/CaveResolution)
if m > Thrash and ore.CFrame.Y < Origin.Y then
Ore.MineOre(nil,ore)
end
end
function Ore.MineOre(plr,target)
for x = -1,1 do
GenerateOre(x,0,0,target.CFrame)
end
for y = -1,1 do
GenerateOre(0,y,0,target.CFrame)
end
for z = -1,1 do
GenerateOre(0,0,z,target.CFrame)
end
if plr and _G.PlayerData[plr.Name] then
if _G.PlayerData[plr.Name].Ores[target.Name] then
_G.PlayerData[plr.Name].Ores[target.Name] = _G.PlayerData[plr.Name].Ores[target.Name] + 1
else
_G.PlayerData[plr.Name].Ores[target.Name] = 1
end
end
target:Destroy()
end
function Ore.StartMine()
workspace.Ores:ClearAllChildren()
SavedPositions = {}
for x = 1,16 do
for z = 1,16 do
GenerateOre(x,0,z)
end
end
end
return Ore
I’ll keep looking into the code myself but if anyone could help me with this I would greatly appreciate it.