Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --!native
- local perlin = require(game:GetService("ServerStorage").noise)
- local players = game:GetService("Players")
- local rf = game:GetService("ReplicatedStorage")
- local seed = perlin.calcseed("xxxThebestxThe_2nd") -- text doesnt mean anything past a seed
- local worldmap = perlin.createNDarray(3)
- local x, z, size = 1024, 1024, 5 -- is this for 8 chunks, 16 chunks give or take
- local processing = false
- local packetCache = {}
- local function setBlock(x, y, z, prop, addprop, divsize, sendpacket)
- if sendpacket == false then
- sendpacket = false
- else
- sendpacket = true
- end
- local parttype
- if prop["ClassName"] then
- parttype = prop["ClassName"]
- prop["ClassName"] = nil
- end
- local instance = {}
- instance.Parent = workspace.worldmap
- instance.Size = Vector3.new(size, size, size)
- instance.CFrame = CFrame.new(x, y, z)
- instance.Color = Color3.new(0, 0, 0)
- instance.Material = Enum.Material.SmoothPlastic
- instance.CastShadow = false
- instance.Anchored = true
- for k, v in pairs(prop) do
- instance[k] = v
- end
- if addprop then
- for k, v in pairs(addprop) do
- if typeof(v) == "CFrame" then
- instance[k] = instance[k] * v
- else
- instance[k] = instance[k] + v
- end
- end
- end
- if divsize then
- worldmap[x / size][y][z / size] = instance
- else
- worldmap[x][y][z] = instance
- end
- if sendpacket then
- local buf = buffer.create(112 / 8) -- (u16,f32,u16) (u8,u8,u8) (u8,u8,u8)
- buffer.writeu16(buf, 0, x)
- buffer.writef32(buf, 16 / 8, y)
- buffer.writeu16(buf, 48 / 8, z)
- buffer.writeu8(buf, 64 / 8, instance.Size.X)
- buffer.writeu8(buf, 72 / 8, instance.Size.Y)
- buffer.writeu8(buf, 80 / 8, instance.Size.Z)
- buffer.writeu8(buf, 88 / 8, math.round(instance.Color.R * 255))
- buffer.writeu8(buf, 96 / 8, math.round(instance.Color.G * 255))
- buffer.writeu8(buf, 104 / 8, math.round(instance.Color.B * 255))
- rf.RenderEvent:FireAllClients(buf, parttype)
- table.insert(packetCache, { packetType = "blockRender", ["buffer"] = buf, partType = parttype })
- end
- end
- local features = {
- ["random-orange"] = { -- min y, max y, max per chunk, rarity, properties, addprop
- 50 + 10,
- 100 + 10,
- 1,
- 500,
- {
- Color = Color3.new(1, 0.756008, 0.322225),
- Size = Vector3.new(size, size * 5, size),
- },
- {
- CFrame = CFrame.new(0, size * 2, 0),
- },
- },
- ["spawnpoint"] = {
- 20 + 10,
- 70 + 10,
- 1,
- 1250,
- {
- Color = Color3.new(0.0683146, 1, 0.954513),
- Size = Vector3.new(size, size / 4, size),
- ClassName = "SpawnLocation",
- },
- {
- CFrame = CFrame.new(0, -(size / 3), 0),
- },
- },
- }
- local function featureRNG(chunkmap, cx, cz, y)
- local chunk = chunkmap[cx][cz]
- for k, v in pairs(features) do
- local passable = true
- if chunk[5][k] and chunk[5][k] >= v[3] then
- passable = false
- end
- if y > features[k][2] and y < features[k][1] then
- passable = false
- end
- if math.random(1, v[4]) == 1 and passable then -- v[4] is valid, its just old untyped code
- chunk[5][k] = chunk[5][k] + 1
- return true, k
- end
- end
- return false
- end
- local function genChunk(cx, cz, x, z, chunkmap)
- local noisemap = perlin.noise(x, z, 0.05, seed, x * cx, z * cz)
- local mod = math.random(1, 100)
- math.randomseed(seed + cx + cz * mod)
- local noisebuf = buffer.create(4112 / 8)
- local offset = 0
- for ix = 1, 16 do
- for iz = 1, 16 do
- buffer.writeu16(noisebuf, offset / 8, noisemap[iz][ix] // 2 ^ -4)
- offset += 16
- local instance = worldmap[(x * cx) + ix][10][(z * cz) + iz]
- local v2 = noisemap[iz][ix]
- instance.CFrame = instance.CFrame * CFrame.new(0, math.clamp(math.round(v2 * 75), 0, 255), 0)
- worldmap[(x * cx) + ix][10][(z * cz) + iz] = nil
- worldmap[(x * cx) + ix][math.clamp(math.round(v2 * 75), 0, 255)][(z * cz) + iz] = instance
- instance.Color = Color3.fromRGB(
- math.clamp(math.round(v2 * 75), 0, 255),
- math.clamp(math.round(v2 * 75), 0, 255),
- math.clamp(math.round(v2 * 75), 0, 255)
- )
- instance.Transparency = 0 -- this is awful code dont touch it tho....
- local gain, featureType = featureRNG(chunkmap, cx + 1, cz + 1, v2 * 75)
- if gain then
- print("Added world feature '" .. featureType .. "' to [" .. cx .. ", " .. cz .. "]")
- setBlock(
- instance.CFrame.X,
- instance.CFrame.Y + size,
- instance.CFrame.Z,
- features[featureType][5],
- features[featureType][6]
- )
- end
- end
- end
- buffer.writeu8(noisebuf, offset / 8, cx)
- buffer.writeu8(noisebuf, (offset + 8) / 8, cz)
- rf.ChunkRenderEvent:FireAllClients(noisebuf)
- table.insert(packetCache, { packetType = "chunkRender", ["buffer"] = noisebuf })
- end
- local function genGrid(cx, cz, x, z)
- rf.GridCreationEvent:FireAllClients(cx, cz, x, z)
- table.insert(packetCache, { packetType = "gridCreation", chunkData = { cx, cz, x, z } })
- for ix = 1, 16 do
- for iz = 1, 16 do
- setBlock(
- ((x * cx) + ix) * size,
- 10,
- ((z * cz) + iz) * size,
- { ["Transparency"] = 1, ["Color"] = Color3.new(0, 0, 0) },
- nil,
- true,
- false
- )
- end
- end
- end
- local function chunkWrapper(t, i, v, chunkmap)
- print("Generating chunk [" .. v[1] .. ", " .. v[2] .. "]...")
- print("Generating part grid...")
- genGrid(v[1] - 1, v[2] - 1, v[3], v[4])
- genChunk(v[1] - 1, v[2] - 1, v[3], v[4], chunkmap)
- chunkmap[v[1]][v[2]][6] = true
- print("Finished generating chunk.")
- end
- local chunkmap = perlin.createNDarray(2) -- format of a chunk is chunkmap[cx][cy] = {cx, cy, x, y, features, loaded} - FEATURES ARE ADDED IN BY CODE, DONT TRY SHOVING THEM IN MANUALLY
- local function processAll()
- print("Processing all chunks.")
- processing = true
- rf.ConfigEvent:FireAllClients(size)
- table.insert(packetCache, { packetType = "configEvent", size = size })
- for k, v in ipairs(chunkmap) do
- for k2, v2 in ipairs(v) do
- task.wait() -- my pc is too slow to run without waiting
- if not v2[6] then
- chunkWrapper(v, k2, v2, chunkmap)
- end
- end
- end
- end
- local function process(cx, cy)
- print("Processing chunk [" .. cx .. ", " .. cy .. "]...")
- chunkWrapper(chunkmap[cx], cy, chunkmap[cx][cy], chunkmap)
- end
- local featlist = {}
- for k, v in pairs(features) do
- featlist[k] = 0
- end
- local function regenerateWorld(wx, wz, wsize, wseed)
- x = wx
- z = wz
- size = wsize
- seed = perlin.calcseed(wseed)
- workspace.worldmap:Destroy()
- local worldfolder = Instance.new("Folder")
- worldfolder.Parent = workspace
- worldfolder.Name = "worldmap"
- chunkmap = perlin.createNDarray(2)
- worldmap = perlin.createNDarray(3)
- print("Regenerating world...")
- for cx = 1, x / 16 do
- for cy = 1, z / 16 do
- print("Added chunk [" .. cx .. ", " .. cy .. "] to chunkmap.")
- chunkmap[cx][cy] = { cx, cy, 16, 16, setmetatable({}, { __index = featlist }) }
- end
- end
- processAll()
- end
- for cx = 1, x / 16 do
- for cy = 1, z / 16 do
- print("Added chunk [" .. cx .. ", " .. cy .. "] to chunkmap.")
- chunkmap[cx][cy] = { cx, cy, 16, 16, setmetatable({}, { __index = featlist }) }
- end
- end
- players.PlayerAdded:Connect(function(plr)
- plr.CharacterAppearanceLoaded:Connect(function()
- if not processing then
- processAll()
- print("Finished!")
- else
- local curpos = #packetCache
- for k, v in ipairs(packetCache) do
- if k > curpos then
- break
- end
- if v.packetType == "blockRender" then
- rf.RenderEvent:FireClient(plr, v["buffer"], v.partType)
- elseif v.packetType == "chunkRender" then
- rf.ChunkRenderEvent:FireClient(plr, v["buffer"])
- elseif v.packetType == "gridCreation" then
- rf.GridCreationEvent:FireClient(
- plr,
- v["chunkData"][1],
- v["chunkData"][2],
- v["chunkData"][3],
- v["chunkData"][4]
- )
- elseif v.packetType == "configEvent" then
- rf.ConfigEvent:FireClient(plr, v.size)
- end
- end
- end
- end)
- end)
- _G.chunkman = {
- processAll = processAll,
- chunkmap = chunkmap,
- setBlock = setBlock,
- process = process,
- regenerateWorld = regenerateWorld,
- }
Advertisement
Add Comment
Please, Sign In to add comment