cepmoc

Untitled

Feb 28th, 2026
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.85 KB | None | 0 0
  1. --!native
  2. local perlin = require(game:GetService("ServerStorage").noise)
  3. local players = game:GetService("Players")
  4. local rf = game:GetService("ReplicatedStorage")
  5. local seed = perlin.calcseed("xxxThebestxThe_2nd") -- text doesnt mean anything past a seed
  6. local worldmap = perlin.createNDarray(3)
  7. local x, z, size = 1024, 1024, 5 -- is this for 8 chunks, 16 chunks give or take
  8. local processing = false
  9. local packetCache = {}
  10.  
  11. local function setBlock(x, y, z, prop, addprop, divsize, sendpacket)
  12.     if sendpacket == false then
  13.         sendpacket = false
  14.     else
  15.         sendpacket = true
  16.     end
  17.     local parttype
  18.     if prop["ClassName"] then
  19.         parttype = prop["ClassName"]
  20.         prop["ClassName"] = nil
  21.     end
  22.     local instance = {}
  23.     instance.Parent = workspace.worldmap
  24.     instance.Size = Vector3.new(size, size, size)
  25.     instance.CFrame = CFrame.new(x, y, z)
  26.     instance.Color = Color3.new(0, 0, 0)
  27.     instance.Material = Enum.Material.SmoothPlastic
  28.     instance.CastShadow = false
  29.     instance.Anchored = true
  30.     for k, v in pairs(prop) do
  31.         instance[k] = v
  32.     end
  33.     if addprop then
  34.         for k, v in pairs(addprop) do
  35.             if typeof(v) == "CFrame" then
  36.                 instance[k] = instance[k] * v
  37.             else
  38.                 instance[k] = instance[k] + v
  39.             end
  40.         end
  41.     end
  42.     if divsize then
  43.         worldmap[x / size][y][z / size] = instance
  44.     else
  45.         worldmap[x][y][z] = instance
  46.     end
  47.     if sendpacket then
  48.         local buf = buffer.create(112 / 8) -- (u16,f32,u16) (u8,u8,u8) (u8,u8,u8)
  49.         buffer.writeu16(buf, 0, x)
  50.         buffer.writef32(buf, 16 / 8, y)
  51.         buffer.writeu16(buf, 48 / 8, z)
  52.         buffer.writeu8(buf, 64 / 8, instance.Size.X)
  53.         buffer.writeu8(buf, 72 / 8, instance.Size.Y)
  54.         buffer.writeu8(buf, 80 / 8, instance.Size.Z)
  55.         buffer.writeu8(buf, 88 / 8, math.round(instance.Color.R * 255))
  56.         buffer.writeu8(buf, 96 / 8, math.round(instance.Color.G * 255))
  57.         buffer.writeu8(buf, 104 / 8, math.round(instance.Color.B * 255))
  58.         rf.RenderEvent:FireAllClients(buf, parttype)
  59.         table.insert(packetCache, { packetType = "blockRender", ["buffer"] = buf, partType = parttype })
  60.     end
  61. end
  62.  
  63. local features = {
  64.     ["random-orange"] = { -- min y, max y, max per chunk, rarity, properties, addprop
  65.         50 + 10,
  66.         100 + 10,
  67.         1,
  68.         500,
  69.         {
  70.             Color = Color3.new(1, 0.756008, 0.322225),
  71.             Size = Vector3.new(size, size * 5, size),
  72.         },
  73.         {
  74.             CFrame = CFrame.new(0, size * 2, 0),
  75.         },
  76.     },
  77.     ["spawnpoint"] = {
  78.         20 + 10,
  79.         70 + 10,
  80.         1,
  81.         1250,
  82.         {
  83.             Color = Color3.new(0.0683146, 1, 0.954513),
  84.             Size = Vector3.new(size, size / 4, size),
  85.             ClassName = "SpawnLocation",
  86.         },
  87.         {
  88.             CFrame = CFrame.new(0, -(size / 3), 0),
  89.         },
  90.     },
  91. }
  92.  
  93. local function featureRNG(chunkmap, cx, cz, y)
  94.     local chunk = chunkmap[cx][cz]
  95.     for k, v in pairs(features) do
  96.         local passable = true
  97.         if chunk[5][k] and chunk[5][k] >= v[3] then
  98.             passable = false
  99.         end
  100.         if y > features[k][2] and y < features[k][1] then
  101.             passable = false
  102.         end
  103.         if math.random(1, v[4]) == 1 and passable then -- v[4] is valid, its just old untyped code
  104.             chunk[5][k] = chunk[5][k] + 1
  105.             return true, k
  106.         end
  107.     end
  108.     return false
  109. end
  110.  
  111. local function genChunk(cx, cz, x, z, chunkmap)
  112.     local noisemap = perlin.noise(x, z, 0.05, seed, x * cx, z * cz)
  113.     local mod = math.random(1, 100)
  114.     math.randomseed(seed + cx + cz * mod)
  115.     local noisebuf = buffer.create(4112 / 8)
  116.     local offset = 0
  117.     for ix = 1, 16 do
  118.         for iz = 1, 16 do
  119.             buffer.writeu16(noisebuf, offset / 8, noisemap[iz][ix] // 2 ^ -4)
  120.             offset += 16
  121.             local instance = worldmap[(x * cx) + ix][10][(z * cz) + iz]
  122.             local v2 = noisemap[iz][ix]
  123.             instance.CFrame = instance.CFrame * CFrame.new(0, math.clamp(math.round(v2 * 75), 0, 255), 0)
  124.             worldmap[(x * cx) + ix][10][(z * cz) + iz] = nil
  125.             worldmap[(x * cx) + ix][math.clamp(math.round(v2 * 75), 0, 255)][(z * cz) + iz] = instance
  126.             instance.Color = Color3.fromRGB(
  127.                 math.clamp(math.round(v2 * 75), 0, 255),
  128.                 math.clamp(math.round(v2 * 75), 0, 255),
  129.                 math.clamp(math.round(v2 * 75), 0, 255)
  130.             )
  131.             instance.Transparency = 0 -- this is awful code dont touch it tho....
  132.             local gain, featureType = featureRNG(chunkmap, cx + 1, cz + 1, v2 * 75)
  133.             if gain then
  134.                 print("Added world feature '" .. featureType .. "' to [" .. cx .. ", " .. cz .. "]")
  135.                 setBlock(
  136.                     instance.CFrame.X,
  137.                     instance.CFrame.Y + size,
  138.                     instance.CFrame.Z,
  139.                     features[featureType][5],
  140.                     features[featureType][6]
  141.                 )
  142.             end
  143.         end
  144.     end
  145.     buffer.writeu8(noisebuf, offset / 8, cx)
  146.     buffer.writeu8(noisebuf, (offset + 8) / 8, cz)
  147.     rf.ChunkRenderEvent:FireAllClients(noisebuf)
  148.     table.insert(packetCache, { packetType = "chunkRender", ["buffer"] = noisebuf })
  149. end
  150.  
  151. local function genGrid(cx, cz, x, z)
  152.     rf.GridCreationEvent:FireAllClients(cx, cz, x, z)
  153.     table.insert(packetCache, { packetType = "gridCreation", chunkData = { cx, cz, x, z } })
  154.     for ix = 1, 16 do
  155.         for iz = 1, 16 do
  156.             setBlock(
  157.                 ((x * cx) + ix) * size,
  158.                 10,
  159.                 ((z * cz) + iz) * size,
  160.                 { ["Transparency"] = 1, ["Color"] = Color3.new(0, 0, 0) },
  161.                 nil,
  162.                 true,
  163.                 false
  164.             )
  165.         end
  166.     end
  167. end
  168.  
  169. local function chunkWrapper(t, i, v, chunkmap)
  170.     print("Generating chunk [" .. v[1] .. ", " .. v[2] .. "]...")
  171.     print("Generating part grid...")
  172.     genGrid(v[1] - 1, v[2] - 1, v[3], v[4])
  173.     genChunk(v[1] - 1, v[2] - 1, v[3], v[4], chunkmap)
  174.     chunkmap[v[1]][v[2]][6] = true
  175.     print("Finished generating chunk.")
  176. end
  177.  
  178. 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
  179. local function processAll()
  180.     print("Processing all chunks.")
  181.     processing = true
  182.     rf.ConfigEvent:FireAllClients(size)
  183.     table.insert(packetCache, { packetType = "configEvent", size = size })
  184.     for k, v in ipairs(chunkmap) do
  185.         for k2, v2 in ipairs(v) do
  186.             task.wait() -- my pc is too slow to run without waiting
  187.             if not v2[6] then
  188.                 chunkWrapper(v, k2, v2, chunkmap)
  189.             end
  190.         end
  191.     end
  192. end
  193.  
  194. local function process(cx, cy)
  195.     print("Processing chunk [" .. cx .. ", " .. cy .. "]...")
  196.     chunkWrapper(chunkmap[cx], cy, chunkmap[cx][cy], chunkmap)
  197. end
  198.  
  199. local featlist = {}
  200. for k, v in pairs(features) do
  201.     featlist[k] = 0
  202. end
  203.  
  204. local function regenerateWorld(wx, wz, wsize, wseed)
  205.     x = wx
  206.     z = wz
  207.     size = wsize
  208.     seed = perlin.calcseed(wseed)
  209.     workspace.worldmap:Destroy()
  210.     local worldfolder = Instance.new("Folder")
  211.     worldfolder.Parent = workspace
  212.     worldfolder.Name = "worldmap"
  213.     chunkmap = perlin.createNDarray(2)
  214.     worldmap = perlin.createNDarray(3)
  215.     print("Regenerating world...")
  216.     for cx = 1, x / 16 do
  217.         for cy = 1, z / 16 do
  218.             print("Added chunk [" .. cx .. ", " .. cy .. "] to chunkmap.")
  219.             chunkmap[cx][cy] = { cx, cy, 16, 16, setmetatable({}, { __index = featlist }) }
  220.         end
  221.     end
  222.     processAll()
  223. end
  224.  
  225. for cx = 1, x / 16 do
  226.     for cy = 1, z / 16 do
  227.         print("Added chunk [" .. cx .. ", " .. cy .. "] to chunkmap.")
  228.         chunkmap[cx][cy] = { cx, cy, 16, 16, setmetatable({}, { __index = featlist }) }
  229.     end
  230. end
  231.  
  232. players.PlayerAdded:Connect(function(plr)
  233.     plr.CharacterAppearanceLoaded:Connect(function()
  234.         if not processing then
  235.             processAll()
  236.             print("Finished!")
  237.         else
  238.             local curpos = #packetCache
  239.             for k, v in ipairs(packetCache) do
  240.                 if k > curpos then
  241.                     break
  242.                 end
  243.                 if v.packetType == "blockRender" then
  244.                     rf.RenderEvent:FireClient(plr, v["buffer"], v.partType)
  245.                 elseif v.packetType == "chunkRender" then
  246.                     rf.ChunkRenderEvent:FireClient(plr, v["buffer"])
  247.                 elseif v.packetType == "gridCreation" then
  248.                     rf.GridCreationEvent:FireClient(
  249.                         plr,
  250.                         v["chunkData"][1],
  251.                         v["chunkData"][2],
  252.                         v["chunkData"][3],
  253.                         v["chunkData"][4]
  254.                     )
  255.                 elseif v.packetType == "configEvent" then
  256.                     rf.ConfigEvent:FireClient(plr, v.size)
  257.                 end
  258.             end
  259.         end
  260.     end)
  261. end)
  262.  
  263. _G.chunkman = {
  264.     processAll = processAll,
  265.     chunkmap = chunkmap,
  266.     setBlock = setBlock,
  267.     process = process,
  268.     regenerateWorld = regenerateWorld,
  269. }
Advertisement
Add Comment
Please, Sign In to add comment